-
Notifications
You must be signed in to change notification settings - Fork 28
/
func.go
268 lines (232 loc) · 7.54 KB
/
func.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
/*
Copyright 2021 The GoPlus Authors (goplus.org)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gogen
import (
"fmt"
"go/ast"
"go/token"
"go/types"
"log"
"github.com/goplus/gogen/internal"
)
// ----------------------------------------------------------------------------
// A Param represents a function parameters and results.
type Param = types.Var
// NewAutoParam returns a new variable representing a function result parameter with auto type.
func (p *Package) NewAutoParam(name string) *Param {
return p.NewAutoParamEx(token.NoPos, name)
}
// NewAutoParamEx returns a new variable representing a function result parameter with auto type.
func (p *Package) NewAutoParamEx(pos token.Pos, name string) *Param {
return types.NewParam(pos, p.Types, name, &unboundType{})
}
// NewParam returns a new variable representing a function parameter.
func (p *Package) NewParam(pos token.Pos, name string, typ types.Type) *Param {
return types.NewParam(pos, p.Types, name, typ)
}
// ----------------------------------------------------------------------------
// A Tuple represents an ordered list of variables; a nil *Tuple is a valid (empty) tuple.
// Tuples are used as components of signatures and to represent the type of multiple
// assignments; they are not first class types of Go.
type Tuple = types.Tuple
// NewTuple returns a new tuple for the given parameters.
func NewTuple(x ...*Param) *Tuple {
return types.NewTuple(x...)
}
// ----------------------------------------------------------------------------
// Func type
type Func struct {
*types.Func
decl *ast.FuncDecl
old funcBodyCtx
arity1 int // 0 for normal, (arity+1) for inlineClosure
}
// Obj returns this function object.
func (p *Func) Obj() types.Object {
return p.Func
}
// Comments returns associated documentation.
func (p *Func) Comments() *ast.CommentGroup {
return p.decl.Doc
}
// SetComments sets associated documentation.
func (p *Func) SetComments(pkg *Package, doc *ast.CommentGroup) *Func {
p.decl.Doc = doc
pkg.setDoc(p.Func, doc)
return p
}
// Ancestor returns ancestor of a closure function.
// It returns itself if the specified func is a normal function.
func (p *Func) Ancestor() *Func {
for {
if fn := p.old.fn; fn != nil {
p = fn
continue
}
return p
}
}
// BodyStart func
func (p *Func) BodyStart(pkg *Package, src ...ast.Node) *CodeBuilder {
if debugInstr {
var recv string
tag := "NewFunc "
name := p.Name()
sig := p.Type().(*types.Signature)
if v := sig.Recv(); IsMethodRecv(v) {
recv = fmt.Sprintf(" (%v)", v.Type())
}
if name == "" {
tag = "NewClosure"
}
log.Printf("%v%v%v %v\n", tag, name, recv, sig)
}
return pkg.cb.startFuncBody(p, src, &p.old)
}
// End is for internal use.
func (p *Func) End(cb *CodeBuilder, src ast.Node) {
if p.isInline() {
p.inlineClosureEnd(cb)
return
}
pkg := cb.pkg
body := &ast.BlockStmt{List: cb.endFuncBody(p.old)}
t, _ := toNormalizeSignature(nil, p.Type().(*types.Signature))
if fn := p.decl; fn == nil { // is closure
expr := &ast.FuncLit{Type: toFuncType(pkg, t), Body: body}
cb.stk.Push(&internal.Elem{Val: expr, Type: t, Src: src})
} else {
fn.Name, fn.Type, fn.Body = ident(p.Name()), toFuncType(pkg, t), body
if recv := t.Recv(); IsMethodRecv(recv) {
fn.Recv = toRecv(pkg, recv)
}
}
}
// NewFuncDecl creates a new function without function body (declaration only).
func (p *Package) NewFuncDecl(pos token.Pos, name string, sig *types.Signature) *Func {
f, err := p.NewFuncWith(pos, name, sig, nil)
if err != nil {
panic(err)
}
fn := f.decl
fn.Name, fn.Type = ident(name), toFuncType(p, sig)
return f
}
// NewFunc creates a new function (should have a function body).
func (p *Package) NewFunc(recv *Param, name string, params, results *Tuple, variadic bool) *Func {
sig := types.NewSignatureType(recv, nil, nil, params, results, variadic)
f, err := p.NewFuncWith(token.NoPos, name, sig, nil)
if err != nil {
panic(err)
}
return f
}
func getRecv(recvTypePos func() token.Pos) token.Pos {
if recvTypePos != nil {
return recvTypePos()
}
return token.NoPos
}
func IsMethodRecv(recv *types.Var) bool {
return recv != nil
}
// NewFuncWith creates a new function (should have a function body).
func (p *Package) NewFuncWith(
pos token.Pos, name string, sig *types.Signature, recvTypePos func() token.Pos) (*Func, error) {
if name == "" {
panic("no func name")
}
cb := p.cb
fn := &Func{Func: types.NewFunc(pos, p.Types, name, sig)}
if recv := sig.Recv(); IsMethodRecv(recv) { // add method to this type
var t *types.Named
var ok bool
var typ = recv.Type()
switch tt := typ.(type) {
case *types.Named:
t, ok = tt, true
case *types.Pointer:
typ = tt.Elem()
t, ok = typ.(*types.Named)
}
if !ok {
return nil, cb.newCodeErrorf(
getRecv(recvTypePos), "invalid receiver type %v (%v is not a defined type)", typ, typ)
}
switch getUnderlying(p, t.Obj().Type()).(type) {
case *types.Interface:
return nil, cb.newCodeErrorf(
getRecv(recvTypePos), "invalid receiver type %v (%v is an interface type)", typ, typ)
case *types.Pointer:
return nil, cb.newCodeErrorf(
getRecv(recvTypePos), "invalid receiver type %v (%v is a pointer type)", typ, typ)
}
if name != "_" { // skip underscore
t.AddMethod(fn.Func)
}
} else if name == "init" { // init is not a normal func
if sig.Params() != nil || sig.Results() != nil {
return nil, cb.newCodeErrorf(
pos, "func init must have no arguments and no return values")
}
} else if name != "_" { // skip underscore
old := p.Types.Scope().Insert(fn.Obj())
if old != nil {
if !(p.allowRedecl && types.Identical(old.Type(), sig)) { // for c2go
oldPos := cb.fset.Position(old.Pos())
return nil, cb.newCodeErrorf(
pos, "%s redeclared in this block\n\t%v: other declaration of %s", name, oldPos, name)
}
}
p.useName(name)
}
if isGopFunc(name) {
p.isGopPkg = true
}
if token.IsExported(name) {
p.expObjTypes = append(p.expObjTypes, sig)
}
fn.decl = &ast.FuncDecl{}
p.file.decls = append(p.file.decls, fn.decl)
return fn, nil
}
func (p *Package) newClosure(sig *types.Signature) *Func {
fn := types.NewFunc(token.NoPos, p.Types, "", sig)
return &Func{Func: fn}
}
func (p *Package) newInlineClosure(sig *types.Signature, arity int) *Func {
fn := types.NewFunc(token.NoPos, p.Types, "", sig)
return &Func{Func: fn, arity1: arity + 1}
}
func (p *Func) isInline() bool {
return p.arity1 != 0
}
func (p *Func) getInlineCallArity() int {
return p.arity1 - 1
}
// ----------------------------------------------------------------------------
type Element = internal.Elem
type InstrFlags token.Pos
const (
InstrFlagEllipsis InstrFlags = 1 << iota
InstrFlagTwoValue
instrFlagApproxType // restricts to all types whose underlying type is T
instrFlagGopxFunc // call Gopx_xxx function
instrFlagGoptFunc // call Gopt_xxx function
instrFlagOpFunc // from callOpFunc
instrFlagBinaryOp // from cb.BinaryOp
)
type Instruction interface {
Call(pkg *Package, args []*Element, flags InstrFlags, src ast.Node) (ret *Element, err error)
}
// ----------------------------------------------------------------------------