// 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 various error reporters.

package types

import (
	
	
	
	
	
	
)

func assert( bool) {
	if ! {
		panic("assertion failed")
	}
}

func unreachable() {
	panic("unreachable")
}

func ( *Checker) ( *Package) string {
	// Qualify the package unless it's the package being type-checked.
	if  != .pkg {
		// If the same package name was used by multiple packages, display the full path.
		if .pkgCnt[.name] > 1 {
			return strconv.Quote(.path)
		}
		return .name
	}
	return ""
}

func ( *Checker) ( string,  ...interface{}) string {
	for ,  := range  {
		switch a := .(type) {
		case nil:
			 = "<nil>"
		case operand:
			panic("internal error: should always pass *operand")
		case *operand:
			 = operandString(, .qualifier)
		case token.Pos:
			 = .fset.Position().String()
		case ast.Expr:
			 = ExprString()
		case Object:
			 = ObjectString(, .qualifier)
		case Type:
			 = TypeString(, .qualifier)
		}
		[] = 
	}
	return fmt.Sprintf(, ...)
}

func ( *Checker) ( token.Pos,  string,  ...interface{}) {
	fmt.Printf("%s:\t%s%s\n",
		.fset.Position(),
		strings.Repeat(".  ", .indent),
		.sprintf(, ...),
	)
}

// dump is only needed for debugging
func ( *Checker) ( string,  ...interface{}) {
	fmt.Println(.sprintf(, ...))
}

func ( *Checker) ( error) {
	if  == nil {
		return
	}
	var  Error
	 := errors.As(, &)
	// Cheap trick: Don't report errors with messages containing
	// "invalid operand" or "invalid type" as those tend to be
	// follow-on errors which don't add useful information. Only
	// exclude them if these strings are not at the beginning,
	// and only if we have at least one error already reported.
	 :=  && (strings.Index(.Msg, "invalid operand") > 0 || strings.Index(.Msg, "invalid type") > 0)
	if .firstErr != nil &&  {
		return
	}

	if .errpos != nil &&  {
		// If we have an internal error and the errpos override is set, use it to
		// augment our error positioning.
		// TODO(rFindley) we may also want to augment the error message and refer
		// to the position (pos) in the original expression.
		 := spanOf(.errpos)
		.Pos = .pos
		.go116start = .start
		.go116end = .end
		 = 
	}

	if .firstErr == nil {
		.firstErr = 
	}

	if trace {
		 := .Pos
		 := .Msg
		if ! {
			 = .Error()
			 = token.NoPos
		}
		.trace(, "ERROR: %s", )
	}

	 := .conf.Error
	if  == nil {
		panic(bailout{}) // report only first error
	}
	()
}

func ( *Checker) ( positioner,  errorCode,  bool,  string) error {
	 := spanOf()
	return Error{
		Fset:       .fset,
		Pos:        .pos,
		Msg:        ,
		Soft:       ,
		go116code:  ,
		go116start: .start,
		go116end:   .end,
	}
}

// newErrorf creates a new Error, but does not handle it.
func ( *Checker) ( positioner,  errorCode,  bool,  string,  ...interface{}) error {
	 := .sprintf(, ...)
	return .newError(, , , )
}

func ( *Checker) ( positioner,  errorCode,  string) {
	.err(.newError(, , false, ))
}

func ( *Checker) ( positioner,  errorCode,  string,  ...interface{}) {
	.error(, , .sprintf(, ...))
}

func ( *Checker) ( positioner,  errorCode,  string,  ...interface{}) {
	.err(.newErrorf(, , true, , ...))
}

func ( *Checker) ( positioner,  string,  ...interface{}) {
	.errorf(, 0, "invalid AST: "+, ...)
}

func ( *Checker) ( positioner,  errorCode,  string,  ...interface{}) {
	.errorf(, , "invalid argument: "+, ...)
}

func ( *Checker) ( positioner,  errorCode,  string,  ...interface{}) {
	.errorf(, , "invalid operation: "+, ...)
}

// The positioner interface is used to extract the position of type-checker
// errors.
type positioner interface {
	Pos() token.Pos
}

// posSpan holds a position range along with a highlighted position within that
// range. This is used for positioning errors, with pos by convention being the
// first position in the source where the error is known to exist, and start
// and end defining the full span of syntax being considered when the error was
// detected. Invariant: start <= pos < end || start == pos == end.
type posSpan struct {
	start, pos, end token.Pos
}

func ( posSpan) () token.Pos {
	return .pos
}

// inNode creates a posSpan for the given node.
// Invariant: node.Pos() <= pos < node.End() (node.End() is the position of the
// first byte after node within the source).
func inNode( ast.Node,  token.Pos) posSpan {
	,  := .Pos(), .End()
	if debug {
		assert( <=  &&  < )
	}
	return posSpan{, , }
}

// atPos wraps a token.Pos to implement the positioner interface.
type atPos token.Pos

func ( atPos) () token.Pos {
	return token.Pos()
}

// spanOf extracts an error span from the given positioner. By default this is
// the trivial span starting and ending at pos, but this span is expanded when
// the argument naturally corresponds to a span of source code.
func spanOf( positioner) posSpan {
	switch x := .(type) {
	case nil:
		panic("internal error: nil")
	case posSpan:
		return 
	case ast.Node:
		 := .Pos()
		return posSpan{, , .End()}
	case *operand:
		if .expr != nil {
			 := .Pos()
			return posSpan{, , .expr.End()}
		}
		return posSpan{token.NoPos, token.NoPos, token.NoPos}
	default:
		 := .Pos()
		return posSpan{, , }
	}
}