forked from go-gl/glow
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtype.go
208 lines (193 loc) · 6.62 KB
/
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
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
package main
import (
"fmt"
"strings"
)
// A Type describes the C and Go type of a function parameter or return value.
type Type struct {
Name string // Name of the type without modifiers
PointerLevel int // Number of levels of declared indirection to the type
CDefinition string // Raw C definition
Cast string // Raw C cast in case conversion is necessary
}
// A Typedef describes a C typedef statement.
type Typedef struct {
Name string // Name of the defined type (or included types)
CDefinition string // Raw C definition
}
func (t Type) String() string {
return fmt.Sprintf("%s%s [%s]", t.Name, t.pointers(), t.CDefinition)
}
// IsVoid indicates whether this type is the void pseudo-type.
func (t Type) IsVoid() bool {
return (t.Name == "void" || t.Name == "GLvoid") && t.PointerLevel == 0
}
// IsDebugProc indicates whether this type is a debug callback function pointer.
func (t Type) IsDebugProc() bool {
return t.Name == "GLDEBUGPROC" || t.Name == "GLDEBUGPROCARB" || t.Name == "GLDEBUGPROCKHR"
}
// CType returns the C definition of the type.
func (t Type) CType() string {
return t.CDefinition
}
// GoCType returns the Go definition of the C type.
func (t Type) GoCType() string {
if strings.HasPrefix(t.Name, "struct ") {
return t.pointers() + "C.struct_" + strings.TrimPrefix(t.Name, "struct ")
}
return t.pointers() + "C." + t.Name
}
// GoType returns the Go definition of the type.
func (t Type) GoType() string {
switch t.Name {
case "GLbyte":
return t.pointers() + "int8"
case "GLubyte":
return t.pointers() + "uint8"
case "GLshort":
return t.pointers() + "int16"
case "GLushort":
return t.pointers() + "uint16"
case "GLint":
return t.pointers() + "int32"
case "GLuint":
return t.pointers() + "uint32"
case "GLint64", "GLint64EXT":
return t.pointers() + "int64"
case "GLuint64", "GLuint64EXT":
return t.pointers() + "uint64"
case "GLfloat", "GLclampf":
return t.pointers() + "float32"
case "GLdouble", "GLclampd":
return t.pointers() + "float64"
case "GLclampx":
return t.pointers() + "int32"
case "GLsizei":
return t.pointers() + "int32"
case "GLfixed":
return t.pointers() + "int32"
case "GLchar", "GLcharARB":
// Chosen (vs. byte) for compatibility where GL uses GLubyte for strings (e.g., glGetString)
return t.pointers() + "uint8"
case "GLboolean":
return t.pointers() + "bool"
case "GLenum", "GLbitfield":
return t.pointers() + "uint32"
case "GLhalf", "GLhalfNV":
// Go has no 16-bit floating point type
return t.pointers() + "uint16"
case "void", "GLvoid":
// Type void* could map to either uintptr or unsafe.Pointer but we use the
// latter because pointers passed from Go to C need to use Go pointer types
// for the sake of the GC
if t.PointerLevel == 1 {
return "unsafe.Pointer"
} else if t.PointerLevel == 2 {
return "*unsafe.Pointer"
}
case "GLintptr", "GLintptrARB":
// Type ptrdiff_t should map to an architecture-width Go int
return t.pointers() + "int"
case "GLsizeiptr", "GLsizeiptrARB":
// Same as GLintptr
return t.pointers() + "int"
case "GLhandleARB", "GLeglImagesOES", "GLvdpauSurfaceNV":
// OpenGL pointer types should fit in a pointer-width Go type. No need to
// use unsafe.Pointer because there is no need for the Go GC to understand
// that these are pointer types. Moreover on most platforms GLhandleARB is
// an integer type.
return t.pointers() + "uintptr"
case "GLsync":
// GLsync is treated as an opaque, pointer-width type. Additional special
// case handling is required for the corresponding typedef, see CTypedef.
return t.pointers() + "uintptr"
case "GLDEBUGPROC", "GLDEBUGPROCARB", "GLDEBUGPROCKHR":
// Special case mapping to the type defined in debug.tmpl
return "DebugProc"
case "uintptr_t":
return t.pointers() + "uintptr"
}
return "unsafe.Pointer"
}
// ConvertGoToC returns an expression that converts a variable from the Go type to the C type.
func (t Type) ConvertGoToC(name string) string {
switch t.Name {
case "GLboolean":
if t.PointerLevel == 0 {
return fmt.Sprintf("(C.GLboolean)(boolToInt(%s))", name)
}
case "void", "GLvoid":
return name
case "GLDEBUGPROC", "GLDEBUGPROCARB", "GLDEBUGPROCKHR":
return fmt.Sprintf("(C.%s)(unsafe.Pointer(&%s))", t.Name, name)
}
if t.PointerLevel >= 1 && t.GoType() != "unsafe.Pointer" {
return fmt.Sprintf("(%s)(unsafe.Pointer(%s))", t.GoCType(), name)
}
return fmt.Sprintf("(%s)(%s)", t.GoCType(), name)
}
// ConvertGoToUintptr returns an expression that converts a variable from the Go type to uintptr type for syscall.Syscall.
func (t Type) ConvertGoToUintptr(name string) string {
switch t.Name {
case "GLboolean":
if t.PointerLevel == 0 {
return fmt.Sprintf("boolToUintptr(%s)", name)
}
case "GLfloat", "GLclampf":
if t.PointerLevel == 0 {
return fmt.Sprintf("uintptr(math.Float32bits(%s))", name)
}
case "GLdouble", "GLclampd":
if t.PointerLevel == 0 {
return fmt.Sprintf("uintptr(math.Float64bits(%s))", name)
}
case "GLDEBUGPROC", "GLDEBUGPROCARB", "GLDEBUGPROCKHR":
return fmt.Sprintf("newDebugProcCallback(%s)", name)
}
if t.PointerLevel >= 1 && t.GoType() != "unsafe.Pointer" {
return fmt.Sprintf("uintptr(unsafe.Pointer(%s))", name)
}
return fmt.Sprintf("uintptr(%s)", name)
}
// ConvertCToGo converts from the C type to the Go type.
func (t Type) ConvertCToGo(name string) string {
if t.Name == "GLboolean" && t.PointerLevel == 0 {
return fmt.Sprintf("%s == TRUE", name)
}
return fmt.Sprintf("(%s)(%s)", t.GoType(), name)
}
// ConvertUintptrToGo converts from the uintptr type to the Go type.
func (t Type) ConvertUintptrToGo(name string) string {
switch t.Name {
case "GLboolean":
if t.PointerLevel == 0 {
return fmt.Sprintf("%s != 0", name)
}
case "GLfloat", "GLclampf":
if t.PointerLevel == 0 {
return fmt.Sprintf("math.Float32frombits(uint32(%s))", name)
}
case "GLdouble", "GLclampd":
if t.PointerLevel == 0 {
return fmt.Sprintf("math.Float64frombits(uint64(%s))", name)
}
}
if t.PointerLevel >= 1 && t.GoType() != "unsafe.Pointer" {
return fmt.Sprintf("(%s)(unsafe.Pointer(%s))", t.GoType(), name)
}
return fmt.Sprintf("(%s)(%s)", t.GoType(), name)
}
func (t Type) pointers() string {
return strings.Repeat("*", t.PointerLevel)
}
// CTypedef returns the C definition of the typedef.
func (t Typedef) CTypedef() string {
// GLsync is defined as an opaque struct pointer, but some OpenGL drivers
// store a non-pointer numeric handle in the type, which is incompatible with
// Go pointer semantics. As a workaround we redefined GLsync to be an opaque
// pointer-width type.
if strings.Contains(t.CDefinition, "GLsync") {
return "typedef uintptr_t GLsync;"
}
return t.CDefinition
}