Skip to content

Commit 9a09eb7

Browse files
committed
[NFC] Replace three fields in UnboundImport with a PointerUnion
It’s not often that a PointerUnion makes the code *more* safe and clear, so let’s take it.
1 parent 9adaddc commit 9a09eb7

File tree

1 file changed

+32
-36
lines changed

1 file changed

+32
-36
lines changed

lib/Sema/NameBinding.cpp

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -81,32 +81,25 @@ namespace {
8181
/// \c AccessPathTy is the common currency type for this.)
8282
ModuleDecl::AccessPathTy declPath;
8383

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.
9889
///
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;
10393

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+
}
10798

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+
}
110103

111104
/// Create an UnboundImport for a user-written import declaration.
112105
explicit UnboundImport(ImportDecl *ID);
@@ -394,18 +387,20 @@ void NameBinder::visitImportDecl(ImportDecl *ID) {
394387
}
395388

396389
void NameBinder::bindImport(UnboundImport &&I) {
390+
auto ID = I.getImportDecl();
391+
397392
if (!I.checkNotTautological(SF)) {
398393
// 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());
401396
return;
402397
}
403398

404399
ModuleDecl *M = getModule(I.modulePath);
405400
if (!I.checkModuleLoaded(M, SF)) {
406401
// 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);
409404
return;
410405
}
411406

@@ -426,8 +421,8 @@ void NameBinder::bindImport(UnboundImport &&I) {
426421

427422
crossImport(M, I);
428423

429-
if (I.ID)
430-
I.ID.get()->setModule(M);
424+
if (ID)
425+
ID.get()->setModule(M);
431426
}
432427

433428
void NameBinder::addImport(const UnboundImport &I, ModuleDecl *M,
@@ -502,7 +497,7 @@ UnboundImport::UnboundImport(ImportDecl *ID)
502497
: importLoc(ID->getLoc()), options(), privateImportFileName(),
503498
importKind(ID->getImportKind(), ID->getKindLoc()),
504499
modulePath(ID->getModulePath()), declPath(ID->getDeclPath()),
505-
ID(ID), attrs(&ID->getAttrs()), underlyingModule()
500+
importOrUnderlyingModuleDecl(ID)
506501
{
507502
if (ID->isExported())
508503
options |= ImportFlags::Exported;
@@ -660,8 +655,9 @@ void UnboundImport::diagnoseInvalidAttr(DeclAttrKind attrKind,
660655
auto diag = diags.diagnose(modulePath.front().Loc, diagID,
661656
modulePath.front().Item);
662657

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);
665661
if (!attr) return;
666662

667663
diag.fixItRemove(attr->getRangeWithAt());
@@ -819,7 +815,7 @@ void BoundImport::validateScope(SourceFile &SF) {
819815
decls.front()->getFullName());
820816
}
821817

822-
unbound.ID.get()->setDecls(ctx.AllocateCopy(decls));
818+
unbound.getImportDecl().get()->setDecls(ctx.AllocateCopy(decls));
823819
}
824820

825821
//===----------------------------------------------------------------------===//
@@ -847,7 +843,7 @@ UnboundImport::UnboundImport(ASTContext &ctx,
847843
// BoundImport::validateScope() from unnecessarily revalidating the
848844
// scope.
849845
declPath(declaringImport.module.first),
850-
ID(nullptr), attrs(nullptr), underlyingModule(declaringImport.module.second)
846+
importOrUnderlyingModuleDecl(declaringImport.module.second)
851847
{
852848
modulePath = ctx.AllocateCopy(
853849
ModuleDecl::AccessPathTy( { overlayName, base.modulePath[0].Loc }));
@@ -891,9 +887,9 @@ void NameBinder::crossImport(ModuleDecl *M, UnboundImport &I) {
891887
if (!SF.shouldCrossImport())
892888
return;
893889

894-
if (I.underlyingModule)
890+
if (I.getUnderlyingModule())
895891
// FIXME: Should we warn if M doesn't reexport underlyingModule?
896-
SF.addSeparatelyImportedOverlay(M, I.underlyingModule.get());
892+
SF.addSeparatelyImportedOverlay(M, I.getUnderlyingModule().get());
897893

898894
// FIXME: Most of the comparisons we do here are probably unnecessary. We
899895
// only need to findCrossImports() on pairs where at least one of the two

0 commit comments

Comments
 (0)