github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/cgo/testdata/types.go (about) 1 package main 2 3 /* 4 // Simple typedef. 5 typedef int myint; 6 7 // Structs, with or without name. 8 typedef struct { 9 int x; 10 int y; 11 } point2d_t; 12 typedef struct point3d { 13 int x; 14 int y; 15 int z; 16 } point3d_t; 17 18 // Structs with reserved field names. 19 struct type1 { 20 // All these fields should be renamed. 21 int type; 22 int _type; 23 int __type; 24 }; 25 struct type2 { 26 // This field should not be renamed. 27 int _type; 28 }; 29 30 // Unions. 31 typedef union { 32 // Union should be treated as a struct. 33 int i; 34 } union1_t; 35 typedef union { 36 // Union must contain a single field and have special getters/setters. 37 int i; 38 double d; 39 short s; 40 } union3_t; 41 typedef union union2d { 42 int i; 43 double d[2]; 44 } union2d_t; 45 typedef union { 46 unsigned char arr[10]; 47 } unionarray_t; 48 49 // Nested structs and unions. 50 typedef struct { 51 point2d_t begin; 52 point2d_t end; 53 int tag; 54 union { 55 point2d_t area; 56 point3d_t solid; 57 } coord; 58 } struct_nested_t; 59 typedef union { 60 point3d_t point; 61 unionarray_t array; 62 union3_t thing; 63 } union_nested_t; 64 65 // Enums. These define constant numbers. All these constants must be given the 66 // correct number. 67 typedef enum option { 68 optionA, 69 optionB, 70 optionC = -5, 71 optionD, 72 optionE = 10, 73 optionF, 74 optionG, 75 } option_t; 76 enum unused { 77 unused1 = 5, 78 }; 79 80 // Anonymous enum. 81 typedef enum { 82 option2A = 20, 83 } option2_t; 84 85 // Various types that are usually translated directly to Go types, but storing 86 // them in a struct reveals them. 87 typedef struct { 88 float f; 89 double d; 90 int *ptr; 91 } types_t; 92 93 // Arrays. 94 typedef int myIntArray[10]; 95 96 // Bitfields. 97 typedef struct { 98 unsigned char start; 99 unsigned char a : 5; 100 unsigned char b : 1; 101 unsigned char c : 2; 102 unsigned char :0; // new field 103 unsigned char d : 6; 104 unsigned char e : 3; 105 // Note that C++ allows bitfields bigger than the underlying type. 106 } bitfield_t; 107 */ 108 import "C" 109 110 // // Test that we can refer from this CGo fragment to the fragment above. 111 // typedef myint myint2; 112 import "C" 113 114 var ( 115 // aliases 116 _ C.float 117 _ C.double 118 119 // Simple typedefs. 120 _ C.myint 121 122 // Structs. 123 _ C.point2d_t 124 _ C.point3d_t 125 _ C.struct_point3d 126 127 // Structs with reserved field names. 128 _ C.struct_type1 129 _ C.struct_type2 130 131 // Unions. 132 _ C.union1_t 133 _ C.union3_t 134 _ C.union2d_t 135 _ C.unionarray_t 136 137 // Nested structs and unions. 138 _ C.struct_nested_t 139 _ C.union_nested_t 140 141 // Enums (anonymous and named). 142 _ C.option_t 143 _ C.enum_option 144 _ C.option2_t 145 146 // Various types. 147 _ C.types_t 148 149 // Arrays. 150 _ C.myIntArray 151 ) 152 153 // Test bitfield accesses. 154 func accessBitfields() { 155 var x C.bitfield_t 156 x.start = 3 157 x.set_bitfield_a(4) 158 x.set_bitfield_b(1) 159 x.set_bitfield_c(2) 160 x.d = 10 161 x.e = 5 162 var _ C.uchar = x.bitfield_a() 163 } 164 165 // Test union accesses. 166 func accessUnion() { 167 var union1 C.union1_t 168 union1.i = 5 169 170 var union2d C.union2d_t 171 var _ *C.int = union2d.unionfield_i() 172 var _ *[2]float64 = union2d.unionfield_d() 173 }