Skip to content

Commit fb8aeff

Browse files
chqrliebvdberg
authored andcommitted
c-generator: simplify enum definitions
* typedef enum types to their implementation type * this improves portability and fixes the tcc build
1 parent 01bccd3 commit fb8aeff

File tree

9 files changed

+36
-50
lines changed

9 files changed

+36
-50
lines changed

generator/c/c_generator.c2

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,16 @@ fn void Generator.emitDeclName(Generator* gen, string_buffer.Buf* out, const Dec
197197

198198
fn void Generator.emitEnum(Generator* gen, string_buffer.Buf* out, Decl* d) {
199199
EnumTypeDecl* etd = cast<EnumTypeDecl*>(d);
200-
out.add("typedef enum {\n");
200+
201+
// typedef enum types to their implementation type
202+
out.add("typedef ");
203+
gen.emitTypePre(out, etd.getImplType());
204+
out.space();
205+
gen.emitCName(out, d);
206+
out.add(";\n");
207+
out.add("enum ");
208+
gen.emitCName(out, d);
209+
out.add(" {\n");
201210
// set generated, otherwise constant self-ref goes wrong
202211
d.setGenerated();
203212
u32 num_constants = etd.getNumConstants();
@@ -218,30 +227,7 @@ fn void Generator.emitEnum(Generator* gen, string_buffer.Buf* out, Decl* d) {
218227
}
219228
out.add(",\n");
220229
}
221-
// add max to ensure size is ok
222-
QualType implType = etd.getImplType();
223-
const BuiltinType* bi = implType.getBuiltin();
224-
out.indent(1);
225-
out.print("_%s_%s_max = ", gen.mod_name, d.getName());
226-
switch (bi.getAlignment()) {
227-
case 1:
228-
out.add("255");
229-
break;
230-
case 2:
231-
out.add("65535");
232-
break;
233-
case 4:
234-
out.add("4294967295");
235-
break;
236-
case 8:
237-
out.add("18446744073709551615");
238-
break;
239-
}
240-
out.newline();
241-
242-
out.add("} __attribute__((packed)) ");
243-
gen.emitCName(out, d);
244-
out.add(";\n\n");
230+
out.add("};\n\n");
245231
}
246232

247233
const char*[] builtinType_cnames = {

test/c_generator/types/enum_func.c2t

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ fn void test3() {
5151

5252
// @expect{atleast, cgen/build.c}
5353

54-
typedef enum {
54+
typedef uint8_t test_Foo;
55+
enum test_Foo {
5556
test_Foo_A,
5657
test_Foo_B,
5758
test_Foo_C,
58-
_test_Foo_max = 255
59-
} __attribute__((packed)) test_Foo;
59+
};
6060

6161
static void test_Foo_func1(test_Foo f);
6262
static void test_Foo_func2(const test_Foo f);

test/c_generator/types/full_enum.c2t

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ fn void test1() {
2121

2222
// @expect{atleast, cgen/build.c}
2323

24-
typedef enum {
24+
typedef uint8_t file1_State;
25+
enum file1_State {
2526
file1_State_A,
2627
file1_State_B,
2728
file1_State_C,
28-
_file1_State_max = 255
29-
} __attribute__((packed)) file1_State;
29+
};
3030

3131
static void file2_test1(void);
3232

test/c_generator/types/keep_same_order.c2t

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ struct test_Point_ {
2222
int32_t x;
2323
};
2424

25-
typedef enum {
25+
typedef int8_t test_State;
26+
enum test_State {
2627
test_State_A,
2728
test_State_B,
2829
test_State_C,
29-
_test_State_max = 255
30-
} __attribute__((packed)) test_State;
30+
};
3131

test/interface/incremental_enum.c2t

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ const u32 Size = 3;
2727

2828
// @expect{atleast, aa.h}
2929

30-
typedef enum {
30+
typedef uint8_t aa_Enum;
31+
enum aa_Enum {
3132
aa_Enum_A,
3233
aa_Enum_B,
3334
aa_Enum_C,
34-
_aa_Enum_max = 255
35-
} __attribute__((packed)) aa_Enum;
35+
};
3636

3737
#define aa_Size 3
3838

test/interface/multi_module.c2t

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ typedef int32_t aa_AA;
4949
#define aa_FIRST 1
5050

5151
// @expect{atleast, bb.h}
52-
typedef enum {
52+
typedef int8_t bb_Enum;
53+
enum bb_Enum {
5354
bb_Enum_One = aa_FIRST,
5455
bb_Enum_Two,
55-
_bb_Enum_max = 255
56-
} __attribute__((packed)) bb_Enum;
56+
};
5757

5858
aa_AA bb_bb1(aa_AA* arg1);
5959

test/interface/multi_module_as.c2t

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ typedef int32_t aa_AA;
5050
#define aa_FIRST 1
5151

5252
// @expect{atleast, bb.h}
53-
typedef enum {
53+
typedef int8_t bb_Enum;
54+
enum bb_Enum {
5455
bb_Enum_One = aa_FIRST,
5556
bb_Enum_Two,
56-
_bb_Enum_max = 255
57-
} __attribute__((packed)) bb_Enum;
57+
};
5858

test/interface/multi_module_local.c2t

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ typedef int32_t aa_AA;
4949
#define aa_FIRST 1
5050

5151
// @expect{atleast, bb.h}
52-
typedef enum {
52+
typedef int8_t bb_Enum;
53+
enum bb_Enum {
5354
bb_Enum_One = aa_FIRST,
5455
bb_Enum_Two,
55-
_bb_Enum_max = 255
56-
} __attribute__((packed)) bb_Enum;
56+
};
5757

5858
aa_AA bb_bb1(aa_AA* arg1);
5959

test/interface/public_by_enum_init.c2t

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ type BB enum i8 {
2626
// @expect{atleast, bb.h}
2727
#include "aa.h"
2828

29-
typedef enum {
30-
bb_BB_BB1 = aa_AA,
31-
_bb_BB_max = 255
32-
} __attribute__((packed)) bb_BB;
29+
typedef int8_t bb_BB;
30+
enum bb_BB {
31+
bb_BB_BB1 = aa_AA,
32+
};
3333

0 commit comments

Comments
 (0)