Skip to content

Commit 1ce545b

Browse files
committed
SIL: add some DeclRef APIs
* `var decl` * `func ==` * `func calleesAreStaticallyKnowable`
1 parent b39a6cd commit 1ce545b

File tree

6 files changed

+34
-0
lines changed

6 files changed

+34
-0
lines changed

SwiftCompilerSources/Sources/Optimizer/PassManager/Context.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,3 +791,9 @@ extension Function {
791791
return context._bridged.appendBlock(bridged).block
792792
}
793793
}
794+
795+
extension DeclRef {
796+
func calleesAreStaticallyKnowable(_ context: some Context) -> Bool {
797+
context._bridged.calleesAreStaticallyKnowable(bridged)
798+
}
799+
}

SwiftCompilerSources/Sources/SIL/DeclRef.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,23 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import SILBridging
14+
import AST
1415

16+
/// A key for referencing an AST declaration in SIL.
17+
///
18+
/// In addition to the AST reference, there are discriminators for referencing different
19+
/// implementation-level entities associated with a single language-level declaration,
20+
/// such as the allocating and initializing entry points of a constructor, etc.
1521
public struct DeclRef: CustomStringConvertible, NoReflectionChildren {
1622
public let bridged: BridgedDeclRef
1723

1824
public var location: Location { Location(bridged: bridged.getLocation()) }
1925

2026
public var description: String { String(taking: bridged.getDebugDescription()) }
27+
28+
public var decl: Decl { bridged.getDecl().decl }
29+
30+
public static func ==(lhs: DeclRef, rhs: DeclRef) -> Bool {
31+
lhs.bridged.isEqualTo(rhs.bridged)
32+
}
2133
}

include/swift/SIL/SILBridging.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,8 +927,10 @@ struct BridgedDeclRef {
927927
BRIDGED_INLINE BridgedDeclRef(swift::SILDeclRef declRef);
928928
BRIDGED_INLINE swift::SILDeclRef unbridged() const;
929929

930+
BRIDGED_INLINE bool isEqualTo(BridgedDeclRef rhs) const;
930931
BridgedOwnedString getDebugDescription() const;
931932
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedLocation getLocation() const;
933+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedDeclObj getDecl() const;
932934
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedDiagnosticArgument asDiagnosticArgument() const;
933935
};
934936

include/swift/SIL/SILBridgingImpl.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,10 +1726,18 @@ swift::SILDeclRef BridgedDeclRef::unbridged() const {
17261726
return *reinterpret_cast<const swift::SILDeclRef *>(&storage);
17271727
}
17281728

1729+
bool BridgedDeclRef::isEqualTo(BridgedDeclRef rhs) const {
1730+
return unbridged() == rhs.unbridged();
1731+
}
1732+
17291733
BridgedLocation BridgedDeclRef::getLocation() const {
17301734
return swift::SILDebugLocation(unbridged().getDecl(), nullptr);
17311735
}
17321736

1737+
BridgedDeclObj BridgedDeclRef::getDecl() const {
1738+
return {unbridged().getDecl()};
1739+
}
1740+
17331741
BridgedDiagnosticArgument BridgedDeclRef::asDiagnosticArgument() const {
17341742
return swift::DiagnosticArgument(unbridged().getDecl()->getName());
17351743
}

include/swift/SILOptimizer/OptimizerBridging.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ struct BridgedPassContext {
339339
SWIFT_IMPORT_UNSAFE OptionalBridgedFunction lookUpNominalDeinitFunction(BridgedDeclObj nominal) const;
340340
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedSubstitutionMap getContextSubstitutionMap(BridgedType type) const;
341341
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedType getBuiltinIntegerType(SwiftInt bitWidth) const;
342+
BRIDGED_INLINE bool calleesAreStaticallyKnowable(BridgedDeclRef method) const;
342343
SWIFT_IMPORT_UNSAFE BridgedFunction createEmptyFunction(BridgedStringRef name,
343344
const BridgedParameterInfo * _Nullable bridgedParams,
344345
SwiftInt paramCount,

include/swift/SILOptimizer/OptimizerBridgingImpl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,11 @@ BridgedType BridgedPassContext::getBuiltinIntegerType(SwiftInt bitWidth) const {
488488
return swift::SILType::getBuiltinIntegerType(bitWidth, ctxt);
489489
}
490490

491+
bool BridgedPassContext::calleesAreStaticallyKnowable(BridgedDeclRef method) const {
492+
swift::SILModule *mod = invocation->getPassManager()->getModule();
493+
return swift::calleesAreStaticallyKnowable(*mod, method.unbridged());
494+
}
495+
491496
void BridgedPassContext::beginTransformFunction(BridgedFunction function) const {
492497
invocation->beginTransformFunction(function.getFunction());
493498
}

0 commit comments

Comments
 (0)