// 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:
		.WriteString("(bad expr)") // 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:
		.WriteByte('(')
		(, .Type)
		.WriteString(" literal)") // shortened

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

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

	case *ast.IndexExpr:
		(, .X)
		.WriteByte('[')
		(, .Index)
		.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('(')
		for ,  := range .Args {
			if  > 0 {
				.WriteString(", ")
			}
			(, )
		}
		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, "; ", false)
		.WriteByte('}')

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

	case *ast.InterfaceType:
		.WriteString("interface{")
		writeFieldList(, .Methods, "; ", 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, ", ", 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(, , ", ", false)
	.WriteByte(')')
}

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

		// field list names
		for ,  := range .Names {
			if  > 0 {
				.WriteString(", ")
			}
			.WriteString(.Name)
		}

		// 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
	}
}