aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/noder
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2021-08-23 20:43:57 -0700
committerRobert Griesemer <gri@golang.org>2021-08-24 16:36:55 +0000
commitb1cdf860dd5f517a2835c6bd48d12dad29ade1da (patch)
tree571be229b1b45cee3d3c84111e99fe1e9b3d4265 /src/cmd/compile/internal/noder
parent1ff0554b5318d5a39e2b26a9c84330e6aa47b1c6 (diff)
downloadgo-b1cdf860dd5f517a2835c6bd48d12dad29ade1da.tar.gz
go-b1cdf860dd5f517a2835c6bd48d12dad29ade1da.zip
cmd/compile/internal/types2: use a TypeList type to hold type arguments
This is a port of CL 343933 from go/types with the necessary adjustments in the compiler. With this CL type parameters and type lists are now held in TParamList and TypeList data types which don't expose the internal representation. Change-Id: I6d60881b5db995dbc04ed3f4a96e8b5d41f83969 Reviewed-on: https://go-review.googlesource.com/c/go/+/344615 Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com> Reviewed-by: Dan Scales <danscales@google.com>
Diffstat (limited to 'src/cmd/compile/internal/noder')
-rw-r--r--src/cmd/compile/internal/noder/expr.go8
-rw-r--r--src/cmd/compile/internal/noder/types.go20
-rw-r--r--src/cmd/compile/internal/noder/writer.go16
3 files changed, 24 insertions, 20 deletions
diff --git a/src/cmd/compile/internal/noder/expr.go b/src/cmd/compile/internal/noder/expr.go
index 3e3c352a32..cb20d645aa 100644
--- a/src/cmd/compile/internal/noder/expr.go
+++ b/src/cmd/compile/internal/noder/expr.go
@@ -360,9 +360,9 @@ func (g *irgen) selectorExpr(pos src.XPos, typ types2.Type, expr *syntax.Selecto
// selinfo.Targs() are the types used to
// instantiate the type of receiver
targs2 := getTargs(selinfo)
- targs := make([]ir.Node, len(targs2))
- for i, targ2 := range targs2 {
- targs[i] = ir.TypeNode(g.typ(targ2))
+ targs := make([]ir.Node, targs2.Len())
+ for i := range targs {
+ targs[i] = ir.TypeNode(g.typ(targs2.At(i)))
}
// Create function instantiation with the type
@@ -386,7 +386,7 @@ func (g *irgen) selectorExpr(pos src.XPos, typ types2.Type, expr *syntax.Selecto
}
// getTargs gets the targs associated with the receiver of a selected method
-func getTargs(selinfo *types2.Selection) []types2.Type {
+func getTargs(selinfo *types2.Selection) *types2.TypeList {
r := deref2(selinfo.Recv())
n := types2.AsNamed(r)
if n == nil {
diff --git a/src/cmd/compile/internal/noder/types.go b/src/cmd/compile/internal/noder/types.go
index dd1fdcf96b..541ed68ef3 100644
--- a/src/cmd/compile/internal/noder/types.go
+++ b/src/cmd/compile/internal/noder/types.go
@@ -66,10 +66,12 @@ func (g *irgen) typ1(typ types2.Type) *types.Type {
// instTypeName2 creates a name for an instantiated type, base on the type args
// (given as types2 types).
-func instTypeName2(name string, targs []types2.Type) string {
+func instTypeName2(name string, targs *types2.TypeList) string {
b := bytes.NewBufferString(name)
b.WriteByte('[')
- for i, targ := range targs {
+ n := targs.Len()
+ for i := 0; i < n; i++ {
+ targ := targs.At(i)
if i > 0 {
b.WriteByte(',')
}
@@ -140,9 +142,10 @@ func (g *irgen) typ0(typ types2.Type) *types.Type {
// non-generic types used to instantiate this type. We'll
// use these when instantiating the methods of the
// instantiated type.
- rparams := make([]*types.Type, len(typ.TArgs()))
- for i, targ := range typ.TArgs() {
- rparams[i] = g.typ1(targ)
+ targs := typ.TArgs()
+ rparams := make([]*types.Type, targs.Len())
+ for i := range rparams {
+ rparams[i] = g.typ1(targs.At(i))
}
ntyp.SetRParams(rparams)
//fmt.Printf("Saw new type %v %v\n", instName, ntyp.HasTParam())
@@ -267,9 +270,10 @@ func (g *irgen) typ0(typ types2.Type) *types.Type {
// and for actually generating the methods for instantiated types.
func (g *irgen) fillinMethods(typ *types2.Named, ntyp *types.Type) {
if typ.NumMethods() != 0 {
- targs := make([]*types.Type, len(typ.TArgs()))
- for i, targ := range typ.TArgs() {
- targs[i] = g.typ1(targ)
+ targs2 := typ.TArgs()
+ targs := make([]*types.Type, targs2.Len())
+ for i := range targs {
+ targs[i] = g.typ1(targs2.At(i))
}
methods := make([]*types.Field, typ.NumMethods())
diff --git a/src/cmd/compile/internal/noder/writer.go b/src/cmd/compile/internal/noder/writer.go
index 02df9a43de..a6bd8b2426 100644
--- a/src/cmd/compile/internal/noder/writer.go
+++ b/src/cmd/compile/internal/noder/writer.go
@@ -299,7 +299,7 @@ func (pw *pkgWriter) typIdx(typ types2.Type, dict *writerDict) typeInfo {
// Type aliases can refer to uninstantiated generic types, so we
// might see len(TParams) != 0 && len(TArgs) == 0 here.
// TODO(mdempsky): Revisit after #46477 is resolved.
- assert(typ.TParams().Len() == len(typ.TArgs()) || len(typ.TArgs()) == 0)
+ assert(typ.TParams().Len() == typ.TArgs().Len() || typ.TArgs().Len() == 0)
// TODO(mdempsky): Why do we need to loop here?
orig := typ
@@ -441,10 +441,10 @@ func (w *writer) param(param *types2.Var) {
// @@@ Objects
-func (w *writer) obj(obj types2.Object, explicits []types2.Type) {
- explicitInfos := make([]typeInfo, len(explicits))
- for i, explicit := range explicits {
- explicitInfos[i] = w.p.typIdx(explicit, w.dict)
+func (w *writer) obj(obj types2.Object, explicits *types2.TypeList) {
+ explicitInfos := make([]typeInfo, explicits.Len())
+ for i := range explicitInfos {
+ explicitInfos[i] = w.p.typIdx(explicits.At(i), w.dict)
}
info := objInfo{idx: w.p.objIdx(obj), explicits: explicitInfos}
@@ -1212,7 +1212,7 @@ func (w *writer) expr(expr syntax.Expr) {
if obj != nil {
if isGlobal(obj) {
w.code(exprName)
- w.obj(obj, targs)
+ w.obj(obj, types2.NewTypeList(targs))
return
}
@@ -1321,7 +1321,7 @@ func (w *writer) expr(expr syntax.Expr) {
// As if w.expr(expr.Fun), but using inf.TArgs instead.
w.code(exprName)
- w.obj(obj, inf.TArgs)
+ w.obj(obj, types2.NewTypeList(inf.TArgs))
} else {
w.expr(expr.Fun)
}
@@ -1711,7 +1711,7 @@ func (w *writer) pkgDecl(decl syntax.Decl) {
// TODO(mdempsky): Revisit after #46477 is resolved.
if name.IsAlias() {
named, ok := name.Type().(*types2.Named)
- if ok && named.TParams().Len() != 0 && len(named.TArgs()) == 0 {
+ if ok && named.TParams().Len() != 0 && named.TArgs().Len() == 0 {
break
}
}