2021-02-03 15:45:26 -08:00
|
|
|
// Copyright 2021 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
// This file will evolve, since we plan to do a mix of stenciling and passing
|
|
|
|
|
// around dictionaries.
|
|
|
|
|
|
|
|
|
|
package noder
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"cmd/compile/internal/base"
|
|
|
|
|
"cmd/compile/internal/ir"
|
|
|
|
|
"cmd/compile/internal/typecheck"
|
|
|
|
|
"cmd/compile/internal/types"
|
2021-02-11 10:50:20 -08:00
|
|
|
"cmd/internal/src"
|
2021-02-03 15:45:26 -08:00
|
|
|
"fmt"
|
2021-02-11 10:50:20 -08:00
|
|
|
"strings"
|
2021-02-03 15:45:26 -08:00
|
|
|
)
|
|
|
|
|
|
cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:
- Add support for substituting in interfaces in subster.typ()
- Fill in the info for the methods for all instantiated generic types,
so those methods will be available for later typechecking (by the old
typechecker) when assigning an instantiated generic type to an
interface. We also want those methods available so we have the list
when we want to instantiate all methods of an instantiated type. We
have both for instantiated types encountered during the initial noder
phase, and for instantiated types created during stenciling of a
function/method.
- When we first create a fully-instantiated generic type (whether
during initial noder2 pass or while instantiating a method/function),
add it to a list so that all of its methods will also be
instantiated. This is needed so that an instantiated type can be
assigned to an interface.
- Properly substitute type names in the names of instantiated methods.
- New accessor methods for types.Type.RParam.
- To deal with generic types which are empty structs (or just don't use
their type params anywhere), we want to set HasTParam if a named type
has any type params that are not fully instantiated, even if the
type param is not used in the type.
- In subst.typ() and elsewhere, always set sym.Def for a new forwarding
type we are creating, so we always create a single unique type for
each generic type instantiation. This handles recursion within a
type, and also recursive relationships across many types or methods.
We remove the seen[] hashtable, which was serving the same purpose,
but for subst.typ() only. We now handle all kinds of recursive types.
- We don't seem to need to force types.CheckSize() on
created/substituted generic types anymore, so commented out for now.
- Add an RParams accessor to types2.Signature, and also a new
exported types2.AsSignature() function.
Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
|
|
|
// For catching problems as we add more features
|
|
|
|
|
// TODO(danscales): remove assertions or replace with base.FatalfAt()
|
|
|
|
|
func assert(p bool) {
|
|
|
|
|
if !p {
|
|
|
|
|
panic("assertion failed")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// stencil scans functions for instantiated generic function calls and creates the
|
|
|
|
|
// required instantiations for simple generic functions. It also creates
|
|
|
|
|
// instantiated methods for all fully-instantiated generic types that have been
|
|
|
|
|
// encountered already or new ones that are encountered during the stenciling
|
|
|
|
|
// process.
|
2021-02-03 15:45:26 -08:00
|
|
|
func (g *irgen) stencil() {
|
|
|
|
|
g.target.Stencils = make(map[*types.Sym]*ir.Func)
|
cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:
- Add support for substituting in interfaces in subster.typ()
- Fill in the info for the methods for all instantiated generic types,
so those methods will be available for later typechecking (by the old
typechecker) when assigning an instantiated generic type to an
interface. We also want those methods available so we have the list
when we want to instantiate all methods of an instantiated type. We
have both for instantiated types encountered during the initial noder
phase, and for instantiated types created during stenciling of a
function/method.
- When we first create a fully-instantiated generic type (whether
during initial noder2 pass or while instantiating a method/function),
add it to a list so that all of its methods will also be
instantiated. This is needed so that an instantiated type can be
assigned to an interface.
- Properly substitute type names in the names of instantiated methods.
- New accessor methods for types.Type.RParam.
- To deal with generic types which are empty structs (or just don't use
their type params anywhere), we want to set HasTParam if a named type
has any type params that are not fully instantiated, even if the
type param is not used in the type.
- In subst.typ() and elsewhere, always set sym.Def for a new forwarding
type we are creating, so we always create a single unique type for
each generic type instantiation. This handles recursion within a
type, and also recursive relationships across many types or methods.
We remove the seen[] hashtable, which was serving the same purpose,
but for subst.typ() only. We now handle all kinds of recursive types.
- We don't seem to need to force types.CheckSize() on
created/substituted generic types anymore, so commented out for now.
- Add an RParams accessor to types2.Signature, and also a new
exported types2.AsSignature() function.
Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
|
|
|
|
|
|
|
|
// Instantiate the methods of instantiated generic types that we have seen so far.
|
|
|
|
|
g.instantiateMethods()
|
|
|
|
|
|
2021-02-08 10:23:05 -08:00
|
|
|
// Don't use range(g.target.Decls) - we also want to process any new instantiated
|
|
|
|
|
// functions that are created during this loop, in order to handle generic
|
|
|
|
|
// functions calling other generic functions.
|
|
|
|
|
for i := 0; i < len(g.target.Decls); i++ {
|
|
|
|
|
decl := g.target.Decls[i]
|
2021-02-21 10:54:38 -08:00
|
|
|
|
|
|
|
|
// Look for function instantiations in bodies of non-generic
|
|
|
|
|
// functions or in global assignments (ignore global type and
|
|
|
|
|
// constant declarations).
|
|
|
|
|
switch decl.Op() {
|
|
|
|
|
case ir.ODCLFUNC:
|
|
|
|
|
if decl.Type().HasTParam() {
|
|
|
|
|
// Skip any generic functions
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case ir.OAS:
|
|
|
|
|
|
|
|
|
|
case ir.OAS2:
|
|
|
|
|
|
|
|
|
|
default:
|
2021-02-03 15:45:26 -08:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-21 10:54:38 -08:00
|
|
|
// For all non-generic code, search for any function calls using
|
|
|
|
|
// generic function instantiations. Then create the needed
|
|
|
|
|
// instantiated function if it hasn't been created yet, and change
|
|
|
|
|
// to calling that function directly.
|
2021-02-03 15:45:26 -08:00
|
|
|
modified := false
|
2021-02-21 10:54:38 -08:00
|
|
|
foundFuncInst := false
|
|
|
|
|
ir.Visit(decl, func(n ir.Node) {
|
|
|
|
|
if n.Op() == ir.OFUNCINST {
|
|
|
|
|
// We found a function instantiation that is not
|
|
|
|
|
// immediately called.
|
|
|
|
|
foundFuncInst = true
|
|
|
|
|
}
|
2021-03-13 22:41:51 -08:00
|
|
|
if n.Op() != ir.OCALL || n.(*ir.CallExpr).X.Op() != ir.OFUNCINST {
|
2021-02-03 15:45:26 -08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// We have found a function call using a generic function
|
|
|
|
|
// instantiation.
|
|
|
|
|
call := n.(*ir.CallExpr)
|
|
|
|
|
inst := call.X.(*ir.InstExpr)
|
cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:
- Add support for substituting in interfaces in subster.typ()
- Fill in the info for the methods for all instantiated generic types,
so those methods will be available for later typechecking (by the old
typechecker) when assigning an instantiated generic type to an
interface. We also want those methods available so we have the list
when we want to instantiate all methods of an instantiated type. We
have both for instantiated types encountered during the initial noder
phase, and for instantiated types created during stenciling of a
function/method.
- When we first create a fully-instantiated generic type (whether
during initial noder2 pass or while instantiating a method/function),
add it to a list so that all of its methods will also be
instantiated. This is needed so that an instantiated type can be
assigned to an interface.
- Properly substitute type names in the names of instantiated methods.
- New accessor methods for types.Type.RParam.
- To deal with generic types which are empty structs (or just don't use
their type params anywhere), we want to set HasTParam if a named type
has any type params that are not fully instantiated, even if the
type param is not used in the type.
- In subst.typ() and elsewhere, always set sym.Def for a new forwarding
type we are creating, so we always create a single unique type for
each generic type instantiation. This handles recursion within a
type, and also recursive relationships across many types or methods.
We remove the seen[] hashtable, which was serving the same purpose,
but for subst.typ() only. We now handle all kinds of recursive types.
- We don't seem to need to force types.CheckSize() on
created/substituted generic types anymore, so commented out for now.
- Add an RParams accessor to types2.Signature, and also a new
exported types2.AsSignature() function.
Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
|
|
|
st := g.getInstantiationForNode(inst)
|
2021-02-03 15:45:26 -08:00
|
|
|
// Replace the OFUNCINST with a direct reference to the
|
|
|
|
|
// new stenciled function
|
|
|
|
|
call.X = st.Nname
|
2021-02-11 10:50:20 -08:00
|
|
|
if inst.X.Op() == ir.OCALLPART {
|
|
|
|
|
// When we create an instantiation of a method
|
|
|
|
|
// call, we make it a function. So, move the
|
|
|
|
|
// receiver to be the first arg of the function
|
|
|
|
|
// call.
|
|
|
|
|
withRecv := make([]ir.Node, len(call.Args)+1)
|
|
|
|
|
dot := inst.X.(*ir.SelectorExpr)
|
|
|
|
|
withRecv[0] = dot.X
|
|
|
|
|
copy(withRecv[1:], call.Args)
|
|
|
|
|
call.Args = withRecv
|
|
|
|
|
}
|
2021-03-23 10:19:11 -07:00
|
|
|
// Transform the Call now, which changes OCALL
|
2021-03-13 22:41:51 -08:00
|
|
|
// to OCALLFUNC and does typecheckaste/assignconvfn.
|
2021-03-23 10:19:11 -07:00
|
|
|
transformCall(call)
|
2021-02-03 15:45:26 -08:00
|
|
|
modified = true
|
|
|
|
|
})
|
2021-02-21 10:54:38 -08:00
|
|
|
|
|
|
|
|
// If we found an OFUNCINST without a corresponding call in the
|
|
|
|
|
// above decl, then traverse the nodes of decl again (with
|
|
|
|
|
// EditChildren rather than Visit), where we actually change the
|
|
|
|
|
// OFUNCINST node to an ONAME for the instantiated function.
|
|
|
|
|
// EditChildren is more expensive than Visit, so we only do this
|
|
|
|
|
// in the infrequent case of an OFUNCINSt without a corresponding
|
|
|
|
|
// call.
|
|
|
|
|
if foundFuncInst {
|
|
|
|
|
var edit func(ir.Node) ir.Node
|
|
|
|
|
edit = func(x ir.Node) ir.Node {
|
|
|
|
|
if x.Op() == ir.OFUNCINST {
|
cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:
- Add support for substituting in interfaces in subster.typ()
- Fill in the info for the methods for all instantiated generic types,
so those methods will be available for later typechecking (by the old
typechecker) when assigning an instantiated generic type to an
interface. We also want those methods available so we have the list
when we want to instantiate all methods of an instantiated type. We
have both for instantiated types encountered during the initial noder
phase, and for instantiated types created during stenciling of a
function/method.
- When we first create a fully-instantiated generic type (whether
during initial noder2 pass or while instantiating a method/function),
add it to a list so that all of its methods will also be
instantiated. This is needed so that an instantiated type can be
assigned to an interface.
- Properly substitute type names in the names of instantiated methods.
- New accessor methods for types.Type.RParam.
- To deal with generic types which are empty structs (or just don't use
their type params anywhere), we want to set HasTParam if a named type
has any type params that are not fully instantiated, even if the
type param is not used in the type.
- In subst.typ() and elsewhere, always set sym.Def for a new forwarding
type we are creating, so we always create a single unique type for
each generic type instantiation. This handles recursion within a
type, and also recursive relationships across many types or methods.
We remove the seen[] hashtable, which was serving the same purpose,
but for subst.typ() only. We now handle all kinds of recursive types.
- We don't seem to need to force types.CheckSize() on
created/substituted generic types anymore, so commented out for now.
- Add an RParams accessor to types2.Signature, and also a new
exported types2.AsSignature() function.
Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
|
|
|
st := g.getInstantiationForNode(x.(*ir.InstExpr))
|
2021-02-21 10:54:38 -08:00
|
|
|
return st.Nname
|
|
|
|
|
}
|
|
|
|
|
ir.EditChildren(x, edit)
|
|
|
|
|
return x
|
|
|
|
|
}
|
|
|
|
|
edit(decl)
|
|
|
|
|
}
|
2021-02-03 15:45:26 -08:00
|
|
|
if base.Flag.W > 1 && modified {
|
|
|
|
|
ir.Dump(fmt.Sprintf("\nmodified %v", decl), decl)
|
|
|
|
|
}
|
cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:
- Add support for substituting in interfaces in subster.typ()
- Fill in the info for the methods for all instantiated generic types,
so those methods will be available for later typechecking (by the old
typechecker) when assigning an instantiated generic type to an
interface. We also want those methods available so we have the list
when we want to instantiate all methods of an instantiated type. We
have both for instantiated types encountered during the initial noder
phase, and for instantiated types created during stenciling of a
function/method.
- When we first create a fully-instantiated generic type (whether
during initial noder2 pass or while instantiating a method/function),
add it to a list so that all of its methods will also be
instantiated. This is needed so that an instantiated type can be
assigned to an interface.
- Properly substitute type names in the names of instantiated methods.
- New accessor methods for types.Type.RParam.
- To deal with generic types which are empty structs (or just don't use
their type params anywhere), we want to set HasTParam if a named type
has any type params that are not fully instantiated, even if the
type param is not used in the type.
- In subst.typ() and elsewhere, always set sym.Def for a new forwarding
type we are creating, so we always create a single unique type for
each generic type instantiation. This handles recursion within a
type, and also recursive relationships across many types or methods.
We remove the seen[] hashtable, which was serving the same purpose,
but for subst.typ() only. We now handle all kinds of recursive types.
- We don't seem to need to force types.CheckSize() on
created/substituted generic types anymore, so commented out for now.
- Add an RParams accessor to types2.Signature, and also a new
exported types2.AsSignature() function.
Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
|
|
|
// We may have seen new fully-instantiated generic types while
|
|
|
|
|
// instantiating any needed functions/methods in the above
|
|
|
|
|
// function. If so, instantiate all the methods of those types
|
|
|
|
|
// (which will then lead to more function/methods to scan in the loop).
|
|
|
|
|
g.instantiateMethods()
|
2021-02-03 15:45:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:
- Add support for substituting in interfaces in subster.typ()
- Fill in the info for the methods for all instantiated generic types,
so those methods will be available for later typechecking (by the old
typechecker) when assigning an instantiated generic type to an
interface. We also want those methods available so we have the list
when we want to instantiate all methods of an instantiated type. We
have both for instantiated types encountered during the initial noder
phase, and for instantiated types created during stenciling of a
function/method.
- When we first create a fully-instantiated generic type (whether
during initial noder2 pass or while instantiating a method/function),
add it to a list so that all of its methods will also be
instantiated. This is needed so that an instantiated type can be
assigned to an interface.
- Properly substitute type names in the names of instantiated methods.
- New accessor methods for types.Type.RParam.
- To deal with generic types which are empty structs (or just don't use
their type params anywhere), we want to set HasTParam if a named type
has any type params that are not fully instantiated, even if the
type param is not used in the type.
- In subst.typ() and elsewhere, always set sym.Def for a new forwarding
type we are creating, so we always create a single unique type for
each generic type instantiation. This handles recursion within a
type, and also recursive relationships across many types or methods.
We remove the seen[] hashtable, which was serving the same purpose,
but for subst.typ() only. We now handle all kinds of recursive types.
- We don't seem to need to force types.CheckSize() on
created/substituted generic types anymore, so commented out for now.
- Add an RParams accessor to types2.Signature, and also a new
exported types2.AsSignature() function.
Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
|
|
|
// instantiateMethods instantiates all the methods of all fully-instantiated
|
|
|
|
|
// generic types that have been added to g.instTypeList.
|
|
|
|
|
func (g *irgen) instantiateMethods() {
|
|
|
|
|
for i := 0; i < len(g.instTypeList); i++ {
|
|
|
|
|
typ := g.instTypeList[i]
|
|
|
|
|
// Get the base generic type by looking up the symbol of the
|
|
|
|
|
// generic (uninstantiated) name.
|
|
|
|
|
baseSym := typ.Sym().Pkg.Lookup(genericTypeName(typ.Sym()))
|
|
|
|
|
baseType := baseSym.Def.(*ir.Name).Type()
|
|
|
|
|
for j, m := range typ.Methods().Slice() {
|
|
|
|
|
name := m.Nname.(*ir.Name)
|
|
|
|
|
targs := make([]ir.Node, len(typ.RParams()))
|
|
|
|
|
for k, targ := range typ.RParams() {
|
|
|
|
|
targs[k] = ir.TypeNode(targ)
|
|
|
|
|
}
|
|
|
|
|
baseNname := baseType.Methods().Slice()[j].Nname.(*ir.Name)
|
|
|
|
|
name.Func = g.getInstantiation(baseNname, targs, true)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
g.instTypeList = nil
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// genericSym returns the name of the base generic type for the type named by
|
|
|
|
|
// sym. It simply returns the name obtained by removing everything after the
|
|
|
|
|
// first bracket ("[").
|
|
|
|
|
func genericTypeName(sym *types.Sym) string {
|
|
|
|
|
return sym.Name[0:strings.Index(sym.Name, "[")]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// getInstantiationForNode returns the function/method instantiation for a
|
|
|
|
|
// InstExpr node inst.
|
|
|
|
|
func (g *irgen) getInstantiationForNode(inst *ir.InstExpr) *ir.Func {
|
2021-02-11 10:50:20 -08:00
|
|
|
if meth, ok := inst.X.(*ir.SelectorExpr); ok {
|
cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:
- Add support for substituting in interfaces in subster.typ()
- Fill in the info for the methods for all instantiated generic types,
so those methods will be available for later typechecking (by the old
typechecker) when assigning an instantiated generic type to an
interface. We also want those methods available so we have the list
when we want to instantiate all methods of an instantiated type. We
have both for instantiated types encountered during the initial noder
phase, and for instantiated types created during stenciling of a
function/method.
- When we first create a fully-instantiated generic type (whether
during initial noder2 pass or while instantiating a method/function),
add it to a list so that all of its methods will also be
instantiated. This is needed so that an instantiated type can be
assigned to an interface.
- Properly substitute type names in the names of instantiated methods.
- New accessor methods for types.Type.RParam.
- To deal with generic types which are empty structs (or just don't use
their type params anywhere), we want to set HasTParam if a named type
has any type params that are not fully instantiated, even if the
type param is not used in the type.
- In subst.typ() and elsewhere, always set sym.Def for a new forwarding
type we are creating, so we always create a single unique type for
each generic type instantiation. This handles recursion within a
type, and also recursive relationships across many types or methods.
We remove the seen[] hashtable, which was serving the same purpose,
but for subst.typ() only. We now handle all kinds of recursive types.
- We don't seem to need to force types.CheckSize() on
created/substituted generic types anymore, so commented out for now.
- Add an RParams accessor to types2.Signature, and also a new
exported types2.AsSignature() function.
Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
|
|
|
return g.getInstantiation(meth.Selection.Nname.(*ir.Name), inst.Targs, true)
|
2021-02-11 10:50:20 -08:00
|
|
|
} else {
|
cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:
- Add support for substituting in interfaces in subster.typ()
- Fill in the info for the methods for all instantiated generic types,
so those methods will be available for later typechecking (by the old
typechecker) when assigning an instantiated generic type to an
interface. We also want those methods available so we have the list
when we want to instantiate all methods of an instantiated type. We
have both for instantiated types encountered during the initial noder
phase, and for instantiated types created during stenciling of a
function/method.
- When we first create a fully-instantiated generic type (whether
during initial noder2 pass or while instantiating a method/function),
add it to a list so that all of its methods will also be
instantiated. This is needed so that an instantiated type can be
assigned to an interface.
- Properly substitute type names in the names of instantiated methods.
- New accessor methods for types.Type.RParam.
- To deal with generic types which are empty structs (or just don't use
their type params anywhere), we want to set HasTParam if a named type
has any type params that are not fully instantiated, even if the
type param is not used in the type.
- In subst.typ() and elsewhere, always set sym.Def for a new forwarding
type we are creating, so we always create a single unique type for
each generic type instantiation. This handles recursion within a
type, and also recursive relationships across many types or methods.
We remove the seen[] hashtable, which was serving the same purpose,
but for subst.typ() only. We now handle all kinds of recursive types.
- We don't seem to need to force types.CheckSize() on
created/substituted generic types anymore, so commented out for now.
- Add an RParams accessor to types2.Signature, and also a new
exported types2.AsSignature() function.
Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
|
|
|
return g.getInstantiation(inst.X.(*ir.Name), inst.Targs, false)
|
2021-02-11 10:50:20 -08:00
|
|
|
}
|
cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:
- Add support for substituting in interfaces in subster.typ()
- Fill in the info for the methods for all instantiated generic types,
so those methods will be available for later typechecking (by the old
typechecker) when assigning an instantiated generic type to an
interface. We also want those methods available so we have the list
when we want to instantiate all methods of an instantiated type. We
have both for instantiated types encountered during the initial noder
phase, and for instantiated types created during stenciling of a
function/method.
- When we first create a fully-instantiated generic type (whether
during initial noder2 pass or while instantiating a method/function),
add it to a list so that all of its methods will also be
instantiated. This is needed so that an instantiated type can be
assigned to an interface.
- Properly substitute type names in the names of instantiated methods.
- New accessor methods for types.Type.RParam.
- To deal with generic types which are empty structs (or just don't use
their type params anywhere), we want to set HasTParam if a named type
has any type params that are not fully instantiated, even if the
type param is not used in the type.
- In subst.typ() and elsewhere, always set sym.Def for a new forwarding
type we are creating, so we always create a single unique type for
each generic type instantiation. This handles recursion within a
type, and also recursive relationships across many types or methods.
We remove the seen[] hashtable, which was serving the same purpose,
but for subst.typ() only. We now handle all kinds of recursive types.
- We don't seem to need to force types.CheckSize() on
created/substituted generic types anymore, so commented out for now.
- Add an RParams accessor to types2.Signature, and also a new
exported types2.AsSignature() function.
Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// getInstantiation gets the instantiantion of the function or method nameNode
|
|
|
|
|
// with the type arguments targs. If the instantiated function is not already
|
|
|
|
|
// cached, then it calls genericSubst to create the new instantiation.
|
|
|
|
|
func (g *irgen) getInstantiation(nameNode *ir.Name, targs []ir.Node, isMeth bool) *ir.Func {
|
|
|
|
|
sym := makeInstName(nameNode.Sym(), targs, isMeth)
|
2021-02-21 10:54:38 -08:00
|
|
|
st := g.target.Stencils[sym]
|
|
|
|
|
if st == nil {
|
|
|
|
|
// If instantiation doesn't exist yet, create it and add
|
|
|
|
|
// to the list of decls.
|
cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:
- Add support for substituting in interfaces in subster.typ()
- Fill in the info for the methods for all instantiated generic types,
so those methods will be available for later typechecking (by the old
typechecker) when assigning an instantiated generic type to an
interface. We also want those methods available so we have the list
when we want to instantiate all methods of an instantiated type. We
have both for instantiated types encountered during the initial noder
phase, and for instantiated types created during stenciling of a
function/method.
- When we first create a fully-instantiated generic type (whether
during initial noder2 pass or while instantiating a method/function),
add it to a list so that all of its methods will also be
instantiated. This is needed so that an instantiated type can be
assigned to an interface.
- Properly substitute type names in the names of instantiated methods.
- New accessor methods for types.Type.RParam.
- To deal with generic types which are empty structs (or just don't use
their type params anywhere), we want to set HasTParam if a named type
has any type params that are not fully instantiated, even if the
type param is not used in the type.
- In subst.typ() and elsewhere, always set sym.Def for a new forwarding
type we are creating, so we always create a single unique type for
each generic type instantiation. This handles recursion within a
type, and also recursive relationships across many types or methods.
We remove the seen[] hashtable, which was serving the same purpose,
but for subst.typ() only. We now handle all kinds of recursive types.
- We don't seem to need to force types.CheckSize() on
created/substituted generic types anymore, so commented out for now.
- Add an RParams accessor to types2.Signature, and also a new
exported types2.AsSignature() function.
Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
|
|
|
st = g.genericSubst(sym, nameNode, targs, isMeth)
|
2021-02-21 10:54:38 -08:00
|
|
|
g.target.Stencils[sym] = st
|
|
|
|
|
g.target.Decls = append(g.target.Decls, st)
|
|
|
|
|
if base.Flag.W > 1 {
|
|
|
|
|
ir.Dump(fmt.Sprintf("\nstenciled %v", st), st)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return st
|
|
|
|
|
}
|
|
|
|
|
|
cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:
- Add support for substituting in interfaces in subster.typ()
- Fill in the info for the methods for all instantiated generic types,
so those methods will be available for later typechecking (by the old
typechecker) when assigning an instantiated generic type to an
interface. We also want those methods available so we have the list
when we want to instantiate all methods of an instantiated type. We
have both for instantiated types encountered during the initial noder
phase, and for instantiated types created during stenciling of a
function/method.
- When we first create a fully-instantiated generic type (whether
during initial noder2 pass or while instantiating a method/function),
add it to a list so that all of its methods will also be
instantiated. This is needed so that an instantiated type can be
assigned to an interface.
- Properly substitute type names in the names of instantiated methods.
- New accessor methods for types.Type.RParam.
- To deal with generic types which are empty structs (or just don't use
their type params anywhere), we want to set HasTParam if a named type
has any type params that are not fully instantiated, even if the
type param is not used in the type.
- In subst.typ() and elsewhere, always set sym.Def for a new forwarding
type we are creating, so we always create a single unique type for
each generic type instantiation. This handles recursion within a
type, and also recursive relationships across many types or methods.
We remove the seen[] hashtable, which was serving the same purpose,
but for subst.typ() only. We now handle all kinds of recursive types.
- We don't seem to need to force types.CheckSize() on
created/substituted generic types anymore, so commented out for now.
- Add an RParams accessor to types2.Signature, and also a new
exported types2.AsSignature() function.
Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
|
|
|
// makeInstName makes the unique name for a stenciled generic function or method,
|
|
|
|
|
// based on the name of the function fy=nsym and the targs. It replaces any
|
|
|
|
|
// existing bracket type list in the name. makeInstName asserts that fnsym has
|
|
|
|
|
// brackets in its name if and only if hasBrackets is true.
|
|
|
|
|
// TODO(danscales): remove the assertions and the hasBrackets argument later.
|
|
|
|
|
//
|
|
|
|
|
// Names of declared generic functions have no brackets originally, so hasBrackets
|
|
|
|
|
// should be false. Names of generic methods already have brackets, since the new
|
|
|
|
|
// type parameter is specified in the generic type of the receiver (e.g. func
|
|
|
|
|
// (func (v *value[T]).set(...) { ... } has the original name (*value[T]).set.
|
|
|
|
|
//
|
|
|
|
|
// The standard naming is something like: 'genFn[int,bool]' for functions and
|
|
|
|
|
// '(*genType[int,bool]).methodName' for methods
|
|
|
|
|
func makeInstName(fnsym *types.Sym, targs []ir.Node, hasBrackets bool) *types.Sym {
|
|
|
|
|
b := bytes.NewBufferString("")
|
|
|
|
|
name := fnsym.Name
|
|
|
|
|
i := strings.Index(name, "[")
|
|
|
|
|
assert(hasBrackets == (i >= 0))
|
|
|
|
|
if i >= 0 {
|
|
|
|
|
b.WriteString(name[0:i])
|
|
|
|
|
} else {
|
|
|
|
|
b.WriteString(name)
|
|
|
|
|
}
|
2021-02-03 15:45:26 -08:00
|
|
|
b.WriteString("[")
|
2021-02-21 10:54:38 -08:00
|
|
|
for i, targ := range targs {
|
2021-02-03 15:45:26 -08:00
|
|
|
if i > 0 {
|
|
|
|
|
b.WriteString(",")
|
|
|
|
|
}
|
[dev.typeparams] cmd/compile: small fixes for stenciling
- Create the stencil name using targ.Type.String(), which handles cases
where, for example, a type argument is a pointer to a named type,
etc. *obj.
- Set name.Def properly for a new stenciled func (have the symbol point
back to the associated function node). Will be required when exporting.
- Add missing copying of Func field when making copies of Name nodes.
(On purpose (it seems), Name nodes don't have a copy() function, so
we have to copy all the needed fields explicitly.)
- Deal with nil type in subster.node(), which is the type of the return
value for a function that doesn't return anything.
- Fix min to match standard want/go form, and add in float tests. Changed
Got -> got in bunch of other typeparam tests.
- Add new tests index.go, settable.go, and smallest.go (similar to
examples in the type param proposal), some of which need the above
changes.
Change-Id: I09a72302bc1fd3635a326da92405222afa222e85
Reviewed-on: https://go-review.googlesource.com/c/go/+/291109
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-02-10 15:26:40 -08:00
|
|
|
b.WriteString(targ.Type().String())
|
2021-02-03 15:45:26 -08:00
|
|
|
}
|
|
|
|
|
b.WriteString("]")
|
cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:
- Add support for substituting in interfaces in subster.typ()
- Fill in the info for the methods for all instantiated generic types,
so those methods will be available for later typechecking (by the old
typechecker) when assigning an instantiated generic type to an
interface. We also want those methods available so we have the list
when we want to instantiate all methods of an instantiated type. We
have both for instantiated types encountered during the initial noder
phase, and for instantiated types created during stenciling of a
function/method.
- When we first create a fully-instantiated generic type (whether
during initial noder2 pass or while instantiating a method/function),
add it to a list so that all of its methods will also be
instantiated. This is needed so that an instantiated type can be
assigned to an interface.
- Properly substitute type names in the names of instantiated methods.
- New accessor methods for types.Type.RParam.
- To deal with generic types which are empty structs (or just don't use
their type params anywhere), we want to set HasTParam if a named type
has any type params that are not fully instantiated, even if the
type param is not used in the type.
- In subst.typ() and elsewhere, always set sym.Def for a new forwarding
type we are creating, so we always create a single unique type for
each generic type instantiation. This handles recursion within a
type, and also recursive relationships across many types or methods.
We remove the seen[] hashtable, which was serving the same purpose,
but for subst.typ() only. We now handle all kinds of recursive types.
- We don't seem to need to force types.CheckSize() on
created/substituted generic types anymore, so commented out for now.
- Add an RParams accessor to types2.Signature, and also a new
exported types2.AsSignature() function.
Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
|
|
|
if i >= 0 {
|
|
|
|
|
i2 := strings.Index(name[i:], "]")
|
|
|
|
|
assert(i2 >= 0)
|
|
|
|
|
b.WriteString(name[i+i2+1:])
|
|
|
|
|
}
|
2021-02-03 15:45:26 -08:00
|
|
|
return typecheck.Lookup(b.String())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Struct containing info needed for doing the substitution as we create the
|
|
|
|
|
// instantiation of a generic function with specified type arguments.
|
|
|
|
|
type subster struct {
|
cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:
- Add support for substituting in interfaces in subster.typ()
- Fill in the info for the methods for all instantiated generic types,
so those methods will be available for later typechecking (by the old
typechecker) when assigning an instantiated generic type to an
interface. We also want those methods available so we have the list
when we want to instantiate all methods of an instantiated type. We
have both for instantiated types encountered during the initial noder
phase, and for instantiated types created during stenciling of a
function/method.
- When we first create a fully-instantiated generic type (whether
during initial noder2 pass or while instantiating a method/function),
add it to a list so that all of its methods will also be
instantiated. This is needed so that an instantiated type can be
assigned to an interface.
- Properly substitute type names in the names of instantiated methods.
- New accessor methods for types.Type.RParam.
- To deal with generic types which are empty structs (or just don't use
their type params anywhere), we want to set HasTParam if a named type
has any type params that are not fully instantiated, even if the
type param is not used in the type.
- In subst.typ() and elsewhere, always set sym.Def for a new forwarding
type we are creating, so we always create a single unique type for
each generic type instantiation. This handles recursion within a
type, and also recursive relationships across many types or methods.
We remove the seen[] hashtable, which was serving the same purpose,
but for subst.typ() only. We now handle all kinds of recursive types.
- We don't seem to need to force types.CheckSize() on
created/substituted generic types anymore, so commented out for now.
- Add an RParams accessor to types2.Signature, and also a new
exported types2.AsSignature() function.
Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
|
|
|
g *irgen
|
|
|
|
|
isMethod bool // If a method is being instantiated
|
|
|
|
|
newf *ir.Func // Func node for the new stenciled function
|
|
|
|
|
tparams []*types.Field
|
|
|
|
|
targs []ir.Node
|
2021-02-03 15:45:26 -08:00
|
|
|
// The substitution map from name nodes in the generic function to the
|
|
|
|
|
// name nodes in the new stenciled function.
|
|
|
|
|
vars map[*ir.Name]*ir.Name
|
|
|
|
|
}
|
|
|
|
|
|
cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:
- Add support for substituting in interfaces in subster.typ()
- Fill in the info for the methods for all instantiated generic types,
so those methods will be available for later typechecking (by the old
typechecker) when assigning an instantiated generic type to an
interface. We also want those methods available so we have the list
when we want to instantiate all methods of an instantiated type. We
have both for instantiated types encountered during the initial noder
phase, and for instantiated types created during stenciling of a
function/method.
- When we first create a fully-instantiated generic type (whether
during initial noder2 pass or while instantiating a method/function),
add it to a list so that all of its methods will also be
instantiated. This is needed so that an instantiated type can be
assigned to an interface.
- Properly substitute type names in the names of instantiated methods.
- New accessor methods for types.Type.RParam.
- To deal with generic types which are empty structs (or just don't use
their type params anywhere), we want to set HasTParam if a named type
has any type params that are not fully instantiated, even if the
type param is not used in the type.
- In subst.typ() and elsewhere, always set sym.Def for a new forwarding
type we are creating, so we always create a single unique type for
each generic type instantiation. This handles recursion within a
type, and also recursive relationships across many types or methods.
We remove the seen[] hashtable, which was serving the same purpose,
but for subst.typ() only. We now handle all kinds of recursive types.
- We don't seem to need to force types.CheckSize() on
created/substituted generic types anymore, so commented out for now.
- Add an RParams accessor to types2.Signature, and also a new
exported types2.AsSignature() function.
Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
|
|
|
// genericSubst returns a new function with name newsym. The function is an
|
|
|
|
|
// instantiation of a generic function or method specified by namedNode with type
|
|
|
|
|
// args targs. For a method with a generic receiver, it returns an instantiated
|
|
|
|
|
// function type where the receiver becomes the first parameter. Otherwise the
|
|
|
|
|
// instantiated method would still need to be transformed by later compiler
|
|
|
|
|
// phases.
|
|
|
|
|
func (g *irgen) genericSubst(newsym *types.Sym, nameNode *ir.Name, targs []ir.Node, isMethod bool) *ir.Func {
|
2021-02-11 10:50:20 -08:00
|
|
|
var tparams []*types.Field
|
cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:
- Add support for substituting in interfaces in subster.typ()
- Fill in the info for the methods for all instantiated generic types,
so those methods will be available for later typechecking (by the old
typechecker) when assigning an instantiated generic type to an
interface. We also want those methods available so we have the list
when we want to instantiate all methods of an instantiated type. We
have both for instantiated types encountered during the initial noder
phase, and for instantiated types created during stenciling of a
function/method.
- When we first create a fully-instantiated generic type (whether
during initial noder2 pass or while instantiating a method/function),
add it to a list so that all of its methods will also be
instantiated. This is needed so that an instantiated type can be
assigned to an interface.
- Properly substitute type names in the names of instantiated methods.
- New accessor methods for types.Type.RParam.
- To deal with generic types which are empty structs (or just don't use
their type params anywhere), we want to set HasTParam if a named type
has any type params that are not fully instantiated, even if the
type param is not used in the type.
- In subst.typ() and elsewhere, always set sym.Def for a new forwarding
type we are creating, so we always create a single unique type for
each generic type instantiation. This handles recursion within a
type, and also recursive relationships across many types or methods.
We remove the seen[] hashtable, which was serving the same purpose,
but for subst.typ() only. We now handle all kinds of recursive types.
- We don't seem to need to force types.CheckSize() on
created/substituted generic types anymore, so commented out for now.
- Add an RParams accessor to types2.Signature, and also a new
exported types2.AsSignature() function.
Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
|
|
|
if isMethod {
|
2021-02-11 10:50:20 -08:00
|
|
|
// Get the type params from the method receiver (after skipping
|
|
|
|
|
// over any pointer)
|
cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:
- Add support for substituting in interfaces in subster.typ()
- Fill in the info for the methods for all instantiated generic types,
so those methods will be available for later typechecking (by the old
typechecker) when assigning an instantiated generic type to an
interface. We also want those methods available so we have the list
when we want to instantiate all methods of an instantiated type. We
have both for instantiated types encountered during the initial noder
phase, and for instantiated types created during stenciling of a
function/method.
- When we first create a fully-instantiated generic type (whether
during initial noder2 pass or while instantiating a method/function),
add it to a list so that all of its methods will also be
instantiated. This is needed so that an instantiated type can be
assigned to an interface.
- Properly substitute type names in the names of instantiated methods.
- New accessor methods for types.Type.RParam.
- To deal with generic types which are empty structs (or just don't use
their type params anywhere), we want to set HasTParam if a named type
has any type params that are not fully instantiated, even if the
type param is not used in the type.
- In subst.typ() and elsewhere, always set sym.Def for a new forwarding
type we are creating, so we always create a single unique type for
each generic type instantiation. This handles recursion within a
type, and also recursive relationships across many types or methods.
We remove the seen[] hashtable, which was serving the same purpose,
but for subst.typ() only. We now handle all kinds of recursive types.
- We don't seem to need to force types.CheckSize() on
created/substituted generic types anymore, so commented out for now.
- Add an RParams accessor to types2.Signature, and also a new
exported types2.AsSignature() function.
Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
|
|
|
recvType := nameNode.Type().Recv().Type
|
|
|
|
|
recvType = deref(recvType)
|
|
|
|
|
tparams = make([]*types.Field, len(recvType.RParams()))
|
|
|
|
|
for i, rparam := range recvType.RParams() {
|
2021-02-11 10:50:20 -08:00
|
|
|
tparams[i] = types.NewField(src.NoXPos, nil, rparam)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
tparams = nameNode.Type().TParams().Fields().Slice()
|
|
|
|
|
}
|
2021-02-03 15:45:26 -08:00
|
|
|
gf := nameNode.Func
|
cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:
- Add support for substituting in interfaces in subster.typ()
- Fill in the info for the methods for all instantiated generic types,
so those methods will be available for later typechecking (by the old
typechecker) when assigning an instantiated generic type to an
interface. We also want those methods available so we have the list
when we want to instantiate all methods of an instantiated type. We
have both for instantiated types encountered during the initial noder
phase, and for instantiated types created during stenciling of a
function/method.
- When we first create a fully-instantiated generic type (whether
during initial noder2 pass or while instantiating a method/function),
add it to a list so that all of its methods will also be
instantiated. This is needed so that an instantiated type can be
assigned to an interface.
- Properly substitute type names in the names of instantiated methods.
- New accessor methods for types.Type.RParam.
- To deal with generic types which are empty structs (or just don't use
their type params anywhere), we want to set HasTParam if a named type
has any type params that are not fully instantiated, even if the
type param is not used in the type.
- In subst.typ() and elsewhere, always set sym.Def for a new forwarding
type we are creating, so we always create a single unique type for
each generic type instantiation. This handles recursion within a
type, and also recursive relationships across many types or methods.
We remove the seen[] hashtable, which was serving the same purpose,
but for subst.typ() only. We now handle all kinds of recursive types.
- We don't seem to need to force types.CheckSize() on
created/substituted generic types anymore, so commented out for now.
- Add an RParams accessor to types2.Signature, and also a new
exported types2.AsSignature() function.
Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
|
|
|
// Pos of the instantiated function is same as the generic function
|
|
|
|
|
newf := ir.NewFunc(gf.Pos())
|
|
|
|
|
newf.Nname = ir.NewNameAt(gf.Pos(), newsym)
|
2021-02-03 15:45:26 -08:00
|
|
|
newf.Nname.Func = newf
|
|
|
|
|
newf.Nname.Defn = newf
|
cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:
- Add support for substituting in interfaces in subster.typ()
- Fill in the info for the methods for all instantiated generic types,
so those methods will be available for later typechecking (by the old
typechecker) when assigning an instantiated generic type to an
interface. We also want those methods available so we have the list
when we want to instantiate all methods of an instantiated type. We
have both for instantiated types encountered during the initial noder
phase, and for instantiated types created during stenciling of a
function/method.
- When we first create a fully-instantiated generic type (whether
during initial noder2 pass or while instantiating a method/function),
add it to a list so that all of its methods will also be
instantiated. This is needed so that an instantiated type can be
assigned to an interface.
- Properly substitute type names in the names of instantiated methods.
- New accessor methods for types.Type.RParam.
- To deal with generic types which are empty structs (or just don't use
their type params anywhere), we want to set HasTParam if a named type
has any type params that are not fully instantiated, even if the
type param is not used in the type.
- In subst.typ() and elsewhere, always set sym.Def for a new forwarding
type we are creating, so we always create a single unique type for
each generic type instantiation. This handles recursion within a
type, and also recursive relationships across many types or methods.
We remove the seen[] hashtable, which was serving the same purpose,
but for subst.typ() only. We now handle all kinds of recursive types.
- We don't seem to need to force types.CheckSize() on
created/substituted generic types anymore, so commented out for now.
- Add an RParams accessor to types2.Signature, and also a new
exported types2.AsSignature() function.
Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
|
|
|
newsym.Def = newf.Nname
|
2021-03-29 08:28:01 -07:00
|
|
|
ir.CurFunc = newf
|
cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:
- Add support for substituting in interfaces in subster.typ()
- Fill in the info for the methods for all instantiated generic types,
so those methods will be available for later typechecking (by the old
typechecker) when assigning an instantiated generic type to an
interface. We also want those methods available so we have the list
when we want to instantiate all methods of an instantiated type. We
have both for instantiated types encountered during the initial noder
phase, and for instantiated types created during stenciling of a
function/method.
- When we first create a fully-instantiated generic type (whether
during initial noder2 pass or while instantiating a method/function),
add it to a list so that all of its methods will also be
instantiated. This is needed so that an instantiated type can be
assigned to an interface.
- Properly substitute type names in the names of instantiated methods.
- New accessor methods for types.Type.RParam.
- To deal with generic types which are empty structs (or just don't use
their type params anywhere), we want to set HasTParam if a named type
has any type params that are not fully instantiated, even if the
type param is not used in the type.
- In subst.typ() and elsewhere, always set sym.Def for a new forwarding
type we are creating, so we always create a single unique type for
each generic type instantiation. This handles recursion within a
type, and also recursive relationships across many types or methods.
We remove the seen[] hashtable, which was serving the same purpose,
but for subst.typ() only. We now handle all kinds of recursive types.
- We don't seem to need to force types.CheckSize() on
created/substituted generic types anymore, so commented out for now.
- Add an RParams accessor to types2.Signature, and also a new
exported types2.AsSignature() function.
Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
|
|
|
|
|
|
|
|
assert(len(tparams) == len(targs))
|
2021-02-03 15:45:26 -08:00
|
|
|
|
|
|
|
|
subst := &subster{
|
cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:
- Add support for substituting in interfaces in subster.typ()
- Fill in the info for the methods for all instantiated generic types,
so those methods will be available for later typechecking (by the old
typechecker) when assigning an instantiated generic type to an
interface. We also want those methods available so we have the list
when we want to instantiate all methods of an instantiated type. We
have both for instantiated types encountered during the initial noder
phase, and for instantiated types created during stenciling of a
function/method.
- When we first create a fully-instantiated generic type (whether
during initial noder2 pass or while instantiating a method/function),
add it to a list so that all of its methods will also be
instantiated. This is needed so that an instantiated type can be
assigned to an interface.
- Properly substitute type names in the names of instantiated methods.
- New accessor methods for types.Type.RParam.
- To deal with generic types which are empty structs (or just don't use
their type params anywhere), we want to set HasTParam if a named type
has any type params that are not fully instantiated, even if the
type param is not used in the type.
- In subst.typ() and elsewhere, always set sym.Def for a new forwarding
type we are creating, so we always create a single unique type for
each generic type instantiation. This handles recursion within a
type, and also recursive relationships across many types or methods.
We remove the seen[] hashtable, which was serving the same purpose,
but for subst.typ() only. We now handle all kinds of recursive types.
- We don't seem to need to force types.CheckSize() on
created/substituted generic types anymore, so commented out for now.
- Add an RParams accessor to types2.Signature, and also a new
exported types2.AsSignature() function.
Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
|
|
|
g: g,
|
|
|
|
|
isMethod: isMethod,
|
|
|
|
|
newf: newf,
|
|
|
|
|
tparams: tparams,
|
|
|
|
|
targs: targs,
|
|
|
|
|
vars: make(map[*ir.Name]*ir.Name),
|
2021-02-03 15:45:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newf.Dcl = make([]*ir.Name, len(gf.Dcl))
|
|
|
|
|
for i, n := range gf.Dcl {
|
|
|
|
|
newf.Dcl[i] = subst.node(n).(*ir.Name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ugly: we have to insert the Name nodes of the parameters/results into
|
|
|
|
|
// the function type. The current function type has no Nname fields set,
|
|
|
|
|
// because it came via conversion from the types2 type.
|
cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:
- Add support for substituting in interfaces in subster.typ()
- Fill in the info for the methods for all instantiated generic types,
so those methods will be available for later typechecking (by the old
typechecker) when assigning an instantiated generic type to an
interface. We also want those methods available so we have the list
when we want to instantiate all methods of an instantiated type. We
have both for instantiated types encountered during the initial noder
phase, and for instantiated types created during stenciling of a
function/method.
- When we first create a fully-instantiated generic type (whether
during initial noder2 pass or while instantiating a method/function),
add it to a list so that all of its methods will also be
instantiated. This is needed so that an instantiated type can be
assigned to an interface.
- Properly substitute type names in the names of instantiated methods.
- New accessor methods for types.Type.RParam.
- To deal with generic types which are empty structs (or just don't use
their type params anywhere), we want to set HasTParam if a named type
has any type params that are not fully instantiated, even if the
type param is not used in the type.
- In subst.typ() and elsewhere, always set sym.Def for a new forwarding
type we are creating, so we always create a single unique type for
each generic type instantiation. This handles recursion within a
type, and also recursive relationships across many types or methods.
We remove the seen[] hashtable, which was serving the same purpose,
but for subst.typ() only. We now handle all kinds of recursive types.
- We don't seem to need to force types.CheckSize() on
created/substituted generic types anymore, so commented out for now.
- Add an RParams accessor to types2.Signature, and also a new
exported types2.AsSignature() function.
Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
|
|
|
oldt := nameNode.Type()
|
2021-02-11 10:50:20 -08:00
|
|
|
// We also transform a generic method type to the corresponding
|
|
|
|
|
// instantiated function type where the receiver is the first parameter.
|
|
|
|
|
newt := types.NewSignature(oldt.Pkg(), nil, nil,
|
|
|
|
|
subst.fields(ir.PPARAM, append(oldt.Recvs().FieldSlice(), oldt.Params().FieldSlice()...), newf.Dcl),
|
|
|
|
|
subst.fields(ir.PPARAMOUT, oldt.Results().FieldSlice(), newf.Dcl))
|
2021-02-03 15:45:26 -08:00
|
|
|
|
|
|
|
|
newf.Nname.Ntype = ir.TypeNode(newt)
|
|
|
|
|
newf.Nname.SetType(newt)
|
|
|
|
|
ir.MarkFunc(newf.Nname)
|
|
|
|
|
newf.SetTypecheck(1)
|
|
|
|
|
newf.Nname.SetTypecheck(1)
|
2021-03-29 08:28:01 -07:00
|
|
|
|
|
|
|
|
// Make sure name/type of newf is set before substituting the body.
|
|
|
|
|
newf.Body = subst.list(gf.Body)
|
|
|
|
|
ir.CurFunc = nil
|
|
|
|
|
|
2021-02-03 15:45:26 -08:00
|
|
|
return newf
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// node is like DeepCopy(), but creates distinct ONAME nodes, and also descends
|
|
|
|
|
// into closures. It substitutes type arguments for type parameters in all the new
|
|
|
|
|
// nodes.
|
|
|
|
|
func (subst *subster) node(n ir.Node) ir.Node {
|
|
|
|
|
// Use closure to capture all state needed by the ir.EditChildren argument.
|
|
|
|
|
var edit func(ir.Node) ir.Node
|
|
|
|
|
edit = func(x ir.Node) ir.Node {
|
|
|
|
|
switch x.Op() {
|
2021-02-08 10:23:05 -08:00
|
|
|
case ir.OTYPE:
|
|
|
|
|
return ir.TypeNode(subst.typ(x.Type()))
|
|
|
|
|
|
2021-02-03 15:45:26 -08:00
|
|
|
case ir.ONAME:
|
|
|
|
|
name := x.(*ir.Name)
|
|
|
|
|
if v := subst.vars[name]; v != nil {
|
|
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
m := ir.NewNameAt(name.Pos(), name.Sym())
|
2021-02-21 10:54:38 -08:00
|
|
|
if name.IsClosureVar() {
|
|
|
|
|
m.SetIsClosureVar(true)
|
|
|
|
|
}
|
2021-02-03 15:45:26 -08:00
|
|
|
t := x.Type()
|
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>
2021-03-12 11:36:02 -08:00
|
|
|
if t == nil {
|
|
|
|
|
assert(name.BuiltinOp != 0)
|
|
|
|
|
} else {
|
|
|
|
|
newt := subst.typ(t)
|
|
|
|
|
m.SetType(newt)
|
|
|
|
|
}
|
|
|
|
|
m.BuiltinOp = name.BuiltinOp
|
2021-02-03 15:45:26 -08:00
|
|
|
m.Curfn = subst.newf
|
|
|
|
|
m.Class = name.Class
|
[dev.typeparams] cmd/compile: small fixes for stenciling
- Create the stencil name using targ.Type.String(), which handles cases
where, for example, a type argument is a pointer to a named type,
etc. *obj.
- Set name.Def properly for a new stenciled func (have the symbol point
back to the associated function node). Will be required when exporting.
- Add missing copying of Func field when making copies of Name nodes.
(On purpose (it seems), Name nodes don't have a copy() function, so
we have to copy all the needed fields explicitly.)
- Deal with nil type in subster.node(), which is the type of the return
value for a function that doesn't return anything.
- Fix min to match standard want/go form, and add in float tests. Changed
Got -> got in bunch of other typeparam tests.
- Add new tests index.go, settable.go, and smallest.go (similar to
examples in the type param proposal), some of which need the above
changes.
Change-Id: I09a72302bc1fd3635a326da92405222afa222e85
Reviewed-on: https://go-review.googlesource.com/c/go/+/291109
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-02-10 15:26:40 -08:00
|
|
|
m.Func = name.Func
|
2021-02-03 15:45:26 -08:00
|
|
|
subst.vars[name] = m
|
|
|
|
|
m.SetTypecheck(1)
|
|
|
|
|
return m
|
|
|
|
|
case ir.OLITERAL, ir.ONIL:
|
|
|
|
|
if x.Sym() != nil {
|
|
|
|
|
return x
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
m := ir.Copy(x)
|
|
|
|
|
if _, isExpr := m.(ir.Expr); isExpr {
|
[dev.typeparams] cmd/compile: small fixes for stenciling
- Create the stencil name using targ.Type.String(), which handles cases
where, for example, a type argument is a pointer to a named type,
etc. *obj.
- Set name.Def properly for a new stenciled func (have the symbol point
back to the associated function node). Will be required when exporting.
- Add missing copying of Func field when making copies of Name nodes.
(On purpose (it seems), Name nodes don't have a copy() function, so
we have to copy all the needed fields explicitly.)
- Deal with nil type in subster.node(), which is the type of the return
value for a function that doesn't return anything.
- Fix min to match standard want/go form, and add in float tests. Changed
Got -> got in bunch of other typeparam tests.
- Add new tests index.go, settable.go, and smallest.go (similar to
examples in the type param proposal), some of which need the above
changes.
Change-Id: I09a72302bc1fd3635a326da92405222afa222e85
Reviewed-on: https://go-review.googlesource.com/c/go/+/291109
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-02-10 15:26:40 -08:00
|
|
|
t := x.Type()
|
|
|
|
|
if t == nil {
|
|
|
|
|
// t can be nil only if this is a call that has no
|
|
|
|
|
// return values, so allow that and otherwise give
|
|
|
|
|
// an error.
|
2021-02-21 10:54:38 -08:00
|
|
|
_, isCallExpr := m.(*ir.CallExpr)
|
|
|
|
|
_, isStructKeyExpr := m.(*ir.StructKeyExpr)
|
2021-03-13 22:41:51 -08:00
|
|
|
if !isCallExpr && !isStructKeyExpr && x.Op() != ir.OPANIC &&
|
|
|
|
|
x.Op() != ir.OCLOSE {
|
[dev.typeparams] cmd/compile: small fixes for stenciling
- Create the stencil name using targ.Type.String(), which handles cases
where, for example, a type argument is a pointer to a named type,
etc. *obj.
- Set name.Def properly for a new stenciled func (have the symbol point
back to the associated function node). Will be required when exporting.
- Add missing copying of Func field when making copies of Name nodes.
(On purpose (it seems), Name nodes don't have a copy() function, so
we have to copy all the needed fields explicitly.)
- Deal with nil type in subster.node(), which is the type of the return
value for a function that doesn't return anything.
- Fix min to match standard want/go form, and add in float tests. Changed
Got -> got in bunch of other typeparam tests.
- Add new tests index.go, settable.go, and smallest.go (similar to
examples in the type param proposal), some of which need the above
changes.
Change-Id: I09a72302bc1fd3635a326da92405222afa222e85
Reviewed-on: https://go-review.googlesource.com/c/go/+/291109
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-02-10 15:26:40 -08:00
|
|
|
base.Fatalf(fmt.Sprintf("Nil type for %v", x))
|
|
|
|
|
}
|
2021-02-21 10:54:38 -08:00
|
|
|
} else if x.Op() != ir.OCLOSURE {
|
[dev.typeparams] cmd/compile: small fixes for stenciling
- Create the stencil name using targ.Type.String(), which handles cases
where, for example, a type argument is a pointer to a named type,
etc. *obj.
- Set name.Def properly for a new stenciled func (have the symbol point
back to the associated function node). Will be required when exporting.
- Add missing copying of Func field when making copies of Name nodes.
(On purpose (it seems), Name nodes don't have a copy() function, so
we have to copy all the needed fields explicitly.)
- Deal with nil type in subster.node(), which is the type of the return
value for a function that doesn't return anything.
- Fix min to match standard want/go form, and add in float tests. Changed
Got -> got in bunch of other typeparam tests.
- Add new tests index.go, settable.go, and smallest.go (similar to
examples in the type param proposal), some of which need the above
changes.
Change-Id: I09a72302bc1fd3635a326da92405222afa222e85
Reviewed-on: https://go-review.googlesource.com/c/go/+/291109
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-02-10 15:26:40 -08:00
|
|
|
m.SetType(subst.typ(x.Type()))
|
|
|
|
|
}
|
2021-02-03 15:45:26 -08:00
|
|
|
}
|
|
|
|
|
ir.EditChildren(m, edit)
|
2021-02-08 14:33:51 -08:00
|
|
|
|
cmd/compile: replace calls to typecheck with transform functions
For additions, compares, and slices, create transform functions that do
just the transformations for those nodes by the typecheck package (given
that the code has been fully typechecked by types2). For nodes that have
no args with typeparams, we call these transform functions directly in
noder2. But for nodes that have args with typeparams, we have to delay
and call the tranform functions during stenciling, since we don't know
the specific types involved.
We indicate that a node still needs transformation by setting Typecheck
to a new value 3. This value means the current type of the node has been
set (via types2), but the node may still need transformation.
Had to export typcheck.IsCmp and typecheck.Assignop from the typecheck
package.
Added new tests list2.go (required delaying compare typecheck/transform
because of != compare in checkList) and adder.go (requires delaying add
typecheck/transform, since it can do addition for numbers or strings).
There are several more transformation functions needed for expressions
(indexing, calls, etc.) and several more complicated ones needed for
statements (mainly various kinds of assignments).
Change-Id: I7d89d13a4108308ea0304a4b815ab60b40c59b0a
Reviewed-on: https://go-review.googlesource.com/c/go/+/303091
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>
2021-03-18 14:36:39 -07:00
|
|
|
if x.Typecheck() == 3 {
|
|
|
|
|
// These are nodes whose transforms were delayed until
|
|
|
|
|
// their instantiated type was known.
|
2021-03-24 14:50:02 -07:00
|
|
|
m.SetTypecheck(1)
|
cmd/compile: replace calls to typecheck with transform functions
For additions, compares, and slices, create transform functions that do
just the transformations for those nodes by the typecheck package (given
that the code has been fully typechecked by types2). For nodes that have
no args with typeparams, we call these transform functions directly in
noder2. But for nodes that have args with typeparams, we have to delay
and call the tranform functions during stenciling, since we don't know
the specific types involved.
We indicate that a node still needs transformation by setting Typecheck
to a new value 3. This value means the current type of the node has been
set (via types2), but the node may still need transformation.
Had to export typcheck.IsCmp and typecheck.Assignop from the typecheck
package.
Added new tests list2.go (required delaying compare typecheck/transform
because of != compare in checkList) and adder.go (requires delaying add
typecheck/transform, since it can do addition for numbers or strings).
There are several more transformation functions needed for expressions
(indexing, calls, etc.) and several more complicated ones needed for
statements (mainly various kinds of assignments).
Change-Id: I7d89d13a4108308ea0304a4b815ab60b40c59b0a
Reviewed-on: https://go-review.googlesource.com/c/go/+/303091
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>
2021-03-18 14:36:39 -07:00
|
|
|
if typecheck.IsCmp(x.Op()) {
|
|
|
|
|
transformCompare(m.(*ir.BinaryExpr))
|
|
|
|
|
} else {
|
2021-03-23 10:19:11 -07:00
|
|
|
switch x.Op() {
|
2021-03-24 14:50:02 -07:00
|
|
|
case ir.OSLICE, ir.OSLICE3:
|
2021-03-23 10:19:11 -07:00
|
|
|
transformSlice(m.(*ir.SliceExpr))
|
|
|
|
|
|
|
|
|
|
case ir.OADD:
|
|
|
|
|
m = transformAdd(m.(*ir.BinaryExpr))
|
|
|
|
|
|
|
|
|
|
case ir.OINDEX:
|
|
|
|
|
transformIndex(m.(*ir.IndexExpr))
|
|
|
|
|
|
|
|
|
|
case ir.OAS2:
|
|
|
|
|
as2 := m.(*ir.AssignListStmt)
|
|
|
|
|
transformAssign(as2, as2.Lhs, as2.Rhs)
|
|
|
|
|
|
|
|
|
|
case ir.OAS:
|
|
|
|
|
as := m.(*ir.AssignStmt)
|
|
|
|
|
lhs, rhs := []ir.Node{as.X}, []ir.Node{as.Y}
|
|
|
|
|
transformAssign(as, lhs, rhs)
|
|
|
|
|
|
|
|
|
|
case ir.OASOP:
|
|
|
|
|
as := m.(*ir.AssignOpStmt)
|
|
|
|
|
transformCheckAssign(as, as.X)
|
|
|
|
|
|
2021-03-29 08:28:01 -07:00
|
|
|
case ir.ORETURN:
|
|
|
|
|
transformReturn(m.(*ir.ReturnStmt))
|
|
|
|
|
|
|
|
|
|
case ir.OSEND:
|
|
|
|
|
transformSend(m.(*ir.SendStmt))
|
|
|
|
|
|
2021-03-23 10:19:11 -07:00
|
|
|
default:
|
|
|
|
|
base.Fatalf("Unexpected node with Typecheck() == 3")
|
|
|
|
|
}
|
cmd/compile: replace calls to typecheck with transform functions
For additions, compares, and slices, create transform functions that do
just the transformations for those nodes by the typecheck package (given
that the code has been fully typechecked by types2). For nodes that have
no args with typeparams, we call these transform functions directly in
noder2. But for nodes that have args with typeparams, we have to delay
and call the tranform functions during stenciling, since we don't know
the specific types involved.
We indicate that a node still needs transformation by setting Typecheck
to a new value 3. This value means the current type of the node has been
set (via types2), but the node may still need transformation.
Had to export typcheck.IsCmp and typecheck.Assignop from the typecheck
package.
Added new tests list2.go (required delaying compare typecheck/transform
because of != compare in checkList) and adder.go (requires delaying add
typecheck/transform, since it can do addition for numbers or strings).
There are several more transformation functions needed for expressions
(indexing, calls, etc.) and several more complicated ones needed for
statements (mainly various kinds of assignments).
Change-Id: I7d89d13a4108308ea0304a4b815ab60b40c59b0a
Reviewed-on: https://go-review.googlesource.com/c/go/+/303091
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>
2021-03-18 14:36:39 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 11:56:46 -07:00
|
|
|
switch x.Op() {
|
|
|
|
|
case ir.OLITERAL:
|
|
|
|
|
t := m.Type()
|
|
|
|
|
if t != x.Type() {
|
|
|
|
|
// types2 will give us a constant with a type T,
|
|
|
|
|
// if an untyped constant is used with another
|
|
|
|
|
// operand of type T (in a provably correct way).
|
|
|
|
|
// When we substitute in the type args during
|
|
|
|
|
// stenciling, we now know the real type of the
|
|
|
|
|
// constant. We may then need to change the
|
|
|
|
|
// BasicLit.val to be the correct type (e.g.
|
|
|
|
|
// convert an int64Val constant to a floatVal
|
|
|
|
|
// constant).
|
|
|
|
|
m.SetType(types.UntypedInt) // use any untyped type for DefaultLit to work
|
|
|
|
|
m = typecheck.DefaultLit(m, t)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case ir.OXDOT:
|
2021-03-24 14:50:02 -07:00
|
|
|
// A method value/call via a type param will have been
|
|
|
|
|
// left as an OXDOT. When we see this during stenciling,
|
|
|
|
|
// finish the transformation, now that we have the
|
|
|
|
|
// instantiated receiver type. We need to do this now,
|
|
|
|
|
// since the access/selection to the method for the real
|
|
|
|
|
// type is very different from the selection for the type
|
|
|
|
|
// param. m will be transformed to an OCALLPART node. It
|
|
|
|
|
// will be transformed to an ODOTMETH or ODOTINTER node if
|
|
|
|
|
// we find in the OCALL case below that the method value
|
|
|
|
|
// is actually called.
|
|
|
|
|
transformDot(m.(*ir.SelectorExpr), false)
|
|
|
|
|
m.SetTypecheck(1)
|
2021-03-18 11:56:46 -07:00
|
|
|
|
|
|
|
|
case ir.OCALL:
|
2021-02-08 14:33:51 -08:00
|
|
|
call := m.(*ir.CallExpr)
|
2021-03-29 08:28:01 -07:00
|
|
|
switch call.X.Op() {
|
|
|
|
|
case ir.OTYPE:
|
2021-03-23 10:19:11 -07:00
|
|
|
// Transform the conversion, now that we know the
|
|
|
|
|
// type argument.
|
|
|
|
|
m = transformConvCall(m.(*ir.CallExpr))
|
2021-03-29 08:28:01 -07:00
|
|
|
|
|
|
|
|
case ir.OCALLPART:
|
2021-03-24 14:50:02 -07:00
|
|
|
// Redo the transformation of OXDOT, now that we
|
2021-03-23 10:19:11 -07:00
|
|
|
// know the method value is being called. Then
|
|
|
|
|
// transform the call.
|
2021-02-09 15:13:19 -08:00
|
|
|
call.X.(*ir.SelectorExpr).SetOp(ir.OXDOT)
|
2021-03-24 14:50:02 -07:00
|
|
|
transformDot(call.X.(*ir.SelectorExpr), true)
|
2021-03-23 10:19:11 -07:00
|
|
|
transformCall(call)
|
2021-03-29 08:28:01 -07:00
|
|
|
|
|
|
|
|
case ir.ODOT, ir.ODOTPTR:
|
2021-03-14 13:46:23 -07:00
|
|
|
// An OXDOT for a generic receiver was resolved to
|
|
|
|
|
// an access to a field which has a function
|
2021-03-23 10:19:11 -07:00
|
|
|
// value. Transform the call to that function, now
|
2021-03-14 13:46:23 -07:00
|
|
|
// that the OXDOT was resolved.
|
2021-03-23 10:19:11 -07:00
|
|
|
transformCall(call)
|
2021-03-29 08:28:01 -07:00
|
|
|
|
|
|
|
|
case ir.ONAME:
|
|
|
|
|
name := call.X.Name()
|
|
|
|
|
if name.BuiltinOp != ir.OXXX {
|
|
|
|
|
switch name.BuiltinOp {
|
|
|
|
|
case ir.OMAKE, ir.OREAL, ir.OIMAG, ir.OLEN, ir.OCAP, ir.OAPPEND:
|
|
|
|
|
// Transform these builtins now that we
|
|
|
|
|
// know the type of the args.
|
|
|
|
|
m = transformBuiltin(call)
|
|
|
|
|
default:
|
|
|
|
|
base.FatalfAt(call.Pos(), "Unexpected builtin op")
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// This is the case of a function value that was a
|
|
|
|
|
// type parameter (implied to be a function via a
|
|
|
|
|
// structural constraint) which is now resolved.
|
|
|
|
|
transformCall(call)
|
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>
2021-03-12 11:36:02 -08:00
|
|
|
}
|
|
|
|
|
|
2021-03-29 08:28:01 -07:00
|
|
|
case ir.OCLOSURE:
|
|
|
|
|
transformCall(call)
|
|
|
|
|
|
|
|
|
|
case ir.OFUNCINST:
|
|
|
|
|
// A call with an OFUNCINST will get transformed
|
2021-03-13 22:41:51 -08:00
|
|
|
// in stencil() once we have created & attached the
|
|
|
|
|
// instantiation to be called.
|
2021-03-29 08:28:01 -07:00
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
base.FatalfAt(call.Pos(), fmt.Sprintf("Unexpected op with CALL during stenciling: %v", call.X.Op()))
|
2021-02-08 14:33:51 -08:00
|
|
|
}
|
|
|
|
|
|
2021-03-18 11:56:46 -07:00
|
|
|
case ir.OCLOSURE:
|
2021-02-03 15:45:26 -08:00
|
|
|
x := x.(*ir.ClosureExpr)
|
|
|
|
|
// Need to save/duplicate x.Func.Nname,
|
|
|
|
|
// x.Func.Nname.Ntype, x.Func.Dcl, x.Func.ClosureVars, and
|
|
|
|
|
// x.Func.Body.
|
|
|
|
|
oldfn := x.Func
|
|
|
|
|
newfn := ir.NewFunc(oldfn.Pos())
|
|
|
|
|
if oldfn.ClosureCalled() {
|
|
|
|
|
newfn.SetClosureCalled(true)
|
|
|
|
|
}
|
2021-02-21 10:54:38 -08:00
|
|
|
newfn.SetIsHiddenClosure(true)
|
2021-02-03 15:45:26 -08:00
|
|
|
m.(*ir.ClosureExpr).Func = newfn
|
cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:
- Add support for substituting in interfaces in subster.typ()
- Fill in the info for the methods for all instantiated generic types,
so those methods will be available for later typechecking (by the old
typechecker) when assigning an instantiated generic type to an
interface. We also want those methods available so we have the list
when we want to instantiate all methods of an instantiated type. We
have both for instantiated types encountered during the initial noder
phase, and for instantiated types created during stenciling of a
function/method.
- When we first create a fully-instantiated generic type (whether
during initial noder2 pass or while instantiating a method/function),
add it to a list so that all of its methods will also be
instantiated. This is needed so that an instantiated type can be
assigned to an interface.
- Properly substitute type names in the names of instantiated methods.
- New accessor methods for types.Type.RParam.
- To deal with generic types which are empty structs (or just don't use
their type params anywhere), we want to set HasTParam if a named type
has any type params that are not fully instantiated, even if the
type param is not used in the type.
- In subst.typ() and elsewhere, always set sym.Def for a new forwarding
type we are creating, so we always create a single unique type for
each generic type instantiation. This handles recursion within a
type, and also recursive relationships across many types or methods.
We remove the seen[] hashtable, which was serving the same purpose,
but for subst.typ() only. We now handle all kinds of recursive types.
- We don't seem to need to force types.CheckSize() on
created/substituted generic types anymore, so commented out for now.
- Add an RParams accessor to types2.Signature, and also a new
exported types2.AsSignature() function.
Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
|
|
|
// Closure name can already have brackets, if it derives
|
|
|
|
|
// from a generic method
|
|
|
|
|
newsym := makeInstName(oldfn.Nname.Sym(), subst.targs, subst.isMethod)
|
2021-02-21 10:54:38 -08:00
|
|
|
newfn.Nname = ir.NewNameAt(oldfn.Nname.Pos(), newsym)
|
|
|
|
|
newfn.Nname.Func = newfn
|
|
|
|
|
newfn.Nname.Defn = newfn
|
|
|
|
|
ir.MarkFunc(newfn.Nname)
|
|
|
|
|
newfn.OClosure = m.(*ir.ClosureExpr)
|
|
|
|
|
|
|
|
|
|
saveNewf := subst.newf
|
2021-03-29 08:28:01 -07:00
|
|
|
ir.CurFunc = newfn
|
2021-02-21 10:54:38 -08:00
|
|
|
subst.newf = newfn
|
|
|
|
|
newfn.Dcl = subst.namelist(oldfn.Dcl)
|
|
|
|
|
newfn.ClosureVars = subst.namelist(oldfn.ClosureVars)
|
|
|
|
|
|
|
|
|
|
// Set Ntype for now to be compatible with later parts of compiler
|
|
|
|
|
newfn.Nname.Ntype = subst.node(oldfn.Nname.Ntype).(ir.Ntype)
|
|
|
|
|
typed(subst.typ(oldfn.Nname.Type()), newfn.Nname)
|
2021-03-13 22:41:51 -08:00
|
|
|
typed(newfn.Nname.Type(), m)
|
2021-02-21 10:54:38 -08:00
|
|
|
newfn.SetTypecheck(1)
|
2021-03-29 08:28:01 -07:00
|
|
|
|
|
|
|
|
// Make sure type of closure function is set before doing body.
|
|
|
|
|
newfn.Body = subst.list(oldfn.Body)
|
|
|
|
|
subst.newf = saveNewf
|
|
|
|
|
ir.CurFunc = saveNewf
|
|
|
|
|
|
2021-02-21 10:54:38 -08:00
|
|
|
subst.g.target.Decls = append(subst.g.target.Decls, newfn)
|
2021-02-03 15:45:26 -08:00
|
|
|
}
|
|
|
|
|
return m
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return edit(n)
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-21 10:54:38 -08:00
|
|
|
func (subst *subster) namelist(l []*ir.Name) []*ir.Name {
|
|
|
|
|
s := make([]*ir.Name, len(l))
|
|
|
|
|
for i, n := range l {
|
|
|
|
|
s[i] = subst.node(n).(*ir.Name)
|
|
|
|
|
if n.Defn != nil {
|
|
|
|
|
s[i].Defn = subst.node(n.Defn)
|
|
|
|
|
}
|
|
|
|
|
if n.Outer != nil {
|
|
|
|
|
s[i].Outer = subst.node(n.Outer).(*ir.Name)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-03 15:45:26 -08:00
|
|
|
func (subst *subster) list(l []ir.Node) []ir.Node {
|
|
|
|
|
s := make([]ir.Node, len(l))
|
|
|
|
|
for i, n := range l {
|
|
|
|
|
s[i] = subst.node(n)
|
|
|
|
|
}
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-21 10:54:38 -08:00
|
|
|
// tstruct substitutes type params in types of the fields of a structure type. For
|
2021-03-14 20:13:05 -07:00
|
|
|
// each field, if Nname is set, tstruct also translates the Nname using
|
|
|
|
|
// subst.vars, if Nname is in subst.vars. To always force the creation of a new
|
|
|
|
|
// (top-level) struct, regardless of whether anything changed with the types or
|
|
|
|
|
// names of the struct's fields, set force to true.
|
|
|
|
|
func (subst *subster) tstruct(t *types.Type, force bool) *types.Type {
|
2021-02-08 14:33:51 -08:00
|
|
|
if t.NumFields() == 0 {
|
cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:
- Add support for substituting in interfaces in subster.typ()
- Fill in the info for the methods for all instantiated generic types,
so those methods will be available for later typechecking (by the old
typechecker) when assigning an instantiated generic type to an
interface. We also want those methods available so we have the list
when we want to instantiate all methods of an instantiated type. We
have both for instantiated types encountered during the initial noder
phase, and for instantiated types created during stenciling of a
function/method.
- When we first create a fully-instantiated generic type (whether
during initial noder2 pass or while instantiating a method/function),
add it to a list so that all of its methods will also be
instantiated. This is needed so that an instantiated type can be
assigned to an interface.
- Properly substitute type names in the names of instantiated methods.
- New accessor methods for types.Type.RParam.
- To deal with generic types which are empty structs (or just don't use
their type params anywhere), we want to set HasTParam if a named type
has any type params that are not fully instantiated, even if the
type param is not used in the type.
- In subst.typ() and elsewhere, always set sym.Def for a new forwarding
type we are creating, so we always create a single unique type for
each generic type instantiation. This handles recursion within a
type, and also recursive relationships across many types or methods.
We remove the seen[] hashtable, which was serving the same purpose,
but for subst.typ() only. We now handle all kinds of recursive types.
- We don't seem to need to force types.CheckSize() on
created/substituted generic types anymore, so commented out for now.
- Add an RParams accessor to types2.Signature, and also a new
exported types2.AsSignature() function.
Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
|
|
|
if t.HasTParam() {
|
|
|
|
|
// For an empty struct, we need to return a new type,
|
|
|
|
|
// since it may now be fully instantiated (HasTParam
|
|
|
|
|
// becomes false).
|
|
|
|
|
return types.NewStruct(t.Pkg(), nil)
|
|
|
|
|
}
|
2021-02-08 14:33:51 -08:00
|
|
|
return t
|
|
|
|
|
}
|
|
|
|
|
var newfields []*types.Field
|
2021-03-14 20:13:05 -07:00
|
|
|
if force {
|
|
|
|
|
newfields = make([]*types.Field, t.NumFields())
|
|
|
|
|
}
|
2021-02-08 14:33:51 -08:00
|
|
|
for i, f := range t.Fields().Slice() {
|
|
|
|
|
t2 := subst.typ(f.Type)
|
2021-02-21 10:54:38 -08:00
|
|
|
if (t2 != f.Type || f.Nname != nil) && newfields == nil {
|
2021-02-08 14:33:51 -08:00
|
|
|
newfields = make([]*types.Field, t.NumFields())
|
|
|
|
|
for j := 0; j < i; j++ {
|
|
|
|
|
newfields[j] = t.Field(j)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if newfields != nil {
|
cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:
- Add support for substituting in interfaces in subster.typ()
- Fill in the info for the methods for all instantiated generic types,
so those methods will be available for later typechecking (by the old
typechecker) when assigning an instantiated generic type to an
interface. We also want those methods available so we have the list
when we want to instantiate all methods of an instantiated type. We
have both for instantiated types encountered during the initial noder
phase, and for instantiated types created during stenciling of a
function/method.
- When we first create a fully-instantiated generic type (whether
during initial noder2 pass or while instantiating a method/function),
add it to a list so that all of its methods will also be
instantiated. This is needed so that an instantiated type can be
assigned to an interface.
- Properly substitute type names in the names of instantiated methods.
- New accessor methods for types.Type.RParam.
- To deal with generic types which are empty structs (or just don't use
their type params anywhere), we want to set HasTParam if a named type
has any type params that are not fully instantiated, even if the
type param is not used in the type.
- In subst.typ() and elsewhere, always set sym.Def for a new forwarding
type we are creating, so we always create a single unique type for
each generic type instantiation. This handles recursion within a
type, and also recursive relationships across many types or methods.
We remove the seen[] hashtable, which was serving the same purpose,
but for subst.typ() only. We now handle all kinds of recursive types.
- We don't seem to need to force types.CheckSize() on
created/substituted generic types anymore, so commented out for now.
- Add an RParams accessor to types2.Signature, and also a new
exported types2.AsSignature() function.
Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
|
|
|
// TODO(danscales): make sure this works for the field
|
|
|
|
|
// names of embedded types (which should keep the name of
|
|
|
|
|
// the type param, not the instantiated type).
|
2021-02-08 14:33:51 -08:00
|
|
|
newfields[i] = types.NewField(f.Pos, f.Sym, t2)
|
2021-02-21 10:54:38 -08:00
|
|
|
if f.Nname != nil {
|
|
|
|
|
// f.Nname may not be in subst.vars[] if this is
|
|
|
|
|
// a function name or a function instantiation type
|
|
|
|
|
// that we are translating
|
cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:
- Add support for substituting in interfaces in subster.typ()
- Fill in the info for the methods for all instantiated generic types,
so those methods will be available for later typechecking (by the old
typechecker) when assigning an instantiated generic type to an
interface. We also want those methods available so we have the list
when we want to instantiate all methods of an instantiated type. We
have both for instantiated types encountered during the initial noder
phase, and for instantiated types created during stenciling of a
function/method.
- When we first create a fully-instantiated generic type (whether
during initial noder2 pass or while instantiating a method/function),
add it to a list so that all of its methods will also be
instantiated. This is needed so that an instantiated type can be
assigned to an interface.
- Properly substitute type names in the names of instantiated methods.
- New accessor methods for types.Type.RParam.
- To deal with generic types which are empty structs (or just don't use
their type params anywhere), we want to set HasTParam if a named type
has any type params that are not fully instantiated, even if the
type param is not used in the type.
- In subst.typ() and elsewhere, always set sym.Def for a new forwarding
type we are creating, so we always create a single unique type for
each generic type instantiation. This handles recursion within a
type, and also recursive relationships across many types or methods.
We remove the seen[] hashtable, which was serving the same purpose,
but for subst.typ() only. We now handle all kinds of recursive types.
- We don't seem to need to force types.CheckSize() on
created/substituted generic types anymore, so commented out for now.
- Add an RParams accessor to types2.Signature, and also a new
exported types2.AsSignature() function.
Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
|
|
|
v := subst.vars[f.Nname.(*ir.Name)]
|
|
|
|
|
// Be careful not to put a nil var into Nname,
|
|
|
|
|
// since Nname is an interface, so it would be a
|
|
|
|
|
// non-nil interface.
|
|
|
|
|
if v != nil {
|
|
|
|
|
newfields[i].Nname = v
|
|
|
|
|
}
|
2021-02-21 10:54:38 -08:00
|
|
|
}
|
2021-02-08 14:33:51 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if newfields != nil {
|
|
|
|
|
return types.NewStruct(t.Pkg(), newfields)
|
|
|
|
|
}
|
|
|
|
|
return t
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:
- Add support for substituting in interfaces in subster.typ()
- Fill in the info for the methods for all instantiated generic types,
so those methods will be available for later typechecking (by the old
typechecker) when assigning an instantiated generic type to an
interface. We also want those methods available so we have the list
when we want to instantiate all methods of an instantiated type. We
have both for instantiated types encountered during the initial noder
phase, and for instantiated types created during stenciling of a
function/method.
- When we first create a fully-instantiated generic type (whether
during initial noder2 pass or while instantiating a method/function),
add it to a list so that all of its methods will also be
instantiated. This is needed so that an instantiated type can be
assigned to an interface.
- Properly substitute type names in the names of instantiated methods.
- New accessor methods for types.Type.RParam.
- To deal with generic types which are empty structs (or just don't use
their type params anywhere), we want to set HasTParam if a named type
has any type params that are not fully instantiated, even if the
type param is not used in the type.
- In subst.typ() and elsewhere, always set sym.Def for a new forwarding
type we are creating, so we always create a single unique type for
each generic type instantiation. This handles recursion within a
type, and also recursive relationships across many types or methods.
We remove the seen[] hashtable, which was serving the same purpose,
but for subst.typ() only. We now handle all kinds of recursive types.
- We don't seem to need to force types.CheckSize() on
created/substituted generic types anymore, so commented out for now.
- Add an RParams accessor to types2.Signature, and also a new
exported types2.AsSignature() function.
Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
|
|
|
// tinter substitutes type params in types of the methods of an interface type.
|
|
|
|
|
func (subst *subster) tinter(t *types.Type) *types.Type {
|
|
|
|
|
if t.Methods().Len() == 0 {
|
|
|
|
|
return t
|
|
|
|
|
}
|
|
|
|
|
var newfields []*types.Field
|
|
|
|
|
for i, f := range t.Methods().Slice() {
|
|
|
|
|
t2 := subst.typ(f.Type)
|
|
|
|
|
if (t2 != f.Type || f.Nname != nil) && newfields == nil {
|
|
|
|
|
newfields = make([]*types.Field, t.NumFields())
|
|
|
|
|
for j := 0; j < i; j++ {
|
|
|
|
|
newfields[j] = t.Methods().Slice()[j]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if newfields != nil {
|
|
|
|
|
newfields[i] = types.NewField(f.Pos, f.Sym, t2)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if newfields != nil {
|
|
|
|
|
return types.NewInterface(t.Pkg(), newfields)
|
|
|
|
|
}
|
|
|
|
|
return t
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// instTypeName creates a name for an instantiated type, based on the name of the
|
|
|
|
|
// generic type and the type args
|
2021-02-21 10:54:38 -08:00
|
|
|
func instTypeName(name string, targs []*types.Type) string {
|
2021-02-11 10:50:20 -08:00
|
|
|
b := bytes.NewBufferString(name)
|
|
|
|
|
b.WriteByte('[')
|
|
|
|
|
for i, targ := range targs {
|
|
|
|
|
if i > 0 {
|
|
|
|
|
b.WriteByte(',')
|
2021-02-03 15:45:26 -08:00
|
|
|
}
|
2021-02-21 10:54:38 -08:00
|
|
|
b.WriteString(targ.String())
|
2021-02-11 10:50:20 -08:00
|
|
|
}
|
|
|
|
|
b.WriteByte(']')
|
|
|
|
|
return b.String()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// typ computes the type obtained by substituting any type parameter in t with the
|
|
|
|
|
// corresponding type argument in subst. If t contains no type parameters, the
|
cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:
- Add support for substituting in interfaces in subster.typ()
- Fill in the info for the methods for all instantiated generic types,
so those methods will be available for later typechecking (by the old
typechecker) when assigning an instantiated generic type to an
interface. We also want those methods available so we have the list
when we want to instantiate all methods of an instantiated type. We
have both for instantiated types encountered during the initial noder
phase, and for instantiated types created during stenciling of a
function/method.
- When we first create a fully-instantiated generic type (whether
during initial noder2 pass or while instantiating a method/function),
add it to a list so that all of its methods will also be
instantiated. This is needed so that an instantiated type can be
assigned to an interface.
- Properly substitute type names in the names of instantiated methods.
- New accessor methods for types.Type.RParam.
- To deal with generic types which are empty structs (or just don't use
their type params anywhere), we want to set HasTParam if a named type
has any type params that are not fully instantiated, even if the
type param is not used in the type.
- In subst.typ() and elsewhere, always set sym.Def for a new forwarding
type we are creating, so we always create a single unique type for
each generic type instantiation. This handles recursion within a
type, and also recursive relationships across many types or methods.
We remove the seen[] hashtable, which was serving the same purpose,
but for subst.typ() only. We now handle all kinds of recursive types.
- We don't seem to need to force types.CheckSize() on
created/substituted generic types anymore, so commented out for now.
- Add an RParams accessor to types2.Signature, and also a new
exported types2.AsSignature() function.
Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
|
|
|
// result is t; otherwise the result is a new type. It deals with recursive types
|
|
|
|
|
// by using TFORW types and finding partially or fully created types via sym.Def.
|
2021-02-11 10:50:20 -08:00
|
|
|
func (subst *subster) typ(t *types.Type) *types.Type {
|
|
|
|
|
if !t.HasTParam() {
|
|
|
|
|
return t
|
|
|
|
|
}
|
2021-02-03 15:45:26 -08:00
|
|
|
|
cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:
- Add support for substituting in interfaces in subster.typ()
- Fill in the info for the methods for all instantiated generic types,
so those methods will be available for later typechecking (by the old
typechecker) when assigning an instantiated generic type to an
interface. We also want those methods available so we have the list
when we want to instantiate all methods of an instantiated type. We
have both for instantiated types encountered during the initial noder
phase, and for instantiated types created during stenciling of a
function/method.
- When we first create a fully-instantiated generic type (whether
during initial noder2 pass or while instantiating a method/function),
add it to a list so that all of its methods will also be
instantiated. This is needed so that an instantiated type can be
assigned to an interface.
- Properly substitute type names in the names of instantiated methods.
- New accessor methods for types.Type.RParam.
- To deal with generic types which are empty structs (or just don't use
their type params anywhere), we want to set HasTParam if a named type
has any type params that are not fully instantiated, even if the
type param is not used in the type.
- In subst.typ() and elsewhere, always set sym.Def for a new forwarding
type we are creating, so we always create a single unique type for
each generic type instantiation. This handles recursion within a
type, and also recursive relationships across many types or methods.
We remove the seen[] hashtable, which was serving the same purpose,
but for subst.typ() only. We now handle all kinds of recursive types.
- We don't seem to need to force types.CheckSize() on
created/substituted generic types anymore, so commented out for now.
- Add an RParams accessor to types2.Signature, and also a new
exported types2.AsSignature() function.
Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
|
|
|
if t.Kind() == types.TTYPEPARAM {
|
2021-02-11 10:50:20 -08:00
|
|
|
for i, tp := range subst.tparams {
|
|
|
|
|
if tp.Type == t {
|
|
|
|
|
return subst.targs[i].Type()
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-05 22:07:56 -08:00
|
|
|
// If t is a simple typeparam T, then t has the name/symbol 'T'
|
|
|
|
|
// and t.Underlying() == t.
|
|
|
|
|
//
|
|
|
|
|
// However, consider the type definition: 'type P[T any] T'. We
|
|
|
|
|
// might use this definition so we can have a variant of type T
|
|
|
|
|
// that we can add new methods to. Suppose t is a reference to
|
|
|
|
|
// P[T]. t has the name 'P[T]', but its kind is TTYPEPARAM,
|
|
|
|
|
// because P[T] is defined as T. If we look at t.Underlying(), it
|
|
|
|
|
// is different, because the name of t.Underlying() is 'T' rather
|
|
|
|
|
// than 'P[T]'. But the kind of t.Underlying() is also TTYPEPARAM.
|
|
|
|
|
// In this case, we do the needed recursive substitution in the
|
|
|
|
|
// case statement below.
|
|
|
|
|
if t.Underlying() == t {
|
|
|
|
|
// t is a simple typeparam that didn't match anything in tparam
|
|
|
|
|
return t
|
|
|
|
|
}
|
|
|
|
|
// t is a more complex typeparam (e.g. P[T], as above, whose
|
|
|
|
|
// definition is just T).
|
|
|
|
|
assert(t.Sym() != nil)
|
cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:
- Add support for substituting in interfaces in subster.typ()
- Fill in the info for the methods for all instantiated generic types,
so those methods will be available for later typechecking (by the old
typechecker) when assigning an instantiated generic type to an
interface. We also want those methods available so we have the list
when we want to instantiate all methods of an instantiated type. We
have both for instantiated types encountered during the initial noder
phase, and for instantiated types created during stenciling of a
function/method.
- When we first create a fully-instantiated generic type (whether
during initial noder2 pass or while instantiating a method/function),
add it to a list so that all of its methods will also be
instantiated. This is needed so that an instantiated type can be
assigned to an interface.
- Properly substitute type names in the names of instantiated methods.
- New accessor methods for types.Type.RParam.
- To deal with generic types which are empty structs (or just don't use
their type params anywhere), we want to set HasTParam if a named type
has any type params that are not fully instantiated, even if the
type param is not used in the type.
- In subst.typ() and elsewhere, always set sym.Def for a new forwarding
type we are creating, so we always create a single unique type for
each generic type instantiation. This handles recursion within a
type, and also recursive relationships across many types or methods.
We remove the seen[] hashtable, which was serving the same purpose,
but for subst.typ() only. We now handle all kinds of recursive types.
- We don't seem to need to force types.CheckSize() on
created/substituted generic types anymore, so commented out for now.
- Add an RParams accessor to types2.Signature, and also a new
exported types2.AsSignature() function.
Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var newsym *types.Sym
|
|
|
|
|
var neededTargs []*types.Type
|
|
|
|
|
var forw *types.Type
|
|
|
|
|
|
|
|
|
|
if t.Sym() != nil {
|
|
|
|
|
// Translate the type params for this type according to
|
|
|
|
|
// the tparam/targs mapping from subst.
|
|
|
|
|
neededTargs = make([]*types.Type, len(t.RParams()))
|
|
|
|
|
for i, rparam := range t.RParams() {
|
|
|
|
|
neededTargs[i] = subst.typ(rparam)
|
|
|
|
|
}
|
|
|
|
|
// For a named (defined) type, we have to change the name of the
|
|
|
|
|
// type as well. We do this first, so we can look up if we've
|
|
|
|
|
// already seen this type during this substitution or other
|
|
|
|
|
// definitions/substitutions.
|
|
|
|
|
genName := genericTypeName(t.Sym())
|
|
|
|
|
newsym = t.Sym().Pkg.Lookup(instTypeName(genName, neededTargs))
|
|
|
|
|
if newsym.Def != nil {
|
|
|
|
|
// We've already created this instantiated defined type.
|
|
|
|
|
return newsym.Def.Type()
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 15:25:21 -08:00
|
|
|
// In order to deal with recursive generic types, create a TFORW
|
|
|
|
|
// type initially and set the Def field of its sym, so it can be
|
|
|
|
|
// found if this type appears recursively within the type.
|
2021-04-02 14:31:15 -07:00
|
|
|
forw = newIncompleteNamedType(t.Pos(), newsym)
|
cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:
- Add support for substituting in interfaces in subster.typ()
- Fill in the info for the methods for all instantiated generic types,
so those methods will be available for later typechecking (by the old
typechecker) when assigning an instantiated generic type to an
interface. We also want those methods available so we have the list
when we want to instantiate all methods of an instantiated type. We
have both for instantiated types encountered during the initial noder
phase, and for instantiated types created during stenciling of a
function/method.
- When we first create a fully-instantiated generic type (whether
during initial noder2 pass or while instantiating a method/function),
add it to a list so that all of its methods will also be
instantiated. This is needed so that an instantiated type can be
assigned to an interface.
- Properly substitute type names in the names of instantiated methods.
- New accessor methods for types.Type.RParam.
- To deal with generic types which are empty structs (or just don't use
their type params anywhere), we want to set HasTParam if a named type
has any type params that are not fully instantiated, even if the
type param is not used in the type.
- In subst.typ() and elsewhere, always set sym.Def for a new forwarding
type we are creating, so we always create a single unique type for
each generic type instantiation. This handles recursion within a
type, and also recursive relationships across many types or methods.
We remove the seen[] hashtable, which was serving the same purpose,
but for subst.typ() only. We now handle all kinds of recursive types.
- We don't seem to need to force types.CheckSize() on
created/substituted generic types anymore, so commented out for now.
- Add an RParams accessor to types2.Signature, and also a new
exported types2.AsSignature() function.
Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
|
|
|
//println("Creating new type by sub", newsym.Name, forw.HasTParam())
|
|
|
|
|
forw.SetRParams(neededTargs)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var newt *types.Type
|
|
|
|
|
|
|
|
|
|
switch t.Kind() {
|
2021-03-05 22:07:56 -08:00
|
|
|
case types.TTYPEPARAM:
|
|
|
|
|
if t.Sym() == newsym {
|
|
|
|
|
// The substitution did not change the type.
|
|
|
|
|
return t
|
|
|
|
|
}
|
|
|
|
|
// Substitute the underlying typeparam (e.g. T in P[T], see
|
|
|
|
|
// the example describing type P[T] above).
|
|
|
|
|
newt = subst.typ(t.Underlying())
|
|
|
|
|
assert(newt != t)
|
2021-02-11 10:50:20 -08:00
|
|
|
|
2021-02-03 15:45:26 -08:00
|
|
|
case types.TARRAY:
|
|
|
|
|
elem := t.Elem()
|
|
|
|
|
newelem := subst.typ(elem)
|
2021-02-08 10:23:05 -08:00
|
|
|
if newelem != elem {
|
2021-02-11 10:50:20 -08:00
|
|
|
newt = types.NewArray(newelem, t.NumElem())
|
2021-02-03 15:45:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case types.TPTR:
|
|
|
|
|
elem := t.Elem()
|
|
|
|
|
newelem := subst.typ(elem)
|
2021-02-08 10:23:05 -08:00
|
|
|
if newelem != elem {
|
cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:
- Add support for substituting in interfaces in subster.typ()
- Fill in the info for the methods for all instantiated generic types,
so those methods will be available for later typechecking (by the old
typechecker) when assigning an instantiated generic type to an
interface. We also want those methods available so we have the list
when we want to instantiate all methods of an instantiated type. We
have both for instantiated types encountered during the initial noder
phase, and for instantiated types created during stenciling of a
function/method.
- When we first create a fully-instantiated generic type (whether
during initial noder2 pass or while instantiating a method/function),
add it to a list so that all of its methods will also be
instantiated. This is needed so that an instantiated type can be
assigned to an interface.
- Properly substitute type names in the names of instantiated methods.
- New accessor methods for types.Type.RParam.
- To deal with generic types which are empty structs (or just don't use
their type params anywhere), we want to set HasTParam if a named type
has any type params that are not fully instantiated, even if the
type param is not used in the type.
- In subst.typ() and elsewhere, always set sym.Def for a new forwarding
type we are creating, so we always create a single unique type for
each generic type instantiation. This handles recursion within a
type, and also recursive relationships across many types or methods.
We remove the seen[] hashtable, which was serving the same purpose,
but for subst.typ() only. We now handle all kinds of recursive types.
- We don't seem to need to force types.CheckSize() on
created/substituted generic types anymore, so commented out for now.
- Add an RParams accessor to types2.Signature, and also a new
exported types2.AsSignature() function.
Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
|
|
|
newt = types.NewPtr(newelem)
|
2021-02-03 15:45:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case types.TSLICE:
|
|
|
|
|
elem := t.Elem()
|
|
|
|
|
newelem := subst.typ(elem)
|
2021-02-08 10:23:05 -08:00
|
|
|
if newelem != elem {
|
2021-02-11 10:50:20 -08:00
|
|
|
newt = types.NewSlice(newelem)
|
2021-02-03 15:45:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case types.TSTRUCT:
|
2021-03-14 20:13:05 -07:00
|
|
|
newt = subst.tstruct(t, false)
|
cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:
- Add support for substituting in interfaces in subster.typ()
- Fill in the info for the methods for all instantiated generic types,
so those methods will be available for later typechecking (by the old
typechecker) when assigning an instantiated generic type to an
interface. We also want those methods available so we have the list
when we want to instantiate all methods of an instantiated type. We
have both for instantiated types encountered during the initial noder
phase, and for instantiated types created during stenciling of a
function/method.
- When we first create a fully-instantiated generic type (whether
during initial noder2 pass or while instantiating a method/function),
add it to a list so that all of its methods will also be
instantiated. This is needed so that an instantiated type can be
assigned to an interface.
- Properly substitute type names in the names of instantiated methods.
- New accessor methods for types.Type.RParam.
- To deal with generic types which are empty structs (or just don't use
their type params anywhere), we want to set HasTParam if a named type
has any type params that are not fully instantiated, even if the
type param is not used in the type.
- In subst.typ() and elsewhere, always set sym.Def for a new forwarding
type we are creating, so we always create a single unique type for
each generic type instantiation. This handles recursion within a
type, and also recursive relationships across many types or methods.
We remove the seen[] hashtable, which was serving the same purpose,
but for subst.typ() only. We now handle all kinds of recursive types.
- We don't seem to need to force types.CheckSize() on
created/substituted generic types anymore, so commented out for now.
- Add an RParams accessor to types2.Signature, and also a new
exported types2.AsSignature() function.
Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
|
|
|
if newt == t {
|
|
|
|
|
newt = nil
|
2021-02-03 15:45:26 -08:00
|
|
|
}
|
2021-02-08 14:33:51 -08:00
|
|
|
|
|
|
|
|
case types.TFUNC:
|
2021-03-14 20:13:05 -07:00
|
|
|
newrecvs := subst.tstruct(t.Recvs(), false)
|
|
|
|
|
newparams := subst.tstruct(t.Params(), false)
|
|
|
|
|
newresults := subst.tstruct(t.Results(), false)
|
2021-02-08 14:33:51 -08:00
|
|
|
if newrecvs != t.Recvs() || newparams != t.Params() || newresults != t.Results() {
|
2021-03-14 20:13:05 -07:00
|
|
|
// If any types have changed, then the all the fields of
|
|
|
|
|
// of recv, params, and results must be copied, because they have
|
|
|
|
|
// offset fields that are dependent, and so must have an
|
|
|
|
|
// independent copy for each new signature.
|
2021-02-08 14:33:51 -08:00
|
|
|
var newrecv *types.Field
|
|
|
|
|
if newrecvs.NumFields() > 0 {
|
2021-03-14 20:13:05 -07:00
|
|
|
if newrecvs == t.Recvs() {
|
|
|
|
|
newrecvs = subst.tstruct(t.Recvs(), true)
|
|
|
|
|
}
|
2021-02-08 14:33:51 -08:00
|
|
|
newrecv = newrecvs.Field(0)
|
|
|
|
|
}
|
2021-03-14 20:13:05 -07:00
|
|
|
if newparams == t.Params() {
|
|
|
|
|
newparams = subst.tstruct(t.Params(), true)
|
|
|
|
|
}
|
|
|
|
|
if newresults == t.Results() {
|
|
|
|
|
newresults = subst.tstruct(t.Results(), true)
|
|
|
|
|
}
|
cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:
- Add support for substituting in interfaces in subster.typ()
- Fill in the info for the methods for all instantiated generic types,
so those methods will be available for later typechecking (by the old
typechecker) when assigning an instantiated generic type to an
interface. We also want those methods available so we have the list
when we want to instantiate all methods of an instantiated type. We
have both for instantiated types encountered during the initial noder
phase, and for instantiated types created during stenciling of a
function/method.
- When we first create a fully-instantiated generic type (whether
during initial noder2 pass or while instantiating a method/function),
add it to a list so that all of its methods will also be
instantiated. This is needed so that an instantiated type can be
assigned to an interface.
- Properly substitute type names in the names of instantiated methods.
- New accessor methods for types.Type.RParam.
- To deal with generic types which are empty structs (or just don't use
their type params anywhere), we want to set HasTParam if a named type
has any type params that are not fully instantiated, even if the
type param is not used in the type.
- In subst.typ() and elsewhere, always set sym.Def for a new forwarding
type we are creating, so we always create a single unique type for
each generic type instantiation. This handles recursion within a
type, and also recursive relationships across many types or methods.
We remove the seen[] hashtable, which was serving the same purpose,
but for subst.typ() only. We now handle all kinds of recursive types.
- We don't seem to need to force types.CheckSize() on
created/substituted generic types anymore, so commented out for now.
- Add an RParams accessor to types2.Signature, and also a new
exported types2.AsSignature() function.
Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
|
|
|
newt = types.NewSignature(t.Pkg(), newrecv, t.TParams().FieldSlice(), newparams.FieldSlice(), newresults.FieldSlice())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case types.TINTER:
|
|
|
|
|
newt = subst.tinter(t)
|
|
|
|
|
if newt == t {
|
|
|
|
|
newt = nil
|
2021-02-03 15:45:26 -08:00
|
|
|
}
|
|
|
|
|
|
2021-03-14 20:13:05 -07:00
|
|
|
case types.TMAP:
|
|
|
|
|
newkey := subst.typ(t.Key())
|
|
|
|
|
newval := subst.typ(t.Elem())
|
|
|
|
|
if newkey != t.Key() || newval != t.Elem() {
|
|
|
|
|
newt = types.NewMap(newkey, newval)
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-13 22:41:51 -08:00
|
|
|
case types.TCHAN:
|
|
|
|
|
elem := t.Elem()
|
|
|
|
|
newelem := subst.typ(elem)
|
|
|
|
|
if newelem != elem {
|
|
|
|
|
newt = types.NewChan(newelem, t.ChanDir())
|
|
|
|
|
if !newt.HasTParam() {
|
|
|
|
|
// TODO(danscales): not sure why I have to do this
|
|
|
|
|
// only for channels.....
|
|
|
|
|
types.CheckSize(newt)
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-03 15:45:26 -08:00
|
|
|
}
|
cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:
- Add support for substituting in interfaces in subster.typ()
- Fill in the info for the methods for all instantiated generic types,
so those methods will be available for later typechecking (by the old
typechecker) when assigning an instantiated generic type to an
interface. We also want those methods available so we have the list
when we want to instantiate all methods of an instantiated type. We
have both for instantiated types encountered during the initial noder
phase, and for instantiated types created during stenciling of a
function/method.
- When we first create a fully-instantiated generic type (whether
during initial noder2 pass or while instantiating a method/function),
add it to a list so that all of its methods will also be
instantiated. This is needed so that an instantiated type can be
assigned to an interface.
- Properly substitute type names in the names of instantiated methods.
- New accessor methods for types.Type.RParam.
- To deal with generic types which are empty structs (or just don't use
their type params anywhere), we want to set HasTParam if a named type
has any type params that are not fully instantiated, even if the
type param is not used in the type.
- In subst.typ() and elsewhere, always set sym.Def for a new forwarding
type we are creating, so we always create a single unique type for
each generic type instantiation. This handles recursion within a
type, and also recursive relationships across many types or methods.
We remove the seen[] hashtable, which was serving the same purpose,
but for subst.typ() only. We now handle all kinds of recursive types.
- We don't seem to need to force types.CheckSize() on
created/substituted generic types anymore, so commented out for now.
- Add an RParams accessor to types2.Signature, and also a new
exported types2.AsSignature() function.
Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
|
|
|
if newt == nil {
|
|
|
|
|
// Even though there were typeparams in the type, there may be no
|
|
|
|
|
// change if this is a function type for a function call (which will
|
|
|
|
|
// have its own tparams/targs in the function instantiation).
|
|
|
|
|
return t
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if t.Sym() == nil {
|
|
|
|
|
// Not a named type, so there was no forwarding type and there are
|
|
|
|
|
// no methods to substitute.
|
|
|
|
|
assert(t.Methods().Len() == 0)
|
2021-02-11 10:50:20 -08:00
|
|
|
return newt
|
|
|
|
|
}
|
|
|
|
|
|
cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:
- Add support for substituting in interfaces in subster.typ()
- Fill in the info for the methods for all instantiated generic types,
so those methods will be available for later typechecking (by the old
typechecker) when assigning an instantiated generic type to an
interface. We also want those methods available so we have the list
when we want to instantiate all methods of an instantiated type. We
have both for instantiated types encountered during the initial noder
phase, and for instantiated types created during stenciling of a
function/method.
- When we first create a fully-instantiated generic type (whether
during initial noder2 pass or while instantiating a method/function),
add it to a list so that all of its methods will also be
instantiated. This is needed so that an instantiated type can be
assigned to an interface.
- Properly substitute type names in the names of instantiated methods.
- New accessor methods for types.Type.RParam.
- To deal with generic types which are empty structs (or just don't use
their type params anywhere), we want to set HasTParam if a named type
has any type params that are not fully instantiated, even if the
type param is not used in the type.
- In subst.typ() and elsewhere, always set sym.Def for a new forwarding
type we are creating, so we always create a single unique type for
each generic type instantiation. This handles recursion within a
type, and also recursive relationships across many types or methods.
We remove the seen[] hashtable, which was serving the same purpose,
but for subst.typ() only. We now handle all kinds of recursive types.
- We don't seem to need to force types.CheckSize() on
created/substituted generic types anymore, so commented out for now.
- Add an RParams accessor to types2.Signature, and also a new
exported types2.AsSignature() function.
Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
|
|
|
forw.SetUnderlying(newt)
|
|
|
|
|
newt = forw
|
|
|
|
|
|
|
|
|
|
if t.Kind() != types.TINTER && t.Methods().Len() > 0 {
|
|
|
|
|
// Fill in the method info for the new type.
|
|
|
|
|
var newfields []*types.Field
|
|
|
|
|
newfields = make([]*types.Field, t.Methods().Len())
|
|
|
|
|
for i, f := range t.Methods().Slice() {
|
|
|
|
|
t2 := subst.typ(f.Type)
|
|
|
|
|
oldsym := f.Nname.Sym()
|
|
|
|
|
newsym := makeInstName(oldsym, subst.targs, true)
|
|
|
|
|
var nname *ir.Name
|
|
|
|
|
if newsym.Def != nil {
|
|
|
|
|
nname = newsym.Def.(*ir.Name)
|
|
|
|
|
} else {
|
|
|
|
|
nname = ir.NewNameAt(f.Pos, newsym)
|
|
|
|
|
nname.SetType(t2)
|
|
|
|
|
newsym.Def = nname
|
|
|
|
|
}
|
|
|
|
|
newfields[i] = types.NewField(f.Pos, f.Sym, t2)
|
|
|
|
|
newfields[i].Nname = nname
|
|
|
|
|
}
|
|
|
|
|
newt.Methods().Set(newfields)
|
|
|
|
|
if !newt.HasTParam() {
|
|
|
|
|
// Generate all the methods for a new fully-instantiated type.
|
|
|
|
|
subst.g.instTypeList = append(subst.g.instTypeList, newt)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return newt
|
2021-02-03 15:45:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// fields sets the Nname field for the Field nodes inside a type signature, based
|
|
|
|
|
// on the corresponding in/out parameters in dcl. It depends on the in and out
|
|
|
|
|
// parameters being in order in dcl.
|
2021-02-11 10:50:20 -08:00
|
|
|
func (subst *subster) fields(class ir.Class, oldfields []*types.Field, dcl []*ir.Name) []*types.Field {
|
2021-02-03 15:45:26 -08:00
|
|
|
// Find the starting index in dcl of declarations of the class (either
|
|
|
|
|
// PPARAM or PPARAMOUT).
|
2021-03-30 13:42:14 -07:00
|
|
|
var i int
|
2021-02-03 15:45:26 -08:00
|
|
|
for i = range dcl {
|
|
|
|
|
if dcl[i].Class == class {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create newfields nodes that are copies of the oldfields nodes, but
|
|
|
|
|
// with substitution for any type params, and with Nname set to be the node in
|
|
|
|
|
// Dcl for the corresponding PPARAM or PPARAMOUT.
|
2021-03-30 13:42:14 -07:00
|
|
|
newfields := make([]*types.Field, len(oldfields))
|
2021-02-03 15:45:26 -08:00
|
|
|
for j := range oldfields {
|
|
|
|
|
newfields[j] = oldfields[j].Copy()
|
|
|
|
|
newfields[j].Type = subst.typ(oldfields[j].Type)
|
2021-03-30 13:42:14 -07:00
|
|
|
// A param field will be missing from dcl if its name is
|
|
|
|
|
// unspecified or specified as "_". So, we compare the dcl sym
|
|
|
|
|
// with the field sym. If they don't match, this dcl (if there is
|
|
|
|
|
// one left) must apply to a later field.
|
|
|
|
|
if i < len(dcl) && dcl[i].Sym() == oldfields[j].Sym {
|
|
|
|
|
newfields[j].Nname = dcl[i]
|
|
|
|
|
i++
|
|
|
|
|
}
|
2021-02-03 15:45:26 -08:00
|
|
|
}
|
|
|
|
|
return newfields
|
|
|
|
|
}
|
cmd/compile: get instantiated generic types working with interfaces
Get instantiatiated generic types working with interfaces, including
typechecking assignments to interfaces and instantiating all the methods
properly. To get it all working, this change includes:
- Add support for substituting in interfaces in subster.typ()
- Fill in the info for the methods for all instantiated generic types,
so those methods will be available for later typechecking (by the old
typechecker) when assigning an instantiated generic type to an
interface. We also want those methods available so we have the list
when we want to instantiate all methods of an instantiated type. We
have both for instantiated types encountered during the initial noder
phase, and for instantiated types created during stenciling of a
function/method.
- When we first create a fully-instantiated generic type (whether
during initial noder2 pass or while instantiating a method/function),
add it to a list so that all of its methods will also be
instantiated. This is needed so that an instantiated type can be
assigned to an interface.
- Properly substitute type names in the names of instantiated methods.
- New accessor methods for types.Type.RParam.
- To deal with generic types which are empty structs (or just don't use
their type params anywhere), we want to set HasTParam if a named type
has any type params that are not fully instantiated, even if the
type param is not used in the type.
- In subst.typ() and elsewhere, always set sym.Def for a new forwarding
type we are creating, so we always create a single unique type for
each generic type instantiation. This handles recursion within a
type, and also recursive relationships across many types or methods.
We remove the seen[] hashtable, which was serving the same purpose,
but for subst.typ() only. We now handle all kinds of recursive types.
- We don't seem to need to force types.CheckSize() on
created/substituted generic types anymore, so commented out for now.
- Add an RParams accessor to types2.Signature, and also a new
exported types2.AsSignature() function.
Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632
Reviewed-on: https://go-review.googlesource.com/c/go/+/298449
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-03 13:33:27 -08:00
|
|
|
|
|
|
|
|
// defer does a single defer of type t, if it is a pointer type.
|
|
|
|
|
func deref(t *types.Type) *types.Type {
|
|
|
|
|
if t.IsPtr() {
|
|
|
|
|
return t.Elem()
|
|
|
|
|
}
|
|
|
|
|
return t
|
|
|
|
|
}
|
2021-03-10 15:25:21 -08:00
|
|
|
|
2021-04-02 14:31:15 -07:00
|
|
|
// newIncompleteNamedType returns a TFORW type t with name specified by sym, such
|
2021-03-10 15:25:21 -08:00
|
|
|
// that t.nod and sym.Def are set correctly.
|
2021-04-02 14:31:15 -07:00
|
|
|
func newIncompleteNamedType(pos src.XPos, sym *types.Sym) *types.Type {
|
2021-03-10 15:25:21 -08:00
|
|
|
name := ir.NewDeclNameAt(pos, ir.OTYPE, sym)
|
|
|
|
|
forw := types.NewNamed(name)
|
|
|
|
|
name.SetType(forw)
|
|
|
|
|
sym.Def = name
|
|
|
|
|
return forw
|
|
|
|
|
}
|