github.com/gocuntian/go@v0.0.0-20160610041250-fee02d270bf8/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 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 id (see Id below) 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 // TODO(gri): shouldn't !ast.IsExported(name) => pkg != nil be an precondition? 68 // if pkg == nil { 69 // panic("nil package in lookup of unexported name") 70 // } 71 if pkg != nil { 72 path = pkg.path 73 if path == "" { 74 path = "_" 75 } 76 } 77 return path + "." + name 78 } 79 80 // An object implements the common parts of an Object. 81 type object struct { 82 parent *Scope 83 pos token.Pos 84 pkg *Package 85 name string 86 typ Type 87 order_ uint32 88 scopePos_ token.Pos 89 } 90 91 func (obj *object) Parent() *Scope { return obj.parent } 92 func (obj *object) Pos() token.Pos { return obj.pos } 93 func (obj *object) Pkg() *Package { return obj.pkg } 94 func (obj *object) Name() string { return obj.name } 95 func (obj *object) Type() Type { return obj.typ } 96 func (obj *object) Exported() bool { return ast.IsExported(obj.name) } 97 func (obj *object) Id() string { return Id(obj.pkg, obj.name) } 98 func (obj *object) String() string { panic("abstract") } 99 func (obj *object) order() uint32 { return obj.order_ } 100 func (obj *object) scopePos() token.Pos { return obj.scopePos_ } 101 102 func (obj *object) setParent(parent *Scope) { obj.parent = parent } 103 func (obj *object) setOrder(order uint32) { assert(order > 0); obj.order_ = order } 104 func (obj *object) setScopePos(pos token.Pos) { obj.scopePos_ = pos } 105 106 func (obj *object) sameId(pkg *Package, name string) bool { 107 // spec: 108 // "Two identifiers are different if they are spelled differently, 109 // or if they appear in different packages and are not exported. 110 // Otherwise, they are the same." 111 if name != obj.name { 112 return false 113 } 114 // obj.Name == name 115 if obj.Exported() { 116 return true 117 } 118 // not exported, so packages must be the same (pkg == nil for 119 // fields in Universe scope; this can only happen for types 120 // introduced via Eval) 121 if pkg == nil || obj.pkg == nil { 122 return pkg == obj.pkg 123 } 124 // pkg != nil && obj.pkg != nil 125 return pkg.path == obj.pkg.path 126 } 127 128 // A PkgName represents an imported Go package. 129 type PkgName struct { 130 object 131 imported *Package 132 used bool // set if the package was used 133 } 134 135 func NewPkgName(pos token.Pos, pkg *Package, name string, imported *Package) *PkgName { 136 return &PkgName{object{nil, pos, pkg, name, Typ[Invalid], 0, token.NoPos}, imported, false} 137 } 138 139 // Imported returns the package that was imported. 140 // It is distinct from Pkg(), which is the package containing the import statement. 141 func (obj *PkgName) Imported() *Package { return obj.imported } 142 143 // A Const represents a declared constant. 144 type Const struct { 145 object 146 val constant.Value 147 visited bool // for initialization cycle detection 148 } 149 150 func NewConst(pos token.Pos, pkg *Package, name string, typ Type, val constant.Value) *Const { 151 return &Const{object{nil, pos, pkg, name, typ, 0, token.NoPos}, val, false} 152 } 153 154 func (obj *Const) Val() constant.Value { return obj.val } 155 156 // A TypeName represents a declared type. 157 type TypeName struct { 158 object 159 } 160 161 func NewTypeName(pos token.Pos, pkg *Package, name string, typ Type) *TypeName { 162 return &TypeName{object{nil, pos, pkg, name, typ, 0, token.NoPos}} 163 } 164 165 // A Variable represents a declared variable (including function parameters and results, and struct fields). 166 type Var struct { 167 object 168 anonymous bool // if set, the variable is an anonymous struct field, and name is the type name 169 visited bool // for initialization cycle detection 170 isField bool // var is struct field 171 used bool // set if the variable was used 172 } 173 174 func NewVar(pos token.Pos, pkg *Package, name string, typ Type) *Var { 175 return &Var{object: object{nil, pos, pkg, name, typ, 0, token.NoPos}} 176 } 177 178 func NewParam(pos token.Pos, pkg *Package, name string, typ Type) *Var { 179 return &Var{object: object{nil, pos, pkg, name, typ, 0, token.NoPos}, used: true} // parameters are always 'used' 180 } 181 182 func NewField(pos token.Pos, pkg *Package, name string, typ Type, anonymous bool) *Var { 183 return &Var{object: object{nil, pos, pkg, name, typ, 0, token.NoPos}, anonymous: anonymous, isField: true} 184 } 185 186 func (obj *Var) Anonymous() bool { return obj.anonymous } 187 188 func (obj *Var) IsField() bool { return obj.isField } 189 190 // A Func represents a declared function, concrete method, or abstract 191 // (interface) method. Its Type() is always a *Signature. 192 // An abstract method may belong to many interfaces due to embedding. 193 type Func struct { 194 object 195 } 196 197 func NewFunc(pos token.Pos, pkg *Package, name string, sig *Signature) *Func { 198 // don't store a nil signature 199 var typ Type 200 if sig != nil { 201 typ = sig 202 } 203 return &Func{object{nil, pos, pkg, name, typ, 0, token.NoPos}} 204 } 205 206 // FullName returns the package- or receiver-type-qualified name of 207 // function or method obj. 208 func (obj *Func) FullName() string { 209 var buf bytes.Buffer 210 writeFuncName(&buf, obj, nil) 211 return buf.String() 212 } 213 214 func (obj *Func) Scope() *Scope { 215 return obj.typ.(*Signature).scope 216 } 217 218 // A Label represents a declared label. 219 type Label struct { 220 object 221 used bool // set if the label was used 222 } 223 224 func NewLabel(pos token.Pos, pkg *Package, name string) *Label { 225 return &Label{object{pos: pos, pkg: pkg, name: name, typ: Typ[Invalid]}, false} 226 } 227 228 // A Builtin represents a built-in function. 229 // Builtins don't have a valid type. 230 type Builtin struct { 231 object 232 id builtinId 233 } 234 235 func newBuiltin(id builtinId) *Builtin { 236 return &Builtin{object{name: predeclaredFuncs[id].name, typ: Typ[Invalid]}, id} 237 } 238 239 // Nil represents the predeclared value nil. 240 type Nil struct { 241 object 242 } 243 244 func writeObject(buf *bytes.Buffer, obj Object, qf Qualifier) { 245 typ := obj.Type() 246 switch obj := obj.(type) { 247 case *PkgName: 248 fmt.Fprintf(buf, "package %s", obj.Name()) 249 if path := obj.imported.path; path != "" && path != obj.name { 250 fmt.Fprintf(buf, " (%q)", path) 251 } 252 return 253 254 case *Const: 255 buf.WriteString("const") 256 257 case *TypeName: 258 buf.WriteString("type") 259 typ = typ.Underlying() 260 261 case *Var: 262 if obj.isField { 263 buf.WriteString("field") 264 } else { 265 buf.WriteString("var") 266 } 267 268 case *Func: 269 buf.WriteString("func ") 270 writeFuncName(buf, obj, qf) 271 if typ != nil { 272 WriteSignature(buf, typ.(*Signature), qf) 273 } 274 return 275 276 case *Label: 277 buf.WriteString("label") 278 typ = nil 279 280 case *Builtin: 281 buf.WriteString("builtin") 282 typ = nil 283 284 case *Nil: 285 buf.WriteString("nil") 286 return 287 288 default: 289 panic(fmt.Sprintf("writeObject(%T)", obj)) 290 } 291 292 buf.WriteByte(' ') 293 294 // For package-level objects, qualify the name. 295 if obj.Pkg() != nil && obj.Pkg().scope.Lookup(obj.Name()) == obj { 296 writePackage(buf, obj.Pkg(), qf) 297 } 298 buf.WriteString(obj.Name()) 299 if typ != nil { 300 buf.WriteByte(' ') 301 WriteType(buf, typ, qf) 302 } 303 } 304 305 func writePackage(buf *bytes.Buffer, pkg *Package, qf Qualifier) { 306 if pkg == nil { 307 return 308 } 309 var s string 310 if qf != nil { 311 s = qf(pkg) 312 } else { 313 s = pkg.Path() 314 } 315 if s != "" { 316 buf.WriteString(s) 317 buf.WriteByte('.') 318 } 319 } 320 321 // ObjectString returns the string form of obj. 322 // The Qualifier controls the printing of 323 // package-level objects, and may be nil. 324 func ObjectString(obj Object, qf Qualifier) string { 325 var buf bytes.Buffer 326 writeObject(&buf, obj, qf) 327 return buf.String() 328 } 329 330 func (obj *PkgName) String() string { return ObjectString(obj, nil) } 331 func (obj *Const) String() string { return ObjectString(obj, nil) } 332 func (obj *TypeName) String() string { return ObjectString(obj, nil) } 333 func (obj *Var) String() string { return ObjectString(obj, nil) } 334 func (obj *Func) String() string { return ObjectString(obj, nil) } 335 func (obj *Label) String() string { return ObjectString(obj, nil) } 336 func (obj *Builtin) String() string { return ObjectString(obj, nil) } 337 func (obj *Nil) String() string { return ObjectString(obj, nil) } 338 339 func writeFuncName(buf *bytes.Buffer, f *Func, qf Qualifier) { 340 if f.typ != nil { 341 sig := f.typ.(*Signature) 342 if recv := sig.Recv(); recv != nil { 343 buf.WriteByte('(') 344 if _, ok := recv.Type().(*Interface); ok { 345 // gcimporter creates abstract methods of 346 // named interfaces using the interface type 347 // (not the named type) as the receiver. 348 // Don't print it in full. 349 buf.WriteString("interface") 350 } else { 351 WriteType(buf, recv.Type(), qf) 352 } 353 buf.WriteByte(')') 354 buf.WriteByte('.') 355 } else if f.pkg != nil { 356 writePackage(buf, f.pkg, qf) 357 } 358 } 359 buf.WriteString(f.name) 360 }