stage4/generate_c/generate_var_list.cc
changeset 160 59d58f5e6caa
parent 158 eb8a5df69bb0
child 166 09004f402097
equal deleted inserted replaced
159:1e4eb0d48385 160:59d58f5e6caa
    63       programs_dt,
    63       programs_dt,
    64       variables_dt
    64       variables_dt
    65     } declarationtype_t;
    65     } declarationtype_t;
    66 
    66 
    67     declarationtype_t current_declarationtype;
    67     declarationtype_t current_declarationtype;
    68   
    68     
       
    69     typedef enum {
       
    70       none_vtc,
       
    71       variable_vtc,
       
    72       pointer_vtc,
       
    73       array_vtc,
       
    74       structure_vtc,
       
    75       function_block_vtc
       
    76     } vartypecategory_t;
       
    77     
       
    78     vartypecategory_t current_var_type_category;
       
    79     
    69   private:
    80   private:
    70     symbol_c *current_var_type_symbol;
    81     symbol_c *current_var_type_symbol;
       
    82     symbol_c *current_var_type_name;
    71     unsigned int current_var_number;
    83     unsigned int current_var_number;
    72     unsigned int step_number;
    84     unsigned int step_number;
    73     unsigned int transition_number;
    85     unsigned int transition_number;
    74     unsigned int action_number;
    86     unsigned int action_number;
    75     bool configuration_defined;
    87     bool configuration_defined;
    80   public:
    92   public:
    81     generate_var_list_c(stage4out_c *s4o_ptr, symbol_c *scope)
    93     generate_var_list_c(stage4out_c *s4o_ptr, symbol_c *scope)
    82     : generate_c_typedecl_c(s4o_ptr) {
    94     : generate_c_typedecl_c(s4o_ptr) {
    83       search_fb_typedecl = new search_fb_typedecl_c(scope);
    95       search_fb_typedecl = new search_fb_typedecl_c(scope);
    84       current_var_number = 0;
    96       current_var_number = 0;
    85       current_var_type_symbol = NULL;
    97       current_var_type_symbol = current_var_type_name = NULL;
    86       current_declarationtype = none_dt;
    98       current_declarationtype = none_dt;
       
    99       current_var_type_category = none_vtc;
    87     }
   100     }
    88     
   101     
    89     ~generate_var_list_c(void) {
   102     ~generate_var_list_c(void) {
    90       delete search_fb_typedecl;
   103       delete search_fb_typedecl;
    91     }
   104     }
    92     
   105     
    93     void update_var_type_symbol(symbol_c *symbol, bool is_fb = false) {
   106     void update_var_type_symbol(symbol_c *symbol) {
    94       
   107       
    95       this->current_var_type_symbol = spec_init_sperator_c::get_spec(symbol);
   108       this->current_var_type_name = spec_init_sperator_c::get_spec(symbol);
    96       if (this->current_var_type_symbol == NULL)
   109       if (this->current_var_type_name == NULL)
    97         ERROR;
   110         ERROR;
    98       
   111       
    99       if (is_fb)
   112       this->current_var_type_symbol = search_fb_typedecl->get_decl(this->current_var_type_name);
   100         this->current_var_type_symbol = search_fb_typedecl->get_decl(this->current_var_type_symbol);
   113       if (this->current_var_type_symbol != NULL)
   101       else
   114         this->current_var_type_category = function_block_vtc;
   102         this->current_var_type_symbol = (symbol_c *)(this->current_var_type_symbol->accept(search_base_type));
   115       else {
       
   116         this->current_var_type_symbol = (symbol_c *)(this->current_var_type_name->accept(search_base_type));
       
   117         
       
   118         structure_element_declaration_list_c *structure_symbol = dynamic_cast<structure_element_declaration_list_c *>(this->current_var_type_symbol);
       
   119         if (structure_symbol != NULL)
       
   120           this->current_var_type_category = structure_vtc;
       
   121         else
       
   122           this->current_var_type_category = variable_vtc;
       
   123       }
       
   124       
   103       if (this->current_var_type_symbol == NULL)
   125       if (this->current_var_type_symbol == NULL)
   104         ERROR;
   126         ERROR;
   105     }
   127     }
   106 
   128 
   107     void reset_var_type_symbol(void) {
   129     void reset_var_type_symbol(void) {
   126       symbol->accept(*this);
   148       symbol->accept(*this);
   127       current_declarationtype = none_dt;
   149       current_declarationtype = none_dt;
   128       s4o.print("\n");
   150       s4o.print("\n");
   129     }
   151     }
   130     
   152     
   131     void declare_variables(symbol_c *symbol, const char* type = "VAR") {
   153     void declare_variables(symbol_c *symbol) {
   132       list_c *list = dynamic_cast<list_c *>(symbol);
   154       list_c *list = dynamic_cast<list_c *>(symbol);
   133       /* should NEVER EVER occur!! */
   155       /* should NEVER EVER occur!! */
   134       if (list == NULL) ERROR;
   156       if (list == NULL) ERROR;
   135 
   157 
   136       for(int i = 0; i < list->n; i++) {
   158       for(int i = 0; i < list->n; i++) {
   137         declare_variable(list->elements[i], type);
   159         declare_variable(list->elements[i]);
   138       }
   160       }
   139     }
   161     }
   140     
   162     
   141     void declare_variable(symbol_c *symbol, const char* type = "VAR") {
   163     void declare_variable(symbol_c *symbol) {
   142       print_var_number();
   164       print_var_number();
   143       s4o.print(";");
   165       s4o.print(";");
   144       s4o.print(type);
   166       switch (this->current_var_type_category) {
       
   167         case pointer_vtc:
       
   168           s4o.print("PT");
       
   169           break;
       
   170         case array_vtc:
       
   171           s4o.print("ARRAY");
       
   172           break;
       
   173         case structure_vtc:
       
   174           s4o.print("STRUCT");
       
   175           break;
       
   176         case function_block_vtc:
       
   177           s4o.print("FB");
       
   178           break;
       
   179         default:
       
   180           s4o.print("VAR");
       
   181           break;
       
   182       }
   145       s4o.print(";");
   183       s4o.print(";");
   146       print_symbol_list();
   184       print_symbol_list();
   147       symbol->accept(*this);
   185       symbol->accept(*this);
   148       s4o.print(";");
   186       s4o.print(";");
   149       print_symbol_list();
   187       print_symbol_list();
   150       symbol->accept(*this);
   188       symbol->accept(*this);
   151       s4o.print(";");
   189       s4o.print(";");
   152       if (strcmp(type, "FB") == 0) {
   190       switch (this->current_var_type_category) {
   153         SYMBOL *current_name;
   191         case structure_vtc:
   154         current_name = new SYMBOL;
   192         case function_block_vtc:
   155         current_name->symbol = symbol;
   193           this->current_var_type_name->accept(*this);
   156         current_symbol_list.push_back(*current_name);
   194           s4o.print(";\n");
   157         this->current_var_type_symbol->accept(*this);
   195           SYMBOL *current_name;
   158         current_symbol_list.pop_back();
   196           current_name = new SYMBOL;
   159       }
   197           current_name->symbol = symbol;
   160       else {
   198           current_symbol_list.push_back(*current_name);
   161         this->current_var_type_symbol->accept(*this);
   199           this->current_var_type_symbol->accept(*this);
   162         s4o.print(";\n");
   200           current_symbol_list.pop_back();
       
   201           break;
       
   202         case array_vtc:
       
   203           this->current_var_type_name->accept(*this);
       
   204           s4o.print(";\n");
       
   205           break;
       
   206         default:
       
   207           this->current_var_type_symbol->accept(*this);
       
   208           s4o.print(";\n");
       
   209           break;
   163       }
   210       }
   164     }
   211     }
   165     
   212     
   166     void print_var_number(void) {
   213     void print_var_number(void) {
   167       char str[10];
   214       char str[10];
   207         /* Start off by setting the current_var_type_symbol and
   254         /* Start off by setting the current_var_type_symbol and
   208          * current_var_init_symbol private variables...
   255          * current_var_init_symbol private variables...
   209          */
   256          */
   210         update_var_type_symbol(symbol->located_var_spec_init);
   257         update_var_type_symbol(symbol->located_var_spec_init);
   211         
   258         
   212         if (symbol->variable_name != NULL)
   259         if (symbol->variable_name != NULL) {
   213           declare_variable(symbol->variable_name, "PT");
   260           this->current_var_type_category = pointer_vtc;
       
   261           declare_variable(symbol->variable_name);
       
   262         }
   214         
   263         
   215         current_var_type_symbol = NULL;
   264         current_var_type_symbol = NULL;
   216         return NULL;
   265         return NULL;
   217     }
   266     }
   218     
   267     
   222       TRACE("array_var_init_decl_c");
   271       TRACE("array_var_init_decl_c");
   223       /* Start off by setting the current_var_type_symbol and
   272       /* Start off by setting the current_var_type_symbol and
   224        * current_var_init_symbol private variables...
   273        * current_var_init_symbol private variables...
   225        */
   274        */
   226       update_var_type_symbol(symbol->array_spec_init);
   275       update_var_type_symbol(symbol->array_spec_init);
   227     
   276       
       
   277       this->current_var_type_category = array_vtc;
   228       declare_variables(symbol->var1_list);
   278       declare_variables(symbol->var1_list);
   229     
   279     
   230       /* Values no longer in scope, and therefore no longer used.
   280       /* Values no longer in scope, and therefore no longer used.
   231        * Make an effort to keep them set to NULL when not in use
   281        * Make an effort to keep them set to NULL when not in use
   232        * in order to catch bugs as soon as possible...
   282        * in order to catch bugs as soon as possible...
   270        */
   320        */
   271     
   321     
   272       /* Start off by setting the current_var_type_symbol and
   322       /* Start off by setting the current_var_type_symbol and
   273        * current_var_init_symbol private variables...
   323        * current_var_init_symbol private variables...
   274        */
   324        */
   275       update_var_type_symbol(symbol, true);
   325       update_var_type_symbol(symbol);
   276     
   326     
   277       /* now to produce the c equivalent... */
   327       /* now to produce the c equivalent... */
   278       declare_variables(symbol->fb_name_list, "FB");
   328       declare_variables(symbol->fb_name_list);
   279     
   329     
   280       /* Values no longer in scope, and therefore no longer used.
   330       /* Values no longer in scope, and therefore no longer used.
   281        * Make an effort to keep them set to NULL when not in use
   331        * Make an effort to keep them set to NULL when not in use
   282        * in order to catch bugs as soon as possible...
   332        * in order to catch bugs as soon as possible...
   283        */
   333        */
   295        */
   345        */
   296     
   346     
   297       /* Start off by setting the current_var_type_symbol and
   347       /* Start off by setting the current_var_type_symbol and
   298        * current_var_init_symbol private variables...
   348        * current_var_init_symbol private variables...
   299        */
   349        */
   300       this->current_var_type_symbol = (symbol_c *)(symbol->specification->accept(*search_fb_typedecl));
   350       update_var_type_symbol(symbol);
   301       if (this->current_var_type_symbol == NULL) {
   351       
   302         this->current_var_type_symbol = symbol->specification;
   352       /* now to produce the c equivalent... */
   303       
   353       if (this->current_var_type_category == variable_vtc)
   304         declare_variable(symbol->global_var_name, "PT");
   354         this->current_var_type_category = pointer_vtc;
   305       }
   355       declare_variable(symbol->global_var_name);
   306       else
       
   307         declare_variable(symbol->global_var_name, "FB");
       
   308       
   356       
   309       /* Values no longer in scope, and therefore no longer used.
   357       /* Values no longer in scope, and therefore no longer used.
   310        * Make an effort to keep them set to NULL when not in use
   358        * Make an effort to keep them set to NULL when not in use
   311        * in order to catch bugs as soon as possible...
   359        * in order to catch bugs as soon as possible...
   312        */
   360        */
   348     
   396     
   349     /*| global_var_name location */
   397     /*| global_var_name location */
   350     // SYM_REF2(global_var_spec_c, global_var_name, location)
   398     // SYM_REF2(global_var_spec_c, global_var_name, location)
   351     void *visit(global_var_spec_c *symbol) {
   399     void *visit(global_var_spec_c *symbol) {
   352       if (symbol->global_var_name != NULL)
   400       if (symbol->global_var_name != NULL)
   353         declare_variable(symbol->global_var_name, "PT");
   401         this->current_var_type_category = pointer_vtc;
       
   402         declare_variable(symbol->global_var_name);
   354       return NULL;
   403       return NULL;
   355     }
   404     }
   356     
   405     
   357     void *visit(var1_init_decl_c *symbol) {
   406     void *visit(var1_init_decl_c *symbol) {
   358       TRACE("var1_init_decl_c");
   407       TRACE("var1_init_decl_c");
   376 
   425 
   377 /********************************/
   426 /********************************/
   378 /* B 1.3.3 - Derived data types */
   427 /* B 1.3.3 - Derived data types */
   379 /********************************/
   428 /********************************/
   380     void *visit(data_type_declaration_c *symbol) {
   429     void *visit(data_type_declaration_c *symbol) {
       
   430       return NULL;
       
   431     }
       
   432 
       
   433     void *visit(structure_element_declaration_list_c *symbol) {
       
   434       for(int i = 0; i < symbol->n; i++) {
       
   435         symbol->elements[i]->accept(*this);
       
   436       }
       
   437       return NULL;
       
   438     }
       
   439 
       
   440     void *visit(structure_element_declaration_c *symbol) {
       
   441       /* Start off by setting the current_var_type_symbol and
       
   442        * current_var_init_symbol private variables...
       
   443        */
       
   444       update_var_type_symbol(symbol->spec_init);
       
   445       
       
   446       /* now to produce the c equivalent... */
       
   447       declare_variable(symbol->structure_element_name);
       
   448       
       
   449       /* Values no longer in scope, and therefore no longer used.
       
   450        * Make an effort to keep them set to NULL when not in use
       
   451        * in order to catch bugs as soon as possible...
       
   452        */
       
   453       reset_var_type_symbol();
       
   454       
   381       return NULL;
   455       return NULL;
   382     }
   456     }
   383 
   457 
   384 /**************************************/
   458 /**************************************/
   385 /* B.1.5 - Program organization units */
   459 /* B.1.5 - Program organization units */
   395 /*****************************/
   469 /*****************************/
   396 /* B 1.5.2 - Function Blocks */
   470 /* B 1.5.2 - Function Blocks */
   397 /*****************************/
   471 /*****************************/
   398     void *visit(function_block_declaration_c *symbol) {
   472     void *visit(function_block_declaration_c *symbol) {
   399       if (current_declarationtype == variables_dt && configuration_defined) {
   473       if (current_declarationtype == variables_dt && configuration_defined) {
   400         symbol->fblock_name->accept(*this);
       
   401         s4o.print(";\n");
       
   402         symbol->var_declarations->accept(*this);
   474         symbol->var_declarations->accept(*this);
   403         symbol->fblock_body->accept(*this);
   475         symbol->fblock_body->accept(*this);
   404       }
   476       }
   405       return NULL;
   477       return NULL;
   406     }
   478     }
   408 /**********************/
   480 /**********************/
   409 /* B 1.5.3 - Programs */
   481 /* B 1.5.3 - Programs */
   410 /**********************/
   482 /**********************/
   411     void *visit(program_declaration_c *symbol) {
   483     void *visit(program_declaration_c *symbol) {
   412       if (current_declarationtype == variables_dt && configuration_defined) {
   484       if (current_declarationtype == variables_dt && configuration_defined) {
   413         symbol->program_type_name->accept(*this);
       
   414         s4o.print(";\n");
       
   415         symbol->var_declarations->accept(*this);
   485         symbol->var_declarations->accept(*this);
   416         symbol->function_block_body->accept(*this);
   486         symbol->function_block_body->accept(*this);
   417       }
   487       }
   418       return NULL;
   488       return NULL;
   419     }
   489     }
   550           break;
   620           break;
   551         case variables_dt:
   621         case variables_dt:
   552           /* Start off by setting the current_var_type_symbol and
   622           /* Start off by setting the current_var_type_symbol and
   553            * current_var_init_symbol private variables...
   623            * current_var_init_symbol private variables...
   554            */
   624            */
   555           update_var_type_symbol(symbol->program_type_name, true);
   625           update_var_type_symbol(symbol->program_type_name);
   556           
   626           
   557           declare_variable(symbol->program_name, "FB");
   627           declare_variable(symbol->program_name);
   558           
   628           
   559           /* Values no longer in scope, and therefore no longer used.
   629           /* Values no longer in scope, and therefore no longer used.
   560            * Make an effort to keep them set to NULL when not in use
   630            * Make an effort to keep them set to NULL when not in use
   561            * in order to catch bugs as soon as possible...
   631            * in order to catch bugs as soon as possible...
   562            */
   632            */