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

View as plain text