stage4/generate_c/generate_c_st.cc
changeset 894 39086e324665
parent 889 5f380b99e95e
child 897 8c3e91c385f5
equal deleted inserted replaced
882:18a39a545ed9 894:39086e324665
    47 class generate_c_st_c: public generate_c_base_c {
    47 class generate_c_st_c: public generate_c_base_c {
    48 
    48 
    49   public:
    49   public:
    50     typedef enum {
    50     typedef enum {
    51       expression_vg,
    51       expression_vg,
    52       assignment_vg,
       
    53       complextype_base_vg,
    52       complextype_base_vg,
    54       complextype_base_assignment_vg,
       
    55       complextype_suffix_vg,
    53       complextype_suffix_vg,
    56       fparam_output_vg
    54       fparam_output_vg
    57     } variablegeneration_t;
    55     } variablegeneration_t;
    58 
    56 
    59     typedef enum {
    57     typedef enum {
   155     else if (vartype == search_var_instance_decl_c::located_vt)
   153     else if (vartype == search_var_instance_decl_c::located_vt)
   156       s4o.print(GET_LOCATED);
   154       s4o.print(GET_LOCATED);
   157     else
   155     else
   158       s4o.print(GET_VAR);
   156       s4o.print(GET_VAR);
   159   }
   157   }
       
   158   
       
   159   variablegeneration_t old_wanted_variablegeneration = wanted_variablegeneration;
   160   s4o.print("(");
   160   s4o.print("(");
   161 
   161   print_variable_prefix();  
   162   variablegeneration_t old_wanted_variablegeneration = wanted_variablegeneration;
       
   163   wanted_variablegeneration = complextype_base_vg;
   162   wanted_variablegeneration = complextype_base_vg;
   164   symbol->accept(*this);
   163   symbol->accept(*this);
   165   s4o.print(",");
   164   s4o.print(",");
   166   wanted_variablegeneration = complextype_suffix_vg;
   165   wanted_variablegeneration = complextype_suffix_vg;
   167   symbol->accept(*this);
   166   symbol->accept(*this);
   175 void *print_setter(symbol_c* symbol,
   174 void *print_setter(symbol_c* symbol,
   176         symbol_c* type,
   175         symbol_c* type,
   177         symbol_c* value,
   176         symbol_c* value,
   178         symbol_c* fb_symbol = NULL,
   177         symbol_c* fb_symbol = NULL,
   179         symbol_c* fb_value = NULL) {
   178         symbol_c* fb_value = NULL) {
   180   
   179  
   181   bool type_is_complex = false;
       
   182   if (fb_symbol == NULL) {
   180   if (fb_symbol == NULL) {
   183     unsigned int vartype = analyse_variable_c::first_nonfb_vardecltype(symbol, scope_);
   181     unsigned int vartype = analyse_variable_c::first_nonfb_vardecltype(symbol, scope_);
   184     type_is_complex = analyse_variable_c::contains_complex_type(symbol);
   182     symbol_c *first_nonfb = analyse_variable_c::find_first_nonfb(symbol);
       
   183     if (first_nonfb == NULL) ERROR;
   185     if (vartype == search_var_instance_decl_c::external_vt) {
   184     if (vartype == search_var_instance_decl_c::external_vt) {
   186       if (!get_datatype_info_c::is_type_valid    (symbol->datatype)) ERROR;
   185       if (!get_datatype_info_c::is_type_valid    (first_nonfb->datatype)) ERROR;
   187       if ( get_datatype_info_c::is_function_block(symbol->datatype))
   186       if ( get_datatype_info_c::is_function_block(first_nonfb->datatype)) // handle situation where we are copying a complete fb -> fb1.fb2.fb3 := fb4 (and fb3 is external!)
   188         s4o.print(SET_EXTERNAL_FB);
   187         s4o.print(SET_EXTERNAL_FB);
   189       else
   188       else
   190         s4o.print(SET_EXTERNAL);
   189         s4o.print(SET_EXTERNAL);
   191     }
   190     }
   192     else if (vartype == search_var_instance_decl_c::located_vt)
   191     else if (vartype == search_var_instance_decl_c::located_vt)
   200       s4o.print(SET_EXTERNAL_FB);
   199       s4o.print(SET_EXTERNAL_FB);
   201     else
   200     else
   202       s4o.print(SET_VAR);
   201       s4o.print(SET_VAR);
   203   }
   202   }
   204   s4o.print("(");
   203   s4o.print("(");
   205 
   204   
   206   if (fb_symbol != NULL) {
   205   if (fb_symbol != NULL) {
   207     print_variable_prefix();
   206     print_variable_prefix();
       
   207     // It is my (MJS) conviction that by this time the following will always be true...
       
   208     //   wanted_variablegeneration == expression_vg;
   208     fb_symbol->accept(*this);
   209     fb_symbol->accept(*this);
   209     s4o.print(".,");
   210     s4o.print(".,");
   210   }
   211     symbol->accept(*this);
   211   else if (type_is_complex)
   212     s4o.print(",");
   212     wanted_variablegeneration = complextype_base_assignment_vg;
   213     s4o.print(",");    
   213   else
   214   }
   214     wanted_variablegeneration = assignment_vg;
   215   else {
   215   
   216     print_variable_prefix();
   216   symbol->accept(*this);
   217     s4o.print(",");    
   217   s4o.print(",");
   218     wanted_variablegeneration = complextype_base_vg;
   218   wanted_variablegeneration = expression_vg;
   219     symbol->accept(*this);
   219   print_check_function(type, value, fb_value);
       
   220   if (type_is_complex) {
       
   221     s4o.print(",");
   220     s4o.print(",");
   222     wanted_variablegeneration = complextype_suffix_vg;
   221     wanted_variablegeneration = complextype_suffix_vg;
   223     symbol->accept(*this);
   222     symbol->accept(*this);
   224   }
   223     s4o.print(",");
       
   224   }
       
   225   wanted_variablegeneration = expression_vg;
       
   226   print_check_function(type, value, fb_value);
   225   s4o.print(")");
   227   s4o.print(")");
   226   wanted_variablegeneration = expression_vg;
   228   wanted_variablegeneration = expression_vg;
   227   return NULL;
   229   return NULL;
   228 }
   230 }
   229 
   231 
   262 /*********************/
   264 /*********************/
   263 /* B 1.4 - Variables */
   265 /* B 1.4 - Variables */
   264 /*********************/
   266 /*********************/
   265 void *visit(symbolic_variable_c *symbol) {
   267 void *visit(symbolic_variable_c *symbol) {
   266   switch (wanted_variablegeneration) {
   268   switch (wanted_variablegeneration) {
   267     case complextype_base_assignment_vg:
       
   268     case assignment_vg:
       
   269       this->print_variable_prefix();
       
   270       s4o.print(",");
       
   271       symbol->var_name->accept(*this);
       
   272       break;
       
   273     case complextype_base_vg:
   269     case complextype_base_vg:
   274       generate_c_base_c::visit(symbol);
   270       symbol->var_name->accept(*this); //generate_c_base_c::visit(symbol);
   275       break;
   271       break;
   276     case complextype_suffix_vg:
   272     case complextype_suffix_vg:
   277       break;
   273       break;
   278     default:
   274     default:
   279       if (this->is_variable_prefix_null()) {
   275       if (this->is_variable_prefix_null()) {
   319         break;
   315         break;
   320     }
   316     }
   321   }
   317   }
   322   this->print_variable_prefix();
   318   this->print_variable_prefix();
   323   s4o.printlocation(symbol->value + 1);
   319   s4o.printlocation(symbol->value + 1);
   324   if ((this->is_variable_prefix_null() && wanted_variablegeneration != fparam_output_vg) ||
   320   if (( this->is_variable_prefix_null() && (wanted_variablegeneration != fparam_output_vg)) ||
   325       wanted_variablegeneration != assignment_vg)
   321       (!this->is_variable_prefix_null() && (wanted_variablegeneration == expression_vg   )) ||
       
   322       (!this->is_variable_prefix_null() && (wanted_variablegeneration == fparam_output_vg)))
   326     s4o.print(")");
   323     s4o.print(")");
   327   return NULL;
   324   return NULL;
   328 }
   325 }
   329 
   326 
   330 /*************************************/
   327 /*************************************/
   332 /*************************************/
   329 /*************************************/
   333 
   330 
   334 // SYM_REF2(structured_variable_c, record_variable, field_selector)
   331 // SYM_REF2(structured_variable_c, record_variable, field_selector)
   335 void *visit(structured_variable_c *symbol) {
   332 void *visit(structured_variable_c *symbol) {
   336   TRACE("structured_variable_c");
   333   TRACE("structured_variable_c");
   337   bool type_is_complex = analyse_variable_c::is_complex_type(symbol->record_variable);
       
   338   switch (wanted_variablegeneration) {
   334   switch (wanted_variablegeneration) {
   339     case complextype_base_vg:
   335     case complextype_base_vg:
   340     case complextype_base_assignment_vg:
       
   341       symbol->record_variable->accept(*this);
   336       symbol->record_variable->accept(*this);
   342       if (!type_is_complex) {
   337       if ( get_datatype_info_c::is_function_block(symbol->record_variable->datatype)) {
       
   338         if (NULL == symbol->record_variable->scope) ERROR;
       
   339         search_var_instance_decl_c search_var_instance_decl(symbol->record_variable->scope);
       
   340         if (search_var_instance_decl_c::external_vt == search_var_instance_decl.get_vartype(get_var_name_c::get_last_field(symbol->record_variable)))
       
   341           s4o.print("->");
       
   342         else  
       
   343           s4o.print(".");
       
   344         symbol->field_selector->accept(*this);
       
   345       }
       
   346       break;
       
   347     case complextype_suffix_vg:
       
   348       symbol->record_variable->accept(*this);
       
   349       if (!get_datatype_info_c::is_function_block(symbol->record_variable->datatype)) { // if the record variable is not a FB, then it will certainly be a structure!
   343         s4o.print(".");
   350         s4o.print(".");
   344         symbol->field_selector->accept(*this);
   351         symbol->field_selector->accept(*this);
   345       }
   352       }
   346       break;
       
   347     case complextype_suffix_vg:
       
   348       symbol->record_variable->accept(*this);
       
   349       if (type_is_complex) {
       
   350         s4o.print(".");
       
   351         symbol->field_selector->accept(*this);
       
   352       }
       
   353       break;
       
   354     case assignment_vg:
       
   355       symbol->record_variable->accept(*this);
       
   356       s4o.print(".");
       
   357       symbol->field_selector->accept(*this);
       
   358       break;
   353       break;
   359     default:
   354     default:
   360       if (this->is_variable_prefix_null()) {
   355       if (this->is_variable_prefix_null()) {
   361         symbol->record_variable->accept(*this);
   356         symbol->record_variable->accept(*this);
   362         s4o.print(".");
   357         s4o.print(".");
   372 /*  subscripted_variable '[' subscript_list ']' */
   367 /*  subscripted_variable '[' subscript_list ']' */
   373 //SYM_REF2(array_variable_c, subscripted_variable, subscript_list)
   368 //SYM_REF2(array_variable_c, subscripted_variable, subscript_list)
   374 void *visit(array_variable_c *symbol) {
   369 void *visit(array_variable_c *symbol) {
   375   switch (wanted_variablegeneration) {
   370   switch (wanted_variablegeneration) {
   376     case complextype_base_vg:
   371     case complextype_base_vg:
   377     case complextype_base_assignment_vg:
       
   378       symbol->subscripted_variable->accept(*this);
   372       symbol->subscripted_variable->accept(*this);
   379       break;
   373       break;
   380     case complextype_suffix_vg:
   374     case complextype_suffix_vg:
   381       symbol->subscripted_variable->accept(*this);
   375       symbol->subscripted_variable->accept(*this);
   382 
   376 
   480       s4o.print(GET_VAR_REF);
   474       s4o.print(GET_VAR_REF);
   481     
   475     
   482     variablegeneration_t old_wanted_variablegeneration = wanted_variablegeneration; 
   476     variablegeneration_t old_wanted_variablegeneration = wanted_variablegeneration; 
   483     s4o.print("(");
   477     s4o.print("(");
   484     wanted_variablegeneration = complextype_base_vg;
   478     wanted_variablegeneration = complextype_base_vg;
       
   479     print_variable_prefix();
   485     symbol->exp->accept(*this);
   480     symbol->exp->accept(*this);
   486     s4o.print(",");
   481     s4o.print(",");
   487     wanted_variablegeneration = complextype_suffix_vg;
   482     wanted_variablegeneration = complextype_suffix_vg;
   488     symbol->exp->accept(*this);
   483     symbol->exp->accept(*this);
   489     s4o.print(")");
   484     s4o.print(")");