// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package types

// validType verifies that the given type does not "expand" indefinitely
// producing a cycle in the type graph. Cycles are detected by marking
// defined types.
// (Cycles involving alias types, as in "type A = [10]A" are detected
// earlier, via the objDecl cycle detection mechanism.)
func ( *Checker) ( *Named) {
	.validType0(, nil, nil)
}

type typeInfo uint

// validType0 checks if the given type is valid. If typ is a type parameter
// its value is looked up in the provided environment. The environment is
// nil if typ is not part of (the RHS of) an instantiated type, in that case
// any type parameter encountered must be from an enclosing function and can
// be ignored. The path is the list of type names that lead to the current typ.
func ( *Checker) ( Type,  *tparamEnv,  []Object) typeInfo {
	const (
		 typeInfo = iota
		
		
		
	)

	switch t := .(type) {
	case nil:
		// We should never see a nil type but be conservative and panic
		// only in debug mode.
		if debug {
			panic("validType0(nil)")
		}

	case *Array:
		return .(.elem, , )

	case *Struct:
		for ,  := range .fields {
			if .(.typ, , ) ==  {
				return 
			}
		}

	case *Union:
		for ,  := range .terms {
			if .(.typ, , ) ==  {
				return 
			}
		}

	case *Interface:
		for ,  := range .embeddeds {
			if .(, , ) ==  {
				return 
			}
		}

	case *Named:
		// Don't report a 2nd error if we already know the type is invalid
		// (e.g., if a cycle was detected earlier, via under).
		if .underlying == Typ[Invalid] {
			.infoMap[] = 
			return 
		}

		switch .infoMap[] {
		case :
			.infoMap[] = 
			.infoMap[] = .(.orig.fromRHS, .push(), append(, .obj))
		case :
			// We have seen type t before and thus must have a cycle.
			.infoMap[] = 
			// t cannot be in an imported package otherwise that package
			// would have reported a type cycle and couldn't have been
			// imported in the first place.
			assert(.obj.pkg == .pkg)
			.underlying = Typ[Invalid] // t is in the current package (no race possibility)
			// Find the starting point of the cycle and report it.
			for ,  := range  {
				if  == .obj {
					.cycleError([:])
					return 
				}
			}
			panic("cycle start not found")
		}
		return .infoMap[]

	case *TypeParam:
		// A type parameter stands for the type (argument) it was instantiated with.
		// Check the corresponding type argument for validity if we have one.
		if  != nil {
			if  := .tmap[];  != nil {
				// Type arguments found in targ must be looked
				// up in the enclosing environment env.link.
				return .(, .link, )
			}
		}
	}

	return 
}

// A tparamEnv provides the environment for looking up the type arguments
// with which type parameters for a given instance were instantiated.
// If we don't have an instance, the corresponding tparamEnv is nil.
type tparamEnv struct {
	tmap substMap
	link *tparamEnv
}

func ( *tparamEnv) ( *Named) *tparamEnv {
	// If typ is not an instantiated type there are no typ-specific
	// type parameters to look up and we don't need an environment.
	 := .TypeArgs()
	if  == nil {
		return nil // no instance => nil environment
	}

	// Populate tmap: remember the type argument for each type parameter.
	// We cannot use makeSubstMap because the number of type parameters
	// and arguments may not match due to errors in the source (too many
	// or too few type arguments). Populate tmap "manually".
	 := .TypeParams()
	,  := .Len(), .Len()
	if  >  {
		 =  // too many targs
	}
	 := make(substMap, )
	for  := 0;  < ; ++ {
		[.At()] = .At()
	}

	return &tparamEnv{tmap: , link: }
}

// TODO(gri) Alternative implementation:
//           We may not need to build a stack of environments to
//           look up the type arguments for type parameters. The
//           same information should be available via the path:
//           We should be able to just walk the path backwards
//           and find the type arguments in the instance objects.