aboutsummaryrefslogtreecommitdiffhomepage
path: root/cgo/testdata
diff options
context:
space:
mode:
authorAyke van Laethem <[email protected]>2019-11-03 18:42:51 +0100
committerRon Evans <[email protected]>2019-11-03 19:15:38 +0100
commitbe7529b2617042108c9ec4d9f0e10839ed7b53b4 (patch)
tree12f5d6271ec46ce54e00e5a7a61679a8860fe124 /cgo/testdata
parent26bdfa9c844f057797455feaeb533f555b528417 (diff)
downloadtinygo-be7529b2617042108c9ec4d9f0e10839ed7b53b4.tar.gz
tinygo-be7529b2617042108c9ec4d9f0e10839ed7b53b4.zip
cgo: add tests for most C types
Diffstat (limited to 'cgo/testdata')
-rw-r--r--cgo/testdata/types.go91
-rw-r--r--cgo/testdata/types.out.go75
2 files changed, 166 insertions, 0 deletions
diff --git a/cgo/testdata/types.go b/cgo/testdata/types.go
new file mode 100644
index 000000000..b129d4be4
--- /dev/null
+++ b/cgo/testdata/types.go
@@ -0,0 +1,91 @@
+package main
+
+/*
+// Simple typedef.
+typedef int myint;
+
+// Structs, with or without name.
+typedef struct {
+ int x;
+ int y;
+} point2d_t;
+typedef struct point3d {
+ int x;
+ int y;
+ int z;
+} point3d_t;
+
+// Enums. These define constant numbers. All these constants must be given the
+// correct number.
+typedef enum option {
+ optionA,
+ optionB,
+ optionC = -5,
+ optionD,
+ optionE = 10,
+ optionF,
+ optionG,
+} option_t;
+
+// Anonymous enum.
+typedef enum {
+ option2A = 20,
+} option2_t;
+
+// Various types that are usually translated directly to Go types, but storing
+// them in a struct reveals them.
+typedef struct {
+ float f;
+ double d;
+ int *ptr;
+} types_t;
+
+// Arrays.
+typedef int myIntArray[10];
+
+// Bitfields.
+typedef struct {
+ unsigned char start;
+ unsigned char a : 5;
+ unsigned char b : 1;
+ unsigned char c : 2;
+ unsigned char :0; // new field
+ unsigned char d : 6;
+ unsigned char e : 3;
+ // Note that C++ allows bitfields bigger than the underlying type.
+} bitfield_t;
+*/
+import "C"
+
+var (
+ // Simple typedefs.
+ _ C.myint
+
+ // Structs.
+ _ C.point2d_t
+ _ C.point3d_t
+ _ C.struct_point3d
+
+ // Enums (anonymous and named).
+ _ C.option_t
+ _ C.enum_option
+ _ C.option2_t
+
+ // Various types.
+ _ C.types_t
+
+ // Arrays.
+ _ C.myIntArray
+ _ C.myIntArrayPtr
+)
+
+// Test bitfield accesses.
+func foo() {
+ var x C.bitfield_t
+ x.start = 3
+ x.a = 4
+ x.b = 1
+ x.c = 2
+ x.d = 10
+ x.e = 5
+}
diff --git a/cgo/testdata/types.out.go b/cgo/testdata/types.out.go
new file mode 100644
index 000000000..6e586a9d7
--- /dev/null
+++ b/cgo/testdata/types.out.go
@@ -0,0 +1,75 @@
+package main
+
+import "unsafe"
+
+const C.option2A = 20
+const C.optionA = 0
+const C.optionB = 1
+const C.optionC = -5
+const C.optionD = -4
+const C.optionE = 10
+const C.optionF = 11
+const C.optionG = 12
+
+type C.int16_t = int16
+type C.int32_t = int32
+type C.int64_t = int64
+type C.int8_t = int8
+type C.uint16_t = uint16
+type C.uint32_t = uint32
+type C.uint64_t = uint64
+type C.uint8_t = uint8
+type C.uintptr_t = uintptr
+type C.char uint8
+type C.int int32
+type C.long int32
+type C.longlong int64
+type C.schar int8
+type C.short int16
+type C.uchar uint8
+type C.uint uint32
+type C.ulong uint32
+type C.ulonglong uint64
+type C.ushort uint16
+type C.bitfield_t = C.struct_1
+type C.myIntArray = [10]C.int
+type C.myint = C.int
+type C.option2_t = C.uint
+type C.option_t = C.enum_option
+type C.point2d_t = struct {
+ x C.int
+ y C.int
+}
+type C.point3d_t = C.struct_point3d
+type C.types_t = struct {
+ f float32
+ d float64
+ ptr *C.int
+}
+
+func (s *C.struct_1) bitfield_a() C.uchar { return s.__bitfield_1 & 0x1f }
+func (s *C.struct_1) set_bitfield_a(value C.uchar) { s.__bitfield_1 = s.__bitfield_1&^0x1f | value&0x1f<<0 }
+func (s *C.struct_1) bitfield_b() C.uchar {
+ return s.__bitfield_1 >> 5 & 0x1
+}
+func (s *C.struct_1) set_bitfield_b(value C.uchar) { s.__bitfield_1 = s.__bitfield_1&^0x20 | value&0x1<<5 }
+func (s *C.struct_1) bitfield_c() C.uchar {
+ return s.__bitfield_1 >> 6
+}
+func (s *C.struct_1) set_bitfield_c(value C.uchar,
+
+) { s.__bitfield_1 = s.__bitfield_1&0x3f | value<<6 }
+
+type C.struct_1 struct {
+ start C.uchar
+ __bitfield_1 C.uchar
+
+ d C.uchar
+ e C.uchar
+}
+type C.struct_point3d struct {
+ x C.int
+ y C.int
+ z C.int
+}
+type C.enum_option C.int