-
Notifications
You must be signed in to change notification settings - Fork 93
/
reflect.go
222 lines (196 loc) · 5.31 KB
/
reflect.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
/*
* 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/.
*
*
* reflect.go
*
* Created on: Apr 11, 2017
* Author: Massimiliano Ghilardi
*/
package reflect
import (
r "reflect"
xr "github.com/cosmos72/gomacro/xreflect"
)
type none struct{}
var (
NilR = r.Value{}
Nil = xr.MakeValue(NilR)
NoneR = r.ValueOf(none{}) // used to indicate "no value"
None = xr.MakeValue(NoneR)
TypeOfInt = r.TypeOf(int(0))
TypeOfInt8 = r.TypeOf(int8(0))
TypeOfInt16 = r.TypeOf(int16(0))
TypeOfInt32 = r.TypeOf(int32(0))
TypeOfInt64 = r.TypeOf(int64(0))
TypeOfUint = r.TypeOf(uint(0))
TypeOfUint8 = r.TypeOf(uint8(0))
TypeOfUint16 = r.TypeOf(uint16(0))
TypeOfUint32 = r.TypeOf(uint32(0))
TypeOfUint64 = r.TypeOf(uint64(0))
TypeOfUintptr = r.TypeOf(uintptr(0))
TypeOfFloat32 = r.TypeOf(float32(0))
TypeOfFloat64 = r.TypeOf(float64(0))
TypeOfComplex64 = r.TypeOf(complex64(0))
TypeOfComplex128 = r.TypeOf(complex128(0))
TypeOfBool = r.TypeOf(false)
TypeOfString = r.TypeOf("")
)
func Category(k r.Kind) r.Kind {
switch k {
case r.Int, r.Int8, r.Int16, r.Int32, r.Int64:
return r.Int
case r.Uint, r.Uint8, r.Uint16, r.Uint32, r.Uint64, r.Uintptr:
return r.Uint
case r.Float32, r.Float64:
return r.Float64
case r.Complex64, r.Complex128:
return r.Complex128
default:
return k
}
}
func IsCategory(k r.Kind, categories ...r.Kind) bool {
k = Category(k)
for _, c := range categories {
if k == c {
return true
}
}
return false
}
// IsOptimizedKind returns true if fast interpreter expects optimized expressions for given Kind
func IsOptimizedKind(k r.Kind) bool {
switch k {
case r.Bool, r.Int, r.Int8, r.Int16, r.Int32, r.Int64,
r.Uint, r.Uint8, r.Uint16, r.Uint32, r.Uint64, r.Uintptr,
r.Float32, r.Float64, r.Complex64, r.Complex128, r.String:
return true
}
return false
}
var kindToType = [...]r.Type{
r.Bool: TypeOfBool,
r.Int: TypeOfInt,
r.Int8: TypeOfInt8,
r.Int16: TypeOfInt16,
r.Int32: TypeOfInt32,
r.Int64: TypeOfInt64,
r.Uint: TypeOfUint,
r.Uint8: TypeOfUint8,
r.Uint16: TypeOfUint16,
r.Uint32: TypeOfUint32,
r.Uint64: TypeOfUint64,
r.Uintptr: TypeOfUintptr,
r.Float32: TypeOfFloat32,
r.Float64: TypeOfFloat64,
r.Complex64: TypeOfComplex64,
r.Complex128: TypeOfComplex128,
r.String: TypeOfString,
}
func KindToType(k r.Kind) r.Type {
if int(k) < len(kindToType) {
return kindToType[k]
}
return nil
}
// ConvertValue converts a value to type t and returns the converted value.
// extends reflect.Value.Convert(t) by allowing conversions from/to complex numbers.
// does not check for overflows or truncation.
func ConvertValue(v xr.Value, to r.Type) xr.Value {
return xr.MakeValue(ConvertValueR(v.ReflectValue(), to))
}
// ConvertValueR converts a value to type t and returns the converted value.
// extends reflect.Value.Convert(t) by allowing conversions from/to complex numbers.
// does not check for overflows or truncation.
func ConvertValueR(v r.Value, to r.Type) r.Value {
t := ValueTypeR(v)
if t == to {
return v
}
if !t.ConvertibleTo(to) {
// reflect.Value does not allow conversions from/to complex types
k := v.Kind()
kto := to.Kind()
if IsCategory(kto, r.Complex128) {
if IsCategory(k, r.Int, r.Uint, r.Float64) {
temp := v.Convert(TypeOfFloat64).Float()
v = r.ValueOf(complex(temp, 0.0))
}
} else if IsCategory(k, r.Complex128) {
if IsCategory(k, r.Int, r.Uint, r.Float64) {
temp := real(v.Complex())
v = r.ValueOf(temp)
}
}
}
return v.Convert(to)
}
func PackValuesR(val0 r.Value, values []r.Value) []r.Value {
if len(values) == 0 && val0 != NoneR {
values = []r.Value{val0}
}
return values
}
func PackValues(val0 xr.Value, values []xr.Value) []xr.Value {
if len(values) == 0 && val0 != None {
values = []xr.Value{val0}
}
return values
}
func PackTypes(typ0 xr.Type, types []xr.Type) []xr.Type {
if len(types) == 0 && typ0 != nil {
types = []xr.Type{typ0}
}
return types
}
func UnpackValues(vals []r.Value) (r.Value, []r.Value) {
val0 := NoneR
if len(vals) > 0 {
val0 = vals[0]
}
return val0, vals
}
// ValueInterfaceR() is a zero-value-safe version of reflect.Value.Interface()
func ValueInterfaceR(v r.Value) interface{} {
if !v.IsValid() || !v.CanInterface() || v == NoneR {
return nil
}
return v.Interface()
}
// ValueInterface() is a zero-value-safe version of xreflect.Value.Interface()
func ValueInterface(v xr.Value) interface{} {
if !v.IsValid() || !v.CanInterface() || v == None {
return nil
}
return v.Interface()
}
// ValueTypeR() is a zero-value-safe version of reflect.Value.Type()
func ValueTypeR(value r.Value) r.Type {
if !value.IsValid() || value == NoneR {
return nil
}
return value.Type()
}
// ValueType() is a zero-value-safe version of xreflect.Value.Type()
func ValueType(value xr.Value) r.Type {
if !value.IsValid() || value == None {
return nil
}
return value.Type()
}
func IsNillableKind(k r.Kind) bool {
switch k {
case r.Invalid, // nil is nillable...
r.Chan, r.Func, r.Interface, r.Map, r.Ptr, r.Slice:
return true
default:
return false
}
}