1
0
mirror of https://github.com/golang/go synced 2024-11-26 14:08:37 -07:00

cmd/compile/internal/typecheck: add NewMethodExpr helper

This CL refactors the common pattern for constructing OMETHEXPR nodes,
which is the most common use of ir.TypeNode currently.

Change-Id: I446a21af97ab5a4bc2f04bbd581c1ede8a5ede60
Reviewed-on: https://go-review.googlesource.com/c/go/+/520978
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Auto-Submit: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
This commit is contained in:
Matthew Dempsky 2023-08-18 18:03:24 -07:00 committed by Gopher Robot
parent 009ca22fe2
commit a2ec545e0a
3 changed files with 14 additions and 7 deletions

View File

@ -1404,8 +1404,7 @@ func (pr *pkgReader) dictNameOf(dict *readerDict) *ir.Name {
assertOffset("type param method exprs", dict.typeParamMethodExprsOffset())
for _, info := range dict.typeParamMethodExprs {
typeParam := dict.targs[info.typeParamIdx]
method := typecheck.Expr(ir.NewSelectorExpr(pos, ir.OXDOT, ir.TypeNode(typeParam), info.method)).(*ir.SelectorExpr)
assert(method.Op() == ir.OMETHEXPR)
method := typecheck.NewMethodExpr(pos, typeParam, info.method)
rsym := method.FuncName().Linksym()
assert(rsym.ABI() == obj.ABIInternal) // must be ABIInternal; see ir.OCFUNC in ssagen/ssa.go
@ -2274,7 +2273,7 @@ func (r *reader) expr() (res ir.Node) {
// expression (OMETHEXPR) and the receiver type is unshaped, then
// we can rely on a statically generated wrapper being available.
if method, ok := wrapperFn.(*ir.SelectorExpr); ok && method.Op() == ir.OMETHEXPR && !recv.HasShape() {
return typecheck.Expr(ir.NewSelectorExpr(pos, ir.OXDOT, ir.TypeNode(recv), method.Sel)).(*ir.SelectorExpr)
return typecheck.NewMethodExpr(pos, recv, method.Sel)
}
return r.methodExprWrap(origPos, recv, implicits, deref, addr, baseFn, dictPtr)
@ -2891,7 +2890,7 @@ func (r *reader) methodExpr() (wrapperFn, baseFn, dictPtr ir.Node) {
// For statically known instantiations, we can take advantage of
// the stenciled wrapper.
base.AssertfAt(!recv.HasShape(), pos, "shaped receiver %v", recv)
wrapperFn := typecheck.Expr(ir.NewSelectorExpr(pos, ir.OXDOT, ir.TypeNode(recv), sym)).(*ir.SelectorExpr)
wrapperFn := typecheck.NewMethodExpr(pos, recv, sym)
base.AssertfAt(types.Identical(sig, wrapperFn.Type()), pos, "wrapper %L does not have type %v", wrapperFn, sig)
return wrapperFn, shapedFn, dictPtr
@ -2899,7 +2898,7 @@ func (r *reader) methodExpr() (wrapperFn, baseFn, dictPtr ir.Node) {
// Simple method expression; no dictionary needed.
base.AssertfAt(!recv.HasShape() || recv.IsInterface(), pos, "shaped receiver %v", recv)
fn := typecheck.Expr(ir.NewSelectorExpr(pos, ir.OXDOT, ir.TypeNode(recv), sym)).(*ir.SelectorExpr)
fn := typecheck.NewMethodExpr(pos, recv, sym)
return fn, fn, nil
}
@ -2924,7 +2923,7 @@ func shapedMethodExpr(pos src.XPos, obj *ir.Name, sym *types.Sym) *ir.SelectorEx
// Construct an OMETHEXPR node.
recv := method.Type.Recv().Type
return typecheck.Expr(ir.NewSelectorExpr(pos, ir.OXDOT, ir.TypeNode(recv), sym)).(*ir.SelectorExpr)
return typecheck.NewMethodExpr(pos, recv, sym)
}
func (r *reader) multiExpr() []ir.Node {

View File

@ -62,7 +62,7 @@ func FixMethodCall(call *ir.CallExpr) {
dot := call.X.(*ir.SelectorExpr)
fn := Expr(ir.NewSelectorExpr(dot.Pos(), ir.OXDOT, ir.TypeNode(dot.X.Type()), dot.Selection.Sym))
fn := NewMethodExpr(dot.Pos(), dot.X.Type(), dot.Selection.Sym)
args := make([]ir.Node, 1+len(call.Args))
args[0] = dot.X

View File

@ -843,6 +843,14 @@ func Lookdot1(errnode ir.Node, s *types.Sym, t *types.Type, fs *types.Fields, do
return r
}
// NewMethodExpr returns an OMETHEXPR node representing method
// expression "recv.sym".
func NewMethodExpr(pos src.XPos, recv *types.Type, sym *types.Sym) *ir.SelectorExpr {
n := Expr(ir.NewSelectorExpr(pos, ir.OXDOT, ir.TypeNode(recv), sym)).(*ir.SelectorExpr)
base.Assert(n.Op() == ir.OMETHEXPR)
return n
}
// typecheckMethodExpr checks selector expressions (ODOT) where the
// base expression is a type expression (OTYPE).
func typecheckMethodExpr(n *ir.SelectorExpr) (res ir.Node) {