Skip to content

Commit 63304d0

Browse files
committed
SILLocation: Move pointsToEnd flag into ASTNodeTy. NFC
The pointsToEnd flag is only meaningful when combined with an AST node, and moving it into the AST pointer frees up a bit in the flags bitfield for later use.
1 parent 5129aea commit 63304d0

File tree

2 files changed

+67
-41
lines changed

2 files changed

+67
-41
lines changed

include/swift/SIL/SILLocation.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ class SILLocation {
106106
using type = Pattern;
107107
};
108108

109-
using ASTNodeTy = llvm::PointerUnion<Stmt *, Expr *, Decl *, Pattern *>;
109+
/// The int flag indicates whether the node's end location should be used.
110+
using ASTNodeTy = llvm::PointerIntPair<
111+
llvm::PointerUnion<Stmt *, Expr *, Decl *, Pattern *>, 1>;
110112

111113
/// Used in case the location for diagnostics does not match the location for
112114
/// debugging.
@@ -159,7 +161,7 @@ class SILLocation {
159161

160162
const FilenameAndLocation *filePositionLoc;
161163
ASTNodeTy ASTNodeLoc;
162-
const ExtendedASTNodeLoc *extendedASTNodeLoc;
164+
ExtendedASTNodeLoc *extendedASTNodeLoc;
163165
SourceLoc sourceLoc;
164166
};
165167

@@ -173,7 +175,7 @@ class SILLocation {
173175
union KindAndFlags {
174176
KindAndFlags() : packedKindAndFlags(0) {}
175177
KindAndFlags(LocationKind kind, StorageKind storageKind)
176-
: fields({kind, storageKind, 0, 0, 0 }) {
178+
: fields({kind, storageKind, 0, 0}) {
177179
assert(fields.kind == kind && "LocationKind overflow");
178180
assert(fields.storageKind == storageKind && "StorageKind overflow");
179181
}
@@ -184,7 +186,6 @@ class SILLocation {
184186
uint8_t kind: 3;
185187
uint8_t storageKind: 2;
186188
uint8_t autoGenerated: 1;
187-
uint8_t pointsToEnd: 1;
188189
uint8_t inPrologue: 1;
189190
} fields;
190191
};
@@ -222,21 +223,21 @@ class SILLocation {
222223
template <typename T>
223224
T *getNodeAs(ASTNodeTy Node) const {
224225
using base = typename base_type<T>::type*;
225-
return dyn_cast_or_null<T>(Node.dyn_cast<base>());
226+
return dyn_cast_or_null<T>(Node.getPointer().dyn_cast<base>());
226227
}
227228

228229
template <typename T>
229230
bool isNode(ASTNodeTy Node) const {
230231
assert(isASTNode());
231232
ASTNodeTy primaryNd = getPrimaryASTNode();
232-
if (primaryNd.is<typename base_type<T>::type*>())
233-
return isa<T>(Node.get<typename base_type<T>::type*>());
233+
if (primaryNd.getPointer().is<typename base_type<T>::type*>())
234+
return isa<T>(Node.getPointer().get<typename base_type<T>::type*>());
234235
return false;
235236
}
236237

237238
template <typename T>
238239
T *castNodeTo(ASTNodeTy Node) const {
239-
return cast<T>(Node.get<typename base_type<T>::type*>());
240+
return cast<T>(Node.getPointer().get<typename base_type<T>::type*>());
240241
}
241242

242243
SourceLoc getSourceLoc(ASTNodeTy N) const;
@@ -299,7 +300,7 @@ class SILLocation {
299300
bool isNull() const {
300301
switch (getStorageKind()) {
301302
case ASTNodeKind:
302-
case ExtendedASTNodeKind: return !getPrimaryASTNode();
303+
case ExtendedASTNodeKind: return !getPrimaryASTNode().getPointer();
303304
case FilenameAndLocationKind: return storage.filePositionLoc == nullptr;
304305
case SourceLocKind: return storage.sourceLoc.isInvalid();
305306
}
@@ -365,7 +366,7 @@ class SILLocation {
365366

366367
/// Changes the default source location position to point to the end of
367368
/// the AST node.
368-
void pointToEnd() { kindAndFlags.fields.pointsToEnd = true; }
369+
void pointToEnd();
369370

370371
/// Mark this location as being part of the function
371372
/// prologue, which means that it deals with setting up the stack
@@ -378,7 +379,7 @@ class SILLocation {
378379

379380
/// Check if the corresponding source code location definitely points
380381
/// to the end of the AST node.
381-
bool alwaysPointsToEnd() const { return kindAndFlags.fields.pointsToEnd; }
382+
bool pointsToEnd() const;
382383

383384
template <typename T> bool is() const { return T::isKind(*this); }
384385

lib/SIL/IR/SILLocation.cpp

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,28 @@ void SILLocation::FilenameAndLocation::print(raw_ostream &OS) const {
3636
OS << filename << ':' << line << ':' << column;
3737
}
3838

39+
void SILLocation::pointToEnd() {
40+
switch (getStorageKind()) {
41+
case ASTNodeKind:
42+
return storage.ASTNodeLoc.setInt(1);
43+
case ExtendedASTNodeKind:
44+
return storage.extendedASTNodeLoc->primary.setInt(1);
45+
default:
46+
assert(false && "only AST nodes can be pointed to end");
47+
}
48+
}
49+
50+
bool SILLocation::pointsToEnd() const {
51+
switch (getStorageKind()) {
52+
case ASTNodeKind:
53+
return storage.ASTNodeLoc.getInt();
54+
case ExtendedASTNodeKind:
55+
return storage.extendedASTNodeLoc->primary.getInt();
56+
default:
57+
return false;
58+
}
59+
}
60+
3961
SourceLoc SILLocation::getSourceLoc() const {
4062
if (isSILFile())
4163
return storage.sourceLoc;
@@ -49,25 +71,24 @@ SourceLoc SILLocation::getSourceLoc() const {
4971
}
5072

5173
SourceLoc SILLocation::getSourceLoc(ASTNodeTy N) const {
52-
if (N.isNull())
74+
auto P = N.getPointer();
75+
if (P.isNull())
5376
return SourceLoc();
5477

55-
if (alwaysPointsToEnd() ||
56-
is<CleanupLocation>() ||
57-
is<ImplicitReturnLocation>())
78+
if (pointsToEnd() || is<CleanupLocation>() || is<ImplicitReturnLocation>())
5879
return getEndSourceLoc(N);
5980

6081
// Use the start location for the ReturnKind.
6182
if (is<ReturnLocation>())
6283
return getStartSourceLoc(N);
6384

64-
if (auto *decl = N.dyn_cast<Decl*>())
85+
if (auto *decl = P.dyn_cast<Decl*>())
6586
return decl->getLoc();
66-
if (auto *expr = N.dyn_cast<Expr*>())
87+
if (auto *expr = P.dyn_cast<Expr*>())
6788
return expr->getLoc();
68-
if (auto *stmt = N.dyn_cast<Stmt*>())
89+
if (auto *stmt = P.dyn_cast<Stmt*>())
6990
return stmt->getStartLoc();
70-
if (auto *patt = N.dyn_cast<Pattern*>())
91+
if (auto *patt = P.dyn_cast<Pattern*>())
7192
return patt->getStartLoc();
7293
llvm_unreachable("impossible SILLocation");
7394
}
@@ -94,13 +115,14 @@ SourceLoc SILLocation::getStartSourceLoc() const {
94115
}
95116

96117
SourceLoc SILLocation::getStartSourceLoc(ASTNodeTy N) {
97-
if (auto *decl = N.dyn_cast<Decl*>())
118+
auto P = N.getPointer();
119+
if (auto *decl = P.dyn_cast<Decl*>())
98120
return decl->getStartLoc();
99-
if (auto *expr = N.dyn_cast<Expr*>())
121+
if (auto *expr = P.dyn_cast<Expr*>())
100122
return expr->getStartLoc();
101-
if (auto *stmt = N.dyn_cast<Stmt*>())
123+
if (auto *stmt = P.dyn_cast<Stmt*>())
102124
return stmt->getStartLoc();
103-
if (auto *patt = N.dyn_cast<Pattern*>())
125+
if (auto *patt = P.dyn_cast<Pattern*>())
104126
return patt->getStartLoc();
105127
llvm_unreachable("impossible SILLocation");
106128
}
@@ -114,13 +136,14 @@ SourceLoc SILLocation::getEndSourceLoc() const {
114136
}
115137

116138
SourceLoc SILLocation::getEndSourceLoc(ASTNodeTy N) {
117-
if (auto decl = N.dyn_cast<Decl*>())
139+
auto P = N.getPointer();
140+
if (auto decl = P.dyn_cast<Decl*>())
118141
return decl->getEndLoc();
119-
if (auto expr = N.dyn_cast<Expr*>())
142+
if (auto expr = P.dyn_cast<Expr*>())
120143
return expr->getEndLoc();
121-
if (auto stmt = N.dyn_cast<Stmt*>())
144+
if (auto stmt = P.dyn_cast<Stmt*>())
122145
return stmt->getEndLoc();
123-
if (auto patt = N.dyn_cast<Pattern*>())
146+
if (auto patt = P.dyn_cast<Pattern*>())
124147
return patt->getEndLoc();
125148
llvm_unreachable("impossible SILLocation");
126149
}
@@ -186,10 +209,10 @@ void SILLocation::dump() const {
186209
printSourceLoc(getSourceLoc(), llvm::dbgs());
187210
}
188211

189-
if (isAutoGenerated()) llvm::dbgs() << ":auto";
190-
if (alwaysPointsToEnd()) llvm::dbgs() << ":end";
191-
if (isInPrologue()) llvm::dbgs() << ":prologue";
192-
if (isSILFile()) llvm::dbgs() << ":sil";
212+
if (isAutoGenerated()) llvm::dbgs() << ":auto";
213+
if (pointsToEnd()) llvm::dbgs() << ":end";
214+
if (isInPrologue()) llvm::dbgs() << ":prologue";
215+
if (isSILFile()) llvm::dbgs() << ":sil";
193216
if (hasASTNodeForDebugging()) {
194217
llvm::dbgs() << ":debug[";
195218
printSourceLoc(getSourceLocForDebugging(), llvm::dbgs());
@@ -219,33 +242,35 @@ void SILLocation::print(raw_ostream &OS) const {
219242
}
220243
}
221244

222-
RegularLocation::RegularLocation(Stmt *S, Pattern *P, SILModule &Module) :
223-
SILLocation(new (Module) ExtendedASTNodeLoc(S, P), RegularKind) {}
245+
RegularLocation::RegularLocation(Stmt *S, Pattern *P, SILModule &Module)
246+
: SILLocation(new(Module) ExtendedASTNodeLoc({S, 0}, {P, 0}), RegularKind) {}
224247

225248
SILLocation::ExtendedASTNodeLoc *
226249
RegularLocation::getDebugOnlyExtendedASTNodeLoc(SILLocation L,
227250
SILModule &Module) {
251+
ASTNodeTy Empty({(Decl *)nullptr, 0});
228252
if (auto D = L.getAsASTNode<Decl>())
229-
return new (Module) ExtendedASTNodeLoc((Decl *)nullptr, D);
253+
return new (Module) ExtendedASTNodeLoc(Empty, {D, 0});
230254
if (auto E = L.getAsASTNode<Expr>())
231-
return new (Module) ExtendedASTNodeLoc((Decl *)nullptr, E);
255+
return new (Module) ExtendedASTNodeLoc(Empty, {E, 0});
232256
if (auto S = L.getAsASTNode<Stmt>())
233-
return new (Module) ExtendedASTNodeLoc((Decl *)nullptr, S);
257+
return new (Module) ExtendedASTNodeLoc(Empty, {S, 0});
234258
auto P = L.getAsASTNode<Pattern>();
235-
return new (Module) ExtendedASTNodeLoc((Decl *)nullptr, P);
259+
return new (Module) ExtendedASTNodeLoc(Empty, {P, 0});
236260
}
237261

238262
SILLocation::ExtendedASTNodeLoc *
239263
RegularLocation::getDiagnosticOnlyExtendedASTNodeLoc(SILLocation L,
240264
SILModule &Module) {
265+
ASTNodeTy Empty({(Decl *)nullptr, 0});
241266
if (auto D = L.getAsASTNode<Decl>())
242-
return new (Module) ExtendedASTNodeLoc(D, (Decl *)nullptr);
267+
return new (Module) ExtendedASTNodeLoc({D, 0}, Empty);
243268
if (auto E = L.getAsASTNode<Expr>())
244-
return new (Module) ExtendedASTNodeLoc(E, (Decl *)nullptr);
269+
return new (Module) ExtendedASTNodeLoc({E, 0}, Empty);
245270
if (auto S = L.getAsASTNode<Stmt>())
246-
return new (Module) ExtendedASTNodeLoc(S, (Decl *)nullptr);
271+
return new (Module) ExtendedASTNodeLoc({S, 0}, Empty);
247272
auto P = L.getAsASTNode<Pattern>();
248-
return new (Module) ExtendedASTNodeLoc(P, (Decl *)nullptr);
273+
return new (Module) ExtendedASTNodeLoc({P, 0}, Empty);
249274
}
250275

251276
RegularLocation::RegularLocation(SILLocation ForDebuggingOrDiagnosticsOnly,

0 commit comments

Comments
 (0)