/
type.go
365 lines (340 loc) · 9.34 KB
/
type.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
/*
* 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/.
*
*
* type.go
*
* Created on: Feb 13, 2017
* Author: Massimiliano Ghilardi
*/
package classic
import (
"fmt"
"go/ast"
r "reflect"
. "github.com/cosmos72/gomacro/base"
"github.com/cosmos72/gomacro/base/genimport"
"github.com/cosmos72/gomacro/base/output"
"github.com/cosmos72/gomacro/base/reflect"
)
func typeOf(value r.Value) r.Type {
if value == NoneR || value == NilR {
return TypeOfInterface
}
return value.Type()
}
func (env *Env) evalExpr1OrType(node ast.Expr) (val r.Value, t r.Type) {
defer func() {
if r := recover(); r != nil {
switch r.(type) {
case output.RuntimeError:
t = env.evalType(node)
default:
panic(r)
}
}
}()
val = env.evalExpr1(node)
return val, nil
}
// evalTypeAlias evaluates a type alias declaration, i.e. type Foo = /*...*/
func (env *Env) evalTypeAlias(name string, node ast.Expr) r.Type {
t := env.evalType(node)
// never define bindings for "_"
if name != "_" {
if _, ok := env.Types.Get(name); ok {
env.Warnf("redefined type alias: %v", name)
} else {
env.Types.Ensure()
}
env.Types.Set(name, t)
}
return t
}
// evalType evaluates a type
func (env *Env) evalType(node ast.Expr) r.Type {
t, _ := env.evalType2(node, false)
return t
}
// evalTypeOrNilR evaluates a type. as a special case used by type switch, evaluates *ast.Ident{Name:"nil"} to nil
func (env *Env) evalTypeOrNilR(node ast.Expr) r.Type {
for {
switch expr := node.(type) {
case *ast.ParenExpr:
node = expr.X
continue
case *ast.Ident:
if expr.Name == "nil" {
val, found := env.resolveIdentifier(expr)
if found && val == NilR {
return nil
}
}
}
break
}
t, _ := env.evalType2(node, false)
return t
}
// evalType0 evaluates a type expression.
// if allowEllipsis is true, it supports the special case &ast.Ellipsis{/*expression*/}
// that represents ellipsis in the last argument of a function declaration.
// The second return value is true both in the case above, and for array types whose length is [...]
func (env *Env) evalType2(node ast.Expr, allowEllipsis bool) (t r.Type, ellipsis bool) {
stars := 0
for {
switch expr := node.(type) {
case *ast.StarExpr:
stars++
node = expr.X
continue
case *ast.ParenExpr:
node = expr.X
continue
case *ast.Ellipsis:
if allowEllipsis {
node = expr.Elt
ellipsis = true
continue
}
}
break
}
if node != nil {
env.Pos = node.Pos()
}
switch node := node.(type) {
case *ast.ArrayType: // also for slices
var ellipsis2 bool
t, ellipsis2 = env.evalTypeArray(node)
if !ellipsis {
ellipsis = ellipsis2
}
case *ast.ChanType:
t = env.evalType(node.Value)
dir := r.BothDir
if node.Dir == ast.SEND {
dir = r.SendDir
} else if node.Dir == ast.RECV {
dir = r.RecvDir
}
t = r.ChanOf(dir, t)
case *ast.FuncType:
t, _, _ = env.evalTypeFunction(node)
case *ast.Ident:
t = env.evalTypeIdentifier(node.Name)
case *ast.InterfaceType:
t = env.evalTypeInterface(node)
case *ast.MapType:
kt := env.evalType(node.Key)
vt := env.evalType(node.Value)
t = r.MapOf(kt, vt)
case *ast.SelectorExpr:
if pkgIdent, ok := node.X.(*ast.Ident); ok {
pkgv := env.evalIdentifier(pkgIdent)
if pkg, ok := pkgv.Interface().(*genimport.PackageRef); ok {
name := node.Sel.Name
if t, ok = pkg.Types[name]; !ok {
env.Errorf("not a type: %v <%v>", node, r.TypeOf(node))
}
} else {
env.Errorf("not a package: %v = %v <%v>", pkgIdent, pkgv, typeOf(pkgv))
}
} else {
env.Errorf("unimplemented qualified type, expecting packageName.identifier: %v <%v>", node, r.TypeOf(node))
}
case *ast.StructType:
// env.Debugf("evalType() struct declaration: %v <%v>", node, r.TypeOf(node))
types, names := env.evalTypeFields(node.Fields)
// env.Debugf("evalType() struct names and types: %v %v", types, names)
fields := makeStructFields(env.FileEnv().Path, names, types)
// env.Debugf("evalType() struct fields: %#v", fields)
t = r.StructOf(fields)
case nil:
// type can be omitted in many case - then we must perform type inference
break
default:
// TODO which types are still missing?
env.Errorf("unimplemented type: %v <%v>", node, r.TypeOf(node))
}
for i := 0; i < stars; i++ {
t = r.PtrTo(t)
}
if allowEllipsis && ellipsis {
t = r.SliceOf(t)
}
return t, ellipsis
}
func (env *Env) evalTypeArray(node *ast.ArrayType) (t r.Type, ellipsis bool) {
t = env.evalType(node.Elt)
n := node.Len
switch n := n.(type) {
case *ast.Ellipsis:
t = r.SliceOf(t)
ellipsis = true
case nil:
t = r.SliceOf(t)
default:
count := env.evalExpr1(n).Int()
t = r.ArrayOf(int(count), t)
}
return t, ellipsis
}
func (env *Env) evalTypeFunction(node *ast.FuncType) (t r.Type, argNames []string, resultNames []string) {
tFunc, _, argNames, resultNames := env.evalTypeFunctionOrMethod(nil, node)
return tFunc, argNames, resultNames
}
func (env *Env) evalTypeFunctionOrMethod(recv *ast.Field, node *ast.FuncType) (tFunc r.Type, tFuncOrMethod r.Type, argNames []string, resultNames []string) {
argTypes, argNames, variadic := env.evalTypeFieldOrParamList(node.Params, true)
resultTypes, resultNames := env.evalTypeFields(node.Results)
tFunc = r.FuncOf(argTypes, resultTypes, variadic)
if recv != nil {
recvTypes, recvNames, _ := env.evalTypeFieldsOrParams([]*ast.Field{recv}, false)
argTypes = append(recvTypes, argTypes...)
argNames = append(recvNames, argNames...)
tFuncOrMethod = r.FuncOf(argTypes, resultTypes, variadic)
} else {
tFuncOrMethod = tFunc
}
return tFunc, tFuncOrMethod, argNames, resultNames
}
func (env *Env) evalTypeFields(fields *ast.FieldList) (types []r.Type, names []string) {
types, names, _ = env.evalTypeFieldOrParamList(fields, false)
return types, names
}
func (env *Env) evalTypeFieldOrParamList(fields *ast.FieldList, allowEllipsis bool) (types []r.Type, names []string, ellipsis bool) {
var list []*ast.Field
if fields != nil {
list = fields.List
}
return env.evalTypeFieldsOrParams(list, allowEllipsis)
}
func (env *Env) evalTypeFieldsOrParams(list []*ast.Field, allowEllipsis bool) (types []r.Type, names []string, ellipsis bool) {
types = make([]r.Type, 0)
names = ZeroStrings
n := len(list)
if n == 0 {
return types, names, ellipsis
}
var t r.Type
for i, f := range list {
t, ellipsis = env.evalType2(f.Type, i == n-1)
if len(f.Names) == 0 {
types = append(types, t)
names = append(names, "_")
// env.Debugf("evalTypeFields() %v -> %v", f.Type, t)
} else {
for _, ident := range f.Names {
types = append(types, t)
names = append(names, ident.Name)
// Debugf("evalTypeFields() %v %v -> %v", ident.Name, f.Type, t)
}
}
}
return types, names, ellipsis
}
func (env *Env) evalTypeIdentifier(name string) r.Type {
for e := env; e != nil; e = e.Outer {
if t, ok := e.Types.Get(name); ok {
return t
}
}
env.Errorf("undefined identifier: %v", name)
return nil
}
func makeStructFields(pkgPath string, names []string, types []r.Type) []r.StructField {
// pkgIdentifier := sanitizeIdentifier(pkgPath)
fields := make([]r.StructField, len(names))
for i, name := range names {
fields[i] = r.StructField{
Name: toExportedName(name), // Go 1.8 reflect.StructOf() supports *only* exported fields
Type: types[i],
Tag: "",
Anonymous: false,
}
}
return fields
}
func toExportedName(name string) string {
if len(name) == 0 {
return name
}
ch := name[0]
if ch >= 'a' && ch <= 'z' {
ch -= 'a' - 'A'
} else if ch == '_' {
ch = 'X'
} else {
return name
}
return fmt.Sprintf("%c%s", ch, name[1:])
}
func (env *Env) valueToType(value r.Value, t r.Type) r.Value {
if value == NoneR || value == NilR {
switch t.Kind() {
case r.Chan, r.Func, r.Interface, r.Map, r.Ptr, r.Slice:
return r.Zero(t)
}
}
newValue := reflect.ConvertValueR(value, t)
if differentIntegerValues(value, newValue) {
env.Warnf("value %d overflows <%v>, truncated to %d", value, t, newValue)
}
return newValue
}
func differentIntegerValues(v1 r.Value, v2 r.Value) bool {
k1, k2 := v1.Kind(), v2.Kind()
switch k1 {
case r.Int, r.Int8, r.Int16, r.Int32, r.Int64:
n1 := v1.Int()
switch k2 {
case r.Int, r.Int8, r.Int16, r.Int32, r.Int64:
return n1 != v2.Int()
case r.Uint, r.Uint8, r.Uint16, r.Uint32, r.Uint64, r.Uintptr:
return n1 < 0 || uint64(n1) != v2.Uint()
default:
return false
}
case r.Uint, r.Uint8, r.Uint16, r.Uint32, r.Uint64, r.Uintptr:
n1 := v1.Uint()
switch k2 {
case r.Int, r.Int8, r.Int16, r.Int32, r.Int64:
n2 := v2.Int()
return n2 < 0 || uint64(n2) != n1
case r.Uint, r.Uint8, r.Uint16, r.Uint32, r.Uint64, r.Uintptr:
return n1 != v2.Uint()
default:
return false
}
default:
return false
}
}
func toValues(args []interface{}) []r.Value {
n := len(args)
values := make([]r.Value, n)
for i := 0; i < n; i++ {
values[i] = r.ValueOf(args[i])
}
return values
}
func toInterfaces(values []r.Value) []interface{} {
n := len(values)
rets := make([]interface{}, n)
for i := 0; i < n; i++ {
rets[i] = toInterface(values[i])
}
return rets
}
func toInterface(value r.Value) interface{} {
if value != NilR && value != NoneR {
return value.Interface()
}
return nil
}