@@ -190,19 +190,38 @@ public struct Builder {
190190 }
191191 }
192192
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+ }
197214
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 ) )
200217 }
201218
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.
203221 public func createBoolLiteral( _ value: Bool ) -> IntegerLiteralInst {
204222 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)
206225 }
207226
208227 public func createAllocRef( _ type: Type , isObjC: Bool = false , canAllocOnStack: Bool = false , isBare: Bool = false ,
0 commit comments