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