Skip to content

Commit 7f67a02

Browse files
committed
Document Module
1 parent cc10dff commit 7f67a02

File tree

1 file changed

+64
-9
lines changed

1 file changed

+64
-9
lines changed

Sources/LLVMSwift/Module.swift

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,31 @@
11
import cllvm
22

3+
/// A `Context` represents execution states for the core LLVM IR system.
34
public class Context {
45
internal let llvm: LLVMContextRef
6+
7+
/// Retrieves the global context instance.
58
public static let global = Context(llvm: LLVMGetGlobalContext()!)
9+
10+
/// Creates a `Context` object from an `LLVMContextRef` object.
611
public init(llvm: LLVMContextRef) {
712
self.llvm = llvm
813
}
914
}
1015

16+
/// Represents the possible errors that can be thrown while interacting with a
17+
/// `Module` object.
1118
public enum ModuleError: Error, CustomStringConvertible {
19+
/// Thrown when a module does not pass the module verification process.
20+
/// Includes the reason the module did not pass verification.
1221
case didNotPassVerification(String)
22+
/// Thrown when a module cannot be printed at a given path. Provides the
23+
/// erroneous path and a deeper reason why printing to that path failed.
1324
case couldNotPrint(path: String, error: String)
25+
/// Thrown when a module cannot emit bitcode because it contains erroneous
26+
/// declarations.
1427
case couldNotEmitBitCode(path: String)
15-
28+
1629
public var description: String {
1730
switch self {
1831
case .didNotPassVerification(let message):
@@ -25,8 +38,17 @@ public enum ModuleError: Error, CustomStringConvertible {
2538
}
2639
}
2740

41+
/// A `Module` represents the top-level structure of an LLVM program. An LLVM
42+
/// module is effectively a translation unit or a collection of translation
43+
/// units merged together.
2844
public final class Module {
2945
internal let llvm: LLVMModuleRef
46+
47+
/// Creates a `Module` with the given name.
48+
///
49+
/// - parameter name: The name of the module.
50+
/// - parameter context: The context to associate this module with. If no
51+
/// context is provided, one will be inferred.
3052
public init(name: String, context: Context? = nil) {
3153
if let context = context {
3254
llvm = LLVMModuleCreateWithNameInContext(name, context.llvm)
@@ -36,13 +58,21 @@ public final class Module {
3658
self.context = Context(llvm: LLVMGetModuleContext(llvm)!)
3759
}
3860
}
39-
61+
62+
/// Returns the context associated with this module.
4063
public let context: Context
41-
64+
65+
/// Obtain the data layout for this module.
4266
public var dataLayout: TargetData {
4367
return TargetData(llvm: LLVMGetModuleDataLayout(llvm))
4468
}
45-
69+
70+
/// Print a representation of a module to a file at the given path.
71+
///
72+
/// If the provided path is not suitable for writing, this function will throw
73+
/// `ModuleError.couldNotPrint`.
74+
///
75+
/// - parameter path: The path to write the module's representation to.
4676
public func print(to path: String) throws {
4777
var err: UnsafeMutablePointer<Int8>?
4878
path.withCString { cString in
@@ -55,7 +85,13 @@ public final class Module {
5585
throw ModuleError.couldNotPrint(path: path, error: String(cString: err))
5686
}
5787
}
58-
88+
89+
/// Writes the bitcode of elements in this module to a file at the given path.
90+
///
91+
/// If the provided path is not suitable for writing, this function will throw
92+
/// `ModuleError.couldNotEmitBitCode`.
93+
///
94+
/// - parameter path: The path to write the module's representation to.
5995
public func emitBitCode(to path: String) throws {
6096
let status = path.withCString { cString -> Int32 in
6197
let mutable = strdup(cString)
@@ -67,17 +103,35 @@ public final class Module {
67103
throw ModuleError.couldNotEmitBitCode(path: path)
68104
}
69105
}
70-
106+
107+
/// Creates a type with the given name in this module if that name does not
108+
/// conflict with an existing type name.
109+
///
110+
/// - parameter name: The name of the type to create.
111+
///
112+
/// - returns: A representation of the newly created type with the given name
113+
/// or nil if such a representation could not be created.
71114
public func type(named name: String) -> IRType? {
72115
guard let type = LLVMGetTypeByName(llvm, name) else { return nil }
73116
return convertType(type)
74117
}
75-
118+
119+
/// Creates a function with the given name in this module if that name does
120+
/// not conflict with an existing type name.
121+
///
122+
/// - parameter name: The name of the function to create.
123+
///
124+
/// - returns: A representation of the newly created function with the given
125+
/// name or nil if such a representation could not be created.
76126
public func function(named name: String) -> Function? {
77127
guard let fn = LLVMGetNamedFunction(llvm, name) else { return nil }
78128
return Function(llvm: fn)
79129
}
80-
130+
131+
/// Verifies that this module is valid, taking the specified action if not.
132+
/// If this module did not pass verification, a description of any invalid
133+
/// constructs is provided with the thrown
134+
/// `ModuleError.didNotPassVerification` error.
81135
public func verify() throws {
82136
var message: UnsafeMutablePointer<Int8>?
83137
let status = Int(LLVMVerifyModule(llvm, LLVMReturnStatusAction, &message))
@@ -86,7 +140,8 @@ public final class Module {
86140
throw ModuleError.didNotPassVerification(String(cString: message))
87141
}
88142
}
89-
143+
144+
/// Dump a representation of this module to stderr.
90145
public func dump() {
91146
LLVMDumpModule(llvm)
92147
}

0 commit comments

Comments
 (0)