Skip to content

[5.5] ABI Checker: include mangled names in ABI descriptor files #39860

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
Oct 22, 2021
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
3 changes: 3 additions & 0 deletions include/swift/APIDigester/ModuleAnalyzerNodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ struct PlatformIntroVersion {
class SDKNodeDecl: public SDKNode {
DeclKind DKind;
StringRef Usr;
StringRef MangledName;
SourceLoc Loc;
StringRef Location;
StringRef ModuleName;
Expand Down Expand Up @@ -463,6 +464,7 @@ class SDKNodeType: public SDKNode {

class SDKNodeTypeNominal : public SDKNodeType {
StringRef USR;
StringRef MangledName;
public:
SDKNodeTypeNominal(SDKNodeInitInfo Info);
// Get the usr of the correspoding nominal type decl.
Expand Down Expand Up @@ -583,6 +585,7 @@ class SDKNodeDeclType: public SDKNodeDecl {
/// in the conformance, thus getName() will give us the name of the protocol.
class SDKNodeConformance: public SDKNode {
StringRef Usr;
StringRef MangledName;
SDKNodeDeclType *TypeDecl;
friend class SDKNodeDeclType;
bool IsABIPlaceholder;
Expand Down
1 change: 1 addition & 0 deletions include/swift/AST/ASTMangler.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ class ASTMangler : public Mangler {

std::string mangleTypeAsContextUSR(const NominalTypeDecl *type);

std::string mangleAnyDecl(const ValueDecl *Decl, bool prefix);
std::string mangleDeclAsUSR(const ValueDecl *Decl, StringRef USRPrefix);

std::string mangleAccessorEntityAsUSR(AccessorKind kind,
Expand Down
1 change: 1 addition & 0 deletions include/swift/IDE/DigesterEnums.def
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ KEY(kind)
KEY_STRING(Name, name)
KEY_STRING(PrintedName, printedName)
KEY_STRING(Usr, usr)
KEY_STRING(MangledName, mangledName)
KEY_STRING(Location, location)
KEY_STRING(ModuleName, moduleName)
KEY_STRING(SuperclassUsr, superclassUsr)
Expand Down
26 changes: 23 additions & 3 deletions lib/APIDigester/ModuleAnalyzerNodes.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "llvm/ADT/STLExtras.h"
#include "swift/Basic/Defer.h"
#include "swift/SIL/SILDeclRef.h"
#include "swift/AST/ASTMangler.h"
#include <swift/APIDigester/ModuleAnalyzerNodes.h>
#include <algorithm>

Expand Down Expand Up @@ -98,7 +99,8 @@ SDKNodeRoot::SDKNodeRoot(SDKNodeInitInfo Info): SDKNode(Info, SDKNodeKind::Root)
JsonFormatVer(Info.JsonFormatVer.hasValue() ? *Info.JsonFormatVer : DIGESTER_JSON_DEFAULT_VERSION) {}

SDKNodeDecl::SDKNodeDecl(SDKNodeInitInfo Info, SDKNodeKind Kind)
: SDKNode(Info, Kind), DKind(Info.DKind), Usr(Info.Usr), Loc(Info.Loc),
: SDKNode(Info, Kind), DKind(Info.DKind), Usr(Info.Usr),
MangledName(Info.MangledName), Loc(Info.Loc),
Location(Info.Location), ModuleName(Info.ModuleName),
DeclAttributes(Info.DeclAttrs), IsImplicit(Info.IsImplicit),
IsStatic(Info.IsStatic), IsDeprecated(Info.IsDeprecated),
Expand All @@ -120,7 +122,8 @@ SDKNodeType::SDKNodeType(SDKNodeInitInfo Info, SDKNodeKind Kind):
ParamValueOwnership(Info.ParamValueOwnership) {}

SDKNodeTypeNominal::SDKNodeTypeNominal(SDKNodeInitInfo Info):
SDKNodeType(Info, SDKNodeKind::TypeNominal), USR(Info.Usr) {}
SDKNodeType(Info, SDKNodeKind::TypeNominal), USR(Info.Usr),
MangledName(Info.MangledName) {}

SDKNodeTypeFunc::SDKNodeTypeFunc(SDKNodeInitInfo Info):
SDKNodeType(Info, SDKNodeKind::TypeFunc) {}
Expand All @@ -139,7 +142,8 @@ SDKNodeDeclType::SDKNodeDeclType(SDKNodeInitInfo Info):

SDKNodeConformance::SDKNodeConformance(SDKNodeInitInfo Info):
SDKNode(Info, SDKNodeKind::Conformance),
Usr(Info.Usr), IsABIPlaceholder(Info.IsABIPlaceholder) {}
Usr(Info.Usr), MangledName(Info.MangledName),
IsABIPlaceholder(Info.IsABIPlaceholder) {}

SDKNodeTypeWitness::SDKNodeTypeWitness(SDKNodeInitInfo Info):
SDKNode(Info, SDKNodeKind::TypeWitness) {}
Expand Down Expand Up @@ -1036,6 +1040,18 @@ static StringRef calculateUsr(SDKContext &Ctx, ValueDecl *VD) {
return StringRef();
}

static StringRef calculateMangledName(SDKContext &Ctx, ValueDecl *VD) {
if (isFromClang(VD)) {
// Don't mangle clang symbols.
return StringRef();
}
if (auto *attr = VD->getAttrs().getAttribute<SILGenNameAttr>()) {
return Ctx.buffer(attr->Name);
}
Mangle::ASTMangler NewMangler;
return Ctx.buffer(NewMangler.mangleAnyDecl(VD, true));
}

static StringRef calculateLocation(SDKContext &SDKCtx, Decl *D) {
if (SDKCtx.getOpts().AvoidLocation)
return StringRef();
Expand Down Expand Up @@ -1298,6 +1314,7 @@ SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, Type Ty, TypeInitInfo Info) :
// If this is a nominal type, get its Usr.
if (auto *ND = Ty->getAnyNominal()) {
Usr = calculateUsr(Ctx, ND);
MangledName = calculateMangledName(Ctx, ND);
}
}

Expand Down Expand Up @@ -1415,6 +1432,7 @@ SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, ValueDecl *VD)
Name = getSimpleName(VD);
PrintedName = getPrintedName(Ctx, VD);
Usr = calculateUsr(Ctx, VD);
MangledName = calculateMangledName(Ctx, VD);
IsThrowing = isFuncThrowing(VD);
IsStatic = VD->isStatic();
IsOverriding = VD->getOverriddenDecl();
Expand Down Expand Up @@ -1970,13 +1988,15 @@ void SDKNodeRoot::jsonize(json::Output &out) {
void SDKNodeConformance::jsonize(json::Output &out) {
SDKNode::jsonize(out);
output(out, KeyKind::KK_usr, Usr);
output(out, KeyKind::KK_mangledName, MangledName);
output(out, KeyKind::KK_isABIPlaceholder, IsABIPlaceholder);
}

void SDKNodeDecl::jsonize(json::Output &out) {
SDKNode::jsonize(out);
out.mapRequired(getKeyContent(Ctx, KeyKind::KK_declKind).data(), DKind);
output(out, KeyKind::KK_usr, Usr);
output(out, KeyKind::KK_mangledName, MangledName);
output(out, KeyKind::KK_location, Location);
output(out, KeyKind::KK_moduleName, ModuleName);
output(out, KeyKind::KK_genericSig, GenericSig);
Expand Down
24 changes: 15 additions & 9 deletions lib/AST/ASTMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -742,16 +742,14 @@ std::string ASTMangler::mangleTypeAsUSR(Type Ty) {
return finalize();
}

std::string ASTMangler::mangleDeclAsUSR(const ValueDecl *Decl,
StringRef USRPrefix) {
#if SWIFT_BUILD_ONLY_SYNTAXPARSERLIB
return std::string(); // not needed for the parser library.
#endif

std::string ASTMangler::mangleAnyDecl(const ValueDecl *Decl, bool prefix) {
DWARFMangling = true;
beginManglingWithoutPrefix();
if (prefix) {
beginMangling();
} else {
beginManglingWithoutPrefix();
}
llvm::SaveAndRestore<bool> allowUnnamedRAII(AllowNamelessEntities, true);
Buffer << USRPrefix;

auto Sig = Decl->getInnermostDeclContext()->getGenericSignatureOfContext();
bindGenericParameters(Sig);
Expand All @@ -773,10 +771,18 @@ std::string ASTMangler::mangleDeclAsUSR(const ValueDecl *Decl,
// We have a custom prefix, so finalize() won't verify for us. If we're not
// in invalid code (coming from an IDE caller) verify manually.
if (!Decl->isInvalid())
verify(Storage.str().drop_front(USRPrefix.size()));
verify(Storage.str());
return finalize();
}

std::string ASTMangler::mangleDeclAsUSR(const ValueDecl *Decl,
StringRef USRPrefix) {
#if SWIFT_BUILD_ONLY_SYNTAXPARSERLIB
return std::string(); // not needed for the parser library.
#endif
return (llvm::Twine(USRPrefix) + mangleAnyDecl(Decl, false)).str();
}

std::string ASTMangler::mangleAccessorEntityAsUSR(AccessorKind kind,
const AbstractStorageDecl *decl,
StringRef USRPrefix,
Expand Down
3 changes: 3 additions & 0 deletions test/api-digester/Inputs/cake.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,6 @@ extension SwiftObjcClass {

@_alwaysEmitIntoClient
public func emitIntoClientFunc() {}

@_silgen_name("silgenName")
public func silgenNamedFunc() {}
Loading