Fix bug when declaring data type directly derived from array type
authorlaurent
Tue, 31 May 2011 15:35:05 +0200
changeset 309 f93bcada0f51
parent 308 833cb2eba36f
child 310 f111a6986c22
Fix bug when declaring data type directly derived from array type
stage4/generate_c/generate_c_il.cc
stage4/generate_c/generate_c_st.cc
stage4/generate_c/generate_c_typedecl.cc
--- a/stage4/generate_c/generate_c_il.cc	Mon May 30 19:28:32 2011 +0100
+++ b/stage4/generate_c/generate_c_il.cc	Tue May 31 15:35:05 2011 +0200
@@ -679,9 +679,9 @@
   for (int i =  0; i < symbol->n; i++) {
     s4o.print("[__");
     current_array_type->accept(*this);
-    s4o.print("_TRANSIDX");
+    s4o.print("_TRANSIDX(");
     print_integer(i);
-    s4o.print("(");
+    s4o.print(",");
     symbol->elements[i]->accept(*this);
     s4o.print(")]");
   }
--- a/stage4/generate_c/generate_c_st.cc	Mon May 30 19:28:32 2011 +0100
+++ b/stage4/generate_c/generate_c_st.cc	Tue May 31 15:35:05 2011 +0200
@@ -339,9 +339,9 @@
   for (int i =  0; i < symbol->n; i++) {
     s4o.print("[__");
     current_array_type->accept(*this);
-    s4o.print("_TRANSIDX");
+    s4o.print("_TRANSIDX(");
     print_integer(i);
-    s4o.print("(");
+    s4o.print(",");
     symbol->elements[i]->accept(*this);
     s4o.print(")]");
   }
--- a/stage4/generate_c/generate_c_typedecl.cc	Mon May 30 19:28:32 2011 +0100
+++ b/stage4/generate_c/generate_c_typedecl.cc	Tue May 31 15:35:05 2011 +0200
@@ -30,6 +30,7 @@
 
   private:
     symbol_c* current_type_name;
+    bool array_is_derived;
     search_base_type_c search_base_type;
 
     generate_c_base_c *basedecl;
@@ -61,6 +62,7 @@
       none_bd,
       subrangebasetype_bd,
       subrangetest_bd,
+      arrayderiveddeclaration_bd,
       arraybasetype_bd,
       arraybasetypeincl_bd,
       arraysubrange_bd,
@@ -317,16 +319,26 @@
 void *visit(array_type_declaration_c *symbol) {
   TRACE("array_type_declaration_c");
   
-  s4o_incl.print("__DECLARE_ARRAY_TYPE(");
+  array_is_derived = false;
+  current_basetypedeclaration = arrayderiveddeclaration_bd;
+  symbol->array_spec_init->accept(*this);
+  current_basetypedeclaration = none_bd;
+
+  if (array_is_derived)
+	s4o_incl.print("__DECLARE_DERIVED_TYPE(");
+  else
+    s4o_incl.print("__DECLARE_ARRAY_TYPE(");
   current_basetypedeclaration = arraybasetypeincl_bd;
   symbol->array_spec_init->accept(*this);
   current_basetypedeclaration = none_bd;
   s4o_incl.print(",");
   symbol->identifier->accept(*basedecl);
-  s4o_incl.print(",");
-  current_basetypedeclaration = arraysubrange_bd;
-  symbol->array_spec_init->accept(*this);
-  current_basetypedeclaration = none_bd;
+  if (!array_is_derived) {
+    s4o_incl.print(",");
+    current_basetypedeclaration = arraysubrange_bd;
+    symbol->array_spec_init->accept(*this);
+    current_basetypedeclaration = none_bd;
+  }
   s4o_incl.print(")\n");
   
   if (search_base_type.type_is_subrange(symbol->identifier)) {
@@ -353,15 +365,40 @@
 void *visit(array_spec_init_c *symbol) {
   TRACE("array_spec_init_c");
   
-  identifier_c *array_type_name = dynamic_cast<identifier_c *>(symbol->array_specification);
-  
-  if (array_type_name == NULL) {
-    current_typedefinition = array_td;
-    symbol->array_specification->accept(*this);
-    current_typedefinition = none_td;
+  identifier_c *array_type_name;
+
+  switch (current_basetypedeclaration) {
+    case arrayderiveddeclaration_bd:
+      array_type_name = dynamic_cast<identifier_c *>(symbol->array_specification);
+      array_is_derived = array_type_name != NULL;
+      break;
+    case arraytranslateindex_bd:
+      if (!array_is_derived) {
+    	current_typedefinition = array_td;
+    	symbol->array_specification->accept(*this);
+    	current_typedefinition = none_td;
+      }
+
+      s4o.print("#define __");
+      current_type_name->accept(*this);
+      s4o.print("_TRANSIDX(row, index) __");
+      if (array_is_derived)
+         symbol->array_specification->accept(*this);
+      else
+         current_type_name->accept(*this);
+      s4o.print("_TRANSIDX##row(index)");
+      break;
+    default:
+      if (array_is_derived) {
+        symbol->array_specification->accept(*basedecl);
+      }
+      else {
+        current_typedefinition = array_td;
+        symbol->array_specification->accept(*this);
+        current_typedefinition = none_td;
+      }
+      break;
   }
-  else
-    symbol->array_specification->accept(*basedecl);
   return NULL;
 }
 
@@ -425,7 +462,7 @@
   symbol->simple_spec_init->accept(*this);
   s4o_incl.print(",");
   symbol->simple_type_name->accept(*basedecl);
-  s4o_incl.print(");\n");
+  s4o_incl.print(")\n");
   return NULL;
 }