-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
79 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,74 @@ | ||
import CLLVM | ||
|
||
/// `PointerType` is used to specify memory locations. Pointers are commonly | ||
/// used to reference objects in memory. | ||
/// | ||
/// `PointerType` may have an optional address space attribute defining the | ||
/// numbered address space where the pointed-to object resides. The default | ||
/// address space is number zero. The semantics of non-zero address spaces are | ||
/// target-specific. | ||
/// | ||
/// Note that LLVM does not permit pointers to void `(void*)` nor does it permit | ||
/// pointers to labels `(label*)`. Use `ptr` instead. | ||
public struct PointerType: TypeRef { | ||
var llvm: LLVMTypeRef | ||
|
||
/// Returns the context associated with this type. | ||
public let context: Context? | ||
|
||
/// Retrieves the underlying LLVM type object. | ||
public var typeRef: LLVMTypeRef { llvm } | ||
|
||
/// Retrieves the type of the value being pointed to. | ||
public let pointee: TypeRef? | ||
|
||
/// Retrieves the address space where the pointed-to object resides. | ||
public let addressSpace: AddressSpace | ||
|
||
/// Create a `PointerType` pointer type that points to a defined type and an optional address space. | ||
/// The created type will exist in the context that its pointee type exists in . | ||
/// | ||
/// - parameter pointee: The type of the pointed-to object. | ||
/// - parameter addressSpace: The optional address space where the pointed-to object resides. | ||
/// - note: The context of this type is taken from it's pointee | ||
public init(pointee: TypeRef, addressSpace: AddressSpace = .zero) { | ||
if pointee is VoidType || pointee is LabelType { | ||
fatalError("Attempted to form pointer to unexpected Void or Label type - use pointer to IntType.int8 instead") | ||
} | ||
self.pointee = pointee | ||
self.addressSpace = addressSpace | ||
llvm = LLVMPointerType(pointee.typeRef, UInt32(addressSpace.rawValue)) | ||
context = nil | ||
} | ||
|
||
/// Create an opaque pointer type in a context. | ||
public init(in context: Context, addressSpace: AddressSpace = .zero) { | ||
pointee = nil | ||
self.addressSpace = addressSpace | ||
llvm = LLVMPointerTypeInContext(context.contextRef, UInt32(addressSpace.rawValue)) | ||
self.context = context | ||
} | ||
|
||
/// Determine whether a pointer is opaque. | ||
/// True if this is an instance of an opaque PointerType. | ||
public static func getPointerTypeIsOpaque(ty: TypeRef) -> Bool { | ||
LLVMPointerTypeIsOpaque(ty.typeRef) != 0 | ||
} | ||
|
||
/// Get the address space of a pointer type. | ||
/// This only works on types that represent pointers. | ||
public static func getPointerAddressSpace(ty: TypeRef) -> AddressSpace { | ||
AddressSpace(LLVMGetPointerAddressSpace(ty.typeRef)) | ||
} | ||
|
||
/// Get the element type of an Pointer type. | ||
public static func getElementType(ty: TypeRef) -> TypeRef { | ||
Types(typeRef: LLVMGetElementType(ty.typeRef)!) | ||
} | ||
} | ||
|
||
extension PointerType: Equatable { | ||
public static func == (lhs: PointerType, rhs: PointerType) -> Bool { | ||
return lhs.typeRef == rhs.typeRef | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters