Skip to content

Commit eed082d

Browse files
chqrliebvdberg
authored andcommitted
C-generator: use native C types in C backend for c_xxx types
* prevent output of type aliases that have a cname attribute that is a native C type * update libs/c2/c2.c2i so native C types get output as themselves
1 parent 9479199 commit eed082d

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

compiler/compiler.c2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ fn void Compiler.build(Compiler* c,
301301
if (opts.msan) c.addFeature("__MSAN__", "1");
302302
if (opts.ubsan) c.addFeature("__UBSAN__", "1");
303303

304+
c.addFeature("USE_NATIVE_CTYPES", "1");
305+
304306
target.addLib(c.auxPool.add("c2", 2, true), false);
305307

306308
c.parser = c2_parser.create(sm,

generator/c/c_generator.c2

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,39 @@ fn void Generator.emitFunctionType(Generator* gen, string_buffer.Buf* out, Decl*
453453
out.add(";\n\n");
454454
}
455455

456+
fn bool match_type(const char* s, const char* list, const char** sp) {
457+
while (*list) {
458+
usize len;
459+
for (len = 0; list[len] && list[len] != ','; len++)
460+
continue;
461+
usize i;
462+
for (i = 0; i < len && s[i] == list[i]; i++)
463+
continue;
464+
if (i == len && (s[i] == '\0' || s[i] == ' ')) {
465+
*sp = s + i;
466+
return true;
467+
}
468+
list += len;
469+
list += (*list == ',');
470+
}
471+
return false;
472+
}
473+
474+
fn bool is_c_type(const char* s) {
475+
for (;;) {
476+
while (*s == ' ') s++;
477+
if (!*s) return true;
478+
if (!match_type(s, "const,volatile,unsigned,signed,short,long,"
479+
"_Bool,char,int,float,double,size_t,ssize_t", &s))
480+
break;
481+
}
482+
return false;
483+
}
484+
456485
fn void Generator.emitAliasType(Generator* gen, string_buffer.Buf* out, Decl* d) {
486+
const char* cname = d.getCName();
487+
if (cname && is_c_type(cname))
488+
return;
457489
// For now, just generate canonicalType as RHS
458490
QualType qt = d.getType();
459491
qt = qt.getCanonicalType(); // just generate final type
@@ -462,7 +494,7 @@ fn void Generator.emitAliasType(Generator* gen, string_buffer.Buf* out, Decl* d)
462494
gen.emitTypePost(out, qt);
463495
out.space();
464496
gen.emitCName(out, d);
465-
out.add(";\n\n");
497+
out.add(";\n");
466498
}
467499

468500
fn bool emitAsDefine(const VarDecl* vd) {
@@ -1353,7 +1385,6 @@ const char[] Include_guard1 =
13531385
const char[] C_types =
13541386
```c
13551387
// --- internally added ---
1356-
typedef unsigned char bool;
13571388
typedef signed char int8_t;
13581389
typedef unsigned char uint8_t;
13591390
typedef signed short int16_t;

libs/c2/c2.c2i

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
module c2;
22

3+
#if USE_NATIVE_CTYPES
4+
type c_char char @(cname="char");
5+
type c_uchar u8 @(cname="unsigned char");
6+
type c_short i16 @(cname="short");
7+
type c_ushort u16 @(cname="unsigned short");
8+
type c_int i32 @(cname="int");
9+
type c_uint u32 @(cname="unsigned int");
10+
type c_longlong i64 @(cname="long long");
11+
type c_ulonglong u64 @(cname="unsigned long long");
12+
type c_float f32 @(cname="float");
13+
type c_double f64 @(cname="double");
14+
#if ARCH_32BIT
15+
type c_long i32 @(cname="long");
16+
type c_ulong u32 @(cname="unsigned long");
17+
type c_size u32 @(cname="size_t");
18+
type c_ssize i32 @(cname="ssize_t");
19+
#else
20+
type c_long i64 @(cname="long");
21+
type c_ulong u64 @(cname="unsigned long");
22+
type c_size u64 @(cname="size_t");
23+
type c_ssize i64 @(cname="ssize_t");
24+
#endif
25+
#else // compiling the compiler from bootstrap code
326
type c_char char;
427
type c_uchar u8;
528
type c_short i16;
@@ -21,6 +44,7 @@ type c_ulong u64;
2144
type c_size u64;
2245
type c_ssize i64;
2346
#endif
47+
#endif
2448

2549
const i8 min_i8 = -128;
2650
const i8 max_i8 = 127;

0 commit comments

Comments
 (0)