Skip to content

Commit cab6232

Browse files
committed
Swift SIL: improvements for Location
* make `var sourceLoc` return nil if the location doesn't have a valid line number * add `var decl`
1 parent 5a67af5 commit cab6232

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

SwiftCompilerSources/Sources/SIL/Location.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import SILBridging
14+
import AST
1415

1516
public struct Location: Equatable, CustomStringConvertible {
1617
let bridged: BridgedLocation
@@ -20,7 +21,10 @@ public struct Location: Equatable, CustomStringConvertible {
2021
}
2122

2223
public var sourceLoc: SourceLoc? {
23-
return SourceLoc(bridged: bridged.getSourceLocation())
24+
if hasValidLineNumber {
25+
return SourceLoc(bridged: bridged.getSourceLocation())
26+
}
27+
return nil
2428
}
2529

2630
/// Keeps the debug scope but marks it as auto-generated.
@@ -34,6 +38,9 @@ public struct Location: Equatable, CustomStringConvertible {
3438

3539
public var isDebugSteppable: Bool { hasValidLineNumber && !isAutoGenerated }
3640

41+
/// The `Decl` if the location refers to a declaration.
42+
public var decl: Decl? { bridged.getDecl().decl }
43+
3744
public static func ==(lhs: Location, rhs: Location) -> Bool {
3845
lhs.bridged.isEqualTo(rhs.bridged)
3946
}

include/swift/SIL/SILBridging.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ struct BridgedLocation {
403403
BRIDGED_INLINE bool isEqualTo(BridgedLocation rhs) const;
404404
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedSourceLoc getSourceLocation() const;
405405
BRIDGED_INLINE bool hasSameSourceLocation(BridgedLocation rhs) const;
406+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedDeclObj getDecl() const;
406407
static BRIDGED_INLINE BridgedLocation fromNominalTypeDecl(BridgedDeclObj decl);
407408
static BRIDGED_INLINE BridgedLocation getArtificialUnreachableLocation();
408409
};

include/swift/SIL/SILBridgingImpl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,9 @@ BridgedSourceLoc BridgedLocation::getSourceLocation() const {
636636
bool BridgedLocation::hasSameSourceLocation(BridgedLocation rhs) const {
637637
return getLoc().hasSameSourceLocation(rhs.getLoc());
638638
}
639+
OptionalBridgedDeclObj BridgedLocation::getDecl() const {
640+
return {getLoc().getLocation().getAsASTNode<swift::Decl>()};
641+
}
639642
BridgedLocation BridgedLocation::fromNominalTypeDecl(BridgedDeclObj decl) {
640643
return swift::SILDebugLocation(decl.unbridged(), nullptr);
641644
}

0 commit comments

Comments
 (0)