-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathscope.go
More file actions
56 lines (47 loc) · 960 Bytes
/
scope.go
File metadata and controls
56 lines (47 loc) · 960 Bytes
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
package knife
import (
"fmt"
"go/token"
"go/types"
)
type Scope struct {
TypesScope *types.Scope
Parent *Scope
Children []*Scope
Pos token.Pos
End token.Pos
Objects map[string]Object
Names []string
}
var _ fmt.Stringer = (*Scope)(nil)
func NewScope(s *types.Scope) *Scope {
if s == nil {
return nil
}
v, _ := cache.Load(s)
cached, _ := v.(*Scope)
if cached != nil {
return cached
}
var ns Scope
cache.Store(s, &ns)
ns.TypesScope = s
ns.Parent = NewScope(s.Parent())
ns.Children = make([]*Scope, s.NumChildren())
for i := range ns.Children {
ns.Children[i] = NewScope(s.Child(i))
}
ns.Pos = s.Pos()
ns.End = s.End()
ns.Objects = make(map[string]Object, s.Len())
ns.Names = make([]string, s.Len())
for i, name := range s.Names() {
ns.Names[i] = name
o := s.Lookup(name)
ns.Objects[name] = NewObject(o)
}
return &ns
}
func (s *Scope) String() string {
return s.TypesScope.String()
}