-
Notifications
You must be signed in to change notification settings - Fork 0
/
type.go
387 lines (361 loc) · 7.91 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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package main
import (
"fmt"
"go/ast"
"github.com/rs/zerolog/log"
)
type Kind uint
const (
Invalid Kind = iota
Bool
Int
Int8
Int16
Int32
Int64
Uint
Uint8
Uint16
Uint32
Uint64
Uintptr
Float32
Float64
Complex64
Complex128
Array
Chan
Func
Interface
Map
Pointer
Slice
String
Struct
UnsafePointer
None
Error
CPyObjectPointer
)
type GoType struct {
T Kind
SliceElemType *GoType
MapKeyType *GoType
MapValType *GoType
PointerTo *GoType
GoRepr string
}
func ToKind(v string) Kind {
switch v {
case "int":
return Int
case "int8":
return Int8
case "int16":
return Int16
case "int32":
return Int32
case "int64":
return Int64
case "uint":
return Uint
case "uint8":
return Uint8
case "uint16":
return Uint16
case "uint32":
return Uint32
case "uint64":
return Uint64
case "bool":
return Bool
case "error":
return Error
case "float32":
return Float32
case "float64":
return Float64
case "complex64":
return Complex64
case "complex128":
return Complex128
case "string":
return String
}
log.Fatal().Caller().Msgf("Type '%s' not supported!", v)
panic("")
}
func IsCPyObjectPtr(expr ast.Expr) bool {
sta, ok := expr.(*ast.StarExpr)
if !ok {
return false
}
sel, ok := sta.X.(*ast.SelectorExpr)
if !ok {
return false
}
ide, ok := sel.X.(*ast.Ident)
if !ok {
return false
}
if ide.Name == "C" && sel.Sel.Name == "PyObject" {
return true
}
return false
}
func AsGoType(expr ast.Expr, context []byte) (*GoType, error) {
switch v := expr.(type) {
case *ast.Ident:
return &GoType{
T: ToKind(v.Name),
GoRepr: GetSourceString(context, expr),
}, nil
case *ast.StarExpr:
if IsCPyObjectPtr(v) {
return &GoType{T: CPyObjectPointer}, nil
} else {
return &GoType{
T: Pointer,
GoRepr: GetSourceString(context, expr)[1:],
}, nil
}
case *ast.ArrayType:
elt, err := AsGoType(v.Elt, context)
if err != nil {
return nil, err
}
return &GoType{
T: Slice,
SliceElemType: elt,
GoRepr: GetSourceString(context, expr),
}, nil
case *ast.MapType:
mapKeyType, err := AsGoType(v.Key, context)
if err != nil {
return nil, err
}
mapValType, err := AsGoType(v.Value, context)
if err != nil {
return nil, err
}
return &GoType{
T: Map,
MapKeyType: mapKeyType,
MapValType: mapValType,
GoRepr: GetSourceString(context, expr),
}, nil
default:
return nil, fmt.Errorf("Type '%s' (%T) not supported!", GetSourceString(context, expr), expr)
}
}
func (g *GoType) Unsupported() {
if g.GoRepr == "" {
log.Fatal().Caller(1).Msgf("Type '%+v' not supported", g)
} else {
log.Fatal().Caller(1).Msgf("Type '%s' not supported", g.GoRepr)
}
}
func (g *GoType) PyArgFormat() string {
// Returns the format used for PyArg_ParseTupleAndKeywords()
// https://docs.python.org/3/c-api/arg.html
switch g.T {
case Bool:
return "p"
case Int:
return "i"
case Int32:
return "l"
case Uint32:
return "k"
case Int64:
return "L"
case Uint64:
return "K"
case Float32:
return "f"
case Float64:
return "d"
case Complex64, Complex128:
return "D"
case String:
return "s"
case Map, Slice, CPyObjectPointer:
return "O"
}
g.Unsupported()
panic("")
}
func (g *GoType) GoCType() string {
switch g.T {
case Int, Bool:
return "C.int"
case Int8:
return "C.int8_t"
case Uint8:
return "C.uint8_t"
case Int16:
return "C.int16_t"
case Uint16:
return "C.uint16_t"
case Int32:
return "C.int32_t"
case Uint32:
return "C.uint32_t"
case Int64:
return "C.int64_t"
case Uint64:
return "C.uint64_t"
case Float32:
return "C.float"
case Float64:
return "C.double"
case Complex64, Complex128:
return "C.Py_complex"
case String:
return "*C.char"
case Map, Slice, CPyObjectPointer:
return "*C.PyObject"
}
g.Unsupported()
panic("")
}
func (g *GoType) CPtrType() string {
switch g.T {
case Int, Bool:
return "int *"
case Int32:
return "int32_t *"
case Int64:
return "int64_t *"
case Uint32:
return "uint32_t *"
case Uint64:
return "uint64_t *"
case Float32:
return "float *"
case Float64:
return "double *"
case Complex64, Complex128:
return "Py_complex *"
case String:
return "char **"
case Map, Slice, CPyObjectPointer:
return "PyObject **"
}
g.Unsupported()
panic("")
}
func (g *GoType) PythonTypeHint() string {
switch g.T {
case None:
return "NoneType"
case Int, Int8, Int16, Int32, Int64, Uint, Uint8, Uint16, Uint32, Uint64:
return "int"
case String:
return "str"
case Bool:
return "bool"
case Map:
return fmt.Sprintf("Dict[%s, %s]", g.MapKeyType.PythonTypeHint(), g.MapValType.PythonTypeHint())
case CPyObjectPointer:
return "object"
case Pointer:
return g.GoRepr
case Float32, Float64:
return "float"
case Complex64, Complex128:
return "complex"
case Slice:
return fmt.Sprintf("List[%s]", g.SliceElemType.PythonTypeHint())
}
g.Unsupported()
panic("")
}
func (g *GoType) GoPyReturn(varname string) string {
switch g.T {
case None: // Equivalent of Python's None
return "return C.Py_None"
case CPyObjectPointer:
return fmt.Sprintf("return %s", varname)
case Bool:
return fmt.Sprintf("return asPyBool(%s)", varname)
case Int, Int8, Int16, Int32, Int64, Uint, Uint8, Uint16, Uint32, Uint64:
return fmt.Sprintf("return asPyLong(%s)", varname)
case Float32, Float64:
return fmt.Sprintf("return asPyFloat(%s)", varname)
case Complex64:
return fmt.Sprintf("return goComplex64AsPyComplex(%s)", varname)
case Complex128:
return fmt.Sprintf("return goComplex128AsPyComplex(%s)", varname)
case String:
return fmt.Sprintf("return asPyString(%s)", varname)
case Error:
return fmt.Sprintf("return asPyError(%s)", varname)
case Pointer:
return fmt.Sprintf("return %sToPyObject(%s)", g.GoRepr, varname)
case Map:
return fmt.Sprintf("return asPyDict(%s, %s, %s)", varname,
g.MapKeyType.GoPyReturnLambda(),
g.MapValType.GoPyReturnLambda())
case Slice:
return fmt.Sprintf("return asPyList(%s, %s)", varname, g.SliceElemType.GoPyReturnLambda())
}
g.Unsupported()
panic("")
}
func (g *GoType) GoPyReturnLambda() string {
switch g.T {
case Pointer:
return fmt.Sprintf("func(v *%s) *C.PyObject { %s }", g.GoRepr, g.GoPyReturn("v"))
default:
return fmt.Sprintf("func(v %s) *C.PyObject { %s }", g.GoRepr, g.GoPyReturn("v"))
}
}
func (g *GoType) CToGoFunction(varname string) string {
switch g.T {
case CPyObjectPointer:
return varname
case Bool:
return fmt.Sprintf("asGoBool(%s)", varname)
case Int, Int8, Int16, Int32, Int64, Uint, Uint8, Uint16, Uint32, Uint64, Float32, Float64:
return fmt.Sprintf("%s(%s)", g.GoRepr, varname)
case Complex64:
return fmt.Sprintf("asGoComplex64(%s)", varname)
case Complex128:
return fmt.Sprintf("asGoComplex128(%s)", varname)
case String:
return fmt.Sprintf("C.GoString(%s)", varname)
case Slice:
return fmt.Sprintf("asGoSlice(%s, %s)", varname,
g.SliceElemType.CToGoLambdaFunction())
case Map:
return fmt.Sprintf("asGoMap(%s, %s, %s)", varname,
g.MapKeyType.CPyObjectToGoLambda(),
g.MapValType.CPyObjectToGoLambda())
}
g.Unsupported()
panic("")
}
func (g *GoType) CToGoLambdaFunction() string {
return fmt.Sprintf("func(o %s) %s { return %s }", g.GoCType(), g.GoRepr, g.CToGoFunction("o"))
}
func (g *GoType) CPyObjectToGo(cPyObjectVarName string) string {
switch g.T {
case String:
return fmt.Sprintf("pyObjectAsGoString(%s)", cPyObjectVarName)
case Int, Int8, Int16, Int32, Int64, Uint, Uint8, Uint16, Uint32, Uint64:
// https://docs.python.org/3/c-api/long.html
return fmt.Sprintf("%s(C.PyLong_AsLong(%s))", g.GoRepr, cPyObjectVarName)
case Float32, Float64:
// https://docs.python.org/3/c-api/float.html
return fmt.Sprintf("%s(C.PyFloat_AsDouble(%s))", g.GoRepr, cPyObjectVarName)
}
g.Unsupported()
panic("")
}
func (g *GoType) CPyObjectToGoLambda() string {
return fmt.Sprintf("func (obj *C.PyObject) %s { return %s }", g.GoRepr, g.CPyObjectToGo("obj"))
}
func (g *GoType) IsNotNone() bool {
return g.T != None
}