From 5d06d165ff36010b3828161401aa425f8e8506a6 Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Thu, 5 Sep 2024 23:09:08 +0700 Subject: [PATCH] cmd/compile: remove types.Type rparams field This field is present during the initial development of generic support inside compiler, and indicating whether a type is fully instantiated is the solely purpose at this moment. Further, its name is also confused, and there have been a TODO to chose a better name for it. Instead, just using a bit to track whether a type is fully instantiated, then this rparams field can be removed to simplify the code. Change-Id: Ia29c6dd5792487c440b83b0f3b77bd60917c2019 Reviewed-on: https://go-review.googlesource.com/c/go/+/611255 Reviewed-by: Tim King LUCI-TryBot-Result: Go LUCI Reviewed-by: Keith Randall Auto-Submit: Cuong Manh Le Reviewed-by: Keith Randall --- src/cmd/compile/internal/noder/reader.go | 17 ++--- src/cmd/compile/internal/types/sizeof_test.go | 2 +- src/cmd/compile/internal/types/type.go | 63 +++++-------------- 3 files changed, 26 insertions(+), 56 deletions(-) diff --git a/src/cmd/compile/internal/noder/reader.go b/src/cmd/compile/internal/noder/reader.go index ce4cc1cc4e..55fbf860df 100644 --- a/src/cmd/compile/internal/noder/reader.go +++ b/src/cmd/compile/internal/noder/reader.go @@ -1190,13 +1190,16 @@ func (r *reader) typeExt(name *ir.Name) { typ := name.Type() if r.hasTypeParams() { - // Set "RParams" (really type arguments here, not parameters) so - // this type is treated as "fully instantiated". This ensures the - // type descriptor is written out as DUPOK and method wrappers are - // generated even for imported types. - var targs []*types.Type - targs = append(targs, r.dict.targs...) - typ.SetRParams(targs) + // Mark type as fully instantiated to ensure the type descriptor is written + // out as DUPOK and method wrappers are generated even for imported types. + typ.SetIsFullyInstantiated(true) + // HasShape should be set if any type argument is or has a shape type. + for _, targ := range r.dict.targs { + if targ.HasShape() { + typ.SetHasShape(true) + break + } + } } name.SetPragma(r.pragmaFlag()) diff --git a/src/cmd/compile/internal/types/sizeof_test.go b/src/cmd/compile/internal/types/sizeof_test.go index 27845fbd2d..3b2aeece3e 100644 --- a/src/cmd/compile/internal/types/sizeof_test.go +++ b/src/cmd/compile/internal/types/sizeof_test.go @@ -21,7 +21,7 @@ func TestSizeof(t *testing.T) { _64bit uintptr // size on 64bit platforms }{ {Sym{}, 32, 64}, - {Type{}, 64, 104}, + {Type{}, 60, 96}, {Map{}, 16, 32}, {Forward{}, 20, 32}, {Func{}, 32, 56}, diff --git a/src/cmd/compile/internal/types/type.go b/src/cmd/compile/internal/types/type.go index d8950ba894..9bb3a70b3e 100644 --- a/src/cmd/compile/internal/types/type.go +++ b/src/cmd/compile/internal/types/type.go @@ -208,16 +208,6 @@ type Type struct { // Note that for pointers, this is always PtrSize even if the element type // is NotInHeap. See size.go:PtrDataSize for details. ptrBytes int64 - - // For defined (named) generic types, a pointer to the list of type params - // (in order) of this type that need to be instantiated. For instantiated - // generic types, this is the targs used to instantiate them. These targs - // may be typeparams (for re-instantiated types such as Value[T2]) or - // concrete types (for fully instantiated types such as Value[int]). - // rparams is only set for named types that are generic or are fully - // instantiated from a generic type, and is otherwise set to nil. - // TODO(danscales): choose a better name. - rparams *[]*Type } // Registers returns the number of integer and floating-point @@ -240,19 +230,24 @@ const ( typeRecur typeIsShape // represents a set of closely related types, for generics typeHasShape // there is a shape somewhere in the type + // typeIsFullyInstantiated reports whether a type is fully instantiated generic type; i.e. + // an instantiated generic type where all type arguments are non-generic or fully instantiated generic types. + typeIsFullyInstantiated ) -func (t *Type) NotInHeap() bool { return t.flags&typeNotInHeap != 0 } -func (t *Type) Noalg() bool { return t.flags&typeNoalg != 0 } -func (t *Type) Deferwidth() bool { return t.flags&typeDeferwidth != 0 } -func (t *Type) Recur() bool { return t.flags&typeRecur != 0 } -func (t *Type) IsShape() bool { return t.flags&typeIsShape != 0 } -func (t *Type) HasShape() bool { return t.flags&typeHasShape != 0 } +func (t *Type) NotInHeap() bool { return t.flags&typeNotInHeap != 0 } +func (t *Type) Noalg() bool { return t.flags&typeNoalg != 0 } +func (t *Type) Deferwidth() bool { return t.flags&typeDeferwidth != 0 } +func (t *Type) Recur() bool { return t.flags&typeRecur != 0 } +func (t *Type) IsShape() bool { return t.flags&typeIsShape != 0 } +func (t *Type) HasShape() bool { return t.flags&typeHasShape != 0 } +func (t *Type) IsFullyInstantiated() bool { return t.flags&typeIsFullyInstantiated != 0 } -func (t *Type) SetNotInHeap(b bool) { t.flags.set(typeNotInHeap, b) } -func (t *Type) SetNoalg(b bool) { t.flags.set(typeNoalg, b) } -func (t *Type) SetDeferwidth(b bool) { t.flags.set(typeDeferwidth, b) } -func (t *Type) SetRecur(b bool) { t.flags.set(typeRecur, b) } +func (t *Type) SetNotInHeap(b bool) { t.flags.set(typeNotInHeap, b) } +func (t *Type) SetNoalg(b bool) { t.flags.set(typeNoalg, b) } +func (t *Type) SetDeferwidth(b bool) { t.flags.set(typeDeferwidth, b) } +func (t *Type) SetRecur(b bool) { t.flags.set(typeRecur, b) } +func (t *Type) SetIsFullyInstantiated(b bool) { t.flags.set(typeIsFullyInstantiated, b) } // Should always do SetHasShape(true) when doing SetIsShape(true). func (t *Type) SetIsShape(b bool) { t.flags.set(typeIsShape, b) } @@ -281,34 +276,6 @@ func (t *Type) Pos() src.XPos { return src.NoXPos } -func (t *Type) RParams() []*Type { - if t.rparams == nil { - return nil - } - return *t.rparams -} - -func (t *Type) SetRParams(rparams []*Type) { - if len(rparams) == 0 { - base.Fatalf("Setting nil or zero-length rparams") - } - t.rparams = &rparams - // HasShape should be set if any type argument is or has a shape type. - for _, rparam := range rparams { - if rparam.HasShape() { - t.SetHasShape(true) - break - } - } -} - -// IsFullyInstantiated reports whether t is a fully instantiated generic type; i.e. an -// instantiated generic type where all type arguments are non-generic or fully -// instantiated generic types. -func (t *Type) IsFullyInstantiated() bool { - return len(t.RParams()) > 0 -} - // Map contains Type fields specific to maps. type Map struct { Key *Type // Key type -- 2.48.1