Skip to content

Commit

Permalink
GI: clean up const generation
Browse files Browse the repository at this point in the history
  • Loading branch information
jwahlstrand committed Mar 31, 2024
1 parent 32129a7 commit acfdabc
Show file tree
Hide file tree
Showing 14 changed files with 176 additions and 289 deletions.
84 changes: 41 additions & 43 deletions GI/src/giexport.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# functions that output expressions for a library in bulk

## Constants, enums, and flags
function _enums_and_flags(es, skiplist, incl_typeinit, const_mod, const_exports, loaded)
for e in es
name = Symbol(get_name(e))
Expand Down Expand Up @@ -39,20 +40,46 @@ function all_const_exprs!(const_mod, const_exports, ns;print_summary=true,incl_t
loaded
end

function export_consts!(ns,path,prefix,skiplist = Symbol[]; doc_prefix = prefix, doc_xml = nothing, export_constants = true)
toplevel, exprs, exports = GI.output_exprs()

const_mod = Expr(:block)

c = all_const_exprs!(const_mod, exports, ns; skiplist= skiplist)
if doc_xml !== nothing
GI.append_const_docs!(const_mod.args, doc_prefix, doc_xml, c)
end
export_constants && push!(const_mod.args, exports)

push!(exprs, const_mod)

## export constants, enums, and flags code
GI.write_to_file(path,"$(prefix)_consts",toplevel)
end

## Structs

function struct_cache_expr!(exprs)
gboxed_types_list = quote
gboxed_types = Any[]
end
push!(exprs,unblock(gboxed_types_list))
end

function get_non_skipped(ns, t, skiplist, exclude_deprecated)
l=get_all(ns,t,exclude_deprecated)
filter!(l) do o
!in(get_name(o), skiplist)
end
l
end

function struct_exprs!(exprs,exports,ns,structs=nothing;print_summary=true,excludelist=[],constructor_skiplist=[],import_as_opaque=[],output_cache_init=true,only_opaque=false,exclude_deprecated=true)
struct_skiplist=excludelist

if structs === nothing
s=get_all(ns,GIStructInfo,exclude_deprecated)
structinfos=filter(p->(get_name(p),struct_skiplist),s)
structs = get_name.(structinfos)
s=get_non_skipped(ns,GIStructInfo,struct_skiplist,exclude_deprecated)
structs = get_name.(s)
end

imported=length(structs)
Expand Down Expand Up @@ -112,8 +139,7 @@ function all_struct_exprs!(exprs,exports,ns;print_summary=true,excludelist=[],co
struct_skiplist=excludelist
loaded=Symbol[]

s=get_all(ns,GIStructInfo,exclude_deprecated)
ss=filter(p->(get_name(p),struct_skiplist),s)
ss=get_non_skipped(ns,GIStructInfo,struct_skiplist,exclude_deprecated)
imported=length(ss)
for ssi in ss
name=get_name(ssi)
Expand Down Expand Up @@ -153,7 +179,7 @@ function all_struct_exprs!(exprs,exports,ns;print_summary=true,excludelist=[],co
end

if print_summary
printstyled("Generated ",imported," structs out of ",length(s),"\n";color=:green)
printstyled("Generated ",imported," structs out of ",length(ss),"\n";color=:green)
end

struct_skiplist, loaded
Expand Down Expand Up @@ -230,7 +256,7 @@ function all_struct_methods!(exprs,ns;print_summary=true,print_detailed=false,sk
end

function all_objects!(exprs,exports,ns;print_summary=true,handled=Symbol[],skiplist=Symbol[],constructor_skiplist=[],output_cache_define=true,output_cache_init=true, exclude_deprecated=true)
objects=get_all(ns,GIObjectInfo,exclude_deprecated)
objects=get_non_skipped(ns,GIObjectInfo,skiplist,exclude_deprecated)

imported=length(objects)
loaded=Symbol[]
Expand All @@ -248,10 +274,6 @@ function all_objects!(exprs,exports,ns;print_summary=true,handled=Symbol[],skipl
imported -= 1
continue
end
if in(name, skiplist)
imported -= 1
continue
end
if get_type_init(o)==:intern # GParamSpec and children output this
continue
end
Expand All @@ -269,7 +291,6 @@ function all_objects!(exprs,exports,ns;print_summary=true,handled=Symbol[],skipl
push!(exprs,gtype_cache_init)
end
for o in objects
in(get_name(o), skiplist) && continue
constructors = get_constructors(o;skiplist=constructor_skiplist, struct_skiplist=skiplist, exclude_deprecated=exclude_deprecated)
isempty(constructors) || append!(exprs,constructors)
end
Expand All @@ -283,14 +304,10 @@ function all_object_methods!(exprs,ns;skiplist=Symbol[],object_skiplist=Symbol[]
not_implemented=0
skipped=0
created=0
objects=get_all(ns,GIObjectInfo,exclude_deprecated)
objects=get_non_skipped(ns,GIObjectInfo,object_skiplist,exclude_deprecated)
for o in objects
name=get_name(o)
methods=get_methods(o)
if in(name,object_skiplist)
skipped+=length(methods)
continue
end
for m in methods
if in(get_name(m),skiplist)
skipped+=1
Expand Down Expand Up @@ -326,14 +343,8 @@ end

function all_object_signals!(exprs,ns;skiplist=Symbol[],object_skiplist=Symbol[], liboverride=nothing, exclude_deprecated=true)
not_implemented=0
skipped=0
created=0
objects=get_all(ns,GIObjectInfo,exclude_deprecated)
objects=get_non_skipped(ns,GIObjectInfo,object_skiplist,exclude_deprecated)
for o in objects
name=get_name(o)
if in(name,object_skiplist)
continue
end
signals = get_all_signals(o)
for s in signals
(exclude_deprecated && is_deprecated(s)) && continue
Expand All @@ -343,43 +354,31 @@ function all_object_signals!(exprs,ns;skiplist=Symbol[],object_skiplist=Symbol[]
end

function all_interfaces!(exprs,exports,ns;print_summary=true,skiplist=Symbol[],exclude_deprecated=true)
interfaces=get_all(ns,GIInterfaceInfo,exclude_deprecated)
interfaces=get_non_skipped(ns,GIInterfaceInfo,skiplist,exclude_deprecated)

imported=length(interfaces)
for i in interfaces
name=get_name(i)
# could use the following to narrow the type
#p=get_prerequisites(i)
#type_init = get_type_init(i)
if in(name,skiplist)
imported-=1
continue
end
push!(exprs,decl(i))
push!(exports.args, get_full_name(i))
end

if print_summary && imported>0
printstyled("Created ",imported," interfaces out of ",length(interfaces),"\n";color=:green)
if print_summary && length(interfaces)>0
printstyled("Created ",length(interfaces)," interfaces\n";color=:green)
end
skiplist
end

function all_interface_methods!(exprs,ns;skiplist=Symbol[],interface_skiplist=Symbol[], liboverride=nothing,exclude_deprecated=true)
not_implemented=0
skipped=0
created=0
interfaces=get_all(ns,GIInterfaceInfo,exclude_deprecated)
interfaces=get_non_skipped(ns,GIInterfaceInfo,interface_skiplist,exclude_deprecated)
for i in interfaces
name=get_name(i)
methods=get_methods(i)
if in(name,interface_skiplist)
skipped+=length(methods)
continue
end
for m in methods
if in(get_name(m),skiplist)
skipped+=1
continue
end
(exclude_deprecated && is_deprecated(m)) && continue
Expand All @@ -403,9 +402,8 @@ function all_functions!(exprs,ns;print_summary=true,skiplist=Symbol[],symbol_ski
j=0
skipped=0
not_implemented=0
for i in get_all(ns,GIFunctionInfo,exclude_deprecated)
if in(get_name(i),skiplist) || occursin("cclosure",string(get_name(i)))
skipped+=1
for i in get_non_skipped(ns,GIFunctionInfo,skiplist,exclude_deprecated)
if occursin("cclosure",string(get_name(i)))
continue
end
if in(get_symbol(i),symbol_skiplist) # quietly drop methods already handled
Expand Down
30 changes: 9 additions & 21 deletions GI/src/giimport.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,14 @@ function decl(enum::GIEnumInfo, incl_typeinit=true)
enumname=get_name(enum)
vals = get_enum_values(enum)
typ = typetag_primitive[get_storage_type(enum)]
body = Expr(:macrocall)
push!(body.args, Symbol("@cenum"))
push!(body.args, Symbol("nothing"))
push!(body.args, :($enumname::$typ))
body = Expr(:macrocall, Symbol("@cenum"), Symbol("nothing"), :($enumname::$typ))
for (name,val) in vals
val=unsafe_trunc(typ,val) # sometimes the value returned by GI is outside the range of the enum's type
fullname=enum_fullname(enumname,name)
push!(body.args, :($fullname = $val) )
end
bloc = Expr(:block)
push!(bloc.args,unblock(body))
# if this enum has a GType we need to define GLib.g_type() to support storing this in GValue
bloc = Expr(:block, unblock(body))
# if this enum has a GType we define GLib.g_type() to support storing this in GValue
if incl_typeinit
gtypeinit = typeinit_def(enum)
gtypeinit !== nothing && push!(bloc.args,unblock(gtypeinit))
Expand All @@ -73,10 +69,7 @@ function decl(enum::GIFlagsInfo, incl_typeinit=true)
enumname=get_name(enum)
vals = get_enum_values(enum)
typ = typetag_primitive[get_storage_type(enum)]
body = Expr(:macrocall)
push!(body.args, Symbol("@bitflag"))
push!(body.args, Symbol("nothing"))
push!(body.args, :($enumname::UInt32))
body = Expr(:macrocall, Symbol("@bitflag"), Symbol("nothing"), :($enumname::UInt32))
seen=UInt32[]
for (name,val) in vals
val=unsafe_trunc(typ,val) # sometimes the value returned by GI is outside the range of the enum's type
Expand All @@ -90,8 +83,7 @@ function decl(enum::GIFlagsInfo, incl_typeinit=true)
fullname=enum_fullname(enumname,"NONE")
push!(body.args, :($fullname = 0))
end
bloc = Expr(:block)
push!(bloc.args,body)
bloc = Expr(:block, body)
if incl_typeinit
gtypeinit = typeinit_def(enum)
push!(bloc.args,unblock(gtypeinit))
Expand Down Expand Up @@ -410,7 +402,6 @@ end
## Callback output
function decl(callbackinfo::GICallbackInfo)
name = get_full_name(callbackinfo)
fargs = Symbol[]
args=get_args(callbackinfo)
rettypeinfo=get_return_type(callbackinfo)
rettype = extract_type(rettypeinfo)
Expand All @@ -434,16 +425,16 @@ function decl(callbackinfo::GICallbackInfo)
end

input_conversion=[]
for arg in args
fargs = map(args) do arg
argname = get_name(arg)
push!(fargs, argname)
if argname != closure_name
typ = extract_type(arg)
expr = convert_from_c(argname,arg,typ)
if expr!==nothing
push!(input_conversion,:($argname = $expr))
end
end
return argname
end
d = quote
function $name($(fargs...))
Expand All @@ -462,13 +453,10 @@ function get_ret_type(signalinfo::GISignalInfo)
end

function get_arg_types(signalinfo::GISignalInfo)
args=get_args(signalinfo)
argctypes_arr=[]
for arg in args
map(get_args(signalinfo)) do arg
argtype = extract_type(arg)
push!(argctypes_arr, argtype.ctype)
argtype.ctype
end
argctypes_arr
end

## Signal output
Expand Down
15 changes: 1 addition & 14 deletions gen/gen_cairo.jl
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
using GI

toplevel, exprs, exports = GI.output_exprs()

ns = GINamespace(:cairo,"1.0")
path="../src/gen"

## constants, enums, and flags, put in a "Constants" submodule

const_mod = Expr(:block)

const_exports = Expr(:export)

GI.all_const_exprs!(const_mod, const_exports, ns)

push!(exprs, const_mod)

## export constants, enums, and flags code
GI.write_to_file(path,"cairo_consts",toplevel)
GI.export_consts!(ns, path, "cairo"; doc_xml = d, export_constants = false)

## structs

Expand Down
17 changes: 1 addition & 16 deletions gen/gen_gdk4.jl
Original file line number Diff line number Diff line change
@@ -1,26 +1,11 @@
using GI

toplevel, exprs, exports = GI.output_exprs()

path="../src/gen"

ns = GINamespace(:Gdk,"4.0")
d = readxml("/usr/share/gir-1.0/$(GI.ns_id(ns)).gir")

## constants, enums, and flags, put in a "Constants" submodule

const_mod = Expr(:block)

const_exports = Expr(:export)

c = GI.all_const_exprs!(const_mod, const_exports, ns)
GI.append_const_docs!(const_mod.args, "gdk4", d, c)
push!(const_mod.args, const_exports)

push!(exprs, const_mod)

## export constants, enums, and flags code
GI.write_to_file(path,"gdk4_consts",toplevel)
GI.export_consts!(ns, path, "gdk4"; doc_xml = d)

## structs

Expand Down
16 changes: 1 addition & 15 deletions gen/gen_gdkpixbuf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,11 @@ using GI

printstyled("Generating code for GdkPixbuf\n";bold=true)

toplevel, exprs, exports = GI.output_exprs()

ns = GINamespace(:GdkPixbuf,"2.0")
d = GI.read_gir(gdk_pixbuf_jll, ns)
path="../src/gen"

## constants, enums, and flags, put in a "Constants" submodule

const_mod = Expr(:block)

const_exports = Expr(:export)

c = GI.all_const_exprs!(const_mod, const_exports, ns, skiplist=[:PixbufFormatFlags])
GI.append_const_docs!(const_mod.args, "gdk-pixbuf", d, c)

push!(exprs, const_mod)

## export constants, enums, and flags code
GI.write_to_file(path,"gdkpixbuf_consts",toplevel)
GI.export_consts!(ns, path, "gdkpixbuf", [:PixbufFormatFlags]; doc_prefix = "gdk-pixbuf", doc_xml = d, export_constants = false)

## structs

Expand Down
15 changes: 1 addition & 14 deletions gen/gen_graphene.jl
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
using GI

toplevel, exprs, exports = GI.output_exprs()

path="../src/gen"

ns = GINamespace(:Graphene,"1.0")

## constants, enums, and flags, put in a "Constants" submodule

const_mod = Expr(:block)

const_exports = Expr(:export)

GI.all_const_exprs!(const_mod, const_exports, ns; incl_typeinit=false)

push!(exprs, const_mod)

## export constants, enums, and flags code
GI.write_to_file(path,"graphene_consts",toplevel)
GI.export_consts!(ns, path, "graphene"; export_constants = false)

## structs

Expand Down
Loading

0 comments on commit acfdabc

Please sign in to comment.