-
Notifications
You must be signed in to change notification settings - Fork 547
/
value.go
523 lines (447 loc) · 11.4 KB
/
value.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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/*
Copyright 2020 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 (
"math"
"reflect"
"strconv"
"github.com/goplus/gop/ast/astutil"
"github.com/goplus/gop/exec.spec"
"github.com/qiniu/x/log"
)
type iKind = astutil.ConstKind
// iValue represents a Go+ value(s).
// - *goFunc
// - *goValue
// - *nonValue
// - *constVal
// - *funcResult
type iValue interface {
Type() reflect.Type
Kind() iKind
Value(i int) iValue
NumValues() int
}
func isBool(v iValue) bool {
return v.NumValues() == 1 && v.Type() == exec.TyBool
}
// -----------------------------------------------------------------------------
type goValue struct {
t reflect.Type
}
func (p *goValue) Kind() iKind {
return kindOf(p.t)
}
func (p *goValue) Type() reflect.Type {
return p.t
}
func (p *goValue) NumValues() int {
return 1
}
func (p *goValue) Value(i int) iValue {
return p
}
// -----------------------------------------------------------------------------
type nonValue struct {
v interface{} // *exec.GoPackage, goInstr, iType, etc.
}
func (p *nonValue) Kind() iKind {
return reflect.Invalid
}
func (p *nonValue) Type() reflect.Type {
return nil
}
func (p *nonValue) NumValues() int {
return 0
}
func (p *nonValue) Value(i int) iValue {
return p
}
// -----------------------------------------------------------------------------
type wrapValue struct {
x iValue
}
func (p *wrapValue) Type() reflect.Type {
if p.x.NumValues() != 2 {
panic("don't call me")
}
return p.x.Value(0).Type()
}
func (p *wrapValue) Kind() iKind {
if p.x.NumValues() != 2 {
panic("don't call me")
}
return p.x.Value(0).Kind()
}
func (p *wrapValue) NumValues() int {
return p.x.NumValues() - 1
}
func (p *wrapValue) Value(i int) iValue {
return p.x.Value(i)
}
// -----------------------------------------------------------------------------
type funcResults struct {
tfn reflect.Type
}
func (p *funcResults) Kind() iKind {
panic("don't call me")
}
func (p *funcResults) Type() reflect.Type {
panic("don't call me")
}
func (p *funcResults) NumValues() int {
return p.tfn.NumOut()
}
func (p *funcResults) Value(i int) iValue {
return &goValue{t: p.tfn.Out(i)}
}
func newFuncResults(tfn reflect.Type) iValue {
if tfn.NumOut() == 1 {
return &goValue{t: tfn.Out(0)}
}
return &funcResults{tfn: tfn}
}
// -----------------------------------------------------------------------------
type qlFunc funcDecl
func newQlFunc(f *funcDecl) *qlFunc {
return (*qlFunc)(f)
}
func (p *qlFunc) FuncInfo() exec.FuncInfo {
return ((*funcDecl)(p)).Get()
}
func (p *qlFunc) Kind() iKind {
return reflect.Func
}
func (p *qlFunc) Type() reflect.Type {
return ((*funcDecl)(p)).Type()
}
func (p *qlFunc) NumValues() int {
return 1
}
func (p *qlFunc) Value(i int) iValue {
return p
}
func (p *qlFunc) Results() iValue {
return newFuncResults(p.Type())
}
func (p *qlFunc) Proto() iFuncType {
return p.Type()
}
// -----------------------------------------------------------------------------
type goFunc struct {
t reflect.Type
addr uint32
kind exec.SymbolKind
isMethod int // 0 - global func, 1 - method
}
func newGoFunc(addr uint32, kind exec.SymbolKind, isMethod int, ctx *blockCtx) *goFunc {
var t reflect.Type
switch kind {
case exec.SymbolFunc:
t = ctx.GetGoFuncType(exec.GoFuncAddr(addr))
case exec.SymbolFuncv:
t = ctx.GetGoFuncvType(exec.GoFuncvAddr(addr))
default:
log.Panicln("getGoFunc: unknown -", kind, addr)
}
return &goFunc{t: t, addr: addr, kind: kind, isMethod: isMethod}
}
func (p *goFunc) Kind() iKind {
return reflect.Func
}
func (p *goFunc) Type() reflect.Type {
return p.t
}
func (p *goFunc) NumValues() int {
return 1
}
func (p *goFunc) Value(i int) iValue {
return p
}
func (p *goFunc) Results() iValue {
return newFuncResults(p.t)
}
func (p *goFunc) Proto() iFuncType {
return p.t
}
// -----------------------------------------------------------------------------
// isConstBound checks a const is bound or not.
func isConstBound(kind astutil.ConstKind) bool {
return astutil.IsConstBound(kind)
}
type constVal struct {
v interface{}
kind iKind
reserve exec.Reserved
}
func newConstVal(v interface{}, kind iKind) *constVal {
return &constVal{v: v, kind: kind, reserve: exec.InvalidReserved}
}
func (p *constVal) Kind() iKind {
return p.kind
}
func (p *constVal) Type() reflect.Type {
if isConstBound(p.kind) {
return exec.TypeFromKind(p.kind)
}
panic("don't call constVal.TypeOf: unbounded")
}
func (p *constVal) NumValues() int {
return 1
}
func (p *constVal) Value(i int) iValue {
return p
}
func (p *constVal) boundKind() reflect.Kind {
if isConstBound(p.kind) {
return p.kind
}
switch p.kind {
case astutil.ConstUnboundInt:
if _, ok := p.v.(int64); ok {
return reflect.Int
}
return reflect.Uint
case astutil.ConstUnboundFloat:
return reflect.Float64
case astutil.ConstUnboundComplex:
return reflect.Complex128
}
log.Panicln("boundKind: unexpected type kind -", p.kind)
return reflect.Invalid
}
func (p *constVal) boundType() reflect.Type {
return exec.TypeFromKind(p.boundKind())
}
func boundType(in iValue) reflect.Type {
if v, ok := in.(*constVal); ok {
return v.boundType()
}
return in.Type()
}
func (p *constVal) bound(t reflect.Type, b exec.Builder) {
kind := t.Kind()
if p.reserve == exec.InvalidReserved { // bounded
if p.kind != kind {
if t == exec.TyEmptyInterface {
return
}
log.Panicln("function call with invalid argument type: requires", t, ", but got", p.kind)
}
return
}
v := boundConst(p.v, t)
p.v, p.kind = v, kind
p.reserve.Push(b, v)
}
func unaryOp(op exec.Operator, x *constVal) *constVal {
i := op.GetInfo()
xkind := x.kind
var kindReal astutil.ConstKind
if isConstBound(xkind) {
kindReal = xkind
} else {
kindReal = realKindOf(xkind)
}
if (i.InFirst & (1 << kindReal)) == 0 {
log.Panicln("unaryOp failed: invalid argument type.")
}
t := exec.TypeFromKind(kindReal)
vx := boundConst(x.v, t)
v := CallBuiltinOp(kindReal, op, vx)
return &constVal{kind: xkind, v: v, reserve: -1}
}
func binaryOp(op exec.Operator, x, y *constVal) *constVal {
i := op.GetInfo()
xkind := x.kind
ykind := y.kind
var kind, kindReal astutil.ConstKind
if isConstBound(xkind) {
kind, kindReal = xkind, xkind
} else if isConstBound(ykind) {
kind, kindReal = ykind, ykind
} else if xkind < ykind {
kind, kindReal = ykind, realKindOf(ykind)
} else {
kind, kindReal = xkind, realKindOf(xkind)
}
if (i.InFirst & (1 << kindReal)) == 0 {
if kindReal != exec.BigInt && op != exec.OpQuo {
log.Panicln("binaryOp failed: invalid first argument type -", i, kindReal)
}
kind = exec.BigRat
} else if i.Out != exec.SameAsFirst {
kind = i.Out
}
t := exec.TypeFromKind(kindReal)
vx := boundConst(x.v, t)
vy := boundConst(y.v, t)
v := CallBuiltinOp(kindReal, op, vx, vy)
return &constVal{kind: kind, v: v, reserve: -1}
}
func kindOf(t reflect.Type) exec.Kind {
kind := t.Kind()
if kind == reflect.Ptr {
switch t {
case exec.TyBigRat:
return exec.BigRat
case exec.TyBigInt:
return exec.BigInt
case exec.TyBigFloat:
return exec.BigFloat
}
}
return kind
}
const (
intSize = strconv.IntSize
)
func isOverflowsIntByInt64(v int64, intSize int) bool {
if intSize == 32 {
return v < int64(math.MinInt32) || v > int64(math.MaxInt32)
}
return false
}
func isOverflowsIntByUint64(v uint64, intSize int) bool {
if intSize == 32 {
return v > uint64(math.MaxInt32)
}
return v > uint64(math.MaxInt64)
}
func boundConst(v interface{}, t reflect.Type) interface{} {
kind := kindOf(t)
if v == nil {
if kind >= reflect.Chan && kind <= reflect.Slice {
return reflect.Zero(t).Interface()
}
log.Panicln("boundConst: can't convert nil into", t)
}
sval := reflect.ValueOf(v)
st := sval.Type()
if t == st {
return v
}
if kind == reflect.Interface {
skind := sval.Kind()
if skind == reflect.Uint64 {
v := sval.Uint()
if isOverflowsIntByUint64(v, intSize) {
log.Panicf("constant %v overflows int\n", v)
}
return int(v)
} else if skind == reflect.Int64 {
v := sval.Int()
if isOverflowsIntByInt64(v, intSize) {
log.Panicf("constant %v overflows int\n", v)
}
return int(v)
}
} else if kind == reflect.Complex128 || kind == reflect.Complex64 {
if skind := sval.Kind(); skind >= reflect.Int && skind <= reflect.Float64 {
fval := sval.Convert(exec.TyFloat64).Float()
return complex(fval, 0)
}
} else if kind >= exec.BigInt {
val := reflect.New(t.Elem())
skind := kindOf(st)
switch {
case skind >= reflect.Int && skind <= reflect.Int64:
sval = sval.Convert(exec.TyInt64)
val.MethodByName("SetInt64").Call([]reflect.Value{sval})
case skind >= reflect.Uint && skind <= reflect.Uintptr:
sval = sval.Convert(exec.TyUint64)
val.MethodByName("SetUint64").Call([]reflect.Value{sval})
case skind >= reflect.Float32 && skind <= reflect.Float64:
sval = sval.Convert(exec.TyFloat64)
val.MethodByName("SetFloat64").Call([]reflect.Value{sval})
case skind == exec.BigInt:
val.MethodByName("SetInt").Call([]reflect.Value{sval})
case skind == exec.BigFloat:
val.MethodByName("SetRat").Call([]reflect.Value{sval})
default:
log.Panicln("boundConst: convert type failed -", skind)
}
return val.Interface()
}
return sval.Convert(t).Interface()
}
func constIsConvertible(v interface{}, t reflect.Type) bool {
styp := reflect.TypeOf(v)
skind := styp.Kind()
switch kind := t.Kind(); kind {
case reflect.String:
return skind == reflect.String
case reflect.Complex128, reflect.Complex64:
return skind >= reflect.Int && skind <= reflect.Complex128
}
return styp.ConvertibleTo(t)
}
func realKindOf(kind astutil.ConstKind) reflect.Kind {
switch kind {
case astutil.ConstUnboundInt:
return reflect.Int64
case astutil.ConstUnboundFloat:
return reflect.Float64
case astutil.ConstUnboundComplex:
return reflect.Complex128
default:
return kind
}
}
func boundElementType(elts []interface{}, base, max, step int) reflect.Type {
var tBound reflect.Type
var kindUnbound iKind
for i := base; i < max; i += step {
e := elts[i].(iValue)
if e.NumValues() != 1 {
log.Panicln("boundElementType: unexpected - multiple return values.")
}
kind := e.Kind()
if !isConstBound(kind) { // unbound
if kindUnbound < kind {
kindUnbound = kind
}
} else {
if t := e.Type(); tBound != t {
if tBound != nil { // mismatched type
return nil
}
tBound = t
}
}
}
if tBound != nil {
for i := base; i < max; i += step {
if e, ok := elts[i].(*constVal); ok {
if !constIsConvertible(e.v, tBound) { // mismatched type
return nil
}
}
}
return tBound
}
var kindBound iKind
for i := base; i < max; i += step {
if e, ok := elts[i].(*constVal); ok && e.kind == kindUnbound {
kind := e.boundKind()
if kind != kindBound {
if kindBound != 0 { // mismatched type
return nil
}
kindBound = kind
}
}
}
return exec.TypeFromKind(kindBound)
}
// -----------------------------------------------------------------------------