Skip to content

Commit ee1c52b

Browse files
committed
Swift SIL: add some APIs
1 parent b707b5a commit ee1c52b

File tree

9 files changed

+103
-2
lines changed

9 files changed

+103
-2
lines changed

SwiftCompilerSources/Sources/Basic/Utils.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,21 @@ public struct StringRef : CustomStringConvertible, NoReflectionChildren {
6464

6565
public var string: String { _bridged.string }
6666
public var description: String { string }
67-
67+
68+
public var count: Int {
69+
Int(_bridged.__bytes_endUnsafe() - _bridged.__bytes_beginUnsafe())
70+
}
71+
72+
public subscript(index: Int) -> UInt8 {
73+
let buffer = UnsafeBufferPointer<UInt8>(start: _bridged.__bytes_beginUnsafe(),
74+
count: count)
75+
return buffer[index]
76+
}
77+
6878
public static func ==(lhs: StringRef, rhs: StaticString) -> Bool {
6979
let lhsBuffer = UnsafeBufferPointer<UInt8>(
7080
start: lhs._bridged.__bytes_beginUnsafe(),
71-
count: Int(lhs._bridged.__bytes_endUnsafe() - lhs._bridged.__bytes_beginUnsafe()))
81+
count: lhs.count)
7282
return rhs.withUTF8Buffer { (rhsBuffer: UnsafeBufferPointer<UInt8>) in
7383
if lhsBuffer.count != rhsBuffer.count { return false }
7484
return lhsBuffer.elementsEqual(rhsBuffer, by: ==)

SwiftCompilerSources/Sources/SIL/ApplySite.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ extension ApplySite {
115115
}
116116
return nil
117117
}
118+
119+
public func hasSemanticsAttribute(_ attr: StaticString) -> Bool {
120+
if let callee = referencedFunction {
121+
return callee.hasSemanticsAttribute(attr)
122+
}
123+
return false
124+
}
118125
}
119126

120127
public protocol FullApplySite : ApplySite {

SwiftCompilerSources/Sources/SIL/Function.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ final public class Function : CustomStringConvertible, HasShortDescription, Hash
126126

127127
public var isTransparent: Bool { bridged.isTransparent() }
128128

129+
public var isAsync: Bool { bridged.isAsync() }
130+
129131
/// True if this is a `[global_init]` function.
130132
///
131133
/// Such a function is typically a global addressor which calls the global's
@@ -222,6 +224,9 @@ final public class Function : CustomStringConvertible, HasShortDescription, Hash
222224
}
223225
}
224226

227+
public var isSerialized: Bool { bridged.isSerialized() }
228+
public var hasValidLinkageForFragileRef: Bool { bridged.hasValidLinkageForFragileRef() }
229+
225230
/// True, if the function runs with a swift 5.1 runtime.
226231
/// Note that this is function specific, because inlinable functions are de-serialized
227232
/// in a client module, which might be compiled with a different deployment target.

SwiftCompilerSources/Sources/SIL/Location.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,8 @@ public struct Location: Equatable, CustomStringConvertible {
3737
public func hasSameSourceLocation(as other: Location) -> Bool {
3838
bridged.hasSameSourceLocation(other.bridged)
3939
}
40+
41+
public static var artificialUnreachableLocation: Location {
42+
Location(bridged: swift.SILDebugLocation.getArtificialUnreachableLocation())
43+
}
4044
}

SwiftCompilerSources/Sources/SIL/Operand.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,19 @@ public struct UseList : CollectionLikeSequence {
107107
return nil
108108
}
109109

110+
public func getSingleUser<I: Instruction>(ofType: I.Type) -> I? {
111+
var result: I? = nil
112+
for use in self {
113+
if let user = use.instruction as? I {
114+
if result != nil {
115+
return nil
116+
}
117+
result = user
118+
}
119+
}
120+
return result
121+
}
122+
110123
public var isSingleUse: Bool { singleUse != nil }
111124

112125
public func makeIterator() -> Iterator {

SwiftCompilerSources/Sources/SIL/Type.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ public struct Type : CustomStringConvertible, NoReflectionChildren {
1919
public var isAddress: Bool { bridged.isAddress() }
2020
public var isObject: Bool { !isAddress }
2121

22+
public var addressType: Type { bridged.getAddressType().type }
23+
public var objectType: Type { bridged.getObjectType().type }
24+
2225
public func isTrivial(in function: Function) -> Bool {
2326
return bridged.isTrivial(function.bridged.getFunction())
2427
}
@@ -52,6 +55,19 @@ public struct Type : CustomStringConvertible, NoReflectionChildren {
5255

5356
public var isOrContainsObjectiveCClass: Bool { bridged.isOrContainsObjectiveCClass() }
5457

58+
public var isBuiltinInteger: Bool { bridged.isBuiltinInteger() }
59+
public var isBuiltinFloat: Bool { bridged.isBuiltinFloat() }
60+
public var isBuiltinVector: Bool { bridged.isBuiltinVector() }
61+
public var builtinVectorElementType: Type { bridged.getBuiltinVectorElementType().type }
62+
63+
public func isBuiltinInteger(withFixedWidth width: Int) -> Bool {
64+
bridged.isBuiltinFixedWidthInteger(UInt32(width))
65+
}
66+
67+
public func isExactSuperclass(of type: Type) -> Bool {
68+
bridged.isExactSuperclassOf(type.bridged)
69+
}
70+
5571
public var tupleElements: TupleElementArray { TupleElementArray(type: self) }
5672

5773
public func getNominalFields(in function: Function) -> NominalFieldsArray {
@@ -167,6 +183,8 @@ extension swift.SILType {
167183
public struct NominalTypeDecl : Equatable {
168184
let bridged: BridgedNominalTypeDecl
169185

186+
public var name: StringRef { StringRef(bridged: bridged.getName()) }
187+
170188
public static func ==(lhs: NominalTypeDecl, rhs: NominalTypeDecl) -> Bool {
171189
lhs.bridged.decl == rhs.bridged.decl
172190
}

include/swift/SIL/SILBridging.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@ struct BridgedFunction {
241241
return getFunction()->isTransparent() == swift::IsTransparent;
242242
}
243243

244+
bool isAsync() const {
245+
return getFunction()->isAsync();
246+
}
247+
244248
bool isGlobalInitFunction() const {
245249
return getFunction()->isGlobalInit();
246250
}
@@ -271,6 +275,14 @@ struct BridgedFunction {
271275
return (InlineStrategy)getFunction()->getInlineStrategy();
272276
}
273277

278+
bool isSerialized() const {
279+
return getFunction()->isSerialized();
280+
}
281+
282+
bool hasValidLinkageForFragileRef() const {
283+
return getFunction()->hasValidLinkageForFragileRef();
284+
}
285+
274286
bool needsStackProtection() const {
275287
return getFunction()->needsStackProtection();
276288
}
@@ -1208,6 +1220,11 @@ struct BridgedBuilder{
12081220

12091221
struct BridgedNominalTypeDecl {
12101222
swift::NominalTypeDecl * _Nonnull decl;
1223+
1224+
SWIFT_IMPORT_UNSAFE
1225+
llvm::StringRef getName() const {
1226+
return decl->getName().str();
1227+
}
12111228
};
12121229

12131230
// Passmanager and Context

include/swift/SIL/SILLocation.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,10 @@ class SILDebugLocation {
743743
return getLocation().hasSameSourceLocation(rhs.getLocation()) &&
744744
getScope() == rhs.getScope();
745745
}
746+
747+
static SILDebugLocation getArtificialUnreachableLocation() {
748+
return SILDebugLocation(ArtificialUnreachableLocation(), nullptr);
749+
}
746750
};
747751

748752
} // end swift namespace

include/swift/SIL/SILType.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,29 @@ class SILType {
267267
});
268268
}
269269

270+
bool isBuiltinInteger() const {
271+
return is<BuiltinIntegerType>();
272+
}
273+
274+
bool isBuiltinFixedWidthInteger(unsigned width) const {
275+
BuiltinIntegerType *bi = getAs<BuiltinIntegerType>();
276+
return bi && bi->isFixedWidth(width);
277+
}
278+
279+
bool isBuiltinFloat() const {
280+
return is<BuiltinFloatType>();
281+
}
282+
283+
bool isBuiltinVector() const {
284+
return is<BuiltinVectorType>();
285+
}
286+
287+
SWIFT_IMPORT_UNSAFE
288+
SILType getBuiltinVectorElementType() const {
289+
auto vector = castTo<BuiltinVectorType>();
290+
return getPrimitiveObjectType(vector.getElementType());
291+
}
292+
270293
/// Retrieve the NominalTypeDecl for a type that maps to a Swift
271294
/// nominal or bound generic nominal type.
272295
SWIFT_IMPORT_UNSAFE

0 commit comments

Comments
 (0)