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

Code before:
        name_val=String, is_toplevel_thunk=Bool,
        scope_layer=LayerId, meta=CompileHints,
        toplevel_pure=Bool)
    expr_to_syntaxtree(graph, e, lnn)
end

@fzone "JL: expr_to_syntaxtree" function expr_to_syntaxtree(ctx, @nospecialize(e), lnn::Union{LineNumberNode, Nothing})
    graph = syntax_graph(ctx)
    toplevel_src = if isnothing(lnn)
        # Provenance sinkhole for all nodes until we hit a linenode
        dummy_src = SourceRef(SourceFile("No source for expression"), 1, 0)
        _insert_tree_node(graph, K"None", dummy_src)
    else
        lnn
    end
    st_id, _ = _insert_convert_expr(e, graph, toplevel_src)
    out = SyntaxTree(graph, st_id)
    return out
end

function _expr_replace(@nospecialize(e), replace_pred::Function, replacer::Function,
                        recurse_pred=(@nospecialize e)->true)
    if replace_pred(e)
        replacer(e)
    elseif e isa Expr && recurse_pred(e)
        Expr(e.head, [_expr_replace(a, replace_pred, replacer, recurse_pred) for a in e.args]...)
    else
        e
    end
end

function _eq_to_kw(@nospecialize(e))
    _expr_replace(e,
        (x)->x isa Expr && x.head === :(=),
        (x)->Expr(:kw, x.args...),
        (x)->x.head in (:escape, :var"hygienic-scope"))
end

function _to_iterspec(exs::Vector, is_generator::Bool)
    if length(exs) === 1 && exs[1].head === :filter
        @assert length(exs[1].args) >= 2
        return Expr(:filter, _to_iterspec(exs[1].args[2:end], true), exs[1].args[1])
    end
    outex = Expr(:iteration)
    for e in exs
        if e.head === :block && !is_generator
            for iter in e.args
                push!(outex.args, Expr(:in, iter.args...))
            end
        elseif e.head === :(=)
            push!(outex.args, Expr(:in, e.args...))

Review comment (suggestion):
```suggestion
        Expr(e.head, Any[_expr_replace(a, replace_pred, replacer, recurse_pred) for a in e.args]...)
```

Code after:
        name_val=String, is_toplevel_thunk=Bool,
        scope_layer=LayerId, meta=CompileHints,
        toplevel_pure=Bool)
    expr_to_syntaxtree(graph, e, lnn)
end

@fzone "JL: expr_to_syntaxtree" function expr_to_syntaxtree(ctx, @nospecialize(e), lnn::Union{LineNumberNode, Nothing})
    graph = syntax_graph(ctx)
    toplevel_src = if isnothing(lnn)
        # Provenance sinkhole for all nodes until we hit a linenode
        dummy_src = SourceRef(SourceFile("No source for expression"), 1, 0)
        _insert_tree_node(graph, K"None", dummy_src)
    else
        lnn
    end
    st_id, _ = _insert_convert_expr(e, graph, toplevel_src)
    out = SyntaxTree(graph, st_id)
    return out
end

function _expr_replace(@nospecialize(e), replace_pred::Function, replacer::Function,
                        recurse_pred=(@nospecialize e)->true)
    if replace_pred(e)
        replacer(e)
    elseif e isa Expr && recurse_pred(e)
        Expr(e.head, Any[_expr_replace(a, replace_pred, replacer, recurse_pred) for a in e.args]...)
    else
        e
    end
end

function _to_iterspec(exs::Vector, is_generator::Bool)
    if length(exs) === 1 && exs[1].head === :filter
        @assert length(exs[1].args) >= 2
        return Expr(:filter, _to_iterspec(exs[1].args[2:end], true), exs[1].args[1])
    end
    outex = Expr(:iteration)
    for e in exs
        if e.head === :block && !is_generator
            for iter in e.args
                push!(outex.args, Expr(:in, iter.args...))
            end
        elseif e.head === :(=)
            push!(outex.args, Expr(:in, e.args...))
        else
            @assert false "unknown iterspec in $e"
        end
    end
    return outex
end