[dev.regabi] cmd/compile: rename ir.Find to ir.Any and update uses

ir.Find is called "any" in C#, Dart, Haskell, Python, R, Ruby, and Rust,
and "any_of" in C++, "anyMatch" in Java, "some" in JavaScript,
"exists in OCaml, and "existsb" in Coq.
(Thanks to Matthew Dempsky for the research.)

This CL changes Find to Any to use the mostly standard name.

It also updates wrapper helpers to use the any terminology:
	hasCall -> anyCall
	hasCallOrChan -> anyCallOrChan
	hasSideEffects -> anySideEffects

Unchanged are "hasNamedResults", "hasUniquePos", and "hasDefaultCase",
which are all about a single node, not any node in the IR tree.

I also renamed hasFall to endsInFallthrough, since its semantics are
neither that of "any" nor that of the remaining "has" functions.

So the new terminology helps separate different kinds of predicates nicely.

Change-Id: I9bb3c9ebf060a30447224be09a5c34ad5244ea0d
Reviewed-on: https://go-review.googlesource.com/c/go/+/278912
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Russ Cox 2020-12-16 10:53:20 -05:00
parent aeedc9f804
commit 0b9cb63b8d
7 changed files with 37 additions and 37 deletions

View file

@ -741,7 +741,7 @@ func geneq(t *types.Type) *obj.LSym {
// return (or goto ret) // return (or goto ret)
fn.PtrBody().Append(nodSym(ir.OLABEL, nil, neq)) fn.PtrBody().Append(nodSym(ir.OLABEL, nil, neq))
fn.PtrBody().Append(ir.Nod(ir.OAS, nr, nodbool(false))) fn.PtrBody().Append(ir.Nod(ir.OAS, nr, nodbool(false)))
if EqCanPanic(t) || hasCall(fn) { if EqCanPanic(t) || anyCall(fn) {
// Epilogue is large, so share it with the equal case. // Epilogue is large, so share it with the equal case.
fn.PtrBody().Append(nodSym(ir.OGOTO, nil, ret)) fn.PtrBody().Append(nodSym(ir.OGOTO, nil, ret))
} else { } else {
@ -782,8 +782,8 @@ func geneq(t *types.Type) *obj.LSym {
return closure return closure
} }
func hasCall(fn *ir.Func) bool { func anyCall(fn *ir.Func) bool {
return ir.Find(fn, func(n ir.Node) bool { return ir.Any(fn, func(n ir.Node) bool {
// TODO(rsc): No methods? // TODO(rsc): No methods?
op := n.Op() op := n.Op()
return op == ir.OCALL || op == ir.OCALLFUNC return op == ir.OCALL || op == ir.OCALLFUNC

View file

@ -574,7 +574,7 @@ func evalConst(n ir.Node) ir.Node {
return origIntConst(n, int64(len(ir.StringVal(nl)))) return origIntConst(n, int64(len(ir.StringVal(nl))))
} }
case types.TARRAY: case types.TARRAY:
if !hasCallOrChan(nl) { if !anyCallOrChan(nl) {
return origIntConst(n, nl.Type().NumElem()) return origIntConst(n, nl.Type().NumElem())
} }
} }
@ -803,9 +803,9 @@ func isGoConst(n ir.Node) bool {
return n.Op() == ir.OLITERAL return n.Op() == ir.OLITERAL
} }
// hasCallOrChan reports whether n contains any calls or channel operations. // anyCallOrChan reports whether n contains any calls or channel operations.
func hasCallOrChan(n ir.Node) bool { func anyCallOrChan(n ir.Node) bool {
return ir.Find(n, func(n ir.Node) bool { return ir.Any(n, func(n ir.Node) bool {
switch n.Op() { switch n.Op() {
case ir.OAPPEND, case ir.OAPPEND,
ir.OCALL, ir.OCALL,

View file

@ -465,7 +465,7 @@ func (v *hairyVisitor) doNode(n ir.Node) error {
func isBigFunc(fn *ir.Func) bool { func isBigFunc(fn *ir.Func) bool {
budget := inlineBigFunctionNodes budget := inlineBigFunctionNodes
return ir.Find(fn, func(n ir.Node) bool { return ir.Any(fn, func(n ir.Node) bool {
budget-- budget--
return budget <= 0 return budget <= 0
}) })
@ -733,7 +733,7 @@ func reassigned(name *ir.Name) bool {
if name.Curfn == nil { if name.Curfn == nil {
return true return true
} }
return ir.Find(name.Curfn, func(n ir.Node) bool { return ir.Any(name.Curfn, func(n ir.Node) bool {
switch n.Op() { switch n.Op() {
case ir.OAS: case ir.OAS:
if n.Left() == name && n != name.Defn { if n.Left() == name && n != name.Defn {

View file

@ -60,7 +60,7 @@ func (s *InitSchedule) tryStaticInit(n ir.Node) bool {
if n.Op() != ir.OAS { if n.Op() != ir.OAS {
return false return false
} }
if ir.IsBlank(n.Left()) && !hasSideEffects(n.Right()) { if ir.IsBlank(n.Left()) && !anySideEffects(n.Right()) {
// Discard. // Discard.
return true return true
} }
@ -546,7 +546,7 @@ func fixedlit(ctxt initContext, kind initKind, n ir.Node, var_ ir.Node, init *ir
for _, r := range n.List().Slice() { for _, r := range n.List().Slice() {
a, value := splitnode(r) a, value := splitnode(r)
if a == ir.BlankNode && !hasSideEffects(value) { if a == ir.BlankNode && !anySideEffects(value) {
// Discard. // Discard.
continue continue
} }

View file

@ -302,7 +302,7 @@ func walkExprSwitch(sw *ir.SwitchStmt) {
// Process body. // Process body.
body.Append(npos(ncase.Pos(), nodSym(ir.OLABEL, nil, label))) body.Append(npos(ncase.Pos(), nodSym(ir.OLABEL, nil, label)))
body.Append(ncase.Body().Slice()...) body.Append(ncase.Body().Slice()...)
if fall, pos := hasFall(ncase.Body().Slice()); !fall { if fall, pos := endsInFallthrough(ncase.Body().Slice()); !fall {
br := ir.Nod(ir.OBREAK, nil, nil) br := ir.Nod(ir.OBREAK, nil, nil)
br.SetPos(pos) br.SetPos(pos)
body.Append(br) body.Append(br)
@ -481,8 +481,8 @@ func allCaseExprsAreSideEffectFree(sw *ir.SwitchStmt) bool {
return true return true
} }
// hasFall reports whether stmts ends with a "fallthrough" statement. // endsInFallthrough reports whether stmts ends with a "fallthrough" statement.
func hasFall(stmts []ir.Node) (bool, src.XPos) { func endsInFallthrough(stmts []ir.Node) (bool, src.XPos) {
// Search backwards for the index of the fallthrough // Search backwards for the index of the fallthrough
// statement. Do not assume it'll be in the last // statement. Do not assume it'll be in the last
// position, since in some cases (e.g. when the statement // position, since in some cases (e.g. when the statement

View file

@ -3765,9 +3765,9 @@ func usefield(n ir.Node) {
Curfn.FieldTrack[sym] = struct{}{} Curfn.FieldTrack[sym] = struct{}{}
} }
// hasSideEffects reports whether n contains any operations that could have observable side effects. // anySideEffects reports whether n contains any operations that could have observable side effects.
func hasSideEffects(n ir.Node) bool { func anySideEffects(n ir.Node) bool {
return ir.Find(n, func(n ir.Node) bool { return ir.Any(n, func(n ir.Node) bool {
switch n.Op() { switch n.Op() {
// Assume side effects unless we know otherwise. // Assume side effects unless we know otherwise.
default: default:

View file

@ -71,16 +71,16 @@ import (
// } // }
// } // }
// //
// The Find function illustrates a different simplification of the pattern, // The Any function illustrates a different simplification of the pattern,
// visiting each node and then its children, recursively, until finding // visiting each node and then its children, recursively, until finding
// a node x for which find(x) returns true, at which point the entire // a node x for which cond(x) returns true, at which point the entire
// traversal stops and returns true. // traversal stops and returns true.
// //
// func Find(n ir.Node, find func(ir.Node)) bool { // func Any(n ir.Node, find cond(ir.Node)) bool {
// stop := errors.New("stop") // stop := errors.New("stop")
// var do func(ir.Node) error // var do func(ir.Node) error
// do = func(x ir.Node) error { // do = func(x ir.Node) error {
// if find(x) { // if cond(x) {
// return stop // return stop
// } // }
// return ir.DoChildren(x, do) // return ir.DoChildren(x, do)
@ -88,9 +88,9 @@ import (
// return do(n) == stop // return do(n) == stop
// } // }
// //
// Visit and Find are presented above as examples of how to use // Visit and Any are presented above as examples of how to use
// DoChildren effectively, but of course, usage that fits within the // DoChildren effectively, but of course, usage that fits within the
// simplifications captured by Visit or Find will be best served // simplifications captured by Visit or Any will be best served
// by directly calling the ones provided by this package. // by directly calling the ones provided by this package.
func DoChildren(n Node, do func(Node) error) error { func DoChildren(n Node, do func(Node) error) error {
if n == nil { if n == nil {
@ -138,19 +138,19 @@ func VisitList(list Nodes, visit func(Node)) {
var stop = errors.New("stop") var stop = errors.New("stop")
// Find looks for a non-nil node x in the IR tree rooted at n // Any looks for a non-nil node x in the IR tree rooted at n
// for which find(x) returns true. // for which cond(x) returns true.
// Find considers nodes in a depth-first, preorder traversal. // Any considers nodes in a depth-first, preorder traversal.
// When Find finds a node x such that find(x) is true, // When Any finds a node x such that cond(x) is true,
// Find ends the traversal and returns true immediately. // Any ends the traversal and returns true immediately.
// Otherwise Find returns false after completing the entire traversal. // Otherwise Any returns false after completing the entire traversal.
func Find(n Node, find func(Node) bool) bool { func Any(n Node, cond func(Node) bool) bool {
if n == nil { if n == nil {
return false return false
} }
var do func(Node) error var do func(Node) error
do = func(x Node) error { do = func(x Node) error {
if find(x) { if cond(x) {
return stop return stop
} }
return DoChildren(x, do) return DoChildren(x, do)
@ -158,13 +158,13 @@ func Find(n Node, find func(Node) bool) bool {
return do(n) == stop return do(n) == stop
} }
// FindList calls Find(x, find) for each node x in the list, in order. // AnyList calls Any(x, cond) for each node x in the list, in order.
// If any call Find(x, find) returns true, FindList stops and // If any call returns true, AnyList stops and returns true.
// returns that result, skipping the remainder of the list. // Otherwise, AnyList returns false after calling Any(x, cond)
// Otherwise FindList returns false. // for every x in the list.
func FindList(list Nodes, find func(Node) bool) bool { func AnyList(list Nodes, cond func(Node) bool) bool {
for _, x := range list.Slice() { for _, x := range list.Slice() {
if Find(x, find) { if Any(x, cond) {
return true return true
} }
} }