-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspec.go
160 lines (143 loc) · 3.07 KB
/
spec.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package gospec
import (
"go/ast"
"go/importer"
"go/parser"
"go/token"
"go/types"
"log"
)
func init() {
spec = NewSpec("")
}
var spec *Spec
type SearchKind int
// src/go/types/scope.go Scope has 4 level:Universe、Package、File、Local
const (
SearchOnlyPackage = iota
SearchPackageAndUniverse
SearchAll
)
type Spec struct {
code string
file *ast.File
pkg *types.Package
checker *types.Checker
SearchKind
}
func NewSpec(code string) *Spec {
s := new(Spec)
addPackageHeadToCode(&code)
packageName := mustGetPackageNameFromCode(code)
s.code = code
var err error
fset := token.NewFileSet()
s.file, err = parser.ParseFile(fset, packageName+".go", code, 0)
if err != nil {
log.Panicf("parse code failed: %s", err)
}
c := new(types.Config)
c.Error = func(err error) {} // 防止触发 go/types.(*Checker).err 方法里的 panic
c.Importer = importer.Default() // 增加golang包导入,使之可以识别 import 的包
s.pkg = types.NewPackage(packageName, "")
s.checker = types.NewChecker(c, fset, s.pkg, nil)
s.SearchKind = SearchPackageAndUniverse // default search in universe and pkg scope
err = s.checker.Files([]*ast.File{s.file})
if err != nil {
log.Panicf("check file failed: %s", err)
}
return s
}
func (s *Spec) GetTypeObject(v string) types.Object {
switch s.SearchKind {
case SearchOnlyPackage:
return s.pkg.Scope().Lookup(v)
case SearchPackageAndUniverse:
o := s.pkg.Scope().Lookup(v)
if o == nil {
o = types.Universe.Lookup(v)
}
return o
case SearchAll:
o := lookupByBFS(s.pkg.Scope(), v)
if o == nil {
o = types.Universe.Lookup(v)
}
return o
default:
panic("unexpect")
}
return nil
}
func (s *Spec) MustGetValidTypeObject(v string) types.Object {
o := s.GetTypeObject(v)
if o == nil {
panic("find <" + v + "> in code <" + s.code + "> failed")
}
return o
}
func (s *Spec) GetType(v string) types.Type {
o := s.GetTypeObject(v)
if o == nil {
return nil
} else {
return o.Type()
}
}
func (s *Spec) MustGetValidType(v string) types.Type {
o := s.GetTypeObject(v)
if o == nil {
panic("find <" + v + "> in code <" + s.code + "> failed")
}
return o.Type()
}
func (s *Spec) GetUnderlyingType(v string) types.Type {
t := s.GetType(v)
if t == nil {
return nil
}
return t.Underlying()
}
func (s *Spec) GetBaseType(v string) types.Type {
t := s.GetType(v)
if t == nil {
return nil
}
p, isPointer := t.(*types.Pointer)
if !isPointer {
return nil
}
return p.Elem()
}
func (s *Spec) IsInUniverse(v string) bool {
switch s.SearchKind {
case SearchOnlyPackage, SearchPackageAndUniverse:
if s.pkg.Scope().Lookup(v) == nil {
if types.Universe.Lookup(v) != nil {
return true
}
}
case SearchAll:
if lookupByBFS(s.pkg.Scope(), v) == nil {
if types.Universe.Lookup(v) != nil {
return true
}
}
default:
panic("unexpect")
}
return false
}
func lookupByBFS(scope *types.Scope, v string) types.Object {
o := scope.Lookup(v)
if o != nil {
return o
}
for i := 0; i < scope.NumChildren(); i++ {
o = lookupByBFS(scope.Child(i), v)
if o != nil {
return o
}
}
return nil
}