diff --git a/Sources/LLVM/Alias.swift b/Sources/LLVM/Alias.swift index 9bd23d14..7f6eb7e2 100644 --- a/Sources/LLVM/Alias.swift +++ b/Sources/LLVM/Alias.swift @@ -2,7 +2,7 @@ import cllvm /// An `Alias` represents a global alias in an LLVM module - a new symbol and /// corresponding metadata for an existing position -public struct Alias: IRValue { +public struct Alias: IRGlobal { internal let llvm: LLVMValueRef /// Retrieves the underlying LLVM value object. diff --git a/Sources/LLVM/Function.swift b/Sources/LLVM/Function.swift index 4b5e618b..0988ae69 100644 --- a/Sources/LLVM/Function.swift +++ b/Sources/LLVM/Function.swift @@ -3,7 +3,7 @@ import cllvm /// A `Function` represents a named function body in LLVM IR source. Functions /// in LLVM IR encapsulate a list of parameters and a sequence of basic blocks /// and provide a way to append to that sequence to build out its body. -public class Function: IRValue { +public class Function: IRGlobal { internal let llvm: LLVMValueRef internal init(llvm: LLVMValueRef) { self.llvm = llvm @@ -107,18 +107,6 @@ public class Function: IRValue { LLVMDeleteFunction(llvm) } - /// Retrieves the linkage information for this function. - public var linkage: Linkage { - get { return Linkage(llvm: LLVMGetLinkage(asLLVM())) } - set { LLVMSetLinkage(asLLVM(), newValue.llvm) } - } - - /// Retrieves the visibility style for this function. - public var visibility: Visibility { - get { return Visibility(llvm: LLVMGetVisibility(asLLVM())) } - set { LLVMSetVisibility(asLLVM(), newValue.llvm) } - } - /// Retrieves the underlying LLVM value object. public func asLLVM() -> LLVMValueRef { return llvm diff --git a/Sources/LLVM/Global.swift b/Sources/LLVM/Global.swift index e373f6ed..e1a5c2e7 100644 --- a/Sources/LLVM/Global.swift +++ b/Sources/LLVM/Global.swift @@ -3,7 +3,7 @@ import cllvm /// A `Global` represents a region of memory allocated at compile time instead /// of at runtime. A global variable must either have an initializer, or make /// reference to an external definition that has an initializer. -public struct Global: IRValue { +public struct Global: IRGlobal { internal let llvm: LLVMValueRef /// Returns whether this global variable has no initializer because it makes @@ -33,18 +33,6 @@ public struct Global: IRValue { set { LLVMSetThreadLocal(asLLVM(), newValue.llvm) } } - /// Retrieves the linkage information for this global value. - public var linkage: Linkage { - get { return Linkage(llvm: LLVMGetLinkage(asLLVM())) } - set { LLVMSetLinkage(asLLVM(), newValue.llvm) } - } - - /// Retrieves the visibility style for this global value. - public var visibility: Visibility { - get { return Visibility(llvm: LLVMGetVisibility(asLLVM())) } - set { LLVMSetVisibility(asLLVM(), newValue.llvm) } - } - /// Deletes the global variable from its containing module. /// - note: This does not remove references to this global from the /// module. Ensure you have removed all insructions that reference diff --git a/Sources/LLVM/IRBuilder.swift b/Sources/LLVM/IRBuilder.swift index da556597..69dde326 100644 --- a/Sources/LLVM/IRBuilder.swift +++ b/Sources/LLVM/IRBuilder.swift @@ -1346,7 +1346,7 @@ public class IRBuilder { /// - parameter type: The type of the aliased value or expression. /// /// - returns: A value representing the newly created alias. - public func addAlias(name: String, to aliasee: IRValue, type: IRType) -> Alias { + public func addAlias(name: String, to aliasee: IRGlobal, type: IRType) -> Alias { return Alias(llvm: LLVMAddAlias(module.llvm, type.asLLVM(), aliasee.asLLVM(), name)) } diff --git a/Sources/LLVM/IRGlobal.swift b/Sources/LLVM/IRGlobal.swift new file mode 100644 index 00000000..66a01400 --- /dev/null +++ b/Sources/LLVM/IRGlobal.swift @@ -0,0 +1,19 @@ +import cllvm + +/// An `IRGlobal` is a value, alias, or function that exists at the top level of +/// an LLVM module. +public protocol IRGlobal: IRValue {} + +extension IRGlobal { + /// Retrieves the linkage information for this global. + public var linkage: Linkage { + get { return Linkage(llvm: LLVMGetLinkage(asLLVM())) } + set { LLVMSetLinkage(asLLVM(), newValue.llvm) } + } + + /// Retrieves the visibility style for this global. + public var visibility: Visibility { + get { return Visibility(llvm: LLVMGetVisibility(asLLVM())) } + set { LLVMSetVisibility(asLLVM(), newValue.llvm) } + } +}