-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathSILLocation.cpp
350 lines (304 loc) · 11.4 KB
/
SILLocation.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
//===--- SILLocation.cpp - Location information for SIL nodes -------------===//
//
// 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 "swift/SIL/SILLocation.h"
#include "swift/SIL/SILModule.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Expr.h"
#include "swift/AST/Pattern.h"
#include "swift/AST/Stmt.h"
#include "swift/AST/Module.h"
#include "swift/Basic/Assertions.h"
#include "swift/Basic/SourceManager.h"
#include "llvm/Support/raw_ostream.h"
using namespace swift;
static_assert(sizeof(SILLocation) <= 2 * sizeof(void *),
"SILLocation must stay small");
SILLocation::SILLocation(Stmt *S) : SILLocation(ASTNodeTy(S), RegularKind) {
if (S->isImplicit())
kindAndFlags.fields.implicit = true;
}
SILLocation::SILLocation(Expr *E) : SILLocation(ASTNodeTy(E), RegularKind) {
if (E->isImplicit())
kindAndFlags.fields.implicit = true;
}
SILLocation::SILLocation(Decl *D) : SILLocation(ASTNodeTy(D), RegularKind) {
if (D && D->isImplicit())
kindAndFlags.fields.implicit = true;
}
SILLocation::SILLocation(Pattern *P) : SILLocation(ASTNodeTy(P), RegularKind) {
if (P->isImplicit())
kindAndFlags.fields.implicit = true;
}
SILLocation::SILLocation(SourceLoc L, LocationKind K, bool Implicit)
: storage(L), kindAndFlags(K, SourceLocKind) {
kindAndFlags.fields.implicit = Implicit;
}
SILLocation::FilenameAndLocation *
SILLocation::FilenameAndLocation::alloc(unsigned line, unsigned column,
StringRef filename, SILModule &module) {
return new (module) FilenameAndLocation(line, column, filename);
}
void SILLocation::FilenameAndLocation::dump() const { print(llvm::dbgs()); }
void SILLocation::FilenameAndLocation::print(raw_ostream &OS) const {
OS << filename << ':' << line << ':' << column;
}
void SILLocation::pointToEnd() {
switch (getStorageKind()) {
case ASTNodeKind:
return storage.ASTNodeLoc.setInt(1);
case ExtendedASTNodeKind:
return storage.extendedASTNodeLoc->primary.setInt(1);
default:
assert(false && "only AST nodes can be pointed to end");
}
}
bool SILLocation::pointsToEnd() const {
switch (getStorageKind()) {
case ASTNodeKind:
return storage.ASTNodeLoc.getInt();
case ExtendedASTNodeKind:
return storage.extendedASTNodeLoc->primary.getInt();
default:
return false;
}
}
SourceLoc SILLocation::getSourceLoc() const {
if (isSILFile())
return storage.sourceLoc;
// Don't crash if the location is a FilenameAndLocation.
// TODO: this is a workaround until rdar://problem/25225083 is implemented.
if (getStorageKind() == FilenameAndLocationKind)
return SourceLoc();
return getSourceLoc(getPrimaryASTNode());
}
SourceLoc SILLocation::getSourceLoc(ASTNodeTy N) const {
auto P = N.getPointer();
if (P.isNull())
return SourceLoc();
if (pointsToEnd() || is<CleanupLocation>() || is<ImplicitReturnLocation>())
return getEndSourceLoc(N);
// Use the start location for the ReturnKind.
if (is<ReturnLocation>())
return getStartSourceLoc(N);
if (auto *decl = P.dyn_cast<Decl*>())
return decl->getLoc();
if (auto *expr = P.dyn_cast<Expr*>())
return expr->getLoc();
if (auto *stmt = P.dyn_cast<Stmt*>())
return stmt->getStartLoc();
if (auto *patt = P.dyn_cast<Pattern*>())
return patt->getStartLoc();
llvm_unreachable("impossible SILLocation");
}
SourceLoc SILLocation::getSourceLocForDebugging() const {
if (hasASTNodeForDebugging())
return getSourceLoc(storage.extendedASTNodeLoc->forDebugging);
if (isNull())
return SourceLoc();
if (isSILFile())
return storage.sourceLoc;
return getSourceLoc(getPrimaryASTNode());
}
SourceLoc SILLocation::getStartSourceLoc() const {
if (isAutoGenerated())
return SourceLoc();
if (isSILFile())
return storage.sourceLoc;
if (getStorageKind() == FilenameAndLocationKind)
return SourceLoc();
return getStartSourceLoc(getPrimaryASTNode());
}
SourceLoc SILLocation::getStartSourceLoc(ASTNodeTy N) {
auto P = N.getPointer();
if (auto *decl = P.dyn_cast<Decl*>())
return decl->getStartLoc();
if (auto *expr = P.dyn_cast<Expr*>())
return expr->getStartLoc();
if (auto *stmt = P.dyn_cast<Stmt*>())
return stmt->getStartLoc();
if (auto *patt = P.dyn_cast<Pattern*>())
return patt->getStartLoc();
llvm_unreachable("impossible SILLocation");
}
SourceLoc SILLocation::getEndSourceLoc() const {
if (isAutoGenerated())
return SourceLoc();
if (isSILFile())
return storage.sourceLoc;
if (getStorageKind() == FilenameAndLocationKind)
return SourceLoc();
return getEndSourceLoc(getPrimaryASTNode());
}
SourceLoc SILLocation::getEndSourceLoc(ASTNodeTy N) {
auto P = N.getPointer();
if (auto decl = P.dyn_cast<Decl*>())
return decl->getEndLoc();
if (auto expr = P.dyn_cast<Expr*>())
return expr->getEndLoc();
if (auto stmt = P.dyn_cast<Stmt*>())
return stmt->getEndLoc();
if (auto patt = P.dyn_cast<Pattern*>())
return patt->getEndLoc();
llvm_unreachable("impossible SILLocation");
}
DeclContext *SILLocation::getAsDeclContext() const {
if (!isASTNode())
return nullptr;
if (auto *D = getAsASTNode<Decl>())
return D->getInnermostDeclContext();
if (auto *E = getAsASTNode<Expr>())
if (auto *DC = dyn_cast<AbstractClosureExpr>(E))
return DC;
return nullptr;
}
SILLocation::FilenameAndLocation SILLocation::decode(SourceLoc Loc,
const SourceManager &SM,
bool ForceGeneratedSourceToDisk) {
FilenameAndLocation DL;
if (Loc.isValid()) {
DL.filename = SM.getDisplayNameForLoc(Loc, ForceGeneratedSourceToDisk);
std::tie(DL.line, DL.column) = SM.getPresumedLineAndColumnForLoc(Loc);
}
return DL;
}
SILLocation::FilenameAndLocation *SILLocation::getCompilerGeneratedLoc() {
static FilenameAndLocation compilerGenerated({0, 0, "<compiler-generated>"});
return &compilerGenerated;
}
static void printSourceLoc(SourceLoc loc, raw_ostream &OS) {
if (!loc.isValid()) {
OS << "<invalid loc>";
return;
}
const char *srcPtr = (const char *)loc.getOpaquePointerValue();
unsigned len = strnlen(srcPtr, 20);
if (len < 20) {
OS << '"' << StringRef(srcPtr, len) << '"';
} else {
OS << '"' << StringRef(srcPtr, 20) << "[...]\"";
}
}
void SILLocation::dump() const {
if (isNull()) {
llvm::dbgs() << "<no loc>" << "\n";
return;
}
if (auto D = getAsASTNode<Decl>())
llvm::dbgs() << Decl::getKindName(D->getKind()) << "Decl @ ";
if (auto E = getAsASTNode<Expr>())
llvm::dbgs() << Expr::getKindName(E->getKind()) << "Expr @ ";
if (auto S = getAsASTNode<Stmt>())
llvm::dbgs() << Stmt::getKindName(S->getKind()) << "Stmt @ ";
if (auto P = getAsASTNode<Pattern>())
llvm::dbgs() << Pattern::getKindName(P->getKind()) << "Pattern @ ";
if (isFilenameAndLocation()) {
getFilenameAndLocation()->dump();
} else {
printSourceLoc(getSourceLoc(), llvm::dbgs());
}
if (isAutoGenerated()) llvm::dbgs() << ":auto";
if (isImplicit()) llvm::dbgs() << ":implicit";
if (pointsToEnd()) llvm::dbgs() << ":end";
if (isInPrologue()) llvm::dbgs() << ":prologue";
if (isSILFile()) llvm::dbgs() << ":sil";
if (hasASTNodeForDebugging()) {
llvm::dbgs() << ":debug[";
printSourceLoc(getSourceLocForDebugging(), llvm::dbgs());
llvm::dbgs() << "]";
}
llvm::dbgs() << "\n";
}
void SILLocation::print(raw_ostream &OS, const SourceManager &SM) const {
if (isNull()) {
OS << "<no loc>";
} else if (isFilenameAndLocation()) {
getFilenameAndLocation()->print(OS);
} else {
getSourceLoc().print(OS, SM);
}
}
void SILLocation::print(raw_ostream &OS) const {
if (isNull()) {
OS << "<no loc>";
} else if (isFilenameAndLocation()) {
getFilenameAndLocation()->print(OS);
} else if (DeclContext *dc = getAsDeclContext()){
getSourceLoc().print(OS, dc->getASTContext().SourceMgr);
} else {
printSourceLoc(getSourceLoc(), OS);
}
}
RegularLocation::RegularLocation(Decl *D, SILLocation LocForDebugging,
SILModule &Module)
: SILLocation(new(Module) ExtendedASTNodeLoc(
{D, 0}, LocForDebugging.getPrimaryASTNode()),
RegularKind) {}
RegularLocation::RegularLocation(Stmt *S, Pattern *P, SILModule &Module)
: SILLocation(new(Module) ExtendedASTNodeLoc({S, 0}, {P, 0}), RegularKind) {}
SILLocation::ExtendedASTNodeLoc *
RegularLocation::getDebugOnlyExtendedASTNodeLoc(SILLocation L,
SILModule &Module) {
ASTNodeTy Empty({(Decl *)nullptr, 0});
if (auto D = L.getAsASTNode<Decl>())
return new (Module) ExtendedASTNodeLoc(Empty, {D, 0});
if (auto E = L.getAsASTNode<Expr>())
return new (Module) ExtendedASTNodeLoc(Empty, {E, 0});
if (auto S = L.getAsASTNode<Stmt>()) {
// If the source location of the SILLocation passed in matches the EndLoc of
// the Stmt, set the primary ASTNodeTy integer to 1, so that
// SILLocation::getSourceLoc returns the EndLoc when queried.
if (L.getSourceLocForDebugging() == S->getEndLoc())
Empty.setInt(1);
return new (Module) ExtendedASTNodeLoc(Empty, {S, 0});
}
auto P = L.getAsASTNode<Pattern>();
return new (Module) ExtendedASTNodeLoc(Empty, {P, 0});
}
SILLocation::ExtendedASTNodeLoc *
RegularLocation::getDiagnosticOnlyExtendedASTNodeLoc(SILLocation L,
SILModule &Module) {
ASTNodeTy Empty({(Decl *)nullptr, 0});
if (auto D = L.getAsASTNode<Decl>())
return new (Module) ExtendedASTNodeLoc({D, 0}, Empty);
if (auto E = L.getAsASTNode<Expr>())
return new (Module) ExtendedASTNodeLoc({E, 0}, Empty);
if (auto S = L.getAsASTNode<Stmt>())
return new (Module) ExtendedASTNodeLoc({S, 0}, Empty);
auto P = L.getAsASTNode<Pattern>();
return new (Module) ExtendedASTNodeLoc({P, 0}, Empty);
}
RegularLocation::RegularLocation(SILLocation ForDebuggingOrDiagnosticsOnly,
SILModule &Module, bool isForDebugOnly)
: SILLocation(isForDebugOnly ? getDebugOnlyExtendedASTNodeLoc(
ForDebuggingOrDiagnosticsOnly, Module)
: getDiagnosticOnlyExtendedASTNodeLoc(
ForDebuggingOrDiagnosticsOnly, Module),
RegularKind) {
markAutoGenerated();
}
ReturnLocation::ReturnLocation(ReturnStmt *RS) :
SILLocation(ASTNodeTy(RS), ReturnKind) {}
ReturnLocation::ReturnLocation(BraceStmt *BS) :
SILLocation(ASTNodeTy(BS), ReturnKind) {}
ImplicitReturnLocation::ImplicitReturnLocation(AbstractClosureExpr *E)
: SILLocation(ASTNodeTy(E), ImplicitReturnKind) { }
ImplicitReturnLocation::ImplicitReturnLocation(ReturnStmt *S)
: SILLocation(ASTNodeTy(S), ImplicitReturnKind) { }
ImplicitReturnLocation::ImplicitReturnLocation(AbstractFunctionDecl *AFD)
: SILLocation(ASTNodeTy(AFD), ImplicitReturnKind) { }
ImplicitReturnLocation::ImplicitReturnLocation(SILLocation L)
: SILLocation(L, ImplicitReturnKind) {
assert(L.isASTNode<Expr>() ||
L.isASTNode<ValueDecl>() ||
L.isASTNode<PatternBindingDecl>() ||
L.isNull());
}