| 1 | // Copyright 2018 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // Package inspector provides helper functions for traversal over the |
| 6 | // syntax trees of a package, including node filtering by type, and |
| 7 | // materialization of the traversal stack. |
| 8 | // |
| 9 | // During construction, the inspector does a complete traversal and |
| 10 | // builds a list of push/pop events and their node type. Subsequent |
| 11 | // method calls that request a traversal scan this list, rather than walk |
| 12 | // the AST, and perform type filtering using efficient bit sets. |
| 13 | // This representation is sometimes called a "balanced parenthesis tree." |
| 14 | // |
| 15 | // Experiments suggest the inspector's traversals are about 2.5x faster |
| 16 | // than [ast.Inspect], but it may take around 5 traversals for this |
| 17 | // benefit to amortize the inspector's construction cost. |
| 18 | // If efficiency is the primary concern, do not use Inspector for |
| 19 | // one-off traversals. |
| 20 | // |
| 21 | // The [Cursor] type provides a more flexible API for efficient |
| 22 | // navigation of syntax trees in all four "cardinal directions". For |
| 23 | // example, traversals may be nested, so you can find each node of |
| 24 | // type A and then search within it for nodes of type B. Or you can |
| 25 | // traverse from a node to its immediate neighbors: its parent, its |
| 26 | // previous and next sibling, or its first and last child. We |
| 27 | // recommend using methods of Cursor in preference to Inspector where |
| 28 | // possible. |
| 29 | package inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/ |
| 30 | |
| 31 | // There are four orthogonal features in a traversal: |
| 32 | // 1 type filtering |
| 33 | // 2 pruning |
| 34 | // 3 postorder calls to f |
| 35 | // 4 stack |
| 36 | // Rather than offer all of them in the API, |
| 37 | // only a few combinations are exposed: |
| 38 | // - Preorder is the fastest and has fewest features, |
| 39 | // but is the most commonly needed traversal. |
| 40 | // - Nodes and WithStack both provide pruning and postorder calls, |
| 41 | // even though few clients need it, because supporting two versions |
| 42 | // is not justified. |
| 43 | // More combinations could be supported by expressing them as |
| 44 | // wrappers around a more generic traversal, but this was measured |
| 45 | // and found to degrade performance significantly (30%). |
| 46 | |
| 47 | import ( |
| 48 | "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/" |
| 49 | |
| 50 | "golang.org/x/tools/go/ast/edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/" |
| 51 | ) |
| 52 | |
| 53 | // An Inspector provides methods for inspecting |
| 54 | // (traversing) the syntax trees of a package. |
| 55 | type Inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector# struct { |
| 56 | eventsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#events. []eventscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event# |
| 57 | } |
| 58 | |
| 59 | func packEdgeKindAndIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/packEdgeKindAndIndex().(ek edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/.Kindscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/Kind#, index int) int32 { |
| 60 | return int32(uint32(index+1)<<7 | uint32(ek)) |
| 61 | } |
| 62 | |
| 63 | // unpackEdgeKindAndIndex unpacks the edge kind and edge index (within |
| 64 | // an []ast.Node slice) from the parent field of a pop event. |
| 65 | func unpackEdgeKindAndIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/unpackEdgeKindAndIndex().(x int32) (edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/.Kindscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/Kind#, int) { |
| 66 | // The "parent" field of a pop node holds the |
| 67 | // edge Kind in the lower 7 bits and the index+1 |
| 68 | // in the upper 25. |
| 69 | return edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/.Kindscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/Kind#(x & 0x7f), int(x>>7) - 1 |
| 70 | } |
| 71 | |
| 72 | // New returns an Inspector for the specified syntax trees. |
| 73 | func Newscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/New().(files []*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#) *Inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector# { |
| 74 | return &Inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#{traversescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/traverse().(files)} |
| 75 | } |
| 76 | |
| 77 | // An event represents a push or a pop |
| 78 | // of an ast.Node during a traversal. |
| 79 | type eventscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event# struct { |
| 80 | nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#node. astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node# |
| 81 | typscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#typ. uint64 // typeOf(node) on push event, or union of typ strictly between push and pop events on pop events |
| 82 | indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#index. int32 // index of corresponding push or pop event |
| 83 | parentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#parent. int32 // index of parent's push node (push nodes only), or packed edge kind/index (pop nodes only) |
| 84 | } |
| 85 | |
| 86 | // TODO: Experiment with storing only the second word of event.node (unsafe.Pointer). |
| 87 | // Type can be recovered from the sole bit in typ. |
| 88 | // [Tried this, wasn't faster. --adonovan] |
| 89 | |
| 90 | // Preorder visits all the nodes of the files supplied to [New] in |
| 91 | // depth-first order. It calls f(n) for each node n before it visits |
| 92 | // n's children. |
| 93 | // |
| 94 | // The complete traversal sequence is determined by [ast.Inspect]. |
| 95 | // The types argument, if non-empty, enables type-based filtering of |
| 96 | // events. The function f is called only for nodes whose type |
| 97 | // matches an element of the types slice. |
| 98 | // |
| 99 | // The [Cursor.Preorder] method provides a richer alternative interface. |
| 100 | // Example: |
| 101 | // |
| 102 | // for c := range in.Root().Preorder(types) { ... } |
| 103 | func (in *Inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#) Preorderscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#Preorder().(types []astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#, f func(astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#)) { |
| 104 | // Because it avoids postorder calls to f, and the pruning |
| 105 | // check, Preorder is almost twice as fast as Nodes. The two |
| 106 | // features seem to contribute similar slowdowns (~1.4x each). |
| 107 | |
| 108 | // This function is equivalent to the PreorderSeq call below, |
| 109 | // but to avoid the additional dynamic call (which adds 13-35% |
| 110 | // to the benchmarks), we expand it out. |
| 111 | // |
| 112 | // in.PreorderSeq(types...)(func(n ast.Node) bool { |
| 113 | // f(n) |
| 114 | // return true |
| 115 | // }) |
| 116 | |
| 117 | mask := maskOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/maskOf().(types) |
| 118 | for i := int32(0); i < int32(len(in.eventsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#events.)); { |
| 119 | ev := in.eventsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#events.[i] |
| 120 | if ev.indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#index. > i { |
| 121 | // push |
| 122 | if ev.typscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#typ.&mask != 0 { |
| 123 | f(ev.nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#node.) |
| 124 | } |
| 125 | pop := ev.indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#index. |
| 126 | if in.eventsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#events.[pop].typscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#typ.&mask == 0 { |
| 127 | // Subtrees do not contain types: skip them and pop. |
| 128 | i = pop + 1 |
| 129 | continue |
| 130 | } |
| 131 | } |
| 132 | i++ |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // Nodes visits the nodes of the files supplied to [New] in depth-first |
| 137 | // order. It calls f(n, true) for each node n before it visits n's |
| 138 | // children. If f returns true, Nodes invokes f recursively for each |
| 139 | // of the non-nil children of the node, followed by a call of |
| 140 | // f(n, false). |
| 141 | // |
| 142 | // The complete traversal sequence is determined by [ast.Inspect]. |
| 143 | // The types argument, if non-empty, enables type-based filtering of |
| 144 | // events. The function f if is called only for nodes whose type |
| 145 | // matches an element of the types slice. |
| 146 | // |
| 147 | // The [Cursor.Inspect] method provides a richer alternative interface. |
| 148 | // Example: |
| 149 | // |
| 150 | // in.Root().Inspect(types, func(c Cursor) bool { |
| 151 | // ... |
| 152 | // return true |
| 153 | // } |
| 154 | func (in *Inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#) Nodesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#Nodes().(types []astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#, f func(n astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#, push bool) (proceed bool)) { |
| 155 | mask := maskOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/maskOf().(types) |
| 156 | for i := int32(0); i < int32(len(in.eventsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#events.)); { |
| 157 | ev := in.eventsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#events.[i] |
| 158 | if ev.indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#index. > i { |
| 159 | // push |
| 160 | pop := ev.indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#index. |
| 161 | if ev.typscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#typ.&mask != 0 { |
| 162 | if !f(ev.nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#node., true) { |
| 163 | i = pop + 1 // jump to corresponding pop + 1 |
| 164 | continue |
| 165 | } |
| 166 | } |
| 167 | if in.eventsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#events.[pop].typscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#typ.&mask == 0 { |
| 168 | // Subtrees do not contain types: skip them. |
| 169 | i = pop |
| 170 | continue |
| 171 | } |
| 172 | } else { |
| 173 | // pop |
| 174 | push := ev.indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#index. |
| 175 | if in.eventsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#events.[push].typscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#typ.&mask != 0 { |
| 176 | f(ev.nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#node., false) |
| 177 | } |
| 178 | } |
| 179 | i++ |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | // WithStack visits nodes in a similar manner to Nodes, but it |
| 184 | // supplies each call to f an additional argument, the current |
| 185 | // traversal stack. The stack's first element is the outermost node, |
| 186 | // an *ast.File; its last is the innermost, n. |
| 187 | // |
| 188 | // The [Cursor.Inspect] method provides a richer alternative interface. |
| 189 | // Example: |
| 190 | // |
| 191 | // in.Root().Inspect(types, func(c Cursor) bool { |
| 192 | // stack := slices.Collect(c.Enclosing()) |
| 193 | // ... |
| 194 | // return true |
| 195 | // }) |
| 196 | func (in *Inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#) WithStackscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#WithStack().(types []astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#, f func(n astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#, push bool, stack []astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#) (proceed bool)) { |
| 197 | mask := maskOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/maskOf().(types) |
| 198 | var stack []astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node# |
| 199 | for i := int32(0); i < int32(len(in.eventsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#events.)); { |
| 200 | ev := in.eventsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#events.[i] |
| 201 | if ev.indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#index. > i { |
| 202 | // push |
| 203 | pop := ev.indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#index. |
| 204 | stack = append(stack, ev.nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#node.) |
| 205 | if ev.typscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#typ.&mask != 0 { |
| 206 | if !f(ev.nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#node., true, stack) { |
| 207 | i = pop + 1 |
| 208 | stack = stack[:len(stack)-1] |
| 209 | continue |
| 210 | } |
| 211 | } |
| 212 | if in.eventsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#events.[pop].typscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#typ.&mask == 0 { |
| 213 | // Subtrees does not contain types: skip them. |
| 214 | i = pop |
| 215 | continue |
| 216 | } |
| 217 | } else { |
| 218 | // pop |
| 219 | push := ev.indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#index. |
| 220 | if in.eventsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#events.[push].typscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#typ.&mask != 0 { |
| 221 | f(ev.nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#node., false, stack) |
| 222 | } |
| 223 | stack = stack[:len(stack)-1] |
| 224 | } |
| 225 | i++ |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | // traverse builds the table of events representing a traversal. |
| 230 | func traversescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/traverse().(files []*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#) []eventscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event# { |
| 231 | // Preallocate approximate number of events |
| 232 | // based on source file extent of the declarations. |
| 233 | // (We use End-Pos not FileStart-FileEnd to neglect |
| 234 | // the effect of long doc comments.) |
| 235 | // This makes traverse faster by 4x (!). |
| 236 | var extent int |
| 237 | for _, f := range files { |
| 238 | extent += int(f.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#End().() - f.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Pos().()) |
| 239 | } |
| 240 | // This estimate is based on the net/http package. |
| 241 | capacity := min(extent*33/100, 1e6) // impose some reasonable maximum (1M) |
| 242 | |
| 243 | v := &visitorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/visitor#{ |
| 244 | eventsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/visitor#events.: make([]eventscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#, 0, capacity), |
| 245 | stackscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/visitor#stack.: []itemscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/item#{{indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/item#index.: -1}}, // include an extra event so file nodes have a parent |
| 246 | } |
| 247 | for _, file := range files { |
| 248 | walkscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/walk().(v, edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/.Invalidscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/Invalid., -1, file) |
| 249 | } |
| 250 | return v.eventsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/visitor#events. |
| 251 | } |
| 252 | |
| 253 | type visitorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/visitor# struct { |
| 254 | eventsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/visitor#events. []eventscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event# |
| 255 | stackscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/visitor#stack. []itemscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/item# |
| 256 | } |
| 257 | |
| 258 | type itemscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/item# struct { |
| 259 | indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/item#index. int32 // index of current node's push event |
| 260 | parentIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/item#parentIndex. int32 // index of parent node's push event |
| 261 | typAccumscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/item#typAccum. uint64 // accumulated type bits of current node's descendants |
| 262 | edgeKindAndIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/item#edgeKindAndIndex. int32 // edge.Kind and index, bit packed |
| 263 | } |
| 264 | |
| 265 | func (v *visitorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/visitor#) pushscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/visitor#push().(ek edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/.Kindscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/Kind#, eindex int, node astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#) { |
| 266 | var ( |
| 267 | index = int32(len(v.eventsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/visitor#events.)) |
| 268 | parentIndex = v.stackscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/visitor#stack.[len(v.stackscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/visitor#stack.)-1].indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/item#index. |
| 269 | ) |
| 270 | v.eventsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/visitor#events. = append(v.eventsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/visitor#events., eventscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#{ |
| 271 | nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#node.: node, |
| 272 | parentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#parent.: parentIndex, |
| 273 | typscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#typ.: typeOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/typeOf().(node), |
| 274 | indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#index.: 0, // (pop index is set later by visitor.pop) |
| 275 | }) |
| 276 | v.stackscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/visitor#stack. = append(v.stackscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/visitor#stack., itemscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/item#{ |
| 277 | indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/item#index.: index, |
| 278 | parentIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/item#parentIndex.: parentIndex, |
| 279 | edgeKindAndIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/item#edgeKindAndIndex.: packEdgeKindAndIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/packEdgeKindAndIndex().(ek, eindex), |
| 280 | }) |
| 281 | |
| 282 | // 2B nodes ought to be enough for anyone! |
| 283 | if int32(len(v.eventsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/visitor#events.)) < 0 { |
| 284 | panic("event index exceeded int32") |
| 285 | } |
| 286 | |
| 287 | // 32M elements in an []ast.Node ought to be enough for anyone! |
| 288 | if ek2, eindex2 := unpackEdgeKindAndIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/unpackEdgeKindAndIndex().(packEdgeKindAndIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/packEdgeKindAndIndex().(ek, eindex)); ek2 != ek || eindex2 != eindex { |
| 289 | panic("Node slice index exceeded uint25") |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | func (v *visitorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/visitor#) popscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/visitor#pop().(node astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#) { |
| 294 | top := len(v.stackscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/visitor#stack.) - 1 |
| 295 | current := v.stackscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/visitor#stack.[top] |
| 296 | |
| 297 | push := &v.eventsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/visitor#events.[current.indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/item#index.] |
| 298 | parent := &v.stackscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/visitor#stack.[top-1] |
| 299 | |
| 300 | push.indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#index. = int32(len(v.eventsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/visitor#events.)) // make push event refer to pop |
| 301 | parent.typAccumscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/item#typAccum. |= current.typAccumscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/item#typAccum. | push.typscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#typ. // accumulate type bits into parent |
| 302 | |
| 303 | v.stackscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/visitor#stack. = v.stackscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/visitor#stack.[:top] |
| 304 | |
| 305 | v.eventsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/visitor#events. = append(v.eventsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/visitor#events., eventscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#{ |
| 306 | nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#node.: node, |
| 307 | typscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#typ.: current.typAccumscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/item#typAccum., |
| 308 | indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#index.: current.indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/item#index., |
| 309 | parentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/event#parent.: current.edgeKindAndIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/item#edgeKindAndIndex., // see [unpackEdgeKindAndIndex] |
| 310 | }) |
| 311 | } |