1
0
mirror of https://github.com/golang/go synced 2024-11-26 20:01:19 -07:00

cmd/compile/internal/ir: remove OrigNode

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 <khr@google.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Auto-Submit: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Matthew Dempsky 2023-09-06 16:05:11 -07:00 committed by Gopher Robot
parent 0a49d4a778
commit d2eab5ff19
5 changed files with 8 additions and 67 deletions

View File

@ -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

View File

@ -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
@ -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
@ -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
@ -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
}

View File

@ -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
}

View File

@ -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()

View File

@ -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])
}