-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy patharguments_wrapper.go
475 lines (408 loc) · 14.3 KB
/
arguments_wrapper.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
package main
import "C"
import (
"fmt"
"regexp"
"strconv"
)
type ArgumentWrapperData struct {
// go-valid argument type (e.g. string, ImVec2, etc.)
ArgType GoIdentifier
// argument deffinition (e.g. arg1, arg1Fin := ...\ndefer arg1Fin())
ArgDef string
// This is equivalent of above (VarName will be appropiate here), but it will not define Finalizer (use this if you're not going to use Finalizer)
ArgDefNoFin string
// one-line go statement that should be called after calling C function
// in order to update go value.
// It is intended to be run in defer statement (and it will be in most cases)
// so it should be one-line function call OR a call of custom function
Finalizer string
// a name of variable of wrapped C type
VarName string
NoFin bool
// CType is a valid type that will have VarName.
CType GoIdentifier
}
type argumentWrapper func(arg ArgDef) ArgumentWrapperData
func getArgWrapper(
a *ArgDef,
makeFirstArgReceiver, isGetter bool,
context *Context,
) (argDeclaration string, data ArgumentWrapperData, err error) {
argWrapperMap := map[CIdentifier]argumentWrapper{
"char": simpleW("rune", "C.char"),
"char*": constCharW,
"char**": charPtrPtrW,
"char* const[]": charPtrPtrW,
"unsigned char": simpleW("uint", "C.uchar"),
"unsigned char*": simplePtrW("uint", "C.uchar"),
"unsigned char**": uCharPtrW,
"size_t": simpleW("uint64", "C.xulong"),
"size_t*": sizeTPtrW,
"float": simpleW("float32", "C.float"),
"float*": simplePtrW("float32", "C.float"),
"double": simpleW("float64", "C.double"),
"double*": simplePtrW("float64", "C.double"),
"short": simpleW("int16", "C.short"),
"short*": simplePtrW("int16", "C.short"),
"unsigned short": simpleW("uint16", "C.ushort"),
"unsigned short*": simplePtrW("uint16", "C.ushort"),
"int": simpleW("int32", "C.int"),
"int32_t": simpleW("int32", "C.int32_t"),
"int*": simplePtrW("int32", "C.int"),
"unsigned int": simpleW("uint32", "C.uint"),
"unsigned int*": simplePtrW("uint32", "C.uint"),
"bool": simpleW("bool", "C.bool"),
"bool*": simplePtrW("bool", "C.bool"),
"time_t": simpleW("uint64", "C.xulong"),
"tm": wrappableW(prefixGoPackage("Tm", "implot", context), "C.struct_tm"),
"tm*": wrappablePtrW(prefixGoPackage("*Tm", "imgui", context), "C.struct_tm"),
"void*": simpleW("unsafe.Pointer", "unsafe.Pointer"),
"uintptr_t": simpleW("uintptr", "C.uintptr_t"),
"ImVec2[2]": wrappablePtrArrayW(2, "C.ImVec2", prefixGoPackage("Vec2", "imgui", context)),
}
// introduce content of preset
for k, v := range context.preset.SimpleTypes {
argWrapperMap[k] = simpleW(prefixGoPackage(v[0], v[2], context), v[1])
}
for k, v := range context.preset.SimplePtrTypes {
argWrapperMap[k+"*"] = simplePtrW(prefixGoPackage(v[0], v[2], context), v[1])
}
for k, v := range context.preset.WrappableTypes {
argWrapperMap[k] = wrappableW(prefixGoPackage(v[0], v[2], context), v[1])
argWrapperMap[k+"*"] = wrappablePtrW(prefixGoPackage("*"+v[0], v[2], context), v[1])
}
if a.Name == "type" || a.Name == "range" {
a.Name += "Arg"
}
if makeFirstArgReceiver {
return "", ArgumentWrapperData{}, nil
}
if isGetter {
argDeclaration = fmt.Sprintf("%s %s", a.Name, a.Type.renameGoIdentifier(context))
data = ArgumentWrapperData{
ArgDef: fmt.Sprintf("%[1]sArg, %[1]sFin := %[1]s.Handle()", a.Name),
ArgDefNoFin: fmt.Sprintf("%[1]sArg, _ := %[1]s.Handle()", a.Name),
VarName: fmt.Sprintf("%sArg", a.Name),
Finalizer: fmt.Sprintf("%sFin()", a.Name),
NoFin: a.RemoveFinalizer,
}
return
}
pureType := TrimPrefix(a.Type, "const ")
if v, ok := argWrapperMap[pureType]; ok {
arg := v(*a)
data = arg
data.NoFin = a.RemoveFinalizer
argDeclaration = fmt.Sprintf("%s %s", a.Name, arg.ArgType)
return argDeclaration, data, nil
}
isPointer := false
if HasSuffix(a.Type, "*") {
pureType = TrimSuffix(pureType, "*")
isPointer = true
}
_, isRefTypedef := context.refTypedefs[pureType]
_, isEnum := context.enumNames[pureType]
_, isRefEnum := context.refEnumNames[pureType]
if isEnum || isRefEnum {
srcPkg := context.flags.PackageName
if isRefTypedef {
srcPkg = context.flags.RefPackageName
}
goType := prefixGoPackage(pureType.renameGoIdentifier(context), GoIdentifier(srcPkg), context)
if isPointer {
argDeclaration = fmt.Sprintf("%s *%s", a.Name, goType)
data = ArgumentWrapperData{
ArgType: GoIdentifier(fmt.Sprintf("*%s", goType)),
VarName: fmt.Sprintf("(*C.%s)(%s)", pureType, a.Name),
CType: GoIdentifier(fmt.Sprintf("*C.%s", pureType)),
}
} else {
argDeclaration = fmt.Sprintf("%s %s", a.Name, goType)
data = ArgumentWrapperData{
ArgType: goType,
VarName: fmt.Sprintf("C.%s(%s)", pureType, a.Name),
CType: GoIdentifier(fmt.Sprintf("C.%s", a.Type)),
}
}
return
}
if HasPrefix(a.Type, "ImVector_") &&
!(HasSuffix(a.Type, "*") || HasSuffix(a.Type, "]")) {
pureType := TrimPrefix(a.Type, "ImVector_") + "*"
dataName := a.Name + "Data"
_, w, err := getArgWrapper(&ArgDef{
Name: dataName,
Type: pureType,
}, false, false, context)
if err != nil {
return "", ArgumentWrapperData{}, fmt.Errorf("creating vector wrapper %w", err)
}
// NOTE Special Case
if pureType == "char*" { // we need to handle it as *byte, not string
charWrapper := simplePtrW("int8", "C.char")
w = charWrapper(ArgDef{
Name: dataName,
Type: pureType,
})
}
data = ArgumentWrapperData{
VarName: string("*" + a.Name + "VecArg"),
ArgType: GoIdentifier(fmt.Sprintf("vectors.Vector[%s]", Replace(w.ArgType, "*", "", 1))),
ArgDef: fmt.Sprintf(`%[5]s := %[2]s.Data
%[1]s
%[2]sVecArg := new(C.%[3]s)
%[2]sVecArg.Size = C.int(%[2]s.Size)
%[2]sVecArg.Capacity = C.int(%[2]s.Capacity)
%[2]sVecArg.Data = %[4]s
%[2]s.Pinner().Pin(%[2]sVecArg.Data)
`, w.ArgDef, a.Name, a.Type, w.VarName, dataName),
ArgDefNoFin: fmt.Sprintf(`%[5]s := %[2]s.Data
%[1]s
%[2]sVecArg := new(C.%[3]s)
%[2]sVecArg.Size = C.int(%[2]s.Size)
%[2]sVecArg.Capacity = C.int(%[2]s.Capacity)
%[2]sVecArg.Data = %[4]s
%[2]s.Pinner().Pin(%[2]sVecArg.Data)
`, w.ArgDefNoFin, a.Name, a.Type, w.VarName, dataName),
Finalizer: fmt.Sprintf("%s\n%s.Pinner().Unpin()", w.Finalizer, a.Name),
NoFin: a.RemoveFinalizer,
}
argDeclaration = fmt.Sprintf("%s %s", a.Name, data.ArgType)
return argDeclaration, data, nil
}
// consider array of form type[number] (check with regex)
r, err := regexp.Compile(`\w*\[\d+\]`)
if err != nil {
return "", ArgumentWrapperData{}, fmt.Errorf("cannot compile regex: %w", err)
}
if r.MatchString(string(a.Type)) {
// split out count and type name ("pureType")
parts := Split(a.Type, "[")
countStr := TrimSuffix(parts[1], "]")
count, err := strconv.Atoi(string(countStr))
if err != nil {
return "", ArgumentWrapperData{}, fmt.Errorf("cannot convert count to int: %w", err)
}
pureType := parts[0]
// decode pureType
singleDefName := a.Name + "V"
_, w, err := getArgWrapper(&ArgDef{
Name: singleDefName,
Type: pureType,
}, false, false, context)
if err != nil {
return "", ArgumentWrapperData{}, fmt.Errorf("creating array wrapper %w", err)
}
// we also need to be able to convert this back
fromC, err := getReturnWrapper(pureType, context)
if err != nil {
return "", ArgumentWrapperData{}, fmt.Errorf("creating array wrapper %w", err)
}
def := fmt.Sprintf(`
%[1]sArg := make([]%[5]s, len(%[1]s))
for i, %[3]s := range %[1]s {
%[2]s
%[1]sArg[i] = %[4]s
}`, a.Name, w.ArgDefNoFin, singleDefName, w.VarName, w.CType)
data := ArgumentWrapperData{
ArgType: GoIdentifier(fmt.Sprintf("*[%d]%s", count, w.ArgType)),
ArgDef: def,
ArgDefNoFin: def,
VarName: fmt.Sprintf("(*%s)(&%sArg[0])", w.CType, a.Name),
Finalizer: fmt.Sprintf(`
for i, %[1]sV := range %[1]sArg {
(*%[1]s)[i] = %[2]s
}
`, a.Name, fmt.Sprintf(fromC.returnStmt, fmt.Sprintf("%sV", a.Name))),
CType: "*" + w.CType,
}
argDeclaration = fmt.Sprintf("%s %s", a.Name, data.ArgType)
return argDeclaration, data, nil
}
_, shouldSkipRefTypedef := context.preset.SkipTypedefs[pureType]
if context.typedefsNames[pureType] || (isRefTypedef && !shouldSkipRefTypedef) {
srcPkg := context.flags.PackageName
if isRefTypedef {
srcPkg = context.flags.RefPackageName
}
goType := prefixGoPackage(pureType.renameGoIdentifier(context), GoIdentifier(srcPkg), context)
cType := GoIdentifier(fmt.Sprintf("C.%s", pureType))
argType := goType
fn := ""
if isPointer {
argType = "*" + argType
cType = GoIdentifier(fmt.Sprintf("*C.%s", pureType))
fn = "Handle"
} else {
fn = "C"
}
w := ArgumentWrapperData{
ArgType: argType,
VarName: fmt.Sprintf("internal.ReinterpretCast[%s](%sArg)", cType, a.Name),
Finalizer: fmt.Sprintf("%sFin()", a.Name),
NoFin: a.RemoveFinalizer,
CType: cType,
}
w.ArgDef = fmt.Sprintf("%[1]sArg, %[1]sFin := %[1]s.%[2]s()", a.Name, fn)
w.ArgDefNoFin = fmt.Sprintf("%[1]sArg, _ := %[1]s.%[2]s()", a.Name, fn)
data = w
argDeclaration = fmt.Sprintf("%s %s", a.Name, data.ArgType)
return
}
return "", ArgumentWrapperData{}, fmt.Errorf("unknown argument type \"%s\"", a.Type)
}
func constCharW(arg ArgDef) ArgumentWrapperData {
return ArgumentWrapperData{
ArgType: "string",
VarName: fmt.Sprintf("%sArg", arg.Name),
ArgDef: fmt.Sprintf("%[1]sArg, %[1]sFin := internal.WrapString[C.char](%[1]s)", arg.Name),
ArgDefNoFin: fmt.Sprintf("%[1]sArg, _ := internal.WrapString[C.char](%[1]s)", arg.Name),
Finalizer: fmt.Sprintf("%sFin()", arg.Name),
CType: "*C.char",
}
}
func charPtrPtrW(arg ArgDef) ArgumentWrapperData {
return ArgumentWrapperData{
ArgType: "[]string",
VarName: fmt.Sprintf("%sArg", arg.Name),
ArgDef: fmt.Sprintf("%[1]sArg, %[1]sFin := internal.WrapStringList[C.char](%[1]s)", arg.Name),
ArgDefNoFin: fmt.Sprintf("%[1]sArg, _ := internal.WrapStringList[C.char](%[1]s)", arg.Name),
Finalizer: fmt.Sprintf("%sFin()", arg.Name),
CType: "**C.char",
}
}
func uCharPtrW(arg ArgDef) ArgumentWrapperData {
return ArgumentWrapperData{
ArgType: "*C.uchar",
VarName: fmt.Sprintf("&%s", arg.Name),
CType: "*C.uchar",
}
}
func sizeTPtrW(arg ArgDef) ArgumentWrapperData {
return ArgumentWrapperData{
ArgType: "*uint64",
VarName: fmt.Sprintf("(*C.xulong)(%s)", arg.Name),
CType: "*C.xulong",
}
}
func int64ArrayW(arg ArgDef) ArgumentWrapperData {
return ArgumentWrapperData{
ArgType: "[]int64",
VarName: fmt.Sprintf("(*C.longlong)(&(%s[0]))", arg.Name),
CType: "*C.longlong",
}
}
func uint64ArrayW(arg ArgDef) ArgumentWrapperData {
return ArgumentWrapperData{
ArgType: "[]uint64",
VarName: fmt.Sprintf("(*C.ulonglong)(&(%s[0]))", arg.Name),
CType: "*C.ulonglong",
}
}
//func voidPtrW(arg ArgDef) ArgumentWrapperData {
// return ArgumentWrapperData{
// ArgType: "unsafe.Pointer",
// ArgDef: fmt.Sprintf("%[1]sArg, %[1]sFin := WrapVoidPtr(%[1]s)", arg.Name),
// ArgDefNoFin: fmt.Sprintf("%[1]sArg, _ := WrapVoidPtr(%[1]s)", arg.Name),
// VarName: fmt.Sprintf("%sArg", arg.Name),
// Finalizer: fmt.Sprintf("%sFin()", arg.Name),
// CType: "unsafe.Pointer",
// }
//}
// generic wrappers:
// C.int -> int32
func simpleW(goType GoIdentifier, cType GoIdentifier) argumentWrapper {
return func(arg ArgDef) ArgumentWrapperData {
return ArgumentWrapperData{
ArgType: goType,
VarName: fmt.Sprintf("%s(%s)", cType, arg.Name),
CType: cType,
}
}
}
// C.int* -> *int32
//
// return simplePtrW(arg.Name, "int16", "C.int")
func simplePtrW(goType GoIdentifier, cType GoIdentifier) argumentWrapper {
return func(arg ArgDef) ArgumentWrapperData {
return ArgumentWrapperData{
ArgType: GoIdentifier(fmt.Sprintf("*%s", goType)),
ArgDef: fmt.Sprintf("%[1]sArg, %[1]sFin := internal.WrapNumberPtr[%[2]s, %[3]s](%[1]s)", arg.Name, cType, goType),
ArgDefNoFin: fmt.Sprintf("%[1]sArg, _ := internal.WrapNumberPtr[%[2]s, %[3]s](%[1]s)", arg.Name, cType, goType),
Finalizer: fmt.Sprintf("%[1]sFin()", arg.Name, cType, goType),
VarName: fmt.Sprintf("%sArg", arg.Name),
CType: "*" + cType,
}
}
}
// C.int*, C.int[] as well as C.int[2] -> [2]*int32
func simplePtrArrayW(size int, cArrayType GoIdentifier, goArrayType GoIdentifier) argumentWrapper {
return func(arg ArgDef) ArgumentWrapperData {
def := fmt.Sprintf(`
%[1]sArg := make([]%[2]s, len(%[1]s))
for i, %[1]sV := range %[1]s {
%[1]sArg[i] = %[2]s(%[1]sV)
}`, arg.Name, cArrayType)
return ArgumentWrapperData{
ArgType: GoIdentifier(fmt.Sprintf("*[%d]%s", size, goArrayType)),
ArgDef: def,
ArgDefNoFin: def,
VarName: fmt.Sprintf("(*%s)(&%sArg[0])", cArrayType, arg.Name),
Finalizer: fmt.Sprintf(`
for i, %[1]sV := range %[1]sArg {
(*%[1]s)[i] = %[3]s(%[1]sV)
}
`, arg.Name, cArrayType, goArrayType),
CType: "*" + cArrayType,
}
}
}
// C.ImVec2 -> ImVec2
func wrappableW(goType, cType GoIdentifier) argumentWrapper {
return func(arg ArgDef) ArgumentWrapperData {
return ArgumentWrapperData{
ArgType: goType,
VarName: fmt.Sprintf("internal.ReinterpretCast[%s](%s.ToC())", cType, arg.Name),
CType: cType,
}
}
}
// C.ImVec2* -> *ImVec2
func wrappablePtrW(goType, cType GoIdentifier) argumentWrapper {
return func(arg ArgDef) ArgumentWrapperData {
return ArgumentWrapperData{
ArgType: goType,
ArgDef: fmt.Sprintf("%[1]sArg, %[1]sFin := internal.Wrap(%[1]s)", arg.Name, goType, cType),
ArgDefNoFin: fmt.Sprintf("%[1]sArg, _ := internal.Wrap(%[1]s)", arg.Name, goType, cType),
Finalizer: fmt.Sprintf("%[1]sFin()", arg.Name, goType, cType),
VarName: fmt.Sprintf("internal.ReinterpretCast[*%s](%sArg)", cType, arg.Name),
CType: "*" + cType,
}
}
}
func wrappablePtrArrayW(size int, cArrayType GoIdentifier, goArrayType GoIdentifier) argumentWrapper {
return func(arg ArgDef) ArgumentWrapperData {
def := fmt.Sprintf(`%[1]sArg := make([]%[2]s, len(%[1]s))
%[1]sFin := make([]func(), len(%[1]s))
for i, %[1]sV := range %[1]s {
var tmp *%[2]s
tmp, %[1]sFin[i] = internal.Wrap(%[1]sV)
%[1]sArg[i] = *tmp
}
`, arg.Name, cArrayType, goArrayType)
return ArgumentWrapperData{
ArgType: GoIdentifier(fmt.Sprintf("[%d]*%s", size, goArrayType)),
ArgDef: def,
ArgDefNoFin: def,
Finalizer: fmt.Sprintf(`
for _, %[1]sV := range %[1]sFin {
%[1]sV()
}
`, arg.Name, cArrayType, goArrayType),
VarName: fmt.Sprintf("(*%s)(&%sArg[0])", cArrayType, arg.Name),
CType: "*" + cArrayType,
}
}
}