// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT.// Source: ../../cmd/compile/internal/types2/object.go// 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 typesimport ()// An Object is a named language entity.// An Object may be a constant ([Const]), type name ([TypeName]),// variable or struct field ([Var]), function or method ([Func]),// imported package ([PkgName]), label ([Label]),// built-in function ([Builtin]),// or the predeclared identifier 'nil' ([Nil]).//// The environment, which is structured as a tree of Scopes,// maps each name to the unique Object that it denotes.typeObjectinterface {Parent() *Scope// scope in which this object is declared; nil for methods and struct fieldsPos() token.Pos// position of object identifier in declarationPkg() *Package// package to which this object belongs; nil for labels and objects in the Universe scopeName() string// package local object nameType() Type// object typeExported() bool// reports whether the name starts with a capital letterId() string// object name if exported, qualified name if not exported (see func Id)// String returns a human-readable string of the object. // Use [ObjectString] to control how package names are formatted in the string.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// setType sets the type of the object. setType(Type)// setOrder sets the order number of the object. It must be > 0. setOrder(uint32)// setParent sets the parent scope of the object. setParent(*Scope)// sameId reports whether obj.Id() and Id(pkg, name) are the same. // If foldCase is true, names are considered equal if they are equal with case folding // and their packages are ignored (e.g., pkg1.m, pkg1.M, pkg2.m, and pkg2.M are all equal). sameId(pkg *Package, name string, foldCase bool) 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)}func isExported( string) bool { , := utf8.DecodeRuneInString()returnunicode.IsUpper()}// Id returns name if it is exported, otherwise it// returns the name qualified with the package path.func ( *Package, string) string {ifisExported() {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 scopePos_ token.Pos}// 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 { returnisExported(.name) }// Id is a wrapper for Id(obj.Pkg(), obj.Name()).func ( *object) () string { returnId(.pkg, .name) }func ( *object) () string { panic("abstract") }func ( *object) () uint32 { return .order_ }func ( *object) () token.Pos { return .scopePos_ }func ( *object) ( *Scope) { .parent = }func ( *object) ( Type) { .typ = }func ( *object) ( uint32) { assert( > 0); .order_ = }func ( *object) ( token.Pos) { .scopePos_ = }func ( *object) ( *Package, string, bool) bool {// If we don't care about capitalization, we also ignore packages.if && strings.EqualFold(.name, ) {returntrue }// 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 != {returnfalse }// obj.Name == nameif .Exported() {returntrue }// not exported, so packages must be the samereturnsamePkg(.pkg, )}// cmp reports whether object a is ordered before object b.// cmp returns://// -1 if a is before b// 0 if a is equivalent to b// +1 if a is behind b//// Objects are ordered nil before non-nil, exported before// non-exported, then by name, and finally (for non-exported// functions) by package path.func ( *object) ( *object) int {if == {return0 }// Nil before non-nil.if == nil {return -1 }if == nil {return +1 }// Exported functions before non-exported. := isExported(.name) := isExported(.name)if != {if {return -1 }return +1 }// Order by name and then (for non-exported names) by package.if .name != .name {returnstrings.Compare(.name, .name) }if ! {returnstrings.Compare(.pkg.path, .pkg.path) }return0}// A PkgName represents an imported Go package.// PkgNames don't have a type.typePkgNamestruct {object imported *Package}// 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, nopos}, }}// 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.typeConststruct {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, 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 is an [Object] that represents a type with a name:// a defined type ([Named]),// an alias type ([Alias]),// a type parameter ([TypeParam]),// or a predeclared type such as int or error.typeTypeNamestruct {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, nopos}}}// NewTypeNameLazy returns a new defined type like NewTypeName, but it// lazily calls unpack to finish constructing the Named object.func _NewTypeNameLazy( token.Pos, *Package, string, func(*Named) ([]*TypeParam, Type, []*Func, []func())) *TypeName { := NewTypeName(, , , nil) := (*Checker)(nil).newNamed(, nil, nil) .loader = return}// IsAlias reports whether obj is an alias name for a type.func ( *TypeName) () bool {switch t := .typ.(type) {casenil:returnfalse// case *Alias: // handled by default casecase *Basic:// unsafe.Pointer is not an alias.if .pkg == Unsafe {returnfalse }// 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 || == universeRunecase *Named:return != .objcase *TypeParam:return != .objdefault:returntrue }}// A Var represents a declared variable (including function parameters and results, and struct fields).typeVarstruct {object origin *Var// if non-nil, the Var from which this one was instantiated kind VarKind embedded bool// if set, the variable is an embedded struct field, and name is the type name}// A VarKind discriminates the various kinds of variables.typeVarKinduint8const ( _ VarKind = iota// (not meaningful)PackageVar// a package-level variableLocalVar// a local variableRecvVar// a method receiver variableParamVar// a function parameter variableResultVar// a function result variableFieldVar// a struct field)var varKindNames = [...]string{0: "VarKind(0)",PackageVar: "PackageVar",LocalVar: "LocalVar",RecvVar: "RecvVar",ParamVar: "ParamVar",ResultVar: "ResultVar",FieldVar: "FieldVar",}func ( VarKind) () string {if0 <= && int() < len(varKindNames) {returnvarKindNames[] }returnfmt.Sprintf("VarKind(%d)", )}// Kind reports what kind of variable v is.func ( *Var) () VarKind { return .kind }// SetKind sets the kind of the variable.// It should be used only immediately after [NewVar] or [NewParam].func ( *Var) ( VarKind) { .kind = }// NewVar returns a new variable.// The arguments set the attributes found with all Objects.//// The caller must subsequently call [Var.SetKind]// if the desired Var is not of kind [PackageVar].func ( token.Pos, *Package, string, Type) *Var {returnnewVar(PackageVar, , , , )}// NewParam returns a new variable representing a function parameter.//// The caller must subsequently call [Var.SetKind] if the desired Var// is not of kind [ParamVar]: for example, [RecvVar] or [ResultVar].func ( token.Pos, *Package, string, Type) *Var {returnnewVar(ParamVar, , , , )}// 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 { := newVar(FieldVar, , , , ) .embedded = return}// newVar returns a new variable.// The arguments set the attributes found with all Objects.func newVar( VarKind, token.Pos, *Package, string, Type) *Var {return &Var{object: object{nil, , , , , 0, nopos}, kind: }}// 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 .kind == FieldVar }// Origin returns the canonical Var for its receiver, i.e. the Var object// recorded in Info.Defs.//// For synthetic Vars created during instantiation (such as struct fields or// function parameters that depend on type arguments), this will be the// corresponding Var on the generic (uninstantiated) type. For all other Vars// Origin returns the receiver.func ( *Var) () *Var {if .origin != nil {return .origin }return}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.typeFuncstruct {object hasPtrRecv_ bool// only valid for methods that don't have a type yet; use hasPtrRecv() to read origin *Func// if non-nil, the Func from which this one was instantiated}// NewFunc returns a new function with the given signature, representing// the function's type.func ( token.Pos, *Package, string, *Signature) *Func {varTypeif != nil { = } else {// Don't store a (typed) nil *Signature. // We can't simply replace it with new(Signature) either, // as this would violate object.{Type,color} invariants. // TODO(adonovan): propose to disallow NewFunc with nil *Signature. }return &Func{object{nil, , , , , 0, nopos}, false, nil}}// Signature returns the signature (type) of the function or method.func ( *Func) () *Signature {if .typ != nil {return .typ.(*Signature) // normal case }// No signature: Signature was called either: // - within go/types, before a FuncDecl's initially // nil Func.Type was lazily populated, indicating // a types bug; or // - by a client after NewFunc(..., nil), // which is arguably a client bug, but we need a // proposal to tighten NewFunc's precondition. // For now, return a trivial signature.returnnew(Signature)}// FullName returns the package- or receiver-type-qualified name of// function or method obj.func ( *Func) () string {varbytes.BufferwriteFuncName(&, , nil)return .String()}// Scope returns the scope of the function's body block.// The result is nil for imported or instantiated functions and methods// (but there is also no mechanism to get to an instantiated function).func ( *Func) () *Scope { return .typ.(*Signature).scope }// Origin returns the canonical Func for its receiver, i.e. the Func object// recorded in Info.Defs.//// For synthetic functions created during instantiation (such as methods on an// instantiated Named type or interface methods that depend on type arguments),// this will be the corresponding Func on the generic (uninstantiated) type.// For all other Funcs Origin returns the receiver.func ( *Func) () *Func {if .origin != nil {return .origin }return}// Pkg returns the package to which the function belongs.//// The result is nil for methods of types in the Universe scope,// like method Error of the error built-in interface type.func ( *Func) () *Package { return .object.Pkg() }// hasPtrRecv reports whether the receiver is of the form *T for the given method obj.func ( *Func) () bool {// If a method's receiver type is set, use that as the source of truth for the receiver. // Caution: Checker.funcDecl (decl.go) marks a function by setting its type to an empty // signature. We may reach here before the signature is fully set up: we must explicitly // check if the receiver is set (we cannot just look for non-nil obj.typ).if , := .typ.(*Signature); != nil && .recv != nil { , := deref(.recv.typ)return }// If a method's type is not set it may be a method/function that is: // 1) client-supplied (via NewFunc with no signature), or // 2) internally created but not yet type-checked. // For case 1) we can't do anything; the client must know what they are doing. // For case 2) we can use the information gathered by the resolver.return .hasPtrRecv_}func (*Func) () {} // a function may be a dependency of an initialization expression// A Label represents a declared label.// Labels don't have a type.typeLabelstruct {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]}, false}}// A Builtin represents a built-in function.// Builtins don't have a valid type.typeBuiltinstruct {object id builtinId}func newBuiltin( builtinId) *Builtin {return &Builtin{object{name: predeclaredFuncs[].name, typ: Typ[Invalid]}, }}// Nil represents the predeclared value nil.typeNilstruct {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)", ) }returncase *Const: .WriteString("const")case *TypeName: = .WriteString("type")ifisTypeParam() { .WriteString(" parameter") }case *Var:if .IsField() { .WriteString("field") } else { .WriteString("var") }case *Func: .WriteString("func ")writeFuncName(, , )if != nil {WriteSignature(, .(*Signature), ) }returncase *Label: .WriteString("label") = nilcase *Builtin: .WriteString("builtin") = nilcase *Nil: .WriteString("nil")returndefault:panic(fmt.Sprintf("writeObject(%T)", )) } .WriteByte(' ')// For package-level objects, qualify the name.if .Pkg() != nil && .Pkg().scope.Lookup(.Name()) == { .WriteString(packagePrefix(.Pkg(), )) } .WriteString(.Name())if == nil {return }if != nil {switch t := .(type) {case *Basic:// Don't print anything more for basic types since there's // no more information.returncasegenericType:if .TypeParams().Len() > 0 {newTypeWriter(, ).tParamList(.TypeParams().list()) } }if .IsAlias() { .WriteString(" =")if , := .(*Alias); { // materialized? (gotypesalias=1) = .fromRHS } } elseif , := .(*TypeParam); != nil { = .bound } else {// TODO(gri) should this be fromRHS for *Named? // (See discussion in #66559.) = .Underlying() } }// Special handling for any: because WriteType will format 'any' as 'any', // resulting in the object string `type any = any` rather than `type any = // interface{}`. To avoid this, swap in a different empty interface.if .Name() == "any" && .Parent() == Universe {assert(Identical(, &emptyInterface)) = &emptyInterface } .WriteByte(' ')WriteType(, , )}func packagePrefix( *Package, Qualifier) string {if == nil {return"" }varstringif != nil { = () } else { = .Path() }if != "" { += "." }return}// ObjectString returns the string form of obj.// The Qualifier controls the printing of// package-level objects, and may be nil.func ( Object, Qualifier) string {varbytes.BufferwriteObject(&, , )return .String()}func ( *PkgName) () string { returnObjectString(, nil) }func ( *Const) () string { returnObjectString(, nil) }func ( *TypeName) () string { returnObjectString(, nil) }func ( *Var) () string { returnObjectString(, nil) }func ( *Func) () string { returnObjectString(, nil) }func ( *Label) () string { returnObjectString(, nil) }func ( *Builtin) () string { returnObjectString(, nil) }func ( *Nil) () string { returnObjectString(, 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('.') } elseif .pkg != nil { .WriteString(packagePrefix(.pkg, )) } } .WriteString(.name)}// objectKind returns a description of the object's kind.func objectKind( Object) string {switch obj := .(type) {case *PkgName:return"package name"case *Const:return"constant"case *TypeName:if .IsAlias() {return"type alias" } elseif , := .Type().(*TypeParam); {return"type parameter" } else {return"defined type" }case *Var:switch .Kind() {casePackageVar:return"package-level variable"caseLocalVar:return"local variable"caseRecvVar:return"receiver"caseParamVar:return"parameter"caseResultVar:return"result variable"caseFieldVar:return"struct field" }case *Func:if .Signature().Recv() != nil {return"method" } else {return"function" }case *Label:return"label"case *Builtin:return"built-in function"case *Nil:return"untyped nil" }ifdebug {panic(fmt.Sprintf("unknown symbol (%T)", )) }return"unknown symbol"}
The pages are generated with Goldsv0.8.4. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.