pervognsen / bitwise

Bitwise is an educational project where we create the software/hardware stack for a computer from scratch.
Other
5.14k stars 212 forks source link

syntax error: 'cv-qualifier' (TypeInfo type_to_cdecl for const structs containing array) #70

Open uucidl opened 6 years ago

uucidl commented 6 years ago

With the following:

struct FooArray
{
    x : int[3];
}

struct FooWithConstArray
{
    array : FooArray const*;
}

func test_const_members() {
    foo := FooWithConstArray{};
    #assert(foo.array.x[0] == 0);
}

Then when TypeInfo gets written, ion generates:

    [53] = &(TypeInfo){TYPE_CONST, .size = sizeof(int (const [3])), .align = alignof(int (const [3])), .base = TYPEID(50, TYPE_ARRAY, int [3])},

the int (const [3]) is not parsed as a valid type expression

Somehow the type entry seems invalid to me, as all arrays in C are const. So the type info should probably not be generated at all (TYPE_CONST with base TYPE_ARRAY)

type_to_cdecl could be instrumented to assert that a TYPE_CONST should not be an array

uucidl commented 6 years ago

My alternative fix was to let type_to_cdecl not emit the redundant const:

diff --git a/deps/bitwise/ion/gen.c b/deps/bitwise/ion/gen.c
index 16c18fe..c729b05 100644
--- a/deps/bitwise/ion/gen.c
+++ b/deps/bitwise/ion/gen.c
@@ -216,6 +216,9 @@ char *type_to_cdecl(Type *type, const char *str) {
     case TYPE_PTR:
         return type_to_cdecl(type->base, cdecl_paren(strf("*%s", str), *str));
     case TYPE_CONST:
+        if (type->base->kind == TYPE_ARRAY) {
+            return type_to_cdecl(type->base, str);
+        }
         return type_to_cdecl(type->base, strf("const %s", cdecl_paren(str, *str)));
     case TYPE_ARRAY:
         if (type->num_elems == 0) {