// 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 printing of expressions.

package types

import (
	
	
	
	
)

// ExprString returns the (possibly shortened) string representation for x.
// Shortened representations are suitable for user interfaces but may not
// necessarily follow Go syntax.
func ( ast.Expr) string {
	var  bytes.Buffer
	WriteExpr(&, )
	return .String()
}

// WriteExpr writes the (possibly shortened) string representation for x to buf.
// Shortened representations are suitable for user interfaces but may not
// necessarily follow Go syntax.
func ( *bytes.Buffer,  ast.Expr) {
	// The AST preserves source-level parentheses so there is
	// no need to introduce them here to correct for different
	// operator precedences. (This assumes that the AST was
	// generated by a Go parser.)

	switch x := .(type) {
	default:
		fmt.Fprintf(, "(ast: %T)", ) // nil, ast.BadExpr, ast.KeyValueExpr

	case *ast.Ident:
		.WriteString(.Name)

	case *ast.Ellipsis:
		.WriteString("...")
		if .Elt != nil {
			(, .Elt)
		}

	case *ast.BasicLit:
		.WriteString(.Value)

	case *ast.FuncLit:
		.WriteByte('(')
		(, .Type)
		.WriteString(" literal)") // shortened

	case *ast.CompositeLit:
		(, .Type)
		.WriteByte('{')
		if len(.Elts) > 0 {
			.WriteString("…")
		}
		.WriteByte('}')

	case *ast.ParenExpr:
		.WriteByte('(')
		(, .X)
		.WriteByte(')')

	case *ast.SelectorExpr:
		(, .X)
		.WriteByte('.')
		.WriteString(.Sel.Name)

	case *ast.IndexExpr, *ast.IndexListExpr:
		 := typeparams.UnpackIndexExpr()
		(, .X)
		.WriteByte('[')
		writeExprList(, .Indices)
		.WriteByte(']')

	case *ast.SliceExpr:
		(, .X)
		.WriteByte('[')
		if .Low != nil {
			(, .Low)
		}
		.WriteByte(':')
		if .High != nil {
			(, .High)
		}
		if .Slice3 {
			.WriteByte(':')
			if .Max != nil {
				(, .Max)
			}
		}
		.WriteByte(']')

	case *ast.TypeAssertExpr:
		(, .X)
		.WriteString(".(")
		(, .Type)
		.WriteByte(')')

	case *ast.CallExpr:
		(, .Fun)
		.WriteByte('(')
		writeExprList(, .Args)
		if .Ellipsis.IsValid() {
			.WriteString("...")
		}
		.WriteByte(')')

	case *ast.StarExpr:
		.WriteByte('*')
		(, .X)

	case *ast.UnaryExpr:
		.WriteString(.Op.String())
		(, .X)

	case *ast.BinaryExpr:
		(, .X)
		.WriteByte(' ')
		.WriteString(.Op.String())
		.WriteByte(' ')
		(, .Y)

	case *ast.ArrayType:
		.WriteByte('[')
		if .Len != nil {
			(, .Len)
		}
		.WriteByte(']')
		(, .Elt)

	case *ast.StructType:
		.WriteString("struct{")
		writeFieldList(, .Fields.List, "; ", false)
		.WriteByte('}')

	case *ast.FuncType:
		.WriteString("func")
		writeSigExpr(, )

	case *ast.InterfaceType:
		.WriteString("interface{")
		writeFieldList(, .Methods.List, "; ", true)
		.WriteByte('}')

	case *ast.MapType:
		.WriteString("map[")
		(, .Key)
		.WriteByte(']')
		(, .Value)

	case *ast.ChanType:
		var  string
		switch .Dir {
		case ast.SEND:
			 = "chan<- "
		case ast.RECV:
			 = "<-chan "
		default:
			 = "chan "
		}
		.WriteString()
		(, .Value)
	}
}

func writeSigExpr( *bytes.Buffer,  *ast.FuncType) {
	.WriteByte('(')
	writeFieldList(, .Params.List, ", ", false)
	.WriteByte(')')

	 := .Results
	 := .NumFields()
	if  == 0 {
		// no result
		return
	}

	.WriteByte(' ')
	if  == 1 && len(.List[0].Names) == 0 {
		// single unnamed result
		WriteExpr(, .List[0].Type)
		return
	}

	// multiple or named result(s)
	.WriteByte('(')
	writeFieldList(, .List, ", ", false)
	.WriteByte(')')
}

func writeFieldList( *bytes.Buffer,  []*ast.Field,  string,  bool) {
	for ,  := range  {
		if  > 0 {
			.WriteString()
		}

		// field list names
		writeIdentList(, .Names)

		// types of interface methods consist of signatures only
		if ,  := .Type.(*ast.FuncType);  != nil &&  {
			writeSigExpr(, )
			continue
		}

		// named fields are separated with a blank from the field type
		if len(.Names) > 0 {
			.WriteByte(' ')
		}

		WriteExpr(, .Type)

		// ignore tag
	}
}

func writeIdentList( *bytes.Buffer,  []*ast.Ident) {
	for ,  := range  {
		if  > 0 {
			.WriteString(", ")
		}
		.WriteString(.Name)
	}
}

func writeExprList( *bytes.Buffer,  []ast.Expr) {
	for ,  := range  {
		if  > 0 {
			.WriteString(", ")
		}
		WriteExpr(, )
	}
}