mirror of
https://github.com/golang/go.git
synced 2026-02-07 03:15:05 +03:00
cmd/compile: deduplication in the source code generated by mknode
Most types in the ir package don't have hidden children, so having their doChildrenWithHidden and editChildrenWithHidden methods call their own doChildren and editChildren methods can save a lot of duplicate code. Change-Id: Ib22a29d6a9a32855f3c3191ca2d26dff94ac556b Reviewed-on: https://go-review.googlesource.com/c/go/+/697476 Reviewed-by: Keith Randall <khr@golang.org> Auto-Submit: Keith Randall <khr@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Keith Randall <khr@google.com> Reviewed-by: Carlos Amedee <carlos@golang.org>
This commit is contained in:
committed by
Gopher Robot
parent
fa706ea50f
commit
ba840c1bf9
@@ -258,6 +258,7 @@ func processType(t *ast.TypeSpec) {
|
||||
var doChildrenWithHiddenBody strings.Builder
|
||||
var editChildrenBody strings.Builder
|
||||
var editChildrenWithHiddenBody strings.Builder
|
||||
var hasHidden bool
|
||||
for _, f := range fields {
|
||||
names := f.Names
|
||||
ft := f.Type
|
||||
@@ -309,6 +310,7 @@ func processType(t *ast.TypeSpec) {
|
||||
"if n.%s != nil {\nn.%s = edit(n.%s).(%s%s)\n}\n", name, name, name, ptr, ft)
|
||||
}
|
||||
if hidden {
|
||||
hasHidden = true
|
||||
continue
|
||||
}
|
||||
if isSlice {
|
||||
@@ -327,19 +329,27 @@ func processType(t *ast.TypeSpec) {
|
||||
}
|
||||
fmt.Fprintf(&buf, "func (n *%s) copy() Node {\nc := *n\n", name)
|
||||
buf.WriteString(copyBody.String())
|
||||
fmt.Fprintf(&buf, "return &c\n}\n")
|
||||
buf.WriteString("return &c\n}\n")
|
||||
fmt.Fprintf(&buf, "func (n *%s) doChildren(do func(Node) bool) bool {\n", name)
|
||||
buf.WriteString(doChildrenBody.String())
|
||||
fmt.Fprintf(&buf, "return false\n}\n")
|
||||
buf.WriteString("return false\n}\n")
|
||||
fmt.Fprintf(&buf, "func (n *%s) doChildrenWithHidden(do func(Node) bool) bool {\n", name)
|
||||
buf.WriteString(doChildrenWithHiddenBody.String())
|
||||
fmt.Fprintf(&buf, "return false\n}\n")
|
||||
if hasHidden {
|
||||
buf.WriteString(doChildrenWithHiddenBody.String())
|
||||
buf.WriteString("return false\n}\n")
|
||||
} else {
|
||||
buf.WriteString("return n.doChildren(do)\n}\n")
|
||||
}
|
||||
fmt.Fprintf(&buf, "func (n *%s) editChildren(edit func(Node) Node) {\n", name)
|
||||
buf.WriteString(editChildrenBody.String())
|
||||
fmt.Fprintf(&buf, "}\n")
|
||||
buf.WriteString("}\n")
|
||||
fmt.Fprintf(&buf, "func (n *%s) editChildrenWithHidden(edit func(Node) Node) {\n", name)
|
||||
buf.WriteString(editChildrenWithHiddenBody.String())
|
||||
fmt.Fprintf(&buf, "}\n")
|
||||
if hasHidden {
|
||||
buf.WriteString(editChildrenWithHiddenBody.String())
|
||||
} else {
|
||||
buf.WriteString("n.editChildren(edit)\n")
|
||||
}
|
||||
buf.WriteString("}\n")
|
||||
}
|
||||
|
||||
func generateHelpers() {
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user