mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
swift-api-digester: include generic signature printing in the module dump. (#18686)
This commit is contained in:
@@ -45,6 +45,7 @@
|
||||
#include "swift/AST/NameLookup.h"
|
||||
#include "swift/AST/PrettyStackTrace.h"
|
||||
#include "swift/AST/USRGeneration.h"
|
||||
#include "swift/AST/GenericSignature.h"
|
||||
#include "swift/Basic/ColorUtils.h"
|
||||
#include "swift/Basic/JSONSerialization.h"
|
||||
#include "swift/Basic/LLVMInitialize.h"
|
||||
@@ -367,8 +368,8 @@ struct SDKNodeInitInfo {
|
||||
std::vector<StringRef> ConformingProtocols;
|
||||
StringRef SuperclassUsr;
|
||||
StringRef EnumRawTypeName;
|
||||
ParentExtensionInfo *ExtInfo = nullptr;
|
||||
TypeInitInfo TypeInfo;
|
||||
StringRef GenericSig;
|
||||
|
||||
SDKNodeInitInfo(SDKContext &Ctx) : Ctx(Ctx) {}
|
||||
SDKNodeInitInfo(SDKContext &Ctx, ValueDecl *VD);
|
||||
@@ -440,8 +441,7 @@ class SDKNodeDecl : public SDKNode {
|
||||
bool IsDeprecated;
|
||||
uint8_t ReferenceOwnership;
|
||||
bool hasDeclAttribute(DeclAttrKind DAKind) const;
|
||||
// Non-null ExtInfo implies this decl is defined in an type extension.
|
||||
ParentExtensionInfo *ExtInfo;
|
||||
StringRef GenericSig;
|
||||
|
||||
protected:
|
||||
SDKNodeDecl(SDKNodeInitInfo Info, SDKNodeKind Kind)
|
||||
@@ -450,7 +450,7 @@ protected:
|
||||
DeclAttributes(Info.DeclAttrs), IsStatic(Info.IsStatic),
|
||||
IsDeprecated(Info.IsDeprecated),
|
||||
ReferenceOwnership(uint8_t(Info.ReferenceOwnership)),
|
||||
ExtInfo(Info.ExtInfo) {}
|
||||
GenericSig(Info.GenericSig) {}
|
||||
|
||||
public:
|
||||
StringRef getUsr() const { return Usr; }
|
||||
@@ -470,11 +470,7 @@ public:
|
||||
bool isDeprecated() const { return IsDeprecated; };
|
||||
bool hasFixedLayout() const;
|
||||
bool isStatic() const { return IsStatic; };
|
||||
bool isFromExtension() const { return ExtInfo; }
|
||||
const ParentExtensionInfo& getExtensionInfo() const {
|
||||
assert(isFromExtension());
|
||||
return *ExtInfo;
|
||||
}
|
||||
StringRef getGenericSignature() const { return GenericSig; }
|
||||
};
|
||||
|
||||
StringRef SDKNodeDecl::getHeaderName() const {
|
||||
@@ -1026,14 +1022,6 @@ SDKNode* SDKNode::constructSDKNode(SDKContext &Ctx,
|
||||
cast<llvm::yaml::MappingNode>(&Mapping)));
|
||||
}
|
||||
break;
|
||||
case KeyKind::KK_parentExtensionReqs: {
|
||||
assert(!Info.ExtInfo);
|
||||
Info.ExtInfo = new (Ctx) ParentExtensionInfo();
|
||||
for (auto &Req : *cast<llvm::yaml::SequenceNode>(Pair.getValue())) {
|
||||
Info.ExtInfo->Requirements.push_back(GetScalarString(&Req));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case KeyKind::KK_conformingProtocols: {
|
||||
assert(Info.ConformingProtocols.empty());
|
||||
for (auto &Name : *cast<llvm::yaml::SequenceNode>(Pair.getValue())) {
|
||||
@@ -1055,6 +1043,9 @@ SDKNode* SDKNode::constructSDKNode(SDKContext &Ctx,
|
||||
case KeyKind::KK_superclassUsr:
|
||||
Info.SuperclassUsr = GetScalarString(Pair.getValue());
|
||||
break;
|
||||
case KeyKind::KK_genericSig:
|
||||
Info.GenericSig = GetScalarString(Pair.getValue());
|
||||
break;
|
||||
case KeyKind::KK_throwing:
|
||||
Info.IsThrowing = true;
|
||||
break;
|
||||
@@ -1350,6 +1341,35 @@ static ReferenceOwnership getReferenceOwnership(ValueDecl *VD) {
|
||||
return ReferenceOwnership::Strong;
|
||||
}
|
||||
|
||||
static StringRef printGenericSignature(SDKContext &Ctx, ValueDecl *VD) {
|
||||
llvm::SmallString<32> Result;
|
||||
llvm::raw_svector_ostream OS(Result);
|
||||
if (auto *PD = dyn_cast<ProtocolDecl>(VD)) {
|
||||
if (PD->getRequirementSignature().empty())
|
||||
return StringRef();
|
||||
OS << "<";
|
||||
bool First = true;
|
||||
for (auto Req: PD->getRequirementSignature()) {
|
||||
if (!First) {
|
||||
OS << ", ";
|
||||
} else {
|
||||
First = false;
|
||||
}
|
||||
Req.print(OS, PrintOptions::printInterface());
|
||||
}
|
||||
OS << ">";
|
||||
return Ctx.buffer(OS.str());
|
||||
}
|
||||
|
||||
if (auto *GC = VD->getAsGenericContext()) {
|
||||
if (auto *Sig = GC->getGenericSignature()) {
|
||||
Sig->print(OS);
|
||||
return Ctx.buffer(OS.str());
|
||||
}
|
||||
}
|
||||
return StringRef();
|
||||
}
|
||||
|
||||
SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, Type Ty,
|
||||
TypeInitInfo TypeInfo) :
|
||||
Ctx(Ctx), Name(getTypeName(Ctx, Ty, TypeInfo.IsImplicitlyUnwrappedOptional)),
|
||||
@@ -1372,8 +1392,8 @@ SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, ValueDecl *VD)
|
||||
IsThrowing(isFuncThrowing(VD)), IsMutating(isFuncMutating(VD)),
|
||||
IsStatic(VD->isStatic()),
|
||||
IsDeprecated(VD->getAttrs().getDeprecated(VD->getASTContext())),
|
||||
SelfIndex(getSelfIndex(VD)),
|
||||
ReferenceOwnership(getReferenceOwnership(VD)), ExtInfo(nullptr) {
|
||||
SelfIndex(getSelfIndex(VD)), ReferenceOwnership(getReferenceOwnership(VD)),
|
||||
GenericSig(printGenericSignature(Ctx, VD)) {
|
||||
|
||||
// Calculate usr for its super class.
|
||||
if (auto *CD = dyn_cast_or_null<ClassDecl>(VD)) {
|
||||
@@ -1386,18 +1406,6 @@ SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, ValueDecl *VD)
|
||||
std::transform(AllAttrs.begin(), AllAttrs.end(), std::back_inserter(DeclAttrs),
|
||||
[](DeclAttribute *attr) { return attr->getKind(); });
|
||||
|
||||
// If the decl is declared in an extension, calculate the extension info.
|
||||
if (auto *Ext = dyn_cast_or_null<ExtensionDecl>(VD->getDeclContext())) {
|
||||
ExtInfo = new (Ctx) ParentExtensionInfo();
|
||||
// Print each generic requirement to the extension info.
|
||||
for (auto Req: Ext->getGenericRequirements()) {
|
||||
llvm::SmallString<32> Result;
|
||||
llvm::raw_svector_ostream OS(Result);
|
||||
Req.print(OS, PrintOptions::printInterface());
|
||||
ExtInfo->Requirements.emplace_back(Ctx.buffer(OS.str()));
|
||||
}
|
||||
}
|
||||
|
||||
// Get all protocol names this type decl conforms to.
|
||||
if (auto *NTD = dyn_cast<NominalTypeDecl>(VD)) {
|
||||
for (auto *P: NTD->getAllProtocols()) {
|
||||
@@ -1798,6 +1806,9 @@ namespace swift {
|
||||
out.mapRequired(getKeyContent(Ctx, KeyKind::KK_location).data(), Location);
|
||||
out.mapRequired(getKeyContent(Ctx, KeyKind::KK_moduleName).data(),
|
||||
ModuleName);
|
||||
auto GSig = D->getGenericSignature();
|
||||
if (!GSig.empty())
|
||||
out.mapRequired(getKeyContent(Ctx, KeyKind::KK_genericSig), GSig);
|
||||
|
||||
if (auto isStatic = D->isStatic())
|
||||
out.mapRequired(getKeyContent(Ctx, KeyKind::KK_static).data(), isStatic);
|
||||
@@ -1837,15 +1848,6 @@ namespace swift {
|
||||
}
|
||||
|
||||
}
|
||||
if (D->isFromExtension()) {
|
||||
// Even if we don't have any requirements on this parent extension,
|
||||
// we still want to have this key present to indicate the member
|
||||
// is from an extension.
|
||||
auto Reqs = D->getExtensionInfo().getGenericRequirements();
|
||||
out.mapRequired(getKeyContent(Ctx,
|
||||
KeyKind::KK_parentExtensionReqs).data(),
|
||||
Reqs);
|
||||
}
|
||||
auto Attributes = D->getDeclAttributes();
|
||||
if (!Attributes.empty())
|
||||
out.mapRequired(getKeyContent(Ctx, KeyKind::KK_declAttributes).data(),
|
||||
|
||||
Reference in New Issue
Block a user