/
function.go
292 lines (246 loc) · 5.14 KB
/
function.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package defs
import (
"go/ast"
"go/types"
"strings"
"github.com/matthewmueller/joy/internal/paths"
"github.com/pkg/errors"
"golang.org/x/tools/go/loader"
"github.com/matthewmueller/joy/internal/compiler/def"
"github.com/matthewmueller/joy/internal/compiler/index"
"github.com/matthewmueller/joy/internal/compiler/util"
)
// Functioner interface
type Functioner interface {
def.Definition
IsAsync() (bool, error)
IsVariadic() bool
Node() *ast.FuncDecl
Rewrite() def.Rewrite
Params() []string
Results() []def.FunctionResult
}
var _ Functioner = (*functions)(nil)
type functions struct {
info *loader.PackageInfo
index *index.Index
id string
path string
name string
kind types.Type
node *ast.FuncDecl
exported bool
tag util.JSTag
runtime bool
processed bool
deps []def.Definition
rewrite def.Rewrite
results []def.FunctionResult
async bool
imports map[string]string
omit bool
params []string
variadic bool
rename string
}
type result struct {
name string
def def.Definition
}
func (r *result) Name() string {
return r.name
}
func (r *result) Definition() def.Definition {
return r.def
}
// Function fn
func Function(index *index.Index, info *loader.PackageInfo, n *ast.FuncDecl) (def.Definition, error) {
obj := info.ObjectOf(n.Name)
packagePath := obj.Pkg().Path()
name := n.Name.Name
idParts := []string{packagePath, name}
id := strings.Join(idParts, " ")
// TODO: scoping
// scope := info.Scopes[n.Type]
var params []string
var variadic bool
for _, param := range n.Type.Params.List {
for _, ident := range param.Names {
params = append(params, ident.Name)
}
if _, ok := param.Type.(*ast.Ellipsis); ok {
variadic = true
}
}
var results []def.FunctionResult
if n.Type.Results != nil {
for _, r := range n.Type.Results.List {
def, err := index.DefinitionOf(packagePath, r.Type)
if err != nil {
return nil, err
}
if len(r.Names) == 0 {
results = append(results, &result{
def: def,
})
}
for _, id := range r.Names {
results = append(results, &result{
name: id.Name,
def: def,
})
}
}
}
// if it's a method don't export,
// if it's the main() function
// export either way
exported := obj.Exported()
if n.Recv != nil {
exported = false
} else if name == "main" {
exported = true
}
tag, e := util.JSTagFromComment(n.Doc)
if e != nil {
return nil, e
}
runtimePath, err := paths.Runtime()
if err != nil {
return nil, errors.Wrapf(err, "error getting runtime path")
}
fromRuntime := strings.HasSuffix(runtimePath, packagePath)
return &functions{
index: index,
info: info,
id: id,
exported: exported,
path: packagePath,
runtime: fromRuntime,
name: name,
node: n,
kind: info.TypeOf(n.Name),
imports: map[string]string{},
params: params,
results: results,
variadic: variadic,
tag: tag,
}, nil
}
func (d *functions) process() (err error) {
state, e := process(d.index, d, d.node)
if e != nil {
return e
}
// copy state into function
d.processed = true
d.async = state.async
d.deps = state.deps
d.imports = state.imports
d.omit = state.omit
d.rewrite = state.rewrite
d.rename = state.rename
if d.tag.Rename == "" {
d.tag = state.tag
}
return nil
}
func (d *functions) ID() string {
return d.id
}
func (d *functions) Name() string {
if d.tag.Rename != "" {
return d.tag.Rename
}
if d.rename != "" {
return d.rename
}
return d.name
}
func (d *functions) OriginalName() string {
return d.name
}
func (d *functions) Path() string {
return d.path
}
func (d *functions) Dependencies() (deps []def.Definition, err error) {
if d.processed {
return d.deps, nil
}
e := d.process()
if e != nil {
return deps, e
}
return d.deps, nil
}
func (d *functions) Exported() bool {
return d.exported
}
func (d *functions) Omitted() bool {
if d.tag.Omit {
return true
}
return d.omit
}
func (d *functions) Node() *ast.FuncDecl {
return d.node
}
func (d *functions) Type() types.Type {
return d.kind
}
func (d *functions) Kind() string {
return "FUNCTION"
}
func (d *functions) IsAsync() (bool, error) {
if d.processed {
return d.async, nil
}
e := d.process()
if e != nil {
return false, e
}
return d.async, nil
}
// Rewrite fn
func (d *functions) Rewrite() def.Rewrite {
return d.rewrite
}
// Params fn
func (d *functions) Params() []string {
return d.params
}
func (d *functions) Imports() map[string]string {
// combine def imports with file imports
imports := map[string]string{}
for alias, path := range d.imports {
imports[alias] = path
}
for alias, path := range d.index.GetImports(d.path) {
imports[alias] = path
}
return imports
}
func (d *functions) FromRuntime() bool {
return d.runtime
}
func (d *functions) maybeAsync(def def.Definition) error {
if d.async || d.ID() == def.ID() {
return nil
}
fn, ok := def.(Functioner)
if !ok {
return nil
}
async, e := fn.IsAsync()
if e != nil {
return e
}
d.async = async
return nil
}
func (d *functions) IsVariadic() bool {
return d.variadic
}
func (d *functions) Results() []def.FunctionResult {
return d.results
}