Source file src/cmd/compile/internal/ir/visit.go
1 // Copyright 2020 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 // IR visitors for walking the IR tree. 6 // 7 // The lowest level helpers are DoChildren and EditChildren, which 8 // nodes help implement and provide control over whether and when 9 // recursion happens during the walk of the IR. 10 // 11 // Although these are both useful directly, two simpler patterns 12 // are fairly common and also provided: Visit and Any. 13 14 package ir 15 16 // DoChildren calls do(x) on each of n's non-nil child nodes x. 17 // If any call returns true, DoChildren stops and returns true. 18 // Otherwise, DoChildren returns false. 19 // 20 // Note that DoChildren(n, do) only calls do(x) for n's immediate children. 21 // If x's children should be processed, then do(x) must call DoChildren(x, do). 22 // 23 // DoChildren allows constructing general traversals of the IR graph 24 // that can stop early if needed. The most general usage is: 25 // 26 // var do func(ir.Node) bool 27 // do = func(x ir.Node) bool { 28 // ... processing BEFORE visiting children ... 29 // if ... should visit children ... { 30 // ir.DoChildren(x, do) 31 // ... processing AFTER visiting children ... 32 // } 33 // if ... should stop parent DoChildren call from visiting siblings ... { 34 // return true 35 // } 36 // return false 37 // } 38 // do(root) 39 // 40 // Since DoChildren does not return true itself, if the do function 41 // never wants to stop the traversal, it can assume that DoChildren 42 // itself will always return false, simplifying to: 43 // 44 // var do func(ir.Node) bool 45 // do = func(x ir.Node) bool { 46 // ... processing BEFORE visiting children ... 47 // if ... should visit children ... { 48 // ir.DoChildren(x, do) 49 // } 50 // ... processing AFTER visiting children ... 51 // return false 52 // } 53 // do(root) 54 // 55 // The Visit function illustrates a further simplification of the pattern, 56 // only processing before visiting children and never stopping: 57 // 58 // func Visit(n ir.Node, visit func(ir.Node)) { 59 // if n == nil { 60 // return 61 // } 62 // var do func(ir.Node) bool 63 // do = func(x ir.Node) bool { 64 // visit(x) 65 // return ir.DoChildren(x, do) 66 // } 67 // do(n) 68 // } 69 // 70 // The Any function illustrates a different simplification of the pattern, 71 // visiting each node and then its children, recursively, until finding 72 // a node x for which cond(x) returns true, at which point the entire 73 // traversal stops and returns true. 74 // 75 // func Any(n ir.Node, cond(ir.Node) bool) bool { 76 // if n == nil { 77 // return false 78 // } 79 // var do func(ir.Node) bool 80 // do = func(x ir.Node) bool { 81 // return cond(x) || ir.DoChildren(x, do) 82 // } 83 // return do(n) 84 // } 85 // 86 // Visit and Any are presented above as examples of how to use 87 // DoChildren effectively, but of course, usage that fits within the 88 // simplifications captured by Visit or Any will be best served 89 // by directly calling the ones provided by this package. 90 func DoChildren(n Node, do func(Node) bool) bool { 91 if n == nil { 92 return false 93 } 94 return n.doChildren(do) 95 } 96 97 // Visit visits each non-nil node x in the IR tree rooted at n 98 // in a depth-first preorder traversal, calling visit on each node visited. 99 func Visit(n Node, visit func(Node)) { 100 if n == nil { 101 return 102 } 103 var do func(Node) bool 104 do = func(x Node) bool { 105 visit(x) 106 return DoChildren(x, do) 107 } 108 do(n) 109 } 110 111 // VisitList calls Visit(x, visit) for each node x in the list. 112 func VisitList(list Nodes, visit func(Node)) { 113 for _, x := range list { 114 Visit(x, visit) 115 } 116 } 117 118 // Any looks for a non-nil node x in the IR tree rooted at n 119 // for which cond(x) returns true. 120 // Any considers nodes in a depth-first, preorder traversal. 121 // When Any finds a node x such that cond(x) is true, 122 // Any ends the traversal and returns true immediately. 123 // Otherwise Any returns false after completing the entire traversal. 124 func Any(n Node, cond func(Node) bool) bool { 125 if n == nil { 126 return false 127 } 128 var do func(Node) bool 129 do = func(x Node) bool { 130 return cond(x) || DoChildren(x, do) 131 } 132 return do(n) 133 } 134 135 // AnyList calls Any(x, cond) for each node x in the list, in order. 136 // If any call returns true, AnyList stops and returns true. 137 // Otherwise, AnyList returns false after calling Any(x, cond) 138 // for every x in the list. 139 func AnyList(list Nodes, cond func(Node) bool) bool { 140 for _, x := range list { 141 if Any(x, cond) { 142 return true 143 } 144 } 145 return false 146 } 147 148 // EditChildren edits the child nodes of n, replacing each child x with edit(x). 149 // 150 // Note that EditChildren(n, edit) only calls edit(x) for n's immediate children. 151 // If x's children should be processed, then edit(x) must call EditChildren(x, edit). 152 // 153 // EditChildren allows constructing general editing passes of the IR graph. 154 // The most general usage is: 155 // 156 // var edit func(ir.Node) ir.Node 157 // edit = func(x ir.Node) ir.Node { 158 // ... processing BEFORE editing children ... 159 // if ... should edit children ... { 160 // EditChildren(x, edit) 161 // ... processing AFTER editing children ... 162 // } 163 // ... return x ... 164 // } 165 // n = edit(n) 166 // 167 // EditChildren edits the node in place. To edit a copy, call Copy first. 168 // As an example, a simple deep copy implementation would be: 169 // 170 // func deepCopy(n ir.Node) ir.Node { 171 // var edit func(ir.Node) ir.Node 172 // edit = func(x ir.Node) ir.Node { 173 // x = ir.Copy(x) 174 // ir.EditChildren(x, edit) 175 // return x 176 // } 177 // return edit(n) 178 // } 179 // 180 // Of course, in this case it is better to call ir.DeepCopy than to build one anew. 181 func EditChildren(n Node, edit func(Node) Node) { 182 if n == nil { 183 return 184 } 185 n.editChildren(edit) 186 } 187