Skip to content

Commit cf331e2

Browse files
authored
Merge pull request #29 from CodaFi/hypervisor
Add Visibility information accessors
2 parents 30eb3bf + 3966d14 commit cf331e2

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

Sources/LLVM/Function.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ public class Function: IRValue {
113113
set { LLVMSetLinkage(asLLVM(), newValue.llvm) }
114114
}
115115

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+
116122
/// Retrieves the underlying LLVM value object.
117123
public func asLLVM() -> LLVMValueRef {
118124
return llvm

Sources/LLVM/Global.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ public struct Global: IRValue {
3939
set { LLVMSetLinkage(asLLVM(), newValue.llvm) }
4040
}
4141

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+
4248
/// Deletes the global variable from its containing module.
4349
/// - note: This does not remove references to this global from the
4450
/// module. Ensure you have removed all insructions that reference

Sources/LLVM/Linkage.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,46 @@
11
import cllvm
22

3+
/// `Visibility` enumerates available visibility styles.
4+
public enum Visibility {
5+
/// On targets that use the ELF object file format, default visibility means
6+
/// that the declaration is visible to other modules and, in shared libraries,
7+
/// means that the declared entity may be overridden. On Darwin, default
8+
/// visibility means that the declaration is visible to other modules. Default
9+
/// visibility corresponds to "external linkage" in the language.
10+
case `default`
11+
/// Two declarations of an object with hidden visibility refer to the same
12+
/// object if they are in the same shared object. Usually, hidden visibility
13+
/// indicates that the symbol will not be placed into the dynamic symbol
14+
/// table, so no other module (executable or shared library) can reference it
15+
/// directly.
16+
case hidden
17+
/// On ELF, protected visibility indicates that the symbol will be placed in
18+
/// the dynamic symbol table, but that references within the defining module
19+
/// will bind to the local symbol. That is, the symbol cannot be overridden by
20+
/// another module.
21+
case protected
22+
23+
static let visibilityMapping: [Visibility: LLVMVisibility] = [
24+
.default: LLVMDefaultVisibility,
25+
.hidden: LLVMHiddenVisibility,
26+
.protected: LLVMProtectedVisibility,
27+
]
28+
29+
internal init(llvm: LLVMVisibility) {
30+
switch llvm {
31+
case LLVMDefaultVisibility: self = .default
32+
case LLVMHiddenVisibility: self = .hidden
33+
case LLVMProtectedVisibility: self = .protected
34+
default: fatalError("unknown visibility type \(llvm)")
35+
}
36+
}
37+
38+
/// Retrieves the corresponding `LLVMLinkage`.
39+
public var llvm: LLVMVisibility {
40+
return Visibility.visibilityMapping[self]!
41+
}
42+
}
43+
344
/// `Linkage` enumerates the supported kinds of linkage for global values. All
445
/// global variables and functions have a linkage.
546
public enum Linkage {

0 commit comments

Comments
 (0)