@@ -9,6 +9,152 @@ public final class Module: ModuleRef {
99 /// Retrieves the underlying LLVM value object.
1010 public var moduleRef : LLVMModuleRef { llvm }
1111
12+ public enum InlineAsmDialect {
13+ case att
14+ case intel
15+ }
16+
17+ /// Named Metadata Node
18+ public class NamedMetadataNode : NamedMetadataNodeRef {
19+ private var llvm : LLVMNamedMDNodeRef
20+
21+ /// Retrieves the underlying LLVM value object.
22+ public var namedMetadataNodeRef : LLVMNamedMDNodeRef { llvm }
23+
24+ init ( llvm: LLVMNamedMDNodeRef ) {
25+ self . llvm = llvm
26+ }
27+
28+ /// Advance a `NamedMetaDataNode` iterator to the next `NamedMetaDataNode`.
29+ ///
30+ /// Returns NULL if the iterator was already at the end and there are no more
31+ /// named metadata nodes.
32+ public func getNext( ) -> NamedMetadataNode ? {
33+ guard let nextRef = LLVMGetNextNamedMetadata ( llvm) else {
34+ return nil
35+ }
36+ return NamedMetadataNode ( llvm: nextRef)
37+ }
38+
39+ /// Decrement a `NamedMetaDataNode` iterator to the previous `NamedMetaDataNode`.
40+ ///
41+ /// Returns NULL if the iterator was already at the beginning and there are
42+ /// no previously named metadata nodes.
43+ public func getPrevious( ) -> NamedMetadataNode ? {
44+ guard let prevRef = LLVMGetPreviousNamedMetadata ( llvm) else {
45+ return nil
46+ }
47+ return NamedMetadataNode ( llvm: prevRef)
48+ }
49+
50+ /// Retrieve the name of a `NamedMetadataNode`.
51+ public func getName( ) -> String ? {
52+ var length : size_t = 0
53+ guard let cStr = LLVMGetNamedMetadataName ( llvm, & length) else {
54+ return nil
55+ }
56+ return String ( cString: cStr)
57+ }
58+ }
59+
60+ /// Enumerates the supported behaviors for resolving collisions when two
61+ /// module flags share the same key. These collisions can occur when the
62+ /// different flags are inserted under the same key, or when modules
63+ /// containing flags under the same key are merged.
64+ public enum ModuleFlagBehavior {
65+ /// Emits an error if two values disagree, otherwise the resulting value
66+ /// is that of the operands.
67+ case error
68+ /// Emits a warning if two values disagree. The result value will be the
69+ /// operand for the flag from the first module being linked.
70+ case warning
71+ /// Adds a requirement that another module flag be present and have a
72+ /// specified value after linking is performed. The value must be a
73+ /// metadata pair, where the first element of the pair is the ID of the
74+ /// module flag to be restricted, and the second element of the pair is
75+ /// the value the module flag should be restricted to. This behavior can
76+ /// be used to restrict the allowable results (via triggering of an error)
77+ /// of linking IDs with the **Override** behavior.
78+ case require
79+ /// Uses the specified value, regardless of the behavior or value of the
80+ /// other module. If both modules specify **Override**, but the values
81+ /// differ, an error will be emitted.
82+ case override
83+ /// Appends the two values, which are required to be metadata nodes.
84+ case append
85+ /// Appends the two values, which are required to be metadata
86+ /// nodes. However, duplicate entries in the second list are dropped
87+ /// during the append operation.
88+ case appendUnique
89+
90+ init ( raw: LLVMModuleFlagBehavior ) {
91+ switch raw {
92+ case LLVMModuleFlagBehaviorError:
93+ self = . error
94+ case LLVMModuleFlagBehaviorWarning:
95+ self = . warning
96+ case LLVMModuleFlagBehaviorRequire:
97+ self = . require
98+ case LLVMModuleFlagBehaviorOverride:
99+ self = . override
100+ case LLVMModuleFlagBehaviorAppend:
101+ self = . append
102+ case LLVMModuleFlagBehaviorAppendUnique:
103+ self = . appendUnique
104+ default :
105+ fatalError ( " Unknown behavior kind " )
106+ }
107+ }
108+ }
109+
110+ class Metadata : MetadataRef {
111+ private let llvm : LLVMMetadataRef
112+ public var metadataRef : LLVMMetadataRef {
113+ llvm }
114+ public init ( llvm: LLVMMetadataRef ) {
115+ self . llvm = llvm
116+ }
117+ }
118+
119+ public class ModuleFlagEntry {
120+ private let llvm : OpaquePointer ?
121+ private let bounds : Int
122+
123+ public init ( llvm: OpaquePointer ? , bounds: Int ) {
124+ self . llvm = llvm
125+ self . bounds = bounds
126+ }
127+
128+ /// Get Metadata flags etries count
129+ public var count : Int { self . bounds }
130+
131+ /// Returns the flag behavior for a module flag entry at a specific index.
132+ public func getFlagBehavior( at index: UInt32 ) -> ModuleFlagBehavior {
133+ let bh = LLVMModuleFlagEntriesGetFlagBehavior ( llvm, index)
134+ return ModuleFlagBehavior ( raw: bh)
135+ }
136+
137+ /// Returns the key for a module flag entry at a specific index.
138+ public func getKey( at index: UInt32 ) -> String {
139+ var length : Int = 0
140+ let keyPointer = LLVMModuleFlagEntriesGetKey ( llvm, index, & length)
141+ return String ( cString: keyPointer!)
142+
143+ }
144+
145+ /// Returns the metadata for a module flag entry at a specific index.
146+ public func getMetadata( at index: UInt32 ) -> MetadataRef {
147+ let metadata = LLVMModuleFlagEntriesGetMetadata ( llvm, index) !
148+ return Metadata ( llvm: metadata)
149+ }
150+
151+ /// Deinitialize this value and dispose of its resources.
152+ deinit {
153+ guard let ptr = llvm else { return }
154+ LLVMDisposeModuleFlagsMetadata ( ptr)
155+ }
156+ }
157+
12158 /// Init function by LLVM Value
13159 public init ( llvm: LLVMModuleRef ) {
14160 self . llvm = llvm
@@ -36,33 +182,46 @@ public final class Module: ModuleRef {
36182 }
37183
38184 /// Return an exact copy of the specified module.
39- public func clone_nodule ( ) -> ModuleRef {
185+ public func cloneModule ( ) -> Self {
40186 let new_module = LLVMCloneModule ( llvm) !
41187 return Self ( llvm: new_module)
42188 }
43189
190+ /// Get and Set the identifier of a module.
191+ public var moduleIdentifier : String {
192+ get {
193+ self . getModuleIdentifier
194+ }
195+ set {
196+ self . setModuleIdentifier ( identifier: newValue)
197+ }
198+ }
199+
44200 /// Obtain the identifier of a module.
45- public var getLLVMModuleIdentifier : String {
201+ public var getModuleIdentifier : String {
46202 var length : UInt = 0
47203 guard let cString = LLVMGetModuleIdentifier ( llvm, & length) else { return " " }
48204 return String ( cString: cString)
49205 }
50206
51- public func setLLVMModuleIdentifier( module: LLVMModuleRef , identifier: String ) {
207+ /// Set the identifier of a module to a string Ident with length Len.
208+ public func setModuleIdentifier( identifier: String ) {
52209 identifier. withCString { cString in
53- LLVMSetModuleIdentifier ( module , cString, identifier. count)
210+ LLVMSetModuleIdentifier ( llvm , cString, identifier. count)
54211 }
55212 }
56213
57- public var getModuleIdentifier : String ? {
58- var length : UInt = 0
59- guard let cString = LLVMGetModuleIdentifier ( llvm, & length) else {
60- return nil
214+ /// Get and Set the original source file name of a module to a string Name
215+ public var sourceFileName : String {
216+ get {
217+ self . getSourceFileName!
218+ }
219+ set {
220+ self . setSourceFileName ( fileName: newValue)
61221 }
62- return String ( cString: cString)
63222 }
64223
65- /// Set the identifier of a module to a string Ident with length Len.
224+ ///Set the original source file name of a module to a string Name
66225 public func setSourceFileName( fileName: String ) {
67226 fileName. withCString { cString in
68227 LLVMSetSourceFileName ( llvm, cString, fileName. utf8. count)
@@ -71,17 +230,17 @@ public final class Module: ModuleRef {
71230
72231 /// Obtain the module's original source file name.
73232 public var getSourceFileName : String ? {
74- var length : size_t = 0
233+ var length : Int = 0
75234 guard let cString = LLVMGetSourceFileName ( llvm, & length) else {
76235 return nil
77236 }
78237 return String ( cString: cString)
79238 }
80239
81240 /// Set the data layout for a module.
82- public func setDataLayout( module : LLVMModuleRef , dataLayout: String ) {
241+ public func setDataLayout( dataLayout: String ) {
83242 dataLayout. withCString { cString in
84- LLVMSetDataLayout ( module , cString)
243+ LLVMSetDataLayout ( llvm , cString)
85244 }
86245 }
87246
@@ -93,13 +252,32 @@ public final class Module: ModuleRef {
93252 return String ( cString: cString)
94253 }
95254
255+
256+ /// Obtain the target triple for a module.
257+ func getTargetTriple( ) -> String {
258+ guard let targetTriplePointer = LLVMGetTarget ( llvm) else {
259+ return " "
260+ }
261+ return String ( cString: targetTriplePointer)
262+ }
263+
96264 /// Set the target triple for a module.
97265 public func setTarget( triple: String ) {
98266 triple. withCString { cString in
99267 LLVMSetTarget ( llvm, cString)
100268 }
101269 }
102270
271+ /// Returns the module flags as an array of flag-key-value triples. The caller
272+ /// is responsible for freeing this array by calling
273+ /// `LLVMDisposeModuleFlagsMetadata`.
274+ public func copyModuleFlagsMetadata( ) -> ModuleFlagEntry ? {
275+ var length : Int = 0
276+ guard let flagsPointer = LLVMCopyModuleFlagsMetadata ( llvm, & length) else { return nil }
277+
278+ return ModuleFlagEntry ( llvm: flagsPointer, bounds: length)
279+ }
280+
103281 /// Destroy a module instance.
104282 ///
105283 /// This must be called for every created module or memory will be leaked.
0 commit comments