Skip to content

Commit 8c0eccf

Browse files
authored
Merge pull request #15265 from slavapestov/decl-name-cleanups
DeclName-related cleanups
2 parents 3c447df + 11d4912 commit 8c0eccf

17 files changed

+52
-42
lines changed

include/swift/AST/Decl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5340,6 +5340,8 @@ class FuncDecl : public AbstractFunctionDecl {
53405340
StaticLoc(StaticLoc), FuncLoc(FuncLoc),
53415341
OverriddenOrBehaviorParamDecl(),
53425342
Operator(nullptr) {
5343+
assert(!Name.getBaseName().isSpecial());
5344+
53435345
Bits.FuncDecl.IsStatic =
53445346
StaticLoc.isValid() || StaticSpelling != StaticSpellingKind::None;
53455347
Bits.FuncDecl.StaticSpelling = static_cast<unsigned>(StaticSpelling);
@@ -5915,8 +5917,6 @@ class ConstructorDecl : public AbstractFunctionDecl {
59155917
GenericParamList *GenericParams,
59165918
DeclContext *Parent);
59175919

5918-
Identifier getName() const { return getFullName().getBaseIdentifier(); }
5919-
59205920
void setParameterLists(ParamDecl *selfParam, ParameterList *bodyParams);
59215921

59225922
SourceLoc getConstructorLoc() const { return getNameLoc(); }

include/swift/AST/DiagnosticsSema.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ ERROR(ambiguous_module_type,none,
631631
"ambiguous type name %0 in module %1", (Identifier, Identifier))
632632
ERROR(use_nonmatching_operator,none,
633633
"%0 is not a %select{binary|prefix unary|postfix unary}1 operator",
634-
(Identifier, unsigned))
634+
(DeclName, unsigned))
635635
ERROR(broken_associated_type_witness,none,
636636
"reference to invalid associated type %0 of type %1", (DeclName, Type))
637637

lib/AST/ASTContext.cpp

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

22252225
// Fix the name of the function itself.
22262226
if (name.getBaseName() != targetName.getBaseName()) {
2227-
diag.fixItReplace(func->getLoc(), targetName.getBaseIdentifier().str());
2227+
diag.fixItReplace(func->getLoc(), targetName.getBaseName().userFacingName());
22282228
}
22292229

22302230
// Fix the argument names that need fixing.

lib/AST/Decl.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4773,15 +4773,15 @@ AbstractFunctionDecl::getObjCSelector(DeclName preferredName) const {
47734773

47744774
auto &ctx = getASTContext();
47754775

4776-
Identifier baseName;
4776+
StringRef baseNameStr;
47774777
if (isa<DestructorDecl>(this)) {
47784778
// Deinitializers are always called "dealloc".
47794779
return ObjCSelector(ctx, 0, ctx.Id_dealloc);
47804780
} else if (auto func = dyn_cast<FuncDecl>(this)) {
47814781
// Otherwise cast this to be able to access getName()
4782-
baseName = func->getName();
4782+
baseNameStr = func->getName().str();
47834783
} else if (auto ctor = dyn_cast<ConstructorDecl>(this)) {
4784-
baseName = ctor->getName();
4784+
baseNameStr = "init";
47854785
} else {
47864786
llvm_unreachable("Unknown subclass of AbstractFunctionDecl");
47874787
}
@@ -4794,10 +4794,12 @@ AbstractFunctionDecl::getObjCSelector(DeclName preferredName) const {
47944794
if (argNames.size() != preferredName.getArgumentNames().size()) {
47954795
return ObjCSelector();
47964796
}
4797-
baseName = preferredName.getBaseIdentifier();
4797+
baseNameStr = preferredName.getBaseName().userFacingName();
47984798
argNames = preferredName.getArgumentNames();
47994799
}
48004800

4801+
auto baseName = ctx.getIdentifier(baseNameStr);
4802+
48014803
if (auto accessor = dyn_cast<AccessorDecl>(this)) {
48024804
// For a getter or setter, go through the variable or subscript decl.
48034805
auto asd = accessor->getStorage();

lib/AST/SwiftNameTranslation.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,28 +73,33 @@ printSwiftEnumElemNameInObjC(const EnumElementDecl *EL, llvm::raw_ostream &OS,
7373
std::pair<Identifier, ObjCSelector> swift::objc_translation::
7474
getObjCNameForSwiftDecl(const ValueDecl *VD, DeclName PreferredName){
7575
ASTContext &Ctx = VD->getASTContext();
76+
Identifier BaseName;
77+
if (PreferredName) {
78+
auto BaseNameStr = PreferredName.getBaseName().userFacingName();
79+
BaseName = Ctx.getIdentifier(BaseNameStr);
80+
}
7681
if (auto *FD = dyn_cast<AbstractFunctionDecl>(VD)) {
7782
return {Identifier(), FD->getObjCSelector(PreferredName)};
7883
} else if (auto *VAD = dyn_cast<VarDecl>(VD)) {
7984
if (PreferredName)
80-
return {PreferredName.getBaseIdentifier(), ObjCSelector()};
85+
return {BaseName, ObjCSelector()};
8186
return {VAD->getObjCPropertyName(), ObjCSelector()};
8287
} else if (auto *SD = dyn_cast<SubscriptDecl>(VD)) {
8388
return getObjCNameForSwiftDecl(SD->getGetter(), PreferredName);
8489
} else if (auto *EL = dyn_cast<EnumElementDecl>(VD)) {
8590
SmallString<64> Buffer;
8691
{
8792
llvm::raw_svector_ostream OS(Buffer);
88-
printSwiftEnumElemNameInObjC(EL, OS, PreferredName.getBaseIdentifier());
93+
printSwiftEnumElemNameInObjC(EL, OS, BaseName);
8994
}
9095
return {Ctx.getIdentifier(Buffer.str()), ObjCSelector()};
9196
} else {
9297
// @objc(ExplicitName) > PreferredName > Swift name.
9398
StringRef Name = getNameForObjC(VD, CustomNamesOnly);
9499
if (!Name.empty())
95100
return {Ctx.getIdentifier(Name), ObjCSelector()};
96-
if (!PreferredName.getBaseName().empty())
97-
return {PreferredName.getBaseIdentifier(), ObjCSelector()};
101+
if (PreferredName)
102+
return {BaseName, ObjCSelector()};
98103
return {Ctx.getIdentifier(getNameForObjC(VD)), ObjCSelector()};
99104
}
100105
}

lib/ClangImporter/ImportDecl.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3938,12 +3938,14 @@ namespace {
39383938

39393939
// Normal case applies when we're importing an older name, or when we're
39403940
// not an init
3941-
if (!isActiveSwiftVersion() || !isFactoryInit(importedName)) {
3941+
if (!isFactoryInit(importedName)) {
39423942
auto result = importNonInitObjCMethodDecl(decl, dc, importedName,
39433943
selector, forceClassMethod,
39443944
accessorInfo);
3945+
39453946
if (!isActiveSwiftVersion() && result)
39463947
markAsVariant(result, *correctSwiftName);
3948+
39473949
return result;
39483950
}
39493951

@@ -3963,6 +3965,9 @@ namespace {
39633965
{decl->param_begin(), decl->param_size()},
39643966
decl->isVariadic(), redundant);
39653967

3968+
if (!isActiveSwiftVersion() && result)
3969+
markAsVariant(result, *correctSwiftName);
3970+
39663971
return result;
39673972
}
39683973

lib/ClangImporter/ImportType.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,11 +1704,13 @@ static bool isObjCMethodResultAudited(const clang::Decl *decl) {
17041704

17051705
DefaultArgumentKind ClangImporter::Implementation::inferDefaultArgument(
17061706
clang::QualType type, OptionalTypeKind clangOptionality,
1707-
Identifier baseName, unsigned numParams, StringRef argumentLabel,
1707+
DeclBaseName baseName, unsigned numParams, StringRef argumentLabel,
17081708
bool isFirstParameter, bool isLastParameter, NameImporter &nameImporter) {
1709+
auto baseNameStr = baseName.userFacingName();
1710+
17091711
// Don't introduce a default argument for setters with only a single
17101712
// parameter.
1711-
if (numParams == 1 && camel_case::getFirstWord(baseName.str()) == "set")
1713+
if (numParams == 1 && camel_case::getFirstWord(baseNameStr) == "set")
17121714
return DefaultArgumentKind::None;
17131715

17141716
// Some nullable parameters default to 'nil'.
@@ -1730,7 +1732,7 @@ DefaultArgumentKind ClangImporter::Implementation::inferDefaultArgument(
17301732
}
17311733

17321734
// Don't introduce an empty options default arguments for setters.
1733-
if (isFirstParameter && camel_case::getFirstWord(baseName.str()) == "set")
1735+
if (isFirstParameter && camel_case::getFirstWord(baseNameStr) == "set")
17341736
return DefaultArgumentKind::None;
17351737

17361738
// Option sets default to "[]" if they have "Options" in their name.
@@ -1752,8 +1754,8 @@ DefaultArgumentKind ClangImporter::Implementation::inferDefaultArgument(
17521754
if (auto objcClass = objcPtrTy->getInterfaceDecl()) {
17531755
if (objcClass->getName() == "NSDictionary") {
17541756
StringRef searchStr = argumentLabel;
1755-
if (searchStr.empty() && !baseName.empty())
1756-
searchStr = baseName.str();
1757+
if (searchStr.empty() && !baseNameStr.empty())
1758+
searchStr = baseNameStr;
17571759

17581760
auto emptyDictionaryKind = DefaultArgumentKind::EmptyDictionary;
17591761
if (clangOptionality == OTK_Optional)
@@ -2125,7 +2127,7 @@ ImportedType ClangImporter::Implementation::importMethodType(
21252127

21262128
auto defaultArg = inferDefaultArgument(
21272129
param->getType(), optionalityOfParam,
2128-
importedName.getDeclName().getBaseIdentifier(), numEffectiveParams,
2130+
importedName.getDeclName().getBaseName(), numEffectiveParams,
21292131
name.empty() ? StringRef() : name.str(), paramIndex == 0,
21302132
isLastParameter, getNameImporter());
21312133
if (defaultArg != DefaultArgumentKind::None)

lib/ClangImporter/ImporterImpl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1111,7 +1111,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
11111111
/// given Clang \c type, \c baseName, and optionality.
11121112
static DefaultArgumentKind
11131113
inferDefaultArgument(clang::QualType type, OptionalTypeKind clangOptionality,
1114-
Identifier baseName, unsigned numParams,
1114+
DeclBaseName baseName, unsigned numParams,
11151115
StringRef argumentLabel, bool isFirstParameter,
11161116
bool isLastParameter, importer::NameImporter &);
11171117

lib/IDE/CodeCompletion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2851,7 +2851,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
28512851

28522852
// Base name
28532853
addLeadingDot(Builder);
2854-
Builder.addTextChunk(AFD->getBaseName().getIdentifier().str());
2854+
Builder.addTextChunk(AFD->getBaseName().userFacingName());
28552855

28562856
// Add the argument labels.
28572857
auto ArgLabels = AFD->getFullName().getArgumentNames();

lib/Sema/CalleeCandidateInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ void CalleeCandidateInfo::collectCalleeCandidates(Expr *fn,
663663
// base uncurried by one level, and we refer to the name of the member, not to
664664
// the name of any base.
665665
if (auto UDE = dyn_cast<UnresolvedDotExpr>(fn)) {
666-
declName = UDE->getName().getBaseIdentifier().str();
666+
declName = UDE->getName().getBaseName().userFacingName();
667667
uncurryLevel = 1;
668668

669669
// If we actually resolved the member to use, return it.

lib/Sema/MiscDiagnostics.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3910,7 +3910,7 @@ Optional<DeclName> TypeChecker::omitNeedlessWords(AbstractFunctionDecl *afd) {
39103910
return None;
39113911

39123912
// String'ify the arguments.
3913-
StringRef baseNameStr = name.getBaseIdentifier().str();
3913+
StringRef baseNameStr = name.getBaseName().userFacingName();
39143914
SmallVector<StringRef, 4> argNameStrs;
39153915
for (auto arg : name.getArgumentNames()) {
39163916
if (arg.empty())

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1890,14 +1890,7 @@ describeRename(ASTContext &ctx, const AvailableAttr *attr, const ValueDecl *D,
18901890
name << parsed.ContextName << '.';
18911891

18921892
if (parsed.IsFunctionName) {
1893-
// FIXME: duplicated from above.
1894-
SmallVector<Identifier, 4> argumentLabelIDs;
1895-
std::transform(parsed.ArgumentLabels.begin(), parsed.ArgumentLabels.end(),
1896-
std::back_inserter(argumentLabelIDs),
1897-
[&ctx](StringRef labelStr) -> Identifier {
1898-
return labelStr.empty() ? Identifier() : ctx.getIdentifier(labelStr);
1899-
});
1900-
name << DeclName(ctx, ctx.getIdentifier(parsed.BaseName), argumentLabelIDs);
1893+
name << parsed.formDeclName(ctx);
19011894
} else {
19021895
name << parsed.BaseName;
19031896
}

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE, DeclContext *DC) {
553553
if (ResultValues.empty()) {
554554
assert(UDRE->getRefKind() != DeclRefKind::Ordinary);
555555
diagnose(Loc, diag::use_nonmatching_operator,
556-
Name.getBaseName().getIdentifier(),
556+
Name,
557557
UDRE->getRefKind() == DeclRefKind::BinaryOperator ? 0 :
558558
UDRE->getRefKind() == DeclRefKind::PrefixOperator ? 1 : 2);
559559
return new (Context) ErrorExpr(UDRE->getSourceRange());

lib/Sema/TypeCheckNameLookup.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -499,8 +499,8 @@ static unsigned getCallEditDistance(DeclName argName, DeclName paramName,
499499
}
500500
assert(argName.getBaseName().getKind() == DeclBaseName::Kind::Normal);
501501

502-
StringRef argBase = argName.getBaseIdentifier().str();
503-
StringRef paramBase = paramName.getBaseIdentifier().str();
502+
StringRef argBase = argName.getBaseName().userFacingName();
503+
StringRef paramBase = paramName.getBaseName().userFacingName();
504504

505505
unsigned distance = argBase.edit_distance(paramBase, maxEditDistance);
506506

@@ -644,12 +644,12 @@ diagnoseTypoCorrection(TypeChecker &tc, DeclNameLoc loc, ValueDecl *decl) {
644644
"member");
645645

646646
return tc.diagnose(parentDecl, diag::note_typo_candidate_implicit_member,
647-
decl->getBaseName().getIdentifier().str(), kind);
647+
decl->getBaseName().userFacingName(), kind);
648648
}
649649
}
650650

651651
return tc.diagnose(decl, diag::note_typo_candidate,
652-
decl->getBaseName().getIdentifier().str());
652+
decl->getBaseName().userFacingName());
653653
}
654654

655655
void TypeChecker::noteTypoCorrection(DeclName writtenName, DeclNameLoc loc,
@@ -660,7 +660,7 @@ void TypeChecker::noteTypoCorrection(DeclName writtenName, DeclNameLoc loc,
660660

661661
if (writtenName.getBaseName() != declName.getBaseName())
662662
diagnostic.fixItReplace(loc.getBaseNameLoc(),
663-
declName.getBaseIdentifier().str());
663+
declName.getBaseName().userFacingName());
664664

665665
// TODO: add fix-its for typo'ed argument labels. This is trickier
666666
// because of the reordering rules.

lib/Sema/TypeCheckPattern.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,9 @@ class ResolvePattern : public ASTVisitor<ResolvePattern,
437437
subPattern = getSubExprPattern(arg);
438438
}
439439

440+
if (ume->getName().getBaseName().isSpecial())
441+
return nullptr;
442+
440443
// FIXME: Compound names.
441444
return new (TC.Context) EnumElementPattern(
442445
ume->getDotLoc(),

test/APINotes/versioned-objc.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ func testRenamedClassMembers(obj: ClassWithManyRenames) {
4848
_ = ClassWithManyRenames.classWithManyRenamesForInt(0)
4949
// CHECK-DIAGS-4: [[@LINE-1]]:{{[0-9]+}}: error: 'classWithManyRenamesForInt' has been replaced by 'init(for:)'
5050

51-
// CHECK-DIAGS-3: [[@LINE+1]]:{{[0-9]+}}: error: 'init(forInt:)' has been replaced by 'init(swift3Factory:)'
51+
// CHECK-DIAGS-3: [[@LINE+1]]:{{[0-9]+}}: error: 'init(forInt:)' has been renamed to 'init(swift3Factory:)'
5252
_ = ClassWithManyRenames(forInt: 0)
53-
// CHECK-DIAGS-4: [[@LINE-1]]:{{[0-9]+}}: error: 'init(forInt:)' has been replaced by 'init(for:)'
53+
// CHECK-DIAGS-4: [[@LINE-1]]:{{[0-9]+}}: error: 'init(forInt:)' has been renamed to 'init(for:)'
5454

5555
// CHECK-DIAGS-3-NOT: :[[@LINE+1]]:{{[0-9]+}}:
5656
_ = ClassWithManyRenames(swift3Factory: 0)
57-
// CHECK-DIAGS-4: [[@LINE-1]]:{{[0-9]+}}: error: 'init(swift3Factory:)' has been replaced by 'init(for:)'
57+
// CHECK-DIAGS-4: [[@LINE-1]]:{{[0-9]+}}: error: 'init(swift3Factory:)' has been renamed to 'init(for:)'
5858

59-
// CHECK-DIAGS-3: [[@LINE+1]]:{{[0-9]+}}: error: 'init(for:)' has been replaced by 'init(swift3Factory:)'
59+
// CHECK-DIAGS-3: [[@LINE+1]]:{{[0-9]+}}: error: 'init(for:)' has been renamed to 'init(swift3Factory:)'
6060
_ = ClassWithManyRenames(for: 0)
6161
// CHECK-DIAGS-4-NOT: :[[@LINE-1]]:{{[0-9]+}}:
6262

tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,7 @@ static bool passNameInfoForDecl(ResolvedCursorInfo CursorInfo,
10971097
DeclName Name = Importer->importName(Named, ObjCName);
10981098
NameTranslatingInfo Result;
10991099
Result.NameKind = SwiftLangSupport::getUIDForNameKind(NameKind::Swift);
1100-
Result.BaseName = Name.getBaseIdentifier().str();
1100+
Result.BaseName = Name.getBaseName().userFacingName();
11011101
std::transform(Name.getArgumentNames().begin(),
11021102
Name.getArgumentNames().end(),
11031103
std::back_inserter(Result.ArgNames),

0 commit comments

Comments
 (0)