Skip to content

Commit 67d7923

Browse files
committed
Rework ConstantStruct.
Simplify constructors, make it easier to construct (non)anonymous structs from Julia values.
1 parent 69c214b commit 67d7923

File tree

1 file changed

+21
-29
lines changed

1 file changed

+21
-29
lines changed

src/core/value/constant.jl

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -195,17 +195,22 @@ end
195195
end
196196
identify(::Type{Value}, ::Val{API.LLVMConstantStructValueKind}) = ConstantStruct
197197

198-
ConstantStruct(constant_vals::Vector{T}, packed::Core.Bool=false) where {T<:Constant} =
199-
ConstantStruct(API.LLVMConstStruct(constant_vals, length(constant_vals), convert(Bool, packed)))
200-
ConstantStruct(constant_vals::Vector{T}, ctx::Context, packed::Core.Bool=false) where {T<:Constant} =
201-
ConstantStruct(API.LLVMConstStructInContext(ctx, constant_vals, length(constant_vals), convert(Bool, packed)))
202-
ConstantStruct(constant_vals::Vector{T}, typ::LLVMType) where {T<:Constant} =
203-
ConstantStruct(API.LLVMConstNamedStruct(typ, constant_vals, length(constant_vals)))
204-
205-
function struct_to_constants(value, ctx::Context)
198+
# anonymous
199+
ConstantStruct(values::Vector{<:Constant}; packed::Core.Bool=false) =
200+
ConstantStruct(API.LLVMConstStruct(values, length(values), convert(Bool, packed)))
201+
ConstantStruct(values::Vector{<:Constant}, ctx::Context; packed::Core.Bool=false) =
202+
ConstantStruct(API.LLVMConstStructInContext(ctx, values, length(values), convert(Bool, packed)))
203+
204+
# named
205+
ConstantStruct(typ::StructType, values::Vector{<:Constant}) =
206+
ConstantStruct(API.LLVMConstNamedStruct(typ, values, length(values)))
207+
208+
# create a ConstantStruct from a Julia object
209+
function ConstantStruct(value::T, ctx::Context=GlobalContext();
210+
anonymous::Core.Bool=false, packed::Core.Bool=false) where {T}
211+
isbitstype(T) || throw(ArgumentError("Can only create a ConstantStruct from an isbits struct"))
206212
constants = Vector{Constant}()
207-
208-
for fieldname in fieldnames(typeof(value))
213+
for fieldname in fieldnames(T)
209214
field = getfield(value, fieldname)
210215

211216
if isa(field, Core.Bool)
@@ -220,25 +225,12 @@ function struct_to_constants(value, ctx::Context)
220225
end
221226
end
222227

223-
return constants
224-
end
225-
226-
function ConstantStruct(value, packed::Core.Bool=false)
227-
isbits(value) || throw(ArgumentError("`value` must be isbits"))
228-
constants = struct_to_constants(value, GlobalContext())
229-
return ConstantStruct(constants, packed)
230-
end
231-
232-
function ConstantStruct(value, ctx::Context, packed::Core.Bool=false)
233-
isbits(value) || throw(ArgumentError("`value` must be isbits"))
234-
constants = struct_to_constants(value, ctx)
235-
return ConstantStruct(constants, ctx, packed)
236-
end
237-
238-
function ConstantStruct(value, typ::LLVMType)
239-
isbits(value) || throw(ArgumentError("`value` must be isbits"))
240-
constants = struct_to_constants(value, context(typ))
241-
return ConstantStruct(constants, typ)
228+
if anonymous
229+
ConstantStruct(constants, ctx; packed=packed)
230+
else
231+
typ = StructType(String(nameof(T)), ctx)
232+
ConstantStruct(typ, constants)
233+
end
242234
end
243235

244236
# vectors

0 commit comments

Comments
 (0)