github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/cmd/compile/internal/types2/named.go (about) 1 // Copyright 2011 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package types2 6 7 import ( 8 "github.com/shogo82148/std/sync" 9 ) 10 11 // A Named represents a named (defined) type. 12 type Named struct { 13 check *Checker 14 obj *TypeName 15 16 // fromRHS holds the type (on RHS of declaration) this *Named type is derived 17 // from (for cycle reporting). Only used by validType, and therefore does not 18 // require synchronization. 19 fromRHS Type 20 21 // information for instantiated types; nil otherwise 22 inst *instance 23 24 mu sync.Mutex 25 state_ uint32 26 underlying Type 27 tparams *TypeParamList 28 29 // methods declared for this type (not the method set of this type) 30 // Signatures are type-checked lazily. 31 // For non-instantiated types, this is a fully populated list of methods. For 32 // instantiated types, methods are individually expanded when they are first 33 // accessed. 34 methods []*Func 35 36 // loader may be provided to lazily load type parameters, underlying type, and methods. 37 loader func(*Named) (tparams []*TypeParam, underlying Type, methods []*Func) 38 } 39 40 // NewNamed returns a new named type for the given type name, underlying type, and associated methods. 41 // If the given type name obj doesn't have a type yet, its type is set to the returned named type. 42 // The underlying type must not be a *Named. 43 func NewNamed(obj *TypeName, underlying Type, methods []*Func) *Named 44 45 // Obj returns the type name for the declaration defining the named type t. For 46 // instantiated types, this is same as the type name of the origin type. 47 func (t *Named) Obj() *TypeName 48 49 // Origin returns the generic type from which the named type t is 50 // instantiated. If t is not an instantiated type, the result is t. 51 func (t *Named) Origin() *Named 52 53 // TypeParams returns the type parameters of the named type t, or nil. 54 // The result is non-nil for an (originally) generic type even if it is instantiated. 55 func (t *Named) TypeParams() *TypeParamList 56 57 // SetTypeParams sets the type parameters of the named type t. 58 // t must not have type arguments. 59 func (t *Named) SetTypeParams(tparams []*TypeParam) 60 61 // TypeArgs returns the type arguments used to instantiate the named type t. 62 func (t *Named) TypeArgs() *TypeList 63 64 // NumMethods returns the number of explicit methods defined for t. 65 func (t *Named) NumMethods() int 66 67 // Method returns the i'th method of named type t for 0 <= i < t.NumMethods(). 68 // 69 // For an ordinary or instantiated type t, the receiver base type of this 70 // method is the named type t. For an uninstantiated generic type t, each 71 // method receiver is instantiated with its receiver type parameters. 72 // 73 // Methods are numbered deterministically: given the same list of source files 74 // presented to the type checker, or the same sequence of NewMethod and AddMethod 75 // calls, the mapping from method index to corresponding method remains the same. 76 // But the specific ordering is not specified and must not be relied on as it may 77 // change in the future. 78 func (t *Named) Method(i int) *Func 79 80 // SetUnderlying sets the underlying type and marks t as complete. 81 // t must not have type arguments. 82 func (t *Named) SetUnderlying(underlying Type) 83 84 // AddMethod adds method m unless it is already in the method list. 85 // The method must be in the same package as t, and t must not have 86 // type arguments. 87 func (t *Named) AddMethod(m *Func) 88 89 // TODO(gri) Investigate if Unalias can be moved to where underlying is set. 90 func (t *Named) Underlying() Type 91 func (t *Named) String() string