@@ -2,8 +2,8 @@ import JSON3
22using Clang. Generators
33using Clang. JLLEnvs
44using CImGuiPack_jll
5- import JuliaFormatter: format_file, format_text
6- import MacroTools: @capture , splitdef, prettify
5+ using JuliaFormatter: format_file, format_text
6+ using MacroTools: @capture , splitdef, prettify, postwalk
77
88
99"""
@@ -319,15 +319,8 @@ function wrap_function!(methods, func_name, func_def, overloads; with_arg_types=
319319 arg_types_strs = [func_metadata[:argsT ][i][:type ] for i in eachindex (arg_names)]
320320
321321 args = copy (arg_names)
322- is_pOut = false
323322
324323 for i in eachindex (args)
325- # pOut arguments are part of cimgui and are a pointer to a return value,
326- # to be filled in by the cimgui function.
327- if arg_names[i] == :pOut
328- is_pOut = true
329- end
330-
331324 arg_type_str = arg_types_strs[i]
332325
333326 # If this is the `self` argument and this function belongs to a struct,
@@ -384,35 +377,11 @@ function wrap_function!(methods, func_name, func_def, overloads; with_arg_types=
384377 arg_names[i] = :(length ($ (arg_names[i - 1 ])))
385378 end
386379
387- func_expr = if is_pOut
388- ccall_info = split_ccall (func_def[:body ])
389- local pOut_type
390- if ! @capture (ccall_info. argtypes[1 ], Ptr{pOut_type_})
391- @warn " Skipping '$func_name ', couldn't get type of the `pOut` argument"
392- return
393- end
394-
395- popfirst! (args)
396- popfirst! (arg_names)
397-
398- # Special-case HSV() because it should return an ImVec4 instead of an ImColor
399- return_expr = if func_name === :ImColor_HSV
400- :(pOut[]. Value)
401- else
402- :(pOut[])
403- end
404-
405- quote
406- function $new_identifier ($ (args... ))
407- pOut = Ref {$pOut_type} ()
408- $ func_name (pOut, $ (arg_names... ))
409- return $ return_expr
410- end
411- end
380+ # Special-case HSV() because it should return an ImVec4 instead of an ImColor
381+ func_expr = if func_name === :ImColor_HSV
382+ :($ new_identifier ($ (args... )) = $ func_name ($ (arg_names... )). Value)
412383 else
413- quote
414- $ new_identifier ($ (args... )) = $ func_name ($ (arg_names... ))
415- end
384+ :($ new_identifier ($ (args... )) = $ func_name ($ (arg_names... )))
416385 end
417386
418387 docstring = create_docstring (func_name, func_metadata)
@@ -511,6 +480,31 @@ function get_wrappers(dag::ExprDAG)
511480 return methods
512481end
513482
483+ function rewrite! (dag)
484+ # In newer versions of the cimgui bindings non-POD-types-that-look-like-POD-types-but-actually-aren't
485+ # are renamed to have a '_c' underscore. In the generated Julia bindings we
486+ # rename these back to their old names for the sake of convenience.
487+ # See: https://github.com/cimgui/cimgui/issues/309
488+ new2old_names = Dict {Symbol, Symbol} ()
489+ for file in (:cimgui_structs_and_enums , :cimplot_structs_and_enums , :cimnodes_structs_and_enums )
490+ structs_and_enums = JSON3. read (getproperty (CImGuiPack_jll, file))
491+ nonpod_used = structs_and_enums[:nonPOD_used ]
492+ merge! (new2old_names, Dict ([Symbol (x, " _c" ) => x for x in keys (nonpod_used)]))
493+ end
494+
495+ for node in dag. nodes
496+ for i in eachindex (node. exprs)
497+ node. exprs[i] = postwalk (node. exprs[i]) do x
498+ if x isa Symbol && x in keys (new2old_names)
499+ new2old_names[x]
500+ else
501+ x
502+ end
503+ end
504+ end
505+ end
506+ end
507+
514508function generate ()
515509 cd (@__DIR__ ) do
516510 include_dir = joinpath (CImGuiPack_jll. artifact_dir, " include" )
@@ -541,7 +535,9 @@ function generate()
541535 " -includestdbool.h" )
542536
543537 ctx = create_context ([cimgui_h, cimplot_h, cimnodes_h, cimgui_impl_h], args, options)
544- build! (ctx)
538+ build! (ctx, BUILDSTAGE_NO_PRINTING)
539+ rewrite! (ctx. dag)
540+ build! (ctx, BUILDSTAGE_PRINTING_ONLY)
545541 end
546542
547543 println ()
0 commit comments