-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.go
216 lines (191 loc) · 5.02 KB
/
functions.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
// Copyright 2013 The GoGL2 Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE.mkd file.
package main
import (
"fmt"
"io"
"sort"
"strings"
)
type Parameter struct {
Name string
Type Type
}
type Function struct {
Name string
Parameters []Parameter
Return Type
}
type Functions map[string]*Function
type SortedFunctions []*Function
func (sf SortedFunctions) Len() int {
return len(sf)
}
func (sf SortedFunctions) Swap(i, j int) {
sf[i], sf[j] = sf[j], sf[i]
}
func (sf SortedFunctions) Less(i, j int) bool {
return sf[i].Name < sf[j].Name
}
func (f *Function) writeCParameters(w io.Writer) {
for i := 0; i < len(f.Parameters); i++ {
p := &f.Parameters[i]
if i != 0 {
fmt.Fprintf(w, ", ")
}
fmt.Fprintf(w, "%s %s", p.Type.CType(), RenameIfReservedCWord(p.Name))
}
}
func (f *Function) WriteCFunctionPtr(w io.Writer) {
ctype := f.Return.CType()
fmt.Fprintf(w, "// %s (APIENTRYP pgl%s)(", ctype, f.Name)
f.writeCParameters(w)
fmt.Fprintln(w, ");")
}
func (f *Function) WriteCFunctionPtrTypedef(w io.Writer) {
ctype := f.Return.CType()
fmt.Fprintf(w, "// typedef %s (APIENTRYP PGL%s)(", ctype, strings.ToUpper(f.Name))
f.writeCParameters(w)
fmt.Fprintln(w, ");")
}
func (f *Function) WriteCDeclaration(w io.Writer) {
ctype := f.Return.CType()
fmt.Fprintf(w, "// GLAPI %s APIENTRY gl%s(", ctype, f.Name)
f.writeCParameters(w)
fmt.Fprintln(w, ");")
}
func (f *Function) WriteCBridgeDefinition(w io.Writer) {
ctype := f.Return.CType()
fmt.Fprintf(w, "// %s gogl%s(PGL%s glfptr", ctype, f.Name, strings.ToUpper(f.Name))
if len(f.Parameters) != 0 {
fmt.Fprintf(w, ", ")
}
f.writeCParameters(w)
fmt.Fprintln(w, ") {")
if f.Return.IsVoid() {
fmt.Fprintf(w, "// ")
} else {
fmt.Fprintf(w, "// return ")
}
fmt.Fprintf(w, "(*glfptr)(")
for i, _ := range f.Parameters {
p := &f.Parameters[i]
if i != 0 {
fmt.Fprintf(w, ", ")
}
fmt.Fprintf(w, "%s", p.Name)
}
fmt.Fprintln(w, ");")
fmt.Fprintln(w, "// }")
}
func (f *Function) WriteGoFunctionPtr(w io.Writer) {
fmt.Fprintf(w, " pgl%s C.PGL%s\n", f.Name, strings.ToUpper(f.Name))
}
func (f *Function) WriteGoGetProcAddress(w io.Writer) {
fmt.Fprintf(w, " if pgl%s = (C.PGL%s)(unsafe.Pointer(glt.GetProcAddress(\"gl%s\"))); pgl%s == nil { return errors.New(\"gl%s\") }\n", f.Name, strings.ToUpper(f.Name), f.Name, f.Name, f.Name)
}
func (f *Function) WriteGoDefinition(w io.Writer, usePtr bool, d *Documentation, majorVersion int) {
err := d.WriteGoCmdDoc(w, f.Name, majorVersion)
if err != nil {
//fmt.Printf("Unable to find function doc: %v\n", err)
}
fmt.Fprintf(w, "func %s(", f.Name)
for i, _ := range f.Parameters {
p := &f.Parameters[i]
if i != 0 {
fmt.Fprintf(w, ", ")
}
fmt.Fprintf(w, "%s %s", RenameIfReservedGoWord(p.Name), p.Type.GoType())
}
if f.Return.IsVoid() {
fmt.Fprintln(w, ") {")
if usePtr {
fmt.Fprintf(w, " C.gogl%s(pgl%s", f.Name, f.Name)
if len(f.Parameters) != 0 {
fmt.Fprintf(w, ", ")
}
} else {
fmt.Fprintf(w, " C.gl%s(", f.Name)
}
} else {
ctype := f.Return.GoType()
tconv := f.Return.GoConversion()
fmt.Fprintf(w, ") %s {\n", ctype)
if usePtr {
fmt.Fprintf(w, "\treturn %s(C.gogl%s(pgl%s", tconv, f.Name, f.Name)
if len(f.Parameters) != 0 {
fmt.Fprintf(w, ", ")
}
} else {
fmt.Fprintf(w, "\treturn %s(C.gl%s(", tconv, f.Name)
}
}
for i, _ := range f.Parameters {
p := &f.Parameters[i]
if i != 0 {
fmt.Fprintf(w, ", ")
}
tconv := p.Type.CgoConversion()
fmt.Fprintf(w, "%s(%s)", tconv, RenameIfReservedGoWord(p.Name))
}
if f.Return.IsVoid() {
fmt.Fprintln(w, ")")
} else {
fmt.Fprintln(w, "))")
}
fmt.Fprintln(w, "}")
}
func (fs Functions) Sort() SortedFunctions {
sortedFunctions := make(SortedFunctions, 0, len(fs))
for _, f := range fs {
sortedFunctions = append(sortedFunctions, f)
}
sort.Sort(sortedFunctions)
return sortedFunctions
}
func (sf SortedFunctions) WriteCFunctionPtrs(w io.Writer) {
for _, f := range sf {
f.WriteCFunctionPtr(w)
}
fmt.Fprintln(w, "// ")
}
func (sf SortedFunctions) WriteCFunctionPtrTypedefs(w io.Writer) {
for _, f := range sf {
f.WriteCFunctionPtrTypedef(w)
}
fmt.Fprintln(w, "// ")
}
func (sf SortedFunctions) WriteCDeclarations(w io.Writer) {
for _, f := range sf {
f.WriteCDeclaration(w)
}
fmt.Fprintln(w, "// ")
}
func (sf SortedFunctions) WriteCBridgeDefinitions(w io.Writer) {
for _, f := range sf {
f.WriteCBridgeDefinition(w)
}
fmt.Fprintln(w, "// ")
}
func (sf SortedFunctions) WriteGoFunctionPtrs(w io.Writer) {
fmt.Fprintln(w, "var (")
for _, f := range sf {
f.WriteGoFunctionPtr(w)
}
fmt.Fprintln(w, ")")
}
func (sf SortedFunctions) WriteGoInitPackage(w io.Writer) {
fmt.Fprintln(w, "func Init() error {")
for _, f := range sf {
f.WriteGoGetProcAddress(w)
}
fmt.Fprintln(w, " return nil")
fmt.Fprintln(w, "}")
}
func (sf SortedFunctions) WriteGoDefinitions(w io.Writer, usePtr bool, d *Documentation, majorVersion int) {
for _, f := range sf {
f.WriteGoDefinition(w, usePtr, d, majorVersion)
}
fmt.Fprintln(w, "")
}