Skip to content

DeclName-related cleanups #15265

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

Merged
merged 6 commits into from
Mar 15, 2018
Merged
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
4 changes: 2 additions & 2 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5340,6 +5340,8 @@ class FuncDecl : public AbstractFunctionDecl {
StaticLoc(StaticLoc), FuncLoc(FuncLoc),
OverriddenOrBehaviorParamDecl(),
Operator(nullptr) {
assert(!Name.getBaseName().isSpecial());

Bits.FuncDecl.IsStatic =
StaticLoc.isValid() || StaticSpelling != StaticSpellingKind::None;
Bits.FuncDecl.StaticSpelling = static_cast<unsigned>(StaticSpelling);
Expand Down Expand Up @@ -5915,8 +5917,6 @@ class ConstructorDecl : public AbstractFunctionDecl {
GenericParamList *GenericParams,
DeclContext *Parent);

Identifier getName() const { return getFullName().getBaseIdentifier(); }

void setParameterLists(ParamDecl *selfParam, ParameterList *bodyParams);

SourceLoc getConstructorLoc() const { return getNameLoc(); }
Expand Down
2 changes: 1 addition & 1 deletion include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ ERROR(ambiguous_module_type,none,
"ambiguous type name %0 in module %1", (Identifier, Identifier))
ERROR(use_nonmatching_operator,none,
"%0 is not a %select{binary|prefix unary|postfix unary}1 operator",
(Identifier, unsigned))
(DeclName, unsigned))
ERROR(broken_associated_type_witness,none,
"reference to invalid associated type %0 of type %1", (DeclName, Type))

Expand Down
2 changes: 1 addition & 1 deletion lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2224,7 +2224,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.getBaseIdentifier().str());
diag.fixItReplace(func->getLoc(), targetName.getBaseName().userFacingName());
}

// Fix the argument names that need fixing.
Expand Down
10 changes: 6 additions & 4 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4773,15 +4773,15 @@ AbstractFunctionDecl::getObjCSelector(DeclName preferredName) const {

auto &ctx = getASTContext();

Identifier baseName;
StringRef baseNameStr;
if (isa<DestructorDecl>(this)) {
// Deinitializers are always called "dealloc".
return ObjCSelector(ctx, 0, ctx.Id_dealloc);
} else if (auto func = dyn_cast<FuncDecl>(this)) {
// Otherwise cast this to be able to access getName()
baseName = func->getName();
baseNameStr = func->getName().str();
} else if (auto ctor = dyn_cast<ConstructorDecl>(this)) {
baseName = ctor->getName();
baseNameStr = "init";
} else {
llvm_unreachable("Unknown subclass of AbstractFunctionDecl");
}
Expand All @@ -4794,10 +4794,12 @@ AbstractFunctionDecl::getObjCSelector(DeclName preferredName) const {
if (argNames.size() != preferredName.getArgumentNames().size()) {
return ObjCSelector();
}
baseName = preferredName.getBaseIdentifier();
baseNameStr = preferredName.getBaseName().userFacingName();
argNames = preferredName.getArgumentNames();
}

auto baseName = ctx.getIdentifier(baseNameStr);

if (auto accessor = dyn_cast<AccessorDecl>(this)) {
// For a getter or setter, go through the variable or subscript decl.
auto asd = accessor->getStorage();
Expand Down
13 changes: 9 additions & 4 deletions lib/AST/SwiftNameTranslation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,28 +73,33 @@ printSwiftEnumElemNameInObjC(const EnumElementDecl *EL, llvm::raw_ostream &OS,
std::pair<Identifier, ObjCSelector> swift::objc_translation::
getObjCNameForSwiftDecl(const ValueDecl *VD, DeclName PreferredName){
ASTContext &Ctx = VD->getASTContext();
Identifier BaseName;
if (PreferredName) {
auto BaseNameStr = PreferredName.getBaseName().userFacingName();
BaseName = Ctx.getIdentifier(BaseNameStr);
}
if (auto *FD = dyn_cast<AbstractFunctionDecl>(VD)) {
return {Identifier(), FD->getObjCSelector(PreferredName)};
} else if (auto *VAD = dyn_cast<VarDecl>(VD)) {
if (PreferredName)
return {PreferredName.getBaseIdentifier(), ObjCSelector()};
return {BaseName, 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.getBaseIdentifier());
printSwiftEnumElemNameInObjC(EL, OS, BaseName);
}
return {Ctx.getIdentifier(Buffer.str()), ObjCSelector()};
} else {
// @objc(ExplicitName) > PreferredName > Swift name.
StringRef Name = getNameForObjC(VD, CustomNamesOnly);
if (!Name.empty())
return {Ctx.getIdentifier(Name), ObjCSelector()};
if (!PreferredName.getBaseName().empty())
return {PreferredName.getBaseIdentifier(), ObjCSelector()};
if (PreferredName)
return {BaseName, ObjCSelector()};
return {Ctx.getIdentifier(getNameForObjC(VD)), ObjCSelector()};
}
}
Expand Down
7 changes: 6 additions & 1 deletion lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3938,12 +3938,14 @@ namespace {

// Normal case applies when we're importing an older name, or when we're
// not an init
if (!isActiveSwiftVersion() || !isFactoryInit(importedName)) {
if (!isFactoryInit(importedName)) {
auto result = importNonInitObjCMethodDecl(decl, dc, importedName,
selector, forceClassMethod,
accessorInfo);

if (!isActiveSwiftVersion() && result)
markAsVariant(result, *correctSwiftName);

return result;
}

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

if (!isActiveSwiftVersion() && result)
markAsVariant(result, *correctSwiftName);

return result;
}

Expand Down
14 changes: 8 additions & 6 deletions lib/ClangImporter/ImportType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1704,11 +1704,13 @@ static bool isObjCMethodResultAudited(const clang::Decl *decl) {

DefaultArgumentKind ClangImporter::Implementation::inferDefaultArgument(
clang::QualType type, OptionalTypeKind clangOptionality,
Identifier baseName, unsigned numParams, StringRef argumentLabel,
DeclBaseName baseName, unsigned numParams, StringRef argumentLabel,
bool isFirstParameter, bool isLastParameter, NameImporter &nameImporter) {
auto baseNameStr = baseName.userFacingName();

// Don't introduce a default argument for setters with only a single
// parameter.
if (numParams == 1 && camel_case::getFirstWord(baseName.str()) == "set")
if (numParams == 1 && camel_case::getFirstWord(baseNameStr) == "set")
return DefaultArgumentKind::None;

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

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

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

auto emptyDictionaryKind = DefaultArgumentKind::EmptyDictionary;
if (clangOptionality == OTK_Optional)
Expand Down Expand Up @@ -2125,7 +2127,7 @@ ImportedType ClangImporter::Implementation::importMethodType(

auto defaultArg = inferDefaultArgument(
param->getType(), optionalityOfParam,
importedName.getDeclName().getBaseIdentifier(), numEffectiveParams,
importedName.getDeclName().getBaseName(), numEffectiveParams,
name.empty() ? StringRef() : name.str(), paramIndex == 0,
isLastParameter, getNameImporter());
if (defaultArg != DefaultArgumentKind::None)
Expand Down
2 changes: 1 addition & 1 deletion lib/ClangImporter/ImporterImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
/// given Clang \c type, \c baseName, and optionality.
static DefaultArgumentKind
inferDefaultArgument(clang::QualType type, OptionalTypeKind clangOptionality,
Identifier baseName, unsigned numParams,
DeclBaseName baseName, unsigned numParams,
StringRef argumentLabel, bool isFirstParameter,
bool isLastParameter, importer::NameImporter &);

Expand Down
2 changes: 1 addition & 1 deletion lib/IDE/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2851,7 +2851,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {

// Base name
addLeadingDot(Builder);
Builder.addTextChunk(AFD->getBaseName().getIdentifier().str());
Builder.addTextChunk(AFD->getBaseName().userFacingName());

// Add the argument labels.
auto ArgLabels = AFD->getFullName().getArgumentNames();
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/CalleeCandidateInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ void CalleeCandidateInfo::collectCalleeCandidates(Expr *fn,
// base uncurried by one level, and we refer to the name of the member, not to
// the name of any base.
if (auto UDE = dyn_cast<UnresolvedDotExpr>(fn)) {
declName = UDE->getName().getBaseIdentifier().str();
declName = UDE->getName().getBaseName().userFacingName();
uncurryLevel = 1;

// If we actually resolved the member to use, return it.
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/MiscDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3910,7 +3910,7 @@ Optional<DeclName> TypeChecker::omitNeedlessWords(AbstractFunctionDecl *afd) {
return None;

// String'ify the arguments.
StringRef baseNameStr = name.getBaseIdentifier().str();
StringRef baseNameStr = name.getBaseName().userFacingName();
SmallVector<StringRef, 4> argNameStrs;
for (auto arg : name.getArgumentNames()) {
if (arg.empty())
Expand Down
9 changes: 1 addition & 8 deletions lib/Sema/TypeCheckAvailability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1890,14 +1890,7 @@ describeRename(ASTContext &ctx, const AvailableAttr *attr, const ValueDecl *D,
name << parsed.ContextName << '.';

if (parsed.IsFunctionName) {
// FIXME: duplicated from above.
SmallVector<Identifier, 4> argumentLabelIDs;
std::transform(parsed.ArgumentLabels.begin(), parsed.ArgumentLabels.end(),
std::back_inserter(argumentLabelIDs),
[&ctx](StringRef labelStr) -> Identifier {
return labelStr.empty() ? Identifier() : ctx.getIdentifier(labelStr);
});
name << DeclName(ctx, ctx.getIdentifier(parsed.BaseName), argumentLabelIDs);
name << parsed.formDeclName(ctx);
} else {
name << parsed.BaseName;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE, DeclContext *DC) {
if (ResultValues.empty()) {
assert(UDRE->getRefKind() != DeclRefKind::Ordinary);
diagnose(Loc, diag::use_nonmatching_operator,
Name.getBaseName().getIdentifier(),
Name,
UDRE->getRefKind() == DeclRefKind::BinaryOperator ? 0 :
UDRE->getRefKind() == DeclRefKind::PrefixOperator ? 1 : 2);
return new (Context) ErrorExpr(UDRE->getSourceRange());
Expand Down
10 changes: 5 additions & 5 deletions lib/Sema/TypeCheckNameLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,8 @@ static unsigned getCallEditDistance(DeclName argName, DeclName paramName,
}
assert(argName.getBaseName().getKind() == DeclBaseName::Kind::Normal);

StringRef argBase = argName.getBaseIdentifier().str();
StringRef paramBase = paramName.getBaseIdentifier().str();
StringRef argBase = argName.getBaseName().userFacingName();
StringRef paramBase = paramName.getBaseName().userFacingName();

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

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

return tc.diagnose(parentDecl, diag::note_typo_candidate_implicit_member,
decl->getBaseName().getIdentifier().str(), kind);
decl->getBaseName().userFacingName(), kind);
}
}

return tc.diagnose(decl, diag::note_typo_candidate,
decl->getBaseName().getIdentifier().str());
decl->getBaseName().userFacingName());
}

void TypeChecker::noteTypoCorrection(DeclName writtenName, DeclNameLoc loc,
Expand All @@ -660,7 +660,7 @@ void TypeChecker::noteTypoCorrection(DeclName writtenName, DeclNameLoc loc,

if (writtenName.getBaseName() != declName.getBaseName())
diagnostic.fixItReplace(loc.getBaseNameLoc(),
declName.getBaseIdentifier().str());
declName.getBaseName().userFacingName());

// TODO: add fix-its for typo'ed argument labels. This is trickier
// because of the reordering rules.
Expand Down
3 changes: 3 additions & 0 deletions lib/Sema/TypeCheckPattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,9 @@ class ResolvePattern : public ASTVisitor<ResolvePattern,
subPattern = getSubExprPattern(arg);
}

if (ume->getName().getBaseName().isSpecial())
return nullptr;

// FIXME: Compound names.
return new (TC.Context) EnumElementPattern(
ume->getDotLoc(),
Expand Down
8 changes: 4 additions & 4 deletions test/APINotes/versioned-objc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ func testRenamedClassMembers(obj: ClassWithManyRenames) {
_ = ClassWithManyRenames.classWithManyRenamesForInt(0)
// CHECK-DIAGS-4: [[@LINE-1]]:{{[0-9]+}}: error: 'classWithManyRenamesForInt' has been replaced by 'init(for:)'

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,7 @@ static bool passNameInfoForDecl(ResolvedCursorInfo CursorInfo,
DeclName Name = Importer->importName(Named, ObjCName);
NameTranslatingInfo Result;
Result.NameKind = SwiftLangSupport::getUIDForNameKind(NameKind::Swift);
Result.BaseName = Name.getBaseIdentifier().str();
Result.BaseName = Name.getBaseName().userFacingName();
std::transform(Name.getArgumentNames().begin(),
Name.getArgumentNames().end(),
std::back_inserter(Result.ArgNames),
Expand Down