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.
12 // An Object describes a named language entity such as a package,
13 // constant, type, variable, function (incl. methods), or label.
14 // All objects implement the Object interface.
16 type Object
interface {
24 // A Package represents the contents (objects) of a Go package.
27 Path
string // import path, "" for current (non-imported) package
28 Scope
*Scope
// package-level scope
29 Imports
map[string]*Package
// map of import paths to imported packages
30 Complete
bool // if set, this package was imported completely
35 // A Const represents a declared constant.
44 // A TypeName represents a declared type.
45 type TypeName
struct {
47 Type Type
// *NamedType or *Basic
52 // A Variable represents a declared variable (including function parameters and results).
57 visited
bool // for initialization cycle detection
61 // A Func represents a declared function.
64 Type Type
// *Signature or *Builtin
69 func (obj
*Package
) GetName() string { return obj
.Name
}
70 func (obj
*Const
) GetName() string { return obj
.Name
}
71 func (obj
*TypeName
) GetName() string { return obj
.Name
}
72 func (obj
*Var
) GetName() string { return obj
.Name
}
73 func (obj
*Func
) GetName() string { return obj
.Name
}
75 func (obj
*Package
) GetType() Type
{ return Typ
[Invalid
] }
76 func (obj
*Const
) GetType() Type
{ return obj
.Type
}
77 func (obj
*TypeName
) GetType() Type
{ return obj
.Type
}
78 func (obj
*Var
) GetType() Type
{ return obj
.Type
}
79 func (obj
*Func
) GetType() Type
{ return obj
.Type
}
81 func (obj
*Package
) GetPos() token
.Pos
{ return obj
.spec
.Pos() }
82 func (obj
*Const
) GetPos() token
.Pos
{
83 for _
, n
:= range obj
.spec
.Names
{
84 if n
.Name
== obj
.Name
{
90 func (obj
*TypeName
) GetPos() token
.Pos
{ return obj
.spec
.Pos() }
91 func (obj
*Var
) GetPos() token
.Pos
{
92 switch d
:= obj
.decl
.(type) {
94 for _
, n
:= range d
.Names
{
95 if n
.Name
== obj
.Name
{
100 for _
, n
:= range d
.Names
{
101 if n
.Name
== obj
.Name
{
105 case *ast
.AssignStmt
:
106 for _
, x
:= range d
.Lhs
{
107 if ident
, isIdent
:= x
.(*ast
.Ident
); isIdent
&& ident
.Name
== obj
.Name
{
114 func (obj
*Func
) GetPos() token
.Pos
{ return obj
.decl
.Name
.Pos() }
116 func (*Package
) anObject() {}
117 func (*Const
) anObject() {}
118 func (*TypeName
) anObject() {}
119 func (*Var
) anObject() {}
120 func (*Func
) anObject() {}
122 // newObj returns a new Object for a given *ast.Object.
123 // It does not canonicalize them (it always returns a new one).
124 // For canonicalization, see check.lookup.
126 // TODO(gri) Once we do identifier resolution completely in
127 // in the typechecker, this functionality can go.
129 func newObj(astObj
*ast
.Object
) Object
{
131 typ
, _
:= astObj
.Type
.(Type
)
138 return &Const
{Name
: name
, Type
: typ
, Val
: astObj
.Data
, spec
: astObj
.Decl
.(*ast
.ValueSpec
)}
140 return &TypeName
{Name
: name
, Type
: typ
, spec
: astObj
.Decl
.(*ast
.TypeSpec
)}
142 switch astObj
.Decl
.(type) {
143 case *ast
.Field
, *ast
.ValueSpec
, *ast
.AssignStmt
: // these are ok
147 return &Var
{Name
: name
, Type
: typ
, decl
: astObj
.Decl
}
149 return &Func
{Name
: name
, Type
: typ
, decl
: astObj
.Decl
.(*ast
.FuncDecl
)}
151 unreachable() // for now