From 0fd7334cf9809c966f8ef3db36928e97dceaff3c Mon Sep 17 00:00:00 2001 From: vdka Date: Tue, 19 Sep 2017 10:54:51 +1000 Subject: [PATCH 1/3] Add ConstString initializer to ArrayType --- Sources/LLVM/ArrayType.swift | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Sources/LLVM/ArrayType.swift b/Sources/LLVM/ArrayType.swift index 359f5e2e..bfd66fe7 100644 --- a/Sources/LLVM/ArrayType.swift +++ b/Sources/LLVM/ArrayType.swift @@ -31,6 +31,18 @@ public struct ArrayType: IRType { } } + /// Creates a constant array value of i8 from a swift string. + /// + /// - parameter string: A string to create a null terminated array from. + /// - parameter context: The context to create the string in. + /// + /// - returns: A null terminated constant array value containing + /// string.utf8.count + 1 i8's. + public static func constant(string: String, in context: Context = .global) -> IRValue { + let length = string.utf8.count + return LLVMConstStringInContext(context.llvm, string, UInt32(length), 0) + } + /// Retrieves the underlying LLVM type object. public func asLLVM() -> LLVMTypeRef { return LLVMArrayType(elementType.asLLVM(), UInt32(count)) From df3e9458ad3a0bf0ee6a1987cf7e1189c2d0b587 Mon Sep 17 00:00:00 2001 From: Ethan Jackwitz Date: Tue, 19 Sep 2017 11:03:11 +1000 Subject: [PATCH 2/3] Update comment to mention use of utf8 bytes --- Sources/LLVM/ArrayType.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sources/LLVM/ArrayType.swift b/Sources/LLVM/ArrayType.swift index bfd66fe7..a6c9d1f3 100644 --- a/Sources/LLVM/ArrayType.swift +++ b/Sources/LLVM/ArrayType.swift @@ -31,7 +31,8 @@ public struct ArrayType: IRType { } } - /// Creates a constant array value of i8 from a swift string. + /// Creates a constant, null terminated array value of utf8 bytes from + /// string's utf8 member. /// /// - parameter string: A string to create a null terminated array from. /// - parameter context: The context to create the string in. From a8ce52af1f51e01521fa97f51faec4526e3b16bf Mon Sep 17 00:00:00 2001 From: Ethan Jackwitz Date: Tue, 19 Sep 2017 11:04:07 +1000 Subject: [PATCH 3/3] Update ArrayType.swift --- Sources/LLVM/ArrayType.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sources/LLVM/ArrayType.swift b/Sources/LLVM/ArrayType.swift index a6c9d1f3..760419aa 100644 --- a/Sources/LLVM/ArrayType.swift +++ b/Sources/LLVM/ArrayType.swift @@ -31,14 +31,14 @@ public struct ArrayType: IRType { } } - /// Creates a constant, null terminated array value of utf8 bytes from - /// string's utf8 member. + /// Creates a constant, null terminated array value of UTF-8 bytes from + /// string's `utf8` member. /// /// - parameter string: A string to create a null terminated array from. /// - parameter context: The context to create the string in. /// /// - returns: A null terminated constant array value containing - /// string.utf8.count + 1 i8's. + /// `string.utf8.count + 1` i8's. public static func constant(string: String, in context: Context = .global) -> IRValue { let length = string.utf8.count return LLVMConstStringInContext(context.llvm, string, UInt32(length), 0)