github.com/riscv/riscv-go@v0.0.0-20200123204226-124ebd6fcc8e/src/go/types/object.go (about) 1 // Copyright 2013 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package types 6 7 import ( 8 "bytes" 9 "fmt" 10 "go/ast" 11 "go/constant" 12 "go/token" 13 ) 14 15 // TODO(gri) Document factory, accessor methods, and fields. General clean-up. 16 17 // An Object describes a named language entity such as a package, 18 // constant, type, variable, function (incl. methods), or label. 19 // All objects implement the Object interface. 20 // 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 // nil for objects in the Universe scope and labels 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 // setOrder sets the order number of the object. It must be > 0. 40 setOrder(uint32) 41 42 // setParent sets the parent scope of the object. 43 setParent(*Scope) 44 45 // sameId reports whether obj.Id() and Id(pkg, name) are the same. 46 sameId(pkg *Package, name string) bool 47 48 // scopePos returns the start position of the scope of this Object 49 scopePos() token.Pos 50 51 // setScopePos sets the start position of the scope for this Object. 52 setScopePos(pos token.Pos) 53 } 54 55 // Id returns name if it is exported, otherwise it 56 // returns the name qualified with the package path. 57 func Id(pkg *Package, name string) string { 58 if ast.IsExported(name) { 59 return name 60 } 61 // unexported names need the package path for differentiation 62 // (if there's no package, make sure we don't start with '.' 63 // as that may change the order of methods between a setup 64 // inside a package and outside a package - which breaks some 65 // tests) 66 path := "_" 67 // pkg is nil for objects in Universe scope and possibly types 68 // introduced via Eval (see also comment in object.sameId) 69 if pkg != nil && pkg.path != "" { 70 path = pkg.path 71 } 72 return path + "." + name 73 } 74 75 // An object implements the common parts of an Object. 76 type object struct { 77 parent *Scope 78 pos token.Pos 79 pkg *Package 80 name string 81 typ Type 82 order_ uint32 83 scopePos_ token.Pos 84 } 85 86 func (obj *object) Parent() *Scope { return obj.parent } 87 func (obj *object) Pos() token.Pos { return obj.pos } 88 func (obj *object) Pkg() *Package { return obj.pkg } 89 func (obj *object) Name() string { return obj.name } 90 func (obj *object) Type() Type { return obj.typ } 91 func (obj *object) Exported() bool { return ast.IsExported(obj.name) } 92 func (obj *object) Id() string { return Id(obj.pkg, obj.name) } 93 func (obj *object) String() string { panic("abstract") } 94 func (obj *object) order() uint32 { return obj.order_ } 95 func (obj *object) scopePos() token.Pos { return obj.scopePos_ } 96 97 func (obj *object) setParent(parent *Scope) { obj.parent = parent } 98 func (obj *object) setOrder(order uint32) { assert(order > 0); obj.order_ = order } 99 func (obj *object) setScopePos(pos token.Pos) { obj.scopePos_ = pos } 100 101 func (obj *object) sameId(pkg *Package, name string) bool { 102 // spec: 103 // "Two identifiers are different if they are spelled differently, 104 // or if they appear in different packages and are not exported. 105 // Otherwise, they are the same." 106 if name != obj.name { 107 return false 108 } 109 // obj.Name == name 110 if obj.Exported() { 111 return true 112 } 113 // not exported, so packages must be the same (pkg == nil for 114 // fields in Universe scope; this can only happen for types 115 // introduced via Eval) 116 if pkg == nil || obj.pkg == nil { 117 return pkg == obj.pkg 118 } 119 // pkg != nil && obj.pkg != nil 120 return pkg.path == obj.pkg.path 121 } 122 123 // A PkgName represents an imported Go package. 124 type PkgName struct { 125 object 126 imported *Package 127 used bool // set if the package was used 128 } 129 130 func NewPkgName(pos token.Pos, pkg *Package, name string, imported *Package) *PkgName { 131 return &PkgName{object{nil, pos, pkg, name, Typ[Invalid], 0, token.NoPos}, imported, false} 132 } 133 134 // Imported returns the package that was imported. 135 // It is distinct from Pkg(), which is the package containing the import statement. 136 func (obj *PkgName) Imported() *Package { return obj.imported } 137 138 // A Const represents a declared constant. 139 type Const struct { 140 object 141 val constant.Value 142 visited bool // for initialization cycle detection 143 } 144 145 func NewConst(pos token.Pos, pkg *Package, name string, typ Type, val constant.Value) *Const { 146 return &Const{object{nil, pos, pkg, name, typ, 0, token.NoPos}, val, false} 147 } 148 149 func (obj *Const) Val() constant.Value { return obj.val } 150 func (*Const) isDependency() {} // a constant may be a dependency of an initialization expression 151 152 // A TypeName represents a name for a (named or alias) type. 153 type TypeName struct { 154 object 155 } 156 157 func NewTypeName(pos token.Pos, pkg *Package, name string, typ Type) *TypeName { 158 return &TypeName{object{nil, pos, pkg, name, typ, 0, token.NoPos}} 159 } 160 161 // IsAlias reports whether obj is an alias name for a type. 162 func (obj *TypeName) IsAlias() bool { 163 switch t := obj.typ.(type) { 164 case nil: 165 return false 166 case *Basic: 167 // Any user-defined type name for a basic type is an alias for a 168 // basic type (because basic types are pre-declared in the Universe 169 // scope, outside any package scope), and so is any type name with 170 // a different name than the name of the basic type it refers to. 171 // Additionaly, we need to look for "byte" and "rune" because they 172 // are aliases but have the same names (for better error messages). 173 return obj.pkg != nil || t.name != obj.name || t == universeByte || t == universeRune 174 case *Named: 175 return obj != t.obj 176 default: 177 return true 178 } 179 } 180 181 // A Variable represents a declared variable (including function parameters and results, and struct fields). 182 type Var struct { 183 object 184 anonymous bool // if set, the variable is an anonymous struct field, and name is the type name 185 visited bool // for initialization cycle detection 186 isField bool // var is struct field 187 used bool // set if the variable was used 188 } 189 190 func NewVar(pos token.Pos, pkg *Package, name string, typ Type) *Var { 191 return &Var{object: object{nil, pos, pkg, name, typ, 0, token.NoPos}} 192 } 193 194 func NewParam(pos token.Pos, pkg *Package, name string, typ Type) *Var { 195 return &Var{object: object{nil, pos, pkg, name, typ, 0, token.NoPos}, used: true} // parameters are always 'used' 196 } 197 198 func NewField(pos token.Pos, pkg *Package, name string, typ Type, anonymous bool) *Var { 199 return &Var{object: object{nil, pos, pkg, name, typ, 0, token.NoPos}, anonymous: anonymous, isField: true} 200 } 201 202 func (obj *Var) Anonymous() bool { return obj.anonymous } 203 func (obj *Var) IsField() bool { return obj.isField } 204 func (*Var) isDependency() {} // a variable may be a dependency of an initialization expression 205 206 // A Func represents a declared function, concrete method, or abstract 207 // (interface) method. Its Type() is always a *Signature. 208 // An abstract method may belong to many interfaces due to embedding. 209 type Func struct { 210 object 211 } 212 213 func NewFunc(pos token.Pos, pkg *Package, name string, sig *Signature) *Func { 214 // don't store a nil signature 215 var typ Type 216 if sig != nil { 217 typ = sig 218 } 219 return &Func{object{nil, pos, pkg, name, typ, 0, token.NoPos}} 220 } 221 222 // FullName returns the package- or receiver-type-qualified name of 223 // function or method obj. 224 func (obj *Func) FullName() string { 225 var buf bytes.Buffer 226 writeFuncName(&buf, obj, nil) 227 return buf.String() 228 } 229 230 func (obj *Func) Scope() *Scope { return obj.typ.(*Signature).scope } 231 func (*Func) isDependency() {} // a function may be a dependency of an initialization expression 232 233 // A Label represents a declared label. 234 type Label struct { 235 object 236 used bool // set if the label was used 237 } 238 239 func NewLabel(pos token.Pos, pkg *Package, name string) *Label { 240 return &Label{object{pos: pos, pkg: pkg, name: name, typ: Typ[Invalid]}, false} 241 } 242 243 // A Builtin represents a built-in function. 244 // Builtins don't have a valid type. 245 type Builtin struct { 246 object 247 id builtinId 248 } 249 250 func newBuiltin(id builtinId) *Builtin { 251 return &Builtin{object{name: predeclaredFuncs[id].name, typ: Typ[Invalid]}, id} 252 } 253 254 // Nil represents the predeclared value nil. 255 type Nil struct { 256 object 257 } 258 259 func writeObject(buf *bytes.Buffer, obj Object, qf Qualifier) { 260 var tname *TypeName 261 typ := obj.Type() 262 263 switch obj := obj.(type) { 264 case *PkgName: 265 fmt.Fprintf(buf, "package %s", obj.Name()) 266 if path := obj.imported.path; path != "" && path != obj.name { 267 fmt.Fprintf(buf, " (%q)", path) 268 } 269 return 270 271 case *Const: 272 buf.WriteString("const") 273 274 case *TypeName: 275 tname = obj 276 buf.WriteString("type") 277 278 case *Var: 279 if obj.isField { 280 buf.WriteString("field") 281 } else { 282 buf.WriteString("var") 283 } 284 285 case *Func: 286 buf.WriteString("func ") 287 writeFuncName(buf, obj, qf) 288 if typ != nil { 289 WriteSignature(buf, typ.(*Signature), qf) 290 } 291 return 292 293 case *Label: 294 buf.WriteString("label") 295 typ = nil 296 297 case *Builtin: 298 buf.WriteString("builtin") 299 typ = nil 300 301 case *Nil: 302 buf.WriteString("nil") 303 return 304 305 default: 306 panic(fmt.Sprintf("writeObject(%T)", obj)) 307 } 308 309 buf.WriteByte(' ') 310 311 // For package-level objects, qualify the name. 312 if obj.Pkg() != nil && obj.Pkg().scope.Lookup(obj.Name()) == obj { 313 writePackage(buf, obj.Pkg(), qf) 314 } 315 buf.WriteString(obj.Name()) 316 317 if typ == nil { 318 return 319 } 320 321 if tname != nil { 322 // We have a type object: Don't print anything more for 323 // basic types since there's no more information (names 324 // are the same; see also comment in TypeName.IsAlias). 325 if _, ok := typ.(*Basic); ok { 326 return 327 } 328 if tname.IsAlias() { 329 buf.WriteString(" =") 330 } else { 331 typ = typ.Underlying() 332 } 333 } 334 335 buf.WriteByte(' ') 336 WriteType(buf, typ, qf) 337 } 338 339 func writePackage(buf *bytes.Buffer, pkg *Package, qf Qualifier) { 340 if pkg == nil { 341 return 342 } 343 var s string 344 if qf != nil { 345 s = qf(pkg) 346 } else { 347 s = pkg.Path() 348 } 349 if s != "" { 350 buf.WriteString(s) 351 buf.WriteByte('.') 352 } 353 } 354 355 // ObjectString returns the string form of obj. 356 // The Qualifier controls the printing of 357 // package-level objects, and may be nil. 358 func ObjectString(obj Object, qf Qualifier) string { 359 var buf bytes.Buffer 360 writeObject(&buf, obj, qf) 361 return buf.String() 362 } 363 364 func (obj *PkgName) String() string { return ObjectString(obj, nil) } 365 func (obj *Const) String() string { return ObjectString(obj, nil) } 366 func (obj *TypeName) String() string { return ObjectString(obj, nil) } 367 func (obj *Var) String() string { return ObjectString(obj, nil) } 368 func (obj *Func) String() string { return ObjectString(obj, nil) } 369 func (obj *Label) String() string { return ObjectString(obj, nil) } 370 func (obj *Builtin) String() string { return ObjectString(obj, nil) } 371 func (obj *Nil) String() string { return ObjectString(obj, nil) } 372 373 func writeFuncName(buf *bytes.Buffer, f *Func, qf Qualifier) { 374 if f.typ != nil { 375 sig := f.typ.(*Signature) 376 if recv := sig.Recv(); recv != nil { 377 buf.WriteByte('(') 378 if _, ok := recv.Type().(*Interface); ok { 379 // gcimporter creates abstract methods of 380 // named interfaces using the interface type 381 // (not the named type) as the receiver. 382 // Don't print it in full. 383 buf.WriteString("interface") 384 } else { 385 WriteType(buf, recv.Type(), qf) 386 } 387 buf.WriteByte(')') 388 buf.WriteByte('.') 389 } else if f.pkg != nil { 390 writePackage(buf, f.pkg, qf) 391 } 392 } 393 buf.WriteString(f.name) 394 }