Skip to content

Commit 4468ad2

Browse files
authored
[Sema] Preparations for removal of getName on ValueDecl (#9972)
With the introduction of special decl names, `Identifier getName()` on `ValueDecl` will be removed and pushed down to nominal declarations whose name is guaranteed not to be special. Prepare for this by calling to `DeclBaseName getBaseName()` instead where appropriate.
1 parent 230dd86 commit 4468ad2

24 files changed

+109
-96
lines changed

lib/Sema/CSApply.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2551,7 +2551,7 @@ namespace {
25512551
bool diagnoseBadInitRef = true;
25522552
auto arg = base->getSemanticsProvidingExpr();
25532553
if (auto dre = dyn_cast<DeclRefExpr>(arg)) {
2554-
if (dre->getDecl()->getName() == cs.getASTContext().Id_self) {
2554+
if (dre->getDecl()->getFullName() == cs.getASTContext().Id_self) {
25552555
// We have a reference to 'self'.
25562556
diagnoseBadInitRef = false;
25572557

lib/Sema/CSDiag.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ static void diagnoseSubElementFailure(Expr *destExpr,
707707
// If we're trying to set an unapplied method, say that.
708708
if (auto *VD = dyn_cast_or_null<ValueDecl>(immInfo.second)) {
709709
std::string message = "'";
710-
message += VD->getName().str().str();
710+
message += VD->getBaseName().getIdentifier().str();
711711
message += "'";
712712

713713
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(VD))
@@ -744,9 +744,9 @@ static void diagnoseSubElementFailure(Expr *destExpr,
744744
else if (isa<DotSyntaxCallExpr>(AE) || isa<DotSyntaxBaseIgnoredExpr>(AE))
745745
name = "method call";
746746

747-
if (auto *DRE =
748-
dyn_cast<DeclRefExpr>(AE->getFn()->getValueProvidingExpr()))
749-
name = std::string("'") + DRE->getDecl()->getName().str().str() + "'";
747+
if (auto *DRE = dyn_cast<DeclRefExpr>(AE->getFn()->getValueProvidingExpr()))
748+
name = std::string("'") +
749+
DRE->getDecl()->getBaseName().getIdentifier().str().str() + "'";
750750

751751
TC.diagnose(loc, diagID, name + " returns immutable value")
752752
.highlight(AE->getSourceRange());
@@ -1945,7 +1945,7 @@ bool CalleeCandidateInfo::diagnoseSimpleErrors(const Expr *E) {
19451945
CD->getResultInterfaceType(), decl->getFormalAccess());
19461946

19471947
} else {
1948-
CS->TC.diagnose(loc, diag::candidate_inaccessible, decl->getName(),
1948+
CS->TC.diagnose(loc, diag::candidate_inaccessible, decl->getBaseName(),
19491949
decl->getFormalAccess());
19501950
}
19511951
for (auto cand : candidates) {
@@ -2451,7 +2451,7 @@ diagnoseTypeMemberOnInstanceLookup(Type baseObjTy,
24512451
// Fetch any declaration to check if the name is '~='
24522452
ValueDecl *decl0 = overloadedFn->getDecls()[0];
24532453

2454-
if (decl0->getName() == decl0->getASTContext().Id_MatchOperator) {
2454+
if (decl0->getBaseName() == decl0->getASTContext().Id_MatchOperator) {
24552455
assert(binaryExpr->getArg()->getElements().size() == 2);
24562456

24572457
// If the rhs of '~=' is the enum type, a single dot suffixes
@@ -2681,7 +2681,7 @@ diagnoseUnviableLookupResults(MemberLookupResult &result, Type baseObjTy,
26812681
case MemberLookupResult::UR_Inaccessible: {
26822682
auto decl = result.UnviableCandidates[0].first;
26832683
// FIXME: What if the unviable candidates have different levels of access?
2684-
diagnose(nameLoc, diag::candidate_inaccessible, decl->getName(),
2684+
diagnose(nameLoc, diag::candidate_inaccessible, decl->getBaseName(),
26852685
decl->getFormalAccess());
26862686
for (auto cand : result.UnviableCandidates)
26872687
diagnose(cand.first, diag::decl_declared_here, memberName);
@@ -4595,7 +4595,7 @@ static bool diagnoseImplicitSelfErrors(Expr *fnExpr, Expr *argExpr,
45954595
return false;
45964596

45974597
auto baseDecl = baseExpr->getDecl();
4598-
if (!baseExpr->isImplicit() || baseDecl->getName() != TC.Context.Id_self)
4598+
if (!baseExpr->isImplicit() || baseDecl->getFullName() != TC.Context.Id_self)
45994599
return false;
46004600

46014601
// Our base expression is an implicit 'self.' reference e.g.
@@ -8596,9 +8596,9 @@ void FailureDiagnosis::diagnoseUnboundArchetype(ArchetypeType *archetype,
85968596

85978597
auto decl = resolved.getDecl();
85988598
if (isa<FuncDecl>(decl)) {
8599-
auto name = decl->getName();
8599+
auto name = decl->getBaseName();
86008600
auto diagID = name.isOperator() ? diag::note_call_to_operator
8601-
: diag::note_call_to_func;
8601+
: diag::note_call_to_func;
86028602
tc.diagnose(decl, diagID, name);
86038603
return;
86048604
}
@@ -8610,7 +8610,7 @@ void FailureDiagnosis::diagnoseUnboundArchetype(ArchetypeType *archetype,
86108610
}
86118611

86128612
if (isa<ParamDecl>(decl)) {
8613-
tc.diagnose(decl, diag::note_init_parameter, decl->getName());
8613+
tc.diagnose(decl, diag::note_init_parameter, decl->getBaseName());
86148614
return;
86158615
}
86168616

lib/Sema/CSGen.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ findReferencedDecl(Expr *expr, DeclNameLoc &loc) {
5858

5959
static bool isArithmeticOperatorDecl(ValueDecl *vd) {
6060
return vd &&
61-
(vd->getName().str() == "+" ||
62-
vd->getName().str() == "-" ||
63-
vd->getName().str() == "*" ||
64-
vd->getName().str() == "/" ||
65-
vd->getName().str() == "%");
61+
(vd->getBaseName() == "+" ||
62+
vd->getBaseName() == "-" ||
63+
vd->getBaseName() == "*" ||
64+
vd->getBaseName() == "/" ||
65+
vd->getBaseName() == "%");
6666
}
6767

6868
static bool mergeRepresentativeEquivalenceClasses(ConstraintSystem &CS,
@@ -357,8 +357,7 @@ namespace {
357357
if (acp1 == acp2)
358358
continue;
359359

360-
if (acp1->getDecl()->getName().str() ==
361-
acp2->getDecl()->getName().str()) {
360+
if (acp1->getDecl()->getBaseName() == acp2->getDecl()->getBaseName()) {
362361

363362
auto tyvar1 = CS.getType(acp1)->getAs<TypeVariableType>();
364363
auto tyvar2 = CS.getType(acp2)->getAs<TypeVariableType>();
@@ -439,8 +438,8 @@ namespace {
439438
if (!isArithmeticOperatorDecl(ODR1->getDecls()[0]))
440439
return;
441440

442-
if (ODR1->getDecls()[0]->getName().str() !=
443-
ODR2->getDecls()[0]->getName().str())
441+
if (ODR1->getDecls()[0]->getBaseName() !=
442+
ODR2->getDecls()[0]->getBaseName())
444443
return;
445444

446445
// All things equal, we can merge the tyvars for the function

lib/Sema/CSRanking.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,8 +1014,8 @@ ConstraintSystem::compareSolutions(ConstraintSystem &cs,
10141014

10151015
// FIXME: Lousy hack for ?? to prefer the catamorphism (flattening)
10161016
// over the mplus (non-flattening) overload if all else is equal.
1017-
if (decl1->getName().str() == "??") {
1018-
assert(decl2->getName().str() == "??");
1017+
if (decl1->getBaseName() == "??") {
1018+
assert(decl2->getBaseName() == "??");
10191019

10201020
auto check = [](const ValueDecl *VD) -> bool {
10211021
if (!VD->getModuleContext()->isStdlibModule())

lib/Sema/DerivedConformanceCodable.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,7 @@ ValueDecl *DerivedConformance::deriveEncodable(TypeChecker &tc,
11181118
if (!isa<StructDecl>(target) && !isa<ClassDecl>(target))
11191119
return nullptr;
11201120

1121-
if (requirement->getName() != tc.Context.Id_encode) {
1121+
if (requirement->getBaseName() != tc.Context.Id_encode) {
11221122
// Unknown requirement.
11231123
tc.diagnose(requirement->getLoc(), diag::broken_encodable_requirement);
11241124
return nullptr;
@@ -1158,7 +1158,7 @@ ValueDecl *DerivedConformance::deriveDecodable(TypeChecker &tc,
11581158
if (!isa<StructDecl>(target) && !isa<ClassDecl>(target))
11591159
return nullptr;
11601160

1161-
if (requirement->getName() != tc.Context.Id_init) {
1161+
if (requirement->getBaseName() != tc.Context.Id_init) {
11621162
// Unknown requirement.
11631163
tc.diagnose(requirement->getLoc(), diag::broken_decodable_requirement);
11641164
return nullptr;

lib/Sema/DerivedConformanceCodingKey.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ ValueDecl *DerivedConformance::deriveCodingKey(TypeChecker &tc,
430430

431431
auto &C = tc.Context;
432432
auto rawType = enumDecl->getRawType();
433-
auto name = requirement->getName();
433+
auto name = requirement->getBaseName();
434434
if (name == C.Id_stringValue) {
435435
// Synthesize `var stringValue: String { get }`
436436
auto stringType = C.getStringDecl()->getDeclaredType();

lib/Sema/DerivedConformanceEquatableHashable.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ ValueDecl *DerivedConformance::deriveEquatable(TypeChecker &tc,
322322
return nullptr;
323323

324324
// Build the necessary decl.
325-
if (requirement->getName().str() == "==") {
325+
if (requirement->getBaseName() == "==") {
326326
if (auto theEnum = dyn_cast<EnumDecl>(type))
327327
return deriveEquatable_enum_eq(tc, parentDecl, theEnum);
328328
else
@@ -476,7 +476,7 @@ ValueDecl *DerivedConformance::deriveHashable(TypeChecker &tc,
476476
return nullptr;
477477

478478
// Build the necessary decl.
479-
if (requirement->getName().str() == "hashValue") {
479+
if (requirement->getBaseName() == "hashValue") {
480480
if (auto theEnum = dyn_cast<EnumDecl>(type))
481481
return deriveHashable_enum_hashValue(tc, parentDecl, theEnum);
482482
else

lib/Sema/DerivedConformanceError.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ ValueDecl *DerivedConformance::deriveBridgedNSError(TypeChecker &tc,
104104

105105
auto enumType = cast<EnumDecl>(type);
106106

107-
if (requirement->getName() == tc.Context.Id_nsErrorDomain)
107+
if (requirement->getBaseName() == tc.Context.Id_nsErrorDomain)
108108
return deriveBridgedNSError_enum_nsErrorDomain(tc, parentDecl, enumType);
109109

110110
tc.diagnose(requirement->getLoc(),

lib/Sema/DerivedConformanceRawRepresentable.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,10 +388,10 @@ ValueDecl *DerivedConformance::deriveRawRepresentable(TypeChecker &tc,
388388
if (!canSynthesizeRawRepresentable(tc, parentDecl, enumDecl))
389389
return nullptr;
390390

391-
if (requirement->getName() == tc.Context.Id_rawValue)
391+
if (requirement->getBaseName() == tc.Context.Id_rawValue)
392392
return deriveRawRepresentable_raw(tc, parentDecl, enumDecl);
393-
394-
if (requirement->getName() == tc.Context.Id_init)
393+
394+
if (requirement->getBaseName() == tc.Context.Id_init)
395395
return deriveRawRepresentable_init(tc, parentDecl, enumDecl);
396396

397397
tc.diagnose(requirement->getLoc(),

lib/Sema/MiscDiagnostics.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,8 @@ static void diagSyntacticUseRestrictions(TypeChecker &TC, const Expr *E,
469469
return;
470470

471471
TC.diagnose(DRE->getStartLoc(), diag::invalid_noescape_use,
472-
DRE->getDecl()->getName(), isa<ParamDecl>(DRE->getDecl()));
472+
DRE->getDecl()->getBaseName(),
473+
isa<ParamDecl>(DRE->getDecl()));
473474

474475
// If we're a parameter, emit a helpful fixit to add @escaping
475476
auto paramDecl = dyn_cast<ParamDecl>(DRE->getDecl());
@@ -482,7 +483,7 @@ static void diagSyntacticUseRestrictions(TypeChecker &TC, const Expr *E,
482483
} else if (isAutoClosure)
483484
// TODO: add in a fixit for autoclosure
484485
TC.diagnose(DRE->getDecl()->getLoc(), diag::noescape_autoclosure,
485-
DRE->getDecl()->getName());
486+
DRE->getDecl()->getBaseName());
486487
}
487488

488489
// Swift 3 mode produces a warning + Fix-It for the missing ".self"
@@ -613,7 +614,7 @@ static void diagSyntacticUseRestrictions(TypeChecker &TC, const Expr *E,
613614
}
614615

615616
TC.diagnose(DRE->getLoc(), diag::warn_unqualified_access,
616-
VD->getName(), VD->getDescriptiveKind(),
617+
VD->getBaseName(), VD->getDescriptiveKind(),
617618
declParent->getDescriptiveKind(), declParent->getFullName());
618619
TC.diagnose(VD, diag::decl_declared_here, VD->getFullName());
619620

@@ -667,7 +668,7 @@ static void diagSyntacticUseRestrictions(TypeChecker &TC, const Expr *E,
667668
if (TC.getDeclTypeCheckingSemantics(DRE->getDecl())
668669
!= DeclTypeCheckingSemantics::Normal) {
669670
TC.diagnose(DRE->getLoc(), diag::unsupported_special_decl_ref,
670-
DRE->getDecl()->getName());
671+
DRE->getDecl()->getBaseName());
671672
}
672673
}
673674

@@ -1214,8 +1215,8 @@ static void diagSyntacticUseRestrictions(TypeChecker &TC, const Expr *E,
12141215

12151216
auto lhs = args->getElement(0);
12161217
auto rhs = args->getElement(1);
1217-
auto calleeName = DRE->getDecl()->getName().str();
1218-
1218+
auto calleeName = DRE->getDecl()->getBaseName();
1219+
12191220
Expr *subExpr = nullptr;
12201221
if (calleeName == "??" &&
12211222
(subExpr = isImplicitPromotionToOptional(lhs))) {
@@ -1442,7 +1443,7 @@ static void diagnoseImplicitSelfUseInClosure(TypeChecker &TC, const Expr *E,
14421443
if (isImplicitSelfUse(MRE->getBase())) {
14431444
TC.diagnose(MRE->getLoc(),
14441445
diag::property_use_in_closure_without_explicit_self,
1445-
MRE->getMember().getDecl()->getName())
1446+
MRE->getMember().getDecl()->getBaseName())
14461447
.fixItInsert(MRE->getLoc(), "self.");
14471448
return { false, E };
14481449
}
@@ -1454,7 +1455,7 @@ static void diagnoseImplicitSelfUseInClosure(TypeChecker &TC, const Expr *E,
14541455
auto MethodExpr = cast<DeclRefExpr>(DSCE->getFn());
14551456
TC.diagnose(DSCE->getLoc(),
14561457
diag::method_call_in_closure_without_explicit_self,
1457-
MethodExpr->getDecl()->getName())
1458+
MethodExpr->getDecl()->getBaseName())
14581459
.fixItInsert(DSCE->getLoc(), "self.");
14591460
return { false, E };
14601461
}
@@ -3680,7 +3681,7 @@ Optional<DeclName> TypeChecker::omitNeedlessWords(AbstractFunctionDecl *afd) {
36803681
};
36813682

36823683
Identifier newBaseName = getReplacementIdentifier(baseNameStr,
3683-
name.getBaseName());
3684+
name.getBaseIdentifier());
36843685
SmallVector<Identifier, 4> newArgNames;
36853686
auto oldArgNames = name.getArgumentNames();
36863687
for (unsigned i = 0, n = argNameStrs.size(); i != n; ++i) {

lib/Sema/PlaygroundTransform.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ class Instrumenter : InstrumenterBase {
331331
ValueDecl *VD = nullptr;
332332
std::tie(RE, VD) = digForVariable(E);
333333
if (VD) {
334-
return VD->getName().str();
334+
return VD->getBaseName().getIdentifier().str();
335335
} else {
336336
return std::string("");
337337
}
@@ -639,7 +639,8 @@ class Instrumenter : InstrumenterBase {
639639
new (Context) MemberRefExpr(B, SourceLoc(), M, DeclNameLoc(),
640640
true, // implicit
641641
AccessSemantics::Ordinary),
642-
MRE->getSourceRange(), M.getDecl()->getName().str().str().c_str());
642+
MRE->getSourceRange(),
643+
M.getDecl()->getBaseName().getIdentifier().str().str().c_str());
643644
} else {
644645
return nullptr;
645646
}

lib/Sema/TypeCheckAttr.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,7 +1837,7 @@ void AttributeChecker::visitFixedLayoutAttr(FixedLayoutAttr *attr) {
18371837
if (VD->getEffectiveAccess() < Accessibility::Public) {
18381838
TC.diagnose(attr->getLocation(),
18391839
diag::fixed_layout_attr_on_internal_type,
1840-
VD->getName(),
1840+
VD->getBaseName(),
18411841
getAccessForDiagnostics(VD))
18421842
.fixItRemove(attr->getRangeWithAt());
18431843
attr->setInvalid();
@@ -1861,7 +1861,7 @@ void AttributeChecker::visitVersionedAttr(VersionedAttr *attr) {
18611861
if (VD->getFormalAccess() != Accessibility::Internal) {
18621862
TC.diagnose(attr->getLocation(),
18631863
diag::versioned_attr_with_explicit_accessibility,
1864-
VD->getName(),
1864+
VD->getBaseName(),
18651865
VD->getFormalAccess())
18661866
.fixItRemove(attr->getRangeWithAt());
18671867
attr->setInvalid();
@@ -1893,7 +1893,7 @@ void AttributeChecker::visitInlineableAttr(InlineableAttr *attr) {
18931893
VD->getFormalAccess() < Accessibility::Public)) {
18941894
TC.diagnose(attr->getLocation(),
18951895
diag::inlineable_decl_not_public,
1896-
VD->getName(),
1896+
VD->getBaseName(),
18971897
getAccessForDiagnostics(VD))
18981898
.fixItRemove(attr->getRangeWithAt());
18991899
attr->setInvalid();

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,10 +1951,10 @@ void TypeChecker::diagnoseUnavailableOverride(ValueDecl *override,
19511951
const AvailableAttr *attr) {
19521952
if (attr->Rename.empty()) {
19531953
if (attr->Message.empty())
1954-
diagnose(override, diag::override_unavailable, override->getName());
1954+
diagnose(override, diag::override_unavailable, override->getBaseName());
19551955
else
19561956
diagnose(override, diag::override_unavailable_msg,
1957-
override->getName(), attr->Message);
1957+
override->getBaseName(), attr->Message);
19581958
diagnose(base, diag::availability_marked_unavailable,
19591959
base->getFullName());
19601960
return;

lib/Sema/TypeCheckCaptures.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ class FindCapturedVars : public ASTWalker {
207207

208208
TC.diagnose(Loc, isDecl ? diag::decl_closure_noescape_use
209209
: diag::closure_noescape_use,
210-
VD->getName());
210+
VD->getBaseName());
211211

212212
// If we're a parameter, emit a helpful fixit to add @escaping
213213
auto paramDecl = dyn_cast<ParamDecl>(VD);
@@ -220,7 +220,8 @@ class FindCapturedVars : public ASTWalker {
220220
"@escaping ");
221221
} else if (isAutoClosure) {
222222
// TODO: add in a fixit for autoclosure
223-
TC.diagnose(VD->getLoc(), diag::noescape_autoclosure, VD->getName());
223+
TC.diagnose(VD->getLoc(), diag::noescape_autoclosure,
224+
VD->getBaseName());
224225
}
225226
}
226227
}
@@ -258,7 +259,7 @@ class FindCapturedVars : public ASTWalker {
258259
if (DC->isLocalContext()) {
259260
TC.diagnose(DRE->getLoc(), diag::capture_across_type_decl,
260261
NTD->getDescriptiveKind(),
261-
D->getName());
262+
D->getBaseName());
262263

263264
TC.diagnose(NTD->getLoc(), diag::type_declared_here);
264265

@@ -325,18 +326,18 @@ class FindCapturedVars : public ASTWalker {
325326
if (Diagnosed.insert(capturedDecl).second) {
326327
if (capturedDecl == DRE->getDecl()) {
327328
TC.diagnose(DRE->getLoc(), diag::capture_before_declaration,
328-
capturedDecl->getName());
329+
capturedDecl->getBaseName());
329330
} else {
330331
TC.diagnose(DRE->getLoc(),
331332
diag::transitive_capture_before_declaration,
332-
DRE->getDecl()->getName(),
333-
capturedDecl->getName());
333+
DRE->getDecl()->getBaseName(),
334+
capturedDecl->getBaseName());
334335
ValueDecl *prevDecl = capturedDecl;
335336
for (auto path : reversed(capturePath)) {
336337
TC.diagnose(path->getLoc(),
337338
diag::transitive_capture_through_here,
338339
path->getName(),
339-
prevDecl->getName());
340+
prevDecl->getBaseName());
340341
prevDecl = path;
341342
}
342343
}

0 commit comments

Comments
 (0)