diff options
author | Robert Griesemer <gri@golang.org> | 2010-01-15 13:27:45 -0800 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2010-01-15 13:27:45 -0800 |
commit | 1dd0f7c2e78657111e265fbc76f281f7850c38c5 (patch) | |
tree | f7a2ae997c05b400635bc380102dd73102b05b0f /src/pkg/go/ast/scope.go | |
parent | 2442a6ddd956d1a902282d9bef429c16348fcf37 (diff) | |
download | golang-1dd0f7c2e78657111e265fbc76f281f7850c38c5.tar.gz |
Steps towards tracking scopes for identifiers.
- Identifiers refer now to the language entity (Object)
that they denote. At the moment this is at best an
approximation.
- Initial data structures for language entities (Objects)
and expression types (Type) independent of the actual
type notations.
- Initial support for declaring and looking up identifiers.
- Updated various dependent files and added support functions.
- Extensively tested to avoid breakage. This is an AST change.
R=rsc
CC=golang-dev, rog
http://codereview.appspot.com/189080
Diffstat (limited to 'src/pkg/go/ast/scope.go')
-rw-r--r-- | src/pkg/go/ast/scope.go | 121 |
1 files changed, 55 insertions, 66 deletions
diff --git a/src/pkg/go/ast/scope.go b/src/pkg/go/ast/scope.go index e8ad12f97..52f44e76b 100644 --- a/src/pkg/go/ast/scope.go +++ b/src/pkg/go/ast/scope.go @@ -4,89 +4,78 @@ package ast -// A Scope maintains the set of identifiers visible -// in the scope and a link to the immediately surrounding -// (outer) scope. +import "go/token" + +type ObjKind int + +// The list of possible Object kinds. +const ( + Err ObjKind = iota // object kind unknown (forward reference or error) + Pkg // package + Con // constant + Typ // type + Var // variable + Fun // function or method +) + + +// An Object describes a language entity such as a package, +// constant, type, variable, or function (incl. methods). // -// NOTE: WORK IN PROGRESS +type Object struct { + Kind ObjKind + Pos token.Position // declaration position + Name string // declared name + Scope *Scope // scope in which the Object is declared +} + + +func NewObj(kind ObjKind, pos token.Position, name string) *Object { + return &Object{kind, pos, name, nil} +} + + +// IsExported returns whether obj is exported. +func (obj *Object) IsExported() bool { return IsExported(obj.Name) } + + +// A Scope maintains the set of named language entities visible +// in the scope and a link to the immediately surrounding (outer) +// scope. // type Scope struct { - Outer *Scope - Names map[string]*Ident + Outer *Scope + Objects map[string]*Object } // NewScope creates a new scope nested in the outer scope. -func NewScope(outer *Scope) *Scope { return &Scope{outer, make(map[string]*Ident)} } +func NewScope(outer *Scope) *Scope { return &Scope{outer, make(map[string]*Object)} } -// Declare inserts an identifier into the scope s. If the -// declaration succeeds, the result is true, if the identifier -// exists already in the scope, the result is false. -// -func (s *Scope) Declare(ident *Ident) bool { - if _, found := s.Names[ident.Value]; found { - return false +// Declare attempts to insert a named object into the scope s. +// If the scope does not contain an object with that name yet, +// Declare inserts the object, and the result is true. Otherwise, +// the scope remains unchanged and the result is false. +func (s *Scope) Declare(obj *Object) bool { + if obj.Name != "_" { + if _, found := s.Objects[obj.Name]; found { + return false + } + s.Objects[obj.Name] = obj } - s.Names[ident.Value] = ident return true } -// Lookup looks up an identifier in the current scope chain. -// If the identifier is found, it is returned; otherwise the -// result is nil. +// Lookup looks up an object in the current scope chain. +// The result is nil if the object is not found. // -func (s *Scope) Lookup(name string) *Ident { +func (s *Scope) Lookup(name string) *Object { for ; s != nil; s = s.Outer { - if ident, found := s.Names[name]; found { - return ident + if obj, found := s.Objects[name]; found { + return obj } } return nil } - - -// TODO(gri) Uncomment once this code is needed. -/* -var Universe = Scope { - Names: map[string]*Ident { - // basic types - "bool": nil, - "byte": nil, - "int8": nil, - "int16": nil, - "int32": nil, - "int64": nil, - "uint8": nil, - "uint16": nil, - "uint32": nil, - "uint64": nil, - "float32": nil, - "float64": nil, - "string": nil, - - // convenience types - "int": nil, - "uint": nil, - "uintptr": nil, - "float": nil, - - // constants - "false": nil, - "true": nil, - "iota": nil, - "nil": nil, - - // functions - "cap": nil, - "len": nil, - "new": nil, - "make": nil, - "panic": nil, - "panicln": nil, - "print": nil, - "println": nil, - } -} -*/
\ No newline at end of file |