-
Notifications
You must be signed in to change notification settings - Fork 51
/
gencpp.go
637 lines (529 loc) · 16.8 KB
/
gencpp.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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
package main
import (
"fmt"
"os"
"strings"
"unicode"
)
// Name of argument in cpp/go files.
// It is used by functions that has text and text_end arguments.
// In this case text_end is replaced by this argument (of type int)
const textLenRegisteredName = "text_len"
// Generate cpp wrapper and return valid functions
func generateCppWrapper(prefix, includePath string, funcDefs []FuncDef, ctx *Context) ([]FuncDef, error) {
var validFuncs []FuncDef
// Generate header
var headerSb strings.Builder
headerSb.WriteString(cppFileHeader)
headerSb.WriteString(fmt.Sprintf(`#pragma once
#include "%s"
#ifdef __cplusplus
extern "C" {
#endif
`, includePath))
cppSb := &strings.Builder{}
cppSb.WriteString(cppFileHeader)
fmt.Fprintf(cppSb, `#include "wrapper.h"
#include "%s"
`, includePath)
// Note for future generations: can't replace with range, because we edit funcDefs later
for i := 0; i < len(funcDefs); i++ {
f := funcDefs[i]
shouldSkip := false
// Check func names (some of them are arbitrarly skipped)
if HasPrefix(f.FuncName, "ImSpan") ||
HasPrefix(f.FuncName, "ImBitArray") {
shouldSkip = true
continue
}
// Process custom_type for arg
for i, a := range f.ArgsT {
if len(a.CustomType) > 0 {
f.Args = ReplaceAll(f.Args, a.Type, a.CustomType)
f.ArgsT[i].Type = a.CustomType
}
}
// Check args (some arg formats are skipped)
for _, a := range f.ArgsT {
if Contains(a.Type, "const T*") ||
Contains(a.Type, "const T") ||
a.Type == "va_list" ||
(a.Name == "self" && a.Type == "ImVector*") {
shouldSkip = true
break
}
}
// check if func name is valid
if len(f.FuncName) == 0 {
shouldSkip = true
}
if shouldSkip {
continue
}
funcName := f.FuncName
// Check lower case member function
funcParts := Split(funcName, "_")
if len(funcParts) == 2 && unicode.IsLower(rune(funcParts[1][0])) {
funcName = funcParts[0] + "_" + CIdentifier(unicode.ToUpper(rune(funcParts[1][0]))) + funcParts[1][1:]
}
// TODO: the majority of these wrappers, those without the V version,
// could be skipped entirely. For those with V, the different versions
// could be generated in Go code.
// So, could be skip cimgui_wrapper.ccp/.h entirely?
cWrapperFuncName := "wrap_" + f.FuncName
// Remove all ... arg
f.Args = strings.Replace(f.Args, ",...", "", 1)
// Remove text_end arg
f.Args = strings.Replace(f.Args, ",const char* text_end_", fmt.Sprintf(",const int %s", textLenRegisteredName), 1) // sometimes happens in cimmarkdown
f.Args = strings.Replace(f.Args, ",const char* text_end", fmt.Sprintf(",const int %s", textLenRegisteredName), 1)
ret := f.Ret
if ret == "void*" {
ret = "uintptr_t"
}
var argsT []ArgDef
var actualCallArgs []CIdentifier
for _, a := range f.ArgsT {
f.AllCallArgs += a.Name + ","
switch {
case a.Name == "...":
continue
case a.Name == "text_end", a.Name == "text_end_":
// chck if there is `text` argument
var found bool
for _, aa := range f.ArgsT {
if aa.Name == "text" {
found = true
break
}
}
if found {
argsT = append(argsT, ArgDef{
Name: "text_len",
Type: "const int",
})
actualCallArgs = append(actualCallArgs, "(text_len > 0) ? text + text_len*sizeof(char) : 0")
} else {
f.Args = strings.Replace(f.Args, ",const int text_len", "", 1)
actualCallArgs = append(actualCallArgs, "0")
continue
}
// this is here, because of a BUG in GO that throws a fatal panic
// when we attempt to print unsafe.Pointer which is valid for C but is not present
// on the GO stack. See https://go.dev/play/p/d09eyqUlVV0
// see:https://github.com/golang/go/issues/64467
// case Contains(a.Type, "void*"):
case a.Type == "void*" || a.Type == "const void*":
actualCallArgs = append(actualCallArgs, CIdentifier(fmt.Sprintf("(%s)(uintptr_t)%s", a.Type, a.Name)))
a.Type = "uintptr_t"
// do some trick: we can't replace void* in a.Args immediaely by using replace, because sometimes
// args are of form (void* user_data, (void* user_data)(int something))
// we need to replace only "plain" arguments (not function-types)
// we will split Args by "," and check each argument to be equal to ("void* ", a.Name)
newArgs := TrimPrefix(f.Args, "(")
newArgs = TrimSuffix(newArgs, ")")
newArgsList := Split(newArgs, ",")
for i, arg := range newArgsList {
switch arg {
case fmt.Sprintf("void* %s", a.Name):
newArgsList[i] = fmt.Sprintf("uintptr_t %s", a.Name)
case fmt.Sprintf("const void* %s", a.Name):
newArgsList[i] = fmt.Sprintf("const uintptr_t %s", a.Name)
}
}
f.Args = fmt.Sprintf("(%s)", Join(newArgsList, ","))
argsT = append(argsT, a)
default:
argsT = append(argsT, a)
actualCallArgs = append(actualCallArgs, a.Name)
}
}
f.AllCallArgs = "(" + TrimSuffix(f.AllCallArgs, ",") + ")"
f.ArgsT = argsT
//f.Args = "("
//for _, a := range argsT {
// f.Args += fmt.Sprintf("%s %s,", a.Type, a.Name)
//}
//f.Args = TrimSuffix(f.Args, ",")
//f.Args += ")"
// Generate shorter function which omits the default args
// Skip functions as function pointer arg
shouldSkip = false
for _, a := range f.ArgsT {
if Contains(a.Type, "(") {
shouldSkip = true
}
}
if len(f.Defaults) > 0 && !shouldSkip && !f.Constructor {
var newArgsT []ArgDef
var invocationArgs []CIdentifier
for _, a := range f.ArgsT {
invocationArgs = append(invocationArgs, a.Name)
shouldIgnore := false
for k := range f.Defaults {
if string(a.Name) == k {
shouldIgnore = true
break
}
}
if !shouldIgnore {
newArgsT = append(newArgsT, a)
}
}
var newArgs []string
for _, a := range newArgsT {
t := a.Type
n := a.Name
// Process array type
if Contains(t, "[") {
parts := Split(t, "[")
t = parts[0]
n = n + "[" + Join(parts[1:], "")
}
newArgs = append(newArgs, fmt.Sprintf("%s %s", t, n))
}
// Generate invocation stmt
invocationStmt := fmt.Sprintf("(%s)", Join(invocationArgs, ","))
for k, v := range f.Defaults {
if v == "ImVec2(0,0)" || v == "ImVec2(0.0f,0.0f)" {
v = "(ImVec2){.x=0, .y=0}"
}
if v == "ImVec2(1,1)" {
v = "(ImVec2){.x=1, .y=1}"
}
if v == "ImVec2(1,0)" {
v = "(ImVec2){.x=1, .y=0}"
}
if v == "ImVec2(0,1)" {
v = "(ImVec2){.x=0, .y=1}"
}
if v == "ImVec2(-1,0)" {
v = "(ImVec2){.x=-1, .y=0}"
}
if v == "ImVec2(-FLT_MIN,0)" {
v = "(ImVec2){.x=-1*igGET_FLT_MIN(), .y=0}"
}
if v == "ImVec4(0,0,0,0)" {
v = "(ImVec4){.x=0, .y=0, .z=0, .w=0}"
}
if v == "ImVec4(1,1,1,1)" {
v = "(ImVec4){.x=1, .y=1, .z=1, .w=1}"
}
if v == "ImVec4(0,0,0,-1)" {
v = "(ImVec4){.x=0, .y=0, .z=0, .w=-1}"
}
if v == "ImPlotPoint(0,0)" {
v = "(ImPlotPoint){.x=0, .y=0}"
}
if v == "ImPlotPoint(1,1)" {
v = "(ImPlotPoint){.x=1, .y=1}"
}
if v == "FLT_MAX" {
v = "igGET_FLT_MAX()"
}
if v == "((void*)0)" {
v = "NULL"
}
if v == "nullptr" || v == "NULL" {
v = "0"
}
if k == "text_end" || k == "text_end_" {
v = "0"
}
if strings.Contains(invocationStmt, ","+k) {
invocationStmt = strings.Replace(invocationStmt, ","+k, ","+v, 1)
} else {
invocationStmt = strings.Replace(invocationStmt, k, v, 1)
}
}
// Generate new function
funcDefs = append(funcDefs, FuncDef{
FuncName: funcName,
OriginalFuncName: funcName + "V",
Args: fmt.Sprintf("(%s)", strings.Join(newArgs, ",")),
ArgsT: newArgsT,
InvocationStmt: invocationStmt,
Defaults: nil,
Location: f.Location,
Constructor: f.Constructor,
Destructor: f.Destructor,
StructSetter: false,
StructGetter: false,
Ret: ret,
StName: f.StName,
NonUDT: f.NonUDT,
CWrapperFuncName: cWrapperFuncName + "V",
AllCallArgs: f.AllCallArgs,
})
// Add V as suffix to current function name
funcName += "V"
cWrapperFuncName += "V"
}
actualCallArgsStr := fmt.Sprintf("(%s)", Join(actualCallArgs, ","))
if len(f.InvocationStmt) > 0 {
actualCallArgsStr = f.InvocationStmt
}
appendValidFunc := func() {
validFuncs = append(validFuncs, FuncDef{
Args: f.Args,
ArgsT: f.ArgsT,
FuncName: funcName,
OriginalFuncName: f.OriginalFuncName,
Defaults: f.Defaults,
Location: f.Location,
Constructor: f.Constructor,
Destructor: f.Destructor,
InvocationStmt: f.InvocationStmt,
Ret: ret,
StName: f.StName,
NonUDT: f.NonUDT,
CWrapperFuncName: cWrapperFuncName,
AllCallArgs: f.AllCallArgs,
Comment: f.Comment,
})
}
invokeFunctionName := f.FuncName
if f.OriginalFuncName != "" {
for _, ff := range validFuncs {
if ff.FuncName == f.OriginalFuncName {
invokeFunctionName = ff.CWrapperFuncName
break
}
}
}
// cppSb.WriteString(fmt.Sprintf("// %#v\n", f))
// if needed, write extra stuff to cpp headers
if string(f.AllCallArgs) == actualCallArgsStr && ret == f.Ret {
cWrapperFuncName = f.FuncName
} else if f.Constructor {
headerSb.WriteString(fmt.Sprintf("extern %s* %s%s;\n", f.StName, cWrapperFuncName, f.Args))
cppSb.WriteString(fmt.Sprintf("%s* %s%s { return %s%s; }\n", f.StName, cWrapperFuncName, f.Args, invokeFunctionName, actualCallArgsStr))
} else if f.Destructor {
headerSb.WriteString(fmt.Sprintf("extern void %s%s;\n", cWrapperFuncName, f.Args))
cppSb.WriteString(fmt.Sprintf("void %s%s { %s%s; }\n", cWrapperFuncName, f.Args, invokeFunctionName, actualCallArgsStr))
} else {
headerSb.WriteString(fmt.Sprintf("extern %s %s%s;\n", ret, cWrapperFuncName, f.Args))
switch f.Ret {
case "void":
cppSb.WriteString(fmt.Sprintf("%s %s%s { %s%s; }\n", f.Ret, cWrapperFuncName, f.Args, invokeFunctionName, actualCallArgsStr))
case "void*":
cppSb.WriteString(fmt.Sprintf("uintptr_t %s%s { return (uintptr_t)%s%s; }\n", cWrapperFuncName, f.Args, invokeFunctionName, actualCallArgsStr))
default:
cppSb.WriteString(fmt.Sprintf("%s %s%s { return %s%s; }\n", f.Ret, cWrapperFuncName, f.Args, invokeFunctionName, actualCallArgsStr))
}
}
if v, ok := ctx.preset.OriginReplace[funcName]; ok {
cWrapperFuncName = v
}
appendValidFunc()
}
headerSb.WriteString(`
#ifdef __cplusplus
}
#endif
`)
headerPath := "wrapper.h"
headerFile, err := os.Create(headerPath)
if err != nil {
return nil, fmt.Errorf("failed to create header file %v: %v", headerPath, err)
}
defer headerFile.Close()
_, err = headerFile.WriteString(headerSb.String())
if err != nil {
return nil, fmt.Errorf("failed to write header file %v: %v", headerPath, err)
}
cppPath := "wrapper.cpp"
cppFile, err := os.Create(cppPath)
if err != nil {
return nil, fmt.Errorf("failed to create cpp file %v: %v", cppPath, err)
}
defer cppFile.Close()
_, err = cppFile.WriteString(cppSb.String())
if err != nil {
return nil, fmt.Errorf("failed to write cpp file %v: %v", cppPath, err)
}
return validFuncs, nil
}
func generateCppStructsAccessor(prefix string, validFuncs []FuncDef, structs []StructDef, context *Context) (accessors []FuncDef, err error) {
var structAccessorFuncs []FuncDef
// makes a setsum with context.preset.SkipFUncs
skipFuncNames := map[CIdentifier]bool{}
// Add all valid function's name to skipFuncNames
for _, f := range validFuncs {
skipFuncNames[f.FuncName] = true
}
sbHeader, sbCpp := &strings.Builder{}, &strings.Builder{}
sbHeader.WriteString(cppFileHeader)
sbCpp.WriteString(cppFileHeader)
sbHeader.WriteString(`#pragma once
#include "wrapper.h"
#ifdef __cplusplus
extern "C" {
#endif
`)
sbCpp.WriteString(`
#include <string.h>
#include "wrapper.h"
#include "structs_accessor.h"
`)
for _, s := range structs {
for _, m := range s.Members {
if Contains(m.Type, "(") || Contains(m.Type, "union") {
continue
}
setterFuncName := CIdentifier(fmt.Sprintf("%[1]s_Set%[2]s", s.Name, Capitalize(Split(m.Name, "[")[0])))
if skipFuncNames[setterFuncName] || context.preset.SkipFuncs[setterFuncName] {
continue
}
memberType := m.Type
if memberType == "void*" {
memberType = "uintptr_t"
}
// Generate setter function
setterFuncDef := FuncDef{
Args: fmt.Sprintf("(%[1]s *%[2]s, %[3]s v)", s.Name, s.Name+"Ptr", m.Type),
ArgsT: []ArgDef{
{
Name: s.Name + "Ptr",
Type: s.Name,
},
{
Name: "v",
Type: memberType + CIdentifier(getSizeArg(m.Size)),
RemoveFinalizer: true,
},
},
FuncName: setterFuncName,
CWrapperFuncName: "wrap_" + setterFuncName,
Location: "",
Constructor: false,
Destructor: false,
StructSetter: true,
Ret: "void",
}
structAccessorFuncs = append(structAccessorFuncs, setterFuncDef)
sbHeader.WriteString(fmt.Sprintf("extern void %s(%s *%s, %s%s v);\n", setterFuncDef.CWrapperFuncName, s.Name, s.Name+"Ptr", memberType, getPtrIfSize(m.Size)))
if m.Size > 0 {
fmt.Fprintf(
sbCpp,
"void %s(%s *%s, %s%s v) { memcpy(%s->%s, v, sizeof(%[4]s)*%[8]d); }\n",
setterFuncDef.CWrapperFuncName,
s.Name,
s.Name+"Ptr",
m.Type,
getPtrIfSize(m.Size),
s.Name+"Ptr",
Split(m.Name, "[")[0],
m.Size,
)
} else {
if m.Type == "void*" {
fmt.Fprintf(
sbCpp,
"void %s(%s *%s, %s%s v) { %s->%s = (void*)v; }\n",
setterFuncDef.CWrapperFuncName, s.Name, s.Name+"Ptr", memberType, getPtrIfSize(m.Size), s.Name+"Ptr", Split(m.Name, "[")[0],
)
} else {
fmt.Fprintf(
sbCpp,
"void %s(%s *%s, %s%s v) { %s->%s = v; }\n",
setterFuncDef.CWrapperFuncName, s.Name, s.Name+"Ptr", m.Type, getPtrIfSize(m.Size), s.Name+"Ptr", Split(m.Name, "[")[0],
)
}
}
getterFuncName := CIdentifier(fmt.Sprintf("%[1]s_Get%[2]s", s.Name, Capitalize(Split(m.Name, "[")[0])))
if skipFuncNames[getterFuncName] || context.preset.SkipFuncs[getterFuncName] {
continue
}
getterFuncDef := FuncDef{
Args: fmt.Sprintf("(%[1]s *%[2]s)", s.Name, "self"),
ArgsT: []ArgDef{
{
Name: "self",
Type: s.Name + "*",
},
},
FuncName: getterFuncName,
CWrapperFuncName: "wrap_" + getterFuncName,
Location: "",
Constructor: false,
Destructor: false,
StructSetter: false, StructGetter: true,
Ret: memberType + CIdentifier(getSizeArg(m.Size)),
}
structAccessorFuncs = append(structAccessorFuncs, getterFuncDef)
fmt.Fprintf(
sbHeader,
"extern %s%s %s(%s *self);\n",
memberType, getPtrIfSize(m.Size), getterFuncDef.CWrapperFuncName, s.Name,
)
// here we change void* to uintptr_t for .go handling
if m.Type == "void*" {
fmt.Fprintf(sbCpp,
"%s%s %s(%s *self) { return (uintptr_t)self->%s; }\n",
memberType, getPtrIfSize(m.Size), getterFuncDef.CWrapperFuncName, s.Name, Split(m.Name, "[")[0],
)
} else {
fmt.Fprintf(sbCpp,
"%s%s %s(%s *self) { return self->%s; }\n",
memberType, getPtrIfSize(m.Size), getterFuncDef.CWrapperFuncName, s.Name, Split(m.Name, "[")[0],
)
}
// if is array type, need to add a special method to get certain index of array
if _, ok := context.arrayIndexGetters[m.Type]; m.Size > 0 && !ok {
AddArrayIndexGetter(m.Type, sbHeader, sbCpp, context)
}
}
}
sbHeader.WriteString(`
#ifdef __cplusplus
}
#endif
`)
headerPath := "structs_accessor.h"
headerFile, err := os.Create(headerPath)
if err != nil {
return nil, fmt.Errorf("failed to create cpp file %v: %v", headerPath, err)
}
defer headerFile.Close()
_, err = headerFile.WriteString(sbHeader.String())
if err != nil {
return nil, fmt.Errorf("failed to write to file %v: %v", headerPath, err)
}
cppPath := "structs_accessor.cpp"
cppFile, err := os.Create(cppPath)
if err != nil {
return nil, fmt.Errorf("failed to create cpp file %v: %v", cppPath, err)
}
defer cppFile.Close()
_, err = cppFile.WriteString(sbCpp.String())
if err != nil {
return nil, fmt.Errorf("failed to write to file %v: %v", cppPath, err)
}
return structAccessorFuncs, nil
}
func getSizeArg(size int) string {
if size > 0 {
return fmt.Sprintf("[%d]", size)
}
return ""
}
func getPtrIfSize(size int) string {
if size > 0 {
return "*"
}
return ""
}
func AddArrayIndexGetter(t CIdentifier, sbHeader, sbCpp *strings.Builder, context *Context) {
tStr := ReplaceAll(ReplaceAll(t, " ", "_"), "*", "Ptr")
getterFuncName := CIdentifier(context.prefix) + "_" + tStr + "_GetAtIdx"
context.arrayIndexGetters[t] = getterFuncName
fmt.Fprintf(
sbHeader,
"extern %[1]s %[2]s(%[1]s *self, int index);\n",
Split(t, "[")[0], getterFuncName,
)
fmt.Fprintf(sbCpp,
"%[1]s %[2]s(%[1]s *self, int index) { return self[index]; }\n",
t, getterFuncName,
)
}