|
1 | 1 | module ArgDecomposition |
2 | 2 |
|
3 | | -# Write your package code here. |
| 3 | +export flatten_to_tuple, reassemble_tuple |
| 4 | + |
| 5 | +struct StaticType{T} end |
| 6 | +gettype(::StaticType{T}) where {T} = T |
| 7 | + |
| 8 | +function _append_fields!(t::Expr, body::Expr, sym::Symbol, ::Type{T}) where {T} |
| 9 | + for f ∈ 1:fieldcount(T) |
| 10 | + TF = fieldtype(T, f) |
| 11 | + Base.issingletontype(TF) && continue |
| 12 | + gfcall = Expr(:call, getfield, sym, f) |
| 13 | + if fieldcount(TF) ≡ 0 |
| 14 | + push!(t.args, gfcall) |
| 15 | + elseif TF <: DataType |
| 16 | + push!(t.args, :(StaticType{$gfcall}())) |
| 17 | + else |
| 18 | + newsym = gensym(sym) |
| 19 | + push!(body.args, Expr(:(=), newsym, gfcall)) |
| 20 | + _append_fields!(t, body, newsym, TF) |
| 21 | + end |
| 22 | + end |
| 23 | + return nothing |
| 24 | +end |
| 25 | +@generated function flatten_to_tuple(r::T) where {T} |
| 26 | + body = Expr(:block, Expr(:meta,:inline)) |
| 27 | + t = Expr(:tuple) |
| 28 | + if Base.issingletontype(T) |
| 29 | + nothing |
| 30 | + elseif fieldcount(T) ≡ 0 |
| 31 | + push!(t.args, :r) |
| 32 | + elseif T <: DataType |
| 33 | + push!(t.args, :(StaticType{r}())) |
| 34 | + else |
| 35 | + _append_fields!(t, body, :r, T) |
| 36 | + end |
| 37 | + push!(body.args, t) |
| 38 | + body |
| 39 | +end |
| 40 | +function rebuild_fields(offset::Int, ::Type{T}) where {T} |
| 41 | + call = (T <: Tuple) ? Expr(:tuple) : Expr(:new, T) |
| 42 | + for f ∈ 1:fieldcount(T) |
| 43 | + TF = fieldtype(T, f) |
| 44 | + if Base.issingletontype(TF) |
| 45 | + push!(call.args, TF.instance) |
| 46 | + elseif fieldcount(TF) ≡ 0 |
| 47 | + push!(call.args, :($getfield(t, $(offset += 1), false))) |
| 48 | + elseif TF <: DataType |
| 49 | + push!(call.args, :($gettype($getfield(t, $(offset += 1), false)))) |
| 50 | + else |
| 51 | + arg, offset = rebuild_fields(offset, TF) |
| 52 | + push!(call.args, arg) |
| 53 | + end |
| 54 | + end |
| 55 | + return call, offset |
| 56 | +end |
| 57 | +@generated function reassemble_tuple(::Type{T}, t::Tuple) where {T} |
| 58 | + if Base.issingletontype(T) |
| 59 | + return T.instance |
| 60 | + elseif fieldcount(T) ≡ 0 |
| 61 | + call = :($getfield(t, 1, false)) |
| 62 | + elseif T <: DataType |
| 63 | + call = :($gettype($getfield(t, 1, false))) |
| 64 | + else |
| 65 | + call, _ = rebuild_fields(0, T) |
| 66 | + end |
| 67 | + Expr(:block, Expr(:meta,:inline), call) |
| 68 | +end |
4 | 69 |
|
5 | 70 | end |
0 commit comments