forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSILLocation.cpp
229 lines (195 loc) · 7.29 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
//===--- SILLocation.cpp - Location information for SIL nodes -------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/SIL/SILLocation.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Expr.h"
#include "swift/AST/Pattern.h"
#include "swift/AST/Stmt.h"
#include "swift/Basic/SourceManager.h"
#include "llvm/Support/raw_ostream.h"
using namespace swift;
SourceLoc SILLocation::getSourceLoc() const {
if (isSILFile())
return Loc.SILFileLoc;
// Don't crash if the location is a DebugLoc.
// TODO: this is a workaround until rdar://problem/25225083 is implemented.
if (isDebugInfoLoc())
return SourceLoc();
return getSourceLoc(Loc.ASTNode.Primary);
}
SourceLoc SILLocation::getSourceLoc(ASTNodeTy N) const {
if (N.isNull())
return SourceLoc();
if (alwaysPointsToStart() ||
alwaysPointsToEnd() ||
is<CleanupLocation>() ||
is<ImplicitReturnLocation>())
return getEndSourceLoc(N);
// Use the start location for the ReturnKind.
if (is<ReturnLocation>())
return getStartSourceLoc(N);
if (auto *decl = N.dyn_cast<Decl*>())
return decl->getLoc();
if (auto *expr = N.dyn_cast<Expr*>())
return expr->getLoc();
if (auto *stmt = N.dyn_cast<Stmt*>())
return stmt->getStartLoc();
if (auto *patt = N.dyn_cast<Pattern*>())
return patt->getStartLoc();
llvm_unreachable("impossible SILLocation");
}
SourceLoc SILLocation::getDebugSourceLoc() const {
assert(!isDebugInfoLoc());
if (isSILFile())
return Loc.SILFileLoc;
if (isAutoGenerated())
return SourceLoc();
if (auto *expr = Loc.ASTNode.Primary.dyn_cast<Expr*>()) {
if (isa<CallExpr>(expr))
return expr->getEndLoc();
// Code that has an autoclosure as location should not show up in
// the line table (rdar://problem/14627460). Note also that the
// closure function still has a valid DW_AT_decl_line. Depending
// on how we decide to resolve rdar://problem/14627460, we may
// want to use the regular getLoc instead and rather use the
// column info.
if (isa<AutoClosureExpr>(expr))
return SourceLoc();
}
if (Loc.ASTNode.ForDebugger)
return getSourceLoc(Loc.ASTNode.ForDebugger);
return getSourceLoc(Loc.ASTNode.Primary);
}
SourceLoc SILLocation::getStartSourceLoc() const {
if (isAutoGenerated())
return SourceLoc();
if (isSILFile())
return Loc.SILFileLoc;
return getStartSourceLoc(Loc.ASTNode.Primary);
}
SourceLoc SILLocation::getStartSourceLoc(ASTNodeTy N) const {
if (auto *decl = N.dyn_cast<Decl*>())
return decl->getStartLoc();
if (auto *expr = N.dyn_cast<Expr*>())
return expr->getStartLoc();
if (auto *stmt = N.dyn_cast<Stmt*>())
return stmt->getStartLoc();
if (auto *patt = N.dyn_cast<Pattern*>())
return patt->getStartLoc();
llvm_unreachable("impossible SILLocation");
}
SourceLoc SILLocation::getEndSourceLoc() const {
if (isAutoGenerated())
return SourceLoc();
if (isSILFile())
return Loc.SILFileLoc;
return getEndSourceLoc(Loc.ASTNode.Primary);
}
SourceLoc SILLocation::getEndSourceLoc(ASTNodeTy N) const {
if (auto decl = N.dyn_cast<Decl*>())
return decl->getEndLoc();
if (auto expr = N.dyn_cast<Expr*>())
return expr->getEndLoc();
if (auto stmt = N.dyn_cast<Stmt*>())
return stmt->getEndLoc();
if (auto patt = N.dyn_cast<Pattern*>())
return patt->getEndLoc();
llvm_unreachable("impossible SILLocation");
}
SILLocation::DebugLoc SILLocation::decode(SourceLoc Loc,
const SourceManager &SM) {
DebugLoc DL;
if (Loc.isValid()) {
DL.Filename = SM.getBufferIdentifierForLoc(Loc);
std::tie(DL.Line, DL.Column) = SM.getLineAndColumn(Loc);
}
return DL;
}
void SILLocation::dump(const SourceManager &SM) const {
if (auto D = getAsASTNode<Decl>())
llvm::errs() << Decl::getKindName(D->getKind()) << "Decl @ ";
if (auto E = getAsASTNode<Expr>())
llvm::errs() << Expr::getKindName(E->getKind()) << "Expr @ ";
if (auto S = getAsASTNode<Stmt>())
llvm::errs() << Stmt::getKindName(S->getKind()) << "Stmt @ ";
if (auto P = getAsASTNode<Pattern>())
llvm::errs() << Pattern::getKindName(P->getKind()) << "Pattern @ ";
print(llvm::errs(), SM);
if (isAutoGenerated()) llvm::errs() << ":auto";
if (alwaysPointsToStart()) llvm::errs() << ":start";
if (alwaysPointsToEnd()) llvm::errs() << ":end";
if (isInTopLevel()) llvm::errs() << ":toplevel";
if (isInPrologue()) llvm::errs() << ":prologue";
if (isSILFile()) llvm::errs() << ":sil";
if (hasDebugLoc()) {
llvm::errs() << ":debug[";
getDebugSourceLoc().print(llvm::errs(), SM);
llvm::errs() << "]\n";
}
}
void SILLocation::print(raw_ostream &OS, const SourceManager &SM) const {
if (isNull())
OS << "<no loc>";
getSourceLoc().print(OS, SM);
}
InlinedLocation InlinedLocation::getInlinedLocation(SILLocation L) {
if (Expr *E = L.getAsASTNode<Expr>())
return InlinedLocation(E, L.getSpecialFlags());
if (Stmt *S = L.getAsASTNode<Stmt>())
return InlinedLocation(S, L.getSpecialFlags());
if (Pattern *P = L.getAsASTNode<Pattern>())
return InlinedLocation(P, L.getSpecialFlags());
if (Decl *D = L.getAsASTNode<Decl>())
return InlinedLocation(D, L.getSpecialFlags());
if (L.isSILFile())
return InlinedLocation(L.Loc.SILFileLoc, L.getSpecialFlags());
if (L.isInTopLevel())
return InlinedLocation::getModuleLocation(L.getSpecialFlags());
if (L.isAutoGenerated()) {
InlinedLocation IL;
IL.markAutoGenerated();
return IL;
}
llvm_unreachable("Cannot construct Inlined loc from the given location.");
}
MandatoryInlinedLocation
MandatoryInlinedLocation::getMandatoryInlinedLocation(SILLocation L) {
if (Expr *E = L.getAsASTNode<Expr>())
return MandatoryInlinedLocation(E, L.getSpecialFlags());
if (Stmt *S = L.getAsASTNode<Stmt>())
return MandatoryInlinedLocation(S, L.getSpecialFlags());
if (Pattern *P = L.getAsASTNode<Pattern>())
return MandatoryInlinedLocation(P, L.getSpecialFlags());
if (Decl *D = L.getAsASTNode<Decl>())
return MandatoryInlinedLocation(D, L.getSpecialFlags());
if (L.isSILFile())
return MandatoryInlinedLocation(L.Loc.SILFileLoc, L.getSpecialFlags());
if (L.isInTopLevel())
return MandatoryInlinedLocation::getModuleLocation(L.getSpecialFlags());
llvm_unreachable("Cannot construct Inlined loc from the given location.");
}
CleanupLocation CleanupLocation::get(SILLocation L) {
if (Expr *E = L.getAsASTNode<Expr>())
return CleanupLocation(E, L.getSpecialFlags());
if (Stmt *S = L.getAsASTNode<Stmt>())
return CleanupLocation(S, L.getSpecialFlags());
if (Pattern *P = L.getAsASTNode<Pattern>())
return CleanupLocation(P, L.getSpecialFlags());
if (Decl *D = L.getAsASTNode<Decl>())
return CleanupLocation(D, L.getSpecialFlags());
if (L.isNull())
return CleanupLocation();
if (L.isSILFile())
return CleanupLocation();
llvm_unreachable("Cannot construct Cleanup loc from the "
"given location.");
}