diff options
author | Dan Scales <danscales@google.com> | 2021-03-12 11:36:02 -0800 |
---|---|---|
committer | Dan Scales <danscales@google.com> | 2021-03-17 16:53:00 +0000 |
commit | 70d54df4f6bd63b0057d718c6fc3fffc0d94bbc1 (patch) | |
tree | 662b46e0f1df451dff6305772ecd0c113a8c8641 /src/cmd/compile/internal/noder/helpers.go | |
parent | 2f3db220d1ff1610e315d95d276782d4533f052b (diff) | |
download | go-70d54df4f6bd63b0057d718c6fc3fffc0d94bbc1.tar.gz go-70d54df4f6bd63b0057d718c6fc3fffc0d94bbc1.zip |
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/cmd/compile/internal/noder/helpers.go')
-rw-r--r-- | src/cmd/compile/internal/noder/helpers.go | 56 |
1 files changed, 44 insertions, 12 deletions
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 { |