-
Notifications
You must be signed in to change notification settings - Fork 5
/
typeconv.go
338 lines (305 loc) · 9.6 KB
/
typeconv.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
package main
import (
"bytes"
"fmt"
"gobject/gi"
"strings"
)
func cgo_array_to_go_array(elem *gi.TypeInfo, name string) string {
return fmt.Sprintf("(*(*[999999]%s)(unsafe.Pointer(%s)))",
cgo_type(elem, type_none), name)
}
type conv_flags int
const (
conv_none conv_flags = 0
conv_pointer conv_flags = 1 << iota
conv_list_member
conv_own_none
conv_own_container
conv_own_everything
)
func ownership_to_conv_flags(t gi.Transfer) conv_flags {
switch t {
case gi.TRANSFER_NOTHING:
return conv_own_none
case gi.TRANSFER_CONTAINER:
return conv_own_container
case gi.TRANSFER_EVERYTHING:
return conv_own_everything
}
return 0
}
//------------------------------------------------------------------
// Go to Cgo Converter
//------------------------------------------------------------------
func go_to_cgo_for_interface(bi *gi.BaseInfo, arg0, arg1 string, flags conv_flags) string {
var out bytes.Buffer
printf := printer_to(&out)
switch bi.Type() {
case gi.INFO_TYPE_OBJECT:
prefix := gi.DefaultRepository().CPrefix(bi.Namespace())
printf("if %s != nil {\n", arg0)
printf("\t%s = %s.InheritedFrom%s%s()\n",
arg1, arg0, prefix, bi.Name())
printf("}")
case gi.INFO_TYPE_ENUM, gi.INFO_TYPE_FLAGS:
ctype := cgo_type_for_interface(bi, type_none)
printf("%s = %s(%s)", arg1, ctype, arg0)
case gi.INFO_TYPE_INTERFACE:
prefix := gi.DefaultRepository().CPrefix(bi.Namespace())
printf("if %s != nil {\n", arg0)
printf("\t%s = %s.Implements%s%s()\n",
arg1, arg0, prefix, bi.Name())
printf("}")
case gi.INFO_TYPE_STRUCT:
ns := bi.Namespace()
if ns == "cairo" {
printf(cairo_go_to_cgo_for_interface(bi, arg0, arg1, flags))
break
}
fullnm := strings.ToLower(ns) + "." + bi.Name()
if config.is_disguised(fullnm) {
flags &^= conv_pointer
}
ctype := cgo_type_for_interface(bi, type_none)
if flags&conv_pointer != 0 {
printf("%s = (*%s)(unsafe.Pointer(%s))",
arg1, ctype, arg0)
} else {
printf("%s = *(*%s)(unsafe.Pointer(&%s))",
arg1, ctype, arg0)
}
case gi.INFO_TYPE_CALLBACK:
printf("if %s != nil {\n", arg0)
printf("\t%s = unsafe.Pointer(&%s)", arg1, arg0)
printf("}")
}
return out.String()
}
func go_to_cgo(ti *gi.TypeInfo, arg0, arg1 string, flags conv_flags) string {
var out bytes.Buffer
printf := printer_to(&out)
switch tag := ti.Tag(); tag {
case gi.TYPE_TAG_VOID:
if ti.IsPointer() {
printf("%s = unsafe.Pointer(%s)", arg1, arg0)
break
}
printf("<ERROR: void>")
case gi.TYPE_TAG_UTF8, gi.TYPE_TAG_FILENAME:
printf("%s = _GoStringToGString(%s)", arg1, arg0)
if flags&conv_own_everything == 0 {
printf("\ndefer C.free(unsafe.Pointer(%s))", arg1)
}
case gi.TYPE_TAG_ARRAY:
switch ti.ArrayType() {
case gi.ARRAY_TYPE_C:
var nelem string
if ti.IsZeroTerminated() {
nelem = fmt.Sprintf("(len(%s) + 1)", arg0)
} else {
nelem = fmt.Sprintf("len(%s)", arg0)
}
// alloc memory
printf("%s = (%s)(C.malloc(C.size_t(int(unsafe.Sizeof(*%s)) * %s)))\n",
arg1, cgo_type(ti, type_none), arg1, nelem)
printf("defer C.free(unsafe.Pointer(%s))\n", arg1)
// convert elements
printf("for i, e := range %s {\n", arg0)
array := cgo_array_to_go_array(ti.ParamType(0), arg1)
conv := go_to_cgo(ti.ParamType(0), "e", array+"[i]", flags)
printf(print_lines_with_indent(conv))
printf("}")
// write a trailing zero if necessary (TODO: buggy)
if ti.IsZeroTerminated() {
printf("\n%s[len(%s)] = nil", array, arg0)
}
}
case gi.TYPE_TAG_GLIST:
case gi.TYPE_TAG_GSLIST:
case gi.TYPE_TAG_GHASH:
case gi.TYPE_TAG_ERROR:
case gi.TYPE_TAG_INTERFACE:
if ti.IsPointer() {
flags |= conv_pointer
}
printf(go_to_cgo_for_interface(ti.Interface(), arg0, arg1, flags))
default:
if ti.IsPointer() {
flags |= conv_pointer
}
printf(go_to_cgo_for_tag(tag, arg0, arg1, flags))
}
return out.String()
}
func go_to_cgo_for_tag(tag gi.TypeTag, arg0, arg1 string, flags conv_flags) string {
switch tag {
case gi.TYPE_TAG_BOOLEAN:
return fmt.Sprintf("%s = _GoBoolToCBool(%s)", arg1, arg0)
default:
if flags&conv_pointer == 0 {
return fmt.Sprintf("%s = %s(%s)", arg1,
cgo_type_for_tag(tag, type_none), arg0)
} else {
return fmt.Sprintf("%s = (%s)(unsafe.Pointer(%s))", arg1,
cgo_type_for_tag(tag, type_pointer), arg0)
}
}
panic("unreachable")
return ""
}
//------------------------------------------------------------------
// Cgo to Go Converter
//------------------------------------------------------------------
func cgo_to_go_for_interface(bi *gi.BaseInfo, arg1, arg2 string, flags conv_flags) string {
var out bytes.Buffer
printf := printer_to(&out)
switch bi.Type() {
case gi.INFO_TYPE_OBJECT, gi.INFO_TYPE_INTERFACE:
gotype := go_type_for_interface(bi, type_return)
if flags&conv_own_everything != 0 {
printf("%s = (*%s)(%sObjectWrap(unsafe.Pointer(%s), false))",
arg2, gotype, config.gns, arg1)
} else {
printf("%s = (*%s)(%sObjectWrap(unsafe.Pointer(%s), true))",
arg2, gotype, config.gns, arg1)
}
case gi.INFO_TYPE_ENUM, gi.INFO_TYPE_FLAGS:
gotype := go_type_for_interface(bi, type_return)
printf("%s = %s(%s)", arg2, gotype, arg1)
case gi.INFO_TYPE_STRUCT, gi.INFO_TYPE_UNION:
ns := bi.Namespace()
if ns == "cairo" {
printf(cairo_cgo_to_go_for_interface(bi, arg1, arg2, flags))
break
}
fullnm := strings.ToLower(ns) + "." + bi.Name()
gotype := go_type_for_interface(bi, type_return)
if flags&conv_list_member != 0 {
printf("%s = *(*%s)(unsafe.Pointer(%s))",
arg2, gotype, arg1)
break
}
if config.is_disguised(fullnm) {
printf("%s = %s{unsafe.Pointer(%s)}",
arg2, gotype, arg1)
break
}
if flags&conv_pointer != 0 {
printf("%s = (*%s)(unsafe.Pointer(%s))",
arg2, gotype, arg1)
} else {
printf("%s = *(*%s)(unsafe.Pointer(&%s))",
arg2, gotype, arg1)
}
}
return out.String()
}
func cgo_to_go(ti *gi.TypeInfo, arg1, arg2 string, flags conv_flags) string {
var out bytes.Buffer
printf := printer_to(&out)
switch tag := ti.Tag(); tag {
case gi.TYPE_TAG_VOID:
if ti.IsPointer() {
printf("%s = %s", arg2, arg1)
break
}
printf("<ERROR: void>")
case gi.TYPE_TAG_UTF8, gi.TYPE_TAG_FILENAME:
printf("%s = C.GoString(%s)", arg2, arg1)
if flags&conv_own_everything != 0 {
printf("\nC.g_free(unsafe.Pointer(%s))", arg1)
}
case gi.TYPE_TAG_ARRAY:
switch ti.ArrayType() {
case gi.ARRAY_TYPE_C:
// array was allocated already at this point
printf("for i := range %s {\n", arg2)
array := cgo_array_to_go_array(ti.ParamType(0), arg1)
conv := cgo_to_go(ti.ParamType(0),
array+"[i]", arg2+"[i]", flags)
printf(print_lines_with_indent(conv))
printf("}")
}
case gi.TYPE_TAG_GLIST:
ptype := ti.ParamType(0)
printf("for iter := (*_GList)(unsafe.Pointer(%s)); iter != nil; iter = iter.next {\n", arg1)
elt := fmt.Sprintf("(%s)(iter.data)",
force_pointer(cgo_type(ptype, type_return|type_list_member)))
printf("\tvar elt %s\n", go_type(ptype, type_return|type_list_member))
conv := cgo_to_go(ptype, elt, "elt", flags|conv_list_member)
printf(print_lines_with_indent(conv))
printf("\t%s = append(%s, elt)\n", arg2, arg2)
printf("}")
case gi.TYPE_TAG_GSLIST:
ptype := ti.ParamType(0)
printf("for iter := (*_GSList)(unsafe.Pointer(%s)); iter != nil; iter = iter.next {\n", arg1)
elt := fmt.Sprintf("(%s)(iter.data)",
force_pointer(cgo_type(ptype, type_return|type_list_member)))
printf("\tvar elt %s\n", go_type(ptype, type_return|type_list_member))
conv := cgo_to_go(ptype, elt, "elt", flags|conv_list_member)
printf(print_lines_with_indent(conv))
printf("\t%s = append(%s, elt)\n", arg2, arg2)
printf("}")
case gi.TYPE_TAG_GHASH:
case gi.TYPE_TAG_ERROR:
printf("if %s != nil {\n", arg1)
printf("\t%s = errors.New(C.GoString(((*_GError)(unsafe.Pointer(%s))).message))\n", arg2, arg1)
printf("\tC.g_error_free(%s)\n", arg1)
printf("}\n")
case gi.TYPE_TAG_INTERFACE:
if ti.IsPointer() {
flags |= conv_pointer
}
printf(cgo_to_go_for_interface(ti.Interface(), arg1, arg2, flags))
default:
if ti.IsPointer() {
flags |= conv_pointer
}
printf(cgo_to_go_for_tag(tag, arg1, arg2, flags))
}
return out.String()
}
func cgo_to_go_for_tag(tag gi.TypeTag, arg1, arg2 string, flags conv_flags) string {
switch tag {
case gi.TYPE_TAG_BOOLEAN:
return fmt.Sprintf("%s = %s != 0", arg2, arg1)
case gi.TYPE_TAG_INT8:
return fmt.Sprintf("%s = int(%s)", arg2, arg1)
case gi.TYPE_TAG_UINT8:
return fmt.Sprintf("%s = int(%s)", arg2, arg1)
case gi.TYPE_TAG_INT16:
return fmt.Sprintf("%s = int(%s)", arg2, arg1)
case gi.TYPE_TAG_UINT16:
return fmt.Sprintf("%s = int(%s)", arg2, arg1)
case gi.TYPE_TAG_INT32:
return fmt.Sprintf("%s = int(%s)", arg2, arg1)
case gi.TYPE_TAG_UINT32:
return fmt.Sprintf("%s = int(%s)", arg2, arg1)
case gi.TYPE_TAG_INT64:
return fmt.Sprintf("%s = int64(%s)", arg2, arg1)
case gi.TYPE_TAG_UINT64:
return fmt.Sprintf("%s = uint64(%s)", arg2, arg1)
case gi.TYPE_TAG_FLOAT:
return fmt.Sprintf("%s = float64(%s)", arg2, arg1)
case gi.TYPE_TAG_DOUBLE:
return fmt.Sprintf("%s = float64(%s)", arg2, arg1)
case gi.TYPE_TAG_GTYPE:
if config.namespace != "GObject" {
return fmt.Sprintf("%s = gobject.Type(%s)", arg2, arg1)
}
return fmt.Sprintf("%s = Type(%s)", arg2, arg1)
case gi.TYPE_TAG_UNICHAR:
return fmt.Sprintf("%s = rune(%s)", arg2, arg1)
}
panic("unreachable")
return ""
}
//------------------------------------------------------------------
// Simple Cgo to Go Converter
//------------------------------------------------------------------
func simple_cgo_to_go(ti *gi.TypeInfo, arg0, arg1 string, flags conv_flags) string {
cgotype := cgo_type(ti, type_none)
arg0 = fmt.Sprintf("(%s)(%s)", cgotype, arg0)
return cgo_to_go(ti, arg0, arg1, flags)
}