Skip to content

Commit 9fcc59b

Browse files
committed
swift-module-digester: include operator declarations in the module dump.
Their changes should have no impact on ABI, but can be source-breaking.
1 parent 7b03ea5 commit 9fcc59b

File tree

7 files changed

+107
-41
lines changed

7 files changed

+107
-41
lines changed

include/swift/IDE/DigesterEnums.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ NODE_KIND(DeclSetter, Setter)
6767
NODE_KIND(DeclSubscript, Subscript)
6868
NODE_KIND_RANGE(DeclAbstractFunc, DeclFunction, DeclSubscript)
6969

70+
NODE_KIND(DeclOperator, OperatorDecl)
7071
NODE_KIND(DeclType, TypeDecl)
7172
NODE_KIND(DeclVar, Var)
7273
NODE_KIND(DeclTypeAlias, TypeAlias)

test/api-digester/Inputs/cake.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,5 @@ public let GlobalVar = 1
8484
public extension P1 {
8585
static func +(lhs: P1, rhs: P1) -> P1 { return lhs }
8686
}
87+
88+
infix operator ..*..

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,16 @@
11471147
"isLet": true,
11481148
"hasStorage": true
11491149
},
1150+
{
1151+
"kind": "OperatorDecl",
1152+
"name": "..*..",
1153+
"printedName": "..*..",
1154+
"declKind": "InfixOperator",
1155+
"moduleName": "cake",
1156+
"declAttributes": [
1157+
"Infix"
1158+
]
1159+
},
11501160
{
11511161
"kind": "TypeDecl",
11521162
"name": "Int",

test/api-digester/Outputs/cake.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,16 @@
11451145
"isLet": true,
11461146
"hasStorage": true
11471147
},
1148+
{
1149+
"kind": "OperatorDecl",
1150+
"name": "..*..",
1151+
"printedName": "..*..",
1152+
"declKind": "InfixOperator",
1153+
"moduleName": "cake",
1154+
"declAttributes": [
1155+
"Infix"
1156+
]
1157+
},
11481158
{
11491159
"kind": "TypeDecl",
11501160
"name": "Int",

tools/swift-api-digester/ModuleAnalyzerNodes.cpp

Lines changed: 72 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ struct swift::ide::api::SDKNodeInitInfo {
3939
std::vector<TypeAttrKind> TypeAttrs;
4040

4141
SDKNodeInitInfo(SDKContext &Ctx) : Ctx(Ctx) {}
42+
SDKNodeInitInfo(SDKContext &Ctx, Decl *D);
4243
SDKNodeInitInfo(SDKContext &Ctx, ValueDecl *VD);
44+
SDKNodeInitInfo(SDKContext &Ctx, OperatorDecl *D);
4345
SDKNodeInitInfo(SDKContext &Ctx, Type Ty, bool IsImplicitlyUnwrappedOptional,
4446
bool hasDefaultArgument);
4547
SDKNode* createSDKNode(SDKNodeKind Kind);
@@ -55,6 +57,15 @@ SDKContext::SDKContext(CheckerOptions Opts): Diags(SourceMgr), Opts(Opts) {
5557
#undef ADD
5658
}
5759

60+
void SDKNodeRoot::registerDescendant(SDKNode *D) {
61+
// Operator doesn't have usr
62+
if (isa<SDKNodeDeclOperator>(D))
63+
return;
64+
if (auto DD = dyn_cast<SDKNodeDecl>(D)) {
65+
assert(!DD->getUsr().empty());
66+
DescendantDeclTable[DD->getUsr()].insert(DD);
67+
}
68+
}
5869

5970
SDKNode::SDKNode(SDKNodeInitInfo Info, SDKNodeKind Kind): Ctx(Info.Ctx),
6071
Name(Info.Name), PrintedName(Info.PrintedName), TheKind(unsigned(Kind)) {}
@@ -86,12 +97,15 @@ SDKNodeTypeFunc::SDKNodeTypeFunc(SDKNodeInitInfo Info):
8697
SDKNodeTypeAlias::SDKNodeTypeAlias(SDKNodeInitInfo Info):
8798
SDKNodeType(Info, SDKNodeKind::TypeAlias) {}
8899

89-
SDKNodeDeclType::SDKNodeDeclType(SDKNodeInitInfo Info):
100+
SDKNodeDeclType::SDKNodeDeclType(SDKNodeInitInfo Info):
90101
SDKNodeDecl(Info, SDKNodeKind::DeclType), SuperclassUsr(Info.SuperclassUsr),
91102
SuperclassNames(Info.SuperclassNames),
92103
ConformingProtocols(Info.ConformingProtocols),
93104
EnumRawTypeName(Info.EnumRawTypeName) {}
94105

106+
SDKNodeDeclOperator::SDKNodeDeclOperator(SDKNodeInitInfo Info):
107+
SDKNodeDecl(Info, SDKNodeKind::DeclOperator) {}
108+
95109
SDKNodeDeclTypeAlias::SDKNodeDeclTypeAlias(SDKNodeInitInfo Info):
96110
SDKNodeDecl(Info, SDKNodeKind::DeclTypeAlias) {}
97111

@@ -306,6 +320,7 @@ StringRef SDKNodeType::getTypeRoleDescription() const {
306320
case SDKNodeKind::TypeFunc:
307321
case SDKNodeKind::TypeAlias:
308322
case SDKNodeKind::DeclType:
323+
case SDKNodeKind::DeclOperator:
309324
llvm_unreachable("Type Parent is wrong");
310325
case SDKNodeKind::DeclFunction:
311326
case SDKNodeKind::DeclConstructor:
@@ -719,6 +734,7 @@ bool static isSDKNodeEqual(SDKContext &Ctx, const SDKNode &L, const SDKNode &R)
719734
}
720735
LLVM_FALLTHROUGH;
721736
}
737+
case SDKNodeKind::DeclOperator:
722738
case SDKNodeKind::DeclTypeAlias: {
723739
auto Left = L.getAs<SDKNodeDecl>();
724740
auto Right = R.getAs<SDKNodeDecl>();
@@ -820,14 +836,14 @@ static StringRef calculateUsr(SDKContext &Ctx, ValueDecl *VD) {
820836
return StringRef();
821837
}
822838

823-
static StringRef calculateLocation(SDKContext &SDKCtx, ValueDecl *VD) {
839+
static StringRef calculateLocation(SDKContext &SDKCtx, Decl *D) {
824840
if (SDKCtx.getOpts().AvoidLocation)
825841
return StringRef();
826-
auto &Ctx = VD->getASTContext();
842+
auto &Ctx = D->getASTContext();
827843
auto &Importer = static_cast<ClangImporter &>(*Ctx.getClangModuleLoader());
828844

829845
clang::SourceManager &SM = Importer.getClangPreprocessor().getSourceManager();
830-
if (ClangNode CN = VD->getClangNode()) {
846+
if (ClangNode CN = D->getClangNode()) {
831847
clang::SourceLocation Loc = CN.getLocation();
832848
Loc = SM.getFileLoc(Loc);
833849
if (Loc.isValid())
@@ -926,10 +942,10 @@ Requirement getCanonicalRequirement(Requirement &Req) {
926942
}
927943
}
928944

929-
static StringRef printGenericSignature(SDKContext &Ctx, ValueDecl *VD) {
945+
static StringRef printGenericSignature(SDKContext &Ctx, Decl *D) {
930946
llvm::SmallString<32> Result;
931947
llvm::raw_svector_ostream OS(Result);
932-
if (auto *PD = dyn_cast<ProtocolDecl>(VD)) {
948+
if (auto *PD = dyn_cast<ProtocolDecl>(D)) {
933949
if (PD->getRequirementSignature().empty())
934950
return StringRef();
935951
OS << "<";
@@ -949,7 +965,7 @@ static StringRef printGenericSignature(SDKContext &Ctx, ValueDecl *VD) {
949965
return Ctx.buffer(OS.str());
950966
}
951967

952-
if (auto *GC = VD->getAsGenericContext()) {
968+
if (auto *GC = D->getAsGenericContext()) {
953969
if (auto *Sig = GC->getGenericSignature()) {
954970
if (Ctx.checkingABI())
955971
Sig->getCanonicalSignature()->print(OS);
@@ -1008,23 +1024,40 @@ SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, Type Ty,
10081024
}
10091025
}
10101026

1027+
SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, Decl *D):
1028+
Ctx(Ctx), DKind(D->getKind()),
1029+
Location(calculateLocation(Ctx, D)),
1030+
ModuleName(D->getModuleContext()->getName().str()),
1031+
GenericSig(printGenericSignature(Ctx, D)),
1032+
IsImplicit(D->isImplicit()),
1033+
IsDeprecated(D->getAttrs().getDeprecated(D->getASTContext())) {
1034+
// Capture all attributes.
1035+
auto AllAttrs = D->getAttrs();
1036+
std::transform(AllAttrs.begin(), AllAttrs.end(), std::back_inserter(DeclAttrs),
1037+
[](DeclAttribute *attr) { return attr->getKind(); });
1038+
}
1039+
1040+
SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, OperatorDecl *OD):
1041+
SDKNodeInitInfo(Ctx, cast<Decl>(OD)) {
1042+
Name = OD->getName().str();
1043+
PrintedName = OD->getName().str();
1044+
}
1045+
10111046
SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, ValueDecl *VD)
1012-
: Ctx(Ctx), DKind(VD->getKind()),
1013-
Name(VD->hasName() ? getEscapedName(VD->getBaseName()) : Ctx.buffer("_")),
1014-
PrintedName(getPrintedName(Ctx, VD)),
1015-
Usr(calculateUsr(Ctx, VD)), Location(calculateLocation(Ctx, VD)),
1016-
ModuleName(VD->getModuleContext()->getName().str()),
1017-
GenericSig(printGenericSignature(Ctx, VD)),
1018-
IsImplicit(VD->isImplicit()),
1019-
IsThrowing(isFuncThrowing(VD)), IsMutating(isFuncMutating(VD)),
1020-
IsStatic(VD->isStatic()),
1021-
IsDeprecated(VD->getAttrs().getDeprecated(VD->getASTContext())),
1022-
IsOverriding(VD->getOverriddenDecl()),
1023-
IsProtocolReq(isa<ProtocolDecl>(VD->getDeclContext()) && VD->isProtocolRequirement()),
1024-
IsOpen(VD->getFormalAccess() == AccessLevel::Open),
1025-
IsInternal(VD->getFormalAccess() < AccessLevel::Public),
1026-
SelfIndex(getSelfIndex(VD)), FixedBinaryOrder(getFixedBinaryOrder(VD)),
1027-
ReferenceOwnership(getReferenceOwnership(VD)) {
1047+
: SDKNodeInitInfo(Ctx, cast<Decl>(VD)) {
1048+
Name = VD->hasName() ? getEscapedName(VD->getBaseName()) : Ctx.buffer("_");
1049+
PrintedName = getPrintedName(Ctx, VD);
1050+
Usr = calculateUsr(Ctx, VD);
1051+
IsThrowing = isFuncThrowing(VD);
1052+
IsMutating = isFuncMutating(VD);
1053+
IsStatic = VD->isStatic();
1054+
IsOverriding = VD->getOverriddenDecl();
1055+
IsProtocolReq = isa<ProtocolDecl>(VD->getDeclContext()) && VD->isProtocolRequirement();
1056+
IsOpen = VD->getFormalAccess() == AccessLevel::Open;
1057+
IsInternal = VD->getFormalAccess() < AccessLevel::Public;
1058+
SelfIndex = getSelfIndex(VD);
1059+
FixedBinaryOrder = getFixedBinaryOrder(VD);
1060+
ReferenceOwnership = getReferenceOwnership(VD);
10281061

10291062
// Calculate usr for its super class.
10301063
if (auto *CD = dyn_cast_or_null<ClassDecl>(VD)) {
@@ -1036,11 +1069,6 @@ SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, ValueDecl *VD)
10361069
}
10371070
}
10381071

1039-
// Capture all attributes.
1040-
auto AllAttrs = VD->getAttrs();
1041-
std::transform(AllAttrs.begin(), AllAttrs.end(), std::back_inserter(DeclAttrs),
1042-
[](DeclAttribute *attr) { return attr->getKind(); });
1043-
10441072
// Get all protocol names this type decl conforms to.
10451073
if (auto *NTD = dyn_cast<NominalTypeDecl>(VD)) {
10461074
for (auto *P: NTD->getAllProtocols()) {
@@ -1178,8 +1206,6 @@ SwiftDeclCollector::shouldIgnore(Decl *D, const Decl* Parent) {
11781206
return true;
11791207
if (isa<ConstructorDecl>(D))
11801208
return false;
1181-
if (isa<OperatorDecl>(D))
1182-
return true;
11831209
if (auto VD = dyn_cast<ValueDecl>(D)) {
11841210
if (VD->getBaseName().empty())
11851211
return true;
@@ -1340,6 +1366,8 @@ void SwiftDeclCollector::lookupVisibleDecls(ArrayRef<ModuleDecl *> Modules) {
13401366
KnownDecls.insert(D);
13411367
if (auto VD = dyn_cast<ValueDecl>(D))
13421368
foundDecl(VD, DeclVisibilityKind::DynamicLookup);
1369+
else
1370+
processDecl(D);
13431371
}
13441372
}
13451373

@@ -1353,7 +1381,7 @@ void SwiftDeclCollector::lookupVisibleDecls(ArrayRef<ModuleDecl *> Modules) {
13531381
});
13541382

13551383
for (auto *VD : ClangMacros)
1356-
processDecl(VD);
1384+
processValueDecl(VD);
13571385

13581386
// Collect extensions to types from other modules and synthesize type nodes
13591387
// for them.
@@ -1370,7 +1398,18 @@ void SwiftDeclCollector::lookupVisibleDecls(ArrayRef<ModuleDecl *> Modules) {
13701398
}
13711399
}
13721400

1373-
void SwiftDeclCollector::processDecl(ValueDecl *VD) {
1401+
SDKNode *SwiftDeclCollector::constructOperatorDeclNode(OperatorDecl *OD) {
1402+
return SDKNodeInitInfo(Ctx, OD).createSDKNode(SDKNodeKind::DeclOperator);
1403+
}
1404+
1405+
void SwiftDeclCollector::processDecl(Decl *D) {
1406+
assert(!isa<ValueDecl>(D));
1407+
if (auto *OD = dyn_cast<OperatorDecl>(D)) {
1408+
RootNode->addChild(constructOperatorDeclNode(OD));
1409+
}
1410+
}
1411+
1412+
void SwiftDeclCollector::processValueDecl(ValueDecl *VD) {
13741413
if (auto FD = dyn_cast<FuncDecl>(VD)) {
13751414
RootNode->addChild(constructFunctionNode(FD, SDKNodeKind::DeclFunction));
13761415
} else if (auto NTD = dyn_cast<NominalTypeDecl>(VD)) {
@@ -1391,7 +1430,7 @@ void SwiftDeclCollector::foundDecl(ValueDecl *VD, DeclVisibilityKind Reason) {
13911430
return;
13921431
}
13931432

1394-
processDecl(VD);
1433+
processValueDecl(VD);
13951434
}
13961435

13971436
void SDKNode::output(json::Output &out, KeyKind Key, bool Value) {

tools/swift-api-digester/ModuleAnalyzerNodes.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -339,12 +339,7 @@ class SDKNodeRoot: public SDKNode {
339339
SDKNodeRoot(SDKNodeInitInfo Info);
340340
static SDKNode *getInstance(SDKContext &Ctx);
341341
static bool classof(const SDKNode *N);
342-
void registerDescendant(SDKNode *D) {
343-
if (auto DD = dyn_cast<SDKNodeDecl>(D)) {
344-
assert(!DD->getUsr().empty());
345-
DescendantDeclTable[DD->getUsr()].insert(DD);
346-
}
347-
}
342+
void registerDescendant(SDKNode *D);
348343
ArrayRef<SDKNodeDecl*> getDescendantsByUsr(StringRef Usr) {
349344
return DescendantDeclTable[Usr].getArrayRef();
350345
}
@@ -468,6 +463,12 @@ class SDKNodeDeclType: public SDKNodeDecl {
468463
void diagnose(SDKNode *Right) override;
469464
};
470465

466+
class SDKNodeDeclOperator : public SDKNodeDecl {
467+
public:
468+
SDKNodeDeclOperator(SDKNodeInitInfo Info);
469+
static bool classof(const SDKNode *N);
470+
};
471+
471472
class SDKNodeDeclTypeAlias : public SDKNodeDecl {
472473
public:
473474
SDKNodeDeclTypeAlias(SDKNodeInitInfo Info);
@@ -600,13 +601,15 @@ class SwiftDeclCollector: public VisibleDeclConsumer {
600601
SDKNode *constructTypeDeclNode(NominalTypeDecl *NTD);
601602
SDKNode *constructInitNode(ConstructorDecl *CD);
602603
SDKNode *constructFunctionNode(FuncDecl* FD, SDKNodeKind Kind);
604+
SDKNode *constructOperatorDeclNode(OperatorDecl *OD);
603605
std::vector<SDKNode*> createParameterNodes(ParameterList *PL);
604606
SDKNode *constructTypeNode(Type T, bool IsImplicitlyUnwrappedOptional = false,
605607
bool hasDefaultArgument = false);
608+
void processValueDecl(ValueDecl *VD);
609+
void foundDecl(ValueDecl *VD, DeclVisibilityKind Reason) override;
610+
void processDecl(Decl *D);
606611
public:
607612
void lookupVisibleDecls(ArrayRef<ModuleDecl *> Modules);
608-
void processDecl(ValueDecl *VD);
609-
void foundDecl(ValueDecl *VD, DeclVisibilityKind Reason) override;
610613
};
611614

612615
int dumpSwiftModules(const CompilerInvocation &InitInvok,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,7 @@ class PrunePass : public MatchedNodeListener, public SDKTreeDiffPass {
10001000
break;
10011001
}
10021002

1003+
case SDKNodeKind::DeclOperator:
10031004
case SDKNodeKind::DeclSubscript:
10041005
case SDKNodeKind::DeclAssociatedType:
10051006
case SDKNodeKind::DeclFunction:

0 commit comments

Comments
 (0)