unsafe
This commit is contained in:
parent
a222b3807e
commit
a00e8690fd
3 changed files with 15 additions and 24 deletions
|
|
@ -337,6 +337,10 @@ impl Transpiler {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_struct_decl(&self, struct_decl: &CStructDecl) -> String {
|
fn generate_struct_decl(&self, struct_decl: &CStructDecl) -> String {
|
||||||
|
// Skip predefined structs that are already declared in included headers
|
||||||
|
if matches!(struct_decl.name.as_str(), "Shader" | "Color") {
|
||||||
|
return String::new();
|
||||||
|
}
|
||||||
let mut output = format!("struct {} {{\n", struct_decl.name);
|
let mut output = format!("struct {} {{\n", struct_decl.name);
|
||||||
for field in &struct_decl.fields {
|
for field in &struct_decl.fields {
|
||||||
output.push_str(&format!(" {} {};\n", field.ty.to_string(), field.name));
|
output.push_str(&format!(" {} {};\n", field.ty.to_string(), field.name));
|
||||||
|
|
@ -346,6 +350,10 @@ impl Transpiler {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_typeinfo_decl(&self, struct_decl: &CStructDecl) -> String {
|
fn generate_typeinfo_decl(&self, struct_decl: &CStructDecl) -> String {
|
||||||
|
// Skip predefined structs
|
||||||
|
if matches!(struct_decl.name.as_str(), "Shader" | "Color") {
|
||||||
|
return String::new();
|
||||||
|
}
|
||||||
if let Some(bitmap) = self.typeinfo_map.get(&struct_decl.name) {
|
if let Some(bitmap) = self.typeinfo_map.get(&struct_decl.name) {
|
||||||
// Generate pointer bitmap as a C array
|
// Generate pointer bitmap as a C array
|
||||||
let bitmap_str = bitmap
|
let bitmap_str = bitmap
|
||||||
|
|
@ -796,7 +804,7 @@ impl Transpiler {
|
||||||
"Vec3" => "suic_vec3".to_string(),
|
"Vec3" => "suic_vec3".to_string(),
|
||||||
_ => format!("struct {}", struct_name),
|
_ => format!("struct {}", struct_name),
|
||||||
};
|
};
|
||||||
format!("({}){{ {} }}", c_type_name, field_inits.join(", "))
|
format!("suic_alloc_struct(&sui_typeinfo_{}, sizeof({}), &(({}){{ {} }}))", struct_name, c_type_name, c_type_name, field_inits.join(", "))
|
||||||
}
|
}
|
||||||
CExpr::EnumLit(enum_name, variant_name, args) => {
|
CExpr::EnumLit(enum_name, variant_name, args) => {
|
||||||
// Find the variant index - for simplicity, assume variants are in order
|
// Find the variant index - for simplicity, assume variants are in order
|
||||||
|
|
|
||||||
|
|
@ -31,15 +31,8 @@ struct Person {
|
||||||
char* name;
|
char* name;
|
||||||
int age;
|
int age;
|
||||||
};
|
};
|
||||||
struct Color {
|
;
|
||||||
uint8_t r;
|
;
|
||||||
uint8_t g;
|
|
||||||
uint8_t b;
|
|
||||||
uint8_t a;
|
|
||||||
};
|
|
||||||
struct Shader {
|
|
||||||
int id;
|
|
||||||
};
|
|
||||||
|
|
||||||
static const uint8_t sui_bitmap_Point[] = { 0, 0 };
|
static const uint8_t sui_bitmap_Point[] = { 0, 0 };
|
||||||
static const TypeInfo sui_typeinfo_Point = {
|
static const TypeInfo sui_typeinfo_Point = {
|
||||||
|
|
@ -53,25 +46,15 @@ static const TypeInfo sui_typeinfo_Person = {
|
||||||
.pointer_count = 1,
|
.pointer_count = 1,
|
||||||
.pointer_bitmap = sui_bitmap_Person
|
.pointer_bitmap = sui_bitmap_Person
|
||||||
};
|
};
|
||||||
static const uint8_t sui_bitmap_Color[] = { 0, 0, 0, 0 };
|
|
||||||
static const TypeInfo sui_typeinfo_Color = {
|
|
||||||
.field_count = 4,
|
|
||||||
.pointer_count = 0,
|
|
||||||
.pointer_bitmap = sui_bitmap_Color
|
|
||||||
};
|
|
||||||
static const uint8_t sui_bitmap_Shader[] = { 0 };
|
|
||||||
static const TypeInfo sui_typeinfo_Shader = {
|
|
||||||
.field_count = 1,
|
|
||||||
.pointer_count = 0,
|
|
||||||
.pointer_bitmap = sui_bitmap_Shader
|
|
||||||
};
|
|
||||||
|
|
||||||
int suic_main(void);
|
int suic_main(void);
|
||||||
|
|
||||||
|
|
||||||
int suic_main(void) {
|
int suic_main(void) {
|
||||||
struct Point* p = (struct Point){ .x = 5, .y = 10 };
|
struct Point* p = suic_alloc_struct(&sui_typeinfo_Point, sizeof(struct Point), &((struct Point){ .x = 5, .y = 10 }));
|
||||||
struct Person* person = (struct Person){ .name = suic_alloc_array(NULL, sizeof(char), 6, "Alice"), .age = 30 };
|
struct Person* person = suic_alloc_struct(&sui_typeinfo_Person, sizeof(struct Person), &((struct Person){ .name = suic_alloc_array(NULL, sizeof(char), 6, "Alice"), .age = 30 }));
|
||||||
int _ = ((*p).x + (*person).age);
|
int _ = ((*p).x + (*person).age);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
BIN
tests/structs.o
BIN
tests/structs.o
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue