Skip to content

Commit

Permalink
cmd/compile/internal/ir: remove OrigNode
Browse files Browse the repository at this point in the history
The OrigNode functionality used to be relevant to the typecheck
frontend, because we wanted to report errors using the same syntax as
the user originally wrote. However, now that types2 handles all
spec-required error diagnostics, there's no need to preserve original
nodes anymore.

Change-Id: I64a0540b8952513913021e7b84d165beb1f9f801
Reviewed-on: https://go-review.googlesource.com/c/go/+/526397
Reviewed-by: Keith Randall <[email protected]>
Reviewed-by: Cuong Manh Le <[email protected]>
Auto-Submit: Matthew Dempsky <[email protected]>
Reviewed-by: Keith Randall <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
  • Loading branch information
mdempsky authored and gopherbot committed Sep 8, 2023
1 parent 0a49d4a commit d2eab5f
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 67 deletions.
59 changes: 7 additions & 52 deletions src/cmd/compile/internal/ir/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,71 +5,26 @@
package ir

import (
"cmd/compile/internal/base"
"cmd/internal/src"
)

// A Node may implement the Orig and SetOrig method to
// maintain a pointer to the "unrewritten" form of a Node.
// If a Node does not implement OrigNode, it is its own Orig.
// Orig returns n.
//
// Note that both SepCopy and Copy have definitions compatible
// with a Node that does not implement OrigNode: such a Node
// is its own Orig, and in that case, that's what both want to return
// anyway (SepCopy unconditionally, and Copy only when the input
// is its own Orig as well, but if the output does not implement
// OrigNode, then neither does the input, making the condition true).
type OrigNode interface {
Node
Orig() Node
SetOrig(Node)
}

// origNode may be embedded into a Node to make it implement OrigNode.
type origNode struct {
orig Node `mknode:"-"`
}

func (n *origNode) Orig() Node { return n.orig }
func (n *origNode) SetOrig(o Node) { n.orig = o }

// Orig returns the “original” node for n.
// If n implements OrigNode, Orig returns n.Orig().
// Otherwise Orig returns n itself.
// TODO(mdempsky): Remove.
func Orig(n Node) Node {
if n, ok := n.(OrigNode); ok {
o := n.Orig()
if o == nil {
Dump("Orig nil", n)
base.Fatalf("Orig returned nil")
}
return o
}
return n
}

// SepCopy returns a separate shallow copy of n,
// breaking any Orig link to any other nodes.
// SepCopy returns a shallow copy of n.
//
// TODO(mdempsky): Replace with Copy.
func SepCopy(n Node) Node {
n = n.copy()
if n, ok := n.(OrigNode); ok {
n.SetOrig(n)
}
return n
return n.copy()
}

// Copy returns a shallow copy of n.
// If Orig(n) == n, then Orig(Copy(n)) == the copy.
// Otherwise the Orig link is preserved as well.
//
// The specific semantics surrounding Orig are subtle but right for most uses.
// See issues #26855 and #27765 for pitfalls.
func Copy(n Node) Node {
c := n.copy()
if n, ok := n.(OrigNode); ok && n.Orig() == n {
c.(OrigNode).SetOrig(c)
}
return c
return n.copy()
}

// DeepCopy returns a “deep” copy of n, with its entire structure copied
Expand Down
4 changes: 0 additions & 4 deletions src/cmd/compile/internal/ir/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ func (n *BinaryExpr) SetOp(op Op) {
// A CallExpr is a function call X(Args).
type CallExpr struct {
miniExpr
origNode
X Node
Args Nodes
RType Node `mknode:"-"` // see reflectdata/helpers.go
Expand All @@ -200,7 +199,6 @@ type CallExpr struct {
func NewCallExpr(pos src.XPos, op Op, fun Node, args []Node) *CallExpr {
n := &CallExpr{X: fun}
n.pos = pos
n.orig = n
n.SetOp(op)
n.Args = args
return n
Expand Down Expand Up @@ -234,7 +232,6 @@ type ClosureExpr struct {
// Before type-checking, the type is Ntype.
type CompLitExpr struct {
miniExpr
origNode
List Nodes // initialized values
RType Node `mknode:"-"` // *runtime._type for OMAPLIT map types
Prealloc *Name
Expand All @@ -251,7 +248,6 @@ func NewCompLitExpr(pos src.XPos, op Op, typ *types.Type, list []Node) *CompLitE
if typ != nil {
n.SetType(typ)
}
n.orig = n
return n
}

Expand Down
4 changes: 1 addition & 3 deletions src/cmd/compile/internal/ir/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,15 +373,13 @@ func NewRangeStmt(pos src.XPos, key, value, x Node, body []Node, distinctVars bo
// A ReturnStmt is a return statement.
type ReturnStmt struct {
miniStmt
origNode // for typecheckargs rewrite
Results Nodes // return list
Results Nodes // return list
}

func NewReturnStmt(pos src.XPos, results []Node) *ReturnStmt {
n := &ReturnStmt{}
n.pos = pos
n.op = ORETURN
n.orig = n
n.Results = results
return n
}
Expand Down
3 changes: 0 additions & 3 deletions src/cmd/compile/internal/typecheck/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,6 @@ func tcCompLit(n *ir.CompLitExpr) (res ir.Node) {
base.Pos = lno
}()

// Save original node (including n.Right)
n.SetOrig(ir.Copy(n))

ir.SetPos(n)

t := n.Type()
Expand Down
5 changes: 0 additions & 5 deletions src/cmd/compile/internal/typecheck/typecheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,11 +624,6 @@ func typecheckargs(n ir.InitNode) {
return
}

// Save n as n.Orig for fmt.go.
if ir.Orig(n) == n {
n.(ir.OrigNode).SetOrig(ir.SepCopy(n))
}

// Rewrite f(g()) into t1, t2, ... = g(); f(t1, t2, ...).
RewriteMultiValueCall(n, list[0])
}
Expand Down

0 comments on commit d2eab5f

Please sign in to comment.