Skip to content

Adjust printing to take into account special DeclNames #9979

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 1 commit into from
Jun 25, 2017
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
31 changes: 24 additions & 7 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2123,13 +2123,6 @@ class ValueDecl : public Decl {
bool hasName() const { return bool(Name); }
bool isOperator() const { return Name.isOperator(); }

/// Returns the string for the base name, or "_" if this is unnamed.
StringRef getNameStr() const {
// TODO: Check if this function is called for special names
assert(!Name.isSpecial() && "Cannot get string for special names");
return hasName() ? Name.getBaseName().getIdentifier().str() : "_";
}

/// Retrieve the full name of the declaration.
/// TODO: Rename to getName?
DeclName getFullName() const { return Name; }
Expand Down Expand Up @@ -2358,6 +2351,12 @@ class TypeDecl : public ValueDecl {
public:
Identifier getName() const { return getFullName().getBaseIdentifier(); }

/// Returns the string for the base name, or "_" if this is unnamed.
StringRef getNameStr() const {
assert(!getFullName().isSpecial() && "Cannot get string for special names");
return hasName() ? getBaseName().getIdentifier().str() : "_";
}

/// The type of this declaration's values. For the type of the
/// declaration itself, use getInterfaceType(), which returns a
/// metatype.
Expand Down Expand Up @@ -4356,6 +4355,12 @@ class VarDecl : public AbstractStorageDecl {

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

/// Returns the string for the base name, or "_" if this is unnamed.
StringRef getNameStr() const {
assert(!getFullName().isSpecial() && "Cannot get string for special names");
return hasName() ? getBaseName().getIdentifier().str() : "_";
}

TypeLoc &getTypeLoc() { return typeLoc; }
TypeLoc getTypeLoc() const { return typeLoc; }

Expand Down Expand Up @@ -4827,6 +4832,12 @@ class AbstractFunctionDecl : public ValueDecl, public GenericContext {
public:
Identifier getName() const { return getFullName().getBaseIdentifier(); }

/// Returns the string for the base name, or "_" if this is unnamed.
StringRef getNameStr() const {
assert(!getFullName().isSpecial() && "Cannot get string for special names");
return hasName() ? getBaseName().getIdentifier().str() : "_";
}

/// \brief Should this declaration be treated as if annotated with transparent
/// attribute.
bool isTransparent() const;
Expand Down Expand Up @@ -5458,6 +5469,12 @@ class EnumElementDecl : public ValueDecl {

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

/// Returns the string for the base name, or "_" if this is unnamed.
StringRef getNameStr() const {
assert(!getFullName().isSpecial() && "Cannot get string for special names");
return hasName() ? getBaseName().getIdentifier().str() : "_";
}

/// \returns false if there was an error during the computation rendering the
/// EnumElementDecl invalid, true otherwise.
bool computeType();
Expand Down
8 changes: 8 additions & 0 deletions include/swift/AST/Identifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,14 @@ class DeclBaseName {
return !isSpecial() && getIdentifier().isEditorPlaceholder();
}

/// A representation of the name to be displayed to users. May be ambiguous
/// between identifiers and special names.
StringRef userFacingName() const {
if (empty())
return "_";
return getIdentifier().str();
}

int compare(DeclBaseName other) const {
// TODO: Sort special names cleverly
return getIdentifier().compare(other.getIdentifier());
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/ASTVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3259,7 +3259,7 @@ bool swift::shouldVerify(const Decl *D, const ASTContext &Context) {
return true;
}

size_t Hash = llvm::hash_value(VD->getNameStr());
size_t Hash = llvm::hash_value(VD->getBaseName().userFacingName());
return Hash % ProcessCount == ProcessId;
#else
return false;
Expand Down
3 changes: 2 additions & 1 deletion lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,8 @@ bool Decl::isPrivateStdlibDecl(bool whitelistProtocols) const {
return false;

// If the name has leading underscore then it's a private symbol.
if (VD->getNameStr().startswith("_"))
if (!VD->getBaseName().isSpecial() &&
VD->getBaseName().getIdentifier().str().startswith("_"))
return true;

return false;
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/DiagnosticEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ void DiagnosticEngine::emitDiagnostic(const Diagnostic &diagnostic) {
}

if (auto value = dyn_cast<ValueDecl>(ppDecl)) {
bufferName += value->getNameStr();
bufferName += value->getBaseName().userFacingName();
} else if (auto ext = dyn_cast<ExtensionDecl>(ppDecl)) {
bufferName += ext->getExtendedType().getString();
}
Expand Down
5 changes: 2 additions & 3 deletions lib/AST/Identifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ raw_ostream &llvm::operator<<(raw_ostream &OS, Identifier I) {
return OS << I.get();
}

raw_ostream &llvm::operator<<(raw_ostream &OS, DeclBaseName I) {
// TODO: Handle special names
return OS << I.getIdentifier();
raw_ostream &llvm::operator<<(raw_ostream &OS, DeclBaseName D) {
return OS << D.userFacingName();
}

raw_ostream &llvm::operator<<(raw_ostream &OS, DeclName I) {
Expand Down
3 changes: 1 addition & 2 deletions lib/AST/SourceEntityWalker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,8 @@ bool SemaAnnotator::walkToDeclPre(Decl *D) {
bool IsExtension = false;

if (auto *VD = dyn_cast<ValueDecl>(D)) {
// TODO: Handle special names
if (VD->hasName() && !VD->isImplicit())
NameLen = VD->getBaseName().getIdentifier().getLength();
NameLen = VD->getBaseName().userFacingName().size();

auto ReportParamList = [&](ParameterList *PL) {
for (auto *PD : *PL) {
Expand Down
3 changes: 2 additions & 1 deletion lib/AST/USRGeneration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ bool ide::printDeclUSR(const ValueDecl *D, raw_ostream &OS) {
auto &Importer = *D->getASTContext().getClangModuleLoader();

auto ClangMacroInfo = ClangN.getAsMacro();
bool Ignore = clang::index::generateUSRForMacro(D->getNameStr(),
bool Ignore = clang::index::generateUSRForMacro(
D->getBaseName().getIdentifier().str(),
ClangMacroInfo->getDefinitionLoc(),
Importer.getClangASTContext().getSourceManager(), Buf);
if (!Ignore)
Expand Down
4 changes: 3 additions & 1 deletion lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2026,7 +2026,9 @@ class DarwinBlacklistDeclConsumer : public swift::VisibleDeclConsumer {
return false;

if (clangModule->Name == "MacTypes") {
return llvm::StringSwitch<bool>(VD->getNameStr())
if (!VD->hasName() || VD->getBaseName().isSpecial())
return true;
return llvm::StringSwitch<bool>(VD->getBaseName().getIdentifier().str())
.Cases("OSErr", "OSStatus", "OptionBits", false)
.Cases("FourCharCode", "OSType", false)
.Case("Boolean", false)
Expand Down
3 changes: 1 addition & 2 deletions lib/FrontendTool/ReferenceDependencies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ bool swift::emitReferenceDependencies(DiagnosticEngine &diags,
}

auto escape = [](DeclBaseName name) -> std::string {
// TODO: Handle special names
return llvm::yaml::escape(name.getIdentifier().str());
return llvm::yaml::escape(name.userFacingName());
};

out << "### Swift dependencies file v0 ###\n";
Expand Down
3 changes: 2 additions & 1 deletion lib/IDE/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3727,7 +3727,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
for (auto Ex : NTD->getExtensions()) {
handleDeclRange(Ex->getMembers(), Reason);
}
} else if (isNameHit(VD->getNameStr())) {
} else if (!VD->getBaseName().isSpecial() &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benlangmuir, is this method ever supposed to produce subscripts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It probably should. The situation would be something like
x[label: #^COMPLETE_HERE^#]

isNameHit(VD->getBaseName().getIdentifier().str())) {
if (VD->hasInterfaceType())
unboxType(VD->getInterfaceType());
}
Expand Down
7 changes: 3 additions & 4 deletions lib/IDE/SwiftSourceDocInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ void ResolvedRangeInfo::print(llvm::raw_ostream &OS) {
}

for (auto &VD : DeclaredDecls) {
OS << "<Declared>" << VD.VD->getNameStr() << "</Declared>";
OS << "<Declared>" << VD.VD->getBaseName() << "</Declared>";
OS << "<OutscopeReference>";
if (VD.ReferredAfterRange)
OS << "true";
Expand All @@ -300,7 +300,7 @@ void ResolvedRangeInfo::print(llvm::raw_ostream &OS) {
OS << "</OutscopeReference>\n";
}
for (auto &RD : ReferencedDecls) {
OS << "<Referenced>" << RD.VD->getNameStr() << "</Referenced>";
OS << "<Referenced>" << RD.VD->getBaseName() << "</Referenced>";
OS << "<Type>";
RD.Ty->print(OS);
OS << "</Type>\n";
Expand Down Expand Up @@ -1023,8 +1023,7 @@ void swift::ide::getLocationInfo(const ValueDecl *VD,
NameLen = getCharLength(SM, R);
} else {
if (VD->hasName()) {
// TODO: Handle special names
NameLen = VD->getBaseName().getIdentifier().getLength();
NameLen = VD->getBaseName().userFacingName().size();
} else {
NameLen = getCharLength(SM, VD->getLoc());
}
Expand Down
6 changes: 3 additions & 3 deletions lib/IDE/SyntaxModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1021,9 +1021,9 @@ class IdRefWalker : public ASTWalker {
return { true, E };
if (DRE->getRefKind() != DeclRefKind::Ordinary)
return { true, E };
// TODO: Handle special names
if (!Fn(CharSourceRange(DRE->getSourceRange().Start,
DRE->getName().getBaseIdentifier().getLength())))
if (!Fn(CharSourceRange(
DRE->getSourceRange().Start,
DRE->getName().getBaseName().userFacingName().size())))
return { false, nullptr };
}
return { true, E };
Expand Down
3 changes: 2 additions & 1 deletion lib/IRGen/GenReflection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,8 @@ class FieldTypeMetadataBuilder : public ReflectionMetadataBuilder {
}

if (IGM.IRGen.Opts.EnableReflectionNames) {
auto fieldName = IGM.getAddrOfFieldName(value->getNameStr());
auto name = value->getBaseName().getIdentifier().str();
auto fieldName = IGM.getAddrOfFieldName(name);
B.addRelativeAddress(fieldName);
} else {
B.addInt32(0);
Expand Down
3 changes: 1 addition & 2 deletions lib/PrintAsObjC/PrintAsObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2605,9 +2605,8 @@ class ModuleWriter {
assert(*lhs != *rhs && "duplicate top-level decl");

auto getSortName = [](const Decl *D) -> StringRef {
// TODO: Handle special names
if (auto VD = dyn_cast<ValueDecl>(D))
return VD->getBaseName().getIdentifier().str();
return VD->getBaseName().userFacingName();

if (auto ED = dyn_cast<ExtensionDecl>(D)) {
auto baseClass = ED->getExtendedType()->getClassOrBoundGenericClass();
Expand Down
3 changes: 1 addition & 2 deletions lib/SILOptimizer/Mandatory/DIMemoryUseCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,11 @@ ValueDecl *DIMemoryObjectInfo::
getPathStringToElement(unsigned Element, std::string &Result) const {
auto &Module = MemoryInst->getModule();

// TODO: Handle special names
if (isAnyInitSelf())
Result = "self";
else if (ValueDecl *VD =
dyn_cast_or_null<ValueDecl>(getLoc().getAsASTNode<Decl>()))
Result = VD->getBaseName().getIdentifier().str();
Result = VD->getBaseName().userFacingName();
else
Result = "<unknown>";

Expand Down
8 changes: 4 additions & 4 deletions lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1528,7 +1528,7 @@ void CalleeCandidateInfo::collectCalleeCandidates(Expr *fn,
if (auto declRefExpr = dyn_cast<DeclRefExpr>(fn)) {
auto decl = declRefExpr->getDecl();
candidates.push_back({ decl, getCalleeLevel(decl) });
declName = decl->getNameStr().str();
declName = decl->getBaseName().userFacingName();
return;
}

Expand All @@ -1549,7 +1549,7 @@ void CalleeCandidateInfo::collectCalleeCandidates(Expr *fn,
}

if (!candidates.empty())
declName = candidates[0].getDecl()->getNameStr().str();
declName = candidates[0].getDecl()->getBaseName().userFacingName();
return;
}

Expand Down Expand Up @@ -1682,7 +1682,7 @@ void CalleeCandidateInfo::collectCalleeCandidates(Expr *fn,
if (candidates.empty()) continue;

if (declName.empty())
declName = candidates[0].getDecl()->getNameStr().str();
declName = candidates[0].getDecl()->getBaseName().userFacingName();
return;
}

Expand Down Expand Up @@ -1813,7 +1813,7 @@ CalleeCandidateInfo::CalleeCandidateInfo(Type baseType,
}

if (!candidates.empty())
declName = candidates[0].getDecl()->getNameStr().str();
declName = candidates[0].getDecl()->getBaseName().userFacingName();
}


Expand Down
6 changes: 3 additions & 3 deletions lib/Sema/MiscDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2639,7 +2639,7 @@ static Expr *endConditionValueForConvertingCStyleForLoop(const ForStmt *FS,
return nullptr;

// Verify that the condition is a simple != or < comparison to the loop variable.
auto comparisonOpName = binaryFuncExpr->getDecl()->getNameStr();
auto comparisonOpName = binaryFuncExpr->getDecl()->getBaseName();
if (comparisonOpName == "!=")
OpKind = OperatorKind::NotEqual;
else if (comparisonOpName == "<")
Expand Down Expand Up @@ -2681,7 +2681,7 @@ static bool unaryOperatorCheckForConvertingCStyleForLoop(const ForStmt *FS,
auto unaryFuncExpr = dyn_cast<DeclRefExpr>(unaryExpr->getFn());
if (!unaryFuncExpr)
return false;
if (unaryFuncExpr->getDecl()->getNameStr() != OpName)
if (unaryFuncExpr->getDecl()->getBaseName() != OpName)
return false;
return incrementDeclRefExpr->getDecl() == loopVar;
}
Expand Down Expand Up @@ -2710,7 +2710,7 @@ static bool binaryOperatorCheckForConvertingCStyleForLoop(TypeChecker &TC,
auto binaryFuncExpr = dyn_cast<DeclRefExpr>(binaryExpr->getFn());
if (!binaryFuncExpr)
return false;
if (binaryFuncExpr->getDecl()->getNameStr() != OpName)
if (binaryFuncExpr->getDecl()->getBaseName() != OpName)
return false;
auto argTupleExpr = dyn_cast<TupleExpr>(binaryExpr->getArg());
if (!argTupleExpr)
Expand Down
18 changes: 11 additions & 7 deletions lib/Sema/TypeCheckAvailability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2442,8 +2442,8 @@ bool AvailabilityWalker::diagnoseIncDecRemoval(const ValueDecl *D,
SourceRange R,
const AvailableAttr *Attr) {
// We can only produce a fixit if we're talking about ++ or --.
bool isInc = D->getNameStr() == "++";
if (!isInc && D->getNameStr() != "--")
bool isInc = D->getBaseName() == "++";
if (!isInc && D->getBaseName() != "--")
return false;

// We can only handle the simple cases of lvalue++ and ++lvalue. This is
Expand Down Expand Up @@ -2503,11 +2503,15 @@ bool AvailabilityWalker::diagnoseMemoryLayoutMigration(const ValueDecl *D,
if (!D->getModuleContext()->isStdlibModule())
return false;

StringRef Property = llvm::StringSwitch<StringRef>(D->getNameStr())
.Case("sizeof", "size")
.Case("alignof", "alignment")
.Case("strideof", "stride")
.Default(StringRef());
StringRef Property;
if (D->getBaseName() == "sizeof") {
Property = "size";
} else if (D->getBaseName() == "alignof") {
Property = "alignment";
} else if (D->getBaseName() == "strideof") {
Property = "stride";
}

if (Property.empty())
return false;

Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckCaptures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ class FindCapturedVars : public ASTWalker {
isNested = f->getDeclContext()->isLocalContext();

if (isInOut && !AFR.isKnownNoEscape() && !isNested) {
if (D->getNameStr() == "self") {
if (D->getBaseName() == D->getASTContext().Id_self) {
TC.diagnose(DRE->getLoc(),
diag::closure_implicit_capture_mutating_self);
} else {
Expand Down
5 changes: 3 additions & 2 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3899,9 +3899,10 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
VD->getNameLoc().isValid() &&
Context.SourceMgr.extractText({VD->getNameLoc(), 1}) != "`") {
TC.diagnose(VD->getNameLoc(), diag::reserved_member_name,
VD->getFullName(), VD->getNameStr());
VD->getFullName(), VD->getBaseName().getIdentifier().str());
TC.diagnose(VD->getNameLoc(), diag::backticks_to_escape)
.fixItReplace(VD->getNameLoc(), "`"+VD->getNameStr().str()+"`");
.fixItReplace(VD->getNameLoc(),
"`" + VD->getBaseName().userFacingName().str() + "`");
}
}

Expand Down
3 changes: 2 additions & 1 deletion lib/Sema/TypeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,8 @@ void TypeChecker::checkForForbiddenPrefix(const Decl *D) {
if (!hasEnabledForbiddenTypecheckPrefix())
return;
if (auto VD = dyn_cast<ValueDecl>(D)) {
checkForForbiddenPrefix(VD->getNameStr());
if (!VD->getBaseName().isSpecial())
checkForForbiddenPrefix(VD->getBaseName().getIdentifier().str());
}
}

Expand Down
3 changes: 2 additions & 1 deletion tools/swift-api-digester/swift-api-digester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1399,7 +1399,8 @@ class SwiftDeclCollector : public VisibleDeclConsumer {
llvm::array_pod_sort(ClangMacros.begin(), ClangMacros.end(),
[](ValueDecl * const *lhs,
ValueDecl * const *rhs) -> int {
return (*lhs)->getNameStr().compare((*rhs)->getNameStr());
return (*lhs)->getBaseName().userFacingName().compare(
(*rhs)->getBaseName().userFacingName());
});

for (auto *VD : ClangMacros)
Expand Down