-
Notifications
You must be signed in to change notification settings - Fork 2
/
translate.go
1001 lines (911 loc) · 31.2 KB
/
translate.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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"fmt"
"log"
"regexp"
"strconv"
"strings"
strip "github.com/grokify/html-strip-tags-go"
)
// Translator interface provides a set of rules for how to translate the
// specification type informaiton into names for other situations.
type Translator interface {
C() string // C name.
CGo() string // Cgo name.
Go() string // Go name.
}
// LiteralTranslator is used for values that are constants or expected to
// be the same in all outputs. For example 198 or (1 << 4) or VK_SUCCESS.
type LiteralTranslator struct {
specName string
}
func (xl8r *LiteralTranslator) C() string { return xl8r.specName }
func (xl8r *LiteralTranslator) CGo() string { return xl8r.specName }
func (xl8r *LiteralTranslator) Go() string { return xl8r.specName }
// BitValueTranslator is outputs 1 shifted by enough bits to represent
// the required bitposition in the bit mask.
type BitValueTranslator struct {
bitPos int
}
func (xl8r *BitValueTranslator) C() string { return xl8r.Go() }
func (xl8r *BitValueTranslator) CGo() string { return xl8r.Go() }
func (xl8r *BitValueTranslator) Go() string { return fmt.Sprintf("(1 << %d)", xl8r.bitPos) }
// ReservedWordTranslator deals with un exported names (think parameter names)
// that are reserved words, like type, range, and func
type ReservedWordTranslator struct {
orig Translator
}
func (xl8r *ReservedWordTranslator) C() string { return xl8r.orig.C() }
func (xl8r *ReservedWordTranslator) CGo() string {
name := xl8r.orig.CGo()
if name == "type" {
name = "_type"
} else if name == "range" {
name = "_range"
} else if name == "func" {
name = "_func"
}
return name
}
func (xl8r *ReservedWordTranslator) Go() string {
name := xl8r.orig.Go()
if name == "type" {
name = "type_"
} else if name == "range" {
name = "range_"
} else if name == "func" {
name = "func_"
}
return name
}
// CamelCaseTranslator converts uppercase defines into More go friendly values.
// For example, VK_API_VERSION vs VkApiVersion.
type CamelCaseTranslator struct {
specName string
}
func (xl8r *CamelCaseTranslator) C() string { return xl8r.specName }
func (xl8r *CamelCaseTranslator) CGo() string { return xl8r.specName }
func (xl8r *CamelCaseTranslator) Go() string {
src := xl8r.specName
if strings.Contains(src, "_") {
lower := strings.ToLower(src)
parts := strings.Split(lower, "_")
for h := 1; h < len(parts); h++ {
parts[h] = strings.Title(parts[h])
}
return strings.Join(parts, "")
}
return src
}
// TypeDefTranslator provides the C. prefix for the CGo translation. It is
// generally used for things like Handles, where the typedef isn't a struct or
// a union.
type TypeDefTranslator struct {
specName string
}
func (xl8r *TypeDefTranslator) C() string { return xl8r.specName }
func (xl8r *TypeDefTranslator) CGo() string { return fmt.Sprintf("C.%s", xl8r.specName) }
func (xl8r *TypeDefTranslator) Go() string { return xl8r.specName }
// StructTranslator provides the C.struct_ prefix for CGo translations. It is
// used for struct types. This could probably be replaced by typedef, as
// the vulkan header typedef's all the structs. Keeping it mostly for semantic
// reasons.
type StructTranslator struct {
specName string
}
func (xl8r *StructTranslator) C() string { return xl8r.specName }
func (xl8r *StructTranslator) CGo() string { return fmt.Sprintf("C.struct_%s", xl8r.specName) }
func (xl8r *StructTranslator) Go() string { return xl8r.specName }
// ExportTranslator runs a series of rules over the Go name to prepare it
// to be part of the exported API. The rules are added to `TranslatorRules`
// in the order they are expected to be executed.
type ExportTranslator struct {
orig Translator
}
func (xl8r *ExportTranslator) C() string { return xl8r.orig.C() }
func (xl8r *ExportTranslator) CGo() string { return xl8r.orig.CGo() }
func (xl8r *ExportTranslator) Go() string {
src := xl8r.orig.Go()
for _, v := range TranslatorRules {
src = v.Translate(src)
}
return src
}
// ExportTranslatorRule provides an interface for defining different
// rules for the ExportTranslator. If a rule does apply, it should return
// the original, it returns the original string.
type ExportTranslatorRule interface {
Translate(string) string
}
// TranslatorRules are the configured list of rules for the ExportTranslator
var TranslatorRules []ExportTranslatorRule
// exportTranslatorRuleDeprefix removes the provided prefix if a word has it.
type exportTranslatorRuleDeprefix struct {
prefix string
}
func (etRule exportTranslatorRuleDeprefix) Translate(src string) string {
if strings.HasPrefix(src, etRule.prefix) {
return src[len(etRule.prefix):]
}
return src
}
// exportTranslatorRuleTitle makes the first character of a string uppercase.
type exportTranslatorRuleTitle struct{}
func (etRule exportTranslatorRuleTitle) Translate(src string) string {
return strings.Title(src)
}
// Converter interface is an extension of the Translator interface. It
// provides code for converting from one type to another type.
type Converter interface {
Translator
CToGo() string
GoToC() string
}
// ScalarConverter provides conversions for the common C scalar types to
// Go types.
type ScalarConverter struct {
specType string // e.g. uint64_t
cGoType string // e.g. C.ulonglong
goType string // e.g. uint64
}
func (xl8r *ScalarConverter) C() string { return xl8r.specType }
func (xl8r *ScalarConverter) CGo() string { return xl8r.cGoType }
func (xl8r *ScalarConverter) Go() string { return xl8r.goType }
func (xl8r *ScalarConverter) CToGo() string {
return fmt.Sprintf("func(x *%s) *%s { /* Scalar */ c2g := %s(*x); return &c2g }", xl8r.CGo(), xl8r.Go(), xl8r.Go())
}
func (xl8r *ScalarConverter) GoToC() string {
return fmt.Sprintf("func(x *%s) *%s { /* Scalar */ g2c := %s(*x); return &g2c }", xl8r.Go(), xl8r.CGo(), xl8r.CGo())
}
var (
Int8Translator *ScalarConverter = &ScalarConverter{"int8_t", "C.int8_t", "int8"}
Int16Translator = &ScalarConverter{"int16_t", "C.int16_t", "int16"}
Int32Translator = &ScalarConverter{"int32_t", "C.int32_t", "int32"}
Int64Translator = &ScalarConverter{"int64_t", "C.int64_t", "int64"}
Uint8Translator = &ScalarConverter{"uint8_t", "C.uint8_t", "byte"}
Uint16Translator = &ScalarConverter{"uint16_t", "C.uint16_t", "uint16"}
Uint32Translator = &ScalarConverter{"uint32_t", "C.uint32_t", "uint32"}
Uint64Translator = &ScalarConverter{"uint64_t", "C.uint64_t", "uint64"}
SizeTranslator = &ScalarConverter{"size_t", "C.size_t", "uint64"}
Float32Translator = &ScalarConverter{"float", "C.float", "float32"}
Float64Translator = &ScalarConverter{"double", "C.double", "float64"}
StringTranslator = &ScalarConverter{"char", "C.char", "byte"}
UnsafePointerTranslator = &ScalarConverter{"void*", "unsafe.Pointer", "unsafe.Pointer"}
)
// TypeDefConverter provides converter methods that convert from
// compatible types. For example a C.VkFlags to a go Flags.
// which is just a cast.
type TypeDefConverter struct {
orig Translator
}
func (xl8r *TypeDefConverter) C() string { return xl8r.orig.C() }
func (xl8r *TypeDefConverter) CGo() string { return xl8r.orig.CGo() }
func (xl8r *TypeDefConverter) Go() string { return xl8r.orig.Go() }
func (xl8r *TypeDefConverter) CToGo() string {
return fmt.Sprintf("/* typedef */ (*%s)", xl8r.Go())
}
func (xl8r *TypeDefConverter) GoToC() string {
return fmt.Sprintf("/* typedef */ (*%s)", xl8r.CGo())
}
// HandleConverter provides converter methods that convert from
// handle types. While this is generally the same as the TypeDef
// converter, it has some additional methods for generating facades and
// is distinct for semantic reasons.
type HandleConverter struct {
orig Translator
}
func (xl8r *HandleConverter) C() string { return xl8r.orig.C() }
func (xl8r *HandleConverter) CGo() string { return xl8r.orig.CGo() }
func (xl8r *HandleConverter) Go() string { return xl8r.orig.Go() }
func (xl8r *HandleConverter) GoFacade() string { return fmt.Sprintf("%s%s", xl8r.orig.Go(), "Facade") }
func (xl8r *HandleConverter) CToGo() string {
return fmt.Sprintf("/* handle */ (*%s)", xl8r.Go())
}
func (xl8r *HandleConverter) GoToC() string {
return fmt.Sprintf("/* handle */ (*%s)", xl8r.CGo())
}
// PointerConverter wraps an existing translator to be treated like a pointer.
// It is mostly used in struct memebers and command parameters.
type PointerConverter struct {
orig Translator
}
func (xl8r *PointerConverter) C() string { return fmt.Sprintf("%s*", xl8r.orig.C()) }
func (xl8r *PointerConverter) CGo() string { return fmt.Sprintf("*%s", xl8r.orig.CGo()) }
func (xl8r *PointerConverter) Go() string { return fmt.Sprintf("*%s", xl8r.orig.Go()) }
func (xl8r *PointerConverter) CToGo() string {
if xl8r.CGo() == "*C.char" && xl8r.Go() == "*byte" {
// Special case to get around "cannot convert *x (type *_Ctype_char) to type *byte"
return fmt.Sprintf("func(x *%s) *%s { /* Pointer */ return (*%s)(unsafe.Pointer(x)) }", xl8r.CGo(), xl8r.Go(), xl8r.Go())
}
return fmt.Sprintf("func(x *%s) *%s { /* Pointer */ c2g := (%s)(*x); return &c2g }", xl8r.CGo(), xl8r.Go(), xl8r.Go())
}
func (xl8r *PointerConverter) GoToC() string {
if xl8r.CGo() == "*C.char" && xl8r.Go() == "*byte" {
// Special case to get around "cannot convert *x (type *byte) to type *_Ctype_char"
return fmt.Sprintf("func(x *%s) *%s { /* Pointer */ return (*%s)(unsafe.Pointer(x)) }", xl8r.Go(), xl8r.CGo(), xl8r.CGo())
}
return fmt.Sprintf("func(x *%s) *%s { /* Pointer */ g2c := (%s)(*x); return &g2c }", xl8r.Go(), xl8r.CGo(), xl8r.CGo())
}
// SliceConverter wraps an existing translator to be treated like a slice.
// It is mostly used in struct memebers and command parameters.
type SliceConverter struct {
orig Translator
}
func (xl8r *SliceConverter) C() string { return fmt.Sprintf("%s*", xl8r.orig.C()) }
func (xl8r *SliceConverter) CGo() string { return fmt.Sprintf("*%s", xl8r.orig.CGo()) }
func (xl8r *SliceConverter) Go() string { return fmt.Sprintf("[]%s", xl8r.orig.Go()) }
func (xl8r *SliceConverter) CToGo() string {
return fmt.Sprintf("func(x *%s) *%s { /* Slice */ slc := unsafe.Slice((*%s)(unsafe.Pointer(x)), (1 << 31)); return &slc }", xl8r.CGo(), xl8r.Go(), xl8r.orig.Go())
}
func (xl8r *SliceConverter) GoToC() string {
return fmt.Sprintf("func(x *%s) *%s { /* Slice */ if len(*x) > 0 { slc := (%s)(unsafe.Pointer(&((*x)[0]))); return &slc }; var ptr unsafe.Pointer; return (*%s)(unsafe.Pointer((&ptr))) }", xl8r.Go(), xl8r.CGo(), xl8r.CGo(), xl8r.CGo())
}
// ArrayConverter wraps an existing translator to be treated like an array.
// It is mostly used in struct members.
type ArrayConverter struct {
orig Translator
size Translator
}
func (xl8r *ArrayConverter) C() string { return fmt.Sprintf("%s[%s]", xl8r.orig.C(), xl8r.size.C()) }
func (xl8r *ArrayConverter) CGo() string {
return fmt.Sprintf("[%s]%s", xl8r.size.CGo(), xl8r.orig.CGo())
}
func (xl8r *ArrayConverter) Go() string {
return fmt.Sprintf("[]%s", xl8r.orig.Go())
}
func (xl8r *ArrayConverter) CToGo() string {
return fmt.Sprintf("func(x *%s) *%s { /* Array for %s */ slc := unsafe.Slice((*%s)(unsafe.Pointer(x)), %s); return &slc }", xl8r.CGo(), xl8r.Go(), xl8r.orig.Go(), xl8r.orig.Go(), xl8r.size.Go())
}
func (xl8r *ArrayConverter) GoToC() string {
return fmt.Sprintf("func(x *%s) **%s { /* Array */ if len(*x) > 0 { slc := (*%s)(unsafe.Pointer(&((*x)[0]))); return &slc }; var ptr unsafe.Pointer; return (**%s)(unsafe.Pointer((&ptr))) }", xl8r.Go(), xl8r.orig.CGo(), xl8r.orig.CGo(), xl8r.orig.CGo())
}
var (
cachedTranslatorMap map[string]Translator = map[string]Translator{
"int8_t": Int8Translator,
"int16_t": Int16Translator,
"int32_t": Int32Translator,
"int64_t": Int64Translator,
"uint8_t": Uint8Translator,
"uint16_t": Uint16Translator,
"uint32_t": Uint32Translator,
"uint64_t": Uint64Translator,
"size_t": SizeTranslator,
"float": Float32Translator,
"double": Float64Translator,
"char": StringTranslator,
"void*": UnsafePointerTranslator,
}
)
func CachedConverter(specType string) (Converter, bool) {
if entry, ok := CachedTranslator(specType); ok {
if converter, ok := entry.(Converter); ok {
return converter, true
}
}
return nil, false
}
func CachedTranslator(specType string) (Translator, bool) {
entry, ok := cachedTranslatorMap[specType]
return entry, ok
}
func GetScalarConverter(specType string) Converter {
if converter, ok := CachedConverter(specType); ok {
return converter
}
panic(fmt.Errorf("Unknown Scalar type requested: %v", specType))
}
func GetCommandTranslator(specType string) Translator {
if entry, ok := CachedTranslator(specType); ok {
return entry
}
entry := &ExportTranslator{&TypeDefTranslator{specType}}
cachedTranslatorMap[specType] = entry
return entry
}
func GetDefineTranslator(specType string) Translator {
if entry, ok := CachedTranslator(specType); ok {
return entry
}
entry := &ExportTranslator{&CamelCaseTranslator{specType}}
cachedTranslatorMap[specType] = entry
return entry
}
func GetBaseConverter(specType string) Converter {
if entry, ok := CachedConverter(specType); ok {
return entry
}
entry := &TypeDefConverter{&ExportTranslator{&TypeDefTranslator{specType}}}
cachedTranslatorMap[specType] = entry
return entry
}
func GetHandleConverter(specType string) Converter {
if converter, ok := CachedConverter(specType); ok {
return converter
}
entry := &HandleConverter{&ExportTranslator{&TypeDefTranslator{specType}}}
cachedTranslatorMap[specType] = entry
return entry
}
func GetEnumNameConverter(specType string) Converter {
if entry, ok := CachedConverter(specType); ok {
return entry
}
entry := &TypeDefConverter{&ExportTranslator{&TypeDefTranslator{specType}}}
cachedTranslatorMap[specType] = entry
return entry
}
func GetEnumValueConverter(specType string) Converter {
if entry, ok := CachedConverter(specType); ok {
return entry
}
entry := &TypeDefConverter{&ExportTranslator{&TypeDefTranslator{specType}}}
cachedTranslatorMap[specType] = entry
return entry
}
func GetFuncConverter(specType string) Converter {
if entry, ok := CachedConverter(specType); ok {
return entry
}
entry := &TypeDefConverter{&ExportTranslator{&TypeDefTranslator{specType}}}
cachedTranslatorMap[specType] = entry
return entry
}
func GetStructConverter(specType string) Converter {
if entry, ok := CachedConverter(specType); ok {
return entry
}
entry := &TypeDefConverter{&ExportTranslator{&StructTranslator{specType}}}
cachedTranslatorMap[specType] = entry
return entry
}
func GetUnionConverter(specType string) Converter {
if entry, ok := CachedConverter(specType); ok {
return entry
}
entry := &TypeDefConverter{&ExportTranslator{&TypeDefTranslator{specType}}}
cachedTranslatorMap[specType] = entry
return entry
}
func CacheConverters(path []*RegistryNode) {
node := path[len(path)-1]
switch node.NodeType {
case RegistryNodeType:
if tiepuh := node.TypeElement(); tiepuh != nil {
switch tiepuh.Category {
case TypeCategoryDefine:
GetDefineTranslator(tiepuh.Name())
case TypeCategoryBasetype:
GetBaseConverter(tiepuh.Name())
case TypeCategoryHandle:
GetHandleConverter(tiepuh.Name())
case TypeCategoryEnum:
GetEnumNameConverter(tiepuh.Name())
case TypeCategoryBitmask:
GetEnumNameConverter(tiepuh.Name())
case TypeCategoryFuncpointer:
GetFuncConverter(tiepuh.Name())
case TypeCategoryStruct:
GetStructConverter(tiepuh.Name())
case TypeCategoryUnion:
GetUnionConverter(tiepuh.Name())
}
}
case RegistryNodeCommand:
if command := node.CommandElement(); command != nil {
GetCommandTranslator(command.Name())
}
}
}
/* const VK_MAX_PHYSICAL_DEVICE_NAME_SIZE uint32 256 */
type ConstData struct {
Name Translator // e.g. VK_MAX_PHYSICAL_DEVICE_NAME_SIZE
Value Translator // e.g. 256
}
/* type VkVersion uint32 */
type DefineData struct {
Name Translator // e.g. VkVersion
HeaderVersionName Translator // e.g. VK_HEADER_VERSION
Value Translator // e.g. 0, 1, 2 or 198
}
/* type VkBool32 uint32 */
type BaseData struct {
Type Translator // e.g. uint32
Name Translator // e.g. VkBool32
}
/* type VkInstance C.VkInstance */
type HandleData struct {
Name Translator // e.g. VkPhysicalDevice
HasParent bool
ParentName Translator // e.g. VkInstance
}
/* type VkResult uint32 */
type EnumData struct {
Type Translator // e.g. uint32
Name Translator // e.g. VkResult or VK_SUCCESS
Values []EnumValueData
}
// const (VK_SUCCESS VkResult = 0)
type EnumValueData struct {
Type Translator // e.g. uint32
Name Translator // e.g. VkResult or VK_SUCCESS
Value Translator // e.g. 0
Alias bool
}
// type PFN_vkFreeFunction unsafe.Pointer
type FuncData struct {
Type Translator // e.g. unsafe.Pointer
Name Translator // e.g. PFN_vkFreeFunction
}
// type VkOffset2D struct {x int32; y int32}
type StructData struct {
Name Translator // e.g. VkOffset2D
Members []StructMemberData
ReadOnly bool
Alias Translator
}
// x int32
type StructMemberData struct {
Name Translator // e.g. x
Type Translator // e.g. int32
Value Translator
Copy bool
Length interface{}
}
// type VkClearValue C.VkClearValue
type UnionData struct {
Name Translator // e.g. VkClearValue
Members []UnionMemberData
}
// VkClearColorValue color
type UnionMemberData struct {
Name Translator // e.g. color
Type Translator // e.g. VkClearColorValue
}
// func VkGetInstanceProcAddr(instance VkInstance, pName string) PFN_vkVoidFunction
type CommandData struct {
Name Translator // e.g. VkGetInstanceProcAddr
Return Translator // e.g. PFN_vkVoidFunction
Parent Translator // i.e. first parameter type
Parameters []CommandParamData
}
// ... (instance VkInstance, pName string) ...
type CommandParamData struct {
Name Translator // e.g. pName
Type Translator // e.g. string
}
func CommandToData(node *RegistryNode, command CommandElement) (bool, interface{}) {
if len(command.Alias()) > 0 {
// TODO add support for Aliases later.
return false, nil
} else {
var returnTranslator Translator
// Cheating a little bit as the spec does not currently have any
// commands that return a pointer
if tXl8r, ok := CachedTranslator(command.Proto.Type); ok {
returnTranslator = tXl8r
} else {
returnTranslator = helperMemberTypeTranslator(command.Proto.Type, "", command.Proto.Raw, "")
}
data := CommandData{
Name: GetCommandTranslator(command.Name()),
Return: returnTranslator,
}
for k, v := range command.Params {
data.Parameters = append(data.Parameters, CommandParamData{
Name: &ReservedWordTranslator{&LiteralTranslator{v.Name}},
Type: helperMemberTypeTranslator(v.Type, v.Length, v.Raw, ""),
})
if k == 0 {
data.Parent = data.Parameters[0].Type
}
}
return true, &struct {
Template string
Data CommandData
}{"command", data}
}
}
func TypeToData(node *RegistryNode, tiepuh TypeElement) (bool, interface{}) {
switch tiepuh.Category {
case TypeCategoryDefine:
tmp := defineTypeToData(node, tiepuh)
return tmp != nil, defineTypeToData(node, tiepuh)
case TypeCategoryBasetype:
tmp := baseTypeToData(node, tiepuh)
return tmp != nil, baseTypeToData(node, tiepuh)
case TypeCategoryHandle:
tmp := handleTypeToData(node, tiepuh)
return tmp != nil, handleTypeToData(node, tiepuh)
case TypeCategoryEnum:
tmp := enumTypeToData(node, tiepuh)
return tmp != nil, enumTypeToData(node, tiepuh)
case TypeCategoryBitmask:
tmp := enumTypeToData(node, tiepuh)
return tmp != nil, enumTypeToData(node, tiepuh)
case TypeCategoryFuncpointer:
tmp := funcTypeToData(node, tiepuh)
return tmp != nil, funcTypeToData(node, tiepuh)
case TypeCategoryStruct:
tmp := structTypeToData(node, tiepuh)
return tmp != nil, structTypeToData(node, tiepuh)
case TypeCategoryUnion:
tmp := unionTypeToData(node, tiepuh)
return tmp != nil, unionTypeToData(node, tiepuh)
}
return false, nil
}
var (
// list of defines that are intentionally ignored because they don't apply
// to go, or are covered by the implementation of another define.
defineSkip map[string]bool = map[string]bool{
"VK_MAKE_VERSION": true, // deprecated
"VK_VERSION_MAJOR": true, // deprecated
"VK_VERSION_MINOR": true, // deprecated
"VK_VERSION_PATCH": true, // deprecated
"VK_API_VERSION": true, // deprecated
"VK_DEFINE_HANDLE": true, // doesn't apply to go.
"VK_USE_64_BIT_PTR_DEFINES": true, // doesn't apply to go.
"VK_NULL_HANDLE": true, // in the handle template.
"VK_DEFINE_NON_DISPATCHABLE_HANDLE": true, // doesn't apply to go.
"VK_API_VERSION_VARIANT": true, // in the version template.
"VK_API_VERSION_MAJOR": true, // in the version template.
"VK_API_VERSION_MINOR": true, // in the version template.
"VK_API_VERSION_PATCH": true, // in the version template.
"VK_API_VERSION_1_0": true, // in the version template.
"VK_API_VERSION_1_1": true, // in the version template.
"VK_API_VERSION_1_2": true, // in the version template.
"VK_API_VERSION_1_3": true, // in the version template.
"VK_MAKE_API_VERSION": true, // in the version template.
}
headerVersionRegexp *regexp.Regexp = regexp.MustCompile("(\\d+)$")
vulkanVersionRegexp *regexp.Regexp = regexp.MustCompile("\\((\\d+, \\d+, \\d+), VK_HEADER_VERSION\\)$")
)
func defineTypeToData(node *RegistryNode, tiepuh TypeElement) *struct {
Template string
Data DefineData
} {
if defineSkip[tiepuh.Name()] {
return nil
}
t := "version"
data := DefineData{
Name: GetDefineTranslator("VK_API_VERSION"),
HeaderVersionName: GetDefineTranslator("VK_HEADER_VERSION"),
}
switch tiepuh.Name() {
case "VK_HEADER_VERSION":
vals := headerVersionRegexp.FindStringSubmatch(tiepuh.Raw)
if len(vals) < 2 {
log.Printf("encountered a %s that could not be parsed / %s", tiepuh.Name(), tiepuh.Raw)
return nil
}
data.Value = &LiteralTranslator{vals[1]}
t = "headerversion"
case "VK_HEADER_VERSION_COMPLETE":
vals := vulkanVersionRegexp.FindStringSubmatch(tiepuh.Raw)
if len(vals) < 2 {
log.Printf("encountered a %s that could not be parsed / %s", tiepuh.Name(), tiepuh.Raw)
return nil
}
data.Value = &LiteralTranslator{vals[1]}
default:
log.Printf("Encountered a define that is not handled %s", tiepuh.Name())
return nil
}
return &struct {
Template string
Data DefineData
}{t, data}
}
func baseTypeToData(node *RegistryNode, tiepuh TypeElement) *struct {
Template string
Data BaseData
} {
if len(tiepuh.TypeTag) == 0 {
return nil
}
data := BaseData{
Type: GetScalarConverter(tiepuh.TypeTag),
Name: GetBaseConverter(tiepuh.Name()),
}
return &struct {
Template string
Data BaseData
}{"base", data}
}
func handleTypeToData(node *RegistryNode, tiepuh TypeElement) *struct {
Template string
Data HandleData
} {
var data HandleData
if t, ok := CachedConverter(tiepuh.Name()); ok {
data.Name = t
} else {
data.Name = GetHandleConverter(tiepuh.Name())
}
if t, ok := CachedConverter(tiepuh.Parent); ok {
data.HasParent = true
data.ParentName = t
}
return &struct {
Template string
Data HandleData
}{"handle", data}
}
func enumTypeToData(node *RegistryNode, tiepuh TypeElement) *struct {
Template string
Data EnumData
} {
ed := EnumData{
Name: GetEnumNameConverter(tiepuh.Name()),
}
templateName := "enum"
var bitmask bool
if tiepuh.Category == TypeCategoryBitmask {
templateName = "bitmask"
if len(tiepuh.TypeTag) > 0 {
if t, ok := CachedTranslator(tiepuh.TypeTag); ok {
ed.Type = t
}
}
bitmask = true
}
for _, v := range node.EnumsParents() {
var largestValue, smallestValue float64
if v.E.Type == EnumsElementBitmask {
bitmask = true
}
for _, v := range v.N.EnumParents() {
evd := EnumValueData{
Type: ed.Name,
Name: GetEnumValueConverter(v.E.Name),
}
var valueTranslator Translator
if len(v.E.Alias) > 0 {
evd.Alias = true
if tXl8r, ok := CachedTranslator(v.E.Alias); ok {
valueTranslator = tXl8r
} else {
valueTranslator = &LiteralTranslator{v.E.Alias}
}
} else if bitmask {
if v.E.BitPos == nil && len(v.E.Value) > 0 {
valueTranslator = &LiteralTranslator{v.E.Value}
} else {
bitPos := 0
if v.E.BitPos != nil {
bitPos = *v.E.BitPos
}
if float64(uint64(1)<<bitPos) > largestValue {
largestValue = float64(uint64(1) << bitPos)
} else if float64(uint64(1)<<bitPos) < smallestValue {
smallestValue = float64(uint64(1) << bitPos)
}
valueTranslator = &BitValueTranslator{bitPos}
}
} else {
valueTranslator = &LiteralTranslator{v.E.Value}
val, _ := strconv.ParseFloat(v.E.Value, 64)
if val > largestValue {
largestValue = val
} else if val < smallestValue {
smallestValue = val
}
}
evd.Value = valueTranslator
ed.Values = append(ed.Values, evd)
}
// I should look up how golang deals with constants to avoid this noise.
if smallestValue < -0.5 && largestValue < float64((^uint32(0))>>1) {
ed.Type = Int32Translator
} else if smallestValue > -0.5 && largestValue > float64(^uint32(0)) {
ed.Type = Uint64Translator
} else if smallestValue < -0.5 && largestValue > float64((^uint32(0))>>1) {
ed.Type = Int64Translator
}
}
if ed.Type == nil {
ed.Type = Uint32Translator
}
return &struct {
Template string
Data EnumData
}{templateName, ed}
}
func funcTypeToData(node *RegistryNode, tiepuh TypeElement) *struct {
Template string
Data FuncData
} {
data := FuncData{
Type: GetScalarConverter("void*"),
Name: GetFuncConverter(tiepuh.Name()),
}
return &struct {
Template string
Data FuncData
}{"func", data}
}
func structTypeToData(node *RegistryNode, tiepuh TypeElement) *struct {
Template string
Data StructData
} {
data := StructData{
Name: GetStructConverter(tiepuh.Name()),
ReadOnly: tiepuh.ReturnedOnly,
}
if len(tiepuh.Alias) > 0 {
data.Alias = GetStructConverter(tiepuh.Alias)
return &struct {
Template string
Data StructData
}{"structalias", data}
}
lengthLookup := make(map[string]*StructMemberData, 0)
for _, v := range tiepuh.StructMembers {
subData := StructMemberData{
Name: &ExportTranslator{&ReservedWordTranslator{&LiteralTranslator{v.Name}}},
Type: helperMemberTypeTranslator(v.Type, v.Length, v.Raw, v.Comment),
}
if len(v.Values) > 0 && len(strings.Split(v.Values, ",")) == 1 && v.Name == "sType" {
subData.Value = &LiteralTranslator{v.Values}
} else if strings.ContainsAny(subData.Type.CGo(), "[]") {
subData.Copy = true
}
data.Members = append(data.Members, subData)
lengthLookup[v.Name] = &data.Members[len(data.Members)-1]
}
for k, v := range tiepuh.StructMembers {
if len(v.Length) > 0 && v.Length != "1" && len(strings.Split(v.Length, ",")) == 1 {
if ptr, ok := lengthLookup[v.Length]; ok && data.Members[k].Type.Go() != "unsafe.Pointer" {
data.Members[k].Length = ptr
}
}
}
return &struct {
Template string
Data StructData
}{"struct", data}
}
func unionTypeToData(node *RegistryNode, tiepuh TypeElement) *struct {
Template string
Data UnionData
} {
data := UnionData{
Name: GetUnionConverter(tiepuh.Name()),
}
for _, v := range tiepuh.StructMembers {
data.Members = append(data.Members, UnionMemberData{
Name: &ExportTranslator{&LiteralTranslator{v.Name}},
Type: helperMemberTypeTranslator(v.Type, v.Length, v.Raw, v.Comment),
})
}
return &struct {
Template string
Data UnionData
}{"union", data}
}
var arrayNumericSizeRegexp *regexp.Regexp = regexp.MustCompile("\\[\\d+\\]")
var arrayConstSizeRegexp *regexp.Regexp = regexp.MustCompile("\\[[^\\]]+\\]")
func helperMemberTypeTranslator(specType, length, raw, comment string) Translator {
if len(specType) == 0 {
specType = "uint32_t"
}
if len(comment) > 0 {
raw = strings.ReplaceAll(raw, fmt.Sprintf("<comment>%s</comment>", comment), "")
}
pointerCount := len(strings.Split(raw, "*")) - 1
if pointerCount > 0 && specType == "void" {
specType = "void*"
}
sliceCount := 0
if len(length) > 0 && pointerCount > 0 {
lengthValues := strings.Split(length, ",")
sliceCount = len(lengthValues)
for _, v := range lengthValues {
v = strings.TrimSpace(v)
if v == "null-terminated" {
sliceCount = sliceCount - 1
} else if v == "1" {
sliceCount = sliceCount - 1
}
}
}
if len(strings.Split(raw, "[")) > 1 {
sliceCount = sliceCount + 1
}
pointerCount = pointerCount - sliceCount
if specType == "void*" {
if pointerCount > 0 {
pointerCount = pointerCount - 1
} else {
sliceCount = sliceCount - 1
}
}
arraySize := arrayNumericSizeRegexp.FindString(raw)
if sliceCount > 0 && len(arraySize) > 2 {
sliceCount = sliceCount - 1
arraySize = arraySize[1 : len(arraySize)-1]
} else if arraySize = arrayConstSizeRegexp.FindString(raw); sliceCount > 0 && len(arraySize) > 2 {
sliceCount = sliceCount - 1
arraySize = arraySize[1 : len(arraySize)-1]
arraySize = strip.StripTags(arraySize)
} else {
arraySize = ""
}
var typeConverter Converter
if cXl8r, ok := CachedConverter(specType); ok {
typeConverter = cXl8r
} else if tXl8r, ok := CachedTranslator(specType); ok {
typeConverter = &TypeDefConverter{tXl8r}
} else {
typeConverter = &TypeDefConverter{&TypeDefTranslator{specType}}
}
for h := 0; h < pointerCount; h++ {
typeConverter = &PointerConverter{typeConverter}
}
for h := 0; h < sliceCount; h++ {
typeConverter = &SliceConverter{typeConverter}
}
if len(arraySize) > 0 {
typeConverter = &ArrayConverter{
typeConverter,
&LiteralTranslator{arraySize},
}
}
return typeConverter
}
func ConstToData(node *RegistryNode) *struct {
Template string
Data []ConstData
} {
list := make([]ConstData, 0, len(node.Parents))
for _, v := range node.EnumsParents() {
for _, v := range v.N.EnumParents() {
var data *ConstData
if len(v.E.Name) > 0 && len(v.E.Value) > 0 {
var value = v.E.Value
if (strings.HasSuffix(value, "F") || strings.HasSuffix(value, "f")) && !(strings.HasPrefix(value, "0x") || strings.HasPrefix(value, "0X")) {
value = value[:len(value)-1]
}
if strings.HasPrefix(value, "(") && strings.HasSuffix(value, ")") {
value = value[1 : len(value)-1]
}
var ulonglong bool
if strings.HasSuffix(value, "ULL") || strings.HasSuffix(value, "ull") {
ulonglong = true
value = value[:len(value)-3]
}
var ulong bool
if strings.HasSuffix(value, "U") || strings.HasSuffix(value, "u") {
ulong = true
value = value[:len(value)-1]
}
if strings.HasPrefix(value, "~") {
if ulonglong {
value = fmt.Sprintf("^uint64(%s)", value[1:])
} else if ulong {
value = fmt.Sprintf("^uint32(%s)", value[1:])
} else {
value = fmt.Sprintf("^%s", value[1:])
}
}
data = &ConstData{
Name: GetBaseConverter(v.E.Name),
Value: &LiteralTranslator{value},
}
} else if len(v.E.Name) > 0 && len(v.E.Alias) > 0 {
data = &ConstData{
Name: GetBaseConverter(v.E.Name),
Value: GetBaseConverter(v.E.Alias),
}
}
if data != nil {
list = append(list, *data)
}
}
}
return &struct {
Template string
Data []ConstData
}{"const", list}