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.
15 // TODO(gri) Document factory, accessor methods, and fields. General clean-up.
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.
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)
30 // String returns a human-readable string of the object.
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).
39 // setOrder sets the order number of the object. It must be > 0.
42 // setParent sets the parent scope of the object.
45 // sameId reports whether obj.Id() and Id(pkg, name) are the same.
46 sameId(pkg
*Package
, name
string) bool
48 // scopePos returns the start position of the scope of this Object
51 // setScopePos sets the start position of the scope for this Object.
52 setScopePos(pos token
.Pos
)
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
) {
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
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
!= "" {
72 return path
+ "." + name
75 // An object implements the common parts of an Object.
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_
}
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
}
101 func (obj
*object
) sameId(pkg
*Package
, name
string) bool {
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
{
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
119 // pkg != nil && obj.pkg != nil
120 return pkg
.path
== obj
.pkg
.path
123 // A PkgName represents an imported Go package.
124 type PkgName
struct {
127 used
bool // set if the package was used
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}
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
}
138 // A Const represents a declared constant.
142 visited
bool // for initialization cycle detection
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}
149 func (obj
*Const
) Val() constant
.Value
{ return obj
.val
}
150 func (*Const
) isDependency() {} // a constant may be a dependency of an initialization expression
152 // A TypeName represents a name for a (named or alias) type.
153 type TypeName
struct {
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
}}
161 // IsAlias reports whether obj is an alias name for a type.
162 func (obj
*TypeName
) IsAlias() bool {
163 switch t
:= obj
.typ
.(type) {
167 // unsafe.Pointer is not an alias.
168 if obj
.pkg
== Unsafe
{
171 // Any user-defined type name for a basic type is an alias for a
172 // basic type (because basic types are pre-declared in the Universe
173 // scope, outside any package scope), and so is any type name with
174 // a different name than the name of the basic type it refers to.
175 // Additionally, we need to look for "byte" and "rune" because they
176 // are aliases but have the same names (for better error messages).
177 return obj
.pkg
!= nil || t
.name
!= obj
.name || t
== universeByte || t
== universeRune
185 // A Variable represents a declared variable (including function parameters and results, and struct fields).
188 anonymous
bool // if set, the variable is an anonymous struct field, and name is the type name
189 visited
bool // for initialization cycle detection
190 isField
bool // var is struct field
191 used
bool // set if the variable was used
194 func NewVar(pos token
.Pos
, pkg
*Package
, name
string, typ Type
) *Var
{
195 return &Var
{object
: object
{nil, pos
, pkg
, name
, typ
, 0, token
.NoPos
}}
198 func NewParam(pos token
.Pos
, pkg
*Package
, name
string, typ Type
) *Var
{
199 return &Var
{object
: object
{nil, pos
, pkg
, name
, typ
, 0, token
.NoPos
}, used
: true} // parameters are always 'used'
202 func NewField(pos token
.Pos
, pkg
*Package
, name
string, typ Type
, anonymous
bool) *Var
{
203 return &Var
{object
: object
{nil, pos
, pkg
, name
, typ
, 0, token
.NoPos
}, anonymous
: anonymous
, isField
: true}
206 func (obj
*Var
) Anonymous() bool { return obj
.anonymous
}
207 func (obj
*Var
) IsField() bool { return obj
.isField
}
208 func (*Var
) isDependency() {} // a variable may be a dependency of an initialization expression
210 // A Func represents a declared function, concrete method, or abstract
211 // (interface) method. Its Type() is always a *Signature.
212 // An abstract method may belong to many interfaces due to embedding.
217 func NewFunc(pos token
.Pos
, pkg
*Package
, name
string, sig
*Signature
) *Func
{
218 // don't store a nil signature
223 return &Func
{object
{nil, pos
, pkg
, name
, typ
, 0, token
.NoPos
}}
226 // FullName returns the package- or receiver-type-qualified name of
227 // function or method obj.
228 func (obj
*Func
) FullName() string {
230 writeFuncName(&buf
, obj
, nil)
234 func (obj
*Func
) Scope() *Scope
{ return obj
.typ
.(*Signature
).scope
}
235 func (*Func
) isDependency() {} // a function may be a dependency of an initialization expression
237 // A Label represents a declared label.
240 used
bool // set if the label was used
243 func NewLabel(pos token
.Pos
, pkg
*Package
, name
string) *Label
{
244 return &Label
{object
{pos
: pos
, pkg
: pkg
, name
: name
, typ
: Typ
[Invalid
]}, false}
247 // A Builtin represents a built-in function.
248 // Builtins don't have a valid type.
249 type Builtin
struct {
254 func newBuiltin(id builtinId
) *Builtin
{
255 return &Builtin
{object
{name
: predeclaredFuncs
[id
].name
, typ
: Typ
[Invalid
]}, id
}
258 // Nil represents the predeclared value nil.
263 func writeObject(buf
*bytes
.Buffer
, obj Object
, qf Qualifier
) {
267 switch obj
:= obj
.(type) {
269 fmt
.Fprintf(buf
, "package %s", obj
.Name())
270 if path
:= obj
.imported
.path
; path
!= "" && path
!= obj
.name
{
271 fmt
.Fprintf(buf
, " (%q)", path
)
276 buf
.WriteString("const")
280 buf
.WriteString("type")
284 buf
.WriteString("field")
286 buf
.WriteString("var")
290 buf
.WriteString("func ")
291 writeFuncName(buf
, obj
, qf
)
293 WriteSignature(buf
, typ
.(*Signature
), qf
)
298 buf
.WriteString("label")
302 buf
.WriteString("builtin")
306 buf
.WriteString("nil")
310 panic(fmt
.Sprintf("writeObject(%T)", obj
))
315 // For package-level objects, qualify the name.
316 if obj
.Pkg() != nil && obj
.Pkg().scope
.Lookup(obj
.Name()) == obj
{
317 writePackage(buf
, obj
.Pkg(), qf
)
319 buf
.WriteString(obj
.Name())
326 // We have a type object: Don't print anything more for
327 // basic types since there's no more information (names
328 // are the same; see also comment in TypeName.IsAlias).
329 if _
, ok
:= typ
.(*Basic
); ok
{
333 buf
.WriteString(" =")
335 typ
= typ
.Underlying()
340 WriteType(buf
, typ
, qf
)
343 func writePackage(buf
*bytes
.Buffer
, pkg
*Package
, qf Qualifier
) {
359 // ObjectString returns the string form of obj.
360 // The Qualifier controls the printing of
361 // package-level objects, and may be nil.
362 func ObjectString(obj Object
, qf Qualifier
) string {
364 writeObject(&buf
, obj
, qf
)
368 func (obj
*PkgName
) String() string { return ObjectString(obj
, nil) }
369 func (obj
*Const
) String() string { return ObjectString(obj
, nil) }
370 func (obj
*TypeName
) String() string { return ObjectString(obj
, nil) }
371 func (obj
*Var
) String() string { return ObjectString(obj
, nil) }
372 func (obj
*Func
) String() string { return ObjectString(obj
, nil) }
373 func (obj
*Label
) String() string { return ObjectString(obj
, nil) }
374 func (obj
*Builtin
) String() string { return ObjectString(obj
, nil) }
375 func (obj
*Nil
) String() string { return ObjectString(obj
, nil) }
377 func writeFuncName(buf
*bytes
.Buffer
, f
*Func
, qf Qualifier
) {
379 sig
:= f
.typ
.(*Signature
)
380 if recv
:= sig
.Recv(); recv
!= nil {
382 if _
, ok
:= recv
.Type().(*Interface
); ok
{
383 // gcimporter creates abstract methods of
384 // named interfaces using the interface type
385 // (not the named type) as the receiver.
386 // Don't print it in full.
387 buf
.WriteString("interface")
389 WriteType(buf
, recv
.Type(), qf
)
393 } else if f
.pkg
!= nil {
394 writePackage(buf
, f
.pkg
, qf
)
397 buf
.WriteString(f
.name
)