Source file src/go/types/object.go

     1  // Code generated by "go test -run=Generate -write=all"; DO NOT EDIT.
     2  // Source: ../../cmd/compile/internal/types2/object.go
     3  
     4  // Copyright 2013 The Go Authors. All rights reserved.
     5  // Use of this source code is governed by a BSD-style
     6  // license that can be found in the LICENSE file.
     7  
     8  package types
     9  
    10  import (
    11  	"bytes"
    12  	"fmt"
    13  	"go/constant"
    14  	"go/token"
    15  	"strings"
    16  	"unicode"
    17  	"unicode/utf8"
    18  )
    19  
    20  // An Object is a named language entity.
    21  // An Object may be a constant ([Const]), type name ([TypeName]),
    22  // variable or struct field ([Var]), function or method ([Func]),
    23  // imported package ([PkgName]), label ([Label]),
    24  // built-in function ([Builtin]),
    25  // or the predeclared identifier 'nil' ([Nil]).
    26  //
    27  // The environment, which is structured as a tree of Scopes,
    28  // maps each name to the unique Object that it denotes.
    29  type Object interface {
    30  	Parent() *Scope // scope in which this object is declared; nil for methods and struct fields
    31  	Pos() token.Pos // position of object identifier in declaration
    32  	Pkg() *Package  // package to which this object belongs; nil for labels and objects in the Universe scope
    33  	Name() string   // package local object name
    34  	Type() Type     // object type
    35  	Exported() bool // reports whether the name starts with a capital letter
    36  	Id() string     // object name if exported, qualified name if not exported (see func Id)
    37  
    38  	// String returns a human-readable string of the object.
    39  	// Use [ObjectString] to control how package names are formatted in the string.
    40  	String() string
    41  
    42  	// order reflects a package-level object's source order: if object
    43  	// a is before object b in the source, then a.order() < b.order().
    44  	// order returns a value > 0 for package-level objects; it returns
    45  	// 0 for all other objects (including objects in file scopes).
    46  	order() uint32
    47  
    48  	// color returns the object's color.
    49  	color() color
    50  
    51  	// setType sets the type of the object.
    52  	setType(Type)
    53  
    54  	// setOrder sets the order number of the object. It must be > 0.
    55  	setOrder(uint32)
    56  
    57  	// setColor sets the object's color. It must not be white.
    58  	setColor(color color)
    59  
    60  	// setParent sets the parent scope of the object.
    61  	setParent(*Scope)
    62  
    63  	// sameId reports whether obj.Id() and Id(pkg, name) are the same.
    64  	// If foldCase is true, names are considered equal if they are equal with case folding
    65  	// and their packages are ignored (e.g., pkg1.m, pkg1.M, pkg2.m, and pkg2.M are all equal).
    66  	sameId(pkg *Package, name string, foldCase bool) bool
    67  
    68  	// scopePos returns the start position of the scope of this Object
    69  	scopePos() token.Pos
    70  
    71  	// setScopePos sets the start position of the scope for this Object.
    72  	setScopePos(pos token.Pos)
    73  }
    74  
    75  func isExported(name string) bool {
    76  	ch, _ := utf8.DecodeRuneInString(name)
    77  	return unicode.IsUpper(ch)
    78  }
    79  
    80  // Id returns name if it is exported, otherwise it
    81  // returns the name qualified with the package path.
    82  func Id(pkg *Package, name string) string {
    83  	if isExported(name) {
    84  		return name
    85  	}
    86  	// unexported names need the package path for differentiation
    87  	// (if there's no package, make sure we don't start with '.'
    88  	// as that may change the order of methods between a setup
    89  	// inside a package and outside a package - which breaks some
    90  	// tests)
    91  	path := "_"
    92  	// pkg is nil for objects in Universe scope and possibly types
    93  	// introduced via Eval (see also comment in object.sameId)
    94  	if pkg != nil && pkg.path != "" {
    95  		path = pkg.path
    96  	}
    97  	return path + "." + name
    98  }
    99  
   100  // An object implements the common parts of an Object.
   101  type object struct {
   102  	parent    *Scope
   103  	pos       token.Pos
   104  	pkg       *Package
   105  	name      string
   106  	typ       Type
   107  	order_    uint32
   108  	color_    color
   109  	scopePos_ token.Pos
   110  }
   111  
   112  // color encodes the color of an object (see Checker.objDecl for details).
   113  type color uint32
   114  
   115  // An object may be painted in one of three colors.
   116  // Color values other than white or black are considered grey.
   117  const (
   118  	white color = iota
   119  	black
   120  	grey // must be > white and black
   121  )
   122  
   123  func (c color) String() string {
   124  	switch c {
   125  	case white:
   126  		return "white"
   127  	case black:
   128  		return "black"
   129  	default:
   130  		return "grey"
   131  	}
   132  }
   133  
   134  // colorFor returns the (initial) color for an object depending on
   135  // whether its type t is known or not.
   136  func colorFor(t Type) color {
   137  	if t != nil {
   138  		return black
   139  	}
   140  	return white
   141  }
   142  
   143  // Parent returns the scope in which the object is declared.
   144  // The result is nil for methods and struct fields.
   145  func (obj *object) Parent() *Scope { return obj.parent }
   146  
   147  // Pos returns the declaration position of the object's identifier.
   148  func (obj *object) Pos() token.Pos { return obj.pos }
   149  
   150  // Pkg returns the package to which the object belongs.
   151  // The result is nil for labels and objects in the Universe scope.
   152  func (obj *object) Pkg() *Package { return obj.pkg }
   153  
   154  // Name returns the object's (package-local, unqualified) name.
   155  func (obj *object) Name() string { return obj.name }
   156  
   157  // Type returns the object's type.
   158  func (obj *object) Type() Type { return obj.typ }
   159  
   160  // Exported reports whether the object is exported (starts with a capital letter).
   161  // It doesn't take into account whether the object is in a local (function) scope
   162  // or not.
   163  func (obj *object) Exported() bool { return isExported(obj.name) }
   164  
   165  // Id is a wrapper for Id(obj.Pkg(), obj.Name()).
   166  func (obj *object) Id() string { return Id(obj.pkg, obj.name) }
   167  
   168  func (obj *object) String() string      { panic("abstract") }
   169  func (obj *object) order() uint32       { return obj.order_ }
   170  func (obj *object) color() color        { return obj.color_ }
   171  func (obj *object) scopePos() token.Pos { return obj.scopePos_ }
   172  
   173  func (obj *object) setParent(parent *Scope)   { obj.parent = parent }
   174  func (obj *object) setType(typ Type)          { obj.typ = typ }
   175  func (obj *object) setOrder(order uint32)     { assert(order > 0); obj.order_ = order }
   176  func (obj *object) setColor(color color)      { assert(color != white); obj.color_ = color }
   177  func (obj *object) setScopePos(pos token.Pos) { obj.scopePos_ = pos }
   178  
   179  func (obj *object) sameId(pkg *Package, name string, foldCase bool) bool {
   180  	// If we don't care about capitalization, we also ignore packages.
   181  	if foldCase && strings.EqualFold(obj.name, name) {
   182  		return true
   183  	}
   184  	// spec:
   185  	// "Two identifiers are different if they are spelled differently,
   186  	// or if they appear in different packages and are not exported.
   187  	// Otherwise, they are the same."
   188  	if obj.name != name {
   189  		return false
   190  	}
   191  	// obj.Name == name
   192  	if obj.Exported() {
   193  		return true
   194  	}
   195  	// not exported, so packages must be the same
   196  	return samePkg(obj.pkg, pkg)
   197  }
   198  
   199  // cmp reports whether object a is ordered before object b.
   200  // cmp returns:
   201  //
   202  //	-1 if a is before b
   203  //	 0 if a is equivalent to b
   204  //	+1 if a is behind b
   205  //
   206  // Objects are ordered nil before non-nil, exported before
   207  // non-exported, then by name, and finally (for non-exported
   208  // functions) by package path.
   209  func (a *object) cmp(b *object) int {
   210  	if a == b {
   211  		return 0
   212  	}
   213  
   214  	// Nil before non-nil.
   215  	if a == nil {
   216  		return -1
   217  	}
   218  	if b == nil {
   219  		return +1
   220  	}
   221  
   222  	// Exported functions before non-exported.
   223  	ea := isExported(a.name)
   224  	eb := isExported(b.name)
   225  	if ea != eb {
   226  		if ea {
   227  			return -1
   228  		}
   229  		return +1
   230  	}
   231  
   232  	// Order by name and then (for non-exported names) by package.
   233  	if a.name != b.name {
   234  		return strings.Compare(a.name, b.name)
   235  	}
   236  	if !ea {
   237  		return strings.Compare(a.pkg.path, b.pkg.path)
   238  	}
   239  
   240  	return 0
   241  }
   242  
   243  // A PkgName represents an imported Go package.
   244  // PkgNames don't have a type.
   245  type PkgName struct {
   246  	object
   247  	imported *Package
   248  }
   249  
   250  // NewPkgName returns a new PkgName object representing an imported package.
   251  // The remaining arguments set the attributes found with all Objects.
   252  func NewPkgName(pos token.Pos, pkg *Package, name string, imported *Package) *PkgName {
   253  	return &PkgName{object{nil, pos, pkg, name, Typ[Invalid], 0, black, nopos}, imported}
   254  }
   255  
   256  // Imported returns the package that was imported.
   257  // It is distinct from Pkg(), which is the package containing the import statement.
   258  func (obj *PkgName) Imported() *Package { return obj.imported }
   259  
   260  // A Const represents a declared constant.
   261  type Const struct {
   262  	object
   263  	val constant.Value
   264  }
   265  
   266  // NewConst returns a new constant with value val.
   267  // The remaining arguments set the attributes found with all Objects.
   268  func NewConst(pos token.Pos, pkg *Package, name string, typ Type, val constant.Value) *Const {
   269  	return &Const{object{nil, pos, pkg, name, typ, 0, colorFor(typ), nopos}, val}
   270  }
   271  
   272  // Val returns the constant's value.
   273  func (obj *Const) Val() constant.Value { return obj.val }
   274  
   275  func (*Const) isDependency() {} // a constant may be a dependency of an initialization expression
   276  
   277  // A TypeName is an [Object] that represents a type with a name:
   278  // a defined type ([Named]),
   279  // an alias type ([Alias]),
   280  // a type parameter ([TypeParam]),
   281  // or a predeclared type such as int or error.
   282  type TypeName struct {
   283  	object
   284  }
   285  
   286  // NewTypeName returns a new type name denoting the given typ.
   287  // The remaining arguments set the attributes found with all Objects.
   288  //
   289  // The typ argument may be a defined (Named) type or an alias type.
   290  // It may also be nil such that the returned TypeName can be used as
   291  // argument for NewNamed, which will set the TypeName's type as a side-
   292  // effect.
   293  func NewTypeName(pos token.Pos, pkg *Package, name string, typ Type) *TypeName {
   294  	return &TypeName{object{nil, pos, pkg, name, typ, 0, colorFor(typ), nopos}}
   295  }
   296  
   297  // NewTypeNameLazy returns a new defined type like NewTypeName, but it
   298  // lazily calls unpack to finish constructing the Named object.
   299  func _NewTypeNameLazy(pos token.Pos, pkg *Package, name string, load func(*Named) ([]*TypeParam, Type, []*Func, []func())) *TypeName {
   300  	obj := NewTypeName(pos, pkg, name, nil)
   301  	n := (*Checker)(nil).newNamed(obj, nil, nil)
   302  	n.loader = load
   303  	return obj
   304  }
   305  
   306  // IsAlias reports whether obj is an alias name for a type.
   307  func (obj *TypeName) IsAlias() bool {
   308  	switch t := obj.typ.(type) {
   309  	case nil:
   310  		return false
   311  	// case *Alias:
   312  	//	handled by default case
   313  	case *Basic:
   314  		// unsafe.Pointer is not an alias.
   315  		if obj.pkg == Unsafe {
   316  			return false
   317  		}
   318  		// Any user-defined type name for a basic type is an alias for a
   319  		// basic type (because basic types are pre-declared in the Universe
   320  		// scope, outside any package scope), and so is any type name with
   321  		// a different name than the name of the basic type it refers to.
   322  		// Additionally, we need to look for "byte" and "rune" because they
   323  		// are aliases but have the same names (for better error messages).
   324  		return obj.pkg != nil || t.name != obj.name || t == universeByte || t == universeRune
   325  	case *Named:
   326  		return obj != t.obj
   327  	case *TypeParam:
   328  		return obj != t.obj
   329  	default:
   330  		return true
   331  	}
   332  }
   333  
   334  // A Var represents a declared variable (including function parameters and results, and struct fields).
   335  type Var struct {
   336  	object
   337  	origin   *Var // if non-nil, the Var from which this one was instantiated
   338  	kind     VarKind
   339  	embedded bool // if set, the variable is an embedded struct field, and name is the type name
   340  }
   341  
   342  // A VarKind discriminates the various kinds of variables.
   343  type VarKind uint8
   344  
   345  const (
   346  	_          VarKind = iota // (not meaningful)
   347  	PackageVar                // a package-level variable
   348  	LocalVar                  // a local variable
   349  	RecvVar                   // a method receiver variable
   350  	ParamVar                  // a function parameter variable
   351  	ResultVar                 // a function result variable
   352  	FieldVar                  // a struct field
   353  )
   354  
   355  var varKindNames = [...]string{
   356  	0:          "VarKind(0)",
   357  	PackageVar: "PackageVar",
   358  	LocalVar:   "LocalVar",
   359  	RecvVar:    "RecvVar",
   360  	ParamVar:   "ParamVar",
   361  	ResultVar:  "ResultVar",
   362  	FieldVar:   "FieldVar",
   363  }
   364  
   365  func (kind VarKind) String() string {
   366  	if 0 <= kind && int(kind) < len(varKindNames) {
   367  		return varKindNames[kind]
   368  	}
   369  	return fmt.Sprintf("VarKind(%d)", kind)
   370  }
   371  
   372  // Kind reports what kind of variable v is.
   373  func (v *Var) Kind() VarKind { return v.kind }
   374  
   375  // SetKind sets the kind of the variable.
   376  // It should be used only immediately after [NewVar] or [NewParam].
   377  func (v *Var) SetKind(kind VarKind) { v.kind = kind }
   378  
   379  // NewVar returns a new variable.
   380  // The arguments set the attributes found with all Objects.
   381  //
   382  // The caller must subsequently call [Var.SetKind]
   383  // if the desired Var is not of kind [PackageVar].
   384  func NewVar(pos token.Pos, pkg *Package, name string, typ Type) *Var {
   385  	return newVar(PackageVar, pos, pkg, name, typ)
   386  }
   387  
   388  // NewParam returns a new variable representing a function parameter.
   389  //
   390  // The caller must subsequently call [Var.SetKind] if the desired Var
   391  // is not of kind [ParamVar]: for example, [RecvVar] or [ResultVar].
   392  func NewParam(pos token.Pos, pkg *Package, name string, typ Type) *Var {
   393  	return newVar(ParamVar, pos, pkg, name, typ)
   394  }
   395  
   396  // NewField returns a new variable representing a struct field.
   397  // For embedded fields, the name is the unqualified type name
   398  // under which the field is accessible.
   399  func NewField(pos token.Pos, pkg *Package, name string, typ Type, embedded bool) *Var {
   400  	v := newVar(FieldVar, pos, pkg, name, typ)
   401  	v.embedded = embedded
   402  	return v
   403  }
   404  
   405  // newVar returns a new variable.
   406  // The arguments set the attributes found with all Objects.
   407  func newVar(kind VarKind, pos token.Pos, pkg *Package, name string, typ Type) *Var {
   408  	return &Var{object: object{nil, pos, pkg, name, typ, 0, colorFor(typ), nopos}, kind: kind}
   409  }
   410  
   411  // Anonymous reports whether the variable is an embedded field.
   412  // Same as Embedded; only present for backward-compatibility.
   413  func (obj *Var) Anonymous() bool { return obj.embedded }
   414  
   415  // Embedded reports whether the variable is an embedded field.
   416  func (obj *Var) Embedded() bool { return obj.embedded }
   417  
   418  // IsField reports whether the variable is a struct field.
   419  func (obj *Var) IsField() bool { return obj.kind == FieldVar }
   420  
   421  // Origin returns the canonical Var for its receiver, i.e. the Var object
   422  // recorded in Info.Defs.
   423  //
   424  // For synthetic Vars created during instantiation (such as struct fields or
   425  // function parameters that depend on type arguments), this will be the
   426  // corresponding Var on the generic (uninstantiated) type. For all other Vars
   427  // Origin returns the receiver.
   428  func (obj *Var) Origin() *Var {
   429  	if obj.origin != nil {
   430  		return obj.origin
   431  	}
   432  	return obj
   433  }
   434  
   435  func (*Var) isDependency() {} // a variable may be a dependency of an initialization expression
   436  
   437  // A Func represents a declared function, concrete method, or abstract
   438  // (interface) method. Its Type() is always a *Signature.
   439  // An abstract method may belong to many interfaces due to embedding.
   440  type Func struct {
   441  	object
   442  	hasPtrRecv_ bool  // only valid for methods that don't have a type yet; use hasPtrRecv() to read
   443  	origin      *Func // if non-nil, the Func from which this one was instantiated
   444  }
   445  
   446  // NewFunc returns a new function with the given signature, representing
   447  // the function's type.
   448  func NewFunc(pos token.Pos, pkg *Package, name string, sig *Signature) *Func {
   449  	var typ Type
   450  	if sig != nil {
   451  		typ = sig
   452  	} else {
   453  		// Don't store a (typed) nil *Signature.
   454  		// We can't simply replace it with new(Signature) either,
   455  		// as this would violate object.{Type,color} invariants.
   456  		// TODO(adonovan): propose to disallow NewFunc with nil *Signature.
   457  	}
   458  	return &Func{object{nil, pos, pkg, name, typ, 0, colorFor(typ), nopos}, false, nil}
   459  }
   460  
   461  // Signature returns the signature (type) of the function or method.
   462  func (obj *Func) Signature() *Signature {
   463  	if obj.typ != nil {
   464  		return obj.typ.(*Signature) // normal case
   465  	}
   466  	// No signature: Signature was called either:
   467  	// - within go/types, before a FuncDecl's initially
   468  	//   nil Func.Type was lazily populated, indicating
   469  	//   a types bug; or
   470  	// - by a client after NewFunc(..., nil),
   471  	//   which is arguably a client bug, but we need a
   472  	//   proposal to tighten NewFunc's precondition.
   473  	// For now, return a trivial signature.
   474  	return new(Signature)
   475  }
   476  
   477  // FullName returns the package- or receiver-type-qualified name of
   478  // function or method obj.
   479  func (obj *Func) FullName() string {
   480  	var buf bytes.Buffer
   481  	writeFuncName(&buf, obj, nil)
   482  	return buf.String()
   483  }
   484  
   485  // Scope returns the scope of the function's body block.
   486  // The result is nil for imported or instantiated functions and methods
   487  // (but there is also no mechanism to get to an instantiated function).
   488  func (obj *Func) Scope() *Scope { return obj.typ.(*Signature).scope }
   489  
   490  // Origin returns the canonical Func for its receiver, i.e. the Func object
   491  // recorded in Info.Defs.
   492  //
   493  // For synthetic functions created during instantiation (such as methods on an
   494  // instantiated Named type or interface methods that depend on type arguments),
   495  // this will be the corresponding Func on the generic (uninstantiated) type.
   496  // For all other Funcs Origin returns the receiver.
   497  func (obj *Func) Origin() *Func {
   498  	if obj.origin != nil {
   499  		return obj.origin
   500  	}
   501  	return obj
   502  }
   503  
   504  // Pkg returns the package to which the function belongs.
   505  //
   506  // The result is nil for methods of types in the Universe scope,
   507  // like method Error of the error built-in interface type.
   508  func (obj *Func) Pkg() *Package { return obj.object.Pkg() }
   509  
   510  // hasPtrRecv reports whether the receiver is of the form *T for the given method obj.
   511  func (obj *Func) hasPtrRecv() bool {
   512  	// If a method's receiver type is set, use that as the source of truth for the receiver.
   513  	// Caution: Checker.funcDecl (decl.go) marks a function by setting its type to an empty
   514  	// signature. We may reach here before the signature is fully set up: we must explicitly
   515  	// check if the receiver is set (we cannot just look for non-nil obj.typ).
   516  	if sig, _ := obj.typ.(*Signature); sig != nil && sig.recv != nil {
   517  		_, isPtr := deref(sig.recv.typ)
   518  		return isPtr
   519  	}
   520  
   521  	// If a method's type is not set it may be a method/function that is:
   522  	// 1) client-supplied (via NewFunc with no signature), or
   523  	// 2) internally created but not yet type-checked.
   524  	// For case 1) we can't do anything; the client must know what they are doing.
   525  	// For case 2) we can use the information gathered by the resolver.
   526  	return obj.hasPtrRecv_
   527  }
   528  
   529  func (*Func) isDependency() {} // a function may be a dependency of an initialization expression
   530  
   531  // A Label represents a declared label.
   532  // Labels don't have a type.
   533  type Label struct {
   534  	object
   535  	used bool // set if the label was used
   536  }
   537  
   538  // NewLabel returns a new label.
   539  func NewLabel(pos token.Pos, pkg *Package, name string) *Label {
   540  	return &Label{object{pos: pos, pkg: pkg, name: name, typ: Typ[Invalid], color_: black}, false}
   541  }
   542  
   543  // A Builtin represents a built-in function.
   544  // Builtins don't have a valid type.
   545  type Builtin struct {
   546  	object
   547  	id builtinId
   548  }
   549  
   550  func newBuiltin(id builtinId) *Builtin {
   551  	return &Builtin{object{name: predeclaredFuncs[id].name, typ: Typ[Invalid], color_: black}, id}
   552  }
   553  
   554  // Nil represents the predeclared value nil.
   555  type Nil struct {
   556  	object
   557  }
   558  
   559  func writeObject(buf *bytes.Buffer, obj Object, qf Qualifier) {
   560  	var tname *TypeName
   561  	typ := obj.Type()
   562  
   563  	switch obj := obj.(type) {
   564  	case *PkgName:
   565  		fmt.Fprintf(buf, "package %s", obj.Name())
   566  		if path := obj.imported.path; path != "" && path != obj.name {
   567  			fmt.Fprintf(buf, " (%q)", path)
   568  		}
   569  		return
   570  
   571  	case *Const:
   572  		buf.WriteString("const")
   573  
   574  	case *TypeName:
   575  		tname = obj
   576  		buf.WriteString("type")
   577  		if isTypeParam(typ) {
   578  			buf.WriteString(" parameter")
   579  		}
   580  
   581  	case *Var:
   582  		if obj.IsField() {
   583  			buf.WriteString("field")
   584  		} else {
   585  			buf.WriteString("var")
   586  		}
   587  
   588  	case *Func:
   589  		buf.WriteString("func ")
   590  		writeFuncName(buf, obj, qf)
   591  		if typ != nil {
   592  			WriteSignature(buf, typ.(*Signature), qf)
   593  		}
   594  		return
   595  
   596  	case *Label:
   597  		buf.WriteString("label")
   598  		typ = nil
   599  
   600  	case *Builtin:
   601  		buf.WriteString("builtin")
   602  		typ = nil
   603  
   604  	case *Nil:
   605  		buf.WriteString("nil")
   606  		return
   607  
   608  	default:
   609  		panic(fmt.Sprintf("writeObject(%T)", obj))
   610  	}
   611  
   612  	buf.WriteByte(' ')
   613  
   614  	// For package-level objects, qualify the name.
   615  	if obj.Pkg() != nil && obj.Pkg().scope.Lookup(obj.Name()) == obj {
   616  		buf.WriteString(packagePrefix(obj.Pkg(), qf))
   617  	}
   618  	buf.WriteString(obj.Name())
   619  
   620  	if typ == nil {
   621  		return
   622  	}
   623  
   624  	if tname != nil {
   625  		switch t := typ.(type) {
   626  		case *Basic:
   627  			// Don't print anything more for basic types since there's
   628  			// no more information.
   629  			return
   630  		case genericType:
   631  			if t.TypeParams().Len() > 0 {
   632  				newTypeWriter(buf, qf).tParamList(t.TypeParams().list())
   633  			}
   634  		}
   635  		if tname.IsAlias() {
   636  			buf.WriteString(" =")
   637  			if alias, ok := typ.(*Alias); ok { // materialized? (gotypesalias=1)
   638  				typ = alias.fromRHS
   639  			}
   640  		} else if t, _ := typ.(*TypeParam); t != nil {
   641  			typ = t.bound
   642  		} else {
   643  			// TODO(gri) should this be fromRHS for *Named?
   644  			// (See discussion in #66559.)
   645  			typ = typ.Underlying()
   646  		}
   647  	}
   648  
   649  	// Special handling for any: because WriteType will format 'any' as 'any',
   650  	// resulting in the object string `type any = any` rather than `type any =
   651  	// interface{}`. To avoid this, swap in a different empty interface.
   652  	if obj.Name() == "any" && obj.Parent() == Universe {
   653  		assert(Identical(typ, &emptyInterface))
   654  		typ = &emptyInterface
   655  	}
   656  
   657  	buf.WriteByte(' ')
   658  	WriteType(buf, typ, qf)
   659  }
   660  
   661  func packagePrefix(pkg *Package, qf Qualifier) string {
   662  	if pkg == nil {
   663  		return ""
   664  	}
   665  	var s string
   666  	if qf != nil {
   667  		s = qf(pkg)
   668  	} else {
   669  		s = pkg.Path()
   670  	}
   671  	if s != "" {
   672  		s += "."
   673  	}
   674  	return s
   675  }
   676  
   677  // ObjectString returns the string form of obj.
   678  // The Qualifier controls the printing of
   679  // package-level objects, and may be nil.
   680  func ObjectString(obj Object, qf Qualifier) string {
   681  	var buf bytes.Buffer
   682  	writeObject(&buf, obj, qf)
   683  	return buf.String()
   684  }
   685  
   686  func (obj *PkgName) String() string  { return ObjectString(obj, nil) }
   687  func (obj *Const) String() string    { return ObjectString(obj, nil) }
   688  func (obj *TypeName) String() string { return ObjectString(obj, nil) }
   689  func (obj *Var) String() string      { return ObjectString(obj, nil) }
   690  func (obj *Func) String() string     { return ObjectString(obj, nil) }
   691  func (obj *Label) String() string    { return ObjectString(obj, nil) }
   692  func (obj *Builtin) String() string  { return ObjectString(obj, nil) }
   693  func (obj *Nil) String() string      { return ObjectString(obj, nil) }
   694  
   695  func writeFuncName(buf *bytes.Buffer, f *Func, qf Qualifier) {
   696  	if f.typ != nil {
   697  		sig := f.typ.(*Signature)
   698  		if recv := sig.Recv(); recv != nil {
   699  			buf.WriteByte('(')
   700  			if _, ok := recv.Type().(*Interface); ok {
   701  				// gcimporter creates abstract methods of
   702  				// named interfaces using the interface type
   703  				// (not the named type) as the receiver.
   704  				// Don't print it in full.
   705  				buf.WriteString("interface")
   706  			} else {
   707  				WriteType(buf, recv.Type(), qf)
   708  			}
   709  			buf.WriteByte(')')
   710  			buf.WriteByte('.')
   711  		} else if f.pkg != nil {
   712  			buf.WriteString(packagePrefix(f.pkg, qf))
   713  		}
   714  	}
   715  	buf.WriteString(f.name)
   716  }
   717  

View as plain text