Skip to content

Commit 23525d9

Browse files
authored
Merge pull request swiftlang#5499 from nkcsgexi/digester-ownership
2 parents 5564173 + 4475b07 commit 23525d9

File tree

4 files changed

+176
-8
lines changed

4 files changed

+176
-8
lines changed

test/api-digester/Inputs/cake.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ public struct S1 {
88

99
public class C1 {
1010
open class func foo1() {}
11+
public weak var Ins : C1?
12+
public unowned var Ins2 : C1 = C1()
1113
}

test/api-digester/Outputs/cake.json

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,148 @@
9090
}
9191
]
9292
},
93+
{
94+
"kind": "Var",
95+
"name": "Ins",
96+
"printedName": "Ins",
97+
"declKind": "Var",
98+
"usr": "s:vC4cake2C13InsXwGSqS0__",
99+
"location": "",
100+
"moduleName": "cake",
101+
"ownership": 1,
102+
"children": [
103+
{
104+
"kind": "TypeNominal",
105+
"name": "WeakStorage",
106+
"printedName": "C1?"
107+
},
108+
{
109+
"kind": "Getter",
110+
"name": "_",
111+
"printedName": "_()",
112+
"declKind": "Func",
113+
"usr": "s:FC4cake2C1g3InsXwGSqS0__",
114+
"location": "",
115+
"moduleName": "cake",
116+
"children": [
117+
{
118+
"kind": "TypeNominal",
119+
"name": "Optional",
120+
"printedName": "C1?",
121+
"children": [
122+
{
123+
"kind": "TypeNominal",
124+
"name": "C1",
125+
"printedName": "C1"
126+
}
127+
]
128+
},
129+
{
130+
"kind": "TypeNominal",
131+
"name": "C1",
132+
"printedName": "C1"
133+
}
134+
]
135+
},
136+
{
137+
"kind": "Setter",
138+
"name": "_",
139+
"printedName": "_()",
140+
"declKind": "Func",
141+
"usr": "s:FC4cake2C1s3InsXwGSqS0__",
142+
"location": "",
143+
"moduleName": "cake",
144+
"children": [
145+
{
146+
"kind": "TypeNominal",
147+
"name": "Void",
148+
"printedName": "()"
149+
},
150+
{
151+
"kind": "TypeNominal",
152+
"name": "C1",
153+
"printedName": "C1"
154+
},
155+
{
156+
"kind": "TypeNominal",
157+
"name": "Optional",
158+
"printedName": "C1?",
159+
"children": [
160+
{
161+
"kind": "TypeNominal",
162+
"name": "C1",
163+
"printedName": "C1"
164+
}
165+
]
166+
}
167+
]
168+
}
169+
]
170+
},
171+
{
172+
"kind": "Var",
173+
"name": "Ins2",
174+
"printedName": "Ins2",
175+
"declKind": "Var",
176+
"usr": "s:vC4cake2C14Ins2XoS0_",
177+
"location": "",
178+
"moduleName": "cake",
179+
"ownership": 2,
180+
"children": [
181+
{
182+
"kind": "TypeNominal",
183+
"name": "UnownedStorage",
184+
"printedName": "C1"
185+
},
186+
{
187+
"kind": "Getter",
188+
"name": "_",
189+
"printedName": "_()",
190+
"declKind": "Func",
191+
"usr": "s:FC4cake2C1g4Ins2XoS0_",
192+
"location": "",
193+
"moduleName": "cake",
194+
"children": [
195+
{
196+
"kind": "TypeNominal",
197+
"name": "C1",
198+
"printedName": "C1"
199+
},
200+
{
201+
"kind": "TypeNominal",
202+
"name": "C1",
203+
"printedName": "C1"
204+
}
205+
]
206+
},
207+
{
208+
"kind": "Setter",
209+
"name": "_",
210+
"printedName": "_()",
211+
"declKind": "Func",
212+
"usr": "s:FC4cake2C1s4Ins2XoS0_",
213+
"location": "",
214+
"moduleName": "cake",
215+
"children": [
216+
{
217+
"kind": "TypeNominal",
218+
"name": "Void",
219+
"printedName": "()"
220+
},
221+
{
222+
"kind": "TypeNominal",
223+
"name": "C1",
224+
"printedName": "C1"
225+
},
226+
{
227+
"kind": "TypeNominal",
228+
"name": "C1",
229+
"printedName": "C1"
230+
}
231+
]
232+
}
233+
]
234+
},
93235
{
94236
"kind": "Constructor",
95237
"name": "init",

tools/swift-api-digester/DigesterEnums.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ KEY(static)
7373
KEY(typeAttributes)
7474
KEY(declAttributes)
7575
KEY(declKind)
76+
KEY(ownership)
7677

7778
KNOWN_TYPE(Optional)
7879
KNOWN_TYPE(ImplicitlyUnwrappedOptional)

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

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -228,23 +228,23 @@ static StringRef getKeyContent(KeyKind Kind) {
228228
}
229229

230230
// The node kind appearing in the tree that describes the content of the SDK
231-
enum class SDKNodeKind {
231+
enum class SDKNodeKind: uint8_t {
232232
#define NODE_KIND(NAME) NAME,
233233
#include "DigesterEnums.def"
234234
};
235235

236-
enum class NodeAnnotation {
236+
enum class NodeAnnotation: uint8_t{
237237
#define NODE_ANNOTATION(NAME) NAME,
238238
#include "DigesterEnums.def"
239239
};
240240

241-
enum class KnownTypeKind {
241+
enum class KnownTypeKind: uint8_t {
242242
#define KNOWN_TYPE(NAME) NAME,
243243
#include "DigesterEnums.def"
244244
Unknown,
245245
};
246246

247-
enum class SDKDeclAttrKind {
247+
enum class SDKDeclAttrKind: uint8_t {
248248
#define DECL_ATTR(Name) DAK_##Name,
249249
#include "DigesterEnums.def"
250250
};
@@ -283,6 +283,7 @@ struct SDKNodeInitInfo {
283283
bool IsMutating = false;
284284
bool IsStatic = false;
285285
Optional<uint8_t> SelfIndex;
286+
Ownership Ownership = Ownership::Strong;
286287
std::vector<SDKDeclAttrKind> DeclAttrs;
287288
std::vector<TypeAttrKind> TypeAttrs;
288289
SDKNodeInitInfo() = default;
@@ -344,20 +345,22 @@ class SDKNodeDecl : public SDKNode {
344345
StringRef ModuleName;
345346
std::vector<SDKDeclAttrKind> DeclAttributes;
346347
bool IsStatic;
348+
uint8_t Ownership;
347349
bool hasDeclAttribute(SDKDeclAttrKind DAKind) const;
348350

349351
protected:
350352
SDKNodeDecl(SDKNodeInitInfo Info, SDKNodeKind Kind) : SDKNode(Info, Kind),
351353
DKind(Info.DKind), Usr(Info.USR), Location(Info.Location),
352354
ModuleName(Info.ModuleName), DeclAttributes(Info.DeclAttrs),
353-
IsStatic(Info.IsStatic) {}
355+
IsStatic(Info.IsStatic), Ownership(uint8_t(Info.Ownership)) {}
354356

355357
public:
356358
StringRef getUsr() const { return Usr; }
357359
StringRef getLocation() const { return Location; }
358360
StringRef getModuleName() const {return ModuleName;}
359361
void addDeclAttribute(SDKDeclAttrKind DAKind);
360362
ArrayRef<SDKDeclAttrKind> getDeclAttributes() const;
363+
swift::Ownership getOwnership() const { return swift::Ownership(Ownership); }
361364
bool isObjc() const { return Usr.startswith("c:"); }
362365
static bool classof(const SDKNode *N);
363366
DeclKind getDeclKind() const { return DKind; }
@@ -762,6 +765,10 @@ NodeUniquePtr SDKNode::constructSDKNode(llvm::yaml::MappingNode *Node) {
762765
auto WithQuote = cast<llvm::yaml::ScalarNode>(N)->getRawValue();
763766
return WithQuote.substr(1, WithQuote.size() - 2);
764767
};
768+
769+
static auto getAsInt = [&](llvm::yaml::Node *N) -> int {
770+
return std::stoi(cast<llvm::yaml::ScalarNode>(N)->getRawValue());
771+
};
765772
SDKNodeKind Kind;
766773
SDKNodeInitInfo Info;
767774
NodeOwnedVector Children;
@@ -778,8 +785,7 @@ NodeUniquePtr SDKNode::constructSDKNode(llvm::yaml::MappingNode *Node) {
778785
Info.Name = GetScalarString(Pair.getValue());
779786
break;
780787
case KeyKind::KK_selfIndex:
781-
Info.SelfIndex = std::stoi(cast<llvm::yaml::ScalarNode>(Pair.getValue())->
782-
getRawValue());
788+
Info.SelfIndex = getAsInt(Pair.getValue());
783789
break;
784790
case KeyKind::KK_usr:
785791
Info.USR = GetScalarString(Pair.getValue());
@@ -808,6 +814,10 @@ NodeUniquePtr SDKNode::constructSDKNode(llvm::yaml::MappingNode *Node) {
808814
case KeyKind::KK_static:
809815
Info.IsStatic = true;
810816
break;
817+
case KeyKind::KK_ownership:
818+
Info.Ownership = swift::Ownership(getAsInt(Pair.getValue()));
819+
assert(Info.Ownership != swift::Ownership::Strong && "Stong is implied.");
820+
break;
811821

812822
case KeyKind::KK_typeAttributes: {
813823
auto *Seq = cast<llvm::yaml::SequenceNode>(Pair.getValue());
@@ -1013,6 +1023,13 @@ static Optional<uint8_t> getSelfIndex(ValueDecl *VD) {
10131023
return None;
10141024
}
10151025

1026+
static Ownership getOwnership(ValueDecl *VD) {
1027+
if (auto OA = VD->getAttrs().getAttribute<OwnershipAttr>()) {
1028+
return OA->get();
1029+
}
1030+
return Ownership::Strong;
1031+
}
1032+
10161033
SDKNodeInitInfo::SDKNodeInitInfo(Type Ty) : Name(getTypeName(Ty)),
10171034
PrintedName(getPrintedName(Ty)) {
10181035
if (isFunctionTypeNoEscape(Ty))
@@ -1025,7 +1042,8 @@ SDKNodeInitInfo::SDKNodeInitInfo(ValueDecl *VD) :
10251042
USR(calculateUsr(VD)), Location(calculateLocation(VD)),
10261043
ModuleName(VD->getModuleContext()->getName().str()),
10271044
IsThrowing(isFuncThrowing(VD)), IsMutating(isFuncMutating(VD)),
1028-
IsStatic(VD->isStatic()), SelfIndex(getSelfIndex(VD)) {
1045+
IsStatic(VD->isStatic()), SelfIndex(getSelfIndex(VD)),
1046+
Ownership(getOwnership(VD)) {
10291047
if (VD->getAttrs().getDeprecated(VD->getASTContext()))
10301048
DeclAttrs.push_back(SDKDeclAttrKind::DAK_deprecated);
10311049
}
@@ -1383,6 +1401,11 @@ namespace swift {
13831401
if (!Attributes.empty())
13841402
out.mapRequired(getKeyContent(KeyKind::KK_declAttributes).data(),
13851403
Attributes);
1404+
// Strong reference is implied, no need for serialization.
1405+
if (D->getOwnership() != Ownership::Strong) {
1406+
uint8_t Raw = uint8_t(D->getOwnership());
1407+
out.mapRequired(getKeyContent(KeyKind::KK_ownership).data(), Raw);
1408+
}
13861409
} else if (auto T = dyn_cast<SDKNodeType>(value.get())) {
13871410
auto Attributes = T->getTypeAttributes();
13881411
if (!Attributes.empty())

0 commit comments

Comments
 (0)