[dev.typeparams] cmd/compile: allow generic funcs to call other generic funcs for stenciling

- Handle generic function calling itself or another generic function in
   stenciling. This is easy - after it is created, just scan an
   instantiated generic function for function instantiations (that may
   needed to be stenciled), just like non-generic functions. The types
   in the function instantiation will already have been set by the
   stenciling.

 - Handle OTYPE nodes in subster.node() (allows for generic type
   conversions).

 - Eliminated some duplicated work in subster.typ().

 - Added new test case fact.go that tests a generic function calling
   itself, and simple generic type conversions.

 - Cause an error if a generic function is to be exported (which we
   don't handle yet).

 - Fixed some suggested changes in the add.go test case that I missed in
   the last review.

Change-Id: I5d61704254c27962f358d5a3d2e0c62a5099f148
Reviewed-on: https://go-review.googlesource.com/c/go/+/290469
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Dan Scales 2021-02-08 10:23:05 -08:00
parent dcb5e0392e
commit 0fbde54ea6
4 changed files with 59 additions and 14 deletions

View file

@ -20,7 +20,11 @@ import (
// creates the required stencils for simple generic functions.
func (g *irgen) stencil() {
g.target.Stencils = make(map[*types.Sym]*ir.Func)
for _, decl := range g.target.Decls {
// 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]
if decl.Op() != ir.ODCLFUNC || decl.Type().NumTParams() > 0 {
// Skip any non-function declarations and skip generic functions
continue
@ -142,6 +146,9 @@ func (subst *subster) node(n ir.Node) ir.Node {
var edit func(ir.Node) ir.Node
edit = func(x ir.Node) ir.Node {
switch x.Op() {
case ir.OTYPE:
return ir.TypeNode(subst.typ(x.Type()))
case ir.ONAME:
name := x.(*ir.Name)
if v := subst.vars[name]; v != nil {
@ -211,21 +218,21 @@ func (subst *subster) typ(t *types.Type) *types.Type {
case types.TARRAY:
elem := t.Elem()
newelem := subst.typ(elem)
if subst.typ(elem) != elem {
if newelem != elem {
return types.NewArray(newelem, t.NumElem())
}
case types.TPTR:
elem := t.Elem()
newelem := subst.typ(elem)
if subst.typ(elem) != elem {
if newelem != elem {
return types.NewPtr(newelem)
}
case types.TSLICE:
elem := t.Elem()
newelem := subst.typ(elem)
if subst.typ(elem) != elem {
if newelem != elem {
return types.NewSlice(newelem)
}