@@ -224,10 +224,10 @@ func NewSignature(recv *Var, params, results *Tuple, variadic bool) *Signature {
224
224
if variadic {
225
225
n := params .Len ()
226
226
if n == 0 {
227
- panic ("types .NewSignature: variadic function must have at least one parameter" )
227
+ panic ("types2 .NewSignature: variadic function must have at least one parameter" )
228
228
}
229
229
if _ , ok := params .At (n - 1 ).typ .(* Slice ); ! ok {
230
- panic ("types .NewSignature: variadic parameter must be of unnamed slice type" )
230
+ panic ("types2 .NewSignature: variadic parameter must be of unnamed slice type" )
231
231
}
232
232
}
233
233
return & Signature {recv : recv , params : params , results : results , variadic : variadic }
@@ -645,12 +645,15 @@ func (c *Chan) Dir() ChanDir { return c.dir }
645
645
// Elem returns the element type of channel c.
646
646
func (c * Chan ) Elem () Type { return c .elem }
647
647
648
+ // TODO(gri) Clean up Named struct below; specifically the fromRHS field (can we use underlying?).
649
+
648
650
// A Named represents a named (defined) type.
649
651
type Named struct {
650
652
check * Checker // for Named.under implementation
651
653
info typeInfo // for cycle detection
652
654
obj * TypeName // corresponding declared object
653
- orig Type // type (on RHS of declaration) this *Named type is derived of (for cycle reporting)
655
+ orig * Named // original, uninstantiated type
656
+ fromRHS Type // type (on RHS of declaration) this *Named type is derived from (for cycle reporting)
654
657
underlying Type // possibly a *Named during setup; never a *Named once set up completely
655
658
tparams []* TypeName // type parameters, or nil
656
659
targs []Type // type arguments (after instantiation), or nil
@@ -662,17 +665,17 @@ type Named struct {
662
665
// The underlying type must not be a *Named.
663
666
func NewNamed (obj * TypeName , underlying Type , methods []* Func ) * Named {
664
667
if _ , ok := underlying .(* Named ); ok {
665
- panic ("types.NewNamed: underlying type must not be *Named" )
666
- }
667
- typ := & Named {obj : obj , orig : underlying , underlying : underlying , methods : methods }
668
- if obj .typ == nil {
669
- obj .typ = typ
668
+ panic ("types2.NewNamed: underlying type must not be *Named" )
670
669
}
671
- return typ
670
+ return ( * Checker )( nil ). newNamed ( obj , nil , underlying , nil , methods )
672
671
}
673
672
674
- func (check * Checker ) NewNamed (obj * TypeName , underlying Type , methods []* Func ) * Named {
675
- typ := & Named {check : check , obj : obj , orig : underlying , underlying : underlying , methods : methods }
673
+ // newNamed is like NewNamed but with a *Checker receiver and additional orig argument.
674
+ func (check * Checker ) newNamed (obj * TypeName , orig * Named , underlying Type , tparams []* TypeName , methods []* Func ) * Named {
675
+ typ := & Named {check : check , obj : obj , orig : orig , fromRHS : underlying , underlying : underlying , tparams : tparams , methods : methods }
676
+ if typ .orig == nil {
677
+ typ .orig = typ
678
+ }
676
679
if obj .typ == nil {
677
680
obj .typ = typ
678
681
}
@@ -682,17 +685,24 @@ func (check *Checker) NewNamed(obj *TypeName, underlying Type, methods []*Func)
682
685
// Obj returns the type name for the named type t.
683
686
func (t * Named ) Obj () * TypeName { return t .obj }
684
687
688
+ // Orig returns the original generic type an instantiated type is derived from.
689
+ // If t is not an instantiated type, the result is t.
690
+ func (t * Named ) Orig () * Named { return t .orig }
691
+
685
692
// TODO(gri) Come up with a better representation and API to distinguish
686
693
// between parameterized instantiated and non-instantiated types.
687
694
688
695
// TParams returns the type parameters of the named type t, or nil.
689
696
// The result is non-nil for an (originally) parameterized type even if it is instantiated.
690
697
func (t * Named ) TParams () []* TypeName { return t .tparams }
691
698
699
+ // SetTParams sets the type parameters of the named type t.
700
+ func (t * Named ) SetTParams (tparams []* TypeName ) { t .tparams = tparams }
701
+
692
702
// TArgs returns the type arguments after instantiation of the named type t, or nil if not instantiated.
693
703
func (t * Named ) TArgs () []Type { return t .targs }
694
704
695
- // SetTArgs sets the type arguments of Named .
705
+ // SetTArgs sets the type arguments of the named type t .
696
706
func (t * Named ) SetTArgs (args []Type ) { t .targs = args }
697
707
698
708
// NumMethods returns the number of explicit methods whose receiver is named type t.
@@ -704,10 +714,10 @@ func (t *Named) Method(i int) *Func { return t.methods[i] }
704
714
// SetUnderlying sets the underlying type and marks t as complete.
705
715
func (t * Named ) SetUnderlying (underlying Type ) {
706
716
if underlying == nil {
707
- panic ("types .Named.SetUnderlying: underlying type must not be nil" )
717
+ panic ("types2 .Named.SetUnderlying: underlying type must not be nil" )
708
718
}
709
719
if _ , ok := underlying .(* Named ); ok {
710
- panic ("types .Named.SetUnderlying: underlying type must not be *Named" )
720
+ panic ("types2 .Named.SetUnderlying: underlying type must not be *Named" )
711
721
}
712
722
t .underlying = underlying
713
723
}
@@ -731,9 +741,9 @@ func nextId() uint64 { return uint64(atomic.AddUint32(&lastId, 1)) }
731
741
// A TypeParam represents a type parameter type.
732
742
type TypeParam struct {
733
743
check * Checker // for lazy type bound completion
734
- id uint64 // unique id
744
+ id uint64 // unique id, for debugging only
735
745
obj * TypeName // corresponding type name
736
- index int // parameter index
746
+ index int // type parameter index in source order, starting at 0
737
747
bound Type // *Named or *Interface; underlying type is always *Interface
738
748
}
739
749
0 commit comments