Skip to content

Add Visibility information accessors #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Sources/LLVM/Function.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ public class Function: IRValue {
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
Expand Down
6 changes: 6 additions & 0 deletions Sources/LLVM/Global.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public struct Global: IRValue {
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
Expand Down
41 changes: 41 additions & 0 deletions Sources/LLVM/Linkage.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,46 @@
import cllvm

/// `Visibility` enumerates available visibility styles.
public enum Visibility {
/// On targets that use the ELF object file format, default visibility means
/// that the declaration is visible to other modules and, in shared libraries,
/// means that the declared entity may be overridden. On Darwin, default
/// visibility means that the declaration is visible to other modules. Default
/// visibility corresponds to "external linkage" in the language.
case `default`
/// Two declarations of an object with hidden visibility refer to the same
/// object if they are in the same shared object. Usually, hidden visibility
/// indicates that the symbol will not be placed into the dynamic symbol
/// table, so no other module (executable or shared library) can reference it
/// directly.
case hidden
/// On ELF, protected visibility indicates that the symbol will be placed in
/// the dynamic symbol table, but that references within the defining module
/// will bind to the local symbol. That is, the symbol cannot be overridden by
/// another module.
case protected

static let visibilityMapping: [Visibility: LLVMVisibility] = [
.default: LLVMDefaultVisibility,
.hidden: LLVMHiddenVisibility,
.protected: LLVMProtectedVisibility,
]

internal init(llvm: LLVMVisibility) {
switch llvm {
case LLVMDefaultVisibility: self = .default
case LLVMHiddenVisibility: self = .hidden
case LLVMProtectedVisibility: self = .protected
default: fatalError("unknown visibility type \(llvm)")
}
}

/// Retrieves the corresponding `LLVMLinkage`.
public var llvm: LLVMVisibility {
return Visibility.visibilityMapping[self]!
}
}

/// `Linkage` enumerates the supported kinds of linkage for global values. All
/// global variables and functions have a linkage.
public enum Linkage {
Expand Down