@@ -190,19 +190,38 @@ public struct Builder {
190
190
}
191
191
}
192
192
193
- private func createIntegerLiteral( _ value: Int , type: Type , treatAsSigned: Bool ) -> IntegerLiteralInst {
194
- let literal = bridged. createIntegerLiteral ( type. bridged, value, treatAsSigned)
195
- return notifyNew ( literal. getAs ( IntegerLiteralInst . self) )
196
- }
193
+ /// Creates a integer literal instruction with the given integer value and
194
+ /// type. If an extension is necessary, the value is extended in accordance
195
+ /// with the signedness of `Value`.
196
+ public func createIntegerLiteral< Value: FixedWidthInteger > (
197
+ _ value: Value ,
198
+ type: Type
199
+ ) -> IntegerLiteralInst {
200
+ precondition (
201
+ Value . bitWidth <= Int . bitWidth,
202
+ " Cannot fit \( Value . bitWidth) -bit integer into \( Int . bitWidth) -bit 'Swift.Int' "
203
+ )
204
+ // Extend the value based on its signedness to the bit width of `Int` and
205
+ // reinterpret it as an `Int`.
206
+ let extendedValue : Int =
207
+ if Value . isSigned {
208
+ Int ( value)
209
+ } else {
210
+ // NB: This initializer is effectively a generic equivalent
211
+ // of `Int(bitPattern:)`
212
+ Int ( truncatingIfNeeded: value)
213
+ }
197
214
198
- public func createIntegerLiteral ( _ value : Int , type: Type ) -> IntegerLiteralInst {
199
- createIntegerLiteral ( value , type : type , treatAsSigned : true )
215
+ let literal = bridged . createIntegerLiteral ( type. bridged , extendedValue , Value . isSigned )
216
+ return notifyNew ( literal . getAs ( IntegerLiteralInst . self ) )
200
217
}
201
218
202
- /// Creates a `Builtin.Int1` integer literal instruction with the given value.
219
+ /// Creates a `Builtin.Int1` integer literal instruction with a value
220
+ /// corresponding to the given Boolean.
203
221
public func createBoolLiteral( _ value: Bool ) -> IntegerLiteralInst {
204
222
let boolType = notificationHandler. getBuiltinIntegerType ( 1 ) . type
205
- return createIntegerLiteral ( value ? 1 : 0 , type: boolType, treatAsSigned: false )
223
+ let integerValue : UInt = value ? 1 : 0
224
+ return createIntegerLiteral ( integerValue, type: boolType)
206
225
}
207
226
208
227
public func createAllocRef( _ type: Type , isObjC: Bool = false , canAllocOnStack: Bool = false , isBare: Bool = false ,
0 commit comments