aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDan Scales <danscales@google.com>2021-03-12 11:36:02 -0800
committerDan Scales <danscales@google.com>2021-03-17 16:53:00 +0000
commit70d54df4f6bd63b0057d718c6fc3fffc0d94bbc1 (patch)
tree662b46e0f1df451dff6305772ecd0c113a8c8641 /src
parent2f3db220d1ff1610e315d95d276782d4533f052b (diff)
downloadgo-70d54df4f6bd63b0057d718c6fc3fffc0d94bbc1.tar.xz
cmd/compile: getting more built-ins to work with generics
For Builtin ops, we currently stay with using the old typechecker to transform the call to a more specific expression and possibly use more specific ops. However, for a bunch of the ops, we delay calling the old typechecker if any of the args have type params, for a variety of reasons. In the near future, we will start creating separate functions that do the same transformations as the old typechecker for calls, builtins, indexing, comparisons, etc. These functions can then be called at noder time for nodes with no type params, and at stenciling time for nodes with type params. Remove unnecessary calls to types1 typechecker for most kinds of statements (still need it for SendStmt, AssignStmt, ReturnStmt, and SelectStmt). In particular, we don't need it for RangeStmt, and this avoids some complaints by the types1 typechecker on generic code. Other small changes: - Fix check on whether to delay calling types1-typechecker on type conversions. Should check if HasTParam is true, rather than if the type is directly a TYPEPARAM. - Don't call types1-typechecker on an indexing operation if the left operand has a typeparam in its type and is not obviously a TMAP, TSLICE, or TARRAY. As above, we will eventually have to create a new function that can do the required transformations (for complicated cases) at noder time or stenciling time. - Copy n.BuiltinOp in subster.node() - The complex arithmetic example in absdiff.go now works. - Added new tests double.go and append.go - Added new example with a new() call in settable.go Change-Id: I8f377afb6126cab1826bd3c2732aa8cdf1f7e0b4 Reviewed-on: https://go-review.googlesource.com/c/go/+/301951 Run-TryBot: Dan Scales <danscales@google.com> TryBot-Result: Go Bot <gobot@golang.org> Trust: Dan Scales <danscales@google.com> Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
Diffstat (limited to 'src')
-rw-r--r--src/cmd/compile/internal/noder/expr.go2
-rw-r--r--src/cmd/compile/internal/noder/helpers.go56
-rw-r--r--src/cmd/compile/internal/noder/stencil.go23
-rw-r--r--src/cmd/compile/internal/noder/stmt.go29
4 files changed, 86 insertions, 24 deletions
diff --git a/src/cmd/compile/internal/noder/expr.go b/src/cmd/compile/internal/noder/expr.go
index 989ebf236e..1ca5552879 100644
--- a/src/cmd/compile/internal/noder/expr.go
+++ b/src/cmd/compile/internal/noder/expr.go
@@ -135,7 +135,7 @@ func (g *irgen) expr0(typ types2.Type, expr syntax.Expr) ir.Node {
index := g.expr(expr.Index)
if index.Op() != ir.OTYPE {
// This is just a normal index expression
- return Index(pos, g.expr(expr.X), index)
+ return Index(pos, g.typ(typ), g.expr(expr.X), index)
}
// This is generic function instantiation with a single type
targs = []ir.Node{index}
diff --git a/src/cmd/compile/internal/noder/helpers.go b/src/cmd/compile/internal/noder/helpers.go
index cf7a3e22b3..1210d4b58c 100644
--- a/src/cmd/compile/internal/noder/helpers.go
+++ b/src/cmd/compile/internal/noder/helpers.go
@@ -83,11 +83,12 @@ func Binary(pos src.XPos, op ir.Op, x, y ir.Node) ir.Node {
}
func Call(pos src.XPos, typ *types.Type, fun ir.Node, args []ir.Node, dots bool) ir.Node {
- // TODO(mdempsky): This should not be so difficult.
+ n := ir.NewCallExpr(pos, ir.OCALL, fun, args)
+ n.IsDDD = dots
+
if fun.Op() == ir.OTYPE {
// Actually a type conversion, not a function call.
- n := ir.NewCallExpr(pos, ir.OCALL, fun, args)
- if fun.Type().Kind() == types.TTYPEPARAM {
+ if fun.Type().HasTParam() || args[0].Type().HasTParam() {
// For type params, don't typecheck until we actually know
// the type.
return typed(typ, n)
@@ -96,9 +97,34 @@ func Call(pos src.XPos, typ *types.Type, fun ir.Node, args []ir.Node, dots bool)
}
if fun, ok := fun.(*ir.Name); ok && fun.BuiltinOp != 0 {
- // Call to a builtin function.
- n := ir.NewCallExpr(pos, ir.OCALL, fun, args)
- n.IsDDD = dots
+ // For Builtin ops, we currently stay with using the old
+ // typechecker to transform the call to a more specific expression
+ // and possibly use more specific ops. However, for a bunch of the
+ // ops, we delay doing the old typechecker if any of the args have
+ // type params, for a variety of reasons:
+ //
+ // OMAKE: hard to choose specific ops OMAKESLICE, etc. until arg type is known
+ // OREAL/OIMAG: can't determine type float32/float64 until arg type know
+ // OLEN/OCAP: old typechecker will complain if arg is not obviously a slice/array.
+ // OAPPEND: old typechecker will complain if arg is not obviously slice, etc.
+ //
+ // We will eventually break out the transforming functionality
+ // needed for builtin's, and call it here or during stenciling, as
+ // appropriate.
+ switch fun.BuiltinOp {
+ case ir.OMAKE, ir.OREAL, ir.OIMAG, ir.OLEN, ir.OCAP, ir.OAPPEND:
+ hasTParam := false
+ for _, arg := range args {
+ if arg.Type().HasTParam() {
+ hasTParam = true
+ break
+ }
+ }
+ if hasTParam {
+ return typed(typ, n)
+ }
+ }
+
switch fun.BuiltinOp {
case ir.OCLOSE, ir.ODELETE, ir.OPANIC, ir.OPRINT, ir.OPRINTN:
return typecheck.Stmt(n)
@@ -124,9 +150,6 @@ func Call(pos src.XPos, typ *types.Type, fun ir.Node, args []ir.Node, dots bool)
}
}
- n := ir.NewCallExpr(pos, ir.OCALL, fun, args)
- n.IsDDD = dots
-
if fun.Op() == ir.OXDOT {
if !fun.(*ir.SelectorExpr).X.Type().HasTParam() {
base.FatalfAt(pos, "Expecting type param receiver in %v", fun)
@@ -230,9 +253,18 @@ func method(typ *types.Type, index int) *types.Field {
return types.ReceiverBaseType(typ).Methods().Index(index)
}
-func Index(pos src.XPos, x, index ir.Node) ir.Node {
- // TODO(mdempsky): Avoid typecheck.Expr (which will call tcIndex)
- return typecheck.Expr(ir.NewIndexExpr(pos, x, index))
+func Index(pos src.XPos, typ *types.Type, x, index ir.Node) ir.Node {
+ n := ir.NewIndexExpr(pos, x, index)
+ // TODO(danscales): Temporary fix. Need to separate out the
+ // transformations done by the old typechecker (in tcIndex()), to be
+ // called here or after stenciling.
+ if x.Type().HasTParam() && x.Type().Kind() != types.TMAP &&
+ x.Type().Kind() != types.TSLICE && x.Type().Kind() != types.TARRAY {
+ // Old typechecker will complain if arg is not obviously a slice/array/map.
+ typed(typ, n)
+ return n
+ }
+ return typecheck.Expr(n)
}
func Slice(pos src.XPos, x, low, high, max ir.Node) ir.Node {
diff --git a/src/cmd/compile/internal/noder/stencil.go b/src/cmd/compile/internal/noder/stencil.go
index 64b3a942e2..55aee9b6ff 100644
--- a/src/cmd/compile/internal/noder/stencil.go
+++ b/src/cmd/compile/internal/noder/stencil.go
@@ -330,8 +330,13 @@ func (subst *subster) node(n ir.Node) ir.Node {
m.SetIsClosureVar(true)
}
t := x.Type()
- newt := subst.typ(t)
- m.SetType(newt)
+ if t == nil {
+ assert(name.BuiltinOp != 0)
+ } else {
+ newt := subst.typ(t)
+ m.SetType(newt)
+ }
+ m.BuiltinOp = name.BuiltinOp
m.Curfn = subst.newf
m.Class = name.Class
m.Func = name.Func
@@ -396,11 +401,23 @@ func (subst *subster) node(n ir.Node) ir.Node {
// that the OXDOT was resolved.
call.SetTypecheck(0)
typecheck.Call(call)
+ } else if name := call.X.Name(); name != nil {
+ switch name.BuiltinOp {
+ case ir.OMAKE, ir.OREAL, ir.OIMAG, ir.OLEN, ir.OCAP, ir.OAPPEND:
+ // Call old typechecker (to do any
+ // transformations) now that we know the
+ // type of the args.
+ m.SetTypecheck(0)
+ m = typecheck.Expr(m)
+ default:
+ base.FatalfAt(call.Pos(), "Unexpected builtin op")
+ }
+
} else if call.X.Op() != ir.OFUNCINST {
// A call with an OFUNCINST will get typechecked
// in stencil() once we have created & attached the
// instantiation to be called.
- base.FatalfAt(call.Pos(), "Expecting OCALLPART or OTYPE or OFUNCINST with CALL")
+ base.FatalfAt(call.Pos(), "Expecting OCALLPART or OTYPE or OFUNCINST or builtin with CALL")
}
}
diff --git a/src/cmd/compile/internal/noder/stmt.go b/src/cmd/compile/internal/noder/stmt.go
index 1775116f41..31c6bfe5c8 100644
--- a/src/cmd/compile/internal/noder/stmt.go
+++ b/src/cmd/compile/internal/noder/stmt.go
@@ -28,7 +28,11 @@ func (g *irgen) stmts(stmts []syntax.Stmt) []ir.Node {
func (g *irgen) stmt(stmt syntax.Stmt) ir.Node {
// TODO(mdempsky): Remove dependency on typecheck.
- return typecheck.Stmt(g.stmt0(stmt))
+ n := g.stmt0(stmt)
+ if n != nil {
+ n.SetTypecheck(1)
+ }
+ return n
}
func (g *irgen) stmt0(stmt syntax.Stmt) ir.Node {
@@ -46,17 +50,20 @@ func (g *irgen) stmt0(stmt syntax.Stmt) ir.Node {
}
return x
case *syntax.SendStmt:
- return ir.NewSendStmt(g.pos(stmt), g.expr(stmt.Chan), g.expr(stmt.Value))
+ n := ir.NewSendStmt(g.pos(stmt), g.expr(stmt.Chan), g.expr(stmt.Value))
+ // Need to do the AssignConv() in tcSend().
+ return typecheck.Stmt(n)
case *syntax.DeclStmt:
return ir.NewBlockStmt(g.pos(stmt), g.decls(stmt.DeclList))
case *syntax.AssignStmt:
if stmt.Op != 0 && stmt.Op != syntax.Def {
op := g.op(stmt.Op, binOps[:])
+ // May need to insert ConvExpr nodes on the args in tcArith
if stmt.Rhs == nil {
- return IncDec(g.pos(stmt), op, g.expr(stmt.Lhs))
+ return typecheck.Stmt(IncDec(g.pos(stmt), op, g.expr(stmt.Lhs)))
}
- return ir.NewAssignOpStmt(g.pos(stmt), op, g.expr(stmt.Lhs), g.expr(stmt.Rhs))
+ return typecheck.Stmt(ir.NewAssignOpStmt(g.pos(stmt), op, g.expr(stmt.Lhs), g.expr(stmt.Rhs)))
}
names, lhs := g.assignList(stmt.Lhs, stmt.Op == syntax.Def)
@@ -65,25 +72,31 @@ func (g *irgen) stmt0(stmt syntax.Stmt) ir.Node {
if len(lhs) == 1 && len(rhs) == 1 {
n := ir.NewAssignStmt(g.pos(stmt), lhs[0], rhs[0])
n.Def = initDefn(n, names)
- return n
+ // Need to set Assigned in checkassign for maps
+ return typecheck.Stmt(n)
}
n := ir.NewAssignListStmt(g.pos(stmt), ir.OAS2, lhs, rhs)
n.Def = initDefn(n, names)
- return n
+ // Need to do tcAssignList().
+ return typecheck.Stmt(n)
case *syntax.BranchStmt:
return ir.NewBranchStmt(g.pos(stmt), g.tokOp(int(stmt.Tok), branchOps[:]), g.name(stmt.Label))
case *syntax.CallStmt:
return ir.NewGoDeferStmt(g.pos(stmt), g.tokOp(int(stmt.Tok), callOps[:]), g.expr(stmt.Call))
case *syntax.ReturnStmt:
- return ir.NewReturnStmt(g.pos(stmt), g.exprList(stmt.Results))
+ n := ir.NewReturnStmt(g.pos(stmt), g.exprList(stmt.Results))
+ // Need to do typecheckaste() for multiple return values
+ return typecheck.Stmt(n)
case *syntax.IfStmt:
return g.ifStmt(stmt)
case *syntax.ForStmt:
return g.forStmt(stmt)
case *syntax.SelectStmt:
- return g.selectStmt(stmt)
+ n := g.selectStmt(stmt)
+ // Need to convert assignments to OSELRECV2 in tcSelect()
+ return typecheck.Stmt(n)
case *syntax.SwitchStmt:
return g.switchStmt(stmt)