Merged
authorLaurent Bessard
Mon, 22 Oct 2012 17:44:40 +0200
changeset 636 dd57e63bdc3d
parent 634 736b36a83548 (current diff)
parent 635 a20a70021d37 (diff)
child 637 1b6f337222e0
Merged
stage4/generate_c/generate_c.cc
--- a/stage4/generate_c/generate_c.cc	Fri Oct 19 12:10:32 2012 +0200
+++ b/stage4/generate_c/generate_c.cc	Mon Oct 22 17:44:40 2012 +0200
@@ -684,6 +684,56 @@
 	HANDLE_ELEMENTARY_DATA_TYPE(safestring_type_name_c, "STRING")
 	HANDLE_ELEMENTARY_DATA_TYPE(safewstring_type_name_c, "WSTRING")
 
+    /***********************************/
+    /* B 1.3.2 - Generic Data Types    */
+    /***********************************/
+
+	/*  structure_type_name ':' structure_specification */
+	//SYM_REF2(structure_type_declaration_c, structure_type_name, structure_specification)
+	void *visit(structure_type_declaration_c *symbol) {
+      current_mode = arraydeclaration_im;
+      symbol->structure_specification->accept(*this);
+      current_mode = arrayname_im;
+      generate_c_typedecl_c::visit(symbol);
+	  current_mode = none_im;
+	  return NULL;
+	}
+
+    /* helper symbol for structure_declaration */
+    /* structure_declaration:  STRUCT structure_element_declaration_list END_STRUCT */
+    /* structure_element_declaration_list structure_element_declaration ';' */
+    //SYM_LIST(structure_element_declaration_list_c)
+    void *visit(structure_element_declaration_list_c *symbol) {
+      switch (current_mode) {
+    	case arraydeclaration_im:
+    	  iterator_visitor_c::visit(symbol);
+    	  break;
+        default:
+    	  generate_c_typedecl_c::visit(symbol);
+    	  break;
+      }
+      return NULL;
+    }
+
+    /*  structure_element_name ':' spec_init */
+    //SYM_REF2(structure_element_declaration_c, structure_element_name, spec_init)
+    void *visit(structure_element_declaration_c *symbol) {
+      switch (current_mode) {
+    	case arraydeclaration_im:
+    	  {
+			array_spec_init_c *spec_init = dynamic_cast<array_spec_init_c*>(symbol->spec_init);
+			if (spec_init != NULL) {
+			  symbol->spec_init->accept(*this);
+			}
+		  }
+    	  break;
+    	default:
+    	  generate_c_typedecl_c::visit(symbol);
+          break;
+      }
+      return NULL;
+    }
+
     /******************************************/
     /* B 1.4.3 - Declaration & Initialization */
     /******************************************/
@@ -712,7 +762,7 @@
     /*  var1_list ':' array_spec_init */
     // SYM_REF2(array_var_init_decl_c, var1_list, array_spec_init)
     void *visit(array_var_init_decl_c *symbol) {
-      current_mode = arrayname_im;
+      current_mode = arraydeclaration_im;
       symbol->array_spec_init->accept(*this);
       current_mode = none_im;
       return NULL;
@@ -722,6 +772,7 @@
     /* array_initialization may be NULL ! */
     void *visit(array_spec_init_c *symbol) {
       switch (current_mode) {
+    	case arraydeclaration_im:
     	case arrayname_im:
     	  {
     	    array_specification_c *specification = dynamic_cast<array_specification_c*>(symbol->array_specification);
@@ -739,17 +790,17 @@
     /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
     void *visit(array_specification_c *symbol) {
       switch (current_mode) {
-        case arrayname_im:
+        case arraydeclaration_im:
           {
+        	current_mode = arrayname_im;
             std::map<std::string,int>::iterator definition;
             current_array_name = "__";
             symbol->non_generic_type_name->accept(*this);
             symbol->array_subrange_list->accept(*this);
+            current_mode = arraydeclaration_im;
 
             definition = inline_array_defined.find(current_array_name);
             if (definition == inline_array_defined.end()) {
-              current_mode = arraydeclaration_im;
-
               s4o_incl.print("__DECLARE_ARRAY_TYPE(");
               s4o_incl.print(current_array_name);
               s4o_incl.print(",");
@@ -762,6 +813,15 @@
             }
           }
           break;
+        case arrayname_im:
+          {
+        	  std::map<std::string,int>::iterator definition;
+			  current_array_name = "__";
+			  symbol->non_generic_type_name->accept(*this);
+			  symbol->array_subrange_list->accept(*this);
+			  s4o_incl.print(current_array_name);
+          }
+          break;
         default:
           return generate_c_typedecl_c::visit(symbol);
           break;
@@ -822,7 +882,7 @@
     void *visit(array_var_declaration_c *symbol) {
       array_specification_c *specification = dynamic_cast<array_specification_c*>(symbol->array_specification);
 	  if (specification != NULL) {
-        current_mode = arrayname_im;
+        current_mode = arraydeclaration_im;
         symbol->array_specification->accept(*this);
         current_mode = none_im;
       }
@@ -863,7 +923,7 @@
     void *visit(located_var_decl_c *symbol) {
       array_spec_init_c* array_spec_init = dynamic_cast<array_spec_init_c*>(symbol->located_var_spec_init);
       if (array_spec_init != NULL) {
-    	current_mode = arrayname_im;
+    	current_mode = arraydeclaration_im;
     	symbol->located_var_spec_init->accept(*this);
     	current_mode = none_im;
       }
@@ -883,7 +943,7 @@
     void *visit(external_declaration_c *symbol) {
       array_specification_c* array_specification = dynamic_cast<array_specification_c*>(symbol->specification);
       if (array_specification != NULL) {
-        current_mode = arrayname_im;
+        current_mode = arraydeclaration_im;
         symbol->specification->accept(*this);
         current_mode = none_im;
       }
@@ -904,7 +964,7 @@
     void *visit(global_var_decl_c *symbol) {
       array_spec_init_c* array_spec_init = dynamic_cast<array_spec_init_c*>(symbol->type_specification);
       if (array_spec_init != NULL) {
-        current_mode = arrayname_im;
+        current_mode = arraydeclaration_im;
         symbol->type_specification->accept(*this);
         current_mode = none_im;
       }
--- a/stage4/generate_c/generate_c_vardecl.cc	Fri Oct 19 12:10:32 2012 +0200
+++ b/stage4/generate_c/generate_c_vardecl.cc	Mon Oct 22 17:44:40 2012 +0200
@@ -94,6 +94,10 @@
       array_specification->accept(*this);
     }
 
+    void set_array_default_initialisation(symbol_c *array_initialization) {
+      array_default_initialization = array_initialization;
+    }
+
     void init_array(symbol_c *var1_list, symbol_c *array_specification, symbol_c *array_initialization) {
       int i;
       
@@ -421,6 +425,7 @@
       if (next_element == element_count) {
         current_element_default_value = spec_init_sperator_c::get_init(symbol->spec_init);
         current_element_type = spec_init_sperator_c::get_spec(symbol->spec_init);
+        if (current_element_type == NULL) ERROR;
         return symbol->structure_element_name;
       }
       /* not yet the desired element... */
@@ -500,6 +505,7 @@
   private:
     symbol_c* structure_type_decl;
     symbol_c* current_element_type;
+    symbol_c* current_element_default_value;
 
   public:
     generate_c_structure_initialization_c(stage4out_c *s4o_ptr): generate_c_typedecl_c(s4o_ptr) {}
@@ -508,6 +514,7 @@
     void init_structure_default(symbol_c *structure_type_name) {
       structure_type_decl = NULL;
       current_element_type = NULL;
+      current_element_default_value = NULL;
       
       current_mode = initdefault_sm;
       structure_type_name->accept(*this);
@@ -604,19 +611,21 @@
         if (i > 1)
           s4o.print(",");
         
+        current_element_type = structure_iterator.element_type();
+        if (current_element_type == NULL) ERROR;
+
+        /* Check whether default value specified in structure declaration...*/
+        current_element_default_value = structure_iterator.default_value();
+
         /* Get the value from an initialization */
         symbol_c *element_value = structure_init_element_iterator.search(element_name);
         
         if (element_value == NULL) {
           /* No value given for parameter, so we must use the default... */
-          /* First check whether default value specified in function declaration...*/
-          element_value = structure_iterator.default_value();
-          current_element_type = structure_iterator.element_type();
+          element_value = current_element_default_value;
         }
         
         if (element_value == NULL) {
-          if (current_element_type == NULL) ERROR;
-          
           /* If not, get the default value of this variable's type */
           element_value = (symbol_c *)current_element_type->accept(*type_initial_value_c::instance());
         }
@@ -644,6 +653,8 @@
     void *visit(array_initial_elements_list_c *symbol) {
       generate_c_array_initialization_c *array_initialization = new generate_c_array_initialization_c(&s4o);
       array_initialization->init_array_size(current_element_type);
+      if (current_element_default_value != NULL)
+    	array_initialization->set_array_default_initialisation(current_element_default_value);
       array_initialization->init_array_values(symbol);
       delete array_initialization;
       return NULL;