summaryrefslogtreecommitdiff
path: root/src/ast
diff options
context:
space:
mode:
authorZuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian>2026-02-01 14:01:51 +0000
committerZuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian>2026-02-01 14:01:51 +0000
commitfbfce63744882d48ea2fc514ab1594000254db80 (patch)
treecc7949dab2149d829d3c04e3d542553ed883593f /src/ast
parenteafd8c67012ea253436b79f703dc0702046703f8 (diff)
Related to #138
Diffstat (limited to 'src/ast')
-rw-r--r--src/ast/ast.c56
-rw-r--r--src/ast/ast.h58
2 files changed, 86 insertions, 28 deletions
diff --git a/src/ast/ast.c b/src/ast/ast.c
index 439a9f5..1b35500 100644
--- a/src/ast/ast.c
+++ b/src/ast/ast.c
@@ -259,6 +259,25 @@ static char *type_to_string_impl(Type *t)
return xstrdup("int32_t");
case TYPE_UINT:
return xstrdup("unsigned int");
+
+ // Portable C Types
+ case TYPE_C_INT:
+ return xstrdup("c_int");
+ case TYPE_C_UINT:
+ return xstrdup("c_uint");
+ case TYPE_C_LONG:
+ return xstrdup("c_long");
+ case TYPE_C_ULONG:
+ return xstrdup("c_ulong");
+ case TYPE_C_SHORT:
+ return xstrdup("c_short");
+ case TYPE_C_USHORT:
+ return xstrdup("c_ushort");
+ case TYPE_C_CHAR:
+ return xstrdup("c_char");
+ case TYPE_C_UCHAR:
+ return xstrdup("c_uchar");
+
case TYPE_INT:
return xstrdup("int");
case TYPE_FLOAT:
@@ -461,8 +480,29 @@ static char *type_to_c_string_impl(Type *t)
return xstrdup("int32_t");
case TYPE_UINT:
return xstrdup("unsigned int");
- case TYPE_INT:
+
+ // Portable C Types (Map directly to C types)
+ case TYPE_C_INT:
return xstrdup("int");
+ case TYPE_C_UINT:
+ return xstrdup("unsigned int");
+ case TYPE_C_LONG:
+ return xstrdup("long");
+ case TYPE_C_ULONG:
+ return xstrdup("unsigned long");
+ case TYPE_C_SHORT:
+ return xstrdup("short");
+ case TYPE_C_USHORT:
+ return xstrdup("unsigned short");
+ case TYPE_C_CHAR:
+ return xstrdup("char");
+ case TYPE_C_UCHAR:
+ return xstrdup("unsigned char");
+
+ case TYPE_INT:
+ // 'int' in Zen C maps to 'i32' now for portability.
+ // FFI should use c_int.
+ return xstrdup("int32_t");
case TYPE_FLOAT:
return xstrdup("float");
case TYPE_BITINT:
@@ -519,8 +559,11 @@ static char *type_to_c_string_impl(Type *t)
return res;
}
- char *res = xmalloc(strlen(inner) + 7);
- sprintf(res, "Slice_%s", inner);
+ char *inner_zens = type_to_string(t->inner);
+ char *res = xmalloc(strlen(inner_zens) + 7);
+ sprintf(res, "Slice_%s", inner_zens);
+ free(inner_zens);
+ free(inner);
return res;
}
@@ -561,7 +604,12 @@ static char *type_to_c_string_impl(Type *t)
return xstrdup("z_closure_T");
case TYPE_GENERIC:
- return xstrdup(t->name);
+ // Use type_to_string to get the mangled name (e.g. Option_int) instead of raw C string
+ // composition This ensures consistency with struct definitions.
+ {
+ char *s = type_to_string(t);
+ return s;
+ }
case TYPE_ALIAS:
return type_to_c_string(t->inner);
diff --git a/src/ast/ast.h b/src/ast/ast.h
index 4498d7c..fa67043 100644
--- a/src/ast/ast.h
+++ b/src/ast/ast.h
@@ -28,30 +28,40 @@ typedef enum
*/
typedef enum
{
- TYPE_VOID, ///< `void` type.
- TYPE_BOOL, ///< `bool` type.
- TYPE_CHAR, ///< `char` type.
- TYPE_STRING, ///< `string` type.
- TYPE_U0, ///< `u0` type.
- TYPE_I8, ///< `i8` type.
- TYPE_U8, ///< `u8` type.
- TYPE_I16, ///< `i16` type.
- TYPE_U16, ///< `u16` type.
- TYPE_I32, ///< `i32` type.
- TYPE_U32, ///< `u32` type.
- TYPE_I64, ///< `i64` type.
- TYPE_U64, ///< `u64` type.
- TYPE_I128, ///< `i128` type.
- TYPE_U128, ///< `u128` type.
- TYPE_F32, ///< `f32` type.
- TYPE_F64, ///< `f64` type.
- TYPE_INT, ///< `int` (alias, usually i32).
- TYPE_FLOAT, ///< `float` (alias).
- TYPE_USIZE, ///< `usize` (pointer size unsigned).
- TYPE_ISIZE, ///< `isize` (pointer size signed).
- TYPE_BYTE, ///< `byte`.
- TYPE_RUNE, ///< `rune`.
- TYPE_UINT, ///< `uint` (alias).
+ TYPE_VOID, ///< `void` type.
+ TYPE_BOOL, ///< `bool` type.
+ TYPE_CHAR, ///< `char` type.
+ TYPE_STRING, ///< `string` type.
+ TYPE_U0, ///< `u0` type.
+ TYPE_I8, ///< `i8` type.
+ TYPE_U8, ///< `u8` type.
+ TYPE_I16, ///< `i16` type.
+ TYPE_U16, ///< `u16` type.
+ TYPE_I32, ///< `i32` type.
+ TYPE_U32, ///< `u32` type.
+ TYPE_I64, ///< `i64` type.
+ TYPE_U64, ///< `u64` type.
+ TYPE_I128, ///< `i128` type.
+ TYPE_U128, ///< `u128` type.
+ TYPE_F32, ///< `f32` type.
+ TYPE_F64, ///< `f64` type.
+ TYPE_INT, ///< `int` (alias, usually i32).
+ TYPE_FLOAT, ///< `float` (alias).
+ TYPE_USIZE, ///< `usize` (pointer size unsigned).
+ TYPE_ISIZE, ///< `isize` (pointer size signed).
+ TYPE_BYTE, ///< `byte`.
+ TYPE_RUNE, ///< `rune`.
+ TYPE_UINT, ///< `uint` (alias).
+ // Portable C Types (FFI)
+ TYPE_C_INT, ///< `c_int` (int).
+ TYPE_C_UINT, ///< `c_uint` (unsigned int).
+ TYPE_C_LONG, ///< `c_long` (long).
+ TYPE_C_ULONG, ///< `c_ulong` (unsigned long).
+ TYPE_C_SHORT, ///< `c_short` (short).
+ TYPE_C_USHORT, ///< `c_ushort` (unsigned short).
+ TYPE_C_CHAR, ///< `c_char` (char).
+ TYPE_C_UCHAR, ///< `c_uchar` (unsigned char).
+
TYPE_STRUCT, ///< Struct type.
TYPE_ENUM, ///< Enum type.
TYPE_POINTER, ///< Pointer type (*).