@@ -195,17 +195,22 @@ end
195
195
end
196
196
identify (:: Type{Value} , :: Val{API.LLVMConstantStructValueKind} ) = ConstantStruct
197
197
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" ))
206
212
constants = Vector {Constant} ()
207
-
208
- for fieldname in fieldnames (typeof (value))
213
+ for fieldname in fieldnames (T)
209
214
field = getfield (value, fieldname)
210
215
211
216
if isa (field, Core. Bool)
@@ -220,25 +225,12 @@ function struct_to_constants(value, ctx::Context)
220
225
end
221
226
end
222
227
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
242
234
end
243
235
244
236
# vectors
0 commit comments