/
binding.go
408 lines (368 loc) · 13.1 KB
/
binding.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
// Copyright ©2016 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package binding provides helpers for building autogenerated cgo bindings.
package binding
import (
"bytes"
"fmt"
"go/ast"
"go/parser"
"go/token"
"sort"
"text/template"
"unsafe"
"github.com/cznic/cc"
"github.com/cznic/xc"
)
func model() *cc.Model {
p := int(unsafe.Sizeof(uintptr(0)))
i := int(unsafe.Sizeof(int(0)))
return &cc.Model{
Items: map[cc.Kind]cc.ModelItem{
cc.Ptr: {Size: p, Align: p, StructAlign: p},
cc.UintPtr: {Size: p, Align: p, StructAlign: p},
cc.Void: {Size: 0, Align: 1, StructAlign: 1},
cc.Char: {Size: 1, Align: 1, StructAlign: 1},
cc.SChar: {Size: 1, Align: 1, StructAlign: 1},
cc.UChar: {Size: 1, Align: 1, StructAlign: 1},
cc.Short: {Size: 2, Align: 2, StructAlign: 2},
cc.UShort: {Size: 2, Align: 2, StructAlign: 2},
cc.Int: {Size: 4, Align: 4, StructAlign: 4},
cc.UInt: {Size: 4, Align: 4, StructAlign: 4},
cc.Long: {Size: i, Align: i, StructAlign: i},
cc.ULong: {Size: i, Align: i, StructAlign: i},
cc.LongLong: {Size: 8, Align: 8, StructAlign: 8},
cc.ULongLong: {Size: 8, Align: 8, StructAlign: 8},
cc.Float: {Size: 4, Align: 4, StructAlign: 4},
cc.Double: {Size: 8, Align: 8, StructAlign: 8},
cc.LongDouble: {Size: 8, Align: 8, StructAlign: 8},
cc.Bool: {Size: 1, Align: 1, StructAlign: 1},
cc.FloatComplex: {Size: 8, Align: 8, StructAlign: 8},
cc.DoubleComplex: {Size: 16, Align: 16, StructAlign: 16},
cc.LongDoubleComplex: {Size: 16, Align: 16, StructAlign: 16},
},
}
}
// TypeKey is a terse C type description.
type TypeKey struct {
IsPointer bool
Kind cc.Kind
}
var goTypes = map[TypeKey]*template.Template{
{Kind: cc.Undefined}: template.Must(template.New("<undefined>").Parse("<undefined>")),
{Kind: cc.Int}: template.Must(template.New("int").Parse("int")),
{Kind: cc.Float}: template.Must(template.New("float32").Parse("float32")),
{Kind: cc.Float, IsPointer: true}: template.Must(template.New("[]float32").Parse("[]float32")),
{Kind: cc.Double}: template.Must(template.New("float64").Parse("float64")),
{Kind: cc.Double, IsPointer: true}: template.Must(template.New("[]float64").Parse("[]float64")),
{Kind: cc.Bool}: template.Must(template.New("bool").Parse("bool")),
{Kind: cc.FloatComplex}: template.Must(template.New("complex64").Parse("complex64")),
{Kind: cc.DoubleComplex}: template.Must(template.New("complex128").Parse("complex128")),
}
// GoTypeFor returns a string representation of the given type using a mapping in
// types. GoTypeFor will panic if no type mapping is found after searching the
// user-provided types mappings and then the following mapping:
// {Kind: cc.Int}: "int",
// {Kind: cc.Float}: "float32",
// {Kind: cc.Float, IsPointer: true}: "[]float32",
// {Kind: cc.Double}: "float64",
// {Kind: cc.Double, IsPointer: true}: "[]float64",
// {Kind: cc.Bool}: "bool",
// {Kind: cc.FloatComplex}: "complex64",
// {Kind: cc.DoubleComplex}: "complex128",
func GoTypeFor(typ cc.Type, name string, types ...map[TypeKey]*template.Template) string {
if typ == nil {
return "<nil>"
}
k := typ.Kind()
isPtr := typ.Kind() == cc.Ptr
if isPtr {
k = typ.Element().Kind()
}
var buf bytes.Buffer
for _, t := range types {
if s, ok := t[TypeKey{Kind: k, IsPointer: isPtr}]; ok {
err := s.Execute(&buf, name)
if err != nil {
panic(err)
}
return buf.String()
}
}
s, ok := goTypes[TypeKey{Kind: k, IsPointer: isPtr}]
if ok {
err := s.Execute(&buf, name)
if err != nil {
panic(err)
}
return buf.String()
}
panic(fmt.Sprintf("unknown type key: %+v", TypeKey{Kind: k, IsPointer: isPtr}))
}
// GoTypeForEnum returns a string representation of the given enum type using a mapping
// in types. GoTypeForEnum will panic if no type mapping is found after searching the
// user-provided types mappings or the type is not an enum.
func GoTypeForEnum(typ cc.Type, name string, types ...map[string]*template.Template) string {
if typ == nil {
return "<nil>"
}
if typ.Kind() != cc.Enum {
panic(fmt.Sprintf("invalid type: %v", typ))
}
tag := typ.Tag()
if tag != 0 {
n := string(xc.Dict.S(tag))
for _, t := range types {
if s, ok := t[n]; ok {
var buf bytes.Buffer
err := s.Execute(&buf, name)
if err != nil {
panic(err)
}
return buf.String()
}
}
}
panic(fmt.Sprintf("unknown type: %+v", typ))
}
var cgoTypes = map[TypeKey]*template.Template{
{Kind: cc.Void, IsPointer: true}: template.Must(template.New("void*").Parse("unsafe.Pointer(&{{.}}[0])")),
{Kind: cc.Int}: template.Must(template.New("int").Parse("C.int({{.}})")),
{Kind: cc.Float}: template.Must(template.New("float").Parse("C.float({{.}})")),
{Kind: cc.Double}: template.Must(template.New("double").Parse("C.double({{.}})")),
{Kind: cc.Float, IsPointer: true}: template.Must(template.New("float*").Parse("(*C.float)(&{{.}}[0])")),
{Kind: cc.Double, IsPointer: true}: template.Must(template.New("double*").Parse("(*C.double)(&{{.}}[0])")),
{Kind: cc.Bool}: template.Must(template.New("bool").Parse("C.bool({{.}})")),
{Kind: cc.FloatComplex}: template.Must(template.New("floatcomplex").Parse("unsafe.Pointer({{.}})")),
{Kind: cc.DoubleComplex}: template.Must(template.New("doublecomplex").Parse("unsafe.Pointer({{.}})")),
{Kind: cc.FloatComplex, IsPointer: true}: template.Must(template.New("floatcomplex*").Parse("unsafe.Pointer(&{{.}}[0])")),
{Kind: cc.DoubleComplex, IsPointer: true}: template.Must(template.New("doublecomplex*").Parse("unsafe.Pointer(&{{.}}[0])")),
}
// CgoConversionFor returns a string representation of the given type using a mapping in
// types. CgoConversionFor will panic if no type mapping is found after searching the
// user-provided types mappings and then the following mapping:
// {Kind: cc.Void, IsPointer: true}: "unsafe.Pointer(&{{.}}[0])",
// {Kind: cc.Int}: "C.int({{.}})",
// {Kind: cc.Float}: "C.float({{.}})",
// {Kind: cc.Float, IsPointer: true}: "(*C.float)({{.}})",
// {Kind: cc.Double}: "C.double({{.}})",
// {Kind: cc.Double, IsPointer: true}: "(*C.double)({{.}})",
// {Kind: cc.Bool}: "C.bool({{.}})",
// {Kind: cc.FloatComplex}: "unsafe.Pointer(&{{.}})",
// {Kind: cc.DoubleComplex}: "unsafe.Pointer(&{{.}})",
// {Kind: cc.FloatComplex, IsPointer: true}: "unsafe.Pointer(&{{.}}[0])",
// {Kind: cc.DoubleComplex, IsPointer: true}: "unsafe.Pointer(&{{.}}[0])",
func CgoConversionFor(name string, typ cc.Type, types ...map[TypeKey]*template.Template) string {
if typ == nil {
return "<nil>"
}
k := typ.Kind()
isPtr := typ.Kind() == cc.Ptr
if isPtr {
k = typ.Element().Kind()
}
for _, t := range types {
if s, ok := t[TypeKey{Kind: k, IsPointer: isPtr}]; ok {
var buf bytes.Buffer
err := s.Execute(&buf, name)
if err != nil {
panic(err)
}
return buf.String()
}
}
s, ok := cgoTypes[TypeKey{Kind: k, IsPointer: isPtr}]
if ok {
var buf bytes.Buffer
err := s.Execute(&buf, name)
if err != nil {
panic(err)
}
return buf.String()
}
panic(fmt.Sprintf("unknown type key: %+v", TypeKey{Kind: k, IsPointer: isPtr}))
}
// CgoConversionForEnum returns a string representation of the given enum type using a mapping
// in types. GoTypeForEnum will panic if no type mapping is found after searching the
// user-provided types mappings or the type is not an enum.
func CgoConversionForEnum(name string, typ cc.Type, types ...map[string]*template.Template) string {
if typ == nil {
return "<nil>"
}
if typ.Kind() != cc.Enum {
panic(fmt.Sprintf("invalid type: %v", typ))
}
tag := typ.Tag()
if tag != 0 {
n := string(xc.Dict.S(tag))
for _, t := range types {
if s, ok := t[n]; ok {
var buf bytes.Buffer
err := s.Execute(&buf, name)
if err != nil {
panic(err)
}
return buf.String()
}
}
}
panic(fmt.Sprintf("unknown type: %+v", typ))
}
// LowerCaseFirst returns s with the first character lower-cased. LowerCaseFirst
// assumes s is an ASCII-represented string.
func LowerCaseFirst(s string) string {
if len(s) == 0 {
return s
}
return string(s[0]|' ') + s[1:]
}
// UpperCaseFirst returns s with the first character upper-cased. UpperCaseFirst
// assumes s is an ASCII-represented string.
func UpperCaseFirst(s string) string {
if len(s) == 0 {
return s
}
return string(s[0]&^' ') + s[1:]
}
// DocComments returns a map of method documentation comments for the package at the
// given path. The first key of the returned map is the type name and the second
// is the method name. Non-method function documentation are in docs[""].
func DocComments(path string) (docs map[string]map[string][]*ast.Comment, err error) {
fset := token.NewFileSet()
pkgs, err := parser.ParseDir(fset, path, nil, parser.ParseComments)
if err != nil {
return nil, err
}
docs = make(map[string]map[string][]*ast.Comment)
for _, p := range pkgs {
for _, f := range p.Files {
for _, n := range f.Decls {
fn, ok := n.(*ast.FuncDecl)
if !ok || fn.Doc == nil {
continue
}
var typ string
if fn.Recv != nil && len(fn.Recv.List) > 0 {
id, ok := fn.Recv.List[0].Type.(*ast.Ident)
if ok {
typ = id.Name
}
}
doc, ok := docs[typ]
if !ok {
doc = make(map[string][]*ast.Comment)
docs[typ] = doc
}
doc[fn.Name.String()] = fn.Doc.List
}
}
}
return docs, nil
}
// Declaration is a description of a C function declaration.
type Declaration struct {
Pos token.Pos
Name string
Return cc.Type
CParameters []cc.Parameter
Variadic bool
}
// Position returns the token position of the declaration.
func (d *Declaration) Position() token.Position { return xc.FileSet.Position(d.Pos) }
// Parameter is a C function parameter.
type Parameter struct{ Parameter cc.Parameter }
// Name returns the name of the parameter.
func (p *Parameter) Name() string { return string(xc.Dict.S(p.Parameter.Name)) }
// Type returns the C type of the parameter.
func (p *Parameter) Type() cc.Type { return p.Parameter.Type }
// Kind returns the C kind of the parameter.
func (p *Parameter) Kind() cc.Kind { return p.Parameter.Type.Kind() }
// Elem returns the pointer type of a pointer parameter or the element type of an
// array parameter.
func (p *Parameter) Elem() cc.Type { return p.Parameter.Type.Element() }
// Parameters returns the declaration's CParameters converted to a []Parameter.
func (d *Declaration) Parameters() []Parameter {
p := make([]Parameter, len(d.CParameters))
for i, c := range d.CParameters {
p[i] = Parameter{c}
}
return p
}
// Declarations returns the C function declarations in the givel set of file paths.
func Declarations(paths ...string) ([]Declaration, error) {
predefined, includePaths, sysIncludePaths, err := cc.HostConfig()
if err != nil {
return nil, fmt.Errorf("binding: failed to get host config: %v", err)
}
t, err := cc.Parse(
predefined+`
#define __const const
#define __attribute__(...)
#define __extension__
#define __inline
#define __restrict
unsigned __builtin_bswap32 (unsigned x);
unsigned long long __builtin_bswap64 (unsigned long long x);
`,
paths,
model(),
cc.IncludePaths(includePaths),
cc.SysIncludePaths(sysIncludePaths),
)
if err != nil {
return nil, fmt.Errorf("binding: failed to parse %q: %v", paths, err)
}
var decls []Declaration
for ; t != nil; t = t.TranslationUnit {
if t.ExternalDeclaration.Case != 1 /* Declaration */ {
continue
}
d := t.ExternalDeclaration.Declaration
if d.Case != 0 {
// Other case is 1: StaticAssertDeclaration.
continue
}
init := d.InitDeclaratorListOpt
if init == nil {
continue
}
idl := init.InitDeclaratorList
if idl.InitDeclaratorList != nil {
// We do not want comma-separated lists.
continue
}
id := idl.InitDeclarator
if id.Case != 0 {
// We do not want assignments.
continue
}
declarator := id.Declarator
if declarator.Type.Kind() != cc.Function {
// We want only functions.
continue
}
params, variadic := declarator.Type.Parameters()
name, _ := declarator.Identifier()
decls = append(decls, Declaration{
Pos: declarator.Pos(),
Name: string(xc.Dict.S(name)),
Return: declarator.Type.Result(),
CParameters: params,
Variadic: variadic,
})
}
sort.Sort(byPosition(decls))
return decls, nil
}
type byPosition []Declaration
func (d byPosition) Len() int { return len(d) }
func (d byPosition) Less(i, j int) bool {
iPos := d[i].Position()
jPos := d[j].Position()
if iPos.Filename == jPos.Filename {
return iPos.Line < jPos.Line
}
return iPos.Filename < jPos.Filename
}
func (d byPosition) Swap(i, j int) { d[i], d[j] = d[j], d[i] }