package gotypes

import (
	
	

	
)

// WalkObjects visits the objects named in t’s source representation: type names,
// struct fields, and explicit interface methods. It follows type arguments but
// not the definitions of named types or aliases. Type parameter constraints are
// walked separately. Returning false skips an object’s children; other branches
// are still visited.
func ( types.Type,  func(types.Object) bool) {
	walkObjects(, map[types.Type]bool{}, )
}

// walkObjects distinguishes type nodes even when types.Identical considers them
// equal, since their source representations can name different objects.
func ( types.Type,  map[types.Type]bool,  func(types.Object) bool) {
	if [] {
		return
	}
	[] = true
	switch u := .(type) {
	case *types.Named:
		if !(.Obj()) {
			return
		}
		for  := range .TypeArgs().Types() {
			(, , )
		}
	case *types.Alias:
		if !(.Obj()) {
			return
		}
		for  := range .TypeArgs().Types() {
			(, , )
		}
	case *types.Pointer:
		(.Elem(), , )
	case *types.Slice:
		(.Elem(), , )
	case *types.Array:
		(.Elem(), , )
	case *types.Chan:
		(.Elem(), , )
	case *types.Map:
		(.Key(), , )
		(.Elem(), , )
	case *types.Signature:
		walkTuple(.Params(), , )
		walkTuple(.Results(), , )
	case *types.Struct:
		for  := range .Fields() {
			if !() {
				continue
			}
			(.Type(), , )
		}
	case *types.Interface:
		for  := range .EmbeddedTypes() {
			(, , )
		}
		for  := range .ExplicitMethods() {
			if !() {
				continue
			}
			(.Type(), , )
		}
	case *types.Union:
		for  := range .Terms() {
			(.Type(), , )
		}
	case *types.Basic, *types.TypeParam:
		// Basic types name no objects; type parameter constraints are handled separately.
	default:
		panic(fmt.Sprintf("plumb: WalkObjects: unhandled type kind %T", ))
	}
}

func ( *types.Tuple,  map[types.Type]bool,  func(types.Object) bool) {
	for  := range .Variables() {
		walkObjects(.Type(), , )
	}
}

// ContainsInvalid reports whether t mentions the invalid type anywhere, meaning
// it failed to type-check and plumb must not render it.
func ( types.Type) bool {
	return typeContains(, hashed.NewSet(types.Hasher{}), func( types.Type) bool {
		,  := .(*types.Basic)
		return  && .Kind() == types.Invalid
	})
}

// ContainsTypeParam reports whether t mentions any type parameter anywhere:
// the test for a pinning that cannot survive a resolution restart, since lifted
// parameters are per-run objects.
func ( types.Type) bool {
	return typeContains(, hashed.NewSet(types.Hasher{}), func( types.Type) bool {
		,  := .(*types.TypeParam)
		return 
	})
}

// typeContains walks t (guarding against cycles) and reports whether pred holds
// for any node. The switch is exhaustive over every types.Type kind: leaf kinds
// terminate explicitly and any unhandled kind panics, never silently reports
// false. A silent false here is precisely how an unrendered invalid type would
// slip past ContainsInvalid and reach the emitter.
func ( types.Type,  *hashed.Set[types.Type],  func(types.Type) bool) bool {
	 = types.Unalias()
	if !.Insert() {
		return false
	}
	if () {
		return true
	}
	switch u := .(type) {
	case *types.Basic, *types.TypeParam:
		// Leaf types: nothing to descend into. A type parameter renders as its
		// name; its constraint is validated separately (see checks.go).
		return false
	case *types.Pointer:
		return (.Elem(), , )
	case *types.Slice:
		return (.Elem(), , )
	case *types.Array:
		return (.Elem(), , )
	case *types.Chan:
		return (.Elem(), , )
	case *types.Map:
		return (.Key(), , ) || (.Elem(), , )
	case *types.Named:
		if  := .TypeArgs();  != nil {
			for  := range .Types() {
				if (, , ) {
					return true
				}
			}
		}
		return false
	case *types.Signature:
		return tupleContains(.Params(), , ) || tupleContains(.Results(), , )
	case *types.Struct:
		for  := range .Fields() {
			if (.Type(), , ) {
				return true
			}
		}
		return false
	case *types.Interface:
		for  := range .EmbeddedTypes() {
			if (, , ) {
				return true
			}
		}
		for  := range .ExplicitMethods() {
			if (.Type(), , ) {
				return true
			}
		}
		return false
	case *types.Union:
		for  := range .Terms() {
			if (.Type(), , ) {
				return true
			}
		}
		return false
	default:
		panic(fmt.Sprintf("plumb: typeContains: unhandled type kind %T", ))
	}
}

func ( *types.Tuple,  *hashed.Set[types.Type],  func(types.Type) bool) bool {
	for  := range .Variables() {
		if typeContains(.Type(), , ) {
			return true
		}
	}
	return false
}

// IsUniverseName reports whether name is a predeclared identifier.
func ( string) bool {
	return types.Universe.Lookup() != nil
}