forked from nsf/gocode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scope.go
66 lines (57 loc) · 1.24 KB
/
scope.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
//-------------------------------------------------------------------------
// Scope
//-------------------------------------------------------------------------
type Scope struct {
parent *Scope // nil for universe scope
entities map[string]*Decl
}
func NewScope(outer *Scope) *Scope {
s := new(Scope)
s.parent = outer
s.entities = make(map[string]*Decl)
return s
}
// returns: new, prev
func AdvanceScope(s *Scope) (*Scope, *Scope) {
if len(s.entities) == 0 {
return s, s.parent
}
return NewScope(s), s
}
// adds declaration or returns an existing one
func (s *Scope) addNamedDecl(d *Decl) *Decl {
return s.addDecl(d.Name, d)
}
func (s *Scope) addDecl(name string, d *Decl) *Decl {
decl, ok := s.entities[name]
if !ok {
s.entities[name] = d
return d
}
return decl
}
func (s *Scope) replaceDecl(name string, d *Decl) {
s.entities[name] = d
}
func (s *Scope) mergeDecl(d *Decl) {
decl, ok := s.entities[d.Name]
if !ok {
s.entities[d.Name] = d
} else {
decl := decl.DeepCopy()
decl.ExpandOrReplace(d)
s.entities[d.Name] = decl
}
}
func (s *Scope) lookup(name string) *Decl {
decl, ok := s.entities[name]
if !ok {
if s.parent != nil {
return s.parent.lookup(name)
} else {
return nil
}
}
return decl
}