1
// Copyright 2009 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
// This file implements scopes and the objects they contain.
15
// A Scope maintains the set of named language entities declared
16
// in the scope and a link to the immediately surrounding (outer)
21
Objects map[string]*Object
24
// NewScope creates a new scope nested in the outer scope.
25
func NewScope(outer *Scope) *Scope {
26
const n = 4 // initial scope capacity
27
return &Scope{outer, make(map[string]*Object, n)}
30
// Lookup returns the object with the given name if it is
31
// found in scope s, otherwise it returns nil. Outer scopes
34
func (s *Scope) Lookup(name string) *Object {
35
return s.Objects[name]
38
// Insert attempts to insert a named object obj into the scope s.
39
// If the scope already contains an object alt with the same name,
40
// Insert leaves the scope unchanged and returns alt. Otherwise
41
// it inserts obj and returns nil."
43
func (s *Scope) Insert(obj *Object) (alt *Object) {
44
if alt = s.Objects[obj.Name]; alt == nil {
45
s.Objects[obj.Name] = obj
51
func (s *Scope) String() string {
53
fmt.Fprintf(&buf, "scope %p {", s)
54
if s != nil && len(s.Objects) > 0 {
56
for _, obj := range s.Objects {
57
fmt.Fprintf(&buf, "\t%s %s\n", obj.Kind, obj.Name)
60
fmt.Fprintf(&buf, "}\n")
64
// ----------------------------------------------------------------------------
67
// An Object describes a named language entity such as a package,
68
// constant, type, variable, function (incl. methods), or label.
70
// The Data fields contains object-specific data:
72
// Kind Data type Data value
73
// Pkg *types.Package package scope
74
// Con int iota for the respective declaration
75
// Con != nil constant value
76
// Typ *Scope (used as method scope during type checking - transient)
80
Name string // declared name
81
Decl interface{} // corresponding Field, XxxSpec, FuncDecl, LabeledStmt, AssignStmt, Scope; or nil
82
Data interface{} // object-specific data; or nil
83
Type interface{} // place holder for type information; may be nil
86
// NewObj creates a new object of a given kind and name.
87
func NewObj(kind ObjKind, name string) *Object {
88
return &Object{Kind: kind, Name: name}
91
// Pos computes the source position of the declaration of an object name.
92
// The result may be an invalid position if it cannot be computed
93
// (obj.Decl may be nil or not correct).
94
func (obj *Object) Pos() token.Pos {
96
switch d := obj.Decl.(type) {
98
for _, n := range d.Names {
104
if d.Name != nil && d.Name.Name == name {
109
for _, n := range d.Names {
115
if d.Name.Name == name {
119
if d.Name.Name == name {
123
if d.Label.Name == name {
127
for _, x := range d.Lhs {
128
if ident, isIdent := x.(*Ident); isIdent && ident.Name == name {
133
// predeclared object - nothing to do for now
138
// ObjKind describes what an object represents.
141
// The list of possible Object kinds.
143
Bad ObjKind = iota // for error handling
148
Fun // function or method
152
var objKindStrings = [...]string{
162
func (kind ObjKind) String() string { return objKindStrings[kind] }