Skip to content

Wrap more parts of the LLVM API #85

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
Apr 3, 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/BasicBlock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ public struct BasicBlock: IRValue {
return BasicBlock(llvm: blockRef)
}

/// Returns the basic block before this basic block, if it exists.
public func previous() -> BasicBlock? {
guard let blockRef = LLVMGetPreviousBasicBlock(llvm) else { return nil }
return BasicBlock(llvm: blockRef)
}

/// Returns a sequence of the Instructions that make up this basic block.
public var instructions: AnySequence<Instruction> {
var current = firstInstruction
Expand Down
7 changes: 7 additions & 0 deletions Sources/LLVM/Instruction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ public struct Instruction: IRValue {
return Instruction(llvm: val)
}

/// Retrieves the parent basic block that contains this instruction, if it
/// exists.
public var parentBlock: BasicBlock? {
guard let parent = LLVMGetInstructionParent(self.llvm) else { return nil }
return BasicBlock(llvm: parent)
}

/// Retrieves the first use of this instruction.
public var firstUse: Use? {
guard let use = LLVMGetFirstUse(llvm) else { return nil }
Expand Down
21 changes: 21 additions & 0 deletions Sources/LLVM/StructType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,27 @@ public struct StructType: IRType {
}
}

/// Retrieves the name associated with this structure type, or the empty
/// string if this type is unnamed.
public var name: String {
guard let sname = LLVMGetStructName(self.llvm) else { return "" }
return String(cString: sname)
}

/// Retrieves the element types associated with this structure type.
public var elementTypes: [IRType] {
var params = [IRType]()
let count = Int(LLVMCountStructElementTypes(self.llvm))
let paramsPtr = UnsafeMutablePointer<LLVMTypeRef?>.allocate(capacity: count)
defer { free(paramsPtr) }
LLVMGetStructElementTypes(self.llvm, paramsPtr)
for i in 0..<count {
let ty = paramsPtr[i]!
params.append(convertType(ty))
}
return params
}

/// Retrieves the underlying LLVM type object.
public func asLLVM() -> LLVMTypeRef {
return llvm
Expand Down