Skip to content

Commit 65d69fe

Browse files
committed
SIL/AST: add some APIs
* `GenericSignature.isEmpty` * `Builder.emitDestroy` * `Function.abi` * `KeyPathInst.substitutionMap` * `KeyPathInst.hasPattern`
1 parent 1a4bd76 commit 65d69fe

File tree

7 files changed

+67
-0
lines changed

7 files changed

+67
-0
lines changed

SwiftCompilerSources/Sources/AST/GenericSignature.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,6 @@ public struct GenericSignature: CustomStringConvertible, NoReflectionChildren {
3333
public func mapTypeIntoContext(_ type: Type) -> Type {
3434
Type(bridged: bridged.mapTypeIntoContext(type.bridged))
3535
}
36+
37+
public var isEmpty: Bool { bridged.impl == nil }
3638
}

SwiftCompilerSources/Sources/SIL/Builder.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,3 +756,23 @@ public struct Builder {
756756
return notifyNew(convertFunction.getAs(ConvertEscapeToNoEscapeInst.self))
757757
}
758758
}
759+
760+
761+
//===----------------------------------------------------------------------===//
762+
// Utilities
763+
//===----------------------------------------------------------------------===//
764+
765+
extension Builder {
766+
public func emitDestroy(of value: Value) {
767+
if value.type.isTrivial(in: value.parentFunction) {
768+
return
769+
}
770+
if value.parentFunction.hasOwnership {
771+
createDestroyValue(operand: value)
772+
} else if value.type.isClass {
773+
createStrongRelease(operand: value)
774+
} else {
775+
createReleaseValue(operand: value)
776+
}
777+
}
778+
}

SwiftCompilerSources/Sources/SIL/Function.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,20 @@ final public class Function : CustomStringConvertible, HasShortDescription, Hash
327327
}
328328
}
329329

330+
public enum ABILanguage {
331+
case C
332+
case Swift
333+
}
334+
335+
public var abi: ABILanguage {
336+
switch bridged.getSILFunctionLanguage() {
337+
case .C: return .C
338+
case .Swift: return .Swift
339+
default:
340+
fatalError()
341+
}
342+
}
343+
330344
public enum SourceFileKind {
331345
case library /// A normal .swift file.
332346
case main /// A .swift file that can have top-level code.

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,14 @@ final public class RefTailAddrInst : SingleValueInstruction, UnaryInstruction {
11111111
}
11121112

11131113
final public class KeyPathInst : SingleValueInstruction {
1114+
public var substitutionMap: SubstitutionMap {
1115+
SubstitutionMap(bridged: bridged.KeyPathInst_getSubstitutionMap())
1116+
}
1117+
1118+
public var hasPattern: Bool {
1119+
bridged.KeyPathInst_hasPattern()
1120+
}
1121+
11141122
public override func visitReferencedFunctions(_ cl: (Function) -> ()) {
11151123
var results = BridgedInstruction.KeyPathFunctionResults()
11161124
for componentIdx in 0..<bridged.KeyPathInst_getNumComponents() {

include/swift/SIL/SILBridging.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,10 @@ struct BridgedFunction {
469469
AlwaysInline
470470
};
471471

472+
enum class ABILanguage {
473+
Swift, C
474+
};
475+
472476
enum class ThunkKind {
473477
IsNotThunk,
474478
IsThunk,
@@ -530,6 +534,7 @@ struct BridgedFunction {
530534
BRIDGED_INLINE EffectsKind getEffectAttribute() const;
531535
BRIDGED_INLINE PerformanceConstraints getPerformanceConstraints() const;
532536
BRIDGED_INLINE InlineStrategy getInlineStrategy() const;
537+
BRIDGED_INLINE ABILanguage getSILFunctionLanguage() const;
533538
BRIDGED_INLINE SerializedKind getSerializedKind() const;
534539
BRIDGED_INLINE bool canBeInlinedIntoCaller(SerializedKind) const;
535540
BRIDGED_INLINE bool hasValidLinkageForFragileRef(SerializedKind) const;
@@ -861,6 +866,8 @@ struct BridgedInstruction {
861866
BRIDGED_INLINE bool AllocRefInst_isBare() const;
862867
BRIDGED_INLINE void AllocRefInst_setIsBare() const;
863868
BRIDGED_INLINE void TermInst_replaceBranchTarget(BridgedBasicBlock from, BridgedBasicBlock to) const;
869+
BRIDGED_INLINE BridgedSubstitutionMap KeyPathInst_getSubstitutionMap() const;
870+
BRIDGED_INLINE bool KeyPathInst_hasPattern() const;
864871
BRIDGED_INLINE SwiftInt KeyPathInst_getNumComponents() const;
865872
BRIDGED_INLINE void KeyPathInst_getReferencedFunctions(SwiftInt componentIdx, KeyPathFunctionResults * _Nonnull results) const;
866873
BRIDGED_INLINE void GlobalAddrInst_clearToken() const;

include/swift/SIL/SILBridgingImpl.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,11 @@ BridgedFunction::InlineStrategy BridgedFunction::getInlineStrategy() const {
823823
return (InlineStrategy)getFunction()->getInlineStrategy();
824824
}
825825

826+
BridgedFunction::ABILanguage BridgedFunction::getSILFunctionLanguage() const {
827+
auto rep = getFunction()->getLoweredFunctionType()->getRepresentation();
828+
return (ABILanguage)swift::getSILFunctionLanguage(rep);
829+
}
830+
826831
BridgedFunction::ThunkKind BridgedFunction::isThunk() const {
827832
return (ThunkKind)getFunction()->isThunk();
828833
}
@@ -1578,6 +1583,14 @@ void BridgedInstruction::TermInst_replaceBranchTarget(BridgedBasicBlock from, Br
15781583
to.unbridged());
15791584
}
15801585

1586+
BridgedSubstitutionMap BridgedInstruction::KeyPathInst_getSubstitutionMap() const {
1587+
return getAs<swift::KeyPathInst>()->getSubstitutions();
1588+
}
1589+
1590+
bool BridgedInstruction::KeyPathInst_hasPattern() const {
1591+
return getAs<swift::KeyPathInst>()->hasPattern();
1592+
}
1593+
15811594
SwiftInt BridgedInstruction::KeyPathInst_getNumComponents() const {
15821595
if (swift::KeyPathPattern *pattern = getAs<swift::KeyPathInst>()->getPattern()) {
15831596
return (SwiftInt)pattern->getComponents().size();

lib/SIL/Utils/SILBridging.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@ static_assert((int)BridgedFunction::InlineStrategy::InlineDefault == (int)swift:
190190
static_assert((int)BridgedFunction::InlineStrategy::NoInline == (int)swift::NoInline);
191191
static_assert((int)BridgedFunction::InlineStrategy::AlwaysInline == (int)swift::AlwaysInline);
192192

193+
static_assert((int)BridgedFunction::ABILanguage::Swift == (int)swift::SILFunctionLanguage::Swift);
194+
static_assert((int)BridgedFunction::ABILanguage::C == (int)swift::SILFunctionLanguage::C);
195+
193196
static_assert((int)BridgedFunction::ThunkKind::IsNotThunk == (int)swift::IsNotThunk);
194197
static_assert((int)BridgedFunction::ThunkKind::IsThunk == (int)swift::IsThunk);
195198
static_assert((int)BridgedFunction::ThunkKind::IsReabstractionThunk == (int)swift::IsReabstractionThunk);

0 commit comments

Comments
 (0)