[dev.regabi] cmd/compile: generate case/comm clause functions in mknode.go

Passes toolstash -cmp.

Change-Id: I52e9d6f35f22d5d59ac6aad02011c5abaac45739
Reviewed-on: https://go-review.googlesource.com/c/go/+/279446
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Cuong Manh Le 2020-12-29 23:26:45 +07:00
parent b3e1ec97fd
commit 5cf3c87fa6
3 changed files with 96 additions and 60 deletions

View file

@ -136,6 +136,10 @@ func main() {
fmt.Fprintf(&buf, "}\n")
}
for _, name := range []string{"CaseClause", "CommClause"} {
sliceHelper(&buf, name)
}
out, err := format.Source(buf.Bytes())
if err != nil {
// write out mangled source so we can see the bug.
@ -148,6 +152,40 @@ func main() {
}
}
func sliceHelper(buf *bytes.Buffer, name string) {
tmpl := fmt.Sprintf(`
func copy%[1]ss(list []*%[2]s) []*%[2]s {
if list == nil {
return nil
}
c := make([]*%[2]s, len(list))
copy(c, list)
return c
}
func maybeDo%[1]ss(list []*%[2]s, err error, do func(Node) error) error {
if err != nil {
return err
}
for _, x := range list {
if x != nil {
if err := do(x); err != nil {
return err
}
}
}
return nil
}
func edit%[1]ss(list []*%[2]s, edit func(Node) Node) {
for i, x := range list {
if x != nil {
list[i] = edit(x).(*%[2]s)
}
}
}
`, strings.TrimSuffix(name, "Clause"), name)
fmt.Fprintln(buf, tmpl)
}
func forNodeFields(typName string, typ *types.Struct, f func(name string, is func(types.Type) bool)) {
for i, n := 0, typ.NumFields(); i < n; i++ {
v := typ.Field(i)

View file

@ -1015,3 +1015,61 @@ func (n *typeNode) doChildren(do func(Node) error) error {
}
func (n *typeNode) editChildren(edit func(Node) Node) {
}
func copyCases(list []*CaseClause) []*CaseClause {
if list == nil {
return nil
}
c := make([]*CaseClause, len(list))
copy(c, list)
return c
}
func maybeDoCases(list []*CaseClause, err error, do func(Node) error) error {
if err != nil {
return err
}
for _, x := range list {
if x != nil {
if err := do(x); err != nil {
return err
}
}
}
return nil
}
func editCases(list []*CaseClause, edit func(Node) Node) {
for i, x := range list {
if x != nil {
list[i] = edit(x).(*CaseClause)
}
}
}
func copyComms(list []*CommClause) []*CommClause {
if list == nil {
return nil
}
c := make([]*CommClause, len(list))
copy(c, list)
return c
}
func maybeDoComms(list []*CommClause, err error, do func(Node) error) error {
if err != nil {
return err
}
for _, x := range list {
if x != nil {
if err := do(x); err != nil {
return err
}
}
}
return nil
}
func editComms(list []*CommClause, edit func(Node) Node) {
for i, x := range list {
if x != nil {
list[i] = edit(x).(*CommClause)
}
}
}

View file

@ -184,36 +184,6 @@ func NewCaseStmt(pos src.XPos, list, body []Node) *CaseClause {
return n
}
// TODO(mdempsky): Generate these with mknode.go.
func copyCases(list []*CaseClause) []*CaseClause {
if list == nil {
return nil
}
c := make([]*CaseClause, len(list))
copy(c, list)
return c
}
func maybeDoCases(list []*CaseClause, err error, do func(Node) error) error {
if err != nil {
return err
}
for _, x := range list {
if x != nil {
if err := do(x); err != nil {
return err
}
}
}
return nil
}
func editCases(list []*CaseClause, edit func(Node) Node) {
for i, x := range list {
if x != nil {
list[i] = edit(x).(*CaseClause)
}
}
}
type CommClause struct {
miniStmt
Comm Node // communication case
@ -227,36 +197,6 @@ func NewCommStmt(pos src.XPos, comm Node, body []Node) *CommClause {
return n
}
// TODO(mdempsky): Generate these with mknode.go.
func copyComms(list []*CommClause) []*CommClause {
if list == nil {
return nil
}
c := make([]*CommClause, len(list))
copy(c, list)
return c
}
func maybeDoComms(list []*CommClause, err error, do func(Node) error) error {
if err != nil {
return err
}
for _, x := range list {
if x != nil {
if err := do(x); err != nil {
return err
}
}
}
return nil
}
func editComms(list []*CommClause, edit func(Node) Node) {
for i, x := range list {
if x != nil {
list[i] = edit(x).(*CommClause)
}
}
}
// A ForStmt is a non-range for loop: for Init; Cond; Post { Body }
// Op can be OFOR or OFORUNTIL (!Cond).
type ForStmt struct {