/
object.go
212 lines (196 loc) · 3.93 KB
/
object.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package define
import (
"errors"
"fmt"
"go/token"
"golang.org/x/tools/go/types"
)
type Type int
const (
Invalid Type = iota
Bad
Const
Var
TypeName
Func
Method
Interface
Package
)
var typeNames = [...]string{
"Invalid",
"Bad",
"Const",
"Var",
"TypeName",
"Func",
"Method",
"Interface",
"Package",
}
func (t Type) String() string {
if int(t) < len(typeNames) {
return typeNames[t]
}
return typeNames[Invalid]
}
// Same as token.Position
type Position struct {
Filename string // filename, if any
Offset int // offset, starting at 0
Line int // line number, starting at 1
Column int // column number, starting at 1 (character count)
}
func newPosition(tp token.Position) *Position {
p := Position(tp)
return &p
}
func (p Position) IsValid() bool { return p.Line > 0 }
func (p Position) String() string {
s := p.Filename
if p.IsValid() {
if s != "" {
s += ":"
}
s += fmt.Sprintf("%d:%d", p.Line, p.Column)
}
if s == "" {
s = "-"
}
return s
}
type Object struct {
Name string
Parent string // parent type
PkgName string
PkgPath string
ObjType Type // only relevanty when finding imported types
Position Position
IsField bool // only relevant when finding imported types
pos token.Pos
}
func (o *Object) setPkg(p *types.Package) {
if p != nil {
o.PkgPath = p.Path()
o.PkgName = p.Name()
}
}
func (o *Object) setParent(obj types.Object) {
if obj != nil {
o.Parent = obj.Name()
o.setPkg(obj.Pkg())
}
}
func (o *Object) Finder() (f posFinder, err error) {
switch o.ObjType {
case Const, TypeName, Func, Interface:
f = declFinder{Name: o.Name}
case Var:
if o.IsField {
if o.Parent == "" {
err = fmt.Errorf("Finder: missing Parent for field: %s", o.Name)
}
f = fieldFinder{
Name: o.Name,
Parent: o.Parent,
}
} else {
f = declFinder{Name: o.Name}
}
case Method:
f = methodFinder{Name: o.Name, TypeName: o.Parent}
case Package:
f = docFinder{}
case Bad:
err = errors.New("Finder: bad object type")
}
return
}
func newSelector(sel *types.Selection) (*Object, error) {
if sel.Obj() == nil {
return nil, errors.New("nil Selection object")
}
o := &Object{
Name: sel.Obj().Name(),
pos: sel.Obj().Pos(),
}
o.setPkg(sel.Obj().Pkg())
switch t := derefType(sel.Recv()).(type) {
case *types.Named:
o.setParent(t.Obj())
default:
// TODO: log type
// Locally declared type, maybe an anonymous struct.
return nil, fmt.Errorf("unexpected Recv type: %#v for object: %#v", t, sel.Obj())
}
switch sel.Kind() {
case types.FieldVal:
o.IsField = true
o.ObjType = Var
case types.MethodVal:
o.ObjType = Method
case types.MethodExpr:
// TODO: Fix
o.ObjType = Method
}
return o, nil
}
func newObject(obj types.Object, sel *types.Selection) (*Object, error) {
// WARN: Dev only
if sel != nil {
return newSelector(sel)
}
o := &Object{
Name: obj.Name(),
pos: obj.Pos(),
}
o.setPkg(obj.Pkg())
switch typ := obj.(type) {
case *types.PkgName:
o.ObjType = Package
if p := typ.Imported(); p != nil {
o.setPkg(p)
}
case *types.Const:
o.ObjType = Const
case *types.TypeName:
o.ObjType = TypeName
case *types.Var:
o.ObjType = Var
o.IsField = typ.IsField()
if t, ok := derefType(typ.Type()).(*types.Named); ok {
o.ObjType = TypeName
// WARN: This looks wrong
o.IsField = false
if obj := t.Obj(); obj != nil {
o.Name = obj.Name()
o.setPkg(obj.Pkg())
o.pos = obj.Pos() // WARN
}
}
case *types.Func:
if sig := typ.Type().(*types.Signature); sig != nil {
o.ObjType = Func
} else {
switch r := derefType(sig.Recv().Type()).(type) {
case *types.Named:
o.ObjType = Method
o.setParent(r.Obj())
case *types.Interface:
o.ObjType = Interface
default:
// This should never happen
}
}
default:
// TODO: log type
o.ObjType = Bad
}
return o, nil
}
func derefType(t types.Type) types.Type {
if p, ok := t.(*types.Pointer); ok {
return p.Elem()
}
return t
}