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.
9 "cmd/compile/internal/syntax"
16 // An Object describes a named language entity such as a package,
17 // constant, type, variable, function (incl. methods), or label.
18 // All objects implement the Object interface.
20 type Object interface {
21 Parent() *Scope // scope in which this object is declared; nil for methods and struct fields
22 Pos() syntax.Pos // position of object identifier in declaration
23 Pkg() *Package // package to which this object belongs; nil for labels and objects in the Universe scope
24 Name() string // package local object name
25 Type() Type // object type
26 Exported() bool // reports whether the name starts with a capital letter
27 Id() string // object name if exported, qualified name if not exported (see func Id)
29 // String returns a human-readable string of the object.
32 // order reflects a package-level object's source order: if object
33 // a is before object b in the source, then a.order() < b.order().
34 // order returns a value > 0 for package-level objects; it returns
35 // 0 for all other objects (including objects in file scopes).
38 // color returns the object's color.
41 // setType sets the type of the object.
44 // setOrder sets the order number of the object. It must be > 0.
47 // setColor sets the object's color. It must not be white.
50 // setParent sets the parent scope of the object.
53 // sameId reports whether obj.Id() and Id(pkg, name) are the same.
54 sameId(pkg *Package, name string) bool
56 // scopePos returns the start position of the scope of this Object
59 // setScopePos sets the start position of the scope for this Object.
60 setScopePos(pos syntax.Pos)
63 func isExported(name string) bool {
64 ch, _ := utf8.DecodeRuneInString(name)
65 return unicode.IsUpper(ch)
68 // Id returns name if it is exported, otherwise it
69 // returns the name qualified with the package path.
70 func Id(pkg *Package, name string) string {
74 // unexported names need the package path for differentiation
75 // (if there's no package, make sure we don't start with '.'
76 // as that may change the order of methods between a setup
77 // inside a package and outside a package - which breaks some
80 // pkg is nil for objects in Universe scope and possibly types
81 // introduced via Eval (see also comment in object.sameId)
82 if pkg != nil && pkg.path != "" {
85 return path + "." + name
88 // An object implements the common parts of an Object.
100 // color encodes the color of an object (see Checker.objDecl for details).
103 // An object may be painted in one of three colors.
104 // Color values other than white or black are considered grey.
108 grey // must be > white and black
111 func (c color) String() string {
122 // colorFor returns the (initial) color for an object depending on
123 // whether its type t is known or not.
124 func colorFor(t Type) color {
131 // Parent returns the scope in which the object is declared.
132 // The result is nil for methods and struct fields.
133 func (obj *object) Parent() *Scope { return obj.parent }
135 // Pos returns the declaration position of the object's identifier.
136 func (obj *object) Pos() syntax.Pos { return obj.pos }
138 // Pkg returns the package to which the object belongs.
139 // The result is nil for labels and objects in the Universe scope.
140 func (obj *object) Pkg() *Package { return obj.pkg }
142 // Name returns the object's (package-local, unqualified) name.
143 func (obj *object) Name() string { return obj.name }
145 // Type returns the object's type.
146 func (obj *object) Type() Type { return obj.typ }
148 // Exported reports whether the object is exported (starts with a capital letter).
149 // It doesn't take into account whether the object is in a local (function) scope
151 func (obj *object) Exported() bool { return isExported(obj.name) }
153 // Id is a wrapper for Id(obj.Pkg(), obj.Name()).
154 func (obj *object) Id() string { return Id(obj.pkg, obj.name) }
156 func (obj *object) String() string { panic("abstract") }
157 func (obj *object) order() uint32 { return obj.order_ }
158 func (obj *object) color() color { return obj.color_ }
159 func (obj *object) scopePos() syntax.Pos { return obj.scopePos_ }
161 func (obj *object) setParent(parent *Scope) { obj.parent = parent }
162 func (obj *object) setType(typ Type) { obj.typ = typ }
163 func (obj *object) setOrder(order uint32) { assert(order > 0); obj.order_ = order }
164 func (obj *object) setColor(color color) { assert(color != white); obj.color_ = color }
165 func (obj *object) setScopePos(pos syntax.Pos) { obj.scopePos_ = pos }
167 func (obj *object) sameId(pkg *Package, name string) bool {
169 // "Two identifiers are different if they are spelled differently,
170 // or if they appear in different packages and are not exported.
171 // Otherwise, they are the same."
172 if name != obj.name {
179 // not exported, so packages must be the same (pkg == nil for
180 // fields in Universe scope; this can only happen for types
181 // introduced via Eval)
182 if pkg == nil || obj.pkg == nil {
183 return pkg == obj.pkg
185 // pkg != nil && obj.pkg != nil
186 return pkg.path == obj.pkg.path
189 // less reports whether object a is ordered before object b.
191 // Objects are ordered nil before non-nil, exported before
192 // non-exported, then by name, and finally (for non-exported
193 // functions) by package height and path.
194 func (a *object) less(b *object) bool {
199 // Nil before non-nil.
207 // Exported functions before non-exported.
208 ea := isExported(a.name)
209 eb := isExported(b.name)
214 // Order by name and then (for non-exported names) by package.
215 if a.name != b.name {
216 return a.name < b.name
219 if a.pkg.height != b.pkg.height {
220 return a.pkg.height < b.pkg.height
222 return a.pkg.path < b.pkg.path
228 // A PkgName represents an imported Go package.
229 // PkgNames don't have a type.
230 type PkgName struct {
233 used bool // set if the package was used
236 // NewPkgName returns a new PkgName object representing an imported package.
237 // The remaining arguments set the attributes found with all Objects.
238 func NewPkgName(pos syntax.Pos, pkg *Package, name string, imported *Package) *PkgName {
239 return &PkgName{object{nil, pos, pkg, name, Typ[Invalid], 0, black, nopos}, imported, false}
242 // Imported returns the package that was imported.
243 // It is distinct from Pkg(), which is the package containing the import statement.
244 func (obj *PkgName) Imported() *Package { return obj.imported }
246 // A Const represents a declared constant.
252 // NewConst returns a new constant with value val.
253 // The remaining arguments set the attributes found with all Objects.
254 func NewConst(pos syntax.Pos, pkg *Package, name string, typ Type, val constant.Value) *Const {
255 return &Const{object{nil, pos, pkg, name, typ, 0, colorFor(typ), nopos}, val}
258 // Val returns the constant's value.
259 func (obj *Const) Val() constant.Value { return obj.val }
261 func (*Const) isDependency() {} // a constant may be a dependency of an initialization expression
263 // A TypeName represents a name for a (defined or alias) type.
264 type TypeName struct {
268 // NewTypeName returns a new type name denoting the given typ.
269 // The remaining arguments set the attributes found with all Objects.
271 // The typ argument may be a defined (Named) type or an alias type.
272 // It may also be nil such that the returned TypeName can be used as
273 // argument for NewNamed, which will set the TypeName's type as a side-
275 func NewTypeName(pos syntax.Pos, pkg *Package, name string, typ Type) *TypeName {
276 return &TypeName{object{nil, pos, pkg, name, typ, 0, colorFor(typ), nopos}}
279 // NewTypeNameLazy returns a new defined type like NewTypeName, but it
280 // lazily calls resolve to finish constructing the Named object.
281 func NewTypeNameLazy(pos syntax.Pos, pkg *Package, name string, resolve func(named *Named) (tparams []*TypeName, underlying Type, methods []*Func)) *TypeName {
282 obj := NewTypeName(pos, pkg, name, nil)
283 NewNamed(obj, nil, nil).resolve = resolve
287 // IsAlias reports whether obj is an alias name for a type.
288 func (obj *TypeName) IsAlias() bool {
289 switch t := obj.typ.(type) {
293 // unsafe.Pointer is not an alias.
294 if obj.pkg == Unsafe {
297 // Any user-defined type name for a basic type is an alias for a
298 // basic type (because basic types are pre-declared in the Universe
299 // scope, outside any package scope), and so is any type name with
300 // a different name than the name of the basic type it refers to.
301 // Additionally, we need to look for "byte" and "rune" because they
302 // are aliases but have the same names (for better error messages).
303 return obj.pkg != nil || t.name != obj.name || t == universeByte || t == universeRune
311 // A Variable represents a declared variable (including function parameters and results, and struct fields).
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
319 // NewVar returns a new variable.
320 // The arguments set the attributes found with all Objects.
321 func NewVar(pos syntax.Pos, pkg *Package, name string, typ Type) *Var {
322 return &Var{object: object{nil, pos, pkg, name, typ, 0, colorFor(typ), nopos}}
325 // NewParam returns a new variable representing a function parameter.
326 func NewParam(pos syntax.Pos, pkg *Package, name string, typ Type) *Var {
327 return &Var{object: object{nil, pos, pkg, name, typ, 0, colorFor(typ), nopos}, used: true} // parameters are always 'used'
330 // NewField returns a new variable representing a struct field.
331 // For embedded fields, the name is the unqualified type name
332 /// under which the field is accessible.
333 func NewField(pos syntax.Pos, pkg *Package, name string, typ Type, embedded bool) *Var {
334 return &Var{object: object{nil, pos, pkg, name, typ, 0, colorFor(typ), nopos}, embedded: embedded, isField: true}
337 // Anonymous reports whether the variable is an embedded field.
338 // Same as Embedded; only present for backward-compatibility.
339 func (obj *Var) Anonymous() bool { return obj.embedded }
341 // Embedded reports whether the variable is an embedded field.
342 func (obj *Var) Embedded() bool { return obj.embedded }
344 // IsField reports whether the variable is a struct field.
345 func (obj *Var) IsField() bool { return obj.isField }
347 func (*Var) isDependency() {} // a variable may be a dependency of an initialization expression
349 // A Func represents a declared function, concrete method, or abstract
350 // (interface) method. Its Type() is always a *Signature.
351 // An abstract method may belong to many interfaces due to embedding.
354 hasPtrRecv bool // only valid for methods that don't have a type yet
357 // NewFunc returns a new function with the given signature, representing
358 // the function's type.
359 func NewFunc(pos syntax.Pos, pkg *Package, name string, sig *Signature) *Func {
360 // don't store a (typed) nil signature
365 return &Func{object{nil, pos, pkg, name, typ, 0, colorFor(typ), nopos}, false}
368 // FullName returns the package- or receiver-type-qualified name of
369 // function or method obj.
370 func (obj *Func) FullName() string {
372 writeFuncName(&buf, obj, nil)
376 // Scope returns the scope of the function's body block.
377 func (obj *Func) Scope() *Scope { return obj.typ.(*Signature).scope }
379 func (*Func) isDependency() {} // a function may be a dependency of an initialization expression
381 // A Label represents a declared label.
382 // Labels don't have a type.
385 used bool // set if the label was used
388 // NewLabel returns a new label.
389 func NewLabel(pos syntax.Pos, pkg *Package, name string) *Label {
390 return &Label{object{pos: pos, pkg: pkg, name: name, typ: Typ[Invalid], color_: black}, false}
393 // A Builtin represents a built-in function.
394 // Builtins don't have a valid type.
395 type Builtin struct {
400 func newBuiltin(id builtinId) *Builtin {
401 return &Builtin{object{name: predeclaredFuncs[id].name, typ: Typ[Invalid], color_: black}, id}
404 // Nil represents the predeclared value nil.
409 func writeObject(buf *bytes.Buffer, obj Object, qf Qualifier) {
413 switch obj := obj.(type) {
415 fmt.Fprintf(buf, "package %s", obj.Name())
416 if path := obj.imported.path; path != "" && path != obj.name {
417 fmt.Fprintf(buf, " (%q)", path)
422 buf.WriteString("const")
426 buf.WriteString("type")
430 buf.WriteString("field")
432 buf.WriteString("var")
436 buf.WriteString("func ")
437 writeFuncName(buf, obj, qf)
439 WriteSignature(buf, typ.(*Signature), qf)
444 buf.WriteString("label")
448 buf.WriteString("builtin")
452 buf.WriteString("nil")
456 panic(fmt.Sprintf("writeObject(%T)", obj))
461 // For package-level objects, qualify the name.
462 if obj.Pkg() != nil && obj.Pkg().scope.Lookup(obj.Name()) == obj {
463 writePackage(buf, obj.Pkg(), qf)
465 buf.WriteString(obj.Name())
472 // We have a type object: Don't print anything more for
473 // basic types since there's no more information (names
474 // are the same; see also comment in TypeName.IsAlias).
475 if _, ok := typ.(*Basic); ok {
479 buf.WriteString(" =")
486 WriteType(buf, typ, qf)
489 func writePackage(buf *bytes.Buffer, pkg *Package, qf Qualifier) {
505 // ObjectString returns the string form of obj.
506 // The Qualifier controls the printing of
507 // package-level objects, and may be nil.
508 func ObjectString(obj Object, qf Qualifier) string {
510 writeObject(&buf, obj, qf)
514 func (obj *PkgName) String() string { return ObjectString(obj, nil) }
515 func (obj *Const) String() string { return ObjectString(obj, nil) }
516 func (obj *TypeName) String() string { return ObjectString(obj, nil) }
517 func (obj *Var) String() string { return ObjectString(obj, nil) }
518 func (obj *Func) String() string { return ObjectString(obj, nil) }
519 func (obj *Label) String() string { return ObjectString(obj, nil) }
520 func (obj *Builtin) String() string { return ObjectString(obj, nil) }
521 func (obj *Nil) String() string { return ObjectString(obj, nil) }
523 func writeFuncName(buf *bytes.Buffer, f *Func, qf Qualifier) {
525 sig := f.typ.(*Signature)
526 if recv := sig.Recv(); recv != nil {
528 if _, ok := recv.Type().(*Interface); ok {
529 // gcimporter creates abstract methods of
530 // named interfaces using the interface type
531 // (not the named type) as the receiver.
532 // Don't print it in full.
533 buf.WriteString("interface")
535 WriteType(buf, recv.Type(), qf)
539 } else if f.pkg != nil {
540 writePackage(buf, f.pkg, qf)
543 buf.WriteString(f.name)