]> ocean-lang.org Git - ocean/commitdiff
oceani: add type entry for new structs early.
authorNeilBrown <neil@brown.name>
Fri, 17 Dec 2021 05:01:11 +0000 (16:01 +1100)
committerNeilBrown <neil@brown.name>
Fri, 17 Dec 2021 05:01:11 +0000 (16:01 +1100)
When a 'struct foo' declaration is found, add the type when 'foo' is
seen rather than when the whole declaration is seen.
This keeps the order of types stable

Signed-off-by: NeilBrown <neil@brown.name>
csrc/oceani-tests.mdc
csrc/oceani.mdc

index 8256950487daa5c37a3fbe1961a09e23ac686de8..3be36db0734c49836117f3957c40e317d3184cac 100644 (file)
@@ -1011,8 +1011,8 @@ various places that `type_err()` are called.
 ###### output: type_err5
        .tmp.code:8:7: error: type already declared: foo
        .tmp.code:2:7: info: this is location of declartion: foo
-       .tmp.code:2:7: error: type has recursive definition: foo
        .tmp.code:5:7: error: type has recursive definition: baz
+       .tmp.code:2:7: error: type has recursive definition: foo
 
 ###### test: type_err6
 
index 0605d97b4a82918ddf952f1cbb761748d8855a32..e032a7d5a268e76c5f6e459e666859640fbcab22 100644 (file)
@@ -2917,24 +2917,31 @@ function will be needed.
        }
 
 ###### top level grammar
-       DeclareStruct -> struct IDENTIFIER FieldBlock Newlines ${ {
-               struct type *t;
-               t = find_type(c, $ID.txt);
-               if (!t)
-                       t = add_type(c, $ID.txt, &structure_prototype);
-               else if (t->size >= 0) {
+       $*type
+       StructName -> IDENTIFIER ${ {
+               struct type *t = find_type(c, $ID.txt);
+
+               if (t && t->size >= 0) {
                        tok_err(c, "error: type already declared", &$ID);
                        tok_err(c, "info: this is location of declartion", &t->first_use);
-                       /* Create a new one - duplicate */
-                       t = add_type(c, $ID.txt, &structure_prototype);
-               } else {
-                       struct type tmp = *t;
-                       *t = structure_prototype;
-                       t->name = tmp.name;
-                       t->next = tmp.next;
+                       t = NULL;
                }
-               t->structure.field_list = $<FB;
+               if (!t)
+                       t = add_type(c, $ID.txt, NULL);
                t->first_use = $ID;
+               $0 = t;
+       } }$
+       $void
+       DeclareStruct -> struct StructName FieldBlock Newlines ${ {
+               struct type *t = $<SN;
+               struct type tmp = *t;
+
+               *t = structure_prototype;
+               t->name = tmp.name;
+               t->next = tmp.next;
+               t->first_use = tmp.first_use;
+
+               t->structure.field_list = $<FB;
        } }$
 
        $*fieldlist
@@ -3166,8 +3173,8 @@ anything in the heap or on the stack.  A reference can be assigned
        | @ IDENTIFIER ${ {
                struct type *t = find_type(c, $ID.txt);
                if (!t) {
-                       t = add_type(c, $ID.txt, NULL);
-                       t->first_use = $ID;
+                       t = add_type(c, $ID.txt, NULL); // UNTESTED
+                       t->first_use = $ID;     // UNTESTED
                }
                $0 = find_anon_type(c, &reference_prototype, "@%.*s",
                                    $ID.txt.len, $ID.txt.txt);