diff --git a/Sources/LLVM/IRBuilder.swift b/Sources/LLVM/IRBuilder.swift index 7647403c..cdf96861 100644 --- a/Sources/LLVM/IRBuilder.swift +++ b/Sources/LLVM/IRBuilder.swift @@ -108,6 +108,43 @@ public enum RealPredicate { } } +extension Module { + /// Searches for and retrieves a global variable with the given name in this + /// module if that name references an existing global variable. + /// + /// - parameter name: The name of the global to reference. + /// + /// - returns: A value representing the referenced global if it exists. + public func global(named name: String) -> Global? { + guard let ref = LLVMGetNamedGlobal(llvm, name) else { return nil } + return Global(llvm: ref) + } + + /// Searches for and retrieves a type with the given name in this module if + /// that name references an existing type. + /// + /// - parameter name: The name of the type to create. + /// + /// - returns: A representation of the newly created type with the given name + /// or nil if such a representation could not be created. + public func type(named name: String) -> IRType? { + guard let type = LLVMGetTypeByName(llvm, name) else { return nil } + return convertType(type) + } + + /// Searches for and retrieves a function with the given name in this module + /// if that name references an existing function. + /// + /// - parameter name: The name of the function to create. + /// + /// - returns: A representation of the newly created function with the given + /// name or nil if such a representation could not be created. + public func function(named name: String) -> Function? { + guard let fn = LLVMGetNamedFunction(llvm, name) else { return nil } + return Function(llvm: fn) + } +} + /// An `IRBuilder` is a helper object that generates LLVM instructions. IR /// Builders keep track of a position within a function or basic block and has /// methods to insert instructions at that position. @@ -935,7 +972,7 @@ public class IRBuilder { return LLVMBuildInsertElement(llvm, vector.asLLVM(), element.asLLVM(), index.asLLVM(), name) } - // MARK: Global Variable Creation Instructions + // MARK: Global Variable Instructions /// Build a named global of the given type. /// @@ -974,8 +1011,8 @@ public class IRBuilder { /// - parameter name: The name for the newly inserted instruction. /// /// - returns: A value representing the newly inserted global string variable. - public func buildGlobalString(_ string: String, name: String = "") -> IRValue { - return LLVMBuildGlobalString(llvm, string, name) + public func buildGlobalString(_ string: String, name: String = "") -> Global { + return Global(llvm: LLVMBuildGlobalString(llvm, string, name)) } /// Builds a named global variable containing a pointer to the contents of the diff --git a/Sources/LLVM/Module.swift b/Sources/LLVM/Module.swift index a7e67bc7..ecc17303 100644 --- a/Sources/LLVM/Module.swift +++ b/Sources/LLVM/Module.swift @@ -110,30 +110,6 @@ public final class Module { } } - /// Searches for and retrieves a type with the given name in this module if - /// that name references an existing type. - /// - /// - parameter name: The name of the type to create. - /// - /// - returns: A representation of the newly created type with the given name - /// or nil if such a representation could not be created. - public func type(named name: String) -> IRType? { - guard let type = LLVMGetTypeByName(llvm, name) else { return nil } - return convertType(type) - } - - /// Searches for and retrieves a function with the given name in this module - /// if that name references an existing function. - /// - /// - parameter name: The name of the function to create. - /// - /// - returns: A representation of the newly created function with the given - /// name or nil if such a representation could not be created. - public func function(named name: String) -> Function? { - guard let fn = LLVMGetNamedFunction(llvm, name) else { return nil } - return Function(llvm: fn) - } - /// Verifies that this module is valid, taking the specified action if not. /// If this module did not pass verification, a description of any invalid /// constructs is provided with the thrown