-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathASTScope.cpp
275 lines (228 loc) · 8.53 KB
/
ASTScope.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//===--- ASTScopeImpl.cpp - Swift Object-Oriented AST Scope ---------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
///
/// This file implements the common functions of the 49 ontology.
///
//===----------------------------------------------------------------------===//
#include "swift/AST/ASTScope.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/ASTWalker.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Expr.h"
#include "swift/AST/Initializer.h"
#include "swift/AST/LazyResolver.h"
#include "swift/AST/Module.h"
#include "swift/AST/NameLookup.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/Pattern.h"
#include "swift/AST/Stmt.h"
#include "swift/AST/TypeRepr.h"
#include "swift/Basic/NullablePtr.h"
#include "swift/Basic/STLExtras.h"
#include "llvm/Support/Compiler.h"
#include <algorithm>
using namespace swift;
using namespace ast_scope;
#pragma mark ASTScope
llvm::SmallVector<const ASTScopeImpl *, 0> ASTScope::unqualifiedLookup(
SourceFile *SF, DeclName name, SourceLoc loc,
const DeclContext *startingContext,
namelookup::AbstractASTScopeDeclConsumer &consumer) {
return ASTScopeImpl::unqualifiedLookup(SF, name, loc, startingContext,
consumer);
}
Optional<bool> ASTScope::computeIsCascadingUse(
ArrayRef<const ast_scope::ASTScopeImpl *> history,
Optional<bool> initialIsCascadingUse) {
return ASTScopeImpl::computeIsCascadingUse(history, initialIsCascadingUse);
}
void ASTScope::dump() const { impl->dump(); }
void ASTScope::print(llvm::raw_ostream &out) const { impl->print(out); }
void ASTScope::dumpOneScopeMapLocation(std::pair<unsigned, unsigned> lineCol) {
impl->dumpOneScopeMapLocation(lineCol);
}
#pragma mark ASTScopeImpl
const PatternBindingEntry &AbstractPatternEntryScope::getPatternEntry() const {
return decl->getPatternList()[patternEntryIndex];
}
Pattern *AbstractPatternEntryScope::getPattern() const {
return getPatternEntry().getPattern();
}
NullablePtr<ClosureExpr> BraceStmtScope::parentClosureIfAny() const {
return !getParent() ? nullptr : getParent().get()->getClosureIfClosureScope();
}
NullablePtr<ClosureExpr> ASTScopeImpl::getClosureIfClosureScope() const {
return nullptr;
}
NullablePtr<ClosureExpr>
AbstractClosureScope::getClosureIfClosureScope() const {
return closureExpr;
}
// Conservative, because using precise info would be circular
SourceRange
AttachedPropertyWrapperScope::getSourceRangeFor(const VarDecl *const vd) {
SourceRange sr;
for (auto *attr : vd->getAttrs().getAttributes<CustomAttr>()) {
if (sr.isInvalid())
sr = attr->getTypeLoc().getSourceRange();
else
sr.widen(attr->getTypeLoc().getSourceRange());
}
return sr;
}
SourceManager &ASTScopeImpl::getSourceManager() const {
return getASTContext().SourceMgr;
}
Stmt *LabeledConditionalStmtScope::getStmt() const {
return getLabeledConditionalStmt();
}
bool AbstractFunctionBodyScope::isAMethod(
const AbstractFunctionDecl *const afd) {
// What makes this interesting is that a method named "init" which is not
// in a nominal type or extension decl body still gets an implicit self
// parameter (even though the program is illegal).
// So when choosing between creating a MethodBodyScope and a
// PureFunctionBodyScope do we go by the enclosing Decl (i.e.
// "afd->getDeclContext()->isTypeContext()") or by
// "bool(afd->getImplicitSelfDecl())"?
//
// Since the code uses \c getImplicitSelfDecl, use that.
return afd->getImplicitSelfDecl();
}
#pragma mark getLabeledConditionalStmt
LabeledConditionalStmt *IfStmtScope::getLabeledConditionalStmt() const {
return stmt;
}
LabeledConditionalStmt *WhileStmtScope::getLabeledConditionalStmt() const {
return stmt;
}
LabeledConditionalStmt *GuardStmtScope::getLabeledConditionalStmt() const {
return stmt;
}
#pragma mark getASTContext
ASTContext &ASTScopeImpl::getASTContext() const {
if (auto d = getDeclIfAny())
return d.get()->getASTContext();
if (auto dc = getDeclContext())
return dc.get()->getASTContext();
return getParent().get()->getASTContext();
}
#pragma mark getDeclContext
NullablePtr<DeclContext> ASTScopeImpl::getDeclContext() const {
return nullptr;
}
NullablePtr<DeclContext> ASTSourceFileScope::getDeclContext() const {
return NullablePtr<DeclContext>(SF);
}
NullablePtr<DeclContext> GenericTypeOrExtensionScope::getDeclContext() const {
return getGenericContext();
}
NullablePtr<DeclContext> GenericParamScope::getDeclContext() const {
return dyn_cast<DeclContext>(holder);
}
NullablePtr<DeclContext> PatternEntryInitializerScope::getDeclContext() const {
return getPatternEntry().getInitContext();
}
NullablePtr<DeclContext> BraceStmtScope::getDeclContext() const {
return getParent().get()->getDeclContext();
}
NullablePtr<DeclContext>
DefaultArgumentInitializerScope::getDeclContext() const {
auto *dc = decl->getDefaultArgumentInitContext();
assert(dc && "If scope exists, this must exist");
return dc;
}
// When asked for a loc in an intializer in a capture list, the asked-for
// context is the closure.
NullablePtr<DeclContext> CaptureListScope::getDeclContext() const {
return expr->getClosureBody();
}
NullablePtr<DeclContext> AttachedPropertyWrapperScope::getDeclContext() const {
return decl->getParentPatternBinding()
->getPatternList()
.front()
.getInitContext();
}
NullablePtr<DeclContext> AbstractFunctionDeclScope::getDeclContext() const {
return decl;
}
NullablePtr<DeclContext> ParameterListScope::getDeclContext() const {
return matchingContext;
}
#pragma mark getClassName
std::string GenericTypeOrExtensionScope::getClassName() const {
return declKindName() + portionName() + "Scope";
}
#define DEFINE_GET_CLASS_NAME(Name) \
std::string Name::getClassName() const { return #Name; }
DEFINE_GET_CLASS_NAME(ASTSourceFileScope)
DEFINE_GET_CLASS_NAME(GenericParamScope)
DEFINE_GET_CLASS_NAME(AbstractFunctionDeclScope)
DEFINE_GET_CLASS_NAME(ParameterListScope)
DEFINE_GET_CLASS_NAME(MethodBodyScope)
DEFINE_GET_CLASS_NAME(PureFunctionBodyScope)
DEFINE_GET_CLASS_NAME(DefaultArgumentInitializerScope)
DEFINE_GET_CLASS_NAME(AttachedPropertyWrapperScope)
DEFINE_GET_CLASS_NAME(PatternEntryDeclScope)
DEFINE_GET_CLASS_NAME(PatternEntryInitializerScope)
DEFINE_GET_CLASS_NAME(ConditionalClauseScope)
DEFINE_GET_CLASS_NAME(ConditionalClausePatternUseScope)
DEFINE_GET_CLASS_NAME(CaptureListScope)
DEFINE_GET_CLASS_NAME(WholeClosureScope)
DEFINE_GET_CLASS_NAME(ClosureParametersScope)
DEFINE_GET_CLASS_NAME(ClosureBodyScope)
DEFINE_GET_CLASS_NAME(TopLevelCodeScope)
DEFINE_GET_CLASS_NAME(SpecializeAttributeScope)
DEFINE_GET_CLASS_NAME(SubscriptDeclScope)
DEFINE_GET_CLASS_NAME(VarDeclScope)
DEFINE_GET_CLASS_NAME(EnumElementScope)
DEFINE_GET_CLASS_NAME(IfStmtScope)
DEFINE_GET_CLASS_NAME(WhileStmtScope)
DEFINE_GET_CLASS_NAME(GuardStmtScope)
DEFINE_GET_CLASS_NAME(LookupParentDiversionScope)
DEFINE_GET_CLASS_NAME(RepeatWhileScope)
DEFINE_GET_CLASS_NAME(DoCatchStmtScope)
DEFINE_GET_CLASS_NAME(SwitchStmtScope)
DEFINE_GET_CLASS_NAME(ForEachStmtScope)
DEFINE_GET_CLASS_NAME(ForEachPatternScope)
DEFINE_GET_CLASS_NAME(CatchStmtScope)
DEFINE_GET_CLASS_NAME(CaseStmtScope)
DEFINE_GET_CLASS_NAME(BraceStmtScope)
#undef DEFINE_GET_CLASS_NAME
#pragma mark getSourceFile
const SourceFile *ASTScopeImpl::getSourceFile() const {
return getParent().get()->getSourceFile();
}
const SourceFile *ASTSourceFileScope::getSourceFile() const { return SF; }
SourceRange ExtensionScope::getBraces() const { return decl->getBraces(); }
SourceRange NominalTypeScope::getBraces() const { return decl->getBraces(); }
NullablePtr<NominalTypeDecl>
ExtensionScope::getCorrespondingNominalTypeDecl() const {
return decl->getExtendedNominal();
}
void ASTScopeImpl::preOrderDo(function_ref<void(ASTScopeImpl *)> fn) {
fn(this);
for (auto *child : getChildren())
child->preOrderDo(fn);
}
void ASTScopeImpl::postOrderDo(function_ref<void(ASTScopeImpl *)> fn) {
for (auto *child : getChildren())
child->postOrderDo(fn);
fn(this);
}
ArrayRef<StmtConditionElement> ConditionalClauseScope::getCond() const {
return stmt->getCond();
}
const StmtConditionElement &
ConditionalClauseScope::getStmtConditionElement() const {
return getCond()[index];
}