Skip to content

swift-api-digester: teach the tool to serialize raw type name for enum declarations. #14947

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
Mar 3, 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
1 change: 1 addition & 0 deletions include/swift/IDE/DigesterEnums.def
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ KEY(superclassUsr)
KEY(parentExtensionReqs)
KEY(hasDefaultArg)
KEY(conformingProtocols)
KEY(enumRawTypeName)

KNOWN_TYPE(Optional)
KNOWN_TYPE(ImplicitlyUnwrappedOptional)
Expand Down
1 change: 1 addition & 0 deletions test/api-digester/Outputs/cake.json
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@
"Hashable",
"RawRepresentable"
],
"enumRawTypeName": "Int",
"children": [
{
"kind": "Var",
Expand Down
38 changes: 37 additions & 1 deletion tools/swift-api-digester/swift-api-digester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ struct SDKNodeInitInfo {
std::vector<TypeAttrKind> TypeAttrs;
std::vector<StringRef> ConformingProtocols;
StringRef SuperclassUsr;
StringRef EnumRawTypeName;
ParentExtensionInfo *ExtInfo = nullptr;
TypeInitInfo TypeInfo;

Expand Down Expand Up @@ -779,13 +780,26 @@ SDKNodeDecl *SDKNodeType::getClosestParentDecl() const {
class SDKNodeTypeDecl : public SDKNodeDecl {
StringRef SuperclassUsr;
std::vector<StringRef> ConformingProtocols;
StringRef EnumRawTypeName;
public:
SDKNodeTypeDecl(SDKNodeInitInfo Info) : SDKNodeDecl(Info, SDKNodeKind::TypeDecl),
SuperclassUsr(Info.SuperclassUsr),
ConformingProtocols(Info.ConformingProtocols){}
ConformingProtocols(Info.ConformingProtocols),
EnumRawTypeName(Info.EnumRawTypeName) {}
static bool classof(const SDKNode *N);
StringRef getSuperClassUsr() const { return SuperclassUsr; }
ArrayRef<StringRef> getAllProtocols() const { return ConformingProtocols; }

#define NOMINAL_TYPE_DECL(ID, PARENT) \
bool is##ID() const { return getDeclKind() == DeclKind::ID; }
#define DECL(ID, PARENT)
#include "swift/AST/DeclNodes.def"

StringRef getEnumRawTypeName() const {
assert(isEnum());
return EnumRawTypeName;
}

Optional<SDKNodeTypeDecl*> getSuperclass() const {
if (SuperclassUsr.empty())
return None;
Expand Down Expand Up @@ -959,6 +973,11 @@ SDKNode* SDKNode::constructSDKNode(SDKContext &Ctx,
}
break;
}
case KeyKind::KK_enumRawTypeName: {
assert(Info.DKind == DeclKind::Enum);
Info.EnumRawTypeName = GetScalarString(Pair.getValue());
break;
}
case KeyKind::KK_printedName:
Info.PrintedName = GetScalarString(Pair.getValue());
break;
Expand Down Expand Up @@ -1294,6 +1313,15 @@ SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, ValueDecl *VD)
ConformingProtocols.push_back(P->getName().str());
}
}

// Get enum raw type name if this is an enum.
if (auto *ED = dyn_cast<EnumDecl>(VD)) {
if (auto RT = ED->getRawType()) {
if (auto *D = RT->getNominalOrBoundGenericNominal()) {
EnumRawTypeName = D->getName().str();
}
}
}
}

SDKNode *SDKNodeInitInfo::createSDKNode(SDKNodeKind Kind) {
Expand Down Expand Up @@ -1675,6 +1703,14 @@ namespace swift {
KeyKind::KK_conformingProtocols).data(),
Pros);
}

auto RawTypeName = TD->isEnum() ? TD->getEnumRawTypeName() : StringRef();
if (!RawTypeName.empty()) {
out.mapRequired(getKeyContent(Ctx,
KeyKind::KK_enumRawTypeName).data(),
RawTypeName);
}

}
if (D->isFromExtension()) {
// Even if we don't have any requirements on this parent extension,
Expand Down