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
5package types2
6
7import (
8	"cmd/compile/internal/syntax"
9	"strings"
10	"sync"
11	"sync/atomic"
12)
13
14// Type-checking Named types is subtle, because they may be recursively
15// defined, and because their full details may be spread across multiple
16// declarations (via methods). For this reason they are type-checked lazily,
17// to avoid information being accessed before it is complete.
18//
19// Conceptually, it is helpful to think of named types as having two distinct
20// sets of information:
21//  - "LHS" information, defining their identity: Obj() and TypeArgs()
22//  - "RHS" information, defining their details: TypeParams(), Underlying(),
23//    and methods.
24//
25// In this taxonomy, LHS information is available immediately, but RHS
26// information is lazy. Specifically, a named type N may be constructed in any
27// of the following ways:
28//  1. type-checked from the source
29//  2. loaded eagerly from export data
30//  3. loaded lazily from export data (when using unified IR)
31//  4. instantiated from a generic type
32//
33// In cases 1, 3, and 4, it is possible that the underlying type or methods of
34// N may not be immediately available.
35//  - During type-checking, we allocate N before type-checking its underlying
36//    type or methods, so that we may resolve recursive references.
37//  - When loading from export data, we may load its methods and underlying
38//    type lazily using a provided load function.
39//  - After instantiating, we lazily expand the underlying type and methods
40//    (note that instances may be created while still in the process of
41//    type-checking the original type declaration).
42//
43// In cases 3 and 4 this lazy construction may also occur concurrently, due to
44// concurrent use of the type checker API (after type checking or importing has
45// finished). It is critical that we keep track of state, so that Named types
46// are constructed exactly once and so that we do not access their details too
47// soon.
48//
49// We achieve this by tracking state with an atomic state variable, and
50// guarding potentially concurrent calculations with a mutex. At any point in
51// time this state variable determines which data on N may be accessed. As
52// state monotonically progresses, any data available at state M may be
53// accessed without acquiring the mutex at state N, provided N >= M.
54//
55// GLOSSARY: Here are a few terms used in this file to describe Named types:
56//  - We say that a Named type is "instantiated" if it has been constructed by
57//    instantiating a generic named type with type arguments.
58//  - We say that a Named type is "declared" if it corresponds to a type
59//    declaration in the source. Instantiated named types correspond to a type
60//    instantiation in the source, not a declaration. But their Origin type is
61//    a declared type.
62//  - We say that a Named type is "resolved" if its RHS information has been
63//    loaded or fully type-checked. For Named types constructed from export
64//    data, this may involve invoking a loader function to extract information
65//    from export data. For instantiated named types this involves reading
66//    information from their origin.
67//  - We say that a Named type is "expanded" if it is an instantiated type and
68//    type parameters in its underlying type and methods have been substituted
69//    with the type arguments from the instantiation. A type may be partially
70//    expanded if some but not all of these details have been substituted.
71//    Similarly, we refer to these individual details (underlying type or
72//    method) as being "expanded".
73//  - When all information is known for a named type, we say it is "complete".
74//
75// Some invariants to keep in mind: each declared Named type has a single
76// corresponding object, and that object's type is the (possibly generic) Named
77// type. Declared Named types are identical if and only if their pointers are
78// identical. On the other hand, multiple instantiated Named types may be
79// identical even though their pointers are not identical. One has to use
80// Identical to compare them. For instantiated named types, their obj is a
81// synthetic placeholder that records their position of the corresponding
82// instantiation in the source (if they were constructed during type checking).
83//
84// To prevent infinite expansion of named instances that are created outside of
85// type-checking, instances share a Context with other instances created during
86// their expansion. Via the pidgeonhole principle, this guarantees that in the
87// presence of a cycle of named types, expansion will eventually find an
88// existing instance in the Context and short-circuit the expansion.
89//
90// Once an instance is complete, we can nil out this shared Context to unpin
91// memory, though this Context may still be held by other incomplete instances
92// in its "lineage".
93
94// A Named represents a named (defined) type.
95type Named struct {
96	check *Checker  // non-nil during type-checking; nil otherwise
97	obj   *TypeName // corresponding declared object for declared types; see above for instantiated types
98
99	// fromRHS holds the type (on RHS of declaration) this *Named type is derived
100	// from (for cycle reporting). Only used by validType, and therefore does not
101	// require synchronization.
102	fromRHS Type
103
104	// information for instantiated types; nil otherwise
105	inst *instance
106
107	mu         sync.Mutex     // guards all fields below
108	state_     uint32         // the current state of this type; must only be accessed atomically
109	underlying Type           // possibly a *Named during setup; never a *Named once set up completely
110	tparams    *TypeParamList // type parameters, or nil
111
112	// methods declared for this type (not the method set of this type)
113	// Signatures are type-checked lazily.
114	// For non-instantiated types, this is a fully populated list of methods. For
115	// instantiated types, methods are individually expanded when they are first
116	// accessed.
117	methods []*Func
118
119	// loader may be provided to lazily load type parameters, underlying type, and methods.
120	loader func(*Named) (tparams []*TypeParam, underlying Type, methods []*Func)
121}
122
123// instance holds information that is only necessary for instantiated named
124// types.
125type instance struct {
126	orig            *Named    // original, uninstantiated type
127	targs           *TypeList // type arguments
128	expandedMethods int       // number of expanded methods; expandedMethods <= len(orig.methods)
129	ctxt            *Context  // local Context; set to nil after full expansion
130}
131
132// namedState represents the possible states that a named type may assume.
133type namedState uint32
134
135const (
136	unresolved namedState = iota // tparams, underlying type and methods might be unavailable
137	resolved                     // resolve has run; methods might be incomplete (for instances)
138	complete                     // all data is known
139)
140
141// NewNamed returns a new named type for the given type name, underlying type, and associated methods.
142// If the given type name obj doesn't have a type yet, its type is set to the returned named type.
143// The underlying type must not be a *Named.
144func NewNamed(obj *TypeName, underlying Type, methods []*Func) *Named {
145	if asNamed(underlying) != nil {
146		panic("underlying type must not be *Named")
147	}
148	return (*Checker)(nil).newNamed(obj, underlying, methods)
149}
150
151// resolve resolves the type parameters, methods, and underlying type of n.
152// This information may be loaded from a provided loader function, or computed
153// from an origin type (in the case of instances).
154//
155// After resolution, the type parameters, methods, and underlying type of n are
156// accessible; but if n is an instantiated type, its methods may still be
157// unexpanded.
158func (n *Named) resolve() *Named {
159	if n.state() >= resolved { // avoid locking below
160		return n
161	}
162
163	// TODO(rfindley): if n.check is non-nil we can avoid locking here, since
164	// type-checking is not concurrent. Evaluate if this is worth doing.
165	n.mu.Lock()
166	defer n.mu.Unlock()
167
168	if n.state() >= resolved {
169		return n
170	}
171
172	if n.inst != nil {
173		assert(n.underlying == nil) // n is an unresolved instance
174		assert(n.loader == nil)     // instances are created by instantiation, in which case n.loader is nil
175
176		orig := n.inst.orig
177		orig.resolve()
178		underlying := n.expandUnderlying()
179
180		n.tparams = orig.tparams
181		n.underlying = underlying
182		n.fromRHS = orig.fromRHS // for cycle detection
183
184		if len(orig.methods) == 0 {
185			n.setState(complete) // nothing further to do
186			n.inst.ctxt = nil
187		} else {
188			n.setState(resolved)
189		}
190		return n
191	}
192
193	// TODO(mdempsky): Since we're passing n to the loader anyway
194	// (necessary because types2 expects the receiver type for methods
195	// on defined interface types to be the Named rather than the
196	// underlying Interface), maybe it should just handle calling
197	// SetTypeParams, SetUnderlying, and AddMethod instead?  Those
198	// methods would need to support reentrant calls though. It would
199	// also make the API more future-proof towards further extensions.
200	if n.loader != nil {
201		assert(n.underlying == nil)
202		assert(n.TypeArgs().Len() == 0) // instances are created by instantiation, in which case n.loader is nil
203
204		tparams, underlying, methods := n.loader(n)
205
206		n.tparams = bindTParams(tparams)
207		n.underlying = underlying
208		n.fromRHS = underlying // for cycle detection
209		n.methods = methods
210		n.loader = nil
211	}
212
213	n.setState(complete)
214	return n
215}
216
217// state atomically accesses the current state of the receiver.
218func (n *Named) state() namedState {
219	return namedState(atomic.LoadUint32(&n.state_))
220}
221
222// setState atomically stores the given state for n.
223// Must only be called while holding n.mu.
224func (n *Named) setState(state namedState) {
225	atomic.StoreUint32(&n.state_, uint32(state))
226}
227
228// newNamed is like NewNamed but with a *Checker receiver.
229func (check *Checker) newNamed(obj *TypeName, underlying Type, methods []*Func) *Named {
230	typ := &Named{check: check, obj: obj, fromRHS: underlying, underlying: underlying, methods: methods}
231	if obj.typ == nil {
232		obj.typ = typ
233	}
234	// Ensure that typ is always sanity-checked.
235	if check != nil {
236		check.needsCleanup(typ)
237	}
238	return typ
239}
240
241// newNamedInstance creates a new named instance for the given origin and type
242// arguments, recording pos as the position of its synthetic object (for error
243// reporting).
244//
245// If set, expanding is the named type instance currently being expanded, that
246// led to the creation of this instance.
247func (check *Checker) newNamedInstance(pos syntax.Pos, orig *Named, targs []Type, expanding *Named) *Named {
248	assert(len(targs) > 0)
249
250	obj := NewTypeName(pos, orig.obj.pkg, orig.obj.name, nil)
251	inst := &instance{orig: orig, targs: newTypeList(targs)}
252
253	// Only pass the expanding context to the new instance if their packages
254	// match. Since type reference cycles are only possible within a single
255	// package, this is sufficient for the purposes of short-circuiting cycles.
256	// Avoiding passing the context in other cases prevents unnecessary coupling
257	// of types across packages.
258	if expanding != nil && expanding.Obj().pkg == obj.pkg {
259		inst.ctxt = expanding.inst.ctxt
260	}
261	typ := &Named{check: check, obj: obj, inst: inst}
262	obj.typ = typ
263	// Ensure that typ is always sanity-checked.
264	if check != nil {
265		check.needsCleanup(typ)
266	}
267	return typ
268}
269
270func (t *Named) cleanup() {
271	assert(t.inst == nil || t.inst.orig.inst == nil)
272	// Ensure that every defined type created in the course of type-checking has
273	// either non-*Named underlying type, or is unexpanded.
274	//
275	// This guarantees that we don't leak any types whose underlying type is
276	// *Named, because any unexpanded instances will lazily compute their
277	// underlying type by substituting in the underlying type of their origin.
278	// The origin must have either been imported or type-checked and expanded
279	// here, and in either case its underlying type will be fully expanded.
280	switch t.underlying.(type) {
281	case nil:
282		if t.TypeArgs().Len() == 0 {
283			panic("nil underlying")
284		}
285	case *Named, *Alias:
286		t.under() // t.under may add entries to check.cleaners
287	}
288	t.check = nil
289}
290
291// Obj returns the type name for the declaration defining the named type t. For
292// instantiated types, this is same as the type name of the origin type.
293func (t *Named) Obj() *TypeName {
294	if t.inst == nil {
295		return t.obj
296	}
297	return t.inst.orig.obj
298}
299
300// Origin returns the generic type from which the named type t is
301// instantiated. If t is not an instantiated type, the result is t.
302func (t *Named) Origin() *Named {
303	if t.inst == nil {
304		return t
305	}
306	return t.inst.orig
307}
308
309// TypeParams returns the type parameters of the named type t, or nil.
310// The result is non-nil for an (originally) generic type even if it is instantiated.
311func (t *Named) TypeParams() *TypeParamList { return t.resolve().tparams }
312
313// SetTypeParams sets the type parameters of the named type t.
314// t must not have type arguments.
315func (t *Named) SetTypeParams(tparams []*TypeParam) {
316	assert(t.inst == nil)
317	t.resolve().tparams = bindTParams(tparams)
318}
319
320// TypeArgs returns the type arguments used to instantiate the named type t.
321func (t *Named) TypeArgs() *TypeList {
322	if t.inst == nil {
323		return nil
324	}
325	return t.inst.targs
326}
327
328// NumMethods returns the number of explicit methods defined for t.
329func (t *Named) NumMethods() int {
330	return len(t.Origin().resolve().methods)
331}
332
333// Method returns the i'th method of named type t for 0 <= i < t.NumMethods().
334//
335// For an ordinary or instantiated type t, the receiver base type of this
336// method is the named type t. For an uninstantiated generic type t, each
337// method receiver is instantiated with its receiver type parameters.
338//
339// Methods are numbered deterministically: given the same list of source files
340// presented to the type checker, or the same sequence of NewMethod and AddMethod
341// calls, the mapping from method index to corresponding method remains the same.
342// But the specific ordering is not specified and must not be relied on as it may
343// change in the future.
344func (t *Named) Method(i int) *Func {
345	t.resolve()
346
347	if t.state() >= complete {
348		return t.methods[i]
349	}
350
351	assert(t.inst != nil) // only instances should have incomplete methods
352	orig := t.inst.orig
353
354	t.mu.Lock()
355	defer t.mu.Unlock()
356
357	if len(t.methods) != len(orig.methods) {
358		assert(len(t.methods) == 0)
359		t.methods = make([]*Func, len(orig.methods))
360	}
361
362	if t.methods[i] == nil {
363		assert(t.inst.ctxt != nil) // we should still have a context remaining from the resolution phase
364		t.methods[i] = t.expandMethod(i)
365		t.inst.expandedMethods++
366
367		// Check if we've created all methods at this point. If we have, mark the
368		// type as fully expanded.
369		if t.inst.expandedMethods == len(orig.methods) {
370			t.setState(complete)
371			t.inst.ctxt = nil // no need for a context anymore
372		}
373	}
374
375	return t.methods[i]
376}
377
378// expandMethod substitutes type arguments in the i'th method for an
379// instantiated receiver.
380func (t *Named) expandMethod(i int) *Func {
381	// t.orig.methods is not lazy. origm is the method instantiated with its
382	// receiver type parameters (the "origin" method).
383	origm := t.inst.orig.Method(i)
384	assert(origm != nil)
385
386	check := t.check
387	// Ensure that the original method is type-checked.
388	if check != nil {
389		check.objDecl(origm, nil)
390	}
391
392	origSig := origm.typ.(*Signature)
393	rbase, _ := deref(origSig.Recv().Type())
394
395	// If rbase is t, then origm is already the instantiated method we're looking
396	// for. In this case, we return origm to preserve the invariant that
397	// traversing Method->Receiver Type->Method should get back to the same
398	// method.
399	//
400	// This occurs if t is instantiated with the receiver type parameters, as in
401	// the use of m in func (r T[_]) m() { r.m() }.
402	if rbase == t {
403		return origm
404	}
405
406	sig := origSig
407	// We can only substitute if we have a correspondence between type arguments
408	// and type parameters. This check is necessary in the presence of invalid
409	// code.
410	if origSig.RecvTypeParams().Len() == t.inst.targs.Len() {
411		smap := makeSubstMap(origSig.RecvTypeParams().list(), t.inst.targs.list())
412		var ctxt *Context
413		if check != nil {
414			ctxt = check.context()
415		}
416		sig = check.subst(origm.pos, origSig, smap, t, ctxt).(*Signature)
417	}
418
419	if sig == origSig {
420		// No substitution occurred, but we still need to create a new signature to
421		// hold the instantiated receiver.
422		copy := *origSig
423		sig = &copy
424	}
425
426	var rtyp Type
427	if origm.hasPtrRecv() {
428		rtyp = NewPointer(t)
429	} else {
430		rtyp = t
431	}
432
433	sig.recv = substVar(origSig.recv, rtyp)
434	return substFunc(origm, sig)
435}
436
437// SetUnderlying sets the underlying type and marks t as complete.
438// t must not have type arguments.
439func (t *Named) SetUnderlying(underlying Type) {
440	assert(t.inst == nil)
441	if underlying == nil {
442		panic("underlying type must not be nil")
443	}
444	if asNamed(underlying) != nil {
445		panic("underlying type must not be *Named")
446	}
447	t.resolve().underlying = underlying
448	if t.fromRHS == nil {
449		t.fromRHS = underlying // for cycle detection
450	}
451}
452
453// AddMethod adds method m unless it is already in the method list.
454// The method must be in the same package as t, and t must not have
455// type arguments.
456func (t *Named) AddMethod(m *Func) {
457	assert(samePkg(t.obj.pkg, m.pkg))
458	assert(t.inst == nil)
459	t.resolve()
460	if t.methodIndex(m.name, false) < 0 {
461		t.methods = append(t.methods, m)
462	}
463}
464
465// methodIndex returns the index of the method with the given name.
466// If foldCase is set, capitalization in the name is ignored.
467// The result is negative if no such method exists.
468func (t *Named) methodIndex(name string, foldCase bool) int {
469	if name == "_" {
470		return -1
471	}
472	if foldCase {
473		for i, m := range t.methods {
474			if strings.EqualFold(m.name, name) {
475				return i
476			}
477		}
478	} else {
479		for i, m := range t.methods {
480			if m.name == name {
481				return i
482			}
483		}
484	}
485	return -1
486}
487
488// Underlying returns the [underlying type] of the named type t, resolving all
489// forwarding declarations. Underlying types are never Named, TypeParam, or
490// Alias types.
491//
492// [underlying type]: https://go.dev/ref/spec#Underlying_types.
493func (t *Named) Underlying() Type {
494	// TODO(gri) Investigate if Unalias can be moved to where underlying is set.
495	return Unalias(t.resolve().underlying)
496}
497
498func (t *Named) String() string { return TypeString(t, nil) }
499
500// ----------------------------------------------------------------------------
501// Implementation
502//
503// TODO(rfindley): reorganize the loading and expansion methods under this
504// heading.
505
506// under returns the expanded underlying type of n0; possibly by following
507// forward chains of named types. If an underlying type is found, resolve
508// the chain by setting the underlying type for each defined type in the
509// chain before returning it. If no underlying type is found or a cycle
510// is detected, the result is Typ[Invalid]. If a cycle is detected and
511// n0.check != nil, the cycle is reported.
512//
513// This is necessary because the underlying type of named may be itself a
514// named type that is incomplete:
515//
516//	type (
517//		A B
518//		B *C
519//		C A
520//	)
521//
522// The type of C is the (named) type of A which is incomplete,
523// and which has as its underlying type the named type B.
524func (n0 *Named) under() Type {
525	u := n0.Underlying()
526
527	// If the underlying type of a defined type is not a defined
528	// (incl. instance) type, then that is the desired underlying
529	// type.
530	var n1 *Named
531	switch u1 := u.(type) {
532	case nil:
533		// After expansion via Underlying(), we should never encounter a nil
534		// underlying.
535		panic("nil underlying")
536	default:
537		// common case
538		return u
539	case *Named:
540		// handled below
541		n1 = u1
542	}
543
544	if n0.check == nil {
545		panic("Named.check == nil but type is incomplete")
546	}
547
548	// Invariant: after this point n0 as well as any named types in its
549	// underlying chain should be set up when this function exits.
550	check := n0.check
551	n := n0
552
553	seen := make(map[*Named]int) // types that need their underlying type resolved
554	var path []Object            // objects encountered, for cycle reporting
555
556loop:
557	for {
558		seen[n] = len(seen)
559		path = append(path, n.obj)
560		n = n1
561		if i, ok := seen[n]; ok {
562			// cycle
563			check.cycleError(path[i:], firstInSrc(path[i:]))
564			u = Typ[Invalid]
565			break
566		}
567		u = n.Underlying()
568		switch u1 := u.(type) {
569		case nil:
570			u = Typ[Invalid]
571			break loop
572		default:
573			break loop
574		case *Named:
575			// Continue collecting *Named types in the chain.
576			n1 = u1
577		}
578	}
579
580	for n := range seen {
581		// We should never have to update the underlying type of an imported type;
582		// those underlying types should have been resolved during the import.
583		// Also, doing so would lead to a race condition (was go.dev/issue/31749).
584		// Do this check always, not just in debug mode (it's cheap).
585		if n.obj.pkg != check.pkg {
586			panic("imported type with unresolved underlying type")
587		}
588		n.underlying = u
589	}
590
591	return u
592}
593
594func (n *Named) lookupMethod(pkg *Package, name string, foldCase bool) (int, *Func) {
595	n.resolve()
596	if samePkg(n.obj.pkg, pkg) || isExported(name) || foldCase {
597		// If n is an instance, we may not have yet instantiated all of its methods.
598		// Look up the method index in orig, and only instantiate method at the
599		// matching index (if any).
600		if i := n.Origin().methodIndex(name, foldCase); i >= 0 {
601			// For instances, m.Method(i) will be different from the orig method.
602			return i, n.Method(i)
603		}
604	}
605	return -1, nil
606}
607
608// context returns the type-checker context.
609func (check *Checker) context() *Context {
610	if check.ctxt == nil {
611		check.ctxt = NewContext()
612	}
613	return check.ctxt
614}
615
616// expandUnderlying substitutes type arguments in the underlying type n.orig,
617// returning the result. Returns Typ[Invalid] if there was an error.
618func (n *Named) expandUnderlying() Type {
619	check := n.check
620	if check != nil && check.conf.Trace {
621		check.trace(n.obj.pos, "-- Named.expandUnderlying %s", n)
622		check.indent++
623		defer func() {
624			check.indent--
625			check.trace(n.obj.pos, "=> %s (tparams = %s, under = %s)", n, n.tparams.list(), n.underlying)
626		}()
627	}
628
629	assert(n.inst.orig.underlying != nil)
630	if n.inst.ctxt == nil {
631		n.inst.ctxt = NewContext()
632	}
633
634	orig := n.inst.orig
635	targs := n.inst.targs
636
637	if asNamed(orig.underlying) != nil {
638		// We should only get a Named underlying type here during type checking
639		// (for example, in recursive type declarations).
640		assert(check != nil)
641	}
642
643	if orig.tparams.Len() != targs.Len() {
644		// Mismatching arg and tparam length may be checked elsewhere.
645		return Typ[Invalid]
646	}
647
648	// Ensure that an instance is recorded before substituting, so that we
649	// resolve n for any recursive references.
650	h := n.inst.ctxt.instanceHash(orig, targs.list())
651	n2 := n.inst.ctxt.update(h, orig, n.TypeArgs().list(), n)
652	assert(n == n2)
653
654	smap := makeSubstMap(orig.tparams.list(), targs.list())
655	var ctxt *Context
656	if check != nil {
657		ctxt = check.context()
658	}
659	underlying := n.check.subst(n.obj.pos, orig.underlying, smap, n, ctxt)
660	// If the underlying type of n is an interface, we need to set the receiver of
661	// its methods accurately -- we set the receiver of interface methods on
662	// the RHS of a type declaration to the defined type.
663	if iface, _ := underlying.(*Interface); iface != nil {
664		if methods, copied := replaceRecvType(iface.methods, orig, n); copied {
665			// If the underlying type doesn't actually use type parameters, it's
666			// possible that it wasn't substituted. In this case we need to create
667			// a new *Interface before modifying receivers.
668			if iface == orig.underlying {
669				old := iface
670				iface = check.newInterface()
671				iface.embeddeds = old.embeddeds
672				assert(old.complete) // otherwise we are copying incomplete data
673				iface.complete = old.complete
674				iface.implicit = old.implicit // should be false but be conservative
675				underlying = iface
676			}
677			iface.methods = methods
678			iface.tset = nil // recompute type set with new methods
679
680			// If check != nil, check.newInterface will have saved the interface for later completion.
681			if check == nil { // golang/go#61561: all newly created interfaces must be fully evaluated
682				iface.typeSet()
683			}
684		}
685	}
686
687	return underlying
688}
689
690// safeUnderlying returns the underlying type of typ without expanding
691// instances, to avoid infinite recursion.
692//
693// TODO(rfindley): eliminate this function or give it a better name.
694func safeUnderlying(typ Type) Type {
695	if t := asNamed(typ); t != nil {
696		return t.underlying
697	}
698	return typ.Underlying()
699}
700