stage4/generate_c/generate_c_vardecl.cc
changeset 945 477393b00f95
parent 931 c0c245197b5a
child 958 7474d2cd1d6e
equal deleted inserted replaced
943:566414d7ba1f 945:477393b00f95
    58     }
    58     }
    59 };
    59 };
    60 
    60 
    61 
    61 
    62 // Does this class really need to derive from generate_c_typedecl_c ???
    62 // Does this class really need to derive from generate_c_typedecl_c ???
    63 class generate_c_array_initialization_c: public generate_c_typedecl_c {
    63 class generate_c_array_initialization_c: public generate_c_base_and_typeid_c {
    64 
    64 
    65   public:
    65   public:
    66     typedef enum {
    66     typedef enum {
    67       none_am,
    67       none_am,
    68       arraysize_am,
    68       arraysize_am,
    81     unsigned long long int array_size;
    81     unsigned long long int array_size;
    82     unsigned long long int defined_values_count;
    82     unsigned long long int defined_values_count;
    83     unsigned long long int current_initialization_count;
    83     unsigned long long int current_initialization_count;
    84 
    84 
    85   public:
    85   public:
    86     generate_c_array_initialization_c(stage4out_c *s4o_ptr): generate_c_typedecl_c(s4o_ptr) {}
    86     generate_c_array_initialization_c(stage4out_c *s4o_ptr): generate_c_base_and_typeid_c(s4o_ptr) {}
    87     ~generate_c_array_initialization_c(void) {}
    87     ~generate_c_array_initialization_c(void) {}
    88 
    88 
    89     void init_array_size(symbol_c *array_specification) {
    89     void init_array_size(symbol_c *array_specification) {
    90       array_size = 1;
    90       array_size = 1;
    91       defined_values_count = 0;
    91       defined_values_count = 0;
   147       symbol_c *type_decl;
   147       symbol_c *type_decl;
   148       switch (current_mode) {
   148       switch (current_mode) {
   149         case arraysize_am:
   149         case arraysize_am:
   150           /* look up the type declaration... */
   150           /* look up the type declaration... */
   151           type_decl = type_symtable.find_value(type_name);
   151           type_decl = type_symtable.find_value(type_name);
   152           if (type_decl == type_symtable.end_value())
   152           if (type_decl == type_symtable.end_value())   ERROR;  // Type declaration not found!!
   153             /* Type declaration not found!! */
       
   154             ERROR;
       
   155           type_decl->accept(*this);
   153           type_decl->accept(*this);
   156           break;
   154           break;
   157         default:
   155         default:
   158           print_token(type_name);
   156           print_token(type_name);
   159           break;
   157           break;
   493         return (void *)symbol->value;
   491         return (void *)symbol->value;
   494       return NULL;
   492       return NULL;
   495     }
   493     }
   496 };
   494 };
   497 
   495 
   498 class generate_c_structure_initialization_c: public generate_c_typedecl_c {
   496 
       
   497 
       
   498 
       
   499 
       
   500 
       
   501 class generate_c_structure_initialization_c: public generate_c_base_and_typeid_c {
   499 
   502 
   500   public:
   503   public:
   501     typedef enum {
   504     typedef enum {
   502       none_sm,
   505       none_sm,
   503       initdefault_sm,
   506       initdefault_sm,
   511     symbol_c* structure_type_decl;
   514     symbol_c* structure_type_decl;
   512     symbol_c* current_element_type;
   515     symbol_c* current_element_type;
   513     symbol_c* current_element_default_value;
   516     symbol_c* current_element_default_value;
   514 
   517 
   515   public:
   518   public:
   516     generate_c_structure_initialization_c(stage4out_c *s4o_ptr): generate_c_typedecl_c(s4o_ptr) {}
   519     generate_c_structure_initialization_c(stage4out_c *s4o_ptr): generate_c_base_and_typeid_c(s4o_ptr) {}
   517     ~generate_c_structure_initialization_c(void) {}
   520     ~generate_c_structure_initialization_c(void) {}
   518 
   521 
   519     void init_structure_default(symbol_c *structure_type_name) {
   522     void init_structure_default(symbol_c *structure_type_name) {
   520       structure_type_decl = NULL;
   523       structure_type_decl = NULL;
   521       current_element_type = NULL;
   524       current_element_type = NULL;
   553       current_mode = initializationvalue_sm;
   556       current_mode = initializationvalue_sm;
   554       structure_initialization->accept(*this);
   557       structure_initialization->accept(*this);
   555     }
   558     }
   556 
   559 
   557     void *visit(identifier_c *type_name) {
   560     void *visit(identifier_c *type_name) {
       
   561       symbol_c *type_decl;
       
   562       switch (current_mode) {
       
   563         case initdefault_sm:
       
   564           /* look up the type declaration... */
       
   565           type_decl = type_symtable.find_value(type_name);
       
   566           if (type_decl == type_symtable.end_value())
       
   567             /* Type declaration not found!! */
       
   568             ERROR;
       
   569           type_decl->accept(*this);
       
   570           break;
       
   571         default:
       
   572           print_token(type_name);
       
   573           break;
       
   574       }
       
   575       return NULL;
       
   576     }
       
   577     
       
   578     void *visit(derived_datatype_identifier_c *type_name) {
   558       symbol_c *type_decl;
   579       symbol_c *type_decl;
   559       switch (current_mode) {
   580       switch (current_mode) {
   560         case initdefault_sm:
   581         case initdefault_sm:
   561           /* look up the type declaration... */
   582           /* look up the type declaration... */
   562           type_decl = type_symtable.find_value(type_name);
   583           type_decl = type_symtable.find_value(type_name);
   687 /***********************************************************************/
   708 /***********************************************************************/
   688 /***********************************************************************/
   709 /***********************************************************************/
   689 
   710 
   690 
   711 
   691 
   712 
   692 class generate_c_vardecl_c: protected generate_c_base_c {
   713 class generate_c_vardecl_c: protected generate_c_base_and_typeid_c {
   693 
   714 
   694   /* A Helper class to the main class... */
   715   /* A Helper class to the main class... */
   695   /* print a string, except the first time it is called */
   716   /* print a string, except the first time it is called */
   696   /* used to print the separator "," before each variable
   717   /* used to print the separator "," before each variable
   697    * declaration, except the first...
   718    * declaration, except the first...
   888     symbol_c *current_var_type_symbol;
   909     symbol_c *current_var_type_symbol;
   889     symbol_c *current_var_init_symbol;
   910     symbol_c *current_var_init_symbol;
   890     void update_type_init(symbol_c *symbol /* a spec_init_c, subrange_spec_init_c, etc... */ ) {
   911     void update_type_init(symbol_c *symbol /* a spec_init_c, subrange_spec_init_c, etc... */ ) {
   891       this->current_var_type_symbol = spec_init_sperator_c::get_spec(symbol);
   912       this->current_var_type_symbol = spec_init_sperator_c::get_spec(symbol);
   892       this->current_var_init_symbol = spec_init_sperator_c::get_init(symbol);
   913       this->current_var_init_symbol = spec_init_sperator_c::get_init(symbol);
   893       if (NULL == this->current_var_type_symbol)
   914       if (NULL == this->current_var_type_symbol) ERROR;
   894         ERROR;
   915       if (NULL == this->current_var_type_symbol->datatype) {debug_c::print(this->current_var_type_symbol); ERROR;}
       
   916       if (get_datatype_info_c::is_array(this->current_var_type_symbol))
       
   917         this->current_var_type_symbol = this->current_var_type_symbol->datatype; 
       
   918       if (NULL == this->current_var_type_symbol) ERROR;
   895       if (NULL == this->current_var_init_symbol) {
   919       if (NULL == this->current_var_init_symbol) {
   896         /* We try to find the data type's default value... */
   920         /* We try to find the data type's default value... */
   897         this->current_var_init_symbol = type_initial_value_c::get(this->current_var_type_symbol);
   921         this->current_var_init_symbol = type_initial_value_c::get(this->current_var_type_symbol);
   898       /* Note that Function Block 'data types' do not have a default value, so we cannot abort if no default value is found! */
   922       /* Note that Function Block 'data types' do not have a default value, so we cannot abort if no default value is found! */
   899       /*
   923       /*
  1095 
  1119 
  1096 
  1120 
  1097 
  1121 
  1098   public:
  1122   public:
  1099     generate_c_vardecl_c(stage4out_c *s4o_ptr, varformat_t varformat, unsigned int vartype, symbol_c* res_name = NULL)
  1123     generate_c_vardecl_c(stage4out_c *s4o_ptr, varformat_t varformat, unsigned int vartype, symbol_c* res_name = NULL)
  1100     : generate_c_base_c(s4o_ptr) {
  1124     : generate_c_base_and_typeid_c(s4o_ptr) {
  1101       wanted_varformat = varformat;
  1125       wanted_varformat = varformat;
  1102       wanted_vartype   = vartype;
  1126       wanted_vartype   = vartype;
  1103       current_vartype  = none_vt;
  1127       current_vartype  = none_vt;
  1104       current_varqualifier = none_vq;
  1128       current_varqualifier = none_vq;
  1105       current_var_type_symbol = NULL;
  1129       current_var_type_symbol = NULL;
  1406    */
  1430    */
  1407 
  1431 
  1408   /* Start off by setting the current_var_type_symbol and
  1432   /* Start off by setting the current_var_type_symbol and
  1409    * current_var_init_symbol private variables...
  1433    * current_var_init_symbol private variables...
  1410    */
  1434    */
  1411   update_type_init(symbol->array_spec_init);
  1435   if (NULL == symbol->array_spec_init->datatype) ERROR;
       
  1436   update_type_init(symbol->array_spec_init->datatype); // we want to print the name of the base datatype, and nt the derived datatype, so we use '->datatype'!
  1412 
  1437 
  1413   /* now to produce the c equivalent... */
  1438   /* now to produce the c equivalent... */
  1414   if (wanted_varformat == constructorinit_vf) {
  1439   if (wanted_varformat == constructorinit_vf) {
  1415     generate_c_array_initialization_c *array_initialization = new generate_c_array_initialization_c(&s4o);
  1440     generate_c_array_initialization_c *array_initialization = new generate_c_array_initialization_c(&s4o);
  1416     array_initialization->set_variable_prefix(get_variable_prefix());
  1441     array_initialization->set_variable_prefix(get_variable_prefix());
  1548    */
  1573    */
  1549 
  1574 
  1550   /* Start off by setting the current_var_type_symbol and
  1575   /* Start off by setting the current_var_type_symbol and
  1551    * current_var_init_symbol private variables...
  1576    * current_var_init_symbol private variables...
  1552    */
  1577    */
  1553   update_type_init(symbol->array_specification);
  1578   if (symbol->array_specification->datatype == NULL) {debug_c::print(symbol->array_specification); ERROR;}
       
  1579   update_type_init(symbol->array_specification->datatype); // we want to print the name of the base datatype, and nt the derived datatype, so we use '->datatype'!
  1554 
  1580 
  1555   /* now to produce the c equivalent... */
  1581   /* now to produce the c equivalent... */
  1556   if (wanted_varformat == constructorinit_vf) {
  1582   if (wanted_varformat == constructorinit_vf) {
  1557     generate_c_array_initialization_c *array_initialization = new generate_c_array_initialization_c(&s4o);
  1583     generate_c_array_initialization_c *array_initialization = new generate_c_array_initialization_c(&s4o);
  1558     array_initialization->set_variable_prefix(get_variable_prefix());
  1584     array_initialization->set_variable_prefix(get_variable_prefix());
  1837    */
  1863    */
  1838 
  1864 
  1839   /* Start off by setting the current_var_type_symbol and
  1865   /* Start off by setting the current_var_type_symbol and
  1840    * current_var_init_symbol private variables...
  1866    * current_var_init_symbol private variables...
  1841    */
  1867    */
  1842   this->current_var_type_symbol = symbol->specification;
  1868   update_type_init(symbol->specification);
  1843   this->current_var_init_symbol = NULL;
  1869   this->current_var_init_symbol = NULL; // We do NOt want to initialize external variables.
       
  1870 
  1844   if(!get_datatype_info_c::is_type_valid(this->current_var_type_symbol)) ERROR;
  1871   if(!get_datatype_info_c::is_type_valid(this->current_var_type_symbol)) ERROR;
  1845   bool is_fb = get_datatype_info_c::is_function_block(this->current_var_type_symbol);
  1872   bool is_fb = get_datatype_info_c::is_function_block(this->current_var_type_symbol);
  1846 
  1873 
  1847   /* now to produce the c equivalent... */
  1874   /* now to produce the c equivalent... */
  1848   switch (wanted_varformat) {
  1875   switch (wanted_varformat) {