translate-c-2 member access

This commit is contained in:
Vexu
2019-12-18 22:29:42 +02:00
parent e65b9e8f7b
commit cf7a5b7a4a
4 changed files with 154 additions and 105 deletions

View File

@@ -1494,6 +1494,35 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\}
});
cases.add_2("field access expression",
\\#define ARROW a->b
\\#define DOT a.b
\\extern struct Foo {
\\ int b;
\\}a;
\\float b = 2.0f;
\\int foo(void) {
\\ struct Foo *c;
\\ a.b;
\\ c->b;
\\}
, &[_][]const u8{
\\pub const struct_Foo = extern struct {
\\ b: c_int,
\\};
\\pub extern var a: struct_Foo;
\\pub export var b: f32 = 2;
\\pub export fn foo() c_int {
\\ var c: [*c]struct_Foo = undefined;
\\ _ = a.b;
\\ _ = c.*.b;
\\}
,
\\pub const DOT = a.b;
,
\\pub const ARROW = a.*.b;
});
/////////////// Cases for only stage1 which are TODO items for stage2 ////////////////
cases.addAllowWarnings("simple data types",
@@ -1702,22 +1731,6 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\}
});
cases.addC("field access expression",
\\struct Foo {
\\ int field;
\\};
\\int read_field(struct Foo *foo) {
\\ return foo->field;
\\}
, &[_][]const u8{
\\pub const struct_Foo = extern struct {
\\ field: c_int,
\\};
\\pub export fn read_field(foo: [*c]struct_Foo) c_int {
\\ return foo.*.field;
\\}
});
cases.addC("array access",
\\int array[100];
\\int foo(int index) {
@@ -2627,4 +2640,20 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\ };
\\}
});
cases.addC("field access expression",
\\struct Foo {
\\ int field;
\\};
\\int read_field(struct Foo *foo) {
\\ return foo->field;
\\}
, &[_][]const u8{
\\pub const struct_Foo = extern struct {
\\ field: c_int,
\\};
\\pub export fn read_field(foo: [*c]struct_Foo) c_int {
\\ return foo.*.field;
\\}
});
}