Skip to content

Commit edbb75e

Browse files
committed
Eliminate the 'Module' variant from UnqualifiedLookupResult.
Make unqualified lookup always provide a declaration for the things it finds, rather than providing either a module or a declaration. Unify various code paths in our type checker now that module declarations come in with the other declarations. Swift SVN r28286
1 parent 2653a65 commit edbb75e

13 files changed

+147
-245
lines changed

include/swift/AST/NameLookup.h

+2-27
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ namespace swift {
3838
struct UnqualifiedLookupResult {
3939
private:
4040
ValueDecl *Base;
41-
union {
42-
ValueDecl *Value;
43-
Module *NamedModule;
44-
};
41+
ValueDecl *Value;
4542

4643
public:
4744
/// Kind - The kind of reference.
@@ -77,27 +74,13 @@ struct UnqualifiedLookupResult {
7774
/// MetaArchetypeMember - "x" refers to a member of the metatype of an
7875
/// archetype type, which is referred to by BaseDecl. The base is evaluated
7976
/// and ignored.
80-
MetaArchetypeMember,
81-
82-
/// ModuleName - "x" refers to a module, either the current
83-
/// module or an imported module.
84-
ModuleName
77+
MetaArchetypeMember
8578
} Kind;
8679

87-
bool hasValueDecl() const {
88-
return Kind != ModuleName;
89-
}
90-
9180
ValueDecl *getValueDecl() const {
92-
assert(hasValueDecl());
9381
return Value;
9482
}
9583

96-
Module *getNamedModule() const {
97-
assert(Kind == ModuleName);
98-
return NamedModule;
99-
}
100-
10184
ValueDecl *getBaseDecl() const {
10285
return Base;
10386
}
@@ -171,14 +154,6 @@ struct UnqualifiedLookupResult {
171154
R.Kind = MetaArchetypeMember;
172155
return R;
173156
}
174-
175-
static UnqualifiedLookupResult getModuleName(Module *m) {
176-
UnqualifiedLookupResult R;
177-
R.Base = nullptr;
178-
R.NamedModule = m;
179-
R.Kind = ModuleName;
180-
return R;
181-
}
182157
};
183158

184159
/// \brief This class implements and represents the result of performing

lib/AST/NameLookup.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ UnqualifiedLookup::UnqualifiedLookup(DeclName Name, DeclContext *DC,
739739

740740
// Look for a module with the given name.
741741
if (Name.isSimpleName(M.getName())) {
742-
Results.push_back(Result::getModuleName(&M));
742+
Results.push_back(Result::getModuleMember(&M));
743743
return;
744744
}
745745

@@ -749,7 +749,7 @@ UnqualifiedLookup::UnqualifiedLookup(DeclName Name, DeclContext *DC,
749749
if (desiredModule) {
750750
forAllVisibleModules(DC, [&](const Module::ImportedModule &import) -> bool {
751751
if (import.second == desiredModule) {
752-
Results.push_back(Result::getModuleName(import.second));
752+
Results.push_back(Result::getModuleMember(import.second));
753753
return false;
754754
}
755755
return true;
@@ -758,7 +758,7 @@ UnqualifiedLookup::UnqualifiedLookup(DeclName Name, DeclContext *DC,
758758
}
759759

760760
TypeDecl* UnqualifiedLookup::getSingleTypeResult() {
761-
if (Results.size() != 1 || !Results.back().hasValueDecl())
761+
if (Results.size() != 1)
762762
return nullptr;
763763
return dyn_cast<TypeDecl>(Results.back().getValueDecl());
764764
}

lib/AST/TypeRepr.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,9 @@ void ComponentIdentTypeRepr::printImpl(ASTPrinter &Printer,
265265
if (Module *Mod = getBoundModule()) {
266266
Printer.printModuleRef(Mod, getIdentifier());
267267
} else if (Type Ty = getBoundType()) {
268-
if (auto NTD = Ty->getAnyNominal())
268+
if (auto ModuleTy = dyn_cast<ModuleType>(Ty.getPointer()))
269+
Printer.printModuleRef(ModuleTy->getModule(), getIdentifier());
270+
else if (auto NTD = Ty->getAnyNominal())
269271
Printer.printTypeRef(NTD, getIdentifier());
270272
else
271273
Printer << getIdentifier().str();

lib/ClangImporter/ImportDecl.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -551,9 +551,6 @@ getOperatorRef(ASTContext &C, Identifier name) {
551551

552552
SmallVector<ValueDecl *, 4> found;
553553
for (auto &result : lookup.Results) {
554-
if (!result.hasValueDecl())
555-
continue;
556-
557554
if (!isa<FuncDecl>(result.getValueDecl()))
558555
continue;
559556

lib/IDE/SourceEntityWalker.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,13 @@ bool SemaAnnotator::walkToTypeReprPre(TypeRepr *T) {
228228
if (auto IdT = dyn_cast<ComponentIdentTypeRepr>(T)) {
229229
if (ValueDecl *VD = IdT->getBoundDecl())
230230
return passReference(VD, IdT->getIdLoc());
231-
if (TypeDecl *TyD = getTypeDecl(IdT->getBoundType()))
231+
if (TypeDecl *TyD = getTypeDecl(IdT->getBoundType())) {
232+
if (ModuleDecl *ModD = dyn_cast<ModuleDecl>(TyD))
233+
return passReference(ModD, std::make_pair(IdT->getIdentifier(),
234+
IdT->getIdLoc()));
235+
232236
return passReference(TyD, IdT->getIdLoc());
237+
}
233238
if (auto Mod = IdT->getBoundModule())
234239
return passReference(Mod, std::make_pair(IdT->getIdentifier(),
235240
IdT->getIdLoc()));
@@ -367,6 +372,9 @@ TypeDecl *SemaAnnotator::getTypeDecl(Type Ty) {
367372

368373
if (NameAliasType *NAT = dyn_cast<NameAliasType>(Ty.getPointer()))
369374
return NAT->getDecl();
375+
if (ModuleType *MT = dyn_cast<ModuleType>(Ty.getPointer()))
376+
return MT->getModule();
377+
370378
return Ty->getAnyNominal();
371379
}
372380

lib/Immediate/Immediate.cpp

+13-15
Original file line numberDiff line numberDiff line change
@@ -1330,24 +1330,22 @@ class REPLEnvironment {
13301330
UnqualifiedLookup lookup(ctx.getIdentifier(Tok.getText()),
13311331
&REPLInputFile, nullptr);
13321332
for (auto result : lookup.Results) {
1333-
if (result.hasValueDecl()) {
1334-
printOrDumpDecl(result.getValueDecl(), doPrint);
1333+
printOrDumpDecl(result.getValueDecl(), doPrint);
13351334

1336-
if (auto typeDecl = dyn_cast<TypeDecl>(result.getValueDecl())) {
1337-
if (auto typeAliasDecl = dyn_cast<TypeAliasDecl>(typeDecl)) {
1338-
TypeDecl *origTypeDecl = typeAliasDecl->getUnderlyingType()
1339-
->getNominalOrBoundGenericNominal();
1340-
if (origTypeDecl) {
1341-
printOrDumpDecl(origTypeDecl, doPrint);
1342-
typeDecl = origTypeDecl;
1343-
}
1335+
if (auto typeDecl = dyn_cast<TypeDecl>(result.getValueDecl())) {
1336+
if (auto typeAliasDecl = dyn_cast<TypeAliasDecl>(typeDecl)) {
1337+
TypeDecl *origTypeDecl = typeAliasDecl->getUnderlyingType()
1338+
->getNominalOrBoundGenericNominal();
1339+
if (origTypeDecl) {
1340+
printOrDumpDecl(origTypeDecl, doPrint);
1341+
typeDecl = origTypeDecl;
13441342
}
1343+
}
13451344

1346-
// Print extensions.
1347-
if (auto nominal = dyn_cast<NominalTypeDecl>(typeDecl)) {
1348-
for (auto extension : nominal->getExtensions()) {
1349-
printOrDumpDecl(extension, doPrint);
1350-
}
1345+
// Print extensions.
1346+
if (auto nominal = dyn_cast<NominalTypeDecl>(typeDecl)) {
1347+
for (auto extension : nominal->getExtensions()) {
1348+
printOrDumpDecl(extension, doPrint);
13511349
}
13521350
}
13531351
}

lib/Parse/ParseSIL.cpp

-5
Original file line numberDiff line numberDiff line change
@@ -763,11 +763,6 @@ static llvm::PointerUnion<ValueDecl*, Module*> lookupTopDecl(Parser &P,
763763
SourceFile::Parsed);
764764
UnqualifiedLookup DeclLookup(Name, &P.SF, nullptr);
765765
assert(DeclLookup.isSuccess() && DeclLookup.Results.size() == 1);
766-
if (DeclLookup.Results.back().Kind == UnqualifiedLookupResult::ModuleName) {
767-
Module *Mod = DeclLookup.Results.back().getNamedModule();
768-
return Mod;
769-
}
770-
assert(DeclLookup.Results.back().hasValueDecl());
771766
ValueDecl *VD = DeclLookup.Results.back().getValueDecl();
772767
return VD;
773768
}

lib/Sema/TypeCheckAttr.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -1071,17 +1071,15 @@ void AttributeChecker::checkApplicationMainAttribute(DeclAttribute *attr,
10711071
/*NonCascading=*/true, SourceLoc(),
10721072
/*IsType=*/false);
10731073
for (const auto &result : lookupMain.Results) {
1074-
if (result.hasValueDecl())
1075-
TC.validateDecl(result.getValueDecl());
1074+
TC.validateDecl(result.getValueDecl());
10761075
}
10771076
auto Foundation = TC.Context.getLoadedModule(C.getIdentifier("Foundation"));
10781077
if (Foundation) {
10791078
UnqualifiedLookup lookupString(C.getIdentifier("NSStringFromClass"),
10801079
Foundation, nullptr, /*NonCascading=*/true,
10811080
SourceLoc(), /*IsType=*/false);
10821081
for (const auto &result : lookupString.Results) {
1083-
if (result.hasValueDecl())
1084-
TC.validateDecl(result.getValueDecl());
1082+
TC.validateDecl(result.getValueDecl());
10851083
}
10861084
}
10871085
}

lib/Sema/TypeCheckConstraints.cpp

+8-15
Original file line numberDiff line numberDiff line change
@@ -299,22 +299,10 @@ static Expr *BindName(UnresolvedDeclRefExpr *UDRE, DeclContext *Context,
299299

300300
// FIXME: Need to refactor the way we build an AST node from a lookup result!
301301

302-
if (!Lookup.Results.empty() &&
303-
Lookup.Results[0].Kind == UnqualifiedLookupResult::ModuleName) {
304-
assert(Lookup.Results.size() == 1 && "module names should be unique");
305-
auto module = Lookup.Results[0].getNamedModule();
306-
return new (TC.Context) DeclRefExpr(module, Loc, /*Implicit=*/false,
307-
AccessSemantics::Ordinary,
308-
module->getDeclaredType());
309-
}
310-
311302
bool AllDeclRefs = true;
312303
SmallVector<ValueDecl*, 4> ResultValues;
313304
for (auto Result : Lookup.Results) {
314305
switch (Result.Kind) {
315-
case UnqualifiedLookupResult::ModuleName:
316-
llvm_unreachable("handled above");
317-
318306
case UnqualifiedLookupResult::MemberProperty:
319307
case UnqualifiedLookupResult::MemberFunction:
320308
case UnqualifiedLookupResult::MetatypeMember:
@@ -353,6 +341,14 @@ static Expr *BindName(UnresolvedDeclRefExpr *UDRE, DeclContext *Context,
353341
if (!UDRE->isSpecialized() &&
354342
ResultValues.size() == 1 && UDRE->getRefKind() == DeclRefKind::Ordinary &&
355343
isa<TypeDecl>(ResultValues[0])) {
344+
// FIXME: This is odd.
345+
if (isa<ModuleDecl>(ResultValues[0])) {
346+
return new (TC.Context) DeclRefExpr(ResultValues[0], Loc,
347+
/*implicit=*/false,
348+
AccessSemantics::Ordinary,
349+
ResultValues[0]->getType());
350+
}
351+
356352
return TypeExpr::createForDecl(Loc, cast<TypeDecl>(ResultValues[0]));
357353
}
358354

@@ -410,7 +406,6 @@ static Expr *BindName(UnresolvedDeclRefExpr *UDRE, DeclContext *Context,
410406
break;
411407
case UnqualifiedLookupResult::ModuleMember:
412408
case UnqualifiedLookupResult::LocalDecl:
413-
case UnqualifiedLookupResult::ModuleName:
414409
AllMemberRefs = false;
415410
break;
416411
case UnqualifiedLookupResult::MetaArchetypeMember:
@@ -1627,8 +1622,6 @@ bool TypeChecker::typeCheckExprPattern(ExprPattern *EP, DeclContext *DC,
16271622

16281623
SmallVector<ValueDecl*, 4> choices;
16291624
for (auto &result : matchLookup.Results) {
1630-
if (!result.hasValueDecl())
1631-
continue;
16321625
choices.push_back(result.getValueDecl());
16331626
}
16341627

lib/Sema/TypeCheckPattern.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ lookupUnqualifiedEnumMemberElement(TypeChecker &TC, DeclContext *DC,
3838
// See if there is any enum element in there.
3939
EnumElementDecl *foundElement = nullptr;
4040
for (auto result : lookup.Results) {
41-
if (!result.hasValueDecl())
42-
continue;
4341
auto *oe = dyn_cast<EnumElementDecl>(result.getValueDecl());
4442
if (!oe)
4543
continue;

lib/Sema/TypeCheckProtocol.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -1923,7 +1923,6 @@ ConformanceChecker::lookupValueWitnesses(ValueDecl *req, bool *ignoringNames) {
19231923

19241924
if (lookup.isSuccess()) {
19251925
for (auto candidate : lookup.Results) {
1926-
assert(candidate.hasValueDecl());
19271926
witnesses.push_back(candidate.getValueDecl());
19281927
}
19291928
}

0 commit comments

Comments
 (0)