Skip to content

Commit 7ce4e17

Browse files
committed
Scalar constants - extend
1 parent f20adb8 commit 7ce4e17

File tree

1 file changed

+30
-39
lines changed

1 file changed

+30
-39
lines changed
Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,81 @@
11
import CLLVM
22

33
/// Functions in this group model ValueRef instances that correspond to constants referring to scalar types.
4-
public enum ScalarConstants {
4+
public enum ScalarConstant {
55
/// Obtain a constant value for an integer type.
6-
public static func constInt(intTy: TypeRef, n: UInt64, signExtend: Bool) -> Value? {
7-
guard let valueRef = LLVMConstInt(intTy.typeRef, n, signExtend.llvm) else { return nil }
6+
public static func constInt(intTy: TypeRef, n: UInt64, signExtend: Bool) -> Value {
7+
let valueRef = LLVMConstInt(intTy.typeRef, n, signExtend.llvm)!
88
return Value(llvm: valueRef)
99
}
1010

1111
/// Obtain a constant value for an integer of arbitrary precision.
1212
public static func constIntOfArbitraryPrecision(intType: TypeRef, words: [UInt64]) -> Value? {
1313
let numWords = UInt32(words.count)
14-
let val = words.withUnsafeBufferPointer { bufferPointer in
15-
LLVMConstIntOfArbitraryPrecision(intType.typeRef, numWords, bufferPointer.baseAddress)
14+
let valueRef = words.withUnsafeBufferPointer { bufferPointer in
15+
LLVMConstIntOfArbitraryPrecision(intType.typeRef, numWords, bufferPointer.baseAddress)!
1616
}
17-
guard let valueRef = val else { return nil }
1817
return Value(llvm: valueRef)
1918
}
2019

2120
/// Obtain a constant value for an integer parsed from a string.
2221
/// A similar API, `constIntOfStringAndSize` is also available. If the
2322
/// string's length is available, it is preferred to call that function instead.
24-
public static func constIntOfString(intType: TypeRef, text: String, radix: UInt8) -> Value? {
25-
let val = text.withCString { cString in
26-
LLVMConstIntOfString(intType.typeRef, cString, radix)
23+
public static func constIntOfString(intType: TypeRef, text: String, radix: UInt8) -> Value {
24+
let valueRef = text.withCString { cString in
25+
LLVMConstIntOfString(intType.typeRef, cString, radix)!
2726
}
28-
guard let valueRef = val else { return nil }
2927
return Value(llvm: valueRef)
3028
}
3129

3230
/// Obtain a constant value for an integer parsed from a string with specified length.
33-
public static func constIntOfStringAndSize(intType: TypeRef, text: String, radix: UInt8) -> Value? {
34-
let val = text.withCString { cString in
31+
public static func constIntOfStringAndSize(intType: TypeRef, text: String, radix: UInt8) -> Value {
32+
let valueRef = text.withCString { cString in
3533
let length = UInt32(text.utf8.count)
36-
return LLVMConstIntOfStringAndSize(intType.typeRef, cString, length, radix)
34+
return LLVMConstIntOfStringAndSize(intType.typeRef, cString, length, radix)!
3735
}
38-
guard let valueRef = val else { return nil }
3936
return Value(llvm: valueRef)
4037
}
4138

4239
/// Obtain a constant value referring to a double floating point value.
43-
public static func constReal(realType: TypeRef, n: Double) -> Value? {
44-
guard let valueRef = LLVMConstReal(realType.typeRef, n) else { return nil }
40+
public static func constReal(realType: TypeRef, n: Double) -> Value {
41+
let valueRef = LLVMConstReal(realType.typeRef, n)!
4542
return Value(llvm: valueRef)
4643
}
4744

4845
/// Obtain a constant for a floating point value parsed from a string.
4946
/// A similar API, LLVMConstRealOfStringAndSize is also available. It
5047
/// should be used if the input string's length is known.
51-
public static func constRealOfString(realType: TypeRef, text: String) -> Value? {
52-
let val = text.withCString { cString in
53-
LLVMConstRealOfString(realType.typeRef, cString)
48+
public static func constRealOfString(realType: TypeRef, text: String) -> Value {
49+
let valueRef = text.withCString { cString in
50+
LLVMConstRealOfString(realType.typeRef, cString)!
5451
}
55-
guard let valueRef = val else { return nil }
5652
return Value(llvm: valueRef)
5753
}
5854

5955
/// Obtain a constant for a floating point value parsed from a string.
60-
public static func constRealOfStringAndSize(realType: TypeRef, text: String) -> Value? {
61-
let val = text.withCString { cString in
56+
public static func constRealOfStringAndSize(realType: TypeRef, text: String) -> Value {
57+
let valueRef = text.withCString { cString in
6258
let length = UInt32(text.utf8.count)
63-
return LLVMConstRealOfStringAndSize(realType.typeRef, cString, length)
59+
return LLVMConstRealOfStringAndSize(realType.typeRef, cString, length)!
6460
}
65-
guard let valueRef = val else { return nil }
6661
return Value(llvm: valueRef)
6762
}
6863

6964
/// Obtain the zero extended value for an integer constant value.
70-
// unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal);
71-
// public static func getLLVMConstIntZExtValue(constantVal: LLVMValueRef) -> UInt64 {
72-
// return LLVMConstIntGetZExtValue(constantVal)
73-
// }
74-
65+
public static func constIntZExtValue(constantVal: ValueRef) -> UInt64 {
66+
LLVMConstIntGetZExtValue(constantVal.valueRef)
67+
}
7568

7669
/// Obtain the sign extended value for an integer constant value.
77-
// long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal);
78-
// public static func constIntSExtValue(constantVal: LLVMValueRef) -> Int64 {
79-
// return LLVMConstIntGetSExtValue(constantVal)
80-
// }
70+
public static func constIntSExtValue(constantVal: ValueRef) -> Int64 {
71+
LLVMConstIntGetSExtValue(constantVal.valueRef)
72+
}
8173

8274
/// Obtain the double value for an floating point constant value.
8375
/// losesInfo indicates if some precision was lost in the conversion.
84-
// double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *losesInfo);
85-
// public static func constRealGetDouble(constantVal: LLVMValueRef) -> (Double, Bool) {
86-
// var losesInfo: LLVMBool = 0
87-
// let result = LLVMConstRealGetDouble(constantVal, &losesInfo)
88-
// return (result, losesInfo != 0)
89-
// }
76+
public static func constRealGetDouble(constantVal: ValueRef) -> (Double, Bool) {
77+
var losesInfo: LLVMBool = 0
78+
let val = LLVMConstRealGetDouble(constantVal.valueRef, &losesInfo)
79+
return (val, losesInfo != 0)
80+
}
9081
}

0 commit comments

Comments
 (0)