forked from cosmos72/gomacro
/
function.go
505 lines (454 loc) · 13.8 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/*
* gomacro - A Go interpreter with Lisp-like macros
*
* Copyright (C) 2017-2019 Massimiliano Ghilardi
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
*
* function.go
*
* Created on Apr 02, 2017
* Author Massimiliano Ghilardi
*/
package fast
import (
"go/ast"
r "reflect"
"github.com/lifepod-solutions/gomacro/base"
"github.com/lifepod-solutions/gomacro/base/reflect"
xr "github.com/lifepod-solutions/gomacro/xreflect"
)
type funcMaker struct {
Name string
nbind int
nintbind int
Param []*Bind
Result []*Bind
resultfun []I
funcbody func(*Env)
}
// DeclFunc compiles a function, macro or method declaration
// For closure declarations, use FuncLit()
//
// This method is named DeclFunc instead of FuncDecl
// for uniformity with DeclType, DeclConst*, DeclVar*, DeclGeneric*
func (c *Comp) DeclFunc(funcdecl *ast.FuncDecl) {
var ismacro bool
if funcdecl.Recv != nil {
switch n := len(funcdecl.Recv.List); n {
case 0:
ismacro = true
case 1:
c.methodDecl(funcdecl)
return
default:
if GENERICS_V1_CXX() || GENERICS_V2_CTI() {
c.DeclGenericFunc(funcdecl)
return
}
c.Errorf("invalid function/method declaration: found %d receivers, expecting at most one: %v", n, funcdecl)
}
}
functype := funcdecl.Type
t, paramnames, resultnames := c.TypeFunction(functype)
// declare the function name and type before compiling its body: allows recursive functions/macros.
funcname := funcdecl.Name.Name
oldbind := c.Binds[funcname]
panicking := true
defer func() {
// On compile error, restore pre-existing declaration
if !panicking || c.Binds == nil {
// nothing to do
} else if oldbind != nil {
c.Binds[funcname] = oldbind
} else {
delete(c.Binds, funcname)
}
}()
var funcbind *Bind
if ismacro {
// use a ConstBind, as builtins do
funcbind = c.NewBind(funcname, ConstBind, c.TypeOfMacro())
} else {
funcbind = c.NewBind(funcname, FuncBind, t)
}
cf := NewComp(c, nil)
info, resultfuns := cf.funcBinds(funcname, functype, t, paramnames, resultnames)
cf.Func = info
if body := funcdecl.Body; body != nil {
// in Go, function arguments/results and function body are in the same scope
for _, node := range body.List {
cf.Stmt(node)
}
}
funcindex := funcbind.Desc.Index()
if funcname == "_" || (!ismacro && funcindex == NoIndex) {
// function/macro named "_". still compile it (to check for compile errors) but discard the compiled code
panicking = false
return
}
// do NOT keep a reference to compile environment!
funcbody := cf.Code.Exec()
var stmt Stmt
if ismacro {
// a macro declaration is a statement:
// executing it stores the macro function into Comp.Binds[funcname].Value
f := cf.macroCreate(t, info, resultfuns, funcbody)
addr := &funcbind.Value
argnum := t.NumIn()
stmt = func(env *Env) (Stmt, *Env) {
fun := f(env)
*addr = Macro{fun, argnum}
env.IP++
return env.Code[env.IP], env
}
} else {
// a function declaration is a statement:
// executing it creates the function in the runtime environment
f := cf.funcCreate(t, info, resultfuns, funcbody)
stmt = func(env *Env) (Stmt, *Env) {
fun := f(env)
// Debugf("setting env.Binds[%d] = %v <%v>", funcindex, fun.Interface(), fun.Type())
env.Vals[funcindex] = fun
env.IP++
return env.Code[env.IP], env
}
}
c.Append(stmt, funcdecl.Pos())
panicking = false
}
func (c *Comp) methodAdd(funcdecl *ast.FuncDecl, t xr.Type) (methodindex int, methods *[]r.Value) {
name := funcdecl.Name.Name
trecv := t.In(0)
if trecv.Kind() == r.Ptr && !trecv.Named() {
// receiver is an unnamed pointer type. add the method to its element type
trecv = trecv.Elem()
}
panicking := true
defer func() {
if panicking {
rec := recover()
c.Errorf("error adding method %s <%v> to type <%v>\n\t%v", name, t, trecv, rec)
}
}()
n1 := trecv.NumExplicitMethod()
methodindex = trecv.AddMethod(name, t)
n2 := trecv.NumExplicitMethod()
if n1 == n2 {
c.Warnf("redefined method: %s.%s", trecv.Name(), name)
}
methods = trecv.GetMethods()
panicking = false
return
}
// methodDecl compiles a method declaration
func (c *Comp) methodDecl(funcdecl *ast.FuncDecl) {
n := len(funcdecl.Recv.List)
if n != 1 {
c.Errorf("invalid function/method declaration: expecting one receiver or nil, found %d receivers: func %v %s(/*...*/)",
n, funcdecl.Recv, funcdecl.Name)
return
}
recvdecl := funcdecl.Recv.List[0]
functype := funcdecl.Type
t, paramnames, resultnames := c.TypeFunctionOrMethod(recvdecl, functype)
// gtype := t.GoType().Underlying().(*types.Signature)
// c.Debugf("declaring method (%v).%s%s %s\n\treflect.Type: <%v>", gtype.Recv().Type(), funcdecl.Name.Name, gtype.Params(), gtype.Results(), t.ReflectType())
// declare the method name and type before compiling its body: allows recursive methods
methodindex, methods := c.methodAdd(funcdecl, t)
cf := NewComp(c, nil)
info, resultfuns := cf.funcBinds(funcdecl.Name.Name, functype, t, paramnames, resultnames)
cf.Func = info
body := funcdecl.Body
if body != nil && len(body.List) != 0 {
// in Go, function arguments/results and function body are in the same scope
cf.List(body.List)
}
// do NOT keep a reference to compile environment!
funcbody := cf.Code.Exec()
f := cf.funcCreate(t, info, resultfuns, funcbody)
// a method declaration is a statement:
// executing it sets the method value in the receiver type
var stmt Stmt
if c.Options&base.OptDebugMethod != 0 {
trecv := t.In(0)
tname := trecv.Name()
if len(tname) == 0 && trecv.Kind() == r.Ptr {
tname = trecv.Elem().Name()
}
methodname := funcdecl.Name
stmt = func(env *Env) (Stmt, *Env) {
(*methods)[methodindex] = f(env).ReflectValue()
env.Run.Debugf("implemented method %s.%s", tname, methodname)
env.IP++
return env.Code[env.IP], env
}
} else {
stmt = func(env *Env) (Stmt, *Env) {
(*methods)[methodindex] = f(env).ReflectValue()
env.IP++
return env.Code[env.IP], env
}
}
c.Append(stmt, funcdecl.Pos())
}
// FuncLit compiles a function literal, i.e. a closure.
// For functions or methods declarations, use FuncDecl()
func (c *Comp) FuncLit(funclit *ast.FuncLit) *Expr {
functype := funclit.Type
t, paramnames, resultnames := c.TypeFunction(functype)
cf := NewComp(c, nil)
info, resultfuns := cf.funcBinds("", functype, t, paramnames, resultnames)
cf.Func = info
body := funclit.Body
if body != nil && len(body.List) != 0 {
// in Go, function arguments/results and function body are in the same scope
cf.List(body.List)
}
// do NOT keep a reference to compile environment!
funcbody := cf.Code.Exec()
f := cf.funcCreate(t, info, resultfuns, funcbody)
// a function literal is an expression:
// executing it returns the function
return exprX1(t, f)
}
// prepare the function parameter binds, result binds and FuncInfo
func (c *Comp) funcBinds(funcname string, functype *ast.FuncType, t xr.Type, paramnames, resultnames []string) (info *FuncInfo, resultfuns []I) {
parambinds := c.funcParamBinds(functype, t, paramnames)
resultbinds, resultfuns := c.funcResultBinds(functype, t, resultnames)
namedresults := true
for _, resultname := range resultnames {
if len(resultname) == 0 {
namedresults = false
}
}
return &FuncInfo{
Name: funcname,
Param: parambinds,
Result: resultbinds,
NamedResults: namedresults,
}, resultfuns
}
// prepare the function parameter binds
func (c *Comp) funcParamBinds(functype *ast.FuncType, t xr.Type, names []string) []*Bind {
nin := t.NumIn()
binds := make([]*Bind, nin)
var namedparams, unnamedparams bool
ismethod := t.IsMethod()
for i := 0; i < nin; i++ {
// names[i] == "" means that argument is unnamed, and thus ignored inside the function.
// change to "_" so that NewBind will not allocate a bind for it - correct optimization...
// just remember to check for such case when creating the function
name := names[i]
if !ismethod || i != 0 {
// method receiver can be named or unnamed, independently from other input parameters
if name == "" {
name = "_"
unnamedparams = true
} else {
namedparams = true
}
}
if namedparams && unnamedparams {
c.Errorf("cannot mix named and unnamed parameters in function declaration: %v", functype)
}
bind := c.NewBind(name, VarBind, t.In(i))
binds[i] = bind
}
return binds
}
// prepare the function result binds
func (c *Comp) funcResultBinds(functype *ast.FuncType, t xr.Type, names []string) (binds []*Bind, funs []I) {
n := t.NumOut()
binds = make([]*Bind, n)
funs = make([]I, n)
var namedresults, unnamedresults bool
for i, n := 0, t.NumOut(); i < n; i++ {
// names[i] == "" means that result is unnamed.
// we must still allocate a bind for it.
name := names[i]
if name == "" {
unnamedresults = true
} else {
namedresults = true
}
if namedresults && unnamedresults {
c.Errorf("cannot mix named and unnamed results in function declaration: %v", functype)
}
c.Pos = functype.Pos()
bind := c.DeclVar0(name, t.Out(i), nil)
binds[i] = bind
// compile the extraction of results from runtime env
funs[i] = c.Bind(bind).WithFun()
}
return
}
func (c *Comp) funcMaker(info *FuncInfo, resultfuns []I, funcbody func(*Env)) *funcMaker {
m := &funcMaker{
Name: info.Name,
nbind: c.BindNum,
nintbind: c.IntBindNum,
Param: info.Param,
Result: info.Result,
resultfun: resultfuns,
funcbody: funcbody,
}
c.FuncMaker = m // store it for debugger command 'backtrace'
return m
}
// actually create the function
func (c *Comp) funcCreate(t xr.Type, info *FuncInfo, resultfuns []I, funcbody func(*Env)) func(*Env) xr.Value {
m := c.funcMaker(info, resultfuns, funcbody)
rtype := t.ReflectType() // has receiver as first parameter (unless it's xreflect.Forward)
nin := t.NumIn()
nout := t.NumOut()
// do not create optimized functions if arguments or results are named types
optimize := rtype != rtypeOfForward
for i := 0; optimize && i < nin; i++ {
rt := rtype.In(i)
k := rt.Kind()
optimize = reflect.IsOptimizedKind(k) && rt == c.Universe.BasicTypes[k].ReflectType()
}
for i := 0; optimize && i < nout; i++ {
rt := rtype.Out(i)
k := rt.Kind()
optimize = reflect.IsOptimizedKind(k) && rt == c.Universe.BasicTypes[k].ReflectType()
}
var fun func(*Env) xr.Value
if optimize {
switch nin {
case 0:
switch nout {
case 0:
fun = c.func0ret0(t, m)
case 1:
fun = c.func0ret1(t, m)
}
case 1:
switch nout {
case 0:
fun = c.func1ret0(t, m)
case 1:
fun = c.func1ret1(t, m)
}
case 2:
switch nout {
case 0:
fun = c.func2ret0(t, m)
}
}
}
if fun == nil {
fun = c.funcGeneric(t, m)
}
return fun
}
// fallback: create a non-optimized function
func (c *Comp) funcGeneric(t xr.Type, m *funcMaker) func(*Env) xr.Value {
// do NOT keep a reference to funcMaker
nbinds := m.nbind
nintbinds := m.nintbind
funcbody := m.funcbody
if funcbody == nil {
// pre-fill rets with zero values
rets := make([]xr.Value, len(m.Result))
for i, bind := range m.Result {
rets[i] = xr.Zero(bind.Type)
}
return func(env *Env) xr.Value {
return xr.MakeFunc(t, func(args []xr.Value) []xr.Value {
return rets
})
}
}
paramdecls := make([]func(*Env, xr.Value), len(m.Param))
for i, bind := range m.Param {
if bind.Desc.Index() != NoIndex {
paramdecls[i] = c.DeclBindRuntimeValue(bind)
}
}
resultexprs := make([]func(*Env) xr.Value, len(m.resultfun))
for i, resultfun := range m.resultfun {
resultexprs[i] = funAsX1(resultfun, m.Result[i].Type)
}
var debugC *Comp
if c.Globals.Options&base.OptDebugger != 0 {
// keep a reference to c only if needed
debugC = c
}
return func(env *Env) xr.Value {
// function is closed over the env used to DECLARE it
env.MarkUsedByClosure()
return xr.MakeFunc(t, func(args []xr.Value) []xr.Value {
env := newEnv4Func(env, nbinds, nintbinds, debugC)
// copy runtime arguments into allocated binds
for i, decl := range paramdecls {
if decl != nil {
// decl == nil means the argument is ignored inside the function
decl(env, args[i])
}
}
// execute the body
funcbody(env)
// read results from allocated binds and return them
rets := make([]xr.Value, len(resultexprs))
for i, expr := range resultexprs {
rets[i] = expr(env)
}
env.freeEnv4Func()
return rets
})
}
}
// create a macro
func (c *Comp) macroCreate(t xr.Type, info *FuncInfo, resultfuns []I, funcbody func(*Env)) func(*Env) func(args []xr.Value) []xr.Value {
m := c.funcMaker(info, resultfuns, funcbody)
paramdecls := make([]func(*Env, xr.Value), len(m.Param))
for i, bind := range m.Param {
if bind.Desc.Index() != NoIndex {
paramdecls[i] = c.DeclBindRuntimeValue(bind)
}
}
resultexprs := make([]func(*Env) xr.Value, len(m.resultfun))
for i, resultfun := range m.resultfun {
resultexprs[i] = funAsX1(resultfun, m.Result[i].Type)
}
// do NOT keep a reference to funcMaker
nbinds := m.nbind
nintbinds := m.nintbind
var debugC *Comp
if c.Globals.Options&base.OptDebugger != 0 {
// keep a reference to c only if needed
debugC = c
}
return func(env *Env) func(args []xr.Value) []xr.Value {
// macro is closed over the env used to DECLARE it
env.MarkUsedByClosure()
return func(args []xr.Value) []xr.Value {
env := newEnv4Func(env, nbinds, nintbinds, debugC)
if funcbody != nil {
// copy runtime arguments into allocated binds
for i, decl := range paramdecls {
if decl != nil {
// decl == nil means the argument is ignored inside the function
decl(env, args[i])
}
}
// execute the body
funcbody(env)
}
// read results from allocated binds and return them
rets := make([]xr.Value, len(resultexprs))
for i, expr := range resultexprs {
rets[i] = expr(env)
}
env.freeEnv4Func()
return rets
}
}
}
func declBindRuntimeValueNop(*Env, xr.Value) {
}