Skip to content

Commit 325a969

Browse files
authored
[AST] Preparations for removal of getName on ValueDecl (#9969)
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 c0ccdb1 commit 325a969

File tree

10 files changed

+40
-36
lines changed

10 files changed

+40
-36
lines changed

include/swift/AST/ReferencedNameTracker.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ public: \
3232
return NAME##s; \
3333
}
3434

35-
TRACKED_SET(Identifier, TopLevelName)
36-
TRACKED_SET(Identifier, DynamicLookupName)
35+
TRACKED_SET(DeclBaseName, TopLevelName)
36+
TRACKED_SET(DeclBaseName, DynamicLookupName)
3737

38-
using MemberPair = std::pair<const NominalTypeDecl *, Identifier>;
38+
using MemberPair = std::pair<const NominalTypeDecl *, DeclBaseName>;
3939
TRACKED_SET(MemberPair, UsedMember)
4040

4141
#undef TRACKED_SET

lib/AST/ASTContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2432,7 +2432,7 @@ bool ASTContext::diagnoseObjCMethodConflicts(SourceFile &sf) {
24322432
Diags.diagnose(conflictingDecl, diag::objc_redecl_same,
24332433
diagInfo.first, diagInfo.second, selector);
24342434
Diags.diagnose(originalDecl, diag::invalid_redecl_prev,
2435-
originalDecl->getName());
2435+
originalDecl->getBaseName());
24362436
} else {
24372437
Diags.diagnose(conflictingDecl, diag::objc_redecl,
24382438
diagInfo.first,

lib/AST/ASTMangler.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,8 @@ static unsigned getUnnamedParamIndex(const ParamDecl *D) {
503503

504504
void ASTMangler::appendDeclName(const ValueDecl *decl) {
505505
if (decl->isOperator()) {
506-
appendIdentifier(translateOperator(decl->getName().str()));
506+
auto name = decl->getBaseName().getIdentifier().str();
507+
appendIdentifier(translateOperator(name));
507508
switch (decl->getAttrs().getUnaryOperatorKind()) {
508509
case UnaryOperatorKind::Prefix:
509510
appendOperator("op");
@@ -516,7 +517,8 @@ void ASTMangler::appendDeclName(const ValueDecl *decl) {
516517
break;
517518
}
518519
} else if (decl->hasName()) {
519-
appendIdentifier(decl->getName().str());
520+
// TODO: Handle special names
521+
appendIdentifier(decl->getBaseName().getIdentifier().str());
520522
} else {
521523
assert(AllowNamelessEntities && "attempt to mangle unnamed decl");
522524
// Fall back to an unlikely name, so that we still generate a valid
@@ -1908,7 +1910,7 @@ void ASTMangler::appendProtocolConformance(const ProtocolConformance *conformanc
19081910
appendIdentifier(
19091911
fileUnit->getDiscriminatorForPrivateValue(behaviorStorage).str());
19101912
appendProtocolName(conformance->getProtocol());
1911-
appendIdentifier(behaviorStorage->getName().str());
1913+
appendIdentifier(behaviorStorage->getBaseName().getIdentifier().str());
19121914
} else {
19131915
auto conformanceDC = conformance->getDeclContext();
19141916
auto conformingType =

lib/AST/Decl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4399,7 +4399,7 @@ DeclName AbstractFunctionDecl::getEffectiveFullName() const {
43994399
case AccessorKind::IsMutableAddressor:
44004400
case AccessorKind::IsGetter:
44014401
return subscript ? subscript->getFullName()
4402-
: DeclName(ctx, afd->getName(),
4402+
: DeclName(ctx, afd->getBaseName(),
44034403
ArrayRef<Identifier>());
44044404

44054405
case AccessorKind::IsSetter:
@@ -4417,7 +4417,7 @@ DeclName AbstractFunctionDecl::getEffectiveFullName() const {
44174417
argNames.append(subscript->getFullName().getArgumentNames().begin(),
44184418
subscript->getFullName().getArgumentNames().end());
44194419
}
4420-
return DeclName(ctx, afd->getName(), argNames);
4420+
return DeclName(ctx, afd->getBaseName(), argNames);
44214421
}
44224422
}
44234423
}

lib/AST/LookupVisibleDecls.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ static void doDynamicLookup(VisibleDeclConsumer &Consumer,
278278
LookupState LS;
279279
const DeclContext *CurrDC;
280280
LazyResolver *TypeResolver;
281-
llvm::DenseSet<std::pair<Identifier, CanType>> FunctionsReported;
281+
llvm::DenseSet<std::pair<DeclBaseName, CanType>> FunctionsReported;
282282
llvm::DenseSet<CanType> SubscriptsReported;
283283
llvm::DenseSet<std::pair<Identifier, CanType>> PropertiesReported;
284284

@@ -349,7 +349,7 @@ static void doDynamicLookup(VisibleDeclConsumer &Consumer,
349349
->getResult()
350350
->getCanonicalType();
351351

352-
auto Signature = std::make_pair(D->getName(), T);
352+
auto Signature = std::make_pair(D->getBaseName(), T);
353353
if (!FunctionsReported.insert(Signature).second)
354354
return;
355355
break;
@@ -678,7 +678,7 @@ static bool shouldSubstIntoDeclType(Type type) {
678678
class OverrideFilteringConsumer : public VisibleDeclConsumer {
679679
public:
680680
std::set<ValueDecl *> AllFoundDecls;
681-
std::map<Identifier, std::set<ValueDecl *>> FoundDecls;
681+
std::map<DeclBaseName, std::set<ValueDecl *>> FoundDecls;
682682
llvm::SetVector<FoundDeclTy> DeclsToReport;
683683
Type BaseTy;
684684
const DeclContext *DC;
@@ -710,11 +710,11 @@ class OverrideFilteringConsumer : public VisibleDeclConsumer {
710710
}
711711

712712
if (VD->isInvalid()) {
713-
FoundDecls[VD->getName()].insert(VD);
713+
FoundDecls[VD->getBaseName()].insert(VD);
714714
DeclsToReport.insert(FoundDeclTy(VD, Reason));
715715
return;
716716
}
717-
auto &PossiblyConflicting = FoundDecls[VD->getName()];
717+
auto &PossiblyConflicting = FoundDecls[VD->getBaseName()];
718718

719719
// Check all overridden decls.
720720
{

lib/AST/ModuleNameLookup.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace {
3333

3434
using CanTypeSet = llvm::SmallSet<CanType, 4, SortCanType>;
3535
using NamedCanTypeSet =
36-
llvm::DenseMap<Identifier, std::pair<ResolutionKind, CanTypeSet>>;
36+
llvm::DenseMap<DeclBaseName, std::pair<ResolutionKind, CanTypeSet>>;
3737
static_assert(ResolutionKind() == ResolutionKind::Overloadable,
3838
"Entries in NamedCanTypeSet should be overloadable initially");
3939
} // end anonymous namespace
@@ -53,7 +53,7 @@ static bool isValidOverload(CanTypeSet &overloads, const ValueDecl *VD) {
5353
}
5454

5555
static bool isValidOverload(NamedCanTypeSet &overloads, const ValueDecl *VD) {
56-
auto &entry = overloads[VD->getName()];
56+
auto &entry = overloads[VD->getBaseName()];
5757
if (entry.first != ResolutionKind::Overloadable)
5858
return false;
5959
return isValidOverload(entry.second, VD);
@@ -80,7 +80,7 @@ static bool updateOverloadSet(CanTypeSet &overloads,
8080
static bool updateOverloadSet(NamedCanTypeSet &overloads,
8181
ArrayRef<ValueDecl *> decls) {
8282
for (auto result : decls) {
83-
auto &entry = overloads[result->getName()];
83+
auto &entry = overloads[result->getBaseName()];
8484
if (!isOverloadable(result))
8585
entry.first = ResolutionKind::Exact;
8686
else if (!result->hasInterfaceType())

lib/AST/NameLookup.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ bool swift::removeShadowedDecls(SmallVectorImpl<ValueDecl*> &decls,
139139
const ModuleDecl *curModule,
140140
LazyResolver *typeResolver) {
141141
// Category declarations by their signatures.
142-
llvm::SmallDenseMap<std::pair<CanType, Identifier>,
142+
llvm::SmallDenseMap<std::pair<CanType, DeclBaseName>,
143143
llvm::TinyPtrVector<ValueDecl *>>
144144
CollidingDeclGroups;
145145

@@ -174,7 +174,7 @@ bool swift::removeShadowedDecls(SmallVectorImpl<ValueDecl*> &decls,
174174

175175
// If we've seen a declaration with this signature before, note it.
176176
auto &knownDecls =
177-
CollidingDeclGroups[std::make_pair(signature, decl->getName())];
177+
CollidingDeclGroups[std::make_pair(signature, decl->getBaseName())];
178178
if (!knownDecls.empty())
179179
anyCollisions = true;
180180

lib/AST/SourceEntityWalker.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,9 @@ bool SemaAnnotator::walkToDeclPre(Decl *D) {
9595
bool IsExtension = false;
9696

9797
if (auto *VD = dyn_cast<ValueDecl>(D)) {
98+
// TODO: Handle special names
9899
if (VD->hasName() && !VD->isImplicit())
99-
NameLen = VD->getName().getLength();
100+
NameLen = VD->getBaseName().getIdentifier().getLength();
100101

101102
auto ReportParamList = [&](ParameterList *PL) {
102103
for (auto *PD : *PL) {

lib/AST/SwiftNameTranslation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ getNameForObjC(const ValueDecl *VD, CustomNamesOnly_t customNamesOnly) {
4747
return anonTypedef->getIdentifier()->getName();
4848
}
4949

50-
return VD->getName().str();
50+
return VD->getBaseName().getIdentifier().str();
5151
}
5252

5353
bool swift::objc_translation::

lib/FrontendTool/ReferenceDependencies.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ bool swift::emitReferenceDependencies(DiagnosticEngine &diags,
237237
VD->getFormalAccess() <= Accessibility::FilePrivate) {
238238
break;
239239
}
240-
out << "- \"" << escape(VD->getName()) << "\"\n";
240+
out << "- \"" << escape(VD->getBaseName()) << "\"\n";
241241
break;
242242
}
243243

@@ -292,7 +292,7 @@ bool swift::emitReferenceDependencies(DiagnosticEngine &diags,
292292
continue;
293293
}
294294
out << "- [\"" << mangledName << "\", \""
295-
<< escape(VD->getName()) << "\"]\n";
295+
<< escape(VD->getBaseName()) << "\"]\n";
296296
}
297297
}
298298

@@ -303,14 +303,15 @@ bool swift::emitReferenceDependencies(DiagnosticEngine &diags,
303303
out << "provides-dynamic-lookup:\n";
304304
class NameCollector : public VisibleDeclConsumer {
305305
private:
306-
SmallVector<Identifier, 16> names;
306+
SmallVector<DeclBaseName, 16> names;
307307
public:
308308
void foundDecl(ValueDecl *VD, DeclVisibilityKind Reason) override {
309-
names.push_back(VD->getName());
309+
names.push_back(VD->getBaseName());
310310
}
311-
ArrayRef<Identifier> getNames() {
311+
ArrayRef<DeclBaseName> getNames() {
312312
llvm::array_pod_sort(names.begin(), names.end(),
313-
[](const Identifier *lhs, const Identifier *rhs) {
313+
[](const DeclBaseName *lhs,
314+
const DeclBaseName *rhs) {
314315
return lhs->compare(*rhs);
315316
});
316317
names.erase(std::unique(names.begin(), names.end()), names.end());
@@ -319,27 +320,27 @@ bool swift::emitReferenceDependencies(DiagnosticEngine &diags,
319320
};
320321
NameCollector collector;
321322
SF->lookupClassMembers({}, collector);
322-
for (Identifier name : collector.getNames()) {
323+
for (DeclBaseName name : collector.getNames()) {
323324
out << "- \"" << escape(name) << "\"\n";
324325
}
325326
}
326327

327328
ReferencedNameTracker *tracker = SF->getReferencedNameTracker();
328329

329-
auto sortedByIdentifier =
330-
[](const llvm::DenseMap<Identifier, bool> map) ->
331-
SmallVector<std::pair<Identifier, bool>, 16> {
332-
SmallVector<std::pair<Identifier, bool>, 16> pairs{map.begin(), map.end()};
330+
auto sortedByName =
331+
[](const llvm::DenseMap<DeclBaseName, bool> map) ->
332+
SmallVector<std::pair<DeclBaseName, bool>, 16> {
333+
SmallVector<std::pair<DeclBaseName,bool>, 16> pairs{map.begin(), map.end()};
333334
llvm::array_pod_sort(pairs.begin(), pairs.end(),
334-
[](const std::pair<Identifier, bool> *first,
335-
const std::pair<Identifier, bool> *second) -> int {
335+
[](const std::pair<DeclBaseName, bool> *first,
336+
const std::pair<DeclBaseName, bool> *second) -> int{
336337
return first->first.compare(second->first);
337338
});
338339
return pairs;
339340
};
340341

341342
out << "depends-top-level:\n";
342-
for (auto &entry : sortedByIdentifier(tracker->getTopLevelNames())) {
343+
for (auto &entry : sortedByName(tracker->getTopLevelNames())) {
343344
assert(!entry.first.empty());
344345
out << "- ";
345346
if (!entry.second)
@@ -406,7 +407,7 @@ bool swift::emitReferenceDependencies(DiagnosticEngine &diags,
406407
}
407408

408409
out << "depends-dynamic-lookup:\n";
409-
for (auto &entry : sortedByIdentifier(tracker->getDynamicLookupNames())) {
410+
for (auto &entry : sortedByName(tracker->getDynamicLookupNames())) {
410411
assert(!entry.first.empty());
411412
out << "- ";
412413
if (!entry.second)

0 commit comments

Comments
 (0)