Skip to content

Commit e857b46

Browse files
committed
[sil] Add a SILDeclRef field to SILFunction, have SILGen set it, and teach SILFunction how to use the field to get the SourceFile associated with the SILFunction.
(cherry picked from commit 025902f)
1 parent 60b453c commit e857b46

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

include/swift/SIL/SILFunction.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ class SILFunction
269269
/// @_dynamicReplacement(for:) function.
270270
SILFunction *ReplacedFunction = nullptr;
271271

272+
/// The SILDeclRef that this function was created for by SILGen if one exists.
273+
SILDeclRef DeclRef = SILDeclRef();
274+
272275
/// This SILFunction REFerences an ad-hoc protocol requirement witness in
273276
/// order to keep it alive, such that it main be obtained in IRGen. Without
274277
/// this explicit reference, the witness would seem not-used, and not be
@@ -1114,6 +1117,12 @@ class SILFunction
11141117
/// Get the source location of the function.
11151118
const SILDebugScope *getDebugScope() const { return DebugScope; }
11161119

1120+
/// Return the SILDeclRef for this SILFunction if one was assigned by SILGen.
1121+
SILDeclRef getDeclRef() const { return DeclRef; }
1122+
1123+
/// Set the SILDeclRef for this SILFunction. Used mainly by SILGen.
1124+
void setDeclRef(SILDeclRef declRef) { DeclRef = declRef; }
1125+
11171126
/// Get this function's bare attribute.
11181127
IsBare_t isBare() const { return IsBare_t(Bare); }
11191128
void setBare(IsBare_t isB) { Bare = isB; }
@@ -1376,6 +1385,9 @@ class SILFunction
13761385

13771386
ActorIsolation getActorIsolation() const { return actorIsolation; }
13781387

1388+
/// Return the source file that this SILFunction belongs to if it exists.
1389+
SourceFile *getSourceFile() const;
1390+
13791391
//===--------------------------------------------------------------------===//
13801392
// Block List Access
13811393
//===--------------------------------------------------------------------===//

lib/SIL/IR/SILFunction.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,3 +1189,11 @@ bool SILFunction::argumentMayRead(Operand *argOp, SILValue addr) {
11891189

11901190
return argumentMayReadFunction({this}, {argOp}, {addr});
11911191
}
1192+
1193+
SourceFile *SILFunction::getSourceFile() const {
1194+
auto declRef = getDeclRef();
1195+
if (!declRef)
1196+
return nullptr;
1197+
1198+
return declRef.getInnermostDeclContext()->getParentSourceFile();
1199+
}

lib/SILGen/SILGen.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,9 @@ void SILGenModule::preEmitFunction(SILDeclRef constant, SILFunction *F,
12361236
// Create a debug scope for the function using astNode as source location.
12371237
F->setDebugScope(new (M) SILDebugScope(Loc, F));
12381238

1239+
// Initialize F with the constant we created for it.
1240+
F->setDeclRef(constant);
1241+
12391242
LLVM_DEBUG(llvm::dbgs() << "lowering ";
12401243
F->printName(llvm::dbgs());
12411244
llvm::dbgs() << " : ";

0 commit comments

Comments
 (0)