[dev.regabi] cmd/compile: introduce IR visitors

This CL introduces the general visitor functionality that will replace
the Left, SetLeft, Right, SetRight, etc methods in the Node interface.

For now, the CL defines the functionality in terms of those methods,
but eventually the Nodes themselves will implement DoChildren
and EditChildren and be relieved of implementing Left, SetLeft, and so on.

The CL also updates Inspect (which moved to visit.go) and DeepCopy
to use the new functionality.

The Find helper is not used in this CL but will be used in a future one.

Passes buildall w/ toolstash -cmp.

Change-Id: Id0eea654a884ab3ea25f48bd8bdd71712b5dcb44
Reviewed-on: https://go-review.googlesource.com/c/go/+/275311
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Russ Cox 2020-12-03 12:57:38 -05:00
parent 7fcf5b994c
commit 0d1b44c645
3 changed files with 289 additions and 61 deletions

View file

@ -521,26 +521,6 @@ func (n *Nodes) AppendNodes(n2 *Nodes) {
n2.slice = nil
}
// inspect invokes f on each node in an AST in depth-first order.
// If f(n) returns false, inspect skips visiting n's children.
func Inspect(n Node, f func(Node) bool) {
if n == nil || !f(n) {
return
}
InspectList(n.Init(), f)
Inspect(n.Left(), f)
Inspect(n.Right(), f)
InspectList(n.List(), f)
InspectList(n.Body(), f)
InspectList(n.Rlist(), f)
}
func InspectList(l Nodes, f func(Node) bool) {
for _, n := range l.Slice() {
Inspect(n, f)
}
}
// nodeQueue is a FIFO queue of *Node. The zero value of nodeQueue is
// a ready-to-use empty queue.
type NodeQueue struct {