stage4/generate_c/generate_c_typedecl.cc
changeset 949 734c255d863c
parent 945 477393b00f95
child 950 935c8b6ba3c5
equal deleted inserted replaced
948:543f8319bdf8 949:734c255d863c
   236 /* Notice that this class inherits from generate_c_base_c, and not from generate_c_base_and_typeid_c.
   236 /* Notice that this class inherits from generate_c_base_c, and not from generate_c_base_and_typeid_c.
   237  * This is intentional! 
   237  * This is intentional! 
   238  * Whenever this class needs to print out the id of a datatype, it will explicitly use a private instance
   238  * Whenever this class needs to print out the id of a datatype, it will explicitly use a private instance
   239  * (generate_c_typeid) of generate_c_base_and_typeid_c!
   239  * (generate_c_typeid) of generate_c_base_and_typeid_c!
   240  */
   240  */
   241 class generate_c_typedecl_c: public generate_c_base_c {
   241 class generate_c_typedecl_c: public generate_c_base_and_typeid_c {
   242 
   242 
   243   protected:
   243   protected:
   244     stage4out_c &s4o_incl;
   244     stage4out_c &s4o_incl;
   245 
   245 
   246   private:
   246   private:
   247     symbol_c* current_type_name;
   247     symbol_c* current_type_name;
   248     generate_c_base_and_typeid_c *generate_c_typeid;
   248     generate_c_base_and_typeid_c *generate_c_typeid;
   249     std::map<std::string, int> datatypes_already_defined;
   249     std::map<std::string, int> datatypes_already_defined;
   250     
   250     
   251   public:
   251   public:
   252     generate_c_typedecl_c(stage4out_c *s4o_ptr): generate_c_base_c(s4o_ptr), s4o_incl(*s4o_ptr) /*, generate_c_print_typename(s4o_ptr) */{
   252     generate_c_typedecl_c(stage4out_c *s4o_ptr): generate_c_base_and_typeid_c(s4o_ptr), s4o_incl(*s4o_ptr) /*, generate_c_print_typename(s4o_ptr) */{
   253       current_typedefinition = none_td;
   253       current_typedefinition = none_td;
   254       current_basetypedeclaration = none_bd;
   254       current_basetypedeclaration = none_bd;
   255       current_type_name = NULL;
   255       current_type_name = NULL;
   256       generate_c_typeid = new generate_c_base_and_typeid_c(&s4o_incl);
   256       generate_c_typeid = new generate_c_base_and_typeid_c(&s4o_incl);
   257     }
   257     }
   279     basetypedeclaration_t current_basetypedeclaration;
   279     basetypedeclaration_t current_basetypedeclaration;
   280 
   280 
   281     void *print_list_incl(list_c *list,
   281     void *print_list_incl(list_c *list,
   282          std::string pre_elem_str = "",
   282          std::string pre_elem_str = "",
   283          std::string inter_elem_str = "",
   283          std::string inter_elem_str = "",
   284          std::string post_elem_str = "",
   284          std::string post_elem_str = "") {
   285          visitor_c *visitor = NULL) {
       
   286       if (visitor == NULL) visitor = this;
       
   287 
   285 
   288       if (list->n > 0) {
   286       if (list->n > 0) {
   289         s4o_incl.print(pre_elem_str);
   287         s4o_incl.print(pre_elem_str);
   290         list->elements[0]->accept(*visitor);
   288         list->elements[0]->accept(*this);
   291       }
   289       }
   292 
   290 
   293       for(int i = 1; i < list->n; i++) {
   291       for(int i = 1; i < list->n; i++) {
   294         s4o_incl.print(inter_elem_str);
   292         s4o_incl.print(inter_elem_str);
   295         list->elements[i]->accept(*visitor);
   293         list->elements[i]->accept(*this);
   296       }
   294       }
   297 
   295 
   298       if (list->n > 0)
   296       if (list->n > 0)
   299         s4o_incl.print(post_elem_str);
   297         s4o_incl.print(post_elem_str);
   300 
   298 
   366 
   364 
   367   s4o_incl.print("__DECLARE_DERIVED_TYPE(");
   365   s4o_incl.print("__DECLARE_DERIVED_TYPE(");
   368   current_type_name->accept(*generate_c_typeid);
   366   current_type_name->accept(*generate_c_typeid);
   369   s4o_incl.print(",");
   367   s4o_incl.print(",");
   370   current_basetypedeclaration = subrangebasetype_bd;
   368   current_basetypedeclaration = subrangebasetype_bd;
   371   symbol->subrange_spec_init->accept(*this);
   369   symbol->subrange_spec_init->accept(*this); // always calls subrange_spec_init_c
   372   current_basetypedeclaration = none_bd;
   370   current_basetypedeclaration = none_bd;
   373   s4o_incl.print(")\n");
   371   s4o_incl.print(")\n");
   374   
   372   
   375   current_basetypedeclaration = subrangetest_bd;
   373   current_basetypedeclaration = subrangetest_bd;
   376   symbol->subrange_spec_init->accept(*this);
   374   symbol->subrange_spec_init->accept(*this); // always calls subrange_spec_init_c
   377   current_basetypedeclaration = none_bd;
   375   current_basetypedeclaration = none_bd;
   378   
   376   
   379   current_type_name = NULL;
   377   current_type_name = NULL;
   380   current_typedefinition = none_td;
   378   current_typedefinition = none_td;
   381 
   379 
   383 }
   381 }
   384 
   382 
   385 /* subrange_specification ASSIGN signed_integer */
   383 /* subrange_specification ASSIGN signed_integer */
   386 void *visit(subrange_spec_init_c *symbol) {
   384 void *visit(subrange_spec_init_c *symbol) {
   387   TRACE("subrange_spec_init_c");
   385   TRACE("subrange_spec_init_c");
   388   symbol->subrange_specification->accept(*this);
   386   symbol->subrange_specification->accept(*this); // always calls subrange_specification_c
   389   return NULL;
   387   return NULL;
   390 }
   388 }
   391 
   389 
   392 /*  integer_type_name '(' subrange')' */
   390 /*  integer_type_name '(' subrange')' */
   393 void *visit(subrange_specification_c *symbol) {
   391 void *visit(subrange_specification_c *symbol) {
   419             symbol->integer_type_name->accept(*this);
   417             symbol->integer_type_name->accept(*this);
   420             s4o_incl.print("(value);\n");
   418             s4o_incl.print("(value);\n");
   421           }
   419           }
   422           */
   420           */
   423 
   421 
   424           symbol->subrange->accept(*this);
   422           symbol->subrange->accept(*this); // always calls subrange_c
   425 
   423 
   426           s4o_incl.indent_left();
   424           s4o_incl.indent_left();
   427           s4o_incl.print("}\n");
   425           s4o_incl.print("}\n");
   428         }
   426         }
   429         else {
   427         else {
   452         s4o_incl.print("[");
   450         s4o_incl.print("[");
   453         s4o_incl.print(symbol->dimension);
   451         s4o_incl.print(symbol->dimension);
   454         s4o_incl.print("]");
   452         s4o_incl.print("]");
   455       }
   453       }
   456       else
   454       else
   457         symbol->lower_limit->accept(*this);
   455         symbol->lower_limit->accept(*this);  // always calls neg_integer_c or integer_c
   458       break;
   456       break;
   459     case subrange_td:
   457     case subrange_td:
   460       s4o_incl.print(s4o_incl.indent_spaces + "if (value < ");
   458       s4o_incl.print(s4o_incl.indent_spaces + "if (value < ");
   461       symbol->lower_limit->accept(*generate_c_typeid);
   459       symbol->lower_limit->accept(*generate_c_typeid);
   462       s4o_incl.print(")\n");
   460       s4o_incl.print(")\n");
   492 
   490 
   493   s4o_incl.print("__DECLARE_ENUMERATED_TYPE(");
   491   s4o_incl.print("__DECLARE_ENUMERATED_TYPE(");
   494   current_type_name->accept(*generate_c_typeid);
   492   current_type_name->accept(*generate_c_typeid);
   495   s4o_incl.print(",\n");
   493   s4o_incl.print(",\n");
   496   s4o_incl.indent_right();
   494   s4o_incl.indent_right();
   497   symbol->enumerated_spec_init->accept(*this);
   495   symbol->enumerated_spec_init->accept(*this); // always calls enumerated_spec_init_c
   498   s4o_incl.indent_left();
   496   s4o_incl.indent_left();
   499   s4o_incl.print(")\n");
   497   s4o_incl.print(")\n");
   500 
   498 
   501   current_type_name = NULL;
   499   current_type_name = NULL;
   502   current_typedefinition = none_td;
   500   current_typedefinition = none_td;
   506 
   504 
   507 /* enumerated_specification ASSIGN enumerated_value */
   505 /* enumerated_specification ASSIGN enumerated_value */
   508 void *visit(enumerated_spec_init_c *symbol) {
   506 void *visit(enumerated_spec_init_c *symbol) {
   509   TRACE("enumerated_spec_init_c");
   507   TRACE("enumerated_spec_init_c");
   510   if (current_typedefinition == enumerated_td)
   508   if (current_typedefinition == enumerated_td)
   511     symbol->enumerated_specification->accept(*this);
   509     symbol->enumerated_specification->accept(*this); // always calls enumerated_value_list_c or derived_datatype_identifier_c
   512   else
   510   else
   513     symbol->enumerated_specification->accept(*generate_c_typeid);
   511     symbol->enumerated_specification->accept(*generate_c_typeid);
   514   return NULL;
   512   return NULL;
   515 }
   513 }
   516 
   514 
   517 /* helper symbol for enumerated_specification->enumerated_spec_init */
   515 /* helper symbol for enumerated_specification->enumerated_spec_init */
   518 /* enumerated_value_list ',' enumerated_value */
   516 /* enumerated_value_list ',' enumerated_value */
   519 void *visit(enumerated_value_list_c *symbol) {
   517 void *visit(enumerated_value_list_c *symbol) {
   520   TRACE("enumerated_value_list_c");
   518   TRACE("enumerated_value_list_c");
   521   print_list_incl(symbol, s4o_incl.indent_spaces, ",\n"+s4o_incl.indent_spaces, "\n");
   519   print_list_incl(symbol, s4o_incl.indent_spaces, ",\n"+s4o_incl.indent_spaces, "\n");  // will always call enumerated_value_c
   522   return NULL;
   520   return NULL;
   523 }
   521 }
   524 
   522 
   525 /* enumerated_type_name '#' identifier */
   523 /* enumerated_type_name '#' identifier */
   526 /* Handled by generate_c_base_c class!!
   524 /* Handled by generate_c_base_c class!!
   551   current_type_name = id;
   549   current_type_name = id;
   552 
   550 
   553   s4o_incl.print("__DECLARE_ARRAY_TYPE(");
   551   s4o_incl.print("__DECLARE_ARRAY_TYPE(");
   554   current_type_name->accept(*generate_c_typeid);
   552   current_type_name->accept(*generate_c_typeid);
   555   s4o_incl.print(",");
   553   s4o_incl.print(",");
   556   symbol->array_spec_init->accept(*this);
   554   symbol->array_spec_init->accept(*this); // always calls array_spec_init_c
   557   s4o_incl.print(")\n");
   555   s4o_incl.print(")\n");
   558 
   556 
   559   current_type_name = NULL;
   557   current_type_name = NULL;
   560   current_typedefinition = none_td;
   558   current_typedefinition = none_td;
   561 
   559 
   571 
   569 
   572 /* array_specification [ASSIGN array_initialization] */
   570 /* array_specification [ASSIGN array_initialization] */
   573 /* array_initialization may be NULL ! */
   571 /* array_initialization may be NULL ! */
   574 void *visit(array_spec_init_c *symbol) {
   572 void *visit(array_spec_init_c *symbol) {
   575   TRACE("array_spec_init_c");  
   573   TRACE("array_spec_init_c");  
   576   symbol->array_specification->accept(*this);
   574   symbol->array_specification->accept(*this); // always calls array_specification_c or derived_datatype_identifier_c
   577   return NULL;
   575   return NULL;
   578 }
   576 }
   579 
   577 
   580 /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
   578 /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
   581 void *visit(array_specification_c *symbol) {
   579 void *visit(array_specification_c *symbol) {
   582   TRACE("array_specification_c");
   580   TRACE("array_specification_c");
   583   // The 2nd and 3rd argument of a call to the __DECLARE_ARRAY_TYPE macro!
   581   // The 2nd and 3rd argument of a call to the __DECLARE_ARRAY_TYPE macro!
   584   symbol->non_generic_type_name->accept(/*generate_c_print_typename*/*generate_c_typeid);
   582   symbol->non_generic_type_name->accept(/*generate_c_print_typename*/*generate_c_typeid);
   585   s4o_incl.print(",");
   583   s4o_incl.print(",");
   586   current_basetypedeclaration = arraysubrange_bd;
   584   current_basetypedeclaration = arraysubrange_bd;
   587   symbol->array_subrange_list->accept(*this);
   585   symbol->array_subrange_list->accept(*this); // always calls array_subrange_list_c, which the iterator_visitor_c base class will call subrange_c
   588   current_basetypedeclaration = none_bd;
   586   current_basetypedeclaration = none_bd;
   589   return NULL;
   587   return NULL;
   590 }
   588 }
   591 
   589 
   592 
   590 
   593 /*  TYPE type_declaration_list END_TYPE */
   591 /*  TYPE type_declaration_list END_TYPE */
   594 void *visit(data_type_declaration_c *symbol) {
   592 void *visit(data_type_declaration_c *symbol) {
   595   TRACE("data_type_declaration_c");
   593   TRACE("data_type_declaration_c");
   596   symbol->type_declaration_list->accept(*this);
   594   symbol->type_declaration_list->accept(*this); // will always call type_declaration_list_c
   597   s4o_incl.print("\n\n");
   595   s4o_incl.print("\n\n");
   598   return NULL;
   596   return NULL;
   599 }
   597 }
   600 
   598 
   601 /* helper symbol for data_type_declaration */
   599 /* helper symbol for data_type_declaration */
   602 void *visit(type_declaration_list_c *symbol) {
   600 void *visit(type_declaration_list_c *symbol) {
   603   TRACE("type_declaration_list_c");
   601   TRACE("type_declaration_list_c");
   604   return print_list_incl(symbol, "", "\n", "\n");
   602   return print_list_incl(symbol, "", "\n", "\n"); // will always call string_type_declaration_c, structure_type_declaration_c, array_type_declaration_c, simple_type_declaration_c, subrange_type_declaration_c, enumerated_type_declaration_c, ref_type_decl_c
   605 }
   603 }
   606 
   604 
   607 /*  simple_type_name ':' simple_spec_init */
   605 /*  simple_type_name ':' simple_spec_init */
   608 void *visit(simple_type_declaration_c *symbol) {
   606 void *visit(simple_type_declaration_c *symbol) {
   609   TRACE("simple_type_declaration_c");
   607   TRACE("simple_type_declaration_c");
   610 
   608 
   611   s4o_incl.print("__DECLARE_DERIVED_TYPE(");
   609   s4o_incl.print("__DECLARE_DERIVED_TYPE(");
   612   symbol->simple_type_name->accept(*generate_c_typeid);
   610   symbol->simple_type_name->accept(*generate_c_typeid);
   613   s4o_incl.print(",");
   611   s4o_incl.print(",");
   614   symbol->simple_spec_init->accept(*this);
   612   symbol->simple_spec_init->accept(*this); // always calls simple_spec_init_c
   615   s4o_incl.print(")\n");
   613   s4o_incl.print(")\n");
   616 
   614 
   617   if (get_datatype_info_c::is_subrange(symbol->simple_type_name)) {
   615   if (get_datatype_info_c::is_subrange(symbol->simple_type_name)) {
   618     s4o_incl.print("#define __CHECK_");
   616     s4o_incl.print("#define __CHECK_");
   619     current_type_name->accept(*generate_c_typeid);
   617     current_type_name->accept(*generate_c_typeid);
   620     s4o_incl.print(" __CHECK_");
   618     s4o_incl.print(" __CHECK_");
   621     symbol->simple_spec_init->accept(*this);
   619     symbol->simple_spec_init->accept(*this); // always calls simple_spec_init_c
   622     s4o_incl.print("\n");
   620     s4o_incl.print("\n");
   623   }
   621   }
   624 
   622 
   625   return NULL;
   623   return NULL;
   626 }
   624 }
   692   current_typedefinition = struct_td;
   690   current_typedefinition = struct_td;
   693 
   691 
   694   s4o_incl.print("__DECLARE_STRUCT_TYPE(");
   692   s4o_incl.print("__DECLARE_STRUCT_TYPE(");
   695   symbol->structure_type_name->accept(*generate_c_typeid);
   693   symbol->structure_type_name->accept(*generate_c_typeid);
   696   s4o_incl.print(",");
   694   s4o_incl.print(",");
   697   symbol->structure_specification->accept(*this);
   695   symbol->structure_specification->accept(*this); // always calls initialized_structure_c or structure_element_declaration_list
   698   s4o_incl.print(")\n");
   696   s4o_incl.print(")\n");
   699 
   697 
   700   current_typedefinition = none_td;
   698   current_typedefinition = none_td;
   701 
   699 
   702   return NULL;
   700   return NULL;
   719   TRACE("structure_element_declaration_list_c");
   717   TRACE("structure_element_declaration_list_c");
   720   s4o_incl.print("\n");
   718   s4o_incl.print("\n");
   721   s4o_incl.indent_right();
   719   s4o_incl.indent_right();
   722   s4o_incl.print(s4o_incl.indent_spaces);
   720   s4o_incl.print(s4o_incl.indent_spaces);
   723 
   721 
   724   print_list_incl(symbol, "", s4o_incl.indent_spaces, "");
   722   print_list_incl(symbol, "", s4o_incl.indent_spaces, ""); // will always call structure_element_declaration_c
   725 
   723 
   726   s4o_incl.indent_left();
   724   s4o_incl.indent_left();
   727   s4o_incl.print(s4o_incl.indent_spaces);
   725   s4o_incl.print(s4o_incl.indent_spaces);
   728   return NULL;
   726   return NULL;
   729 }
   727 }
   871   current_typedefinition = none_td;
   869   current_typedefinition = none_td;
   872 
   870 
   873   s4o_incl.print("__DECLARE_REFTO_TYPE(");
   871   s4o_incl.print("__DECLARE_REFTO_TYPE(");
   874   symbol->ref_type_name->accept(*generate_c_typeid);
   872   symbol->ref_type_name->accept(*generate_c_typeid);
   875   s4o_incl.print(", ");
   873   s4o_incl.print(", ");
   876   symbol->ref_spec_init->accept(*this);
   874   symbol->ref_spec_init->accept(*this); // always calls ref_spec_init_c
   877   s4o_incl.print(")\n");
   875   s4o_incl.print(")\n");
   878 
   876 
   879   current_type_name = NULL;
   877   current_type_name = NULL;
   880   current_typedefinition = none_td;
   878   current_typedefinition = none_td;
   881 
   879 
  1064     /* For the moment, we do not support initialising reference data types */
  1062     /* For the moment, we do not support initialising reference data types */
  1065     /* ref_spec_init: ref_spec [ ASSIGN ref_initialization ] */ 
  1063     /* ref_spec_init: ref_spec [ ASSIGN ref_initialization ] */ 
  1066     /* NOTE: ref_initialization may be NULL!! */
  1064     /* NOTE: ref_initialization may be NULL!! */
  1067     // SYM_REF2(ref_spec_init_c, ref_spec, ref_initialization)
  1065     // SYM_REF2(ref_spec_init_c, ref_spec, ref_initialization)
  1068     void *visit(ref_spec_init_c *symbol) {
  1066     void *visit(ref_spec_init_c *symbol) {
  1069       symbol->ref_spec->accept(*this);
  1067       symbol->ref_spec->accept(*this); //--> always calls ref_spec_c or derived_datatype_identifier_c
  1070       int implicit_id_count = symbol->ref_spec->anotations_map.count("generate_c_annotaton__implicit_type_id");
  1068       int implicit_id_count = symbol->ref_spec->anotations_map.count("generate_c_annotaton__implicit_type_id");
  1071       if (implicit_id_count  > 1) ERROR;
  1069       if (implicit_id_count  > 1) ERROR;
  1072       if (implicit_id_count == 1)
  1070       if (implicit_id_count == 1)
  1073         symbol->anotations_map["generate_c_annotaton__implicit_type_id"] = symbol->ref_spec->anotations_map["generate_c_annotaton__implicit_type_id"];
  1071         symbol->anotations_map["generate_c_annotaton__implicit_type_id"] = symbol->ref_spec->anotations_map["generate_c_annotaton__implicit_type_id"];
  1074       return NULL;
  1072       return NULL;
  1085     void *visit(eno_param_declaration_c      *symbol) {return NULL;}
  1083     void *visit(eno_param_declaration_c      *symbol) {return NULL;}
  1086 
  1084 
  1087     /* array_specification [ASSIGN array_initialization] */
  1085     /* array_specification [ASSIGN array_initialization] */
  1088     /* array_initialization may be NULL ! */
  1086     /* array_initialization may be NULL ! */
  1089     void *visit(array_spec_init_c *symbol) {
  1087     void *visit(array_spec_init_c *symbol) {
  1090       symbol->array_specification->accept(*this);
  1088       symbol->array_specification->accept(*this); //--> always calls array_specification_c or derived_datatype_identifier_c
  1091       int implicit_id_count = symbol->array_specification->anotations_map.count("generate_c_annotaton__implicit_type_id");
  1089       int implicit_id_count = symbol->array_specification->anotations_map.count("generate_c_annotaton__implicit_type_id");
  1092       if (implicit_id_count  > 1) ERROR;
  1090       if (implicit_id_count  > 1) ERROR;
  1093       if (implicit_id_count == 1)
  1091       if (implicit_id_count == 1)
  1094         symbol->anotations_map["generate_c_annotaton__implicit_type_id"] = symbol->array_specification->anotations_map["generate_c_annotaton__implicit_type_id"];
  1092         symbol->anotations_map["generate_c_annotaton__implicit_type_id"] = symbol->array_specification->anotations_map["generate_c_annotaton__implicit_type_id"];
  1095       return NULL;
  1093       return NULL;
  1128     /***********************/
  1126     /***********************/
  1129     /* B 1.5.1 - Functions */
  1127     /* B 1.5.1 - Functions */
  1130     /***********************/      
  1128     /***********************/      
  1131     void *visit(function_declaration_c *symbol) {
  1129     void *visit(function_declaration_c *symbol) {
  1132       prefix = symbol->derived_function_name;
  1130       prefix = symbol->derived_function_name;
  1133       symbol->var_declarations_list->accept(*this);
  1131       symbol->var_declarations_list->accept(*this); //--> always calls var_declarations_list_c
  1134       prefix = NULL;
  1132       prefix = NULL;
  1135       return NULL;
  1133       return NULL;
  1136     }
  1134     }
  1137     /*****************************/
  1135     /*****************************/
  1138     /* B 1.5.2 - Function Blocks */
  1136     /* B 1.5.2 - Function Blocks */
  1139     /*****************************/
  1137     /*****************************/
  1140     void *visit(function_block_declaration_c *symbol) {
  1138     void *visit(function_block_declaration_c *symbol) {
  1141       prefix = symbol->fblock_name;
  1139       prefix = symbol->fblock_name;
  1142       symbol->var_declarations->accept(*this);
  1140       symbol->var_declarations->accept(*this); //--> always calls var_declarations_list_c
  1143       prefix = NULL;
  1141       prefix = NULL;
  1144       return NULL;
  1142       return NULL;
  1145     }
  1143     }
  1146     /**********************/
  1144     /**********************/
  1147     /* B 1.5.3 - Programs */
  1145     /* B 1.5.3 - Programs */
  1148     /**********************/    
  1146     /**********************/    
  1149     void *visit(program_declaration_c *symbol) {
  1147     void *visit(program_declaration_c *symbol) {
  1150       prefix = symbol->program_type_name;
  1148       prefix = symbol->program_type_name;
  1151       symbol->var_declarations->accept(*this);
  1149       symbol->var_declarations->accept(*this); //--> always calls var_declarations_list_c
  1152       prefix = NULL;
  1150       prefix = NULL;
  1153       return NULL;
  1151       return NULL;
  1154     }
  1152     }
  1155 };
  1153 };
  1156 
  1154