Skip to content

[Misc] Migration to DeclBaseName in preparation for special decl names #9328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2129,7 +2129,7 @@ class ValueDecl : public Decl {

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

/// Retrieve the name to use for this declaration when interoperating
/// with the Objective-C runtime.
Expand Down
79 changes: 71 additions & 8 deletions include/swift/AST/Identifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ class DeclBaseName {
Identifier Ident;

public:
DeclBaseName() : DeclBaseName(Identifier()) {}

DeclBaseName(Identifier I) : Ident(I) {}

bool isSpecial() const { return false; }
Expand All @@ -223,6 +225,36 @@ class DeclBaseName {

bool empty() const { return !isSpecial() && getIdentifier().empty(); }

bool isOperator() const {
return !isSpecial() && getIdentifier().isOperator();
}

bool isEditorPlaceholder() const {
return !isSpecial() && getIdentifier().isEditorPlaceholder();
}

int compare(DeclBaseName other) const {
// TODO: Sort special names cleverly
return getIdentifier().compare(other.getIdentifier());
}

bool operator==(StringRef Str) const {
return !isSpecial() && getIdentifier().str() == Str;
}
bool operator!=(StringRef Str) const { return !(*this == Str); }

bool operator==(DeclBaseName RHS) const { return Ident == RHS.Ident; }
bool operator!=(DeclBaseName RHS) const { return !(*this == RHS); }

bool operator<(DeclBaseName RHS) const {
return Ident.get() < RHS.Ident.get();
}

// TODO: Remove once migration to DeclBaseName has been completed
operator Identifier() {
return getIdentifier();
}

const void *getAsOpaquePointer() const { return Ident.get(); }

static DeclBaseName getFromOpaquePointer(void *P) {
Expand All @@ -234,6 +266,24 @@ class DeclBaseName {

namespace llvm {

raw_ostream &operator<<(raw_ostream &OS, swift::DeclBaseName D);

// DeclBaseNames hash just like pointers.
template<> struct DenseMapInfo<swift::DeclBaseName> {
static swift::DeclBaseName getEmptyKey() {
return swift::Identifier::getEmptyKey();
}
static swift::DeclBaseName getTombstoneKey() {
return swift::Identifier::getTombstoneKey();
}
static unsigned getHashValue(swift::DeclBaseName Val) {
return DenseMapInfo<const void *>::getHashValue(Val.getAsOpaquePointer());
}
static bool isEqual(swift::DeclBaseName LHS, swift::DeclBaseName RHS) {
return LHS == RHS;
}
};

// A DeclBaseName is "pointer like".
template <typename T> class PointerLikeTypeTraits;
template <> class PointerLikeTypeTraits<swift::DeclBaseName> {
Expand Down Expand Up @@ -324,14 +374,19 @@ class DeclName {
/// Retrieve the 'base' name, i.e., the name that follows the introducer,
/// such as the 'foo' in 'func foo(x:Int, y:Int)' or the 'bar' in
/// 'var bar: Int'.
// TODO: Return DeclBaseName (remove two calls to getIdentifier)
Identifier getBaseName() const {
DeclBaseName getBaseName() const {
if (auto compound = SimpleOrCompound.dyn_cast<CompoundDeclName*>())
return compound->BaseName.getIdentifier();
return compound->BaseName;

return SimpleOrCompound.get<BaseNameAndCompound>()
.getPointer()
.getIdentifier();
return SimpleOrCompound.get<BaseNameAndCompound>().getPointer();
}

/// Assert that the base name is not special and return its identifier.
Identifier getBaseIdentifier() const {
auto baseName = getBaseName();
assert(!baseName.isSpecial() &&
"Can't retrieve the identifier of a special base name");
return baseName.getIdentifier();
}

/// Retrieve the names of the arguments, if there are any.
Expand All @@ -342,6 +397,8 @@ class DeclName {
return { };
}

bool isSpecial() const { return getBaseName().isSpecial(); }

explicit operator bool() const {
if (SimpleOrCompound.dyn_cast<CompoundDeclName*>())
return true;
Expand All @@ -366,14 +423,20 @@ class DeclName {

/// True if this name is a simple one-component name identical to the
/// given identifier.
bool isSimpleName(Identifier name) const {
bool isSimpleName(DeclBaseName name) const {
return isSimpleName() && getBaseName() == name;
}

/// True if this name is a simple one-component name equal to the
/// given string.
bool isSimpleName(StringRef name) const {
return isSimpleName() && getBaseName().str().equals(name);
if (!isSimpleName())
return false;

if (getBaseName().isSpecial())
return false;

return getBaseIdentifier().str().equals(name);
}

/// True if this name is an operator.
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1912,7 +1912,7 @@ bool swift::fixDeclarationName(InFlightDiagnostic &diag, ValueDecl *decl,

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

// Fix the argument names that need fixing.
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1818,7 +1818,7 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
}
void visitOverloadedDeclRefExpr(OverloadedDeclRefExpr *E) {
printCommon(E, "overloaded_decl_ref_expr")
<< " name=" << E->getDecls()[0]->getName()
<< " name=" << E->getDecls()[0]->getBaseName()
<< " #decls=" << E->getDecls().size()
<< " specialized=" << (E->isSpecialized()? "yes" : "no")
<< " function_ref=" << getFunctionRefKindStr(E->getFunctionRefKind());
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/ASTVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2217,7 +2217,7 @@ class Verifier : public ASTWalker {
dumpRef(decl);
Out << " is missing witness for "
<< conformance->getProtocol()->getName().str()
<< "." << req->getName().str()
<< "." << req->getBaseName()
<< "\n";
abort();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/CaptureInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void CaptureInfo::print(raw_ostream &OS) const {

interleave(getCaptures(),
[&](const CapturedValue &capture) {
OS << capture.getDecl()->getName();
OS << capture.getDecl()->getBaseName();

if (capture.isDirect())
OS << "<direct>";
Expand Down
4 changes: 2 additions & 2 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4476,7 +4476,7 @@ ObjCSelector AbstractFunctionDecl::getObjCSelector(
if (argNames.size() != preferredName.getArgumentNames().size()) {
return ObjCSelector();
}
baseName = preferredName.getBaseName();
baseName = preferredName.getBaseIdentifier();
argNames = preferredName.getArgumentNames();
}

Expand Down Expand Up @@ -5065,7 +5065,7 @@ ConstructorDecl::getDelegatingOrChainedInitKind(DiagnosticEngine *diags,
} else if (auto *CRE = dyn_cast<ConstructorRefCallExpr>(Callee)) {
arg = CRE->getArg();
} else if (auto *dotExpr = dyn_cast<UnresolvedDotExpr>(Callee)) {
if (dotExpr->getName().getBaseName().str() != "init")
if (dotExpr->getName().getBaseName() != "init")
return { true, E };

arg = dotExpr->getBase();
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/DeclContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ unsigned DeclContext::printContext(raw_ostream &OS, unsigned indent) const {
}
case DeclContextKind::SubscriptDecl: {
auto *SD = cast<SubscriptDecl>(this);
OS << " name=" << SD->getName();
OS << " name=" << SD->getBaseName();
if (SD->hasInterfaceType())
OS << " : " << SD->getInterfaceType();
else
Expand Down
5 changes: 5 additions & 0 deletions lib/AST/Identifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ raw_ostream &llvm::operator<<(raw_ostream &OS, Identifier I) {
return OS << I.get();
}

raw_ostream &llvm::operator<<(raw_ostream &OS, DeclBaseName I) {
// TODO: Handle special names
return OS << I.getIdentifier();
}

raw_ostream &llvm::operator<<(raw_ostream &OS, DeclName I) {
if (I.isSimpleName())
return OS << I.getBaseName();
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ void ModuleDecl::lookupObjCMethods(
void BuiltinUnit::lookupValue(ModuleDecl::AccessPathTy accessPath, DeclName name,
NLKind lookupKind,
SmallVectorImpl<ValueDecl*> &result) const {
getCache().lookupValue(name.getBaseName(), lookupKind, *this, result);
getCache().lookupValue(name.getBaseIdentifier(), lookupKind, *this, result);
}

void BuiltinUnit::lookupObjCMethods(
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/NameLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,7 @@ UnqualifiedLookup::UnqualifiedLookup(DeclName Name, DeclContext *DC,
return;
}

ModuleDecl *desiredModule = Ctx.getLoadedModule(Name.getBaseName());
ModuleDecl *desiredModule = Ctx.getLoadedModule(Name.getBaseIdentifier());
if (!desiredModule && Name == Ctx.TheBuiltinModule->getName())
desiredModule = Ctx.TheBuiltinModule;
if (desiredModule) {
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/PrettyStackTrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void swift::printDeclDescription(llvm::raw_ostream &out, const Decl *D,
bool hasPrintedName = false;
if (auto *named = dyn_cast<ValueDecl>(D)) {
if (named->hasName()) {
out << '\'' << named->getName() << '\'';
out << '\'' << named->getFullName() << '\'';
hasPrintedName = true;
} else if (auto *fn = dyn_cast<FuncDecl>(named)) {
if (auto *ASD = fn->getAccessorStorageDecl()) {
Expand Down
6 changes: 3 additions & 3 deletions lib/AST/SwiftNameTranslation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ getObjCNameForSwiftDecl(const ValueDecl *VD, DeclName PreferredName){
return {Identifier(), FD->getObjCSelector(Resolver, PreferredName)};
} else if (auto *VAD = dyn_cast<VarDecl>(VD)) {
if (PreferredName)
return {PreferredName.getBaseName(), ObjCSelector()};
return {PreferredName.getBaseIdentifier(), ObjCSelector()};
return {VAD->getObjCPropertyName(), ObjCSelector()};
} else if (auto *SD = dyn_cast<SubscriptDecl>(VD)) {
return getObjCNameForSwiftDecl(SD->getGetter(), PreferredName);
} else if (auto *EL = dyn_cast<EnumElementDecl>(VD)) {
SmallString<64> Buffer;
{
llvm::raw_svector_ostream OS(Buffer);
printSwiftEnumElemNameInObjC(EL, OS, PreferredName.getBaseName());
printSwiftEnumElemNameInObjC(EL, OS, PreferredName.getBaseIdentifier());
}
return {Ctx.getIdentifier(Buffer.str()), ObjCSelector()};
} else {
Expand All @@ -94,7 +94,7 @@ getObjCNameForSwiftDecl(const ValueDecl *VD, DeclName PreferredName){
if (!Name.empty())
return {Ctx.getIdentifier(Name), ObjCSelector()};
if (!PreferredName.getBaseName().empty())
return {PreferredName.getBaseName(), ObjCSelector()};
return {PreferredName.getBaseIdentifier(), ObjCSelector()};
return {Ctx.getIdentifier(getNameForObjC(VD)), ObjCSelector()};
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1691,7 +1691,7 @@ ClangImporter::Implementation::exportSelector(DeclName name,
clang::ASTContext &ctx = getClangASTContext();

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

auto argNames = name.getArgumentNames();
if (argNames.empty())
Expand Down Expand Up @@ -2883,7 +2883,7 @@ void ClangImporter::Implementation::lookupValue(
auto &clangCtx = getClangASTContext();
auto clangTU = clangCtx.getTranslationUnitDecl();

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

Expand All @@ -2896,7 +2896,7 @@ void ClangImporter::Implementation::lookupValue(
} else {
// Try to import a macro.
auto clangMacro = entry.get<clang::MacroInfo *>();
decl = importMacro(name.getBaseName(), clangMacro);
decl = importMacro(name.getBaseIdentifier(), clangMacro);
if (!decl) continue;
}

Expand Down Expand Up @@ -2986,7 +2986,7 @@ void ClangImporter::Implementation::lookupObjCMembers(
DeclName name,
VisibleDeclConsumer &consumer) {
auto &clangCtx = getClangASTContext();
auto baseName = name.getBaseName().str();
auto baseName = name.getBaseIdentifier().str();

for (auto clangDecl : table.lookupObjCMembers(baseName)) {
// If the entry is not visible, skip it.
Expand Down
2 changes: 1 addition & 1 deletion lib/ClangImporter/IAMInference.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ struct IAMResult {
}

bool isInit() const {
return isStaticMember() && name.getBaseName().str() == "init";
return isStaticMember() && name.getBaseName() == "init";
}
};
}
Expand Down
Loading