// Copyright 2012 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 typechecking of conversions.

package types

import (
	
	
)

// Conversion type-checks the conversion T(x).
// The result is in x.
func ( *Checker) ( *operand,  Type) {
	 := .mode == constant_

	var  bool
	switch {
	case  && isConstType():
		// constant conversion
		switch  := .Underlying().(*Basic); {
		case representableConst(.val, , , &.val):
			 = true
		case isInteger(.typ) && isString():
			 := unicode.ReplacementChar
			if ,  := constant.Uint64Val(.val);  &&  <= unicode.MaxRune {
				 = rune()
			}
			.val = constant.MakeString(string())
			 = true
		}
	case .convertibleTo(, ):
		// non-constant conversion
		.mode = value
		 = true
	}

	if ! {
		.errorf(, _InvalidConversion, "cannot convert %s to %s", , )
		.mode = invalid
		return
	}

	// The conversion argument types are final. For untyped values the
	// conversion provides the type, per the spec: "A constant may be
	// given a type explicitly by a constant declaration or conversion,...".
	if isUntyped(.typ) {
		 := 
		// - For conversions to interfaces, use the argument's default type.
		// - For conversions of untyped constants to non-constant types, also
		//   use the default type (e.g., []byte("foo") should report string
		//   not []byte as type for the constant "foo").
		// - Keep untyped nil for untyped nil arguments.
		// - For integer to string conversions, keep the argument type.
		//   (See also the TODO below.)
		if IsInterface() ||  && !isConstType() {
			 = Default(.typ)
		} else if isInteger(.typ) && isString() {
			 = .typ
		}
		.updateExprType(.expr, , true)
	}

	.typ = 
}

// TODO(gri) convertibleTo checks if T(x) is valid. It assumes that the type
// of x is fully known, but that's not the case for say string(1<<s + 1.0):
// Here, the type of 1<<s + 1.0 will be UntypedFloat which will lead to the
// (correct!) refusal of the conversion. But the reported error is essentially
// "cannot convert untyped float value to string", yet the correct error (per
// the spec) is that we cannot shift a floating-point value: 1 in 1<<s should
// be converted to UntypedFloat because of the addition of 1.0. Fixing this
// is tricky because we'd have to run updateExprType on the argument first.
// (Issue #21982.)

// convertibleTo reports whether T(x) is valid.
// The check parameter may be nil if convertibleTo is invoked through an
// exported API call, i.e., when all methods have been type-checked.
func ( *operand) ( *Checker,  Type) bool {
	// "x is assignable to T"
	if ,  := .assignableTo(, , nil);  {
		return true
	}

	// "x's type and T have identical underlying types if tags are ignored"
	 := .typ
	 := .Underlying()
	 := .Underlying()
	if .identicalIgnoreTags(, ) {
		return true
	}

	// "x's type and T are unnamed pointer types and their pointer base types
	// have identical underlying types if tags are ignored"
	if ,  := .(*Pointer);  {
		if ,  := .(*Pointer);  {
			if .identicalIgnoreTags(.base.Underlying(), .base.Underlying()) {
				return true
			}
		}
	}

	// "x's type and T are both integer or floating point types"
	if (isInteger() || isFloat()) && (isInteger() || isFloat()) {
		return true
	}

	// "x's type and T are both complex types"
	if isComplex() && isComplex() {
		return true
	}

	// "x is an integer or a slice of bytes or runes and T is a string type"
	if (isInteger() || isBytesOrRunes()) && isString() {
		return true
	}

	// "x is a string and T is a slice of bytes or runes"
	if isString() && isBytesOrRunes() {
		return true
	}

	// package unsafe:
	// "any pointer or value of underlying type uintptr can be converted into a unsafe.Pointer"
	if (isPointer() || isUintptr()) && isUnsafePointer() {
		return true
	}
	// "and vice versa"
	if isUnsafePointer() && (isPointer() || isUintptr()) {
		return true
	}

	return false
}

func isUintptr( Type) bool {
	,  := .Underlying().(*Basic)
	return  && .kind == Uintptr
}

func isUnsafePointer( Type) bool {
	// TODO(gri): Is this (typ.Underlying() instead of just typ) correct?
	//            The spec does not say so, but gc claims it is. See also
	//            issue 6326.
	,  := .Underlying().(*Basic)
	return  && .kind == UnsafePointer
}

func isPointer( Type) bool {
	,  := .Underlying().(*Pointer)
	return 
}

func isBytesOrRunes( Type) bool {
	if ,  := .(*Slice);  {
		,  := .elem.Underlying().(*Basic)
		return  && (.kind == Byte || .kind == Rune)
	}
	return false
}