* tree-ssa-reassoc.c (reassociate_bb): Clarify code slighly.
[official-gcc.git] / libgo / go / go / types / object.go
blobf0bcd6766b52eada028801f4b659f42d4bf05bdc
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.
5 package types
7 import (
8 "bytes"
9 "fmt"
10 "go/ast"
11 "go/constant"
12 "go/token"
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.
31 String() string
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
39 // setOrder sets the order number of the object. It must be > 0.
40 setOrder(uint32)
42 // setParent sets the parent scope of the object.
43 setParent(*Scope)
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
49 scopePos() token.Pos
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) {
59 return 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
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
72 return path + "." + name
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
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 {
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
109 // obj.Name == name
110 if obj.Exported() {
111 return true
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 {
125 object
126 imported *Package
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.
139 type Const struct {
140 object
141 val constant.Value
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 {
154 object
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) {
164 case nil:
165 return false
166 case *Basic:
167 // unsafe.Pointer is not an alias.
168 if obj.pkg == Unsafe {
169 return false
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
178 case *Named:
179 return obj != t.obj
180 default:
181 return true
185 // A Variable represents a declared variable (including function parameters and results, and struct fields).
186 type Var struct {
187 object
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.
213 type Func struct {
214 object
217 func NewFunc(pos token.Pos, pkg *Package, name string, sig *Signature) *Func {
218 // don't store a nil signature
219 var typ Type
220 if sig != nil {
221 typ = sig
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 {
229 var buf bytes.Buffer
230 writeFuncName(&buf, obj, nil)
231 return buf.String()
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.
238 type Label struct {
239 object
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 {
250 object
251 id builtinId
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.
259 type Nil struct {
260 object
263 func writeObject(buf *bytes.Buffer, obj Object, qf Qualifier) {
264 var tname *TypeName
265 typ := obj.Type()
267 switch obj := obj.(type) {
268 case *PkgName:
269 fmt.Fprintf(buf, "package %s", obj.Name())
270 if path := obj.imported.path; path != "" && path != obj.name {
271 fmt.Fprintf(buf, " (%q)", path)
273 return
275 case *Const:
276 buf.WriteString("const")
278 case *TypeName:
279 tname = obj
280 buf.WriteString("type")
282 case *Var:
283 if obj.isField {
284 buf.WriteString("field")
285 } else {
286 buf.WriteString("var")
289 case *Func:
290 buf.WriteString("func ")
291 writeFuncName(buf, obj, qf)
292 if typ != nil {
293 WriteSignature(buf, typ.(*Signature), qf)
295 return
297 case *Label:
298 buf.WriteString("label")
299 typ = nil
301 case *Builtin:
302 buf.WriteString("builtin")
303 typ = nil
305 case *Nil:
306 buf.WriteString("nil")
307 return
309 default:
310 panic(fmt.Sprintf("writeObject(%T)", obj))
313 buf.WriteByte(' ')
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())
321 if typ == nil {
322 return
325 if tname != nil {
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 {
330 return
332 if tname.IsAlias() {
333 buf.WriteString(" =")
334 } else {
335 typ = typ.Underlying()
339 buf.WriteByte(' ')
340 WriteType(buf, typ, qf)
343 func writePackage(buf *bytes.Buffer, pkg *Package, qf Qualifier) {
344 if pkg == nil {
345 return
347 var s string
348 if qf != nil {
349 s = qf(pkg)
350 } else {
351 s = pkg.Path()
353 if s != "" {
354 buf.WriteString(s)
355 buf.WriteByte('.')
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 {
363 var buf bytes.Buffer
364 writeObject(&buf, obj, qf)
365 return buf.String()
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) {
378 if f.typ != nil {
379 sig := f.typ.(*Signature)
380 if recv := sig.Recv(); recv != nil {
381 buf.WriteByte('(')
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")
388 } else {
389 WriteType(buf, recv.Type(), qf)
391 buf.WriteByte(')')
392 buf.WriteByte('.')
393 } else if f.pkg != nil {
394 writePackage(buf, f.pkg, qf)
397 buf.WriteString(f.name)