@@ -81,32 +81,25 @@ namespace {
81
81
// / \c AccessPathTy is the common currency type for this.)
82
82
ModuleDecl::AccessPathTy declPath;
83
83
84
- //
85
- // Set only on UnboundImports that represent physical ImportDecls:
86
- //
87
-
88
- // / If this UnboundImport directly represents an ImportDecl, the ImportDecl
89
- // / it represents.
90
- // /
91
- // / This property should only be used to control where information is added
92
- // / to the AST. Don't retrieve information directly from the ImportDecl; use
93
- // / the other member variables.
94
- NullablePtr<ImportDecl> ID;
95
-
96
- // / If this UnboundImport directly represents an ImportDecl, the attributes
97
- // / of that ImportDecl.
84
+ // / If this UnboundImport directly represents an ImportDecl, contains the
85
+ // / ImportDecl it represents. This should only be used for diagnostics and
86
+ // / for updating the AST; if you want to read information about the import,
87
+ // / get it from the other fields in \c UnboundImport rather than from the
88
+ // / \c ImportDecl.
98
89
// /
99
- // / This property should only be used to improve diagnostics. Don't pull
100
- // / information about the import's attributes from this property; use
101
- // / \c options instead.
102
- NullablePtr<DeclAttributes> attrs;
90
+ // / If this UnboundImport represents a cross-import, contains the declaring
91
+ // / module's \c ModuleDecl.
92
+ PointerUnion<ImportDecl *, ModuleDecl *> importOrUnderlyingModuleDecl;
103
93
104
- //
105
- // Set only on UnboundImports that represent cross-import overlays:
106
- //
94
+ NullablePtr<ImportDecl> getImportDecl () const {
95
+ return importOrUnderlyingModuleDecl.is <ImportDecl *>() ?
96
+ importOrUnderlyingModuleDecl.get <ImportDecl *>() : nullptr ;
97
+ }
107
98
108
- // / The module this cross-import overlay is overlaying.
109
- NullablePtr<ModuleDecl> underlyingModule;
99
+ NullablePtr<ModuleDecl> getUnderlyingModule () const {
100
+ return importOrUnderlyingModuleDecl.is <ModuleDecl *>() ?
101
+ importOrUnderlyingModuleDecl.get <ModuleDecl *>() : nullptr ;
102
+ }
110
103
111
104
// / Create an UnboundImport for a user-written import declaration.
112
105
explicit UnboundImport (ImportDecl *ID);
@@ -394,18 +387,20 @@ void NameBinder::visitImportDecl(ImportDecl *ID) {
394
387
}
395
388
396
389
void NameBinder::bindImport (UnboundImport &&I) {
390
+ auto ID = I.getImportDecl ();
391
+
397
392
if (!I.checkNotTautological (SF)) {
398
393
// No need to process this import further.
399
- if (I. ID )
400
- I. ID .get ()->setModule (SF.getParentModule ());
394
+ if (ID)
395
+ ID.get ()->setModule (SF.getParentModule ());
401
396
return ;
402
397
}
403
398
404
399
ModuleDecl *M = getModule (I.modulePath );
405
400
if (!I.checkModuleLoaded (M, SF)) {
406
401
// Can't process further. checkModuleLoaded() will have diagnosed this.
407
- if (I. ID )
408
- I. ID .get ()->setModule (nullptr );
402
+ if (ID)
403
+ ID.get ()->setModule (nullptr );
409
404
return ;
410
405
}
411
406
@@ -426,8 +421,8 @@ void NameBinder::bindImport(UnboundImport &&I) {
426
421
427
422
crossImport (M, I);
428
423
429
- if (I. ID )
430
- I. ID .get ()->setModule (M);
424
+ if (ID)
425
+ ID.get ()->setModule (M);
431
426
}
432
427
433
428
void NameBinder::addImport (const UnboundImport &I, ModuleDecl *M,
@@ -502,7 +497,7 @@ UnboundImport::UnboundImport(ImportDecl *ID)
502
497
: importLoc(ID->getLoc ()), options(), privateImportFileName(),
503
498
importKind(ID->getImportKind (), ID->getKindLoc()),
504
499
modulePath(ID->getModulePath ()), declPath(ID->getDeclPath ()),
505
- ID (ID), attrs(&ID-> getAttrs ()), underlyingModule( )
500
+ importOrUnderlyingModuleDecl (ID)
506
501
{
507
502
if (ID->isExported ())
508
503
options |= ImportFlags::Exported;
@@ -660,8 +655,9 @@ void UnboundImport::diagnoseInvalidAttr(DeclAttrKind attrKind,
660
655
auto diag = diags.diagnose (modulePath.front ().Loc , diagID,
661
656
modulePath.front ().Item );
662
657
663
- if (!attrs) return ;
664
- auto *attr = attrs.get ()->getAttribute (attrKind);
658
+ auto *ID = getImportDecl ().getPtrOrNull ();
659
+ if (!ID) return ;
660
+ auto *attr = ID->getAttrs ().getAttribute (attrKind);
665
661
if (!attr) return ;
666
662
667
663
diag.fixItRemove (attr->getRangeWithAt ());
@@ -819,7 +815,7 @@ void BoundImport::validateScope(SourceFile &SF) {
819
815
decls.front ()->getFullName ());
820
816
}
821
817
822
- unbound.ID .get ()->setDecls (ctx.AllocateCopy (decls));
818
+ unbound.getImportDecl () .get ()->setDecls (ctx.AllocateCopy (decls));
823
819
}
824
820
825
821
// ===----------------------------------------------------------------------===//
@@ -847,7 +843,7 @@ UnboundImport::UnboundImport(ASTContext &ctx,
847
843
// BoundImport::validateScope() from unnecessarily revalidating the
848
844
// scope.
849
845
declPath(declaringImport.module .first),
850
- ID( nullptr ), attrs( nullptr ), underlyingModule (declaringImport.module .second)
846
+ importOrUnderlyingModuleDecl (declaringImport.module .second)
851
847
{
852
848
modulePath = ctx.AllocateCopy (
853
849
ModuleDecl::AccessPathTy ( { overlayName, base.modulePath [0 ].Loc }));
@@ -891,9 +887,9 @@ void NameBinder::crossImport(ModuleDecl *M, UnboundImport &I) {
891
887
if (!SF.shouldCrossImport ())
892
888
return ;
893
889
894
- if (I.underlyingModule )
890
+ if (I.getUnderlyingModule () )
895
891
// FIXME: Should we warn if M doesn't reexport underlyingModule?
896
- SF.addSeparatelyImportedOverlay (M, I.underlyingModule .get ());
892
+ SF.addSeparatelyImportedOverlay (M, I.getUnderlyingModule () .get ());
897
893
898
894
// FIXME: Most of the comparisons we do here are probably unnecessary. We
899
895
// only need to findCrossImports() on pairs where at least one of the two
0 commit comments