// Copyright 2013 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.

// This file implements type-checking of identifiers and type expressions.

package types

import (
	
	
	
	. 
	
)

// ident type-checks identifier e and initializes x with the value or type of e.
// If an error occurred, x.mode is set to invalid.
// For the meaning of def, see Checker.definedType, below.
// If wantType is set, the identifier e is expected to denote a type.
func ( *Checker) ( *operand,  *ast.Ident,  *TypeName,  bool) {
	.mode = invalid
	.expr = 

	,  := .lookupScope(.Name)
	switch  {
	case nil:
		if .Name == "_" {
			.error(, InvalidBlank, "cannot use _ as value or type")
		} else if isValidName(.Name) {
			.errorf(, UndeclaredName, "undefined: %s", .Name)
		}
		return
	case universeComparable:
		if !.verifyVersionf(, go1_18, "predeclared %s", .Name) {
			return // avoid follow-on errors
		}
	}
	// Because the representation of any depends on gotypesalias, we don't check
	// pointer identity here.
	if .Name() == "any" && .Parent() == Universe {
		if !.verifyVersionf(, go1_18, "predeclared %s", .Name) {
			return // avoid follow-on errors
		}
	}
	.recordUse(, )

	// If we want a type but don't have one, stop right here and avoid potential problems
	// with missing underlying types. This also gives better error messages in some cases
	// (see go.dev/issue/65344).
	,  := .(*TypeName)
	if ! &&  {
		.errorf(, NotAType, "%s is not a type", .Name())
		// avoid "declared but not used" errors
		// (don't use Checker.use - we don't want to evaluate too much)
		if ,  := .(*Var);  != nil && .pkg == .pkg /* see Checker.use1 */ {
			.used = true
		}
		return
	}

	// Type-check the object.
	// Only call Checker.objDecl if the object doesn't have a type yet
	// (in which case we must actually determine it) or the object is a
	// TypeName from the current package and we also want a type (in which case
	// we might detect a cycle which needs to be reported). Otherwise we can skip
	// the call and avoid a possible cycle error in favor of the more informative
	// "not a type/value" error that this function's caller will issue (see
	// go.dev/issue/25790).
	//
	// Note that it is important to avoid calling objDecl on objects from other
	// packages, to avoid races: see issue #69912.
	 := .Type()
	if  == nil || ( &&  && .Pkg() == .pkg) {
		.objDecl(, )
		 = .Type() // type must have been assigned by Checker.objDecl
	}
	assert( != nil)

	// The object may have been dot-imported.
	// If so, mark the respective package as used.
	// (This code is only needed for dot-imports. Without them,
	// we only have to mark variables, see *Var case below).
	if  := .dotImportMap[dotImportKey{, .Name()}];  != nil {
		.used = true
	}

	switch obj := .(type) {
	case *PkgName:
		.errorf(, InvalidPkgUse, "use of package %s not in selector", .name)
		return

	case *Const:
		.addDeclDep()
		if !isValid() {
			return
		}
		if  == universeIota {
			if .iota == nil {
				.error(, InvalidIota, "cannot use iota outside constant declaration")
				return
			}
			.val = .iota
		} else {
			.val = .val
		}
		assert(.val != nil)
		.mode = constant_

	case *TypeName:
		if !.conf._EnableAlias && .isBrokenAlias() {
			.errorf(, InvalidDeclCycle, "invalid use of type alias %s in recursive type (see go.dev/issue/50729)", .name)
			return
		}
		.mode = typexpr

	case *Var:
		// It's ok to mark non-local variables, but ignore variables
		// from other packages to avoid potential race conditions with
		// dot-imported variables.
		if .pkg == .pkg {
			.used = true
		}
		.addDeclDep()
		if !isValid() {
			return
		}
		.mode = variable

	case *Func:
		.addDeclDep()
		.mode = value

	case *Builtin:
		.id = .id
		.mode = builtin

	case *Nil:
		.mode = value

	default:
		panic("unreachable")
	}

	.typ = 
}

// typ type-checks the type expression e and returns its type, or Typ[Invalid].
// The type must not be an (uninstantiated) generic type.
func ( *Checker) ( ast.Expr) Type {
	return .definedType(, nil)
}

// varType type-checks the type expression e and returns its type, or Typ[Invalid].
// The type must not be an (uninstantiated) generic type and it must not be a
// constraint interface.
func ( *Checker) ( ast.Expr) Type {
	 := .definedType(, nil)
	.validVarType(, )
	return 
}

// validVarType reports an error if typ is a constraint interface.
// The expression e is used for error reporting, if any.
func ( *Checker) ( ast.Expr,  Type) {
	// If we have a type parameter there's nothing to do.
	if isTypeParam() {
		return
	}

	// We don't want to call under() or complete interfaces while we are in
	// the middle of type-checking parameter declarations that might belong
	// to interface methods. Delay this check to the end of type-checking.
	.later(func() {
		if ,  := under().(*Interface);  != nil {
			 := computeInterfaceTypeSet(, .Pos(), ) // TODO(gri) is this the correct position?
			if !.IsMethodSet() {
				if .comparable {
					.softErrorf(, MisplacedConstraintIface, "cannot use type %s outside a type constraint: interface is (or embeds) comparable", )
				} else {
					.softErrorf(, MisplacedConstraintIface, "cannot use type %s outside a type constraint: interface contains type constraints", )
				}
			}
		}
	}).describef(, "check var type %s", )
}

// definedType is like typ but also accepts a type name def.
// If def != nil, e is the type specification for the type named def, declared
// in a type declaration, and def.typ.underlying will be set to the type of e
// before any components of e are type-checked.
func ( *Checker) ( ast.Expr,  *TypeName) Type {
	 := .typInternal(, )
	assert(isTyped())
	if isGeneric() {
		.errorf(, WrongTypeArgCount, "cannot use generic type %s without instantiation", )
		 = Typ[Invalid]
	}
	.recordTypeAndValue(, typexpr, , nil)
	return 
}

// genericType is like typ but the type must be an (uninstantiated) generic
// type. If cause is non-nil and the type expression was a valid type but not
// generic, cause will be populated with a message describing the error.
//
// Note: If the type expression was invalid and an error was reported before,
// cause will not be populated; thus cause alone cannot be used to determine
// if an error occurred.
func ( *Checker) ( ast.Expr,  *string) Type {
	 := .typInternal(, nil)
	assert(isTyped())
	if isValid() && !isGeneric() {
		if  != nil {
			* = .sprintf("%s is not a generic type", )
		}
		 = Typ[Invalid]
	}
	// TODO(gri) what is the correct call below?
	.recordTypeAndValue(, typexpr, , nil)
	return 
}

// goTypeName returns the Go type name for typ and
// removes any occurrences of "types." from that name.
func goTypeName( Type) string {
	return strings.ReplaceAll(fmt.Sprintf("%T", ), "types.", "")
}

// typInternal drives type checking of types.
// Must only be called by definedType or genericType.
func ( *Checker) ( ast.Expr,  *TypeName) ( Type) {
	if .conf._Trace {
		.trace(.Pos(), "-- type %s", )
		.indent++
		defer func() {
			.indent--
			var  Type
			if  != nil {
				// Calling under() here may lead to endless instantiations.
				// Test case: type T[P any] *T[P]
				 = safeUnderlying()
			}
			if  ==  {
				.trace(.Pos(), "=> %s // %s", , goTypeName())
			} else {
				.trace(.Pos(), "=> %s (under = %s) // %s", , , goTypeName())
			}
		}()
	}

	switch e := .(type) {
	case *ast.BadExpr:
		// ignore - error reported before

	case *ast.Ident:
		var  operand
		.ident(&, , , true)

		switch .mode {
		case typexpr:
			 := .typ
			setDefType(, )
			return 
		case invalid:
			// ignore - error reported before
		case novalue:
			.errorf(&, NotAType, "%s used as type", &)
		default:
			.errorf(&, NotAType, "%s is not a type", &)
		}

	case *ast.SelectorExpr:
		var  operand
		.selector(&, , , true)

		switch .mode {
		case typexpr:
			 := .typ
			setDefType(, )
			return 
		case invalid:
			// ignore - error reported before
		case novalue:
			.errorf(&, NotAType, "%s used as type", &)
		default:
			.errorf(&, NotAType, "%s is not a type", &)
		}

	case *ast.IndexExpr, *ast.IndexListExpr:
		 := unpackIndexedExpr()
		.verifyVersionf(inNode(, .lbrack), go1_18, "type instantiation")
		return .instantiatedType(, )

	case *ast.ParenExpr:
		// Generic types must be instantiated before they can be used in any form.
		// Consequently, generic types cannot be parenthesized.
		return .definedType(.X, )

	case *ast.ArrayType:
		if .Len == nil {
			 := new(Slice)
			setDefType(, )
			.elem = .varType(.Elt)
			return 
		}

		 := new(Array)
		setDefType(, )
		// Provide a more specific error when encountering a [...] array
		// rather than leaving it to the handling of the ... expression.
		if ,  := .Len.(*ast.Ellipsis);  {
			.error(.Len, BadDotDotDotSyntax, "invalid use of [...] array (outside a composite literal)")
			.len = -1
		} else {
			.len = .arrayLength(.Len)
		}
		.elem = .varType(.Elt)
		if .len >= 0 {
			return 
		}
		// report error if we encountered [...]

	case *ast.Ellipsis:
		// dots are handled explicitly where they are legal
		// (array composite literals and parameter lists)
		.error(, InvalidDotDotDot, "invalid use of '...'")
		.use(.Elt)

	case *ast.StructType:
		 := new(Struct)
		setDefType(, )
		.structType(, )
		return 

	case *ast.StarExpr:
		 := new(Pointer)
		.base = Typ[Invalid] // avoid nil base in invalid recursive type declaration
		setDefType(, )
		.base = .varType(.X)
		// If typ.base is invalid, it's unlikely that *base is particularly
		// useful - even a valid dereferenciation will lead to an invalid
		// type again, and in some cases we get unexpected follow-on errors
		// (e.g., go.dev/issue/49005). Return an invalid type instead.
		if !isValid(.base) {
			return Typ[Invalid]
		}
		return 

	case *ast.FuncType:
		 := new(Signature)
		setDefType(, )
		.funcType(, nil, )
		return 

	case *ast.InterfaceType:
		 := .newInterface()
		setDefType(, )
		.interfaceType(, , )
		return 

	case *ast.MapType:
		 := new(Map)
		setDefType(, )

		.key = .varType(.Key)
		.elem = .varType(.Value)

		// spec: "The comparison operators == and != must be fully defined
		// for operands of the key type; thus the key type must not be a
		// function, map, or slice."
		//
		// Delay this check because it requires fully setup types;
		// it is safe to continue in any case (was go.dev/issue/6667).
		.later(func() {
			if !Comparable(.key) {
				var  string
				if isTypeParam(.key) {
					 = " (missing comparable constraint)"
				}
				.errorf(.Key, IncomparableMapKey, "invalid map key type %s%s", .key, )
			}
		}).describef(.Key, "check map key %s", .key)

		return 

	case *ast.ChanType:
		 := new(Chan)
		setDefType(, )

		 := SendRecv
		switch .Dir {
		case ast.SEND | ast.RECV:
			// nothing to do
		case ast.SEND:
			 = SendOnly
		case ast.RECV:
			 = RecvOnly
		default:
			.errorf(, InvalidSyntaxTree, "unknown channel direction %d", .Dir)
			// ok to continue
		}

		.dir = 
		.elem = .varType(.Value)
		return 

	default:
		.errorf(, NotAType, "%s is not a type", )
		.use()
	}

	 := Typ[Invalid]
	setDefType(, )
	return 
}

func setDefType( *TypeName,  Type) {
	if  != nil {
		switch t := .typ.(type) {
		case *Alias:
			// t.fromRHS should always be set, either to an invalid type
			// in the beginning, or to typ in certain cyclic declarations.
			if .fromRHS != Typ[Invalid] && .fromRHS !=  {
				panic(sprintf(nil, nil, true, "t.fromRHS = %s, typ = %s\n", .fromRHS, ))
			}
			.fromRHS = 
		case *Basic:
			assert( == Typ[Invalid])
		case *Named:
			.underlying = 
		default:
			panic(fmt.Sprintf("unexpected type %T", ))
		}
	}
}

func ( *Checker) ( *indexedExpr,  *TypeName) ( Type) {
	if .conf._Trace {
		.trace(.Pos(), "-- instantiating type %s with %s", .x, .indices)
		.indent++
		defer func() {
			.indent--
			// Don't format the underlying here. It will always be nil.
			.trace(.Pos(), "=> %s", )
		}()
	}

	defer func() {
		setDefType(, )
	}()

	var  string
	 := .genericType(.x, &)
	if  != "" {
		.errorf(.orig, NotAGenericType, invalidOp+"%s (%s)", .orig, )
	}
	if !isValid() {
		return  // error already reported
	}
	// typ must be a generic Alias or Named type (but not a *Signature)
	if ,  := .(*Signature);  {
		panic("unexpected generic signature")
	}
	 := .(genericType)

	// evaluate arguments
	 := .typeList(.indices)
	if  == nil {
		return Typ[Invalid]
	}

	// create instance
	// The instance is not generic anymore as it has type arguments, but it still
	// satisfies the genericType interface because it has type parameters, too.
	 := .instance(.Pos(), , , nil, .context()).(genericType)

	// For Named types, orig.tparams may not be set up, so we need to do expansion later.
	.later(func() {
		// This is an instance from the source, not from recursive substitution,
		// and so it must be resolved during type-checking so that we can report
		// errors.
		.recordInstance(.orig, , )

		 := .(interface{ () *TypeName }).().name
		 := .TypeParams().list()
		if .validateTArgLen(.Pos(), , len(), len()) {
			// check type constraints
			if ,  := .verify(.Pos(), .TypeParams().list(), , .context());  != nil {
				// best position for error reporting
				 := .Pos()
				if  < len(.indices) {
					 = .indices[].Pos()
				}
				.softErrorf(atPos(), InvalidTypeArg, "%v", )
			} else {
				.mono.recordInstance(.pkg, .Pos(), , , .indices)
			}
		}
	}).describef(, "verify instantiation %s", )

	return 
}

// arrayLength type-checks the array length expression e
// and returns the constant length >= 0, or a value < 0
// to indicate an error (and thus an unknown length).
func ( *Checker) ( ast.Expr) int64 {
	// If e is an identifier, the array declaration might be an
	// attempt at a parameterized type declaration with missing
	// constraint. Provide an error message that mentions array
	// length.
	if ,  := .(*ast.Ident);  != nil {
		 := .lookup(.Name)
		if  == nil {
			.errorf(, InvalidArrayLen, "undefined array length %s or missing type constraint", .Name)
			return -1
		}
		if ,  := .(*Const); ! {
			.errorf(, InvalidArrayLen, "invalid array length %s", .Name)
			return -1
		}
	}

	var  operand
	.expr(nil, &, )
	if .mode != constant_ {
		if .mode != invalid {
			.errorf(&, InvalidArrayLen, "array length %s must be constant", &)
		}
		return -1
	}

	if isUntyped(.typ) || isInteger(.typ) {
		if  := constant.ToInt(.val); .Kind() == constant.Int {
			if representableConst(, , Typ[Int], nil) {
				if ,  := constant.Int64Val();  &&  >= 0 {
					return 
				}
			}
		}
	}

	var  string
	if isInteger(.typ) {
		 = "invalid array length %s"
	} else {
		 = "array length %s must be integer"
	}
	.errorf(&, InvalidArrayLen, , &)
	return -1
}

// typeList provides the list of types corresponding to the incoming expression list.
// If an error occurred, the result is nil, but all list elements were type-checked.
func ( *Checker) ( []ast.Expr) []Type {
	 := make([]Type, len()) // res != nil even if len(list) == 0
	for ,  := range  {
		 := .varType()
		if !isValid() {
			 = nil
		}
		if  != nil {
			[] = 
		}
	}
	return 
}