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

package types

import (
	
	
	
	
)

// An Object describes a named language entity such as a package,
// constant, type, variable, function (incl. methods), or label.
// All objects implement the Object interface.
//
type Object interface {
	Parent() *Scope // scope in which this object is declared; nil for methods and struct fields
	Pos() token.Pos // position of object identifier in declaration
	Pkg() *Package  // package to which this object belongs; nil for labels and objects in the Universe scope
	Name() string   // package local object name
	Type() Type     // object type
	Exported() bool // reports whether the name starts with a capital letter
	Id() string     // object name if exported, qualified name if not exported (see func Id)

	// String returns a human-readable string of the object.
	String() string

	// order reflects a package-level object's source order: if object
	// a is before object b in the source, then a.order() < b.order().
	// order returns a value > 0 for package-level objects; it returns
	// 0 for all other objects (including objects in file scopes).
	order() uint32

	// color returns the object's color.
	color() color

	// setOrder sets the order number of the object. It must be > 0.
	setOrder(uint32)

	// setColor sets the object's color. It must not be white.
	setColor(color color)

	// setParent sets the parent scope of the object.
	setParent(*Scope)

	// sameId reports whether obj.Id() and Id(pkg, name) are the same.
	sameId(pkg *Package, name string) bool

	// scopePos returns the start position of the scope of this Object
	scopePos() token.Pos

	// setScopePos sets the start position of the scope for this Object.
	setScopePos(pos token.Pos)
}

// Id returns name if it is exported, otherwise it
// returns the name qualified with the package path.
func ( *Package,  string) string {
	if token.IsExported() {
		return 
	}
	// unexported names need the package path for differentiation
	// (if there's no package, make sure we don't start with '.'
	// as that may change the order of methods between a setup
	// inside a package and outside a package - which breaks some
	// tests)
	 := "_"
	// pkg is nil for objects in Universe scope and possibly types
	// introduced via Eval (see also comment in object.sameId)
	if  != nil && .path != "" {
		 = .path
	}
	return  + "." + 
}

// An object implements the common parts of an Object.
type object struct {
	parent    *Scope
	pos       token.Pos
	pkg       *Package
	name      string
	typ       Type
	order_    uint32
	color_    color
	scopePos_ token.Pos
}

// color encodes the color of an object (see Checker.objDecl for details).
type color uint32

// An object may be painted in one of three colors.
// Color values other than white or black are considered grey.
const (
	white color = iota
	black
	grey // must be > white and black
)

func ( color) () string {
	switch  {
	case white:
		return "white"
	case black:
		return "black"
	default:
		return "grey"
	}
}

// colorFor returns the (initial) color for an object depending on
// whether its type t is known or not.
func colorFor( Type) color {
	if  != nil {
		return black
	}
	return white
}

// Parent returns the scope in which the object is declared.
// The result is nil for methods and struct fields.
func ( *object) () *Scope { return .parent }

// Pos returns the declaration position of the object's identifier.
func ( *object) () token.Pos { return .pos }

// Pkg returns the package to which the object belongs.
// The result is nil for labels and objects in the Universe scope.
func ( *object) () *Package { return .pkg }

// Name returns the object's (package-local, unqualified) name.
func ( *object) () string { return .name }

// Type returns the object's type.
func ( *object) () Type { return .typ }

// Exported reports whether the object is exported (starts with a capital letter).
// It doesn't take into account whether the object is in a local (function) scope
// or not.
func ( *object) () bool { return token.IsExported(.name) }

// Id is a wrapper for Id(obj.Pkg(), obj.Name()).
func ( *object) () string { return Id(.pkg, .name) }

func ( *object) () string      { panic("abstract") }
func ( *object) () uint32       { return .order_ }
func ( *object) () color        { return .color_ }
func ( *object) () token.Pos { return .scopePos_ }

func ( *object) ( *Scope)   { .parent =  }
func ( *object) ( uint32)     { assert( > 0); .order_ =  }
func ( *object) ( color)      { assert( != white); .color_ =  }
func ( *object) ( token.Pos) { .scopePos_ =  }

func ( *object) ( *Package,  string) bool {
	// spec:
	// "Two identifiers are different if they are spelled differently,
	// or if they appear in different packages and are not exported.
	// Otherwise, they are the same."
	if  != .name {
		return false
	}
	// obj.Name == name
	if .Exported() {
		return true
	}
	// not exported, so packages must be the same (pkg == nil for
	// fields in Universe scope; this can only happen for types
	// introduced via Eval)
	if  == nil || .pkg == nil {
		return  == .pkg
	}
	// pkg != nil && obj.pkg != nil
	return .path == .pkg.path
}

// A PkgName represents an imported Go package.
// PkgNames don't have a type.
type PkgName struct {
	object
	imported *Package
	used     bool // set if the package was used
}

// NewPkgName returns a new PkgName object representing an imported package.
// The remaining arguments set the attributes found with all Objects.
func ( token.Pos,  *Package,  string,  *Package) *PkgName {
	return &PkgName{object{nil, , , , Typ[Invalid], 0, black, token.NoPos}, , false}
}

// Imported returns the package that was imported.
// It is distinct from Pkg(), which is the package containing the import statement.
func ( *PkgName) () *Package { return .imported }

// A Const represents a declared constant.
type Const struct {
	object
	val constant.Value
}

// NewConst returns a new constant with value val.
// The remaining arguments set the attributes found with all Objects.
func ( token.Pos,  *Package,  string,  Type,  constant.Value) *Const {
	return &Const{object{nil, , , , , 0, colorFor(), token.NoPos}, }
}

// Val returns the constant's value.
func ( *Const) () constant.Value { return .val }

func (*Const) () {} // a constant may be a dependency of an initialization expression

// A TypeName represents a name for a (defined or alias) type.
type TypeName struct {
	object
}

// NewTypeName returns a new type name denoting the given typ.
// The remaining arguments set the attributes found with all Objects.
//
// The typ argument may be a defined (Named) type or an alias type.
// It may also be nil such that the returned TypeName can be used as
// argument for NewNamed, which will set the TypeName's type as a side-
// effect.
func ( token.Pos,  *Package,  string,  Type) *TypeName {
	return &TypeName{object{nil, , , , , 0, colorFor(), token.NoPos}}
}

// IsAlias reports whether obj is an alias name for a type.
func ( *TypeName) () bool {
	switch t := .typ.(type) {
	case nil:
		return false
	case *Basic:
		// unsafe.Pointer is not an alias.
		if .pkg == Unsafe {
			return false
		}
		// Any user-defined type name for a basic type is an alias for a
		// basic type (because basic types are pre-declared in the Universe
		// scope, outside any package scope), and so is any type name with
		// a different name than the name of the basic type it refers to.
		// Additionally, we need to look for "byte" and "rune" because they
		// are aliases but have the same names (for better error messages).
		return .pkg != nil || .name != .name ||  == universeByte ||  == universeRune
	case *Named:
		return  != .obj
	default:
		return true
	}
}

// A Variable represents a declared variable (including function parameters and results, and struct fields).
type Var struct {
	object
	embedded bool // if set, the variable is an embedded struct field, and name is the type name
	isField  bool // var is struct field
	used     bool // set if the variable was used
}

// NewVar returns a new variable.
// The arguments set the attributes found with all Objects.
func ( token.Pos,  *Package,  string,  Type) *Var {
	return &Var{object: object{nil, , , , , 0, colorFor(), token.NoPos}}
}

// NewParam returns a new variable representing a function parameter.
func ( token.Pos,  *Package,  string,  Type) *Var {
	return &Var{object: object{nil, , , , , 0, colorFor(), token.NoPos}, used: true} // parameters are always 'used'
}

// NewField returns a new variable representing a struct field.
// For embedded fields, the name is the unqualified type name
/// under which the field is accessible.
func ( token.Pos,  *Package,  string,  Type,  bool) *Var {
	return &Var{object: object{nil, , , , , 0, colorFor(), token.NoPos}, embedded: , isField: true}
}

// Anonymous reports whether the variable is an embedded field.
// Same as Embedded; only present for backward-compatibility.
func ( *Var) () bool { return .embedded }

// Embedded reports whether the variable is an embedded field.
func ( *Var) () bool { return .embedded }

// IsField reports whether the variable is a struct field.
func ( *Var) () bool { return .isField }

func (*Var) () {} // a variable may be a dependency of an initialization expression

// A Func represents a declared function, concrete method, or abstract
// (interface) method. Its Type() is always a *Signature.
// An abstract method may belong to many interfaces due to embedding.
type Func struct {
	object
	hasPtrRecv bool // only valid for methods that don't have a type yet
}

// NewFunc returns a new function with the given signature, representing
// the function's type.
func ( token.Pos,  *Package,  string,  *Signature) *Func {
	// don't store a nil signature
	var  Type
	if  != nil {
		 = 
	}
	return &Func{object{nil, , , , , 0, colorFor(), token.NoPos}, false}
}

// FullName returns the package- or receiver-type-qualified name of
// function or method obj.
func ( *Func) () string {
	var  bytes.Buffer
	writeFuncName(&, , nil)
	return .String()
}

// Scope returns the scope of the function's body block.
func ( *Func) () *Scope { return .typ.(*Signature).scope }

func (*Func) () {} // a function may be a dependency of an initialization expression

// A Label represents a declared label.
// Labels don't have a type.
type Label struct {
	object
	used bool // set if the label was used
}

// NewLabel returns a new label.
func ( token.Pos,  *Package,  string) *Label {
	return &Label{object{pos: , pkg: , name: , typ: Typ[Invalid], color_: black}, false}
}

// A Builtin represents a built-in function.
// Builtins don't have a valid type.
type Builtin struct {
	object
	id builtinId
}

func newBuiltin( builtinId) *Builtin {
	return &Builtin{object{name: predeclaredFuncs[].name, typ: Typ[Invalid], color_: black}, }
}

// Nil represents the predeclared value nil.
type Nil struct {
	object
}

func writeObject( *bytes.Buffer,  Object,  Qualifier) {
	var  *TypeName
	 := .Type()

	switch obj := .(type) {
	case *PkgName:
		fmt.Fprintf(, "package %s", .Name())
		if  := .imported.path;  != "" &&  != .name {
			fmt.Fprintf(, " (%q)", )
		}
		return

	case *Const:
		.WriteString("const")

	case *TypeName:
		 = 
		.WriteString("type")

	case *Var:
		if .isField {
			.WriteString("field")
		} else {
			.WriteString("var")
		}

	case *Func:
		.WriteString("func ")
		writeFuncName(, , )
		if  != nil {
			WriteSignature(, .(*Signature), )
		}
		return

	case *Label:
		.WriteString("label")
		 = nil

	case *Builtin:
		.WriteString("builtin")
		 = nil

	case *Nil:
		.WriteString("nil")
		return

	default:
		panic(fmt.Sprintf("writeObject(%T)", ))
	}

	.WriteByte(' ')

	// For package-level objects, qualify the name.
	if .Pkg() != nil && .Pkg().scope.Lookup(.Name()) ==  {
		writePackage(, .Pkg(), )
	}
	.WriteString(.Name())

	if  == nil {
		return
	}

	if  != nil {
		// We have a type object: Don't print anything more for
		// basic types since there's no more information (names
		// are the same; see also comment in TypeName.IsAlias).
		if ,  := .(*Basic);  {
			return
		}
		if .IsAlias() {
			.WriteString(" =")
		} else {
			 = .Underlying()
		}
	}

	.WriteByte(' ')
	WriteType(, , )
}

func writePackage( *bytes.Buffer,  *Package,  Qualifier) {
	if  == nil {
		return
	}
	var  string
	if  != nil {
		 = ()
	} else {
		 = .Path()
	}
	if  != "" {
		.WriteString()
		.WriteByte('.')
	}
}

// ObjectString returns the string form of obj.
// The Qualifier controls the printing of
// package-level objects, and may be nil.
func ( Object,  Qualifier) string {
	var  bytes.Buffer
	writeObject(&, , )
	return .String()
}

func ( *PkgName) () string  { return ObjectString(, nil) }
func ( *Const) () string    { return ObjectString(, nil) }
func ( *TypeName) () string { return ObjectString(, nil) }
func ( *Var) () string      { return ObjectString(, nil) }
func ( *Func) () string     { return ObjectString(, nil) }
func ( *Label) () string    { return ObjectString(, nil) }
func ( *Builtin) () string  { return ObjectString(, nil) }
func ( *Nil) () string      { return ObjectString(, nil) }

func writeFuncName( *bytes.Buffer,  *Func,  Qualifier) {
	if .typ != nil {
		 := .typ.(*Signature)
		if  := .Recv();  != nil {
			.WriteByte('(')
			if ,  := .Type().(*Interface);  {
				// gcimporter creates abstract methods of
				// named interfaces using the interface type
				// (not the named type) as the receiver.
				// Don't print it in full.
				.WriteString("interface")
			} else {
				WriteType(, .Type(), )
			}
			.WriteByte(')')
			.WriteByte('.')
		} else if .pkg != nil {
			writePackage(, .pkg, )
		}
	}
	.WriteString(.name)
}