Skip to content

Commit 2c17a2d

Browse files
committed
Extract linkage and visibility accessors into the new IRGlobal protocol
1 parent 545949b commit 2c17a2d

File tree

4 files changed

+22
-39
lines changed

4 files changed

+22
-39
lines changed

Sources/LLVM/Alias.swift

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,9 @@ import cllvm
22

33
/// An `Alias` represents a global alias in an LLVM module - a new symbol and
44
/// corresponding metadata for an existing position
5-
public struct Alias: IRValue {
5+
public struct Alias: IRGlobal {
66
internal let llvm: LLVMValueRef
77

8-
/// Retrieves the linkage information for this alias.
9-
public var linkage: Linkage {
10-
get { return Linkage(llvm: LLVMGetLinkage(asLLVM())) }
11-
set { LLVMSetLinkage(asLLVM(), newValue.llvm) }
12-
}
13-
14-
/// Retrieves the visibility style for this alias.
15-
public var visibility: Visibility {
16-
get { return Visibility(llvm: LLVMGetVisibility(asLLVM())) }
17-
set { LLVMSetVisibility(asLLVM(), newValue.llvm) }
18-
}
19-
208
/// Retrieves the underlying LLVM value object.
219
public func asLLVM() -> LLVMValueRef {
2210
return llvm

Sources/LLVM/Function.swift

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import cllvm
33
/// A `Function` represents a named function body in LLVM IR source. Functions
44
/// in LLVM IR encapsulate a list of parameters and a sequence of basic blocks
55
/// and provide a way to append to that sequence to build out its body.
6-
public class Function: IRValue {
6+
public class Function: IRGlobal {
77
internal let llvm: LLVMValueRef
88
internal init(llvm: LLVMValueRef) {
99
self.llvm = llvm
@@ -107,18 +107,6 @@ public class Function: IRValue {
107107
LLVMDeleteFunction(llvm)
108108
}
109109

110-
/// Retrieves the linkage information for this function.
111-
public var linkage: Linkage {
112-
get { return Linkage(llvm: LLVMGetLinkage(asLLVM())) }
113-
set { LLVMSetLinkage(asLLVM(), newValue.llvm) }
114-
}
115-
116-
/// Retrieves the visibility style for this function.
117-
public var visibility: Visibility {
118-
get { return Visibility(llvm: LLVMGetVisibility(asLLVM())) }
119-
set { LLVMSetVisibility(asLLVM(), newValue.llvm) }
120-
}
121-
122110
/// Retrieves the underlying LLVM value object.
123111
public func asLLVM() -> LLVMValueRef {
124112
return llvm

Sources/LLVM/Global.swift

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import cllvm
33
/// A `Global` represents a region of memory allocated at compile time instead
44
/// of at runtime. A global variable must either have an initializer, or make
55
/// reference to an external definition that has an initializer.
6-
public struct Global: IRValue {
6+
public struct Global: IRGlobal {
77
internal let llvm: LLVMValueRef
88

99
/// Returns whether this global variable has no initializer because it makes
@@ -33,18 +33,6 @@ public struct Global: IRValue {
3333
set { LLVMSetThreadLocal(asLLVM(), newValue.llvm) }
3434
}
3535

36-
/// Retrieves the linkage information for this global value.
37-
public var linkage: Linkage {
38-
get { return Linkage(llvm: LLVMGetLinkage(asLLVM())) }
39-
set { LLVMSetLinkage(asLLVM(), newValue.llvm) }
40-
}
41-
42-
/// Retrieves the visibility style for this global value.
43-
public var visibility: Visibility {
44-
get { return Visibility(llvm: LLVMGetVisibility(asLLVM())) }
45-
set { LLVMSetVisibility(asLLVM(), newValue.llvm) }
46-
}
47-
4836
/// Deletes the global variable from its containing module.
4937
/// - note: This does not remove references to this global from the
5038
/// module. Ensure you have removed all insructions that reference

Sources/LLVM/IRGlobal.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import cllvm
2+
3+
/// An `IRGlobal` is a value, alias, or function that exists at the top level of
4+
/// an LLVM module.
5+
public protocol IRGlobal: IRValue {}
6+
7+
extension IRGlobal {
8+
/// Retrieves the linkage information for this global.
9+
public var linkage: Linkage {
10+
get { return Linkage(llvm: LLVMGetLinkage(asLLVM())) }
11+
set { LLVMSetLinkage(asLLVM(), newValue.llvm) }
12+
}
13+
14+
/// Retrieves the visibility style for this global.
15+
public var visibility: Visibility {
16+
get { return Visibility(llvm: LLVMGetVisibility(asLLVM())) }
17+
set { LLVMSetVisibility(asLLVM(), newValue.llvm) }
18+
}
19+
}

0 commit comments

Comments
 (0)