Skip to content

Commit 66bb9ce

Browse files
authored
swift-api-digester: teach the tool to serialize conforming protocols. (swiftlang#14923)
1 parent 23fc721 commit 66bb9ce

File tree

4 files changed

+217
-4
lines changed

4 files changed

+217
-4
lines changed

include/swift/IDE/DigesterEnums.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ KEY(ownership)
9191
KEY(superclassUsr)
9292
KEY(parentExtensionReqs)
9393
KEY(hasDefaultArg)
94+
KEY(conformingProtocols)
9495

9596
KNOWN_TYPE(Optional)
9697
KNOWN_TYPE(ImplicitlyUnwrappedOptional)

test/api-digester/Inputs/cake.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
public struct S1 {
1+
public protocol P1 {}
2+
public protocol P2 {}
3+
public struct S1: P1 {
24
public static func foo1() {}
35
mutating public func foo2() {}
46
internal func foo3() {}
@@ -7,6 +9,8 @@ public struct S1 {
79
public func foo6() -> Void {}
810
}
911

12+
extension S1: P2 {}
13+
1014
public class C0<T1, T2, T3> {}
1115

1216
public class C1: C0<S1, S1, S1> {
@@ -24,4 +28,8 @@ public extension C0 {
2428
}
2529

2630
public func foo1(_ a: Int = 1, b: S1) {}
27-
public func foo2(_ a: Int = #line, b: S1) {}
31+
public func foo2(_ a: Int = #line, b: S1) {}
32+
33+
public enum Number: Int {
34+
case one
35+
}

test/api-digester/Outputs/cake.json

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,24 @@
33
"name": "TopLevel",
44
"printedName": "TopLevel",
55
"children": [
6+
{
7+
"kind": "TypeDecl",
8+
"name": "P1",
9+
"printedName": "P1",
10+
"declKind": "Protocol",
11+
"usr": "s:4cake2P1P",
12+
"location": "",
13+
"moduleName": "cake"
14+
},
15+
{
16+
"kind": "TypeDecl",
17+
"name": "P2",
18+
"printedName": "P2",
19+
"declKind": "Protocol",
20+
"usr": "s:4cake2P2P",
21+
"location": "",
22+
"moduleName": "cake"
23+
},
624
{
725
"kind": "TypeDecl",
826
"name": "C0",
@@ -93,6 +111,10 @@
93111
"usr": "s:4cake2S1V",
94112
"location": "",
95113
"moduleName": "cake",
114+
"conformingProtocols": [
115+
"P1",
116+
"P2"
117+
],
96118
"children": [
97119
{
98120
"kind": "Function",
@@ -389,6 +411,165 @@
389411
"printedName": "S1"
390412
}
391413
]
414+
},
415+
{
416+
"kind": "TypeDecl",
417+
"name": "Number",
418+
"printedName": "Number",
419+
"declKind": "Enum",
420+
"usr": "s:4cake6NumberO",
421+
"location": "",
422+
"moduleName": "cake",
423+
"conformingProtocols": [
424+
"Equatable",
425+
"Hashable",
426+
"RawRepresentable"
427+
],
428+
"children": [
429+
{
430+
"kind": "Var",
431+
"name": "one",
432+
"printedName": "one",
433+
"declKind": "EnumElement",
434+
"usr": "s:4cake6NumberO3oneyA2CmF",
435+
"location": "",
436+
"moduleName": "cake",
437+
"children": [
438+
{
439+
"kind": "TypeFunc",
440+
"name": "Function",
441+
"printedName": "(Number.Type) -> Number",
442+
"children": [
443+
{
444+
"kind": "TypeNominal",
445+
"name": "Number",
446+
"printedName": "Number"
447+
},
448+
{
449+
"kind": "TypeNominal",
450+
"name": "Metatype",
451+
"printedName": "Number.Type",
452+
"children": [
453+
{
454+
"kind": "TypeNominal",
455+
"name": "Number",
456+
"printedName": "Number"
457+
}
458+
]
459+
}
460+
]
461+
}
462+
]
463+
},
464+
{
465+
"kind": "TypeAlias",
466+
"name": "RawValue",
467+
"printedName": "RawValue",
468+
"declKind": "TypeAlias",
469+
"usr": "s:4cake6NumberO8RawValuea",
470+
"location": "",
471+
"moduleName": "cake",
472+
"children": [
473+
{
474+
"kind": "TypeNominal",
475+
"name": "Int",
476+
"printedName": "Int"
477+
}
478+
]
479+
},
480+
{
481+
"kind": "Var",
482+
"name": "hashValue",
483+
"printedName": "hashValue",
484+
"declKind": "Var",
485+
"usr": "s:4cake6NumberO9hashValueSivp",
486+
"location": "",
487+
"moduleName": "cake",
488+
"children": [
489+
{
490+
"kind": "TypeNominal",
491+
"name": "Int",
492+
"printedName": "Int"
493+
},
494+
{
495+
"kind": "Getter",
496+
"name": "_",
497+
"printedName": "_()",
498+
"declKind": "Accessor",
499+
"usr": "s:4cake6NumberO9hashValueSivg",
500+
"location": "",
501+
"moduleName": "cake",
502+
"children": [
503+
{
504+
"kind": "TypeNominal",
505+
"name": "Int",
506+
"printedName": "Int"
507+
}
508+
]
509+
}
510+
]
511+
},
512+
{
513+
"kind": "Constructor",
514+
"name": "init",
515+
"printedName": "init(rawValue:)",
516+
"declKind": "Constructor",
517+
"usr": "s:4cake6NumberO8rawValueACSgSi_tcfc",
518+
"location": "",
519+
"moduleName": "cake",
520+
"children": [
521+
{
522+
"kind": "TypeNominal",
523+
"name": "Optional",
524+
"printedName": "Number?",
525+
"children": [
526+
{
527+
"kind": "TypeNominal",
528+
"name": "Number",
529+
"printedName": "Number"
530+
}
531+
]
532+
},
533+
{
534+
"kind": "TypeNominal",
535+
"name": "Int",
536+
"printedName": "Int"
537+
}
538+
]
539+
},
540+
{
541+
"kind": "Var",
542+
"name": "rawValue",
543+
"printedName": "rawValue",
544+
"declKind": "Var",
545+
"usr": "s:4cake6NumberO8rawValueSivp",
546+
"location": "",
547+
"moduleName": "cake",
548+
"children": [
549+
{
550+
"kind": "TypeNominal",
551+
"name": "Int",
552+
"printedName": "Int"
553+
},
554+
{
555+
"kind": "Getter",
556+
"name": "_",
557+
"printedName": "_()",
558+
"declKind": "Accessor",
559+
"usr": "s:4cake6NumberO8rawValueSivg",
560+
"location": "",
561+
"moduleName": "cake",
562+
"children": [
563+
{
564+
"kind": "TypeNominal",
565+
"name": "Int",
566+
"printedName": "Int"
567+
}
568+
]
569+
}
570+
]
571+
}
572+
]
392573
}
393574
]
394575
}

tools/swift-api-digester/swift-api-digester.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ struct SDKNodeInitInfo {
331331
Ownership Ownership = Ownership::Strong;
332332
std::vector<SDKDeclAttrKind> DeclAttrs;
333333
std::vector<TypeAttrKind> TypeAttrs;
334+
std::vector<StringRef> ConformingProtocols;
334335
StringRef SuperclassUsr;
335336
ParentExtensionInfo *ExtInfo = nullptr;
336337
TypeInitInfo TypeInfo;
@@ -774,12 +775,14 @@ SDKNodeDecl *SDKNodeType::getClosestParentDecl() const {
774775

775776
class SDKNodeTypeDecl : public SDKNodeDecl {
776777
StringRef SuperclassUsr;
778+
std::vector<StringRef> ConformingProtocols;
777779
public:
778780
SDKNodeTypeDecl(SDKNodeInitInfo Info) : SDKNodeDecl(Info, SDKNodeKind::TypeDecl),
779-
SuperclassUsr(Info.SuperclassUsr) {}
781+
SuperclassUsr(Info.SuperclassUsr),
782+
ConformingProtocols(Info.ConformingProtocols){}
780783
static bool classof(const SDKNode *N);
781784
StringRef getSuperClassUsr() const { return SuperclassUsr; }
782-
785+
ArrayRef<StringRef> getAllProtocols() const { return ConformingProtocols; }
783786
Optional<SDKNodeTypeDecl*> getSuperclass() const {
784787
if (SuperclassUsr.empty())
785788
return None;
@@ -946,6 +949,13 @@ SDKNode* SDKNode::constructSDKNode(SDKContext &Ctx,
946949
}
947950
break;
948951
}
952+
case KeyKind::KK_conformingProtocols: {
953+
assert(Info.ConformingProtocols.empty());
954+
for (auto &Name : *cast<llvm::yaml::SequenceNode>(Pair.getValue())) {
955+
Info.ConformingProtocols.push_back(GetScalarString(&Name));
956+
}
957+
break;
958+
}
949959
case KeyKind::KK_printedName:
950960
Info.PrintedName = GetScalarString(Pair.getValue());
951961
break;
@@ -1272,6 +1282,13 @@ SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, ValueDecl *VD) : Ctx(Ctx),
12721282
ExtInfo->Requirements.emplace_back(Ctx.buffer(OS.str()));
12731283
}
12741284
}
1285+
1286+
// Get all protocol names this type decl conforms to.
1287+
if (auto *NTD = dyn_cast<NominalTypeDecl>(VD)) {
1288+
for (auto *P: NTD->getAllProtocols()) {
1289+
ConformingProtocols.push_back(P->getName().str());
1290+
}
1291+
}
12751292
}
12761293

12771294
SDKNode *SDKNodeInitInfo::createSDKNode(SDKNodeKind Kind) {
@@ -1647,6 +1664,12 @@ namespace swift {
16471664
out.mapRequired(getKeyContent(Ctx, KeyKind::KK_superclassUsr).data(),
16481665
Super);
16491666
}
1667+
auto Pros = TD->getAllProtocols();
1668+
if (!Pros.empty()) {
1669+
out.mapRequired(getKeyContent(Ctx,
1670+
KeyKind::KK_conformingProtocols).data(),
1671+
Pros);
1672+
}
16501673
}
16511674
if (D->isFromExtension()) {
16521675
// Even if we don't have any requirements on this parent extension,

0 commit comments

Comments
 (0)