Skip to content

Commit 6bc705a

Browse files
Expose LLVMBuildStructGEP2 from the C API
Version 2 takes an explicit type parameter. This avoids a segmentation fault that happens when the pointer does not have an explicit type in LLVM
1 parent 7c14d64 commit 6bc705a

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Sources/LLVM/IRBuilder.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,22 @@ extension IRBuilder {
11531153
return LLVMBuildStructGEP(llvm, ptr.asLLVM(), UInt32(index), name)
11541154
}
11551155

1156+
/// Build a GEP (Get Element Pointer) instruction suitable for indexing into
1157+
/// a struct. This uses LLVMBuildStructGEP2 from cllvm
1158+
///
1159+
/// - parameter ptr: The base address for the index calculation.
1160+
/// - parameter type: The type of the struct to index into. If nil is specified, the
1161+
/// type property of the pointer is used.
1162+
/// - parameter index: The offset from the base for the index calculation.
1163+
/// - parameter name: The name for the newly inserted instruction.
1164+
///
1165+
/// - returns: A value representing the address of a subelement of the given
1166+
/// struct value.
1167+
public func buildStructGEP2(_ ptr: IRValue, type: IRType? = nil, index: Int, name: String = "") -> IRValue {
1168+
let structType = type ?? ptr.type
1169+
return LLVMBuildStructGEP2(llvm, structType.asLLVM(), ptr.asLLVM(), UInt32(index), name)
1170+
}
1171+
11561172
/// Build an ExtractValue instruction to retrieve an indexed value from a
11571173
/// struct or array value.
11581174
///

0 commit comments

Comments
 (0)