-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathSymbolGraphASTWalker.cpp
190 lines (164 loc) · 6.15 KB
/
SymbolGraphASTWalker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//===--- SymbolGraphASTWalker.cpp - Symbol Graph AST Walker ---------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/StringSwitch.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Module.h"
#include "swift/Serialization/SerializedModuleLoader.h"
#include "swift/SymbolGraphGen/SymbolGraphGen.h"
#include "SymbolGraphASTWalker.h"
using namespace swift;
using namespace symbolgraphgen;
SymbolGraphASTWalker::SymbolGraphASTWalker(ModuleDecl &M,
const SymbolGraphOptions &Options)
: Options(Options),
M(M),
MainGraph(*this, M, None, Ctx) {}
/// Get a "sub" symbol graph for the parent module of a type that
/// the main module `M` is extending.
SymbolGraph *SymbolGraphASTWalker::getModuleSymbolGraph(ModuleDecl *M) {
if (this->M.getNameStr().equals(M->getNameStr())) {
return &MainGraph;
}
auto Found = ExtendedModuleGraphs.find(M);
if (Found != ExtendedModuleGraphs.end()) {
return Found->getSecond();
}
auto *Memory = Ctx.allocate(sizeof(SymbolGraph), alignof(SymbolGraph));
auto *SG = new (Memory) SymbolGraph(*this,
MainGraph.M,
Optional<ModuleDecl *>(M),
Ctx);
ExtendedModuleGraphs.insert({M, SG});
return SG;
}
namespace {
bool isUnavailableOrObsoleted(const Decl *D) {
if (const auto *Avail =
D->getAttrs().getUnavailable(D->getASTContext())) {
switch (Avail->getVersionAvailability(D->getASTContext())) {
case AvailableVersionComparison::Unavailable:
case AvailableVersionComparison::Obsoleted:
return true;
default:
break;
}
}
return false;
}
} // end anonymous namespace
bool SymbolGraphASTWalker::walkToDeclPre(Decl *D, CharSourceRange Range) {
if (isUnavailableOrObsoleted(D)) {
return false;
}
switch (D->getKind()) {
// We'll record nodes for the following kinds of declarations.
case swift::DeclKind::Class:
case swift::DeclKind::Struct:
case swift::DeclKind::Enum:
case swift::DeclKind::EnumElement:
case swift::DeclKind::Protocol:
case swift::DeclKind::Constructor:
case swift::DeclKind::Func:
case swift::DeclKind::Var:
case swift::DeclKind::Subscript:
case swift::DeclKind::TypeAlias:
case swift::DeclKind::AssociatedType:
case swift::DeclKind::Extension:
break;
// We'll descend into everything else.
default:
return true;
}
auto SG = getModuleSymbolGraph(D->getModuleContext());
// If this is an extension, let's check that it implies some new conformances,
// potentially with generic requirements.
if (const auto *Extension = dyn_cast<ExtensionDecl>(D)) {
const auto *ExtendedNominal = Extension->getExtendedNominal();
// Ignore effecively private decls.
if (ExtendedNominal->hasUnderscoredNaming()) {
return false;
}
if (isUnavailableOrObsoleted(ExtendedNominal)) {
return false;
}
// If there are some protocol conformances on this extension, we'll
// grab them for some new conformsTo relationships.
if (!Extension->getInherited().empty()) {
auto ExtendedSG =
getModuleSymbolGraph(ExtendedNominal->getModuleContext());
// The symbol graph to use to record these relationships.
SmallVector<const ProtocolDecl *, 4> Protocols;
SmallVector<const ProtocolCompositionType *, 4> UnexpandedCompositions;
auto HandleProtocolOrComposition = [&](Type Ty) {
if (const auto *Proto =
dyn_cast_or_null<ProtocolDecl>(Ty->getAnyNominal())) {
Protocols.push_back(Proto);
} else if (const auto *Comp = Ty->getAs<ProtocolCompositionType>()) {
UnexpandedCompositions.push_back(Comp);
} else {
abort();
}
};
for (const auto InheritedLoc : Extension->getInherited()) {
auto InheritedTy = InheritedLoc.getType();
if (!InheritedTy) {
continue;
}
HandleProtocolOrComposition(InheritedTy);
}
while (!UnexpandedCompositions.empty()) {
const auto *Comp = UnexpandedCompositions.pop_back_val();
for (const auto Member : Comp->getMembers()) {
HandleProtocolOrComposition(Member);
}
}
Symbol Source(ExtendedSG, ExtendedNominal, nullptr);
for (const auto *Proto : Protocols) {
Symbol Target(&MainGraph, Proto, nullptr);
ExtendedSG->recordEdge(Source, Target, RelationshipKind::ConformsTo(),
Extension);
}
// While we won't record this node per se, or all of the other kinds of
// relationships, we might establish some synthesized members because we
// extended an external type.
if (ExtendedNominal->getModuleContext() != &M) {
ExtendedSG->recordConformanceSynthesizedMemberRelationships({
ExtendedSG,
ExtendedNominal,
nullptr
});
}
}
// Continue looking into the extension.
return true;
}
auto *VD = cast<ValueDecl>(D);
if (!SG->canIncludeDeclAsNode(VD)) {
return false;
}
// If this symbol extends a type from another module, record it in that
// module's symbol graph, which will be emitted separately.
if (const auto *Extension
= dyn_cast_or_null<ExtensionDecl>(VD->getDeclContext())) {
if (const auto *ExtendedNominal = Extension->getExtendedNominal()) {
auto ExtendedModule = ExtendedNominal->getModuleContext();
auto ExtendedSG = getModuleSymbolGraph(ExtendedModule);
if (ExtendedModule != &M) {
ExtendedSG->recordNode(Symbol(ExtendedSG, VD, nullptr));
return true;
}
}
}
// Otherwise, record this in the main module `M`'s symbol graph.
SG->recordNode(Symbol(SG, VD, nullptr));
return true;
}