Skip to content

Commit

Permalink
Add StdPrinter
Browse files Browse the repository at this point in the history
  • Loading branch information
Gnimuc committed Apr 6, 2021
1 parent 9a1450a commit 734f6d1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/generator/Generators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export LinkTypedefToAnonymousTagType
export ProloguePrinter
export RemoveCircularReference
export ResolveDependency
export StdPrinter
export TopologicalSort

include("context.jl")
Expand Down
19 changes: 12 additions & 7 deletions src/generator/context.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ function find_dependent_headers(headers::Vector{T}, args::Vector, general_ops::D
end

"""
create_context(headers::Vector, args::Vector, options::Dict)
create_context(headers::Vector, args::Vector=String[], options::Dict=Dict())
Create a context from a vector of paths of headers, a vector of compiler flags and
a option dict.
"""
function create_context(headers::Vector, args::Vector, options::Dict)
function create_context(headers::Vector, args::Vector=String[], options::Dict=Dict())
ctx = Context(options)

general_options = get(options, "general", Dict())
Expand Down Expand Up @@ -149,19 +149,24 @@ function create_context(headers::Vector, args::Vector, options::Dict)
common_file = get(general_options, "output_common_file_path", "")

output_file_path = get(general_options, "output_file_path", "")
@assert !isempty(output_file_path) "output file path is empty, please set `output_file_path` in the toml file."
if isempty(api_file) && isempty(common_file)
push!(ctx.passes, ProloguePrinter(output_file_path))
push!(ctx.passes, GeneralPrinter(output_file_path))
push!(ctx.passes, EpiloguePrinter(output_file_path))
if !isempty(output_file_path)
push!(ctx.passes, ProloguePrinter(output_file_path))
push!(ctx.passes, GeneralPrinter(output_file_path))
push!(ctx.passes, EpiloguePrinter(output_file_path))
else
# print to stdout if there is no `output_file_path`
# this is handy when playing in REPL
push!(ctx.passes, StdPrinter())
end
else
# TODO: impl
end

return ctx
end

create_context(header::AbstractString, args, ops) = create_context([header], args, ops)
create_context(header::AbstractString, args=String[], ops=Dict()) = create_context([header], args, ops)

@enum BuildStage begin
BUILDSTAGE_ALL
Expand Down
30 changes: 30 additions & 0 deletions src/generator/passes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,36 @@ function (x::GeneralPrinter)(dag::ExprDAG, options::Dict)
return dag
end

"""
StdPrinter <: AbstractPrinter
In this pass, structs/unions/enums are dumped to stdout.
"""
mutable struct StdPrinter <: AbstractPrinter
show_info::Bool
end
StdPrinter(; info=true) = StdPrinter(info)

function (x::StdPrinter)(dag::ExprDAG, options::Dict)
general_options = get(options, "general", Dict())
log_options = get(general_options, "log", Dict())
show_info = get(log_options, "StdPrinter_log", x.show_info)
blacklist = get(general_options, "printer_blacklist", [])

for node in dag.nodes
string(node.id) blacklist && continue
node.type isa AbstractMacroNodeType && continue
pretty_print(stdout, node, general_options)
end
# print macros
for node in dag.nodes
string(node.id) blacklist && continue
node.type isa AbstractMacroNodeType || continue
pretty_print(stdout, node, options)
end

return dag
end

"""
ProloguePrinter <: AbstractPrinter
In this pass, prologues are dumped to file.
Expand Down

0 comments on commit 734f6d1

Please sign in to comment.