Skip to content
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 @@ -91,6 +91,7 @@ KEY(ownership)
KEY(superclassUsr)
KEY(parentExtensionReqs)
KEY(hasDefaultArg)
KEY(conformingProtocols)

KNOWN_TYPE(Optional)
KNOWN_TYPE(ImplicitlyUnwrappedOptional)
Expand Down
12 changes: 10 additions & 2 deletions test/api-digester/Inputs/cake.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
public struct S1 {
public protocol P1 {}
public protocol P2 {}
public struct S1: P1 {
public static func foo1() {}
mutating public func foo2() {}
internal func foo3() {}
Expand All @@ -7,6 +9,8 @@ public struct S1 {
public func foo6() -> Void {}
}

extension S1: P2 {}

public class C0<T1, T2, T3> {}

public class C1: C0<S1, S1, S1> {
Expand All @@ -24,4 +28,8 @@ public extension C0 {
}

public func foo1(_ a: Int = 1, b: S1) {}
public func foo2(_ a: Int = #line, b: S1) {}
public func foo2(_ a: Int = #line, b: S1) {}

public enum Number: Int {
case one
}
181 changes: 181 additions & 0 deletions test/api-digester/Outputs/cake.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@
"name": "TopLevel",
"printedName": "TopLevel",
"children": [
{
"kind": "TypeDecl",
"name": "P1",
"printedName": "P1",
"declKind": "Protocol",
"usr": "s:4cake2P1P",
"location": "",
"moduleName": "cake"
},
{
"kind": "TypeDecl",
"name": "P2",
"printedName": "P2",
"declKind": "Protocol",
"usr": "s:4cake2P2P",
"location": "",
"moduleName": "cake"
},
{
"kind": "TypeDecl",
"name": "C0",
Expand Down Expand Up @@ -93,6 +111,10 @@
"usr": "s:4cake2S1V",
"location": "",
"moduleName": "cake",
"conformingProtocols": [
"P1",
"P2"
],
"children": [
{
"kind": "Function",
Expand Down Expand Up @@ -389,6 +411,165 @@
"printedName": "S1"
}
]
},
{
"kind": "TypeDecl",
"name": "Number",
"printedName": "Number",
"declKind": "Enum",
"usr": "s:4cake6NumberO",
"location": "",
"moduleName": "cake",
"conformingProtocols": [
"Equatable",
"Hashable",
"RawRepresentable"
],
"children": [
{
"kind": "Var",
"name": "one",
"printedName": "one",
"declKind": "EnumElement",
"usr": "s:4cake6NumberO3oneyA2CmF",
"location": "",
"moduleName": "cake",
"children": [
{
"kind": "TypeFunc",
"name": "Function",
"printedName": "(Number.Type) -> Number",
"children": [
{
"kind": "TypeNominal",
"name": "Number",
"printedName": "Number"
},
{
"kind": "TypeNominal",
"name": "Metatype",
"printedName": "Number.Type",
"children": [
{
"kind": "TypeNominal",
"name": "Number",
"printedName": "Number"
}
]
}
]
}
]
},
{
"kind": "TypeAlias",
"name": "RawValue",
"printedName": "RawValue",
"declKind": "TypeAlias",
"usr": "s:4cake6NumberO8RawValuea",
"location": "",
"moduleName": "cake",
"children": [
{
"kind": "TypeNominal",
"name": "Int",
"printedName": "Int"
}
]
},
{
"kind": "Var",
"name": "hashValue",
"printedName": "hashValue",
"declKind": "Var",
"usr": "s:4cake6NumberO9hashValueSivp",
"location": "",
"moduleName": "cake",
"children": [
{
"kind": "TypeNominal",
"name": "Int",
"printedName": "Int"
},
{
"kind": "Getter",
"name": "_",
"printedName": "_()",
"declKind": "Accessor",
"usr": "s:4cake6NumberO9hashValueSivg",
"location": "",
"moduleName": "cake",
"children": [
{
"kind": "TypeNominal",
"name": "Int",
"printedName": "Int"
}
]
}
]
},
{
"kind": "Constructor",
"name": "init",
"printedName": "init(rawValue:)",
"declKind": "Constructor",
"usr": "s:4cake6NumberO8rawValueACSgSi_tcfc",
"location": "",
"moduleName": "cake",
"children": [
{
"kind": "TypeNominal",
"name": "Optional",
"printedName": "Number?",
"children": [
{
"kind": "TypeNominal",
"name": "Number",
"printedName": "Number"
}
]
},
{
"kind": "TypeNominal",
"name": "Int",
"printedName": "Int"
}
]
},
{
"kind": "Var",
"name": "rawValue",
"printedName": "rawValue",
"declKind": "Var",
"usr": "s:4cake6NumberO8rawValueSivp",
"location": "",
"moduleName": "cake",
"children": [
{
"kind": "TypeNominal",
"name": "Int",
"printedName": "Int"
},
{
"kind": "Getter",
"name": "_",
"printedName": "_()",
"declKind": "Accessor",
"usr": "s:4cake6NumberO8rawValueSivg",
"location": "",
"moduleName": "cake",
"children": [
{
"kind": "TypeNominal",
"name": "Int",
"printedName": "Int"
}
]
}
]
}
]
}
]
}
27 changes: 25 additions & 2 deletions tools/swift-api-digester/swift-api-digester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ struct SDKNodeInitInfo {
Ownership Ownership = Ownership::Strong;
std::vector<SDKDeclAttrKind> DeclAttrs;
std::vector<TypeAttrKind> TypeAttrs;
std::vector<StringRef> ConformingProtocols;
StringRef SuperclassUsr;
ParentExtensionInfo *ExtInfo = nullptr;
TypeInitInfo TypeInfo;
Expand Down Expand Up @@ -774,12 +775,14 @@ SDKNodeDecl *SDKNodeType::getClosestParentDecl() const {

class SDKNodeTypeDecl : public SDKNodeDecl {
StringRef SuperclassUsr;
std::vector<StringRef> ConformingProtocols;
public:
SDKNodeTypeDecl(SDKNodeInitInfo Info) : SDKNodeDecl(Info, SDKNodeKind::TypeDecl),
SuperclassUsr(Info.SuperclassUsr) {}
SuperclassUsr(Info.SuperclassUsr),
ConformingProtocols(Info.ConformingProtocols){}
static bool classof(const SDKNode *N);
StringRef getSuperClassUsr() const { return SuperclassUsr; }

ArrayRef<StringRef> getAllProtocols() const { return ConformingProtocols; }
Optional<SDKNodeTypeDecl*> getSuperclass() const {
if (SuperclassUsr.empty())
return None;
Expand Down Expand Up @@ -946,6 +949,13 @@ SDKNode* SDKNode::constructSDKNode(SDKContext &Ctx,
}
break;
}
case KeyKind::KK_conformingProtocols: {
assert(Info.ConformingProtocols.empty());
for (auto &Name : *cast<llvm::yaml::SequenceNode>(Pair.getValue())) {
Info.ConformingProtocols.push_back(GetScalarString(&Name));
}
break;
}
case KeyKind::KK_printedName:
Info.PrintedName = GetScalarString(Pair.getValue());
break;
Expand Down Expand Up @@ -1272,6 +1282,13 @@ SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, ValueDecl *VD) : Ctx(Ctx),
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()) {
ConformingProtocols.push_back(P->getName().str());
}
}
}

SDKNode *SDKNodeInitInfo::createSDKNode(SDKNodeKind Kind) {
Expand Down Expand Up @@ -1647,6 +1664,12 @@ namespace swift {
out.mapRequired(getKeyContent(Ctx, KeyKind::KK_superclassUsr).data(),
Super);
}
auto Pros = TD->getAllProtocols();
if (!Pros.empty()) {
out.mapRequired(getKeyContent(Ctx,
KeyKind::KK_conformingProtocols).data(),
Pros);
}
}
if (D->isFromExtension()) {
// Even if we don't have any requirements on this parent extension,
Expand Down