Skip to content

Commit 7a11d5a

Browse files
committed
SwiftCompilerSources: bridge Function.sourceFileKind.
1 parent 19350d9 commit 7a11d5a

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

SwiftCompilerSources/Sources/SIL/Function.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,27 @@ final public class Function : CustomStringConvertible, HasShortDescription, Hash
256256
fatalError()
257257
}
258258
}
259+
260+
public enum SourceFileKind {
261+
case library /// A normal .swift file.
262+
case main /// A .swift file that can have top-level code.
263+
case sil /// Came from a .sil file.
264+
case interface /// Came from a .swiftinterface file, representing another module.
265+
case macroExpansion /// Came from a macro expansion.
266+
case defaultArgument /// Came from default argument at caller side
267+
};
268+
269+
public var sourceFileKind: SourceFileKind? {
270+
switch bridged.getSourceFileKind() {
271+
case .Library: return .library
272+
case .Main: return .main
273+
case .SIL: return .sil
274+
case .Interface: return .interface
275+
case .MacroExpansion: return .macroExpansion
276+
case .DefaultArgument: return .defaultArgument
277+
case .None: return nil
278+
}
279+
}
259280
}
260281

261282
public func == (lhs: Function, rhs: Function) -> Bool { lhs === rhs }

include/swift/SIL/SILBridging.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,16 @@ struct BridgedFunction {
477477
IsSerializedForPackage
478478
};
479479

480+
enum class OptionalSourceFileKind {
481+
Library,
482+
Main,
483+
SIL,
484+
Interface,
485+
MacroExpansion,
486+
DefaultArgument, // must match swift::SourceFileKind::DefaultArgument
487+
None
488+
};
489+
480490
SWIFT_NAME("init(obj:)")
481491
BridgedFunction(SwiftObject obj) : obj(obj) {}
482492
BridgedFunction() {}
@@ -529,6 +539,7 @@ struct BridgedFunction {
529539
bool isAutodiffVJP() const;
530540
SwiftInt specializationLevel() const;
531541
SWIFT_IMPORT_UNSAFE BridgedSubstitutionMap getMethodSubstitutions(BridgedSubstitutionMap contextSubs) const;
542+
BRIDGED_INLINE OptionalSourceFileKind getSourceFileKind() const;
532543

533544
enum class ParseEffectsMode {
534545
argumentEffectsFromSource,

include/swift/SIL/SILBridgingImpl.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "SILBridging.h"
2323
#include "swift/AST/Builtins.h"
2424
#include "swift/AST/Decl.h"
25+
#include "swift/AST/SourceFile.h"
2526
#include "swift/AST/SubstitutionMap.h"
2627
#include "swift/AST/Types.h"
2728
#include "swift/Basic/BasicBridging.h"
@@ -924,6 +925,14 @@ swift::SILFunction * _Nullable OptionalBridgedFunction::getFunction() const {
924925
return static_cast<swift::SILFunction *>(obj);
925926
}
926927

928+
BridgedFunction::OptionalSourceFileKind BridgedFunction::getSourceFileKind() const {
929+
if (auto *dc = getFunction()->getDeclContext()) {
930+
if (auto *sourceFile = dc->getParentSourceFile())
931+
return (OptionalSourceFileKind)sourceFile->Kind;
932+
}
933+
return OptionalSourceFileKind::None;
934+
}
935+
927936
//===----------------------------------------------------------------------===//
928937
// BridgedGlobalVar
929938
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)