Skip to content

Commit 236cd6b

Browse files
d-ronnqvistmeg-gupta
authored andcommitted
[SymbolGraphGen] Emit "functionSignature" info for initializers and subscripts (swiftlang#70207)
rdar://111072228
1 parent 9cf6c41 commit 236cd6b

File tree

3 files changed

+151
-58
lines changed

3 files changed

+151
-58
lines changed

lib/SymbolGraphGen/Symbol.cpp

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -407,35 +407,38 @@ void Symbol::serializeDocComment(llvm::json::OStream &OS) const {
407407
}
408408

409409
void Symbol::serializeFunctionSignature(llvm::json::OStream &OS) const {
410-
if (const auto *FD = dyn_cast_or_null<FuncDecl>(D)) {
411-
OS.attributeObject("functionSignature", [&](){
410+
auto serializeParameterList = [&](const swift::ParameterList *ParamList) {
411+
if (ParamList->size()) {
412+
OS.attributeArray("parameters", [&]() {
413+
for (const auto *Param : *ParamList) {
414+
auto ExternalName = Param->getArgumentName().str();
415+
auto InternalName = Param->getParameterName().str();
416+
417+
OS.object([&]() {
418+
if (ExternalName.empty()) {
419+
OS.attribute("name", InternalName);
420+
} else {
421+
OS.attribute("name", ExternalName);
422+
if (ExternalName != InternalName && !InternalName.empty()) {
423+
OS.attribute("internalName", InternalName);
424+
}
425+
}
426+
Graph->serializeDeclarationFragments(
427+
"declarationFragments",
428+
Symbol(Graph, Param, getSynthesizedBaseTypeDecl(),
429+
getBaseType()),
430+
OS);
431+
}); // end parameter object
432+
}
433+
}); // end parameters:
434+
}
435+
};
412436

437+
if (const auto *FD = dyn_cast_or_null<FuncDecl>(D)) {
438+
OS.attributeObject("functionSignature", [&]() {
413439
// Parameters
414440
if (const auto *ParamList = FD->getParameters()) {
415-
if (ParamList->size()) {
416-
OS.attributeArray("parameters", [&](){
417-
for (const auto *Param : *ParamList) {
418-
auto ExternalName = Param->getArgumentName().str();
419-
auto InternalName = Param->getParameterName().str();
420-
421-
OS.object([&](){
422-
if (ExternalName.empty()) {
423-
OS.attribute("name", InternalName);
424-
} else {
425-
OS.attribute("name", ExternalName);
426-
if (ExternalName != InternalName &&
427-
!InternalName.empty()) {
428-
OS.attribute("internalName", InternalName);
429-
}
430-
}
431-
Graph->serializeDeclarationFragments("declarationFragments",
432-
Symbol(Graph, Param,
433-
getSynthesizedBaseTypeDecl(),
434-
getBaseType()), OS);
435-
}); // end parameter object
436-
}
437-
}); // end parameters:
438-
}
441+
serializeParameterList(ParamList);
439442
}
440443

441444
// Returns
@@ -444,6 +447,26 @@ void Symbol::serializeFunctionSignature(llvm::json::OStream &OS) const {
444447
OS);
445448
}
446449
});
450+
} else if (const auto *CD = dyn_cast_or_null<ConstructorDecl>(D)) {
451+
OS.attributeObject("functionSignature", [&]() {
452+
// Parameters
453+
if (const auto *ParamList = CD->getParameters()) {
454+
serializeParameterList(ParamList);
455+
}
456+
});
457+
} else if (const auto *SD = dyn_cast_or_null<SubscriptDecl>(D)) {
458+
OS.attributeObject("functionSignature", [&]() {
459+
// Parameters
460+
if (const auto *ParamList = SD->getIndices()) {
461+
serializeParameterList(ParamList);
462+
}
463+
464+
// Returns
465+
if (const auto ReturnType = SD->getElementInterfaceType()) {
466+
Graph->serializeDeclarationFragments("returns", ReturnType, BaseType,
467+
OS);
468+
}
469+
});
447470
}
448471
}
449472

test/SymbolGraph/Symbols/Mixins/DeclarationFragments/Full/Properties/Subscripts.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public struct S {
1414
}
1515

1616
// FULL-LABEL: "precise": "s:10Subscripts1SVyS2icip"
17+
// FULL: "functionSignature": {
18+
// FULL: "returns": [
1719
// FULL: "declarationFragments": [
1820
// FULL-NEXT: {
1921
// FULL-NEXT: "kind": "keyword",
Lines changed: 100 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,105 @@
11
// RUN: %empty-directory(%t)
22
// RUN: %target-build-swift %s -module-name FunctionSignature -emit-module -emit-module-path %t/
33
// RUN: %target-swift-symbolgraph-extract -module-name FunctionSignature -I %t -pretty-print -output-dir %t
4-
// RUN: %FileCheck %s --input-file %t/FunctionSignature.symbols.json
4+
// RUN: %FileCheck %s --input-file %t/FunctionSignature.symbols.json --check-prefix=FUNC
5+
// RUN: %FileCheck %s --input-file %t/FunctionSignature.symbols.json --check-prefix=INIT
6+
// RUN: %FileCheck %s --input-file %t/FunctionSignature.symbols.json --check-prefix=SUBSCRIPT
57

6-
public func foo(_ noext: Int, ext int: Int) -> String {
7-
return "OK"
8-
}
8+
public struct MyStruct {
9+
public init(_ noext: Int, ext int: Int) {}
10+
11+
// INIT-LABEL: "precise": "s:17FunctionSignature8MyStructV_3extACSi_Sitcfc",
12+
// INIT: "name": "noext"
13+
// INIT-NOT: "internalName": "noext"
14+
// INIT-NEXT: declarationFragments
15+
16+
// INIT: "kind": "identifier"
17+
// INIT-NEXT: "spelling": "noext"
18+
// INIT: "kind": "text"
19+
// INIT-NEXT: "spelling": ": "
20+
// INIT: "kind": "typeIdentifier"
21+
// INIT-NEXT: "spelling": "Int"
22+
// INIT-NEXT: "preciseIdentifier": "s:Si"
23+
24+
// INIT: "name": "ext"
25+
// INIT-NEXT: "internalName": "int"
26+
// INIT-NEXT: declarationFragments
27+
28+
// INIT: "kind": "identifier"
29+
// INIT-NEXT: "spelling": "int"
30+
// INIT: "kind": "text"
31+
// INIT-NEXT: "spelling": ": "
32+
// INIT: "kind": "typeIdentifier"
33+
// INIT-NEXT: "spelling": "Int"
34+
// INIT-NEXT: "preciseIdentifier": "s:Si"
35+
36+
public subscript(_ noext: Int, ext int: Int) -> String {
37+
get { return "OK" }
38+
set { }
39+
}
40+
41+
// SUBSCRIPT-LABEL: "precise": "s:17FunctionSignature8MyStructV_3extSSSi_Sitcip",
42+
// SUBSCRIPT: "name": "noext"
43+
// SUBSCRIPT-NOT: "internalName": "noext"
44+
// SUBSCRIPT-NEXT: declarationFragments
45+
46+
// SUBSCRIPT: "kind": "identifier"
47+
// SUBSCRIPT-NEXT: "spelling": "noext"
48+
// SUBSCRIPT: "kind": "text"
49+
// SUBSCRIPT-NEXT: "spelling": ": "
50+
// SUBSCRIPT: "kind": "typeIdentifier"
51+
// SUBSCRIPT-NEXT: "spelling": "Int"
52+
// SUBSCRIPT-NEXT: "preciseIdentifier": "s:Si"
53+
54+
// SUBSCRIPT: "name": "ext"
55+
// SUBSCRIPT-NEXT: "internalName": "int"
56+
// SUBSCRIPT-NEXT: declarationFragments
957

10-
// CHECK: "name": "noext"
11-
// CHECK-NOT: "internalName": "noext"
12-
// CHECK-NEXT: declarationFragments
13-
14-
// CHECK: "kind": "identifier"
15-
// CHECK-NEXT: "spelling": "noext"
16-
// CHECK: "kind": "text"
17-
// CHECK-NEXT: "spelling": ": "
18-
// CHECK: "kind": "typeIdentifier"
19-
// CHECK-NEXT: "spelling": "Int"
20-
// CHECK-NEXT: "preciseIdentifier": "s:Si"
21-
22-
// CHECK: "name": "ext"
23-
// CHECK-NEXT: "internalName": "int"
24-
// CHECK-NEXT: declarationFragments
25-
26-
// CHECK: "kind": "identifier"
27-
// CHECK-NEXT: "spelling": "int"
28-
// CHECK: "kind": "text"
29-
// CHECK-NEXT: "spelling": ": "
30-
// CHECK: "kind": "typeIdentifier"
31-
// CHECK-NEXT: "spelling": "Int"
32-
// CHECK-NEXT: "preciseIdentifier": "s:Si"
33-
34-
// CHECK: returns
35-
// CHECK: "kind": "typeIdentifier"
36-
// CHECK-NEXT: "spelling": "String"
37-
// CHECK-NEXT: "preciseIdentifier": "s:SS"
58+
// SUBSCRIPT: "kind": "identifier"
59+
// SUBSCRIPT-NEXT: "spelling": "int"
60+
// SUBSCRIPT: "kind": "text"
61+
// SUBSCRIPT-NEXT: "spelling": ": "
62+
// SUBSCRIPT: "kind": "typeIdentifier"
63+
// SUBSCRIPT-NEXT: "spelling": "Int"
64+
// SUBSCRIPT-NEXT: "preciseIdentifier": "s:Si"
65+
66+
// SUBSCRIPT: returns
67+
// SUBSCRIPT: "kind": "typeIdentifier"
68+
// SUBSCRIPT-NEXT: "spelling": "String"
69+
// SUBSCRIPT-NEXT: "preciseIdentifier": "s:SS"
70+
71+
public func foo(_ noext: Int, ext int: Int) -> String {
72+
return "OK"
73+
}
74+
75+
// FUNC-LABEL: "precise": "s:17FunctionSignature8MyStructV3foo_3extSSSi_SitF",
76+
// FUNC: "name": "noext"
77+
// FUNC-NOT: "internalName": "noext"
78+
// FUNC-NEXT: declarationFragments
79+
80+
// FUNC: "kind": "identifier"
81+
// FUNC-NEXT: "spelling": "noext"
82+
// FUNC: "kind": "text"
83+
// FUNC-NEXT: "spelling": ": "
84+
// FUNC: "kind": "typeIdentifier"
85+
// FUNC-NEXT: "spelling": "Int"
86+
// FUNC-NEXT: "preciseIdentifier": "s:Si"
87+
88+
// FUNC: "name": "ext"
89+
// FUNC-NEXT: "internalName": "int"
90+
// FUNC-NEXT: declarationFragments
91+
92+
// FUNC: "kind": "identifier"
93+
// FUNC-NEXT: "spelling": "int"
94+
// FUNC: "kind": "text"
95+
// FUNC-NEXT: "spelling": ": "
96+
// FUNC: "kind": "typeIdentifier"
97+
// FUNC-NEXT: "spelling": "Int"
98+
// FUNC-NEXT: "preciseIdentifier": "s:Si"
99+
100+
// FUNC: returns
101+
// FUNC: "kind": "typeIdentifier"
102+
// FUNC-NEXT: "spelling": "String"
103+
// FUNC-NEXT: "preciseIdentifier": "s:SS"
104+
105+
}

0 commit comments

Comments
 (0)