Merge pull request #64590 from adrian-prantl/implicit-sillocation

Preserve the Implicit attribute in SILLocation. (NFC)
This commit is contained in:
Adrian Prantl
2023-03-30 13:46:51 -07:00
committed by GitHub
3 changed files with 108 additions and 59 deletions

View File

@@ -25,8 +25,33 @@ using namespace swift;
static_assert(sizeof(SILLocation) <= 2 * sizeof(void *),
"SILLocation must stay small");
SILLocation::FilenameAndLocation *SILLocation::FilenameAndLocation::
alloc(unsigned line, unsigned column, StringRef filename, SILModule &module) {
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)
: storage(L), kindAndFlags(K, SourceLocKind) {
kindAndFlags.fields.implicit = true;
}
SILLocation::FilenameAndLocation *
SILLocation::FilenameAndLocation::alloc(unsigned line, unsigned column,
StringRef filename, SILModule &module) {
return new (module) FilenameAndLocation(line, column, filename);
}
@@ -36,6 +61,28 @@ 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;
@@ -49,25 +96,24 @@ SourceLoc SILLocation::getSourceLoc() const {
}
SourceLoc SILLocation::getSourceLoc(ASTNodeTy N) const {
if (N.isNull())
auto P = N.getPointer();
if (P.isNull())
return SourceLoc();
if (alwaysPointsToEnd() ||
is<CleanupLocation>() ||
is<ImplicitReturnLocation>())
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 = N.dyn_cast<Decl*>())
if (auto *decl = P.dyn_cast<Decl*>())
return decl->getLoc();
if (auto *expr = N.dyn_cast<Expr*>())
if (auto *expr = P.dyn_cast<Expr*>())
return expr->getLoc();
if (auto *stmt = N.dyn_cast<Stmt*>())
if (auto *stmt = P.dyn_cast<Stmt*>())
return stmt->getStartLoc();
if (auto *patt = N.dyn_cast<Pattern*>())
if (auto *patt = P.dyn_cast<Pattern*>())
return patt->getStartLoc();
llvm_unreachable("impossible SILLocation");
}
@@ -94,13 +140,14 @@ SourceLoc SILLocation::getStartSourceLoc() const {
}
SourceLoc SILLocation::getStartSourceLoc(ASTNodeTy N) {
if (auto *decl = N.dyn_cast<Decl*>())
auto P = N.getPointer();
if (auto *decl = P.dyn_cast<Decl*>())
return decl->getStartLoc();
if (auto *expr = N.dyn_cast<Expr*>())
if (auto *expr = P.dyn_cast<Expr*>())
return expr->getStartLoc();
if (auto *stmt = N.dyn_cast<Stmt*>())
if (auto *stmt = P.dyn_cast<Stmt*>())
return stmt->getStartLoc();
if (auto *patt = N.dyn_cast<Pattern*>())
if (auto *patt = P.dyn_cast<Pattern*>())
return patt->getStartLoc();
llvm_unreachable("impossible SILLocation");
}
@@ -114,13 +161,14 @@ SourceLoc SILLocation::getEndSourceLoc() const {
}
SourceLoc SILLocation::getEndSourceLoc(ASTNodeTy N) {
if (auto decl = N.dyn_cast<Decl*>())
auto P = N.getPointer();
if (auto decl = P.dyn_cast<Decl*>())
return decl->getEndLoc();
if (auto expr = N.dyn_cast<Expr*>())
if (auto expr = P.dyn_cast<Expr*>())
return expr->getEndLoc();
if (auto stmt = N.dyn_cast<Stmt*>())
if (auto stmt = P.dyn_cast<Stmt*>())
return stmt->getEndLoc();
if (auto patt = N.dyn_cast<Pattern*>())
if (auto patt = P.dyn_cast<Pattern*>())
return patt->getEndLoc();
llvm_unreachable("impossible SILLocation");
}
@@ -186,10 +234,11 @@ void SILLocation::dump() const {
printSourceLoc(getSourceLoc(), llvm::dbgs());
}
if (isAutoGenerated()) llvm::dbgs() << ":auto";
if (alwaysPointsToEnd()) llvm::dbgs() << ":end";
if (isInPrologue()) llvm::dbgs() << ":prologue";
if (isSILFile()) llvm::dbgs() << ":sil";
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());
@@ -219,33 +268,35 @@ void SILLocation::print(raw_ostream &OS) const {
}
}
RegularLocation::RegularLocation(Stmt *S, Pattern *P, SILModule &Module) :
SILLocation(new (Module) ExtendedASTNodeLoc(S, P), 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((Decl *)nullptr, D);
return new (Module) ExtendedASTNodeLoc(Empty, {D, 0});
if (auto E = L.getAsASTNode<Expr>())
return new (Module) ExtendedASTNodeLoc((Decl *)nullptr, E);
return new (Module) ExtendedASTNodeLoc(Empty, {E, 0});
if (auto S = L.getAsASTNode<Stmt>())
return new (Module) ExtendedASTNodeLoc((Decl *)nullptr, S);
return new (Module) ExtendedASTNodeLoc(Empty, {S, 0});
auto P = L.getAsASTNode<Pattern>();
return new (Module) ExtendedASTNodeLoc((Decl *)nullptr, P);
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, (Decl *)nullptr);
return new (Module) ExtendedASTNodeLoc({D, 0}, Empty);
if (auto E = L.getAsASTNode<Expr>())
return new (Module) ExtendedASTNodeLoc(E, (Decl *)nullptr);
return new (Module) ExtendedASTNodeLoc({E, 0}, Empty);
if (auto S = L.getAsASTNode<Stmt>())
return new (Module) ExtendedASTNodeLoc(S, (Decl *)nullptr);
return new (Module) ExtendedASTNodeLoc({S, 0}, Empty);
auto P = L.getAsASTNode<Pattern>();
return new (Module) ExtendedASTNodeLoc(P, (Decl *)nullptr);
return new (Module) ExtendedASTNodeLoc({P, 0}, Empty);
}
RegularLocation::RegularLocation(SILLocation ForDebuggingOrDiagnosticsOnly,