/
union_type.go
182 lines (142 loc) · 5.58 KB
/
union_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
package def
import (
"fmt"
"io"
"strings"
"github.com/antchfx/xmlquery"
"github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
)
type unionType struct {
structType
internalByteSize string
}
func (t *unionType) Category() TypeCategory { return CatUnion }
func (t *unionType) IsIdenticalPublicAndInternal() bool { return false }
func (t *unionType) Resolve(tr TypeRegistry, vr ValueRegistry) *IncludeSet {
if t.isResolved {
return NewIncludeSet()
}
is := t.structType.Resolve(tr, vr)
is.ResolvedTypes[t.registryName] = t
t.isResolved = true
return is
}
func (t *unionType) PrintPublicDeclaration(w io.Writer) {
t.PrintDocLink(w)
fmt.Fprintf(w, "type %s struct {\n", t.PublicName())
for _, m := range t.members {
m.PrintPublicDeclaration(w)
fmt.Fprintf(w, "as%s bool\n", m.PublicName())
}
fmt.Fprintf(w, "}\n\n")
for i, m := range t.members {
if m.pointerDepth > 0 && m.resolvedType.PublicName() != "string" {
if m.resolvedType.PublicName() == "unsafe.Pointer" || m.resolvedType.Category() == CatPointer { // bugfix/issue-16
fmt.Fprintf(w, "func (u *%s) As%s(ptr %s) {\n",
t.PublicName(), m.PublicName(), m.resolvedType.PublicName(),
)
fmt.Fprintf(w, " u.%s = ptr\n", m.PublicName())
} else {
fmt.Fprintf(w, "func (u *%s) As%s(vals []%s) {\n",
t.PublicName(), m.PublicName(), m.resolvedType.PublicName(),
)
fmt.Fprintf(w, " copy(u.%s[:], vals)\n", m.PublicName())
}
} else {
fmt.Fprintf(w, "func (u *%s) As%s(val %s) {\n",
t.PublicName(), m.PublicName(), m.resolvedType.PublicName(),
)
fmt.Fprintf(w, " u.%s = val\n", m.PublicName())
}
for j, n := range t.members {
fmt.Fprintf(w, " u.as%s = %v\n", n.PublicName(), i == j)
}
fmt.Fprintf(w, "}\n\n")
}
}
func (t *unionType) PrintInternalDeclaration(w io.Writer) {
var preamble, structDecl, epilogue strings.Builder
if t.isReturnedOnly {
logrus.WithField("registry type", t.registryName).
Error("union is returned only, which is not yet handled in the binding")
}
// _vk type declaration
var sizeString = t.internalByteSize
if t.internalByteSize == "" {
switch t.members[0].resolvedType.Category() { // updated with bugfix/issue-16
case CatPointer:
sizeString = fmt.Sprintf("unsafe.Sizeof((%s)(nil))", t.members[0].resolvedType.InternalName()) // Internal name will include the pointer and the underlying type
case CatArray:
fallthrough // Array will use the same syntax as structs
case CatStruct:
sizeString = fmt.Sprintf("unsafe.Sizeof(%s{})", t.members[0].resolvedType.InternalName())
default:
sizeString = fmt.Sprintf("unsafe.Sizeof(%s(0))", t.members[0].resolvedType.InternalName()) // fallthrough to assmption that this is a primitve type
}
}
fmt.Fprintf(w, "type %s [%s]byte\n", t.InternalName(), sizeString)
fmt.Fprintf(w, "func (u *%s) Vulkanize() *%s {\n", t.PublicName(), t.InternalName())
fmt.Fprintf(w, " switch true {\n")
for _, m := range t.members {
fmt.Fprintf(w, " case u.as%s:\n", m.PublicName())
fmt.Fprintf(w, " return (*%s)(unsafe.Pointer(&u.%s))\n", t.InternalName(), m.PublicName())
// TODO should be tested but I think there is a bug here. If I have a union with mixed 32 and 64 bit types, and I cast a
// 32 bit field as 64 bits (as an 8 byte array), will the field be in the most significant bits of the array?
// Where does Vulkan/C expect them to be?
}
fmt.Fprintf(w, " default:\nreturn &%s{}\n", t.InternalName())
fmt.Fprintf(w, " }\n")
fmt.Fprintf(w, "}\n")
// Don't attempt to Goify for now. There may be a command that returns a union, how to handle that TBD
// fmt.Fprintf(w, "func (u *%s) Goify() %s {\n", t.InternalName(), t.PublicName())
// fmt.Fprintf(w, " panic(\"Cannot Goify to a Vulkan union type!\")\n")
// fmt.Fprintf(w, "}\n")
fmt.Fprint(w, preamble.String(), structDecl.String(), epilogue.String())
}
func (t *unionType) TranslateToInternal(inputVar string) string {
if t.IsIdenticalPublicAndInternal() {
return fmt.Sprintf("%s(%s)", t.InternalName(), inputVar)
} else {
return fmt.Sprintf("*%s.Vulkanize()", inputVar)
}
}
func ReadUnionTypesFromXML(doc *xmlquery.Node, tr TypeRegistry, vr ValueRegistry, api string) {
queryString := fmt.Sprintf("//types/type[@category='union' and (@api='%s' or not(@api))]", api)
for _, node := range xmlquery.Find(doc, queryString) {
s := newUnionTypeFromXML(node, api)
tr[s.RegistryName()] = s
}
}
func newUnionTypeFromXML(node *xmlquery.Node, api string) *unionType {
rval := unionType{}
rval.registryName = node.SelectAttr("name")
rval.isReturnedOnly = node.SelectAttr("returnedonly") == "true"
queryString := fmt.Sprintf("member[@api='%s' or not(@api)]", api)
for _, mNode := range xmlquery.Find(node, queryString) {
rval.members = append(rval.members, newStructMemberFromXML(mNode))
}
return &rval
}
func ReadUnionExceptionsFromJSON(exceptions gjson.Result, tr TypeRegistry, vr ValueRegistry) {
exceptions.Get("union").ForEach(func(key, exVal gjson.Result) bool {
if key.String() == "!comment" {
return true
} // Ignore comments
if entry, found := tr[key.String()]; !found {
logrus.WithField("registry type", key.String()).
Warn("no existing registry type for union exception")
} else {
if entry.Category() != CatUnion {
logrus.WithField("registry type", key.String()).
WithField("category", entry.Category().String()).
Error("exception for union type was not a union in the registry")
}
UpdateUnionTypeFromJSON(key, exVal, entry.(*unionType))
}
return true
})
}
func UpdateUnionTypeFromJSON(key, json gjson.Result, td *unionType) {
td.internalByteSize = json.Get("go:internalSize").String()
}