From: David Chase Date: Fri, 30 Jan 2026 20:37:59 +0000 (+0200) Subject: cmd/compile: add node collapse/expand to html ast output X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=a3688ab13e76762a168f43e91ca9422c847ee896;p=gostls13.git cmd/compile: add node collapse/expand to html ast output AI-generated code, plus a lot of reviewing and tweaking of arrows and fonts. This adds subtree collapse/expand triangles. Change-Id: I2dd322abdf7ef956b1435946d79f864a6150f976 Reviewed-on: https://go-review.googlesource.com/c/go/+/740481 Reviewed-by: Keith Randall Reviewed-by: Keith Randall LUCI-TryBot-Result: Go LUCI --- diff --git a/src/cmd/compile/internal/ir/html.go b/src/cmd/compile/internal/ir/html.go index c17d4655ec..8a111c1dd4 100644 --- a/src/cmd/compile/internal/ir/html.go +++ b/src/cmd/compile/internal/ir/html.go @@ -96,6 +96,11 @@ func (w *HTMLWriter) Fatalf(msg string, args ...any) { base.FatalfAt(src.NoXPos, msg, args...) } +const ( + RIGHT_ARROW = "\u25BA" // click-to-open (is closed) + DOWN_ARROW = "\u25BC" // click-to-close (is open) +) + func (w *HTMLWriter) start() { if w == nil { return @@ -131,6 +136,10 @@ in that file, at that file:line, or at that file:line:column, respectively.
I locations are not treated as a single location, but as a sequence of locations that can be independently highlighted.

+

+Click on a ` + DOWN_ARROW + ` to collapse a subtree, or on a ` + RIGHT_ARROW + ` to expand a subtree. +

+ @@ -236,8 +245,26 @@ func (h *HTMLWriter) dumpNodesHTML(list Nodes, depth int) { } } +// indent prints indentation to w. +func (h *HTMLWriter) indentForToggle(depth int, hasChildren bool) { + h.Print("\n") + if depth == 0 { + return + } + for i := 0; i < depth-1; i++ { + h.Print(". ") + } + if hasChildren { + h.Print(". ") + } else { + h.Print(". ") + } +} + func (h *HTMLWriter) dumpNodeHTML(n Node, depth int) { - indent(h.w, depth) + hasChildren := nodeHasChildren(n) + h.indentForToggle(depth, hasChildren) + if depth > 40 { h.Print("...") return @@ -262,10 +289,16 @@ func (h *HTMLWriter) dumpNodeHTML(n Node, depth int) { h.Printf("", h.canonId(n)) defer h.Printf("") + if hasChildren { + h.Print(`` + DOWN_ARROW + ` `) // NOTE TRAILING SPACE after ! + } + if len(n.Init()) != 0 { + h.Print(``) h.Printf("%+v-init", n.Op()) h.dumpNodesHTML(n.Init(), depth+1) h.indent(depth) + h.Print(``) } switch n.Op() { @@ -319,6 +352,10 @@ func (h *HTMLWriter) dumpNodeHTML(n Node, depth int) { n := n.(*Func) h.Printf("%+v", n.Op()) h.dumpNodeHeaderHTML(n) + if hasChildren { + h.Print(``) + defer h.Print(``) + } fn := n if len(fn.Dcl) > 0 { h.indent(depth) @@ -341,6 +378,10 @@ func (h *HTMLWriter) dumpNodeHTML(n Node, depth int) { } return } + if hasChildren { + h.Print(``) + defer h.Print(``) + } v := reflect.ValueOf(n).Elem() t := reflect.TypeOf(n).Elem() @@ -395,6 +436,54 @@ func (h *HTMLWriter) dumpNodeHTML(n Node, depth int) { } } +func nodeHasChildren(n Node) bool { + if n == nil { + return false + } + if len(n.Init()) != 0 { + return true + } + switch n.Op() { + case OLITERAL, ONAME, ONONAME, OTYPE: + return false + case ODCLFUNC: + n := n.(*Func) + return len(n.Dcl) > 0 || len(n.ClosureVars) > 0 || len(n.Body) > 0 + } + + v := reflect.ValueOf(n).Elem() + t := reflect.TypeOf(n).Elem() + nf := t.NumField() + for i := 0; i < nf; i++ { + tf := t.Field(i) + vf := v.Field(i) + if tf.PkgPath != "" { + continue + } + switch tf.Type.Kind() { + case reflect.Interface, reflect.Ptr, reflect.Slice: + if vf.IsNil() { + continue + } + } + switch val := vf.Interface().(type) { + case Node: + return true + case Nodes: + if len(val) > 0 { + return true + } + default: + if vf.Kind() == reflect.Slice && vf.Type().Elem().Implements(nodeType) { + if vf.Len() > 0 { + return true + } + } + } + } + return false +} + func (h *HTMLWriter) dumpNodeHeaderHTML(n Node) { // print pointer to be able to see identical nodes if base.Debug.DumpPtrs != 0 { @@ -666,6 +755,14 @@ body.darkmode text { /* Capture alternative for outline-black and ellipse.outline-black when in dark mode */ body.darkmode .outline-black { outline: gray solid 2px; } +.toggle { + cursor: pointer; + display: inline-block; + text-align: center; + user-select: none; + font-size: 12px; // hand-tweaked +} + ` @@ -921,6 +1018,26 @@ function toggleDarkMode() { } } +function toggle_node(e) { + event.stopPropagation(); + var parent = e.parentNode; + var children = parent.children; + for (var i = 0; i < children.length; i++) { + if (children[i].classList.contains("node-body")) { + if (children[i].style.display == "none") { + children[i].style.display = ""; + } else { + children[i].style.display = "none"; + } + } + } + if (e.innerText == "` + RIGHT_ARROW + `") { + e.innerText = "` + DOWN_ARROW + `"; + } else { + e.innerText = "` + RIGHT_ARROW + `"; + } +} + ` )