-
Notifications
You must be signed in to change notification settings - Fork 546
/
func_type_and_var.go
253 lines (225 loc) · 6.37 KB
/
func_type_and_var.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
/*
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 cl
import (
"go/constant"
"go/types"
"log"
"reflect"
"strconv"
"github.com/goplus/gop/ast"
"github.com/goplus/gop/token"
"github.com/goplus/gox"
)
// -----------------------------------------------------------------------------
func toFuncType(ctx *blockCtx, typ *ast.FuncType, recv *types.Var) *types.Signature {
params, variadic := toParams(ctx, typ.Params.List)
results := toResults(ctx, typ.Results)
return types.NewSignature(recv, params, results, variadic)
}
func toRecv(ctx *blockCtx, recv *ast.FieldList) *types.Var {
v := recv.List[0]
return ctx.pkg.NewParam(v.Names[0].Name, toType(ctx, v.Type))
}
func getRecvTypeName(recv *ast.FieldList) string {
typ := recv.List[0].Type
if t, ok := typ.(*ast.StarExpr); ok {
typ = t.X
}
return typ.(*ast.Ident).Name
}
func toResults(ctx *blockCtx, in *ast.FieldList) *types.Tuple {
if in == nil {
return nil
}
flds := in.List
n := len(flds)
args := make([]*types.Var, 0, n)
for _, fld := range flds {
args = toParam(ctx, fld, args)
}
return types.NewTuple(args...)
}
func toParams(ctx *blockCtx, flds []*ast.Field) (typ *types.Tuple, variadic bool) {
n := len(flds)
if n == 0 {
return nil, false
}
args := make([]*types.Var, 0, n)
for _, fld := range flds {
args = toParam(ctx, fld, args)
}
_, ok := flds[n-1].Type.(*ast.Ellipsis)
return types.NewTuple(args...), ok
}
func toParam(ctx *blockCtx, fld *ast.Field, args []*gox.Param) []*gox.Param {
typ := toType(ctx, fld.Type)
pkg := ctx.pkg
if len(fld.Names) == 0 {
return append(args, pkg.NewParam("", typ))
}
for _, name := range fld.Names {
args = append(args, pkg.NewParam(name.Name, typ))
}
return args
}
// -----------------------------------------------------------------------------
func toType(ctx *blockCtx, typ ast.Expr) types.Type {
switch v := typ.(type) {
case *ast.Ident:
return toIdentType(ctx, v.Name)
case *ast.StarExpr:
elem := toType(ctx, v.X)
return types.NewPointer(elem)
case *ast.ArrayType:
return toArrayType(ctx, v)
case *ast.InterfaceType:
return toInterfaceType(ctx, v)
case *ast.Ellipsis:
elem := toType(ctx, v.Elt)
return types.NewSlice(elem)
case *ast.MapType:
return toMapType(ctx, v)
case *ast.StructType:
return toStructType(ctx, v)
case *ast.ChanType:
return toChanType(ctx, v)
case *ast.FuncType:
return toFuncType(ctx, v, nil)
/* case *ast.SelectorExpr:
return toExternalType(ctx, v)
*/
}
log.Panicln("toType: unknown -", reflect.TypeOf(typ))
return nil
}
var (
typesChanDirs = [...]types.ChanDir{
ast.RECV: types.RecvOnly,
ast.SEND: types.SendOnly,
ast.SEND | ast.RECV: types.SendRecv,
}
)
func toIdentType(ctx *blockCtx, ident string) types.Type {
v, _ := lookupParent(ctx, ident)
if v == nil {
tyName := gopPrefix + ident
if v = ctx.pkg.Builtin().Types.Scope().Lookup(tyName); v == nil {
log.Panicln("TODO: symbol not found -", ident)
}
}
if t, ok := v.(*types.TypeName); ok {
return t.Type()
}
panic("TODO: not a type")
}
func toChanType(ctx *blockCtx, v *ast.ChanType) *types.Chan {
return types.NewChan(typesChanDirs[v.Dir], toType(ctx, v.Value))
}
func toStructType(ctx *blockCtx, v *ast.StructType) *types.Struct {
pkg := ctx.pkg.Types
fieldList := v.Fields.List
fields := make([]*types.Var, 0, len(fieldList))
tags := make([]string, 0, len(fieldList))
for _, field := range fieldList {
typ := toType(ctx, field.Type)
if field.Names == nil { // embedded
fld := types.NewField(token.NoPos, pkg, getTypeName(typ), typ, true)
fields = append(fields, fld)
tags = append(tags, toFieldTag(field.Tag))
continue
}
for _, name := range field.Names {
fld := types.NewField(token.NoPos, pkg, name.Name, typ, false)
fields = append(fields, fld)
tags = append(tags, toFieldTag(field.Tag))
}
}
return types.NewStruct(fields, tags)
}
func toFieldTag(v *ast.BasicLit) string {
if v != nil {
tag, err := strconv.Unquote(v.Value)
if err != nil {
log.Panicln("TODO: toFieldTag -", err)
}
return tag
}
return ""
}
func getTypeName(typ types.Type) string {
switch t := typ.(type) {
case *types.Named:
return t.Obj().Name()
default:
panic("TODO: getTypeName")
}
}
func toMapType(ctx *blockCtx, v *ast.MapType) *types.Map {
key := toType(ctx, v.Key)
val := toType(ctx, v.Value)
return types.NewMap(key, val)
}
func toArrayType(ctx *blockCtx, v *ast.ArrayType) types.Type {
elem := toType(ctx, v.Elt)
if v.Len == nil {
return types.NewSlice(elem)
}
if _, ok := v.Len.(*ast.Ellipsis); ok {
return types.NewArray(elem, -1) // A negative length indicates an unknown length
}
return types.NewArray(elem, toInt64(ctx, v.Len))
}
func toInt64(ctx *blockCtx, e ast.Expr) int64 {
cb := ctx.pkg.ConstStart()
compileExpr(ctx, e)
tv := cb.EndConst()
if v, ok := constant.Int64Val(tv.Value); ok {
return v
}
panic("TODO: require integer constant")
}
func toInterfaceType(ctx *blockCtx, v *ast.InterfaceType) types.Type {
methodsList := v.Methods.List
if methodsList == nil {
return types.NewInterfaceType(nil, nil)
}
var pkg = ctx.pkg.Types
var methods []*types.Func
var embeddeds []types.Type
for _, m := range methodsList {
if m.Type == nil { // embedded
panic("TODO: embedded")
}
name := m.Names[0].Name
typ, ok := m.Type.(*ast.FuncType)
if !ok {
panic("TODO: not function type")
}
sig := toFuncType(ctx, typ, nil)
methods = append(methods, types.NewFunc(token.NoPos, pkg, name, sig))
}
intf := types.NewInterfaceType(methods, embeddeds).Complete()
return intf
}
// -----------------------------------------------------------------------------
func toString(l *ast.BasicLit) string {
if l.Kind == token.STRING {
s, err := strconv.Unquote(l.Value)
if err == nil {
return s
}
}
panic("TODO: toString - convert ast.BasicLit to string failed")
}
// -----------------------------------------------------------------------------