Skip to content

Commit 5bab518

Browse files
committed
swift-module-digester: include parameters' value ownership values in the digests.
1 parent 78271f1 commit 5bab518

File tree

5 files changed

+52
-24
lines changed

5 files changed

+52
-24
lines changed

include/swift/IDE/DigesterEnums.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ KEY_STRING(SuperclassUsr, superclassUsr)
141141
KEY_STRING(EnumRawTypeName, enumRawTypeName)
142142
KEY_STRING(GenericSig, genericSig)
143143
KEY_STRING(FuncSelfKind, funcSelfKind)
144+
KEY_STRING(ParamValueOwnership, paramValueOwnership)
144145

145146
KEY_STRING_ARR(SuperclassNames, superclassNames)
146147
KEY_STRING_ARR(ConformingProtocols, conformingProtocols)

test/api-digester/Outputs/cake-abi.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@
563563
"kind": "TypeNominal",
564564
"name": "Hasher",
565565
"printedName": "Hasher",
566+
"paramValueOwnership": "InOut",
566567
"usr": "s:s6HasherV"
567568
}
568569
],

test/api-digester/Outputs/cake.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,7 @@
631631
"kind": "TypeNominal",
632632
"name": "Hasher",
633633
"printedName": "Hasher",
634+
"paramValueOwnership": "InOut",
634635
"usr": "s:s6HasherV"
635636
}
636637
],

tools/swift-api-digester/ModuleAnalyzerNodes.cpp

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ struct swift::ide::api::SDKNodeInitInfo {
4242
SDKNodeInitInfo(SDKContext &Ctx, Decl *D);
4343
SDKNodeInitInfo(SDKContext &Ctx, ValueDecl *VD);
4444
SDKNodeInitInfo(SDKContext &Ctx, OperatorDecl *D);
45-
SDKNodeInitInfo(SDKContext &Ctx, Type Ty, bool IsImplicitlyUnwrappedOptional,
46-
bool hasDefaultArgument);
45+
SDKNodeInitInfo(SDKContext &Ctx, Type Ty, TypeInitInfo Info = TypeInitInfo());
4746
SDKNode* createSDKNode(SDKNodeKind Kind);
4847
};
4948

@@ -86,7 +85,8 @@ SDKNodeDecl::SDKNodeDecl(SDKNodeInitInfo Info, SDKNodeKind Kind)
8685

8786
SDKNodeType::SDKNodeType(SDKNodeInitInfo Info, SDKNodeKind Kind):
8887
SDKNode(Info, Kind), TypeAttributes(Info.TypeAttrs),
89-
HasDefaultArg(Info.HasDefaultArg) {}
88+
HasDefaultArg(Info.HasDefaultArg),
89+
ParamValueOwnership(Info.ParamValueOwnership) {}
9090

9191
SDKNodeTypeNominal::SDKNodeTypeNominal(SDKNodeInitInfo Info):
9292
SDKNodeType(Info, SDKNodeKind::TypeNominal), USR(Info.Usr) {}
@@ -668,6 +668,8 @@ bool static isSDKNodeEqual(SDKContext &Ctx, const SDKNode &L, const SDKNode &R)
668668
return false;
669669
if (Left->hasDefaultArgument() != Right->hasDefaultArgument())
670670
return false;
671+
if (Left->getParamValueOwnership() != Right->getParamValueOwnership())
672+
return false;
671673
if (Left->getPrintedName() == Right->getPrintedName())
672674
return true;
673675
return Left->getName() == Right->getName() &&
@@ -1013,12 +1015,11 @@ static Optional<uint8_t> getFixedBinaryOrder(ValueDecl *VD) {
10131015
return llvm::None;
10141016
}
10151017

1016-
SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, Type Ty,
1017-
bool IsImplicitlyUnwrappedOptional = false,
1018-
bool hasDefaultArgument = false) :
1019-
Ctx(Ctx), Name(getTypeName(Ctx, Ty, IsImplicitlyUnwrappedOptional)),
1020-
PrintedName(getPrintedName(Ctx, Ty, IsImplicitlyUnwrappedOptional)),
1021-
HasDefaultArg(hasDefaultArgument) {
1018+
SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, Type Ty, TypeInitInfo Info) :
1019+
Ctx(Ctx), Name(getTypeName(Ctx, Ty, Info.IsImplicitlyUnwrappedOptional)),
1020+
PrintedName(getPrintedName(Ctx, Ty, Info.IsImplicitlyUnwrappedOptional)),
1021+
ParamValueOwnership(Info.ValueOwnership),
1022+
HasDefaultArg(Info.hasDefaultArgument) {
10221023
if (isFunctionTypeNoEscape(Ty))
10231024
TypeAttrs.push_back(TypeAttrKind::TAK_noescape);
10241025
// If this is a nominal type, get its Usr.
@@ -1123,14 +1124,11 @@ case SDKNodeKind::X: \
11231124
// Recursively construct a node that represents a type, for instance,
11241125
// representing the return value type of a function decl.
11251126
SDKNode *swift::ide::api::
1126-
SwiftDeclCollector::constructTypeNode(Type T,
1127-
bool IsImplicitlyUnwrappedOptional,
1128-
bool hasDefaultArgument) {
1127+
SwiftDeclCollector::constructTypeNode(Type T, TypeInitInfo Info) {
11291128
if (Ctx.checkingABI()) {
11301129
T = T->getCanonicalType();
11311130
}
1132-
SDKNode* Root = SDKNodeInitInfo(Ctx, T, IsImplicitlyUnwrappedOptional,
1133-
hasDefaultArgument).createSDKNode(SDKNodeKind::TypeNominal);
1131+
SDKNode* Root = SDKNodeInitInfo(Ctx, T, Info).createSDKNode(SDKNodeKind::TypeNominal);
11341132

11351133
if (auto NAT = dyn_cast<NameAliasType>(T.getPointer())) {
11361134
SDKNode* Root = SDKNodeInitInfo(Ctx, T).createSDKNode(SDKNodeKind::TypeAlias);
@@ -1179,10 +1177,20 @@ std::vector<SDKNode*> swift::ide::api::
11791177
SwiftDeclCollector::createParameterNodes(ParameterList *PL) {
11801178
std::vector<SDKNode*> Result;
11811179
for (auto param: *PL) {
1182-
Result.push_back(constructTypeNode(param->getInterfaceType(),
1183-
param->getAttrs().hasAttribute<ImplicitlyUnwrappedOptionalAttr>(),
1184-
param->getDefaultArgumentKind() != DefaultArgumentKind::None));
1185-
1180+
TypeInitInfo Info;
1181+
Info.IsImplicitlyUnwrappedOptional = param->getAttrs().
1182+
hasAttribute<ImplicitlyUnwrappedOptionalAttr>();
1183+
Info.hasDefaultArgument = param->getDefaultArgumentKind() !=
1184+
DefaultArgumentKind::None;
1185+
switch (param->getValueOwnership()) {
1186+
#define CASE(KIND) case ValueOwnership::KIND: Info.ValueOwnership = #KIND; break;
1187+
CASE(Owned)
1188+
CASE(InOut)
1189+
CASE(Shared)
1190+
case ValueOwnership::Default: break;
1191+
#undef CASE
1192+
}
1193+
Result.push_back(constructTypeNode(param->getInterfaceType(), Info));
11861194
}
11871195
return Result;
11881196
}
@@ -1195,8 +1203,10 @@ SDKNode *swift::ide::api::
11951203
SwiftDeclCollector::constructFunctionNode(FuncDecl* FD,
11961204
SDKNodeKind Kind) {
11971205
auto Func = SDKNodeInitInfo(Ctx, FD).createSDKNode(Kind);
1198-
Func->addChild(constructTypeNode(FD->getResultInterfaceType(),
1199-
FD->getAttrs().hasAttribute<ImplicitlyUnwrappedOptionalAttr>()));
1206+
TypeInitInfo Info;
1207+
Info.IsImplicitlyUnwrappedOptional = FD->getAttrs().
1208+
hasAttribute<ImplicitlyUnwrappedOptionalAttr>();
1209+
Func->addChild(constructTypeNode(FD->getResultInterfaceType(), Info));
12001210
for (auto *Node : createParameterNodes(FD->getParameters()))
12011211
Func->addChild(Node);
12021212
return Func;
@@ -1295,8 +1305,10 @@ SwiftDeclCollector::constructExternalExtensionNode(NominalTypeDecl *NTD,
12951305
SDKNode *swift::ide::api::
12961306
SwiftDeclCollector::constructVarNode(ValueDecl *VD) {
12971307
auto Var = SDKNodeInitInfo(Ctx, VD).createSDKNode(SDKNodeKind::DeclVar);
1298-
Var->addChild(constructTypeNode(VD->getInterfaceType(),
1299-
VD->getAttrs().hasAttribute<ImplicitlyUnwrappedOptionalAttr>()));
1308+
TypeInitInfo Info;
1309+
Info.IsImplicitlyUnwrappedOptional = VD->getAttrs().
1310+
hasAttribute<ImplicitlyUnwrappedOptionalAttr>();
1311+
Var->addChild(constructTypeNode(VD->getInterfaceType(), Info));
13001312
if (auto VAD = dyn_cast<AbstractStorageDecl>(VD)) {
13011313
if (auto Getter = VAD->getGetter())
13021314
Var->addChild(constructFunctionNode(Getter, SDKNodeKind::DeclGetter));
@@ -1513,6 +1525,7 @@ void SDKNodeType::jsonize(json::Output &out) {
15131525
SDKNode::jsonize(out);
15141526
out.mapOptional(getKeyContent(Ctx, KeyKind::KK_typeAttributes).data(), TypeAttributes);
15151527
output(out, KeyKind::KK_hasDefaultArg, HasDefaultArg);
1528+
output(out, KeyKind::KK_paramValueOwnership, ParamValueOwnership);
15161529
}
15171530

15181531
void SDKNodeTypeNominal::jsonize(json::Output &out) {

tools/swift-api-digester/ModuleAnalyzerNodes.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,9 @@ class SDKNodeType: public SDKNode {
350350
std::vector<TypeAttrKind> TypeAttributes;
351351
bool HasDefaultArg;
352352

353+
// Empty() implies "Default"
354+
StringRef ParamValueOwnership;
355+
353356
protected:
354357
SDKNodeType(SDKNodeInitInfo Info, SDKNodeKind Kind);
355358
~SDKNodeType() = default;
@@ -365,6 +368,7 @@ class SDKNodeType: public SDKNode {
365368
bool hasDefaultArgument() const { return HasDefaultArg; }
366369
bool isTopLevelType() const { return !isa<SDKNodeType>(getParent()); }
367370
StringRef getTypeRoleDescription() const;
371+
StringRef getParamValueOwnership() const { return ParamValueOwnership; }
368372
static bool classof(const SDKNode *N);
369373
virtual void jsonize(json::Output &Out) override;
370374
virtual void diagnose(SDKNode *Right) override;
@@ -566,6 +570,15 @@ class SDKNodeDeclSetter: public SDKNodeDeclAbstractFunc {
566570
static bool classof(const SDKNode *N);
567571
};
568572

573+
// The additional information we need for a type node in the digest.
574+
// We use type node to represent entities more than types, e.g. parameters, so
575+
// this struct is necessary to pass down to create a type node.
576+
struct TypeInitInfo {
577+
bool IsImplicitlyUnwrappedOptional = false;
578+
bool hasDefaultArgument = false;
579+
StringRef ValueOwnership;
580+
};
581+
569582
class SwiftDeclCollector: public VisibleDeclConsumer {
570583
SDKContext &Ctx;
571584
std::vector<std::unique_ptr<llvm::MemoryBuffer>> OwnedBuffers;
@@ -607,8 +620,7 @@ class SwiftDeclCollector: public VisibleDeclConsumer {
607620
SDKNode *constructFunctionNode(FuncDecl* FD, SDKNodeKind Kind);
608621
SDKNode *constructOperatorDeclNode(OperatorDecl *OD);
609622
std::vector<SDKNode*> createParameterNodes(ParameterList *PL);
610-
SDKNode *constructTypeNode(Type T, bool IsImplicitlyUnwrappedOptional = false,
611-
bool hasDefaultArgument = false);
623+
SDKNode *constructTypeNode(Type T, TypeInitInfo Info = TypeInitInfo());
612624
void processValueDecl(ValueDecl *VD);
613625
void foundDecl(ValueDecl *VD, DeclVisibilityKind Reason) override;
614626
void processDecl(Decl *D);

0 commit comments

Comments
 (0)