Skip to content

Commit f302afc

Browse files
ahoppenjrose-apple
authored andcommitted
Unify approach to printing declaration names (#9320)
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 e068bc3 commit f302afc

18 files changed

+44
-35
lines changed

lib/AST/ASTDumper.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1821,7 +1821,7 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
18211821
}
18221822
void visitOverloadedDeclRefExpr(OverloadedDeclRefExpr *E) {
18231823
printCommon(E, "overloaded_decl_ref_expr")
1824-
<< " name=" << E->getDecls()[0]->getName()
1824+
<< " name=" << E->getDecls()[0]->getBaseName()
18251825
<< " #decls=" << E->getDecls().size()
18261826
<< " function_ref=" << getFunctionRefKindStr(E->getFunctionRefKind());
18271827

lib/AST/ASTVerifier.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2277,7 +2277,7 @@ class Verifier : public ASTWalker {
22772277
dumpRef(decl);
22782278
Out << " is missing witness for "
22792279
<< conformance->getProtocol()->getName().str()
2280-
<< "." << req->getName().str()
2280+
<< "." << req->getBaseName()
22812281
<< "\n";
22822282
abort();
22832283
}

lib/AST/CaptureInfo.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void CaptureInfo::print(raw_ostream &OS) const {
5454

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

5959
if (capture.isDirect())
6060
OS << "<direct>";

lib/AST/DeclContext.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ unsigned DeclContext::printContext(raw_ostream &OS, unsigned indent) const {
799799
}
800800
case DeclContextKind::SubscriptDecl: {
801801
auto *SD = cast<SubscriptDecl>(this);
802-
OS << " name=" << SD->getName();
802+
OS << " name=" << SD->getBaseName();
803803
if (SD->hasInterfaceType())
804804
OS << " : " << SD->getInterfaceType();
805805
else

lib/AST/Identifier.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ raw_ostream &llvm::operator<<(raw_ostream &OS, Identifier I) {
2727
return OS << I.get();
2828
}
2929

30+
raw_ostream &llvm::operator<<(raw_ostream &OS, DeclBaseName I) {
31+
// TODO: Handle special names
32+
return OS << I.getIdentifier();
33+
}
34+
3035
raw_ostream &llvm::operator<<(raw_ostream &OS, DeclName I) {
3136
if (I.isSimpleName())
3237
return OS << I.getBaseName();

lib/AST/PrettyStackTrace.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void swift::printDeclDescription(llvm::raw_ostream &out, const Decl *D,
4444
bool hasPrintedName = false;
4545
if (auto *named = dyn_cast<ValueDecl>(D)) {
4646
if (named->hasName()) {
47-
out << '\'' << named->getName() << '\'';
47+
out << '\'' << named->getFullName() << '\'';
4848
hasPrintedName = true;
4949
} else if (auto *fn = dyn_cast<FuncDecl>(named)) {
5050
if (auto *ASD = fn->getAccessorStorageDecl()) {

lib/ClangImporter/ImportName.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ void ClangImporter::Implementation::printSwiftName(ImportedName name,
377377
printFullContextPrefix(name, os, *this);
378378

379379
// Base name.
380-
os << name.getDeclName().getBaseName().str();
380+
os << name.getDeclName().getBaseName();
381381

382382
// Determine the number of argument labels we'll be producing.
383383
auto argumentNames = name.getDeclName().getArgumentNames();

lib/FrontendTool/ReferenceDependencies.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,9 @@ bool swift::emitReferenceDependencies(DiagnosticEngine &diags,
149149
return true;
150150
}
151151

152-
auto escape = [](Identifier name) -> std::string {
153-
return llvm::yaml::escape(name.str());
152+
auto escape = [](DeclBaseName name) -> std::string {
153+
// TODO: Handle special names
154+
return llvm::yaml::escape(name.getIdentifier().str());
154155
};
155156

156157
out << "### Swift dependencies file v0 ###\n";

lib/IRGen/GenMeta.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2377,7 +2377,7 @@ namespace {
23772377
llvm::raw_svector_ostream os(out);
23782378

23792379
for (ValueDecl *prop : fields) {
2380-
os << prop->getName().str() << '\0';
2380+
os << prop->getBaseName() << '\0';
23812381
++numFields;
23822382
}
23832383
// The final null terminator is provided by getAddrOfGlobalString.

lib/PrintAsObjC/PrintAsObjC.cpp

+13-10
Original file line numberDiff line numberDiff line change
@@ -696,15 +696,17 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
696696
if (AvAttr->PlatformAgnostic == PlatformAgnosticAvailabilityKind::Unavailable) {
697697
// Availability for *
698698
if (!AvAttr->Rename.empty()) {
699-
// NB: Don't bother getting obj-c names, we can't get one for the rename
700-
os << " SWIFT_UNAVAILABLE_MSG(\"'" << VD->getName() << "' has been renamed to '";
701-
printEncodedString(AvAttr->Rename, false);
702-
os << '\'';
703-
if (!AvAttr->Message.empty()) {
704-
os << ": ";
705-
printEncodedString(AvAttr->Message, false);
706-
}
707-
os << "\")";
699+
// NB: Don't bother getting obj-c names, we can't get one for the
700+
// rename
701+
os << " SWIFT_UNAVAILABLE_MSG(\"'" << VD->getBaseName()
702+
<< "' has been renamed to '";
703+
printEncodedString(AvAttr->Rename, false);
704+
os << '\'';
705+
if (!AvAttr->Message.empty()) {
706+
os << ": ";
707+
printEncodedString(AvAttr->Message, false);
708+
}
709+
os << "\")";
708710
} else if (!AvAttr->Message.empty()) {
709711
os << " SWIFT_UNAVAILABLE_MSG(";
710712
printEncodedString(AvAttr->Message);
@@ -790,7 +792,8 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
790792
}
791793
if (!AvAttr->Rename.empty()) {
792794
// NB: Don't bother getting obj-c names, we can't get one for the rename
793-
os << ",message=\"'" << VD->getName() << "' has been renamed to '";
795+
os << ",message=\"'" << VD->getBaseName()
796+
<< "' has been renamed to '";
794797
printEncodedString(AvAttr->Rename, false);
795798
os << '\'';
796799
if (!AvAttr->Message.empty()) {

lib/SIL/SILPrinter.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,9 @@ static void printValueDecl(ValueDecl *Decl, raw_ostream &OS) {
238238
assert(Decl->hasName());
239239

240240
if (Decl->isOperator())
241-
OS << '"' << Decl->getName() << '"';
241+
OS << '"' << Decl->getBaseName() << '"';
242242
else
243-
OS << Decl->getName();
243+
OS << Decl->getBaseName();
244244
}
245245

246246
/// SILDeclRef uses sigil "#" and prints the fully qualified dotted path.
@@ -2449,7 +2449,7 @@ void SILWitnessTable::print(llvm::raw_ostream &OS, bool Verbose) const {
24492449
case MissingOptional: {
24502450
// optional requirement 'declref': <<not present>>
24512451
OS << "optional requirement '"
2452-
<< witness.getMissingOptionalWitness().Witness->getName()
2452+
<< witness.getMissingOptionalWitness().Witness->getBaseName()
24532453
<< "': <<not present>>";
24542454
break;
24552455
}

lib/SILGen/SILGenLValue.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,7 @@ namespace {
983983
}
984984

985985
void printBase(raw_ostream &OS, StringRef name) const {
986-
OS << name << "(" << decl->getName() << ")";
986+
OS << name << "(" << decl->getBaseName() << ")";
987987
if (IsSuper) OS << " isSuper";
988988
if (IsDirectAccessorUse) OS << " isDirectAccessorUse";
989989
if (subscriptIndexExpr) {

lib/Sema/MiscDiagnostics.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -3192,7 +3192,7 @@ class ObjCSelectorWalker : public ASTWalker {
31923192
name = bestMethod->getFullName();
31933193
}
31943194

3195-
out << nominal->getName().str() << "." << name.getBaseName().str();
3195+
out << nominal->getName().str() << "." << name.getBaseName();
31963196
auto argNames = name.getArgumentNames();
31973197

31983198
// Only print the parentheses if there are some argument

lib/Sema/TypeCheckConstraints.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -2998,9 +2998,9 @@ void Solution::dump(raw_ostream &out) const {
29982998
out << " as ";
29992999
if (choice.getBaseType())
30003000
out << choice.getBaseType()->getString() << ".";
3001-
3002-
out << choice.getDecl()->getName().str() << ": "
3003-
<< ovl.second.openedType->getString() << "\n";
3001+
3002+
out << choice.getDecl()->getBaseName() << ": "
3003+
<< ovl.second.openedType->getString() << "\n";
30043004
break;
30053005

30063006
case OverloadChoiceKind::BaseType:
@@ -3165,9 +3165,9 @@ void ConstraintSystem::print(raw_ostream &out) {
31653165
case OverloadChoiceKind::DeclViaUnwrappedOptional:
31663166
if (choice.getBaseType())
31673167
out << choice.getBaseType()->getString() << ".";
3168-
out << choice.getDecl()->getName() << ": "
3169-
<< resolved->BoundType->getString() << " == "
3170-
<< resolved->ImpliedType->getString() << "\n";
3168+
out << choice.getDecl()->getBaseName() << ": "
3169+
<< resolved->BoundType->getString() << " == "
3170+
<< resolved->ImpliedType->getString() << "\n";
31713171
break;
31723172

31733173
case OverloadChoiceKind::BaseType:

lib/Sema/TypeCheckExprObjC.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ Optional<Type> TypeChecker::checkObjCKeyPathExpr(DeclContext *dc,
187187
else
188188
needDot = true;
189189

190-
keyPathOS << component.str();
190+
keyPathOS << component;
191191
};
192192

193193
bool isInvalid = false;

lib/Sema/TypeCheckProtocol.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -3888,10 +3888,10 @@ namespace {
38883888
for (unsigned i : indices(ValueWitnesses)) {
38893889
auto &valueWitness = ValueWitnesses[i];
38903890
llvm::errs() << i << ": " << (Decl*)valueWitness.first
3891-
<< ' ' << valueWitness.first->getName() << '\n';
3891+
<< ' ' << valueWitness.first->getBaseName() << '\n';
38923892
valueWitness.first->getDeclContext()->dumpContext();
38933893
llvm::errs() << " for " << (Decl*)valueWitness.second
3894-
<< ' ' << valueWitness.second->getName() << '\n';
3894+
<< ' ' << valueWitness.second->getBaseName() << '\n';
38953895
valueWitness.second->getDeclContext()->dumpContext();
38963896
}
38973897
}

lib/Serialization/Deserialization.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ namespace {
8686
os << "While deserializing ";
8787

8888
if (auto VD = dyn_cast<ValueDecl>(DeclOrOffset.get())) {
89-
os << "'" << VD->getName() << "' (" << IDAndKind{VD, ID} << ")";
89+
os << "'" << VD->getBaseName() << "' (" << IDAndKind{VD, ID} << ")";
9090
} else if (auto ED = dyn_cast<ExtensionDecl>(DeclOrOffset.get())) {
9191
os << "extension of '" << ED->getExtendedType() << "' ("
9292
<< IDAndKind{ED, ID} << ")";

tools/swift-ide-test/swift-ide-test.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1979,7 +1979,7 @@ class ASTTypePrinter : public ASTWalker {
19791979
if (auto *VD = dyn_cast<ValueDecl>(D)) {
19801980
OS.indent(IndentLevel * 2);
19811981
OS << Decl::getKindName(VD->getKind()) << "Decl '''"
1982-
<< VD->getName().str() << "''' ";
1982+
<< VD->getBaseName() << "''' ";
19831983
VD->getInterfaceType().print(OS, Options);
19841984
OS << "\n";
19851985
}

0 commit comments

Comments
 (0)