Skip to content

Commit

Permalink
Merge pull request #85 from CodaFi/i-am-not-a-wrapper
Browse files Browse the repository at this point in the history
Wrap more parts of the LLVM API
  • Loading branch information
CodaFi authored Apr 3, 2017
2 parents 9ab928a + 7a1d4a4 commit 0a357ec
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
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

0 comments on commit 0a357ec

Please sign in to comment.