/
type.go
763 lines (662 loc) · 17.1 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
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
package ffi
import (
"fmt"
"reflect"
"unsafe"
)
// #include "ffi.h"
// static void _go_ffi_type_set_type(ffi_type *t, unsigned short type)
// {
// t->type = type;
// }
// static unsigned short _go_ffi_type_get_type(ffi_type *t)
// {
// return t->type;
// }
// static void _go_ffi_type_set_elements(ffi_type *t, void *elmts)
// {
// t->elements = (ffi_type**)(elmts);
// }
// static void *_go_ffi_type_get_offset(void *data, unsigned n, ffi_type **types)
// {
// size_t ofs = 0;
// unsigned i;
// unsigned short a;
// for (i = 0; i < n && types[i]; i++) {
// a = ofs % types[i]->alignment;
// if (a != 0) ofs += types[i]->alignment-a;
// ofs += types[i]->size;
// }
// if (i < n || !types[i])
// return 0;
// a = ofs % types[i]->alignment;
// if (a != 0) ofs += types[i]->alignment-a;
// return data+ofs;
// }
// static int _go_ffi_type_get_offsetof(ffi_type *t, int i)
// {
// void *v;
// void *data = NULL + 2; // make a non-null pointer
// if (t->type != FFI_TYPE_STRUCT) return 0;
// v = _go_ffi_type_get_offset(data, i, t->elements);
// if (v) {
// return (int)(v - data);
// } else {
// return 0;
// }
// return 0;
// }
import "C"
type Kind uint
const (
Void Kind = C.FFI_TYPE_VOID
Int Kind = C.FFI_TYPE_INT
Float Kind = C.FFI_TYPE_FLOAT
Double Kind = C.FFI_TYPE_DOUBLE
LongDouble Kind = C.FFI_TYPE_LONGDOUBLE
Uint8 Kind = C.FFI_TYPE_UINT8
Int8 Kind = C.FFI_TYPE_SINT8
Uint16 Kind = C.FFI_TYPE_UINT16
Int16 Kind = C.FFI_TYPE_SINT16
Uint32 Kind = C.FFI_TYPE_UINT32
Int32 Kind = C.FFI_TYPE_SINT32
Uint64 Kind = C.FFI_TYPE_UINT64
Int64 Kind = C.FFI_TYPE_SINT64
Struct Kind = C.FFI_TYPE_STRUCT
Ptr Kind = C.FFI_TYPE_POINTER
//FIXME
Array Kind = 255 + iota
Slice
String
)
func (k Kind) String() string {
switch k {
case Void:
return "Void"
case Int:
return "Int"
case Float:
return "Float"
case Double:
return "Double"
case LongDouble:
return "LongDouble"
case Uint8:
return "Uint8"
case Int8:
return "Int8"
case Uint16:
return "Uint16"
case Int16:
return "Int16"
case Uint32:
return "Uint32"
case Int32:
return "Int32"
case Uint64:
return "Uint64"
case Int64:
return "Int64"
case Struct:
return "Struct"
case Ptr:
return "Ptr"
case Array:
return "Array"
case Slice:
return "Slice"
case String:
return "String"
}
panic("unreachable")
}
// Type is a FFI type, describing functions' type arguments
type Type interface {
cptr() *C.ffi_type
// Name returns the type's name.
Name() string
// Size returns the number of bytes needed to store
// a value of the given type.
Size() uintptr
// String returns a string representation of the type.
String() string
// Kind returns the specific kind of this type
Kind() Kind
// Align returns the alignment in bytes of a value of this type.
Align() int
// Len returns an array type's length
// It panics if the type's Kind is not Array.
Len() int
// Elem returns a type's element type.
// It panics if the type's Kind is not Array or Ptr
Elem() Type
// Field returns a struct type's i'th field.
// It panics if the type's Kind is not Struct.
// It panics if i is not in the range [0, NumField()).
Field(i int) StructField
// NumField returns a struct type's field count.
// It panics if the type's Kind is not Struct.
NumField() int
// GoType returns the reflect.Type this ffi.Type is mirroring
// It returns nil if there is no such equivalent go type.
GoType() reflect.Type
// set_gotype sets the reflect.Type associated with this ffi.Type
set_gotype(t reflect.Type)
}
type cffi_type struct {
n string
c *C.ffi_type
rt reflect.Type
}
func (t *cffi_type) cptr() *C.ffi_type {
return t.c
}
func (t *cffi_type) Name() string {
return t.n
}
func (t *cffi_type) Size() uintptr {
return uintptr(t.c.size)
}
func (t *cffi_type) String() string {
// fixme:
return t.n
}
func (t *cffi_type) Kind() Kind {
return Kind(C._go_ffi_type_get_type(t.c))
}
func (t *cffi_type) Align() int {
return int(t.c.alignment)
}
func (t *cffi_type) Len() int {
if t.Kind() != Array {
panic("ffi: Len of non-array type")
}
tt := (*cffi_array)(unsafe.Pointer(&t))
return tt.Len()
}
func (t *cffi_type) Elem() Type {
switch t.Kind() {
case Array:
tt := (*cffi_array)(unsafe.Pointer(&t))
return tt.Elem()
case Ptr:
tt := (*cffi_ptr)(unsafe.Pointer(&t))
return tt.Elem()
case Slice:
tt := (*cffi_slice)(unsafe.Pointer(&t))
return tt.Elem()
}
panic("ffi: Elem of invalid type")
}
func (t *cffi_type) NumField() int {
if t.Kind() != Struct {
panic("ffi: NumField of non-struct type")
}
tt := (*cffi_struct)(unsafe.Pointer(&t))
return tt.NumField()
}
func (t *cffi_type) Field(i int) StructField {
if t.Kind() != Struct {
panic("ffi: Field of non-struct type")
}
tt := (*cffi_struct)(unsafe.Pointer(&t))
return tt.Field(i)
}
func (t *cffi_type) GoType() reflect.Type {
return t.rt
}
func (t *cffi_type) set_gotype(rt reflect.Type) {
t.rt = rt
}
var (
C_void Type = &cffi_type{"void", &C.ffi_type_void, nil}
C_uchar = &cffi_type{"unsigned char", &C.ffi_type_uchar, reflect.TypeOf(uint8(0))}
C_char = &cffi_type{"char", &C.ffi_type_schar, reflect.TypeOf(int8(0))}
C_ushort = &cffi_type{"unsigned short", &C.ffi_type_ushort, reflect.TypeOf(uint16(0))}
C_short = &cffi_type{"short", &C.ffi_type_sshort, reflect.TypeOf(int16(0))}
C_uint = &cffi_type{"unsigned int", &C.ffi_type_uint, reflect.TypeOf(uint(0))}
C_int = &cffi_type{"int", &C.ffi_type_sint, reflect.TypeOf(int(0))}
C_ulong = &cffi_type{"unsigned long", &C.ffi_type_ulong, reflect.TypeOf(uint64(0))}
C_long = &cffi_type{"long", &C.ffi_type_slong, reflect.TypeOf(int64(0))}
C_uint8 = &cffi_type{"uint8", &C.ffi_type_uint8, reflect.TypeOf(uint8(0))}
C_int8 = &cffi_type{"int8", &C.ffi_type_sint8, reflect.TypeOf(int8(0))}
C_uint16 = &cffi_type{"uint16", &C.ffi_type_uint16, reflect.TypeOf(uint16(0))}
C_int16 = &cffi_type{"int16", &C.ffi_type_sint16, reflect.TypeOf(int16(0))}
C_uint32 = &cffi_type{"uint32", &C.ffi_type_uint32, reflect.TypeOf(uint32(0))}
C_int32 = &cffi_type{"int32", &C.ffi_type_sint32, reflect.TypeOf(int32(0))}
C_uint64 = &cffi_type{"uint64", &C.ffi_type_uint64, reflect.TypeOf(uint64(0))}
C_int64 = &cffi_type{"int64", &C.ffi_type_sint64, reflect.TypeOf(int64(0))}
C_float = &cffi_type{"float", &C.ffi_type_float, reflect.TypeOf(float32(0.))}
C_double = &cffi_type{"double", &C.ffi_type_double, reflect.TypeOf(float64(0.))}
C_longdouble = &cffi_type{"long double", &C.ffi_type_longdouble, nil}
C_pointer = &cffi_type{"*", &C.ffi_type_pointer, reflect.TypeOf(nil)}
)
type StructField struct {
Name string // Name is the field name
Type Type // field type
Offset uintptr // offset within struct, in bytes
}
type cffi_struct struct {
cffi_type
fields []StructField
}
func (t *cffi_struct) NumField() int {
return len(t.fields)
}
func (t *cffi_struct) Field(i int) StructField {
if i < 0 || i >= len(t.fields) {
panic("ffi: field index out of range")
}
return t.fields[i]
}
func (t *cffi_struct) set_gotype(rt reflect.Type) {
t.cffi_type.rt = rt
}
type Field struct {
Name string // Name is the field name
Type Type // field type
}
var g_id_ch chan int
// NewStructType creates a new ffi_type describing a C-struct
func NewStructType(name string, fields []Field) (Type, error) {
if name == "" {
// anonymous type...
// generate some id.
name = fmt.Sprintf("_ffi_anon_type_%d", <-g_id_ch)
}
if t := TypeByName(name); t != nil {
// check the definitions are the same
if t.NumField() != len(fields) {
return nil, fmt.Errorf("ffi.NewStructType: inconsistent re-declaration of [%s]", name)
}
for i := range fields {
if fields[i].Name != t.Field(i).Name {
return nil, fmt.Errorf("ffi.NewStructType: inconsistent re-declaration of [%s] (field #%d name mismatch)", name, i)
}
if fields[i].Type != t.Field(i).Type {
return nil, fmt.Errorf("ffi.NewStructType: inconsistent re-declaration of [%s] (field #%d type mismatch)", name, i)
}
}
return t, nil
}
c := C.ffi_type{}
t := &cffi_struct{
cffi_type: cffi_type{n: name, c: &c},
fields: make([]StructField, len(fields)),
}
t.cffi_type.c.size = 0
t.cffi_type.c.alignment = 0
C._go_ffi_type_set_type(t.cptr(), C.FFI_TYPE_STRUCT)
var c_fields **C.ffi_type = nil
if len(fields) > 0 {
var cargs = make([]*C.ffi_type, len(fields)+1)
for i, f := range fields {
cargs[i] = f.Type.cptr()
}
cargs[len(fields)] = nil
c_fields = &cargs[0]
}
C._go_ffi_type_set_elements(t.cptr(), unsafe.Pointer(c_fields))
// initialize type (computes alignment and size)
_, err := NewCif(DefaultAbi, t, nil)
if err != nil {
return nil, err
}
for i := 0; i < len(fields); i++ {
//cft := C._go_ffi_type_get_element(t.cptr(), C.int(i))
ff := fields[i]
t.fields[i] = StructField{
ff.Name,
TypeByName(ff.Type.Name()),
uintptr(C._go_ffi_type_get_offsetof(t.cptr(), C.int(i))),
}
}
register_type(t)
return t, nil
}
type cffi_array struct {
cffi_type
len int
elem Type
}
func (t *cffi_array) Kind() Kind {
// FIXME: ffi has no concept of array (as they decay to pointers in C)
//return Kind(C._go_ffi_type_get_type(t.c))
return Array
}
func (t *cffi_array) Len() int {
return t.len
}
func (t *cffi_array) Elem() Type {
return t.elem
}
// NewArrayType creates a new ffi_type with the given size and element type.
func NewArrayType(sz int, elmt Type) (Type, error) {
n := fmt.Sprintf("%s[%d]", elmt.Name(), sz)
if t := TypeByName(n); t != nil {
return t, nil
}
c := C.ffi_type{}
t := &cffi_array{
cffi_type: cffi_type{n: n, c: &c},
len: sz,
elem: elmt,
}
t.cffi_type.c.size = C.size_t(sz * int(elmt.Size()))
t.cffi_type.c.alignment = C_pointer.c.alignment
var c_fields **C.ffi_type = nil
C._go_ffi_type_set_elements(t.cptr(), unsafe.Pointer(c_fields))
C._go_ffi_type_set_type(t.cptr(), C.FFI_TYPE_POINTER)
// initialize type (computes alignment and size)
_, err := NewCif(DefaultAbi, t, nil)
if err != nil {
return nil, err
}
register_type(t)
return t, nil
}
type cffi_ptr struct {
cffi_type
elem Type
}
func (t *cffi_ptr) Elem() Type {
return t.elem
}
// NewPointerType creates a new ffi_type with the given element type
func NewPointerType(elmt Type) (Type, error) {
n := elmt.Name() + "*"
if t := TypeByName(n); t != nil {
return t, nil
}
c := C.ffi_type{}
t := &cffi_ptr{
cffi_type: cffi_type{n: n, c: &c},
elem: elmt,
}
if elmt.GoType() != nil {
t.cffi_type.rt = reflect.PtrTo(elmt.GoType())
}
t.cffi_type.c.size = C_pointer.c.size
t.cffi_type.c.alignment = C_pointer.c.alignment
var c_fields **C.ffi_type = nil
C._go_ffi_type_set_elements(t.cptr(), unsafe.Pointer(c_fields))
C._go_ffi_type_set_type(t.cptr(), C.FFI_TYPE_POINTER)
// initialize type (computes alignment and size)
_, err := NewCif(DefaultAbi, t, nil)
if err != nil {
return nil, err
}
register_type(t)
return t, nil
}
type cffi_slice struct {
cffi_type
elem Type
}
func (t *cffi_slice) Kind() Kind {
// FIXME: ffi has no concept of array (as they decay to pointers in C)
//return Kind(C._go_ffi_type_get_type(t.c))
return Slice
}
func (t *cffi_slice) Elem() Type {
return t.elem
}
// NewSliceType creates a new ffi_type slice with the given element type
func NewSliceType(elmt Type) (Type, error) {
n := elmt.Name() + "[]"
if t := TypeByName(n); t != nil {
return t, nil
}
c := C.ffi_type{}
t := &cffi_slice{
cffi_type: cffi_type{n: n, c: &c},
elem: elmt,
}
t.cffi_type.c.size = 0
t.cffi_type.c.alignment = 0
C._go_ffi_type_set_type(t.cptr(), C.FFI_TYPE_STRUCT)
var c_fields **C.ffi_type = nil
var cargs = make([]*C.ffi_type, 3+1)
cargs[0] = C_pointer.cptr() // ptr to C-array
csize := unsafe.Sizeof(reflect.SliceHeader{}.Cap)
if csize == 8 {
// Go 1.1 spec allows (but doesn't force) sizeof(int) == 8
cargs[1] = C_int64.cptr() // len
cargs[2] = C_int64.cptr() // cap
} else {
cargs[1] = C_int.cptr() // len
cargs[2] = C_int.cptr() // cap
}
cargs[3] = nil
c_fields = &cargs[0]
C._go_ffi_type_set_elements(t.cptr(), unsafe.Pointer(c_fields))
// initialize type (computes alignment and size)
_, err := NewCif(DefaultAbi, t, nil)
if err != nil {
return nil, err
}
register_type(t)
return t, nil
}
// the global map of types
var g_types map[string]Type
// TypeByName returns a ffi.Type by name.
// Returns nil if no such type exists
func TypeByName(n string) Type {
t, ok := g_types[n]
if ok {
return t
}
return nil
}
func register_type(t Type) {
g_types[t.Name()] = t
}
func ctype_from_gotype(rt reflect.Type) Type {
var t Type
switch rt.Kind() {
case reflect.Int:
t = C_int
case reflect.Int8:
t = C_int8
case reflect.Int16:
t = C_int16
case reflect.Int32:
t = C_int32
case reflect.Int64:
t = C_int64
case reflect.Uint:
t = C_uint
case reflect.Uint8:
t = C_uint8
case reflect.Uint16:
t = C_uint16
case reflect.Uint32:
t = C_uint32
case reflect.Uint64:
t = C_uint64
case reflect.Float32:
t = C_float
case reflect.Float64:
t = C_double
case reflect.Array:
et := ctype_from_gotype(rt.Elem())
ct, err := NewArrayType(rt.Len(), et)
if err != nil {
panic("ffi: " + err.Error())
}
ct.set_gotype(rt)
t = ct
case reflect.Ptr:
et := ctype_from_gotype(rt.Elem())
ct, err := NewPointerType(et)
if err != nil {
panic("ffi: " + err.Error())
}
t = ct
case reflect.Slice:
et := ctype_from_gotype(rt.Elem())
ct, err := NewSliceType(et)
if err != nil {
panic("ffi: " + err.Error())
}
ct.set_gotype(rt)
t = ct
case reflect.Struct:
fields := make([]Field, rt.NumField())
for i := 0; i < rt.NumField(); i++ {
field := rt.Field(i)
fields[i] = Field{
Name: field.Name,
Type: ctype_from_gotype(field.Type),
}
}
ct, err := NewStructType(rt.Name(), fields)
if err != nil {
panic("ffi: " + err.Error())
}
ct.set_gotype(rt)
t = ct
case reflect.String:
panic("unimplemented")
default:
panic("unhandled kind [" + rt.Kind().String() + "]")
}
return t
}
// Associate creates a link b/w a ffi.Type and a reflect.Type to allow
// automatic conversions b/w these types.
func Associate(ct Type, rt reflect.Type) error {
crt := ct.GoType()
if crt != nil {
if crt != rt {
return fmt.Errorf("ffi.Associate: ffi.Type [%s] already associated to reflect.Type [%s]", ct.Name(), crt.Name())
}
return nil
}
ct.set_gotype(rt)
if ct.GoType() != rt {
panic("ffi.Associate: internal error")
}
return nil
}
// PtrTo returns the pointer type with element t.
// For example, if t represents type Foo, PtrTo(t) represents *Foo.
func PtrTo(t Type) Type {
typ, err := NewPointerType(t)
if err != nil {
return nil
}
return typ
}
// TypeOf returns the ffi Type of the value in the interface{}.
// TypeOf(nil) returns nil
// TypeOf(reflect.Type) returns the ffi Type corresponding to the reflected value
func TypeOf(i interface{}) Type {
switch typ := i.(type) {
case reflect.Type:
return ctype_from_gotype(typ)
case reflect.Value:
return ctype_from_gotype(typ.Type())
default:
rt := reflect.TypeOf(i)
return ctype_from_gotype(rt)
}
panic("unreachable")
}
// is_compatible returns whether two ffi Types are binary compatible
func is_compatible(t1, t2 Type) bool {
if t1.Kind() != t2.Kind() {
//FIXME: test if it is int/intX and uint/uintX
return false
}
switch t1.Kind() {
case Struct:
for i := 0; i < t1.NumField(); i++ {
f1 := t1.Field(i)
f2 := t2.Field(i)
if !is_compatible(f1.Type, f2.Type) {
return false
}
}
case Array:
if t1.Len() != t2.Len() {
return false
}
et1 := t1.Elem()
et2 := t2.Elem()
if !is_compatible(et1, et2) {
return false
}
return true
case Ptr:
et1 := t1.Elem()
et2 := t2.Elem()
if !is_compatible(et1, et2) {
return false
}
return true
case Slice:
et1 := t1.Elem()
et2 := t2.Elem()
if !is_compatible(et1, et2) {
return false
}
return true
case String:
panic("unimplemented: ffi.String")
}
return true
}
func init() {
// init out id counter channel
g_id_ch = make(chan int, 1)
go func() {
i := 0
for {
g_id_ch <- i
i++
}
}()
g_types = make(map[string]Type)
// initialize all builtin types
init_type := func(t Type) {
n := t.Name()
//fmt.Printf("ctype [%s] - size: %v...\n", n, t.Size())
if _, ok := g_types[n]; ok {
//fmt.Printf("ctypes [%s] already registered\n", n)
return
}
//NewCif(DefaultAbi, t, nil)
//fmt.Printf("ctype [%s] - size: %v\n", n, t.Size())
g_types[n] = t
}
init_type(C_void)
init_type(C_uchar)
init_type(C_char)
init_type(C_ushort)
init_type(C_short)
init_type(C_uint)
init_type(C_int)
init_type(C_ulong)
init_type(C_long)
init_type(C_uint8)
init_type(C_int8)
init_type(C_uint16)
init_type(C_int16)
init_type(C_uint32)
init_type(C_int32)
init_type(C_uint64)
init_type(C_int64)
init_type(C_float)
init_type(C_double)
init_type(C_longdouble)
init_type(C_pointer)
}
// make sure ffi_types satisfy ffi.Type interface
var _ Type = (*cffi_type)(nil)
var _ Type = (*cffi_array)(nil)
var _ Type = (*cffi_ptr)(nil)
var _ Type = (*cffi_slice)(nil)
var _ Type = (*cffi_struct)(nil)
// EOF