Skip to content

Commit c0ccdb1

Browse files
authored
Change getBaseName to return DeclBaseName instead of Identifier (#9968)
This changes `getBaseName()` on `DeclName` to return a `DeclBaseName` instead of an `Identifier`. All places that will continue to be expecting an `Identifier` are changed to call `getBaseIdentifier` which will later assert that the `DeclName` is actually backed by an identifier and not a special name. For transitional purposes, a conversion operator from `DeclBaseName` to `Identifier` has been added that will be removed again once migration to DeclBaseName has been completed in other parts of the compiler. Unify approach to printing declaration names Printing a declaration's name using `<<` and `getBaseName()` is be independent of the return type of `getBaseName()` which will change in the future from `Identifier` to `DeclBaseName`
1 parent f302afc commit c0ccdb1

29 files changed

+125
-95
lines changed

include/swift/AST/Decl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2137,7 +2137,7 @@ class ValueDecl : public Decl {
21372137

21382138
/// Retrieve the base name of the declaration, ignoring any argument
21392139
/// names.
2140-
DeclName getBaseName() const { return Name.getBaseName(); }
2140+
DeclBaseName getBaseName() const { return Name.getBaseName(); }
21412141

21422142
/// Retrieve the name to use for this declaration when interoperating
21432143
/// with the Objective-C runtime.

include/swift/AST/Identifier.h

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,11 @@ class DeclBaseName {
250250
return Ident.get() < RHS.Ident.get();
251251
}
252252

253+
// TODO: Remove once migration to DeclBaseName has been completed
254+
operator Identifier() {
255+
return getIdentifier();
256+
}
257+
253258
const void *getAsOpaquePointer() const { return Ident.get(); }
254259

255260
static DeclBaseName getFromOpaquePointer(void *P) {
@@ -369,14 +374,19 @@ class DeclName {
369374
/// Retrieve the 'base' name, i.e., the name that follows the introducer,
370375
/// such as the 'foo' in 'func foo(x:Int, y:Int)' or the 'bar' in
371376
/// 'var bar: Int'.
372-
// TODO: Return DeclBaseName (remove two calls to getIdentifier)
373-
Identifier getBaseName() const {
377+
DeclBaseName getBaseName() const {
374378
if (auto compound = SimpleOrCompound.dyn_cast<CompoundDeclName*>())
375-
return compound->BaseName.getIdentifier();
379+
return compound->BaseName;
376380

377-
return SimpleOrCompound.get<BaseNameAndCompound>()
378-
.getPointer()
379-
.getIdentifier();
381+
return SimpleOrCompound.get<BaseNameAndCompound>().getPointer();
382+
}
383+
384+
/// Assert that the base name is not special and return its identifier.
385+
Identifier getBaseIdentifier() const {
386+
auto baseName = getBaseName();
387+
assert(!baseName.isSpecial() &&
388+
"Can't retrieve the identifier of a special base name");
389+
return baseName.getIdentifier();
380390
}
381391

382392
/// Retrieve the names of the arguments, if there are any.
@@ -387,6 +397,8 @@ class DeclName {
387397
return { };
388398
}
389399

400+
bool isSpecial() const { return getBaseName().isSpecial(); }
401+
390402
explicit operator bool() const {
391403
if (SimpleOrCompound.dyn_cast<CompoundDeclName*>())
392404
return true;
@@ -411,14 +423,20 @@ class DeclName {
411423

412424
/// True if this name is a simple one-component name identical to the
413425
/// given identifier.
414-
bool isSimpleName(Identifier name) const {
426+
bool isSimpleName(DeclBaseName name) const {
415427
return isSimpleName() && getBaseName() == name;
416428
}
417429

418430
/// True if this name is a simple one-component name equal to the
419431
/// given string.
420432
bool isSimpleName(StringRef name) const {
421-
return isSimpleName() && getBaseName().str().equals(name);
433+
if (!isSimpleName())
434+
return false;
435+
436+
if (getBaseName().isSpecial())
437+
return false;
438+
439+
return getBaseIdentifier().str().equals(name);
422440
}
423441

424442
/// True if this name is an operator.

lib/AST/ASTContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1937,7 +1937,7 @@ bool swift::fixDeclarationName(InFlightDiagnostic &diag, ValueDecl *decl,
19371937

19381938
// Fix the name of the function itself.
19391939
if (name.getBaseName() != targetName.getBaseName()) {
1940-
diag.fixItReplace(func->getLoc(), targetName.getBaseName().str());
1940+
diag.fixItReplace(func->getLoc(), targetName.getBaseIdentifier().str());
19411941
}
19421942

19431943
// Fix the argument names that need fixing.

lib/AST/Decl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4546,7 +4546,7 @@ ObjCSelector AbstractFunctionDecl::getObjCSelector(
45464546
if (argNames.size() != preferredName.getArgumentNames().size()) {
45474547
return ObjCSelector();
45484548
}
4549-
baseName = preferredName.getBaseName();
4549+
baseName = preferredName.getBaseIdentifier();
45504550
argNames = preferredName.getArgumentNames();
45514551
}
45524552

@@ -5135,7 +5135,7 @@ ConstructorDecl::getDelegatingOrChainedInitKind(DiagnosticEngine *diags,
51355135
} else if (auto *CRE = dyn_cast<ConstructorRefCallExpr>(Callee)) {
51365136
arg = CRE->getArg();
51375137
} else if (auto *dotExpr = dyn_cast<UnresolvedDotExpr>(Callee)) {
5138-
if (dotExpr->getName().getBaseName().str() != "init")
5138+
if (dotExpr->getName().getBaseName() != "init")
51395139
return { true, E };
51405140

51415141
arg = dotExpr->getBase();

lib/AST/Module.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ void ModuleDecl::lookupObjCMethods(
478478
void BuiltinUnit::lookupValue(ModuleDecl::AccessPathTy accessPath, DeclName name,
479479
NLKind lookupKind,
480480
SmallVectorImpl<ValueDecl*> &result) const {
481-
getCache().lookupValue(name.getBaseName(), lookupKind, *this, result);
481+
getCache().lookupValue(name.getBaseIdentifier(), lookupKind, *this, result);
482482
}
483483

484484
void BuiltinUnit::lookupObjCMethods(

lib/AST/NameLookup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ UnqualifiedLookup::UnqualifiedLookup(DeclName Name, DeclContext *DC,
943943
return;
944944
}
945945

946-
ModuleDecl *desiredModule = Ctx.getLoadedModule(Name.getBaseName());
946+
ModuleDecl *desiredModule = Ctx.getLoadedModule(Name.getBaseIdentifier());
947947
if (!desiredModule && Name == Ctx.TheBuiltinModule->getName())
948948
desiredModule = Ctx.TheBuiltinModule;
949949
if (desiredModule) {

lib/AST/SwiftNameTranslation.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,15 @@ getObjCNameForSwiftDecl(const ValueDecl *VD, DeclName PreferredName){
7777
return {Identifier(), FD->getObjCSelector(Resolver, PreferredName)};
7878
} else if (auto *VAD = dyn_cast<VarDecl>(VD)) {
7979
if (PreferredName)
80-
return {PreferredName.getBaseName(), ObjCSelector()};
80+
return {PreferredName.getBaseIdentifier(), ObjCSelector()};
8181
return {VAD->getObjCPropertyName(), ObjCSelector()};
8282
} else if (auto *SD = dyn_cast<SubscriptDecl>(VD)) {
8383
return getObjCNameForSwiftDecl(SD->getGetter(), PreferredName);
8484
} else if (auto *EL = dyn_cast<EnumElementDecl>(VD)) {
8585
SmallString<64> Buffer;
8686
{
8787
llvm::raw_svector_ostream OS(Buffer);
88-
printSwiftEnumElemNameInObjC(EL, OS, PreferredName.getBaseName());
88+
printSwiftEnumElemNameInObjC(EL, OS, PreferredName.getBaseIdentifier());
8989
}
9090
return {Ctx.getIdentifier(Buffer.str()), ObjCSelector()};
9191
} else {
@@ -94,7 +94,7 @@ getObjCNameForSwiftDecl(const ValueDecl *VD, DeclName PreferredName){
9494
if (!Name.empty())
9595
return {Ctx.getIdentifier(Name), ObjCSelector()};
9696
if (!PreferredName.getBaseName().empty())
97-
return {PreferredName.getBaseName(), ObjCSelector()};
97+
return {PreferredName.getBaseIdentifier(), ObjCSelector()};
9898
return {Ctx.getIdentifier(getNameForObjC(VD)), ObjCSelector()};
9999
}
100100
}

lib/ClangImporter/ClangImporter.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,7 +1740,7 @@ ClangImporter::Implementation::exportSelector(DeclName name,
17401740
clang::ASTContext &ctx = getClangASTContext();
17411741

17421742
SmallVector<clang::IdentifierInfo *, 8> pieces;
1743-
pieces.push_back(exportName(name.getBaseName()).getAsIdentifierInfo());
1743+
pieces.push_back(exportName(name.getBaseIdentifier()).getAsIdentifierInfo());
17441744

17451745
auto argNames = name.getArgumentNames();
17461746
if (argNames.empty())
@@ -2932,7 +2932,7 @@ void ClangImporter::Implementation::lookupValue(
29322932
auto &clangCtx = getClangASTContext();
29332933
auto clangTU = clangCtx.getTranslationUnitDecl();
29342934

2935-
for (auto entry : table.lookup(name.getBaseName().str(), clangTU)) {
2935+
for (auto entry : table.lookup(name.getBaseIdentifier().str(), clangTU)) {
29362936
// If the entry is not visible, skip it.
29372937
if (!isVisibleClangEntry(clangCtx, entry)) continue;
29382938

@@ -2945,7 +2945,7 @@ void ClangImporter::Implementation::lookupValue(
29452945
} else {
29462946
// Try to import a macro.
29472947
auto clangMacro = entry.get<clang::MacroInfo *>();
2948-
decl = importMacro(name.getBaseName(), clangMacro);
2948+
decl = importMacro(name.getBaseIdentifier(), clangMacro);
29492949
if (!decl) continue;
29502950
}
29512951

@@ -3035,7 +3035,7 @@ void ClangImporter::Implementation::lookupObjCMembers(
30353035
DeclName name,
30363036
VisibleDeclConsumer &consumer) {
30373037
auto &clangCtx = getClangASTContext();
3038-
auto baseName = name.getBaseName().str();
3038+
auto baseName = name.getBaseIdentifier().str();
30393039

30403040
for (auto clangDecl : table.lookupObjCMembers(baseName)) {
30413041
// If the entry is not visible, skip it.

lib/ClangImporter/IAMInference.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ struct IAMResult {
103103
}
104104

105105
bool isInit() const {
106-
return isStaticMember() && name.getBaseName().str() == "init";
106+
return isStaticMember() && name.getBaseName() == "init";
107107
}
108108
};
109109
}

lib/ClangImporter/ImportDecl.cpp

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2161,7 +2161,7 @@ namespace {
21612161
Decl *VisitTypedefNameDecl(const clang::TypedefNameDecl *Decl) {
21622162
Optional<ImportedName> correctSwiftName;
21632163
auto importedName = importFullName(Decl, correctSwiftName);
2164-
auto Name = importedName.getDeclName().getBaseName();
2164+
auto Name = importedName.getDeclName().getBaseIdentifier();
21652165
if (Name.empty())
21662166
return nullptr;
21672167

@@ -2388,7 +2388,7 @@ namespace {
23882388
return nullptr;
23892389

23902390
ASTContext &cxt = Impl.SwiftContext;
2391-
auto name = importedName.getDeclName().getBaseName();
2391+
auto name = importedName.getDeclName().getBaseIdentifier();
23922392

23932393
// Create the enum declaration and record it.
23942394
StructDecl *errorWrapper = nullptr;
@@ -2719,7 +2719,7 @@ namespace {
27192719
if (unimported != constant && enumeratorDecl) {
27202720
ImportedName importedName =
27212721
Impl.importFullName(constant, getActiveSwiftVersion());
2722-
Identifier name = importedName.getDeclName().getBaseName();
2722+
Identifier name = importedName.getDeclName().getBaseIdentifier();
27232723
if (name.empty()) {
27242724
// Clear the existing declaration so we don't try to process it
27252725
// twice later.
@@ -2842,7 +2842,7 @@ namespace {
28422842
return nullptr;
28432843

28442844
// Create the struct declaration and record it.
2845-
auto name = importedName.getDeclName().getBaseName();
2845+
auto name = importedName.getDeclName().getBaseIdentifier();
28462846
auto result = Impl.createDeclWithClangNode<StructDecl>(decl,
28472847
Accessibility::Public,
28482848
Impl.importSourceLoc(decl->getLocStart()),
@@ -3037,7 +3037,7 @@ namespace {
30373037
auto importedName = importFullName(decl, correctSwiftName);
30383038
if (!importedName) return nullptr;
30393039

3040-
auto name = importedName.getDeclName().getBaseName();
3040+
auto name = importedName.getDeclName().getBaseIdentifier();
30413041
if (name.empty())
30423042
return nullptr;
30433043

@@ -3146,7 +3146,7 @@ namespace {
31463146
auto importedName = importFullName(decl, correctSwiftName);
31473147
if (!importedName) return nullptr;
31483148

3149-
auto name = importedName.getDeclName().getBaseName();
3149+
auto name = importedName.getDeclName().getBaseIdentifier();
31503150

31513151
auto dc =
31523152
Impl.importDeclContextOf(decl, importedName.getEffectiveContext());
@@ -3368,7 +3368,7 @@ namespace {
33683368
importedName.setEffectiveContext(decl->getDeclContext());
33693369
}
33703370

3371-
auto name = importedName.getDeclName().getBaseName();
3371+
auto name = importedName.getDeclName().getBaseIdentifier();
33723372

33733373
auto dc =
33743374
Impl.importDeclContextOf(decl, importedName.getEffectiveContext());
@@ -3423,7 +3423,7 @@ namespace {
34233423
auto importedName = importFullName(decl, correctSwiftName);
34243424
if (!importedName) return nullptr;
34253425

3426-
auto name = importedName.getDeclName().getBaseName();
3426+
auto name = importedName.getDeclName().getBaseIdentifier();
34273427
auto dc =
34283428
Impl.importDeclContextOf(decl, importedName.getEffectiveContext());
34293429
if (!dc)
@@ -4101,7 +4101,7 @@ namespace {
41014101
return importCompatibilityTypeAlias(decl, importedName,
41024102
*correctSwiftName);
41034103

4104-
Identifier name = importedName.getDeclName().getBaseName();
4104+
Identifier name = importedName.getDeclName().getBaseIdentifier();
41054105

41064106
// FIXME: Figure out how to deal with incomplete protocols, since that
41074107
// notion doesn't exist in Swift.
@@ -4239,7 +4239,7 @@ namespace {
42394239
return importCompatibilityTypeAlias(decl, importedName,
42404240
*correctSwiftName);
42414241

4242-
auto name = importedName.getDeclName().getBaseName();
4242+
auto name = importedName.getDeclName().getBaseIdentifier();
42434243

42444244
if (!decl->hasDefinition()) {
42454245
// Check if this class is implemented in its adapter.
@@ -4445,8 +4445,9 @@ namespace {
44454445
assert(dc);
44464446

44474447
Optional<ImportedName> correctSwiftName;
4448-
auto name =
4449-
importFullName(decl, correctSwiftName).getDeclName().getBaseName();
4448+
auto name = importFullName(decl, correctSwiftName)
4449+
.getDeclName()
4450+
.getBaseIdentifier();
44504451
if (name.empty())
44514452
return nullptr;
44524453

@@ -4578,7 +4579,7 @@ namespace {
45784579

45794580
Optional<ImportedName> correctSwiftName;
45804581
auto importedName = importFullName(decl, correctSwiftName);
4581-
auto name = importedName.getDeclName().getBaseName();
4582+
auto name = importedName.getDeclName().getBaseIdentifier();
45824583

45834584
if (name.empty()) return nullptr;
45844585

@@ -4832,7 +4833,7 @@ Decl *SwiftDeclConverter::importCompatibilityTypeAlias(
48324833
// Create the type alias.
48334834
auto alias = Impl.createDeclWithClangNode<TypeAliasDecl>(
48344835
decl, Accessibility::Public, Impl.importSourceLoc(decl->getLocStart()),
4835-
SourceLoc(), compatibilityName.getDeclName().getBaseName(),
4836+
SourceLoc(), compatibilityName.getDeclName().getBaseIdentifier(),
48364837
Impl.importSourceLoc(decl->getLocation()), /*generic params*/nullptr, dc);
48374838
alias->setUnderlyingType(underlyingType);
48384839

@@ -5030,7 +5031,7 @@ Decl *SwiftDeclConverter::importEnumCase(const clang::EnumConstantDecl *decl,
50305031
auto &context = Impl.SwiftContext;
50315032
Optional<ImportedName> correctSwiftName;
50325033
auto name =
5033-
importFullName(decl, correctSwiftName).getDeclName().getBaseName();
5034+
importFullName(decl, correctSwiftName).getDeclName().getBaseIdentifier();
50345035
if (name.empty())
50355036
return nullptr;
50365037

@@ -5089,7 +5090,7 @@ SwiftDeclConverter::importOptionConstant(const clang::EnumConstantDecl *decl,
50895090
NominalTypeDecl *theStruct) {
50905091
Optional<ImportedName> correctSwiftName;
50915092
ImportedName nameInfo = importFullName(decl, correctSwiftName);
5092-
Identifier name = nameInfo.getDeclName().getBaseName();
5093+
Identifier name = nameInfo.getDeclName().getBaseIdentifier();
50935094
if (name.empty())
50945095
return nullptr;
50955096

@@ -5376,7 +5377,7 @@ SwiftDeclConverter::getImplicitProperty(ImportedName importedName,
53765377
}
53775378

53785379
// Find the other accessor, if it exists.
5379-
auto propertyName = importedName.getDeclName().getBaseName();
5380+
auto propertyName = importedName.getDeclName().getBaseIdentifier();
53805381
auto lookupTable =
53815382
Impl.findLookupTable(*getClangSubmoduleForDecl(accessor));
53825383
assert(lookupTable && "No lookup table?");
@@ -7996,6 +7997,6 @@ Identifier
79967997
ClangImporter::getEnumConstantName(const clang::EnumConstantDecl *enumConstant){
79977998
return Impl.importFullName(enumConstant, Impl.CurrentVersion)
79987999
.getDeclName()
7999-
.getBaseName();
8000+
.getBaseIdentifier();
80008001
}
80018002

lib/ClangImporter/ImportType.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,7 +1672,7 @@ ParameterList *ClangImporter::Implementation::importFunctionParameterList(
16721672
// Figure out the name for this parameter.
16731673
Identifier bodyName = importFullName(param, CurrentVersion)
16741674
.getDeclName()
1675-
.getBaseName();
1675+
.getBaseIdentifier();
16761676

16771677
// Retrieve the argument name.
16781678
Identifier name;
@@ -2105,7 +2105,7 @@ Type ClangImporter::Implementation::importMethodType(
21052105
// Figure out the name for this parameter.
21062106
Identifier bodyName = importFullName(param, CurrentVersion)
21072107
.getDeclName()
2108-
.getBaseName();
2108+
.getBaseIdentifier();
21092109

21102110
// Figure out the name for this argument, which comes from the method name.
21112111
Identifier name;
@@ -2144,7 +2144,7 @@ Type ClangImporter::Implementation::importMethodType(
21442144

21452145
auto defaultArg = inferDefaultArgument(
21462146
param->getType(), optionalityOfParam,
2147-
importedName.getDeclName().getBaseName(), numEffectiveParams,
2147+
importedName.getDeclName().getBaseIdentifier(), numEffectiveParams,
21482148
name.empty() ? StringRef() : name.str(), paramIndex == 0,
21492149
isLastParameter, getNameImporter());
21502150
if (defaultArg != DefaultArgumentKind::None)
@@ -2261,7 +2261,7 @@ Type ClangImporter::Implementation::importAccessorMethodType(
22612261
} else {
22622262
const clang::ParmVarDecl *param = clangDecl->parameters().front();
22632263
ImportedName fullBodyName = importFullName(param, CurrentVersion);
2264-
Identifier bodyName = fullBodyName.getDeclName().getBaseName();
2264+
Identifier bodyName = fullBodyName.getDeclName().getBaseIdentifier();
22652265
SourceLoc nameLoc = importSourceLoc(param->getLocation());
22662266
Identifier argLabel = functionName.getDeclName().getArgumentNames().front();
22672267
auto paramInfo

0 commit comments

Comments
 (0)