@@ -41,7 +41,7 @@ public enum ModuleError: Error, CustomStringConvertible {
41
41
/// A `Module` represents the top-level structure of an LLVM program. An LLVM
42
42
/// module is effectively a translation unit or a collection of translation
43
43
/// units merged together.
44
- public final class Module {
44
+ public final class Module : CustomStringConvertible {
45
45
internal let llvm : LLVMModuleRef
46
46
47
47
/// Creates a `Module` with the given name.
@@ -123,11 +123,64 @@ public final class Module {
123
123
}
124
124
}
125
125
126
+ /// Retrieves the sequence of functions that make up this module.
127
+ public var functions : AnySequence < Function > {
128
+ var current = firstFunction
129
+ return AnySequence< Function> {
130
+ return AnyIterator< Function> {
131
+ defer { current = current? . next ( ) }
132
+ return current
133
+ }
134
+ }
135
+ }
136
+
137
+ /// Retrieves the first function in this module, if there are any functions.
138
+ public var firstFunction : Function ? {
139
+ guard let fn = LLVMGetFirstFunction ( llvm) else { return nil }
140
+ return Function ( llvm: fn)
141
+ }
142
+
143
+ /// Retrieves the last function in this module, if there are any functions.
144
+ public var lastFunction : Function ? {
145
+ guard let fn = LLVMGetLastFunction ( llvm) else { return nil }
146
+ return Function ( llvm: fn)
147
+ }
148
+
149
+ /// Retrieves the first global in this module, if there are any globals.
150
+ public var firstGlobal : Global ? {
151
+ guard let fn = LLVMGetFirstGlobal ( llvm) else { return nil }
152
+ return Global ( llvm: fn)
153
+ }
154
+
155
+ /// Retrieves the last global in this module, if there are any globals.
156
+ public var lastGlobal : Global ? {
157
+ guard let fn = LLVMGetLastGlobal ( llvm) else { return nil }
158
+ return Global ( llvm: fn)
159
+ }
160
+
161
+ /// Retrieves the sequence of functions that make up this module.
162
+ public var globals : AnySequence < Global > {
163
+ var current = firstGlobal
164
+ return AnySequence< Global> {
165
+ return AnyIterator< Global> {
166
+ defer { current = current? . next ( ) }
167
+ return current
168
+ }
169
+ }
170
+ }
171
+
126
172
/// Dump a representation of this module to stderr.
127
173
public func dump( ) {
128
174
LLVMDumpModule ( llvm)
129
175
}
130
176
177
+ /// The full text IR of this module
178
+ public var description : String {
179
+ let cStr = LLVMPrintModuleToString ( llvm) !
180
+ defer { LLVMDisposeMessage ( cStr) }
181
+ return String ( cString: cStr)
182
+ }
183
+
131
184
deinit {
132
185
LLVMDisposeModule ( llvm)
133
186
}
0 commit comments