stage4/generate_c/generate_c_vardecl.cc
changeset 1037 bf39078476e4
parent 1035 0a58b2720976
child 1041 56ebe2a31b5b
equal deleted inserted replaced
1036:1000db86d0af 1037:bf39078476e4
   834      *                a = 9;
   834      *                a = 9;
   835      *                b = 99;
   835      *                b = 99;
   836      *                c = 99.9;
   836      *                c = 99.9;
   837      *
   837      *
   838      * constructorinit_vf: initialising of member variables...
   838      * constructorinit_vf: initialising of member variables...
       
   839      * TODO: FIX THIS COMMENT!!! It is wrong!!!
   839      *                e.g. for a constructor...
   840      *                e.g. for a constructor...
   840      *                class_name_c(void)
   841      *                class_name_c(void)
   841      *                : a(9), b(99), c(99.9)  { // code... }
   842      *                : a(9), b(99), c(99.9)  { // code... }
   842      *                  --------------------
   843      *                  --------------------
   843      *               This class/function will produce the
   844      *               This class/function will produce the
   954           s4o.print("retain");
   955           s4o.print("retain");
   955           break;
   956           break;
   956       }
   957       }
   957       return NULL;
   958       return NULL;
   958     }
   959     }
       
   960 
       
   961     /* helper function for declare_variables().
       
   962      * Only called from one place!
       
   963      * 
       
   964      * If we were to follow the visitor pattern, the following code should really be placed inside the
       
   965      *  method visit(structure_element_initialization_list_c *), but would be conditionally executed in 
       
   966      *  a specific state/situation (which would need to be indicated through flags -> yuck). 
       
   967      * Instead of adding the code there inside an if() statement, I (msousa) prefered to keep it separate.
       
   968      * 
       
   969      * To be honest I consider this a quick hack. 
       
   970      * The time is approaching for when this class will need a general clean up.
       
   971      */
       
   972     void print_fb_explicit_initial_values(symbol_c *fbvar_name, symbol_c *init_values_list) {
       
   973       structure_element_initialization_list_c *init_list = dynamic_cast<structure_element_initialization_list_c *>(init_values_list);
       
   974       if (NULL == init_list) ERROR;
       
   975       
       
   976       for (int i = 0; i < init_list->n; i++) {
       
   977         structure_element_initialization_c *init_list_elem = dynamic_cast<structure_element_initialization_c *>(init_list->elements[i]);
       
   978         if (NULL == init_list_elem) ERROR;
       
   979         s4o.print("\n");
       
   980         s4o.print(s4o.indent_spaces);
       
   981         s4o.print(INIT_VAR);
       
   982         s4o.print("(");
       
   983         this->print_variable_prefix();
       
   984         fbvar_name->accept(*this);
       
   985         s4o.print(".");
       
   986         init_list_elem->structure_element_name->accept(*this);
       
   987         s4o.print(",");
       
   988         init_list_elem->value->accept(*this);
       
   989         print_retain();
       
   990         s4o.print(")");        
       
   991       }
       
   992     };
   959 
   993 
   960     /* Actually produce the output where variables are declared... */
   994     /* Actually produce the output where variables are declared... */
   961     /* Note that located variables and EN/ENO are the exception, they
   995     /* Note that located variables and EN/ENO are the exception, they
   962      * being declared in the located_var_decl_c,
   996      * being declared in the located_var_decl_c,
   963      * en_param_declaration_c and eno_param_declaration_c visitors...
   997      * en_param_declaration_c and eno_param_declaration_c visitors...
  1090       }
  1124       }
  1091 
  1125 
  1092       if (wanted_varformat == constructorinit_vf) {
  1126       if (wanted_varformat == constructorinit_vf) {
  1093         for(int i = 0; i < list->n; i++) {
  1127         for(int i = 0; i < list->n; i++) {
  1094           if (is_fb) {
  1128           if (is_fb) {
       
  1129             /* If we are declaring and/or initializing a FB instance, then we
       
  1130              * simply call the FBNAME_init__() function, which will initialise the
       
  1131              * FB instance with the default values of this FB type.
       
  1132              *  For a FB instance declared as:
       
  1133              *     VAR my_fb : FB_typ; END_VAR
       
  1134              * The generated C code will look something like:
       
  1135              *   FB_TYP_init__(&data__->MY_FB,retain);
       
  1136              */
  1095             s4o.print(nv->get());
  1137             s4o.print(nv->get());
  1096             this->current_var_type_symbol->accept(*this);
  1138             this->current_var_type_symbol->accept(*this);
  1097             s4o.print(FB_INIT_SUFFIX);
  1139             s4o.print(FB_INIT_SUFFIX);
  1098             s4o.print("(&");
  1140             s4o.print("(&");
  1099             this->print_variable_prefix();
  1141             this->print_variable_prefix();
  1100             list->elements[i]->accept(*this);
  1142             list->elements[i]->accept(*this);
  1101             print_retain();
  1143             print_retain();
  1102             s4o.print(");");
  1144             s4o.print(");");
       
  1145             if (this->current_var_init_symbol != NULL) {
       
  1146               /* This FB instance declaration includes an explicit initialiser list
       
  1147                * e.g. VAR my_fb : FB_typ := (var1 := 42, var2 := 'hello'); END_VAR
       
  1148                *                         --------------------------------
       
  1149                * To handle this, we insert some extra code to set each of the initialised
       
  1150                * FB variables one by one...
       
  1151                * The generated C code will lokk something like:
       
  1152                * __INIT_VAR(data__->my_fb.var1, __INT_LITERAL(42), retain);
       
  1153                * __INIT_VAR(data__->my_fb.var1, __STRING_LITERAL("hello"), retain);
       
  1154                */  
       
  1155               print_fb_explicit_initial_values(list->elements[i], this->current_var_init_symbol);
       
  1156             }
  1103           }
  1157           }
  1104           else if (this->current_var_init_symbol != NULL) {
  1158           else if (this->current_var_init_symbol != NULL) {
  1105             s4o.print(nv->get());
  1159             s4o.print(nv->get());
  1106             s4o.print(INIT_VAR);
  1160             s4o.print(INIT_VAR);
  1107             s4o.print("(");
  1161             s4o.print("(");
  1254 }
  1308 }
  1255 
  1309 
  1256 void *visit(input_declarations_c *symbol) {
  1310 void *visit(input_declarations_c *symbol) {
  1257   TRACE("input_declarations_c");
  1311   TRACE("input_declarations_c");
  1258   if ((wanted_vartype & input_vt) != 0) {
  1312   if ((wanted_vartype & input_vt) != 0) {
  1259 /*
       
  1260     // TO DO ...
       
  1261     if (symbol->option != NULL)
       
  1262       symbol->option->accept(*this);
       
  1263 */
       
  1264     //s4o.indent_right();
  1313     //s4o.indent_right();
  1265     current_vartype = input_vt;
  1314     current_vartype = input_vt;
  1266     if (symbol->option != NULL)
  1315     if (symbol->option != NULL)
  1267       symbol->option->accept(*this);
  1316       symbol->option->accept(*this);
  1268     symbol->input_declaration_list->accept(*this);
  1317     symbol->input_declaration_list->accept(*this);
  1329         s4o.print(";\n");
  1378         s4o.print(";\n");
  1330       }
  1379       }
  1331     }
  1380     }
  1332 
  1381 
  1333     if (wanted_varformat == constructorinit_vf) {
  1382     if (wanted_varformat == constructorinit_vf) {
  1334       /* NOTE: I (Mario) think this is dead code - never gets executed. Must confirm it before deleting it... */
       
  1335       s4o.print(nv->get());
  1383       s4o.print(nv->get());
  1336       s4o.print(INIT_VAR);
  1384       s4o.print(INIT_VAR);
  1337       s4o.print("(");
  1385       s4o.print("(");
  1338       this->print_variable_prefix();
  1386       this->print_variable_prefix();
  1339       // s4o.print("EN = __BOOL_LITERAL(TRUE);");
  1387       // s4o.print("EN = __BOOL_LITERAL(TRUE);");
  1399       s4o.indent_left();
  1447       s4o.indent_left();
  1400       s4o.print(s4o.indent_spaces + "}\n");
  1448       s4o.print(s4o.indent_spaces + "}\n");
  1401     }
  1449     }
  1402 
  1450 
  1403     if (wanted_varformat == constructorinit_vf) {
  1451     if (wanted_varformat == constructorinit_vf) {
  1404       /* NOTE: I (Mario) think this is dead code - never gets executed. Must confirm it before deleting it... */
       
  1405       s4o.print(nv->get());
  1452       s4o.print(nv->get());
  1406       s4o.print(INIT_VAR);
  1453       s4o.print(INIT_VAR);
  1407       s4o.print("(");
  1454       s4o.print("(");
  1408       this->print_variable_prefix();
  1455       this->print_variable_prefix();
  1409       // s4o.print("ENO = __BOOL_LITERAL(TRUE);");
  1456       // s4o.print("ENO = __BOOL_LITERAL(TRUE);");