Skip to content

Commit 86c3973

Browse files
committed
Extend: CompositeConstant
1 parent 21f4aef commit 86c3973

File tree

1 file changed

+38
-22
lines changed

1 file changed

+38
-22
lines changed

llvm-api/LLVM/Core/Values/Constants/Composite.swift

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -105,26 +105,42 @@ public enum CompositeConstant {
105105
return Value(llvm: valueRef)
106106
}
107107

108-
/**
109-
* Create a non-anonymous ConstantStruct from values.
110-
*/
111-
// LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy, LLVMValueRef *ConstantVals, unsigned Count);
112-
113-
/**
114-
* Get element of a constant aggregate (struct, array or vector) at the
115-
* specified index. Returns null if the index is out of range, or it's not
116-
* possible to determine the element (e.g., because the constant is a
117-
* constant expression.)
118-
*/
119-
// LLVMValueRef LLVMGetAggregateElement(LLVMValueRef C, unsigned Idx);
120-
121-
/**
122-
* Get an element at specified index as a constant.
123-
*/
124-
// LLVM_ATTRIBUTE_C_DEPRECATED( LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef C, unsigned idx), "Use LLVMGetAggregateElement instead");
125-
126-
/**
127-
* Create a ConstantVector from values.
128-
*/
129-
// LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size);
108+
/// Create a non-anonymous `ConstantStruct` from values.
109+
public static func constNamedStruct(structType: TypeRef, constantValues: [ValueRef]) -> Value? {
110+
let count = UInt32(constantValues.count)
111+
let values = UnsafeMutablePointer<LLVMValueRef?>.allocate(capacity: Int(count))
112+
defer {
113+
values.deallocate()
114+
}
115+
116+
for (index, value) in constantValues.enumerated() {
117+
values[index] = value.valueRef
118+
}
119+
guard let valueRef = LLVMConstNamedStruct(structType.typeRef, values, count) else { return nil }
120+
return Value(llvm: valueRef)
121+
}
122+
123+
/// Get element of a constant aggregate (struct, array or vector) at the
124+
/// specified index. Returns null if the index is out of range, or it's not
125+
/// possible to determine the element (e.g., because the constant is a
126+
/// constant expression.)
127+
public static func getAggregateElement(aggregate: ValueRef, index: UInt32) -> Value? {
128+
guard let valueRef = LLVMGetAggregateElement(aggregate.valueRef, index) else { return nil }
129+
return Value(llvm: valueRef)
130+
}
131+
132+
/// Create a ConstantVector from values.
133+
public static func constVector(scalarConstantValues: [ValueRef]) -> Value? {
134+
let size = UInt32(scalarConstantValues.count)
135+
let values = UnsafeMutablePointer<LLVMValueRef?>.allocate(capacity: Int(size))
136+
defer {
137+
values.deallocate()
138+
}
139+
140+
for (index, value) in scalarConstantValues.enumerated() {
141+
values[index] = value.valueRef
142+
}
143+
guard let valueRef = LLVMConstVector(values, size) else { return nil }
144+
return Value(llvm: valueRef)
145+
}
130146
}

0 commit comments

Comments
 (0)