@@ -108,6 +108,43 @@ public enum RealPredicate {
108108 }
109109}
110110
111+ extension Module {
112+ /// Searches for and retrieves a global variable with the given name in this
113+ /// module if that name references an existing global variable.
114+ ///
115+ /// - parameter name: The name of the global to reference.
116+ ///
117+ /// - returns: A value representing the referenced global if it exists.
118+ public func global( named name: String ) -> Global ? {
119+ guard let ref = LLVMGetNamedGlobal ( llvm, name) else { return nil }
120+ return Global ( llvm: ref)
121+ }
122+
123+ /// Searches for and retrieves a type with the given name in this module if
124+ /// that name references an existing type.
125+ ///
126+ /// - parameter name: The name of the type to create.
127+ ///
128+ /// - returns: A representation of the newly created type with the given name
129+ /// or nil if such a representation could not be created.
130+ public func type( named name: String ) -> IRType ? {
131+ guard let type = LLVMGetTypeByName ( llvm, name) else { return nil }
132+ return convertType ( type)
133+ }
134+
135+ /// Searches for and retrieves a function with the given name in this module
136+ /// if that name references an existing function.
137+ ///
138+ /// - parameter name: The name of the function to create.
139+ ///
140+ /// - returns: A representation of the newly created function with the given
141+ /// name or nil if such a representation could not be created.
142+ public func function( named name: String ) -> Function ? {
143+ guard let fn = LLVMGetNamedFunction ( llvm, name) else { return nil }
144+ return Function ( llvm: fn)
145+ }
146+ }
147+
111148/// An `IRBuilder` is a helper object that generates LLVM instructions. IR
112149/// Builders keep track of a position within a function or basic block and has
113150/// methods to insert instructions at that position.
@@ -974,8 +1011,8 @@ public class IRBuilder {
9741011 /// - parameter name: The name for the newly inserted instruction.
9751012 ///
9761013 /// - returns: A value representing the newly inserted global string variable.
977- public func buildGlobalString( _ string: String , name: String = " " ) -> IRValue {
978- return LLVMBuildGlobalString ( llvm, string, name)
1014+ public func buildGlobalString( _ string: String , name: String = " " ) -> Global {
1015+ return Global ( llvm : LLVMBuildGlobalString ( llvm, string, name) )
9791016 }
9801017
9811018 /// Builds a named global variable containing a pointer to the contents of the
@@ -989,19 +1026,6 @@ public class IRBuilder {
9891026 public func buildGlobalStringPtr( _ string: String , name: String = " " ) -> IRValue {
9901027 return LLVMBuildGlobalStringPtr ( llvm, string, name)
9911028 }
992-
993- /// Builds a named reference to a global variable with the given name, if it
994- /// exists.
995- ///
996- /// - parameter name: The name of the global to reference.
997- ///
998- /// - returns: A value representing the referenced global if it exists.
999- public func referenceGlobal( named name: String ) -> Global ? {
1000- guard let ref = LLVMGetNamedGlobal ( module. llvm, name) else {
1001- return nil
1002- }
1003- return Global ( llvm: ref)
1004- }
10051029
10061030 deinit {
10071031 LLVMDisposeBuilder ( llvm)
0 commit comments