Skip to content

Commit

Permalink
Add partial UnionDecl support, fix #55
Browse files Browse the repository at this point in the history
tested against pthread.h. Works for primitive types, will complain (but proceed) for unions with struct sub-fields
  • Loading branch information
ihnorton committed May 13, 2014
1 parent 9ff1bb0 commit e74e868
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/wrap_c.jl
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,16 @@ end
# declare a block of bytes to match.
###############################################################################

typesize(cu::CLCursor) = typesize(cu_type(cu))
typesize(t::CLType) = sizeof(eval(cl_to_jl[typeof(t)]))
typesize(t::Record) = begin warn(" incorrect typesize for Record field"); 0 end
typesize(t::Unexposed) = being warn(" incorrect typesize for Unexposed field"); 0 end
typesize(t::ConstantArray) = cindex.getArraySize(t)

function largestfield(cu::UnionDecl)
maxsize,maxelem = 0
maxsize,maxelem = 0,0
fields = children(cu)
for i in 1:length(fields)
maxelem = ( (maxsize > (typesize(fields[i]))) ? maxelem : i )
maxelem = ( (maxsize > (typesize(cu_type(fields[i])))) ? maxelem : i )
end
fields[maxelem]
end
Expand Down Expand Up @@ -312,6 +314,20 @@ function wrap(context::WrapContext, buf::IO, sd::StructDecl; usename = "")
println(buf, e)
end

function wrap(context::WrapContext, buf::IO, ud::UnionDecl; usename = "")
if (usename == "" && (usename = name(ud)) == "")
warn("Skipping unnamed StructDecl")
return
end

b = Expr(:block)
e = Expr(:type, !context.options.immutable_structs, symbol(usename), b)
max_cu = largestfield(ud)
push!(b.args, Expr(:(::), symbol("_"*usename), repr_jl(cu_type(max_cu))))

println(buf, e)
end

function efunsig(name::Symbol, args::Vector{Symbol}, types)
x = { Expr(:(::), a, t) for (a,t) in zip(args,types) }
Expr(:call, name, x...)
Expand Down Expand Up @@ -463,7 +479,7 @@ function wrap(context::WrapContext, buf::IO, cursor::TypeRef; usename="")
end

function wrap(context::WrapContext, buf::IO, cursor; usename="")
#warn("Not wrapping $(typeof(cursor))")
warn("Not wrapping $(typeof(cursor)) $usename $(name(cursor))")
end


Expand Down

0 comments on commit e74e868

Please sign in to comment.