8686
8787# Recursive construction of the TypedSyntaxNode tree from the SyntaxNodeTree
8888function addchildren! (tparent, parent, src:: CodeInfo , node2ssa, symtyps, mappings)
89- if haschildren (parent) && tparent. children === nothing
90- tparent. children = TypedSyntaxNode[]
91- end
92- for child in children (parent)
93- tnode = TypedSyntaxNode (tparent, nothing , TypedSyntaxData (child. data:: SyntaxData , src, gettyp (node2ssa, child, src)))
94- if tnode. typ === nothing && (#= is_literal(child) ||=# kind (child) == K " Identifier" )
95- tnode. typ = get (symtyps, child, nothing )
89+ if ! is_leaf (parent)
90+ if tparent. children === nothing
91+ tparent. children = TypedSyntaxNode[]
92+ end
93+ for child in children (parent)
94+ typ = gettyp (node2ssa, child, src)
95+ tnode = TypedSyntaxNode (tparent, nothing , TypedSyntaxData (child. data:: SyntaxData , src, typ))
96+ if tnode. typ === nothing && (#= is_literal(child) ||=# kind (child) == K " Identifier" )
97+ tnode. typ = get (symtyps, child, nothing )
98+ end
99+ push! (tparent, tnode)
100+ addchildren! (tnode, child, src, node2ssa, symtyps, mappings)
96101 end
97- push! (tparent, tnode)
98- addchildren! (tnode, child, src, node2ssa, symtyps, mappings)
99102 end
100103 # In `return f(args..)`, copy any types assigned to `f(args...)` up to the `[return]` node
101- if kind (tparent) == K " return" && haschildren (tparent)
104+ if kind (tparent) == K " return" && ! is_leaf (tparent)
102105 childs = children (tparent)
103106 tparent. typ = isempty (childs) ? Nothing : only (childs). typ
104107 end
@@ -121,33 +124,31 @@ function map_signature!(sig::TypedSyntaxNode, slotnames::Vector{Symbol}, slottyp
121124 nchildren = length (children (arg))
122125 nchildren == 1 && return nothing , defaultval
123126 @assert nchildren == 2
124- arg = child ( arg, 1 )
127+ arg = arg[ 1 ]
125128 end
126129 if kind (arg) == K " $"
127130 return nothing , defaultval
128131 end
129132 if kind (arg) == K " ."
130133 arg = last (children (arg))
131- if kind (arg) == K "inert "
132- @assert kind (only ( children ( arg))) == K " $"
133- return nothing , defaultval
134+ if kind (arg) != K "Identifier "
135+ @assert kind (arg) in KSet " quote $"
136+ arg = only ( children (arg))
134137 end
135- @assert kind (arg) == K " quote"
136- arg = only (children (arg))
137138 end
138139 if kind (arg) == K " curly"
139140 arg = first (children (arg))
140141 end
141142 if kind (arg) == K " var"
142- arg = child ( arg, 1 )
143+ arg = arg[ 1 ]
143144 end
144145 kind (arg) == K " tuple" && return nothing , defaultval # FIXME ? see extrema2 test
145146 @assert kind (arg) == K " Identifier" || is_operator (arg)
146147 return arg, defaultval
147148 end
148149
149150 while kind (sig) ∈ KSet " where ::" # handle MyType{T}(args...) and return-type annotations
150- sig = child ( sig, 1 )
151+ sig = sig[ 1 ]
151152 end
152153 @assert kind (sig) == K " call"
153154 # First, match named args, since those have to be unique
@@ -241,14 +242,14 @@ function map_signature!(sig::TypedSyntaxNode, slotnames::Vector{Symbol}, slottyp
241242 end
242243 idx == 0 && continue
243244 if kind (arg) == K " ::" && length (children (arg)) == 2
244- arg = child ( arg, 1 )
245+ arg = arg[ 1 ]
245246 end
246247 arg. typ = slottypes[idx]
247248 end
248249
249250 # It's annoying to print the signature as `foo::typeof(foo)(a::Int)`
250251 # Strip the type annotation from the function name
251- arg, _ = argidentifier (child ( sig, 1 ) )
252+ arg, _ = argidentifier (sig[ 1 ] )
252253 if arg != = nothing
253254 arg. typ = nothing
254255 end
@@ -264,8 +265,8 @@ function striparg(arg)
264265 arg = only (children (arg))
265266 end
266267 if kind (arg) == K " ="
267- defaultval = child ( arg, 2 )
268- arg = child ( arg, 1 )
268+ defaultval = arg[ 2 ]
269+ arg = arg[ 1 ]
269270 end
270271 if kind (arg) == K " macrocall"
271272 arg = last (children (arg)) # FIXME : is the variable always the final argument?
@@ -351,10 +352,10 @@ end
351352
352353function is_function_def (node) # this is not `Base.is_function_def`
353354 kind (node) == K " function" && return true
354- if kind (node) == K " =" && length ( children ( node)) >= 1
355- sig = child ( node, 1 )
355+ if kind (node) == K " =" && ! is_leaf ( node)
356+ sig = node[ 1 ]
356357 while (kind (sig) ∈ KSet " where ::" ) # allow MyType{T}(args...) and return-type annotations
357- sig = child ( sig, 1 )
358+ sig = sig[ 1 ]
358359 end
359360 kind (sig) == K " call" && return true
360361 end
@@ -366,7 +367,7 @@ function get_function_def(rootnode)
366367 while kind (rootnode) ∈ KSet " macrocall global local const"
367368 idx = findlast (node -> is_function_def (node) || kind (node) == K " macrocall" , children (rootnode))
368369 idx === nothing && break
369- rootnode = child ( rootnode, idx)
370+ rootnode = rootnode[ idx]
370371 end
371372 return rootnode
372373end
@@ -375,7 +376,7 @@ function num_positional_args(tsn::AbstractSyntaxNode)
375376 TypedSyntax. is_function_def (tsn) || return 0
376377 sig, _ = children (tsn)
377378 while kind (sig) ∈ KSet " where ::"
378- sig = child ( sig, 1 )
379+ sig = sig[ 1 ]
379380 end
380381 @assert kind (sig) == K " call"
381382 for (i, node) in enumerate (children (sig))
@@ -392,7 +393,7 @@ function collect_symbol_nodes(rootnode)
392393 rootnode = get_function_def (rootnode)
393394 is_function_def (rootnode) || error (" expected function definition, got " , sourcetext (rootnode))
394395 symlocs = Dict {Any,Vector{typeof(rootnode)}} ()
395- return collect_symbol_nodes! (symlocs, child ( rootnode, 2 ) )
396+ return collect_symbol_nodes! (symlocs, rootnode[ 2 ] )
396397end
397398
398399function collect_symbol_nodes! (symlocs:: AbstractDict , node)
@@ -409,10 +410,10 @@ function collect_symbol_nodes!(symlocs::AbstractDict, node)
409410 locs = get! (Vector{typeof (node)}, symlocs, node. val)
410411 push! (locs, node)
411412 end
412- if haschildren (node)
413+ if ! is_leaf (node)
413414 if kind (node) == K " do"
414415 # process only `g(args...)` in `g(args...) do ... end`
415- collect_symbol_nodes! (symlocs, child ( node, 1 ) )
416+ collect_symbol_nodes! (symlocs, node[ 1 ] )
416417 elseif kind (node) == K " generator"
417418 for c in Iterators. drop (children (node), 1 )
418419 collect_symbol_nodes! (symlocs, c)
@@ -560,7 +561,7 @@ function map_ssas_to_source(src::CodeInfo, mi::MethodInstance, rootnode::SyntaxN
560561 argnode = p
561562 p = argnode. parent:: MaybeTypedSyntaxNode
562563 end
563- is_prec_assignment (p) && argnode == child (p, 1 + is_rhs) # is it the correct side of an assignment?
564+ is_prec_assignment (p) && argnode == p[ 1 + is_rhs] # is it the correct side of an assignment?
564565 end
565566 return targets
566567 end
@@ -719,7 +720,7 @@ function map_ssas_to_source(src::CodeInfo, mi::MethodInstance, rootnode::SyntaxN
719720 while ! is_prec_assignment (lhsnode) && (lhsparent = lhsnode. parent; lhsparent != = nothing )
720721 lhsnode = lhsparent
721722 end
722- lhsnode, found = find_identifier_or_tuplechild (child ( lhsnode, 1 ) , sym)
723+ lhsnode, found = find_identifier_or_tuplechild (lhsnode[ 1 ] , sym)
723724 if found
724725 symtyps[lhsnode] = ssavaluetypes[i]
725726 end
@@ -750,7 +751,7 @@ function map_ssas_to_source(src::CodeInfo, mi::MethodInstance, rootnode::SyntaxN
750751 for t in itr
751752 haskey (symtyps, t) && continue
752753 if skipped_parent (t) == node
753- is_prec_assignment (node) && t == child ( node, 1 ) && continue
754+ is_prec_assignment (node) && t == node[ 1 ] && continue
754755 symtyps[t] = if j > 0
755756 ssavaluetypes[j]
756757 elseif have_slottypes
@@ -861,6 +862,7 @@ function skipped_parent(node::SyntaxNode)
861862 pnode = node. parent
862863 pnode === nothing && return node
863864 ppnode = pnode. parent
865+ kind (pnode) == K " in" && kind (ppnode) == K " iteration" && return ppnode
864866 if ppnode != = nothing && kind (pnode) ∈ KSet " ... quote" # might need to add more things here
865867 if kind (node) == K " Identifier" && kind (pnode) == K " quote" && kind (ppnode) == K " ." && sourcetext (node) == " materialize"
866868 return ppnode. parent
0 commit comments