Repository: JuliaLang/julia
File: JuliaLowering/src/linear_ir.jl (Julia)

Code before:
struct FinallyHandler{Attrs}
    tagvar::SyntaxTree{Attrs}
    target::JumpTarget{Attrs}
    exit_actions::Vector{Tuple{Symbol,Union{Nothing,SyntaxTree{Attrs}}}}
end

function FinallyHandler(tagvar::SyntaxTree{Attrs}, target::JumpTarget) where {Attrs}
    FinallyHandler{Attrs}(tagvar, target,
        Vector{Tuple{Symbol, Union{Nothing,SyntaxTree{Attrs}}}}())
end


"""
Context for creating linear IR.

One of these is created per lambda expression to flatten the body down to
a sequence of statements (linear IR), which eventually becomes one CodeInfo.
"""
struct LinearIRContext{Attrs} <: AbstractLoweringContext
    graph::SyntaxGraph{Attrs}
    code::SyntaxList{Attrs, Vector{NodeId}}
    bindings::Bindings
    next_label_id::Ref{Int}
    is_toplevel_thunk::Bool
    lambda_bindings::LambdaBindings
    argmap::Dict{IdTag, SyntaxTree{Attrs}}
    return_type::Union{Nothing, SyntaxTree{Attrs}}
    break_targets::Dict{String, JumpTarget{Attrs}}
    handler_token_stack::SyntaxList{Attrs, Vector{NodeId}}
    catch_token_stack::SyntaxList{Attrs, Vector{NodeId}}
    finally_handlers::Vector{FinallyHandler{Attrs}}
    symbolic_jump_targets::Dict{String,JumpTarget{Attrs}}
    symbolic_jump_origins::Vector{JumpOrigin{Attrs}}
    meta::Dict{Symbol, Any}
    mod::Module
end

function LinearIRContext(ctx, is_toplevel_thunk, lambda_bindings, return_type)
    graph = syntax_graph(ctx)
    rett = isnothing(return_type) ? nothing : reparent(graph, return_type)
    Attrs = typeof(graph.attributes)
    LinearIRContext(graph, SyntaxList(ctx), ctx.bindings, Ref(0),
                    is_toplevel_thunk, lambda_bindings, Dict{IdTag, SyntaxTree{Attrs}}(), rett,
                    Dict{String,JumpTarget{Attrs}}(), SyntaxList(ctx), SyntaxList(ctx),
                    Vector{FinallyHandler{Attrs}}(), Dict{String,JumpTarget{Attrs}}(),
                    Vector{JumpOrigin{Attrs}}(), Dict{Symbol, Any}(), ctx.mod)
end

function current_lambda_bindings(ctx::LinearIRContext)
    ctx.lambda_bindings
end

Review comment (suggestion):
We may want a `Dict{IdTag, IdTag}` instead if I'm correct in thinking `var_id` is the only thing that changes.  Remapping a node shouldn't change its provenance or other attributes

Code after:
struct FinallyHandler{Attrs}
    tagvar::SyntaxTree{Attrs}
    target::JumpTarget{Attrs}
    exit_actions::Vector{Tuple{Symbol,Union{Nothing,SyntaxTree{Attrs}}}}
end

function FinallyHandler(tagvar::SyntaxTree{Attrs}, target::JumpTarget) where {Attrs}
    FinallyHandler{Attrs}(tagvar, target,
        Vector{Tuple{Symbol, Union{Nothing,SyntaxTree{Attrs}}}}())
end


"""
Context for creating linear IR.

One of these is created per lambda expression to flatten the body down to
a sequence of statements (linear IR), which eventually becomes one CodeInfo.
"""
struct LinearIRContext{Attrs} <: AbstractLoweringContext
    graph::SyntaxGraph{Attrs}
    code::SyntaxList{Attrs, Vector{NodeId}}
    bindings::Bindings
    next_label_id::Ref{Int}
    is_toplevel_thunk::Bool
    lambda_bindings::LambdaBindings
    argmap::Dict{IdTag, IdTag}
    return_type::Union{Nothing, SyntaxTree{Attrs}}
    break_targets::Dict{String, JumpTarget{Attrs}}
    handler_token_stack::SyntaxList{Attrs, Vector{NodeId}}
    catch_token_stack::SyntaxList{Attrs, Vector{NodeId}}
    finally_handlers::Vector{FinallyHandler{Attrs}}
    symbolic_jump_targets::Dict{String,JumpTarget{Attrs}}
    symbolic_jump_origins::Vector{JumpOrigin{Attrs}}
    meta::Dict{Symbol, Any}
    mod::Module
end

function LinearIRContext(ctx, is_toplevel_thunk, lambda_bindings, return_type)
    graph = syntax_graph(ctx)
    rett = isnothing(return_type) ? nothing : reparent(graph, return_type)
    Attrs = typeof(graph.attributes)
    LinearIRContext(graph, SyntaxList(ctx), ctx.bindings, Ref(0),
                    is_toplevel_thunk, lambda_bindings, Dict{IdTag,IdTag}(), rett,
                    Dict{String,JumpTarget{Attrs}}(), SyntaxList(ctx), SyntaxList(ctx),
                    Vector{FinallyHandler{Attrs}}(), Dict{String,JumpTarget{Attrs}}(),
                    Vector{JumpOrigin{Attrs}}(), Dict{Symbol, Any}(), ctx.mod)
end

function current_lambda_bindings(ctx::LinearIRContext)
    ctx.lambda_bindings
end