Skip to content

Commit c5efebe

Browse files
committed
Preserve the Implicit attribute in SILLocation. (NFC)
This change aims at reducing the need for SIL passes to check into the AST storage of SILLocations. The end goal is to eventually merge this with the autogenerated flag, but at the moment the emergent semantics of both properties are not identical.
1 parent 63304d0 commit c5efebe

File tree

3 files changed

+42
-19
lines changed

3 files changed

+42
-19
lines changed

include/swift/SIL/SILLocation.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ class SILLocation {
175175
union KindAndFlags {
176176
KindAndFlags() : packedKindAndFlags(0) {}
177177
KindAndFlags(LocationKind kind, StorageKind storageKind)
178-
: fields({kind, storageKind, 0, 0}) {
178+
: fields({kind, storageKind, 0, 0, 0}) {
179179
assert(fields.kind == kind && "LocationKind overflow");
180180
assert(fields.storageKind == storageKind && "StorageKind overflow");
181181
}
@@ -186,6 +186,7 @@ class SILLocation {
186186
uint8_t kind: 3;
187187
uint8_t storageKind: 2;
188188
uint8_t autoGenerated: 1;
189+
uint8_t implicit: 1;
189190
uint8_t inPrologue: 1;
190191
} fields;
191192
};
@@ -270,8 +271,7 @@ class SILLocation {
270271
: storage(ext), kindAndFlags(K, ExtendedASTNodeKind) {
271272
}
272273

273-
SILLocation(SourceLoc L, LocationKind K)
274-
: storage(L), kindAndFlags(K, SourceLocKind) {}
274+
SILLocation(SourceLoc L, LocationKind K);
275275

276276
// Used by SILInstruction.
277277
SILLocation(Storage storage, uint8_t packedKindAndFlags) :
@@ -284,10 +284,10 @@ class SILLocation {
284284
//
285285
// It is okay to pass a nullptr to these constructors, but preferably, a
286286
// null-location should be created with `invalid()`.
287-
SILLocation(Stmt *S) : SILLocation(ASTNodeTy(S), RegularKind) {}
288-
SILLocation(Expr *E) : SILLocation(ASTNodeTy(E), RegularKind) {}
289-
SILLocation(Decl *D) : SILLocation(ASTNodeTy(D), RegularKind) {}
290-
SILLocation(Pattern *P) : SILLocation(ASTNodeTy(P), RegularKind) {}
287+
SILLocation(Stmt *S);
288+
SILLocation(Expr *E);
289+
SILLocation(Decl *D);
290+
SILLocation(Pattern *P);
291291

292292
/// Returns a null location.
293293
static SILLocation invalid() { return SILLocation(); }
@@ -354,6 +354,11 @@ class SILLocation {
354354
/// body, such as thunks or default destructors.
355355
bool isAutoGenerated() const { return kindAndFlags.fields.autoGenerated; }
356356

357+
/// Returns true if the location was created from an implicit AST node.
358+
/// TODO: This is very similar to autogenerated,
359+
/// and these two properties should be merged.
360+
bool isImplicit() const { return kindAndFlags.fields.implicit; }
361+
357362
/// Returns false if the location should be represented in debuginfo.
358363
bool isHiddenFromDebugInfo() const {
359364
return isAutoGenerated() && !hasASTNodeForDebugging();

lib/SIL/IR/SILLocation.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,33 @@ using namespace swift;
2525
static_assert(sizeof(SILLocation) <= 2 * sizeof(void *),
2626
"SILLocation must stay small");
2727

28-
SILLocation::FilenameAndLocation *SILLocation::FilenameAndLocation::
29-
alloc(unsigned line, unsigned column, StringRef filename, SILModule &module) {
28+
SILLocation::SILLocation(Stmt *S) : SILLocation(ASTNodeTy(S), RegularKind) {
29+
if (S->isImplicit())
30+
kindAndFlags.fields.implicit = true;
31+
}
32+
33+
SILLocation::SILLocation(Expr *E) : SILLocation(ASTNodeTy(E), RegularKind) {
34+
if (E->isImplicit())
35+
kindAndFlags.fields.implicit = true;
36+
}
37+
SILLocation::SILLocation(Decl *D) : SILLocation(ASTNodeTy(D), RegularKind) {
38+
if (D && D->isImplicit())
39+
kindAndFlags.fields.implicit = true;
40+
}
41+
42+
SILLocation::SILLocation(Pattern *P) : SILLocation(ASTNodeTy(P), RegularKind) {
43+
if (P->isImplicit())
44+
kindAndFlags.fields.implicit = true;
45+
}
46+
47+
SILLocation::SILLocation(SourceLoc L, LocationKind K)
48+
: storage(L), kindAndFlags(K, SourceLocKind) {
49+
kindAndFlags.fields.implicit = true;
50+
}
51+
52+
SILLocation::FilenameAndLocation *
53+
SILLocation::FilenameAndLocation::alloc(unsigned line, unsigned column,
54+
StringRef filename, SILModule &module) {
3055
return new (module) FilenameAndLocation(line, column, filename);
3156
}
3257

@@ -210,6 +235,7 @@ void SILLocation::dump() const {
210235
}
211236

212237
if (isAutoGenerated()) llvm::dbgs() << ":auto";
238+
if (isImplicit()) llvm::dbgs() << ":implicit";
213239
if (pointsToEnd()) llvm::dbgs() << ":end";
214240
if (isInPrologue()) llvm::dbgs() << ":prologue";
215241
if (isSILFile()) llvm::dbgs() << ":sil";

lib/SILOptimizer/Mandatory/DiagnoseUnreachable.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -647,16 +647,8 @@ static bool isUserCode(const SILInstruction *I) {
647647

648648
// If the instruction corresponds to user-written return or some other
649649
// statement, we know it corresponds to user code.
650-
if (Loc.is<RegularLocation>() || Loc.is<ReturnLocation>()) {
651-
if (auto *E = Loc.getAsASTNode<Expr>())
652-
return !E->isImplicit();
653-
if (auto *D = Loc.getAsASTNode<Decl>())
654-
return !D->isImplicit();
655-
if (auto *S = Loc.getAsASTNode<Stmt>())
656-
return !S->isImplicit();
657-
if (auto *P = Loc.getAsASTNode<Pattern>())
658-
return !P->isImplicit();
659-
}
650+
if (Loc.is<RegularLocation>() || Loc.is<ReturnLocation>())
651+
return !Loc.isImplicit();
660652
return false;
661653
}
662654

0 commit comments

Comments
 (0)