Skip to content

Commit 7a7abc6

Browse files
committed
Handle Expr-nospecialize
1 parent 3e51701 commit 7a7abc6

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

src/ast.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,17 +498,20 @@ end
498498
# the middle of a pass.
499499
const CompileHints = Base.ImmutableDict{Symbol,Any}
500500

501-
function setmeta(ex::SyntaxTree; kws...)
501+
function setmeta!(ex::SyntaxTree; kws...)
502502
@assert length(kws) == 1 # todo relax later ?
503503
key = first(keys(kws))
504504
value = first(values(kws))
505505
meta = begin
506506
m = get(ex, :meta, nothing)
507507
isnothing(m) ? CompileHints(key, value) : CompileHints(m, key, value)
508508
end
509-
setattr(ex; meta=meta)
509+
setattr!(ex; meta=meta)
510+
ex
510511
end
511512

513+
setmeta(ex::SyntaxTree; kws...) = setmeta!(copy_node(ex); kws...)
514+
512515
function getmeta(ex::SyntaxTree, name::Symbol, default)
513516
meta = get(ex, :meta, nothing)
514517
isnothing(meta) ? default : get(meta, name, default)

src/compat.jl

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ function _insert_convert_expr(@nospecialize(e), graph::SyntaxGraph, src::SourceA
191191
maybe_kind = find_kind(string(e.head))
192192
st_k = isnothing(maybe_kind) ? K"None" : maybe_kind
193193
st_flags = 0x0000
194-
child_exprs = copy(e.args)
194+
child_exprs::Vector{Any} = copy(e.args)
195195

196196
# The following are special cases where the kind, flags, or children are
197197
# different from what we guessed above.
@@ -247,9 +247,21 @@ function _insert_convert_expr(@nospecialize(e), graph::SyntaxGraph, src::SourceA
247247
# Existing behaviour appears to just ignore any extra args
248248
return _insert_convert_expr(e.args[1], graph, src)
249249
elseif e.head === :meta
250-
@assert nargs <= 2
251250
@assert e.args[1] isa Symbol
252-
child_exprs[1] = Expr(:sym_not_identifier, e.args[1])
251+
if e.args[1] === :nospecialize
252+
if nargs > 2
253+
st_k = K"block"
254+
# Kick the can down the road
255+
child_exprs = map(c->Expr(:meta, :nospecialize, c), child_exprs[2:end])
256+
else
257+
st_id, src = _insert_convert_expr(e.args[2], graph, src)
258+
setmeta!(SyntaxTree(graph, st_id); nospecialize=true)
259+
return st_id, src
260+
end
261+
else
262+
@assert nargs === 1
263+
child_exprs[1] = Expr(:sym_not_identifier, e.args[1])
264+
end
253265
elseif e.head === Symbol("'")
254266
@assert nargs === 1
255267
st_k = K"call"
@@ -394,14 +406,19 @@ function _insert_convert_expr(@nospecialize(e), graph::SyntaxGraph, src::SourceA
394406

395407
st_flags |= JS.NON_TERMINAL_FLAG
396408
st_id = _insert_tree_node(graph, st_k, src, st_flags)
409+
st_child_ids, last_src = _insert_expr_children(child_exprs, graph, src)
410+
setchildren!(graph, st_id, st_child_ids)
411+
return st_id, last_src
412+
end
413+
414+
function _insert_expr_children(child_exprs::Vector{Any}, graph::SyntaxGraph,
415+
src::SourceAttrType)
397416
st_child_ids = NodeId[]
398417
last_src = src
399418
for c in child_exprs
400419
(c_id, c_src) = _insert_convert_expr(c, graph, last_src)
401420
isnothing(c_id) || push!(st_child_ids, c_id)
402421
last_src = something(c_src, src)
403422
end
404-
405-
setchildren!(graph, st_id, st_child_ids)
406-
return (st_id, last_src)
423+
return st_child_ids, last_src
407424
end

src/syntax_graph.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,13 +246,17 @@ function attrnames(ex::SyntaxTree)
246246
[name for (name, value) in pairs(attrs) if haskey(value, ex._id)]
247247
end
248248

249-
function setattr(ex::SyntaxTree; extra_attrs...)
249+
function copy_node(ex::SyntaxTree)
250250
graph = syntax_graph(ex)
251251
id = newnode!(graph)
252252
if !is_leaf(ex)
253253
setchildren!(graph, id, _node_ids(graph, children(ex)...))
254254
end
255-
ex2 = SyntaxTree(graph, id)
255+
SyntaxTree(graph, id)
256+
end
257+
258+
function setattr(ex::SyntaxTree; extra_attrs...)
259+
ex2 = copy_node(ex)
256260
copy_attrs!(ex2, ex, true)
257261
setattr!(ex2; extra_attrs...)
258262
ex2

0 commit comments

Comments
 (0)