stage4/generate_cc/generate_cc_vardecl.cc
changeset 70 e1f0ebd2d9ec
parent 69 41cb5b80416e
child 71 c2c867171c07
equal deleted inserted replaced
69:41cb5b80416e 70:e1f0ebd2d9ec
     1 /*
       
     2  * (c) 2003 Mario de Sousa
       
     3  *
       
     4  * Offered to the public under the terms of the GNU General Public License
       
     5  * as published by the Free Software Foundation; either version 2 of the
       
     6  * License, or (at your option) any later version.
       
     7  *
       
     8  * This program is distributed in the hope that it will be useful, but
       
     9  * WITHOUT ANY WARRANTY; without even the implied warranty of
       
    10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
       
    11  * Public License for more details.
       
    12  *
       
    13  * This code is made available on the understanding that it will not be
       
    14  * used in safety-critical situations without a full and competent review.
       
    15  */
       
    16 
       
    17 /*
       
    18  * An IEC 61131-3 IL and ST compiler.
       
    19  *
       
    20  * Based on the
       
    21  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
       
    22  *
       
    23  */
       
    24 
       
    25 
       
    26 /*
       
    27  * Conversion of variable declaration constructs.
       
    28  *
       
    29  * This is part of the 4th stage that generates
       
    30  * a c++ source program equivalent to the IL and ST
       
    31  * code.
       
    32  */
       
    33 
       
    34 
       
    35 
       
    36 
       
    37 //#include <stdio.h>  /* required for NULL */
       
    38 //#include <string>
       
    39 //#include <iostream>
       
    40 
       
    41 //#include "../../util/symtable.hh"
       
    42 
       
    43 
       
    44 
       
    45 
       
    46 
       
    47 
       
    48 
       
    49 
       
    50 
       
    51 /***********************************************************************/
       
    52 /***********************************************************************/
       
    53 /***********************************************************************/
       
    54 /***********************************************************************/
       
    55 /***********************************************************************/
       
    56 /***********************************************************************/
       
    57 /***********************************************************************/
       
    58 /***********************************************************************/
       
    59 
       
    60 
       
    61 
       
    62 
       
    63 class generate_cc_vardecl_c: protected generate_cc_typedecl_c {
       
    64 
       
    65   /* A Helper class to the main class... */
       
    66   /* print a string, except the first time it is called */
       
    67   /* used to print the separator "," before each variable
       
    68    * declaration, except the first...
       
    69    *
       
    70    * Needs to be done in a seperate class because this is called
       
    71    * from within various locations within the code.
       
    72    */
       
    73   class next_var_c {
       
    74     private:
       
    75       bool print_flag;
       
    76       std::string str1, str2;
       
    77 
       
    78       next_var_c *embedded_scope;
       
    79 
       
    80     public:
       
    81       next_var_c(std::string s1, std::string s2) {
       
    82         str1 = s1;
       
    83         str2 = s2;
       
    84         print_flag = false;
       
    85         embedded_scope = NULL;
       
    86       }
       
    87 
       
    88       std::string get(void) {
       
    89         if (NULL != embedded_scope)
       
    90           return embedded_scope->get();
       
    91 
       
    92         bool old_print_flag = print_flag;
       
    93         print_flag = true;
       
    94         if (!old_print_flag)
       
    95           return str1;
       
    96         else
       
    97           return str2;
       
    98       }
       
    99 
       
   100       /* Create a new next_var_c for an embedded scope.
       
   101        * From now on, every call to get() will return the
       
   102        * inner-most scope...!
       
   103        */
       
   104       void push(std::string s1, std::string s2) {
       
   105         if (NULL != embedded_scope)
       
   106           return embedded_scope->push(s1, s2);
       
   107 
       
   108         embedded_scope = new next_var_c(s1, s2);
       
   109         if (NULL == embedded_scope)
       
   110           ERROR;
       
   111         return;
       
   112       }
       
   113 
       
   114       /* Remove the inner-most scope... */
       
   115       void pop(void) {
       
   116         if (NULL != embedded_scope)
       
   117           return embedded_scope->pop();
       
   118 
       
   119         delete embedded_scope;
       
   120         embedded_scope = NULL;
       
   121       }
       
   122   };
       
   123 
       
   124   private:
       
   125     /* used to generate the ',' separating the parameters in a function call */
       
   126     next_var_c *nv;
       
   127 
       
   128 
       
   129   public:
       
   130     /* the types of variables that need to be processed... */
       
   131     static const unsigned int none_vt	  = 0x0000;
       
   132     static const unsigned int input_vt	  = 0x0001;  // VAR_INPUT
       
   133     static const unsigned int output_vt	  = 0x0002;  // VAR_OUTPUT
       
   134     static const unsigned int inoutput_vt = 0x0004;  // VAR_IN_OUT
       
   135     static const unsigned int private_vt  = 0x0008;  // VAR
       
   136     static const unsigned int temp_vt	  = 0x0010;  // VAR_TEMP
       
   137     static const unsigned int external_vt = 0x0020;  // VAR_EXTERNAL
       
   138     static const unsigned int global_vt   = 0x0040;  // VAR_GLOBAL
       
   139 						     //    Globals declared inside a resource will not be declared
       
   140 						     //    unless global_vt is acompanied by resource_vt
       
   141     static const unsigned int located_vt  = 0x0080;  // VAR <var_name> AT <location>
       
   142     static const unsigned int program_vt  = 0x0100;  // PROGRAM (inside a configuration!)
       
   143 						     //    Programs declared inside a resource will not be declared
       
   144 						     //    unless program_vt is acompanied by resource_vt
       
   145 
       
   146     static const unsigned int resource_vt = 0x8000;  // RESOURCE (inside a configuration!)
       
   147                                                      //    This, just of itself, will not print out any declarations!!
       
   148 						     //    It must be acompanied by either program_vt and/or global_vt
       
   149 
       
   150     /* How variables should be declared: as local variables or
       
   151      * variables within a function call interface.
       
   152      *
       
   153      * This will define the format of the output generated
       
   154      * by this class.
       
   155      *
       
   156      * finterface_vf: function interface parameters
       
   157      *               e.g.  f(  int a, long b, real c );
       
   158      *                         ---------------------
       
   159      *               This class/function will produce the
       
   160      *               underlined code of the above examples,
       
   161      *               and no more!!
       
   162      *
       
   163      * localinit_vf: local declaration. Will include variable
       
   164      *           initialisation if it is not a located variable.
       
   165      *           e.g.
       
   166      *                int a = 9;
       
   167      *                long b = 99;
       
   168      *                real c = 99.9;
       
   169      *
       
   170      * local_vf: local declaration. Will NOT include variable
       
   171      *           initialisation.
       
   172      *           e.g.
       
   173      *                int a;
       
   174      *                long b;
       
   175      *                real c;
       
   176      *
       
   177      * init_vf: local initialisation without declaration.
       
   178      *           e.g.
       
   179      *                a = 9;
       
   180      *                b = 99;
       
   181      *                c = 99.9;
       
   182      *
       
   183      * constructorinit_vf: initialising of member variables...
       
   184      *                e.g. for a constructor...
       
   185      *                class_name_c(void)
       
   186      *                : a(9), b(99), c(99.9)  { // code... }
       
   187      *                  --------------------
       
   188      *               This class/function will produce the
       
   189      *               underlined code of the above examples,
       
   190      *               and no more!!
       
   191      *
       
   192      * globalinit_vf: initialising of static c++ variables. These
       
   193      *                variables may have been declared as static inside
       
   194      *                a class, in which case the scope within which they were
       
   195      *                previously delcared must be passed as the second parameter
       
   196      *                to the print() function.
       
   197      *
       
   198      *                e.g.
       
   199      *                __plc_pt_c<INT, 8*sizeof(INT)> START_P::loc = __plc_pt_c<INT, 8*sizeof(INT)>("I2");
       
   200      */
       
   201     typedef enum {finterface_vf,
       
   202                     local_vf,
       
   203                     localstatic_vf,
       
   204 		    localinit_vf,
       
   205 		    init_vf,
       
   206 		    constructorinit_vf,
       
   207 		    globalinit_vf
       
   208 		   } varformat_t;
       
   209 
       
   210 
       
   211   private:
       
   212     /* variable used to store the types of variables that need to be processed... */
       
   213     /* Only set in the constructor...! */
       
   214     /* Will contain a set of values of generate_cc_vardecl_c::XXXX_vt */
       
   215     unsigned int wanted_vartype;
       
   216     /* variable used to store the type of variable currently being processed... */
       
   217     /* Will contain a single value of generate_cc_vardecl_c::XXXX_vt */
       
   218     unsigned int current_vartype;
       
   219 
       
   220     /* How variables should be declared: as local variables or
       
   221      * variables within a function interface...
       
   222      */
       
   223     /* Only set in the constructor...! */
       
   224     varformat_t wanted_varformat;
       
   225 
       
   226     /* The number of variables already declared. */
       
   227     /* Used to declare 'void' in case no variables are declared in a function interface... */
       
   228     int finterface_var_count;
       
   229 
       
   230 
       
   231 
       
   232     /* Holds the references to the type and initial value
       
   233      * of the variables currently being declared.
       
   234      * Please read the comment under var1_init_decl_c for further
       
   235      * details...
       
   236      *
       
   237      * We make an effort to keep these pointers pointing to NULL
       
   238      * whenever we are outside the scope of variable declaration
       
   239      * (i.e. when we are traversing a part of the parse tree which
       
   240      * is not part of variable declaration) in order tio try to catch
       
   241      * any bugs as soon as possible.
       
   242      */
       
   243     symbol_c *current_var_type_symbol;
       
   244     symbol_c *current_var_init_symbol;
       
   245     void update_type_init(symbol_c *symbol /* a spec_init_c, subrange_spec_init_c, etc... */ ) {
       
   246       this->current_var_type_symbol = spec_init_sperator_c::get_spec(symbol);
       
   247       this->current_var_init_symbol = spec_init_sperator_c::get_init(symbol);
       
   248       if (NULL == this->current_var_type_symbol)
       
   249         ERROR;
       
   250       if (NULL == this->current_var_init_symbol) {
       
   251         /* We try to find the data type's default value... */
       
   252         this->current_var_init_symbol = (symbol_c *)this->current_var_type_symbol->accept(*type_initial_value_c::instance());
       
   253       /* Note that Function Block 'data types' do not have a default value, so we cannot abort if no default value is found! */
       
   254       /*
       
   255       if (NULL == this->current_var_init_symbol)
       
   256         ERROR;
       
   257       */
       
   258       }
       
   259     }
       
   260 
       
   261     void void_type_init(void) {
       
   262       this->current_var_type_symbol = NULL;
       
   263       this->current_var_init_symbol = NULL;
       
   264     }
       
   265 
       
   266     /* Only used when wanted_varformat == globalinit_vf
       
   267      * Holds a pointer to an identifier_c, which in turns contains
       
   268      * the identifier of the scope within which the static member was
       
   269      * declared.
       
   270      */
       
   271     symbol_c *globalnamespace;
       
   272 
       
   273     /* Actually produce the output where variables are declared... */
       
   274     /* Note that located variables are the exception, they
       
   275      * being declared in the located_var_decl_c visitor...
       
   276      */
       
   277     void *declare_variables(symbol_c *symbol, bool is_fb = false) {
       
   278       list_c *list = dynamic_cast<list_c *>(symbol);
       
   279       /* should NEVER EVER occur!! */
       
   280       if (list == NULL) ERROR;
       
   281 
       
   282       /* now to produce the c equivalent... */
       
   283       if ((wanted_varformat == local_vf) ||
       
   284           (wanted_varformat == init_vf) ||
       
   285           (wanted_varformat == localinit_vf)) {
       
   286         for(int i = 0; i < list->n; i++) {
       
   287           s4o.print(s4o.indent_spaces);
       
   288           if (wanted_varformat != init_vf) {
       
   289             this->current_var_type_symbol->accept(*this);
       
   290             s4o.print(" ");
       
   291           }
       
   292           print_variable_prefix();
       
   293           list->elements[i]->accept(*this);
       
   294           if (wanted_varformat != local_vf) {
       
   295             if (this->current_var_init_symbol != NULL) {
       
   296               s4o.print(" = ");
       
   297               this->current_var_init_symbol->accept(*this);
       
   298             }
       
   299           }
       
   300           s4o.print(";\n");
       
   301         }
       
   302       }
       
   303 
       
   304       if (wanted_varformat == finterface_vf) {
       
   305         for(int i = 0; i < list->n; i++) {
       
   306           finterface_var_count++;
       
   307           s4o.print(nv->get());
       
   308           s4o.print("\n" + s4o.indent_spaces);
       
   309           this->current_var_type_symbol->accept(*this);
       
   310           if ((current_vartype & (output_vt | inoutput_vt)) != 0)
       
   311             s4o.print(" &");
       
   312           else
       
   313             s4o.print(" ");
       
   314           list->elements[i]->accept(*this);
       
   315           /* We do not print the initial value at function declaration!
       
   316            * It is up to the caller to pass the correct default value
       
   317            * if none is specified in the ST source code
       
   318            */
       
   319           /* if (this->current_var_init_symbol != NULL) {
       
   320                s4o.print(" = "); this->current_var_init_symbol->accept(*this);}
       
   321            */
       
   322         }
       
   323       }
       
   324 
       
   325       if (wanted_varformat == constructorinit_vf) {
       
   326         for(int i = 0; i < list->n; i++) {
       
   327           if (is_fb) {
       
   328             s4o.print(nv->get());
       
   329             this->current_var_type_symbol->accept(*this);
       
   330             s4o.print(FB_INIT_SUFFIX);
       
   331             s4o.print("(&");
       
   332             this->print_variable_prefix();
       
   333             list->elements[i]->accept(*this);
       
   334             s4o.print(");");
       
   335           }
       
   336           else if (this->current_var_init_symbol != NULL) {
       
   337             s4o.print(nv->get());
       
   338             this->print_variable_prefix();
       
   339             list->elements[i]->accept(*this);
       
   340             s4o.print(" = ");
       
   341             this->current_var_init_symbol->accept(*this);
       
   342             s4o.print(";");
       
   343           }
       
   344         }
       
   345       }
       
   346 
       
   347       return NULL;
       
   348     }
       
   349 
       
   350 
       
   351 
       
   352 
       
   353   public:
       
   354     generate_cc_vardecl_c(stage4out_c *s4o_ptr, varformat_t varformat, unsigned int vartype)
       
   355     : generate_cc_typedecl_c(s4o_ptr) {
       
   356       wanted_varformat = varformat;
       
   357       wanted_vartype   = vartype;
       
   358       current_vartype  = none_vt;
       
   359       current_var_type_symbol = NULL;
       
   360       current_var_init_symbol = NULL;
       
   361       globalnamespace         = NULL;
       
   362       nv = NULL;
       
   363     }
       
   364 
       
   365     ~generate_cc_vardecl_c(void) {}
       
   366 
       
   367 
       
   368     void print(symbol_c *symbol, symbol_c *scope = NULL, const char *variable_prefix = NULL) {
       
   369       this->set_variable_prefix(variable_prefix);
       
   370       if (globalinit_vf == wanted_varformat)
       
   371         globalnamespace = scope;
       
   372 
       
   373       finterface_var_count = 0;
       
   374 
       
   375       switch (wanted_varformat) {
       
   376         case constructorinit_vf: nv = new next_var_c("", "\n"+s4o.indent_spaces); break;
       
   377         case finterface_vf:      /* fall through... */
       
   378         case localinit_vf:       /* fall through... */
       
   379         case local_vf:           nv = new next_var_c("", ", "); break;
       
   380         default:                 nv = NULL;
       
   381       } /* switch() */
       
   382 
       
   383       symbol->accept(*this);
       
   384 
       
   385       /* special case... */
       
   386       if (wanted_varformat == finterface_vf)
       
   387         if (finterface_var_count == 0)
       
   388           s4o.print("void");
       
   389 
       
   390       delete nv;
       
   391       nv = NULL;
       
   392       globalnamespace = NULL;
       
   393     }
       
   394 
       
   395 
       
   396   protected:
       
   397 /***************************/
       
   398 /* B 0 - Programming Model */
       
   399 /***************************/
       
   400   /* leave for derived classes... */
       
   401 
       
   402 /*************************/
       
   403 /* B.1 - Common elements */
       
   404 /*************************/
       
   405 /*******************************************/
       
   406 /* B 1.1 - Letters, digits and identifiers */
       
   407 /*******************************************/
       
   408   /* done in base class(es) */
       
   409 
       
   410 /*********************/
       
   411 /* B 1.2 - Constants */
       
   412 /*********************/
       
   413   /* originally empty... */
       
   414 
       
   415 /******************************/
       
   416 /* B 1.2.1 - Numeric Literals */
       
   417 /******************************/
       
   418   /* done in base class(es) */
       
   419 
       
   420 /*******************************/
       
   421 /* B.1.2.2   Character Strings */
       
   422 /*******************************/
       
   423   /* done in base class(es) */
       
   424 
       
   425 /***************************/
       
   426 /* B 1.2.3 - Time Literals */
       
   427 /***************************/
       
   428 /************************/
       
   429 /* B 1.2.3.1 - Duration */
       
   430 /************************/
       
   431   /* done in base class(es) */
       
   432 
       
   433 /************************************/
       
   434 /* B 1.2.3.2 - Time of day and Date */
       
   435 /************************************/
       
   436   /* done in base class(es) */
       
   437 
       
   438 /**********************/
       
   439 /* B.1.3 - Data types */
       
   440 /**********************/
       
   441 /***********************************/
       
   442 /* B 1.3.1 - Elementary Data Types */
       
   443 /***********************************/
       
   444   /* done in base class(es) */
       
   445 
       
   446 /********************************/
       
   447 /* B.1.3.2 - Generic data types */
       
   448 /********************************/
       
   449   /* originally empty... */
       
   450 
       
   451 /********************************/
       
   452 /* B 1.3.3 - Derived data types */
       
   453 /********************************/
       
   454   /* done in base class(es) */
       
   455 
       
   456 /*********************/
       
   457 /* B 1.4 - Variables */
       
   458 /*********************/
       
   459   /* done in base class(es) */
       
   460 
       
   461 /********************************************/
       
   462 /* B.1.4.1   Directly Represented Variables */
       
   463 /********************************************/
       
   464   /* done in base class(es) */
       
   465 
       
   466 /*************************************/
       
   467 /* B.1.4.2   Multi-element Variables */
       
   468 /*************************************/
       
   469   /* done in base class(es) */
       
   470 
       
   471 /******************************************/
       
   472 /* B 1.4.3 - Declaration & Initialisation */
       
   473 /******************************************/
       
   474 void *visit(constant_option_c *symbol) {s4o.print("CONSTANT"); return NULL;}
       
   475 void *visit(retain_option_c *symbol) {s4o.print("RETAIN"); return NULL;}
       
   476 void *visit(non_retain_option_c *symbol) {s4o.print("NON_RETAIN"); return NULL;}
       
   477 
       
   478 void *visit(input_declarations_c *symbol) {
       
   479   TRACE("input_declarations_c");
       
   480   if ((wanted_vartype & input_vt) != 0) {
       
   481 /*
       
   482     // TO DO ...
       
   483     if (symbol->option != NULL)
       
   484       symbol->option->accept(*this);
       
   485 */
       
   486     //s4o.indent_right();
       
   487     current_vartype = input_vt;
       
   488     symbol->input_declaration_list->accept(*this);
       
   489     current_vartype = none_vt;
       
   490     //s4o.indent_left();
       
   491   }
       
   492   return NULL;
       
   493 }
       
   494 
       
   495 /* helper symbol for input_declarations */
       
   496 void *visit(input_declaration_list_c *symbol) {
       
   497   TRACE("input_declaration_list_c");
       
   498   print_list(symbol);
       
   499   return NULL;
       
   500 }
       
   501 
       
   502 void *visit(edge_declaration_c *symbol) {
       
   503   TRACE("edge_declaration_c");
       
   504   // TO DO ...
       
   505   symbol->var1_list->accept(*this);
       
   506   s4o.print(" : BOOL ");
       
   507   symbol->edge->accept(*this);
       
   508   return NULL;
       
   509 }
       
   510 
       
   511 void *visit(raising_edge_option_c *symbol) {
       
   512   // TO DO ...
       
   513   s4o.print("R_EDGE");
       
   514   return NULL;
       
   515 }
       
   516 
       
   517 
       
   518 #if 0
       
   519 /* var1_list ':' array_spec_init */
       
   520 SYM_REF2(array_var_init_decl_c, var1_list, array_spec_init)
       
   521 #endif
       
   522 
       
   523 /*  var1_list ':' initialized_structure */
       
   524 // SYM_REF2(structured_var_init_decl_c, var1_list, initialized_structure)
       
   525 void *visit(structured_var_init_decl_c *symbol) {
       
   526   TRACE("structured_var_init_decl_c");
       
   527   /* Please read the comments inside the var1_init_decl_c
       
   528    * visitor, as they apply here too.
       
   529    */
       
   530 
       
   531   /* Start off by setting the current_var_type_symbol and
       
   532    * current_var_init_symbol private variables...
       
   533    */
       
   534   update_type_init(symbol->initialized_structure);
       
   535 
       
   536   /* now to produce the c equivalent... */
       
   537   symbol->var1_list->accept(*this);
       
   538 
       
   539   /* Values no longer in scope, and therefore no longer used.
       
   540    * Make an effort to keep them set to NULL when not in use
       
   541    * in order to catch bugs as soon as possible...
       
   542    */
       
   543   void_type_init();
       
   544 
       
   545   return NULL;
       
   546 }
       
   547 
       
   548 /* fb_name_list ':' function_block_type_name ASSIGN structure_initialization */
       
   549 /* structure_initialization -> may be NULL ! */
       
   550 void *visit(fb_name_decl_c *symbol) {
       
   551   TRACE("fb_name_decl_c");
       
   552   /* Please read the comments inside the var1_init_decl_c
       
   553    * visitor, as they apply here too.
       
   554    */
       
   555 
       
   556   /* Start off by setting the current_var_type_symbol and
       
   557    * current_var_init_symbol private variables...
       
   558    */
       
   559   update_type_init(symbol);
       
   560 
       
   561   /* now to produce the c equivalent... */
       
   562   symbol->fb_name_list->accept(*this);
       
   563 
       
   564   /* Values no longer in scope, and therefore no longer used.
       
   565    * Make an effort to keep them set to NULL when not in use
       
   566    * in order to catch bugs as soon as possible...
       
   567    */
       
   568   void_type_init();
       
   569 
       
   570   return NULL;
       
   571 }
       
   572 
       
   573 /* fb_name_list ',' fb_name */
       
   574 void *visit(fb_name_list_c *symbol) {
       
   575   TRACE("fb_name_list_c");
       
   576   declare_variables(symbol, true);
       
   577   return NULL;
       
   578 }
       
   579 
       
   580 
       
   581 /* VAR_OUTPUT [RETAIN | NON_RETAIN] var_init_decl_list END_VAR */
       
   582 /* option -> may be NULL ! */
       
   583 void *visit(output_declarations_c *symbol) {
       
   584   TRACE("output_declarations_c");
       
   585   if ((wanted_vartype & output_vt) != 0) {
       
   586 /*
       
   587     // TO DO ...
       
   588     if (symbol->option != NULL)
       
   589       symbol->option->accept(*this);
       
   590 */
       
   591     //s4o.indent_right();
       
   592     current_vartype = output_vt;
       
   593     symbol->var_init_decl_list->accept(*this);
       
   594     current_vartype = none_vt;
       
   595     //s4o.indent_left();
       
   596   }
       
   597   return NULL;
       
   598 }
       
   599 
       
   600 /*  VAR_IN_OUT var_declaration_list END_VAR */
       
   601 void *visit(input_output_declarations_c *symbol) {
       
   602   TRACE("input_output_declarations_c");
       
   603   if ((wanted_vartype & inoutput_vt) != 0) {
       
   604     //s4o.indent_right();
       
   605     current_vartype = inoutput_vt;
       
   606     symbol->var_declaration_list->accept(*this);
       
   607     current_vartype = none_vt;
       
   608     //s4o.indent_left();
       
   609   }
       
   610   return NULL;
       
   611 }
       
   612 
       
   613 /* helper symbol for input_output_declarations */
       
   614 /* var_declaration_list var_declaration ';' */
       
   615 void *visit(var_declaration_list_c *symbol) {
       
   616   TRACE("var_declaration_list_c");
       
   617   print_list(symbol);
       
   618   return NULL;
       
   619 }
       
   620 
       
   621 #if 0
       
   622 /*  var1_list ':' array_specification */
       
   623 SYM_REF2(array_var_declaration_c, var1_list, array_specification)
       
   624 #endif
       
   625 
       
   626 /*  var1_list ':' structure_type_name */
       
   627 //SYM_REF2(structured_var_declaration_c, var1_list, structure_type_name)
       
   628 void *visit(structured_var_declaration_c *symbol) {
       
   629   TRACE("structured_var_declaration_c");
       
   630   /* Please read the comments inside the var1_init_decl_c
       
   631    * visitor, as they apply here too.
       
   632    */
       
   633 
       
   634   /* Start off by setting the current_var_type_symbol and
       
   635    * current_var_init_symbol private variables...
       
   636    */
       
   637   update_type_init(symbol->structure_type_name);
       
   638 
       
   639   /* now to produce the c equivalent... */
       
   640   symbol->var1_list->accept(*this);
       
   641 
       
   642   /* Values no longer in scope, and therefore no longer used.
       
   643    * Make an effort to keep them set to NULL when not in use
       
   644    * in order to catch bugs as soon as possible...
       
   645    */
       
   646   void_type_init();
       
   647 
       
   648   return NULL;
       
   649 }
       
   650 
       
   651 
       
   652 /* VAR [CONSTANT] var_init_decl_list END_VAR */
       
   653 /* option -> may be NULL ! */
       
   654 /* helper symbol for input_declarations */
       
   655 void *visit(var_declarations_c *symbol) {
       
   656   TRACE("var_declarations_c");
       
   657   if ((wanted_vartype & private_vt) != 0) {
       
   658 /*
       
   659     // TO DO ...
       
   660     if (symbol->option != NULL)
       
   661       symbol->option->accept(*this);
       
   662 */
       
   663     current_vartype = private_vt;
       
   664     symbol->var_init_decl_list->accept(*this);
       
   665     current_vartype = none_vt;
       
   666   }
       
   667   return NULL;
       
   668 }
       
   669 
       
   670 /*  VAR RETAIN var_init_decl_list END_VAR */
       
   671 void *visit(retentive_var_declarations_c *symbol) {
       
   672   TRACE("retentive_var_declarations_c");
       
   673   if ((wanted_vartype & private_vt) != 0) {
       
   674     current_vartype = private_vt;
       
   675     symbol->var_init_decl_list->accept(*this);
       
   676     current_vartype = none_vt;
       
   677   }
       
   678   return NULL;
       
   679 }
       
   680 
       
   681 /*  VAR [CONSTANT|RETAIN|NON_RETAIN] located_var_decl_list END_VAR */
       
   682 /* option -> may be NULL ! */
       
   683 //SYM_REF2(located_var_declarations_c, option, located_var_decl_list)
       
   684 void *visit(located_var_declarations_c *symbol) {
       
   685   TRACE("located_var_declarations_c");
       
   686   if ((wanted_vartype & located_vt) != 0) {
       
   687 /*
       
   688     // TO DO ...
       
   689     if (symbol->option != NULL)
       
   690       symbol->option->accept(*this);
       
   691 */
       
   692     current_vartype = located_vt;
       
   693     symbol->located_var_decl_list->accept(*this);
       
   694     current_vartype = none_vt;
       
   695   }
       
   696   return NULL;
       
   697 }
       
   698 
       
   699 /* helper symbol for located_var_declarations */
       
   700 /* located_var_decl_list located_var_decl ';' */
       
   701 //SYM_LIST(located_var_decl_list_c)
       
   702 void *visit(located_var_decl_list_c *symbol) {
       
   703   TRACE("located_var_decl_list_c");
       
   704   print_list(symbol);
       
   705   return NULL;
       
   706 }
       
   707 
       
   708 
       
   709 /*  [variable_name] location ':' located_var_spec_init */
       
   710 /* variable_name -> may be NULL ! */
       
   711 //SYM_REF4(located_var_decl_c, variable_name, location, located_var_spec_init, unused)
       
   712 void *visit(located_var_decl_c *symbol) {
       
   713   TRACE("located_var_decl_c");
       
   714   /* Please read the comments inside the var1_init_decl_c
       
   715    * visitor, as they apply here too.
       
   716    */
       
   717 
       
   718   /* Start off by setting the current_var_type_symbol and
       
   719    * current_var_init_symbol private variables...
       
   720    */
       
   721   update_type_init(symbol->located_var_spec_init);
       
   722 
       
   723   /* now to produce the c equivalent... */
       
   724   switch(wanted_varformat) {
       
   725     case local_vf:
       
   726       s4o.print(s4o.indent_spaces);
       
   727       this->current_var_type_symbol->accept(*this);
       
   728       s4o.print(" *");
       
   729       if (symbol->variable_name != NULL)
       
   730         symbol->variable_name->accept(*this);
       
   731       else
       
   732         symbol->location->accept(*this);
       
   733       s4o.print(";\n");
       
   734       break;
       
   735 
       
   736     case constructorinit_vf:
       
   737       s4o.print(nv->get());
       
   738       s4o.print("{extern ");
       
   739       this->current_var_type_symbol->accept(*this);
       
   740       s4o.print(" ");
       
   741       symbol->location->accept(*this);
       
   742       s4o.print("; ");
       
   743       print_variable_prefix();
       
   744       if (symbol->variable_name != NULL)
       
   745         symbol->variable_name->accept(*this);
       
   746       else
       
   747         symbol->location->accept(*this);
       
   748       s4o.print(" = &");
       
   749       symbol->location->accept(*this);
       
   750       s4o.print(";}");
       
   751       break;
       
   752 
       
   753     case globalinit_vf:
       
   754       s4o.print(s4o.indent_spaces + "__plc_pt_c<");
       
   755       this->current_var_type_symbol->accept(*this);
       
   756       s4o.print(", 8*sizeof(");
       
   757       this->current_var_type_symbol->accept(*this);
       
   758       s4o.print(")> ");
       
   759       if (this->globalnamespace != NULL) {
       
   760         this->globalnamespace->accept(*this);
       
   761         s4o.print("::");
       
   762       }
       
   763       if (symbol->variable_name != NULL)
       
   764         symbol->variable_name->accept(*this);
       
   765       else
       
   766         symbol->location->accept(*this);
       
   767 
       
   768       s4o.print(" = ");
       
   769 
       
   770       s4o.print(s4o.indent_spaces + "__plc_pt_c<");
       
   771       this->current_var_type_symbol->accept(*this);
       
   772       s4o.print(", 8*sizeof(");
       
   773       this->current_var_type_symbol->accept(*this);
       
   774       s4o.print(")>(\"");
       
   775       symbol->location->accept(*this);
       
   776       s4o.print("\"");
       
   777       if (this->current_var_init_symbol != NULL) {
       
   778         s4o.print(", ");
       
   779         this->current_var_init_symbol->accept(*this);
       
   780       }
       
   781       s4o.print(");\n");
       
   782       break;
       
   783 
       
   784     default:
       
   785       ERROR;
       
   786   } /* switch() */
       
   787 
       
   788   /* Values no longer in scope, and therefore no longer used.
       
   789    * Make an effort to keep them set to NULL when not in use
       
   790    * in order to catch bugs as soon as possible...
       
   791    */
       
   792   void_type_init();
       
   793 
       
   794   return NULL;
       
   795 }
       
   796 
       
   797 
       
   798 
       
   799 
       
   800 /*| VAR_EXTERNAL [CONSTANT] external_declaration_list END_VAR */
       
   801 /* option -> may be NULL ! */
       
   802 //SYM_REF2(external_var_declarations_c, option, external_declaration_list)
       
   803 void *visit(external_var_declarations_c *symbol) {
       
   804   TRACE("external_var_declarations_c");
       
   805   if ((wanted_vartype & external_vt) != 0) {
       
   806 /*
       
   807     // TODO ...
       
   808     if (symbol->option != NULL)
       
   809       symbol->option->accept(*this);
       
   810 */
       
   811     //s4o.indent_right();
       
   812     current_vartype = external_vt;
       
   813     symbol->external_declaration_list->accept(*this);
       
   814     current_vartype = none_vt;
       
   815     //s4o.indent_left();
       
   816   }
       
   817   return NULL;
       
   818 }
       
   819 
       
   820 /* helper symbol for external_var_declarations */
       
   821 /*| external_declaration_list external_declaration';' */
       
   822 //SYM_LIST(external_declaration_list_c)
       
   823 /* helper symbol for input_declarations */
       
   824 void *visit(external_declaration_list_c *symbol) {
       
   825   TRACE("external_declaration_list_c");
       
   826   print_list(symbol);
       
   827   return NULL;
       
   828 }
       
   829 
       
   830 
       
   831 /*  global_var_name ':' (simple_specification|subrange_specification|enumerated_specification|array_specification|prev_declared_structure_type_name|function_block_type_name */
       
   832 //SYM_REF2(external_declaration_c, global_var_name, specification)
       
   833 void *visit(external_declaration_c *symbol) {
       
   834   TRACE("external_declaration_c");
       
   835   /* Please read the comments inside the var1_init_decl_c
       
   836    * visitor, as they apply here too.
       
   837    */
       
   838 
       
   839   /* Start off by setting the current_var_type_symbol and
       
   840    * current_var_init_symbol private variables...
       
   841    */
       
   842   this->current_var_type_symbol = symbol->specification;
       
   843   this->current_var_init_symbol = NULL;
       
   844 
       
   845   /* now to produce the c equivalent... */
       
   846   switch (wanted_varformat) {
       
   847     case local_vf:
       
   848     case localinit_vf:
       
   849       s4o.print(s4o.indent_spaces);
       
   850       this->current_var_type_symbol->accept(*this);
       
   851       s4o.print(" *");
       
   852       symbol->global_var_name->accept(*this);
       
   853       if ((wanted_varformat == localinit_vf) &&
       
   854           (this->current_var_init_symbol != NULL)) {
       
   855         s4o.print(" = ");
       
   856         this->current_var_init_symbol->accept(*this);
       
   857       }
       
   858       s4o.print(";\n");
       
   859       break;
       
   860 
       
   861     case constructorinit_vf:
       
   862       s4o.print(nv->get());
       
   863       s4o.print("{extern ");
       
   864       this->current_var_type_symbol->accept(*this);
       
   865       s4o.print(" *");
       
   866       symbol->global_var_name->accept(*this);
       
   867       s4o.print("; ");
       
   868       print_variable_prefix();
       
   869       symbol->global_var_name->accept(*this);
       
   870       s4o.print(" = ");
       
   871       symbol->global_var_name->accept(*this);
       
   872       s4o.print(";}");
       
   873       break;
       
   874 
       
   875     case finterface_vf:
       
   876       finterface_var_count++;
       
   877       s4o.print(nv->get());
       
   878       this->current_var_type_symbol->accept(*this);
       
   879       s4o.print(" *");
       
   880       symbol->global_var_name->accept(*this);
       
   881       break;
       
   882 
       
   883     default:
       
   884       ERROR;
       
   885   }
       
   886 
       
   887   /* Values no longer in scope, and therefore no longer used.
       
   888    * Make an effort to keep them set to NULL when not in use
       
   889    * in order to catch bugs as soon as possible...
       
   890    */
       
   891   void_type_init();
       
   892 
       
   893   return NULL;
       
   894 }
       
   895 
       
   896 
       
   897 
       
   898 /*| VAR_GLOBAL [CONSTANT|RETAIN] global_var_decl_list END_VAR */
       
   899 /* option -> may be NULL ! */
       
   900 // SYM_REF2(global_var_declarations_c, option, global_var_decl_list)
       
   901 void *visit(global_var_declarations_c *symbol) {
       
   902   TRACE("global_var_declarations_c");
       
   903   if ((wanted_vartype & global_vt) != 0) {
       
   904 /*
       
   905     // TODO ...
       
   906     if (symbol->option != NULL)
       
   907       symbol->option->accept(*this);
       
   908 */
       
   909     //s4o.indent_right();
       
   910     unsigned int previous_vartype = current_vartype;
       
   911       // previous_vartype will be either none_vt, or resource_vt
       
   912     current_vartype = global_vt;
       
   913     symbol->global_var_decl_list->accept(*this);
       
   914     current_vartype = previous_vartype;
       
   915     //s4o.indent_left();
       
   916   }
       
   917   return NULL;
       
   918 }
       
   919 
       
   920 /* helper symbol for global_var_declarations */
       
   921 /*| global_var_decl_list global_var_decl ';' */
       
   922 //SYM_LIST(global_var_decl_list_c)
       
   923 void *visit(global_var_decl_list_c *symbol) {
       
   924   TRACE("global_var_decl_list_c");
       
   925   print_list(symbol);
       
   926   return NULL;
       
   927 }
       
   928 
       
   929 
       
   930 
       
   931 /*| global_var_spec ':' [located_var_spec_init|function_block_type_name] */
       
   932 /* type_specification ->may be NULL ! */
       
   933 // SYM_REF2(global_var_decl_c, global_var_spec, type_specification)
       
   934 void *visit(global_var_decl_c *symbol) {
       
   935   TRACE("global_var_decl_c");
       
   936   /* Please read the comments inside the var1_init_decl_c
       
   937    * visitor, as they apply here too.
       
   938    */
       
   939 
       
   940   /* Start off by setting the current_var_type_symbol and
       
   941    * current_var_init_symbol private variables...
       
   942    */
       
   943   update_type_init(symbol->type_specification);
       
   944 
       
   945   /* now to produce the c equivalent... */
       
   946   symbol->global_var_spec->accept(*this);
       
   947 
       
   948   /* Values no longer in scope, and therefore no longer used.
       
   949    * Make an effort to keep them set to NULL when not in use
       
   950    * in order to catch bugs as soon as possible...
       
   951    */
       
   952   void_type_init();
       
   953 
       
   954   return NULL;
       
   955 }
       
   956 
       
   957 
       
   958 /*| global_var_name location */
       
   959 // SYM_REF2(global_var_spec_c, global_var_name, location)
       
   960 void *visit(global_var_spec_c *symbol) {
       
   961   TRACE("global_var_spec_c");
       
   962 
       
   963   /* now to produce the c equivalent... */
       
   964   switch(wanted_varformat) {
       
   965     case local_vf:
       
   966     case localstatic_vf:
       
   967       /* NOTE: located variables must be declared static, as the connection to the
       
   968        * MatPLC point must be initialised at program startup, and not whenever
       
   969        * a new function block is instantiated!
       
   970        * Nevertheless, this construct never occurs inside a Function Block, but
       
   971        * only inside a configuration. In this case, only a single instance will
       
   972        * be created, directly at startup, so it is not necessary that the variables
       
   973        * be declared static.
       
   974        */
       
   975       s4o.print(s4o.indent_spaces);
       
   976       if (symbol->global_var_name != NULL) {
       
   977         s4o.print("extern ");
       
   978         this->current_var_type_symbol->accept(*this);
       
   979         s4o.print(" ");
       
   980         symbol->location->accept(*this);
       
   981         s4o.print(";\n");
       
   982         if (wanted_varformat == localstatic_vf)
       
   983           s4o.print("static ");
       
   984         this->current_var_type_symbol->accept(*this);
       
   985         s4o.print(" *");
       
   986         symbol->global_var_name->accept(*this);
       
   987         s4o.print(" = &");
       
   988         symbol->location->accept(*this);
       
   989         s4o.print(";\n");
       
   990       }
       
   991       break;
       
   992 
       
   993     case constructorinit_vf:
       
   994       s4o.print(nv->get());
       
   995       
       
   996       if (symbol->global_var_name != NULL) {
       
   997         s4o.print("*");
       
   998         symbol->global_var_name->accept(*this);
       
   999       }
       
  1000       else
       
  1001         symbol->location->accept(*this);
       
  1002       s4o.print(" = ");
       
  1003       if (this->current_var_init_symbol != NULL) {
       
  1004         this->current_var_init_symbol->accept(*this);
       
  1005       }
       
  1006       s4o.print(";");
       
  1007       break;
       
  1008 
       
  1009     default:
       
  1010       ERROR;
       
  1011   } /* switch() */
       
  1012 
       
  1013   return NULL;
       
  1014 }
       
  1015 
       
  1016 
       
  1017 
       
  1018 /*  AT direct_variable */
       
  1019 // SYM_REF2(location_c, direct_variable, unused)
       
  1020 void *visit(location_c *symbol) {
       
  1021   TRACE("location_c");
       
  1022   return symbol->direct_variable->accept(*this);
       
  1023 }
       
  1024 
       
  1025 
       
  1026 /*| global_var_list ',' global_var_name */
       
  1027 //SYM_LIST(global_var_list_c)
       
  1028 void *visit(global_var_list_c *symbol) {
       
  1029   TRACE("global_var_list_c");
       
  1030   list_c *list = dynamic_cast<list_c *>(symbol);
       
  1031   /* should NEVER EVER occur!! */
       
  1032   if (list == NULL) ERROR;
       
  1033 
       
  1034   /* now to produce the c equivalent... */
       
  1035   switch (wanted_varformat) {
       
  1036     case local_vf:
       
  1037     case localinit_vf:
       
  1038       for(int i = 0; i < list->n; i++) {
       
  1039         s4o.print(s4o.indent_spaces);
       
  1040         this->current_var_type_symbol->accept(*this);
       
  1041         s4o.print(" __");
       
  1042         list->elements[i]->accept(*this);
       
  1043         s4o.print(";\n");
       
  1044         this->current_var_type_symbol->accept(*this);
       
  1045         s4o.print(" *");
       
  1046         list->elements[i]->accept(*this);
       
  1047         s4o.print(" = &__");
       
  1048         list->elements[i]->accept(*this);
       
  1049 #if 0
       
  1050         if (wanted_varformat == localinit_vf) {
       
  1051           if (this->current_var_init_symbol != NULL) {
       
  1052             s4o.print(" = ");
       
  1053             this->current_var_init_symbol->accept(*this);
       
  1054           }
       
  1055         }
       
  1056 #endif
       
  1057         s4o.print(";\n");
       
  1058       }
       
  1059       break;
       
  1060 
       
  1061     case constructorinit_vf:
       
  1062       if (this->current_var_init_symbol != NULL) {
       
  1063         for(int i = 0; i < list->n; i++) {
       
  1064           s4o.print(nv->get());
       
  1065 
       
  1066           s4o.print("*");
       
  1067           list->elements[i]->accept(*this);
       
  1068           s4o.print(" = ");
       
  1069           this->current_var_init_symbol->accept(*this);
       
  1070           s4o.print(";");
       
  1071 #if 0
       
  1072  	  /* The following code would be for globalinit_vf !!
       
  1073 	   * But it is not currently required...
       
  1074 	   */
       
  1075 	  s4o.print(s4o.indent_spaces + "__ext_element_c<");
       
  1076           this->current_var_type_symbol->accept(*this);
       
  1077           s4o.print("> ");
       
  1078           if (this->globalnamespace != NULL) {
       
  1079             this->globalnamespace->accept(*this);
       
  1080             s4o.print("::");
       
  1081           }
       
  1082           list->elements[i]->accept(*this);
       
  1083 
       
  1084           if (this->current_var_init_symbol != NULL) {
       
  1085             s4o.print(" = ");
       
  1086             s4o.print("__ext_element_c<");
       
  1087             this->current_var_type_symbol->accept(*this);
       
  1088             s4o.print(">(");
       
  1089             this->current_var_init_symbol->accept(*this);
       
  1090             s4o.print(")");
       
  1091 	  }
       
  1092           s4o.print(";\n");
       
  1093 #endif
       
  1094         }
       
  1095       }
       
  1096       break;
       
  1097 
       
  1098     default:
       
  1099       ERROR; /* not supported, and not needed either... */
       
  1100   }
       
  1101 
       
  1102   return NULL;
       
  1103 }
       
  1104 
       
  1105 
       
  1106 #if 0
       
  1107 /*  var1_list ':' single_byte_string_spec */
       
  1108 SYM_REF2(single_byte_string_var_declaration_c, var1_list, single_byte_string_spec)
       
  1109 
       
  1110 /*  STRING ['[' integer ']'] [ASSIGN single_byte_character_string] */
       
  1111 /* integer ->may be NULL ! */
       
  1112 /* single_byte_character_string ->may be NULL ! */
       
  1113 SYM_REF2(single_byte_string_spec_c, integer, single_byte_character_string)
       
  1114 
       
  1115 /*  var1_list ':' double_byte_string_spec */
       
  1116 SYM_REF2(double_byte_string_var_declaration_c, var1_list, double_byte_string_spec)
       
  1117 
       
  1118 /*  WSTRING ['[' integer ']'] [ASSIGN double_byte_character_string] */
       
  1119 /* integer ->may be NULL ! */
       
  1120 /* double_byte_character_string ->may be NULL ! */
       
  1121 SYM_REF2(double_byte_string_spec_c, integer, double_byte_character_string)
       
  1122 
       
  1123 /*| VAR [RETAIN|NON_RETAIN] incompl_located_var_decl_list END_VAR */
       
  1124 /* option ->may be NULL ! */
       
  1125 SYM_REF2(incompl_located_var_declarations_c, option, incompl_located_var_decl_list)
       
  1126 
       
  1127 /* helper symbol for incompl_located_var_declarations */
       
  1128 /*| incompl_located_var_decl_list incompl_located_var_decl ';' */
       
  1129 SYM_LIST(incompl_located_var_decl_list_c)
       
  1130 
       
  1131 /*  variable_name incompl_location ':' var_spec */
       
  1132 SYM_REF4(incompl_located_var_decl_c, variable_name, incompl_location, var_spec, unused)
       
  1133 
       
  1134 /*  AT incompl_location_token */
       
  1135 SYM_TOKEN(incompl_location_c)
       
  1136 #endif
       
  1137 
       
  1138 
       
  1139 void *visit(falling_edge_option_c *symbol) {
       
  1140   // TO DO ...
       
  1141   s4o.print("F_EDGE");
       
  1142   return NULL;
       
  1143 }
       
  1144 
       
  1145 
       
  1146 void *visit(var1_init_decl_c *symbol) {
       
  1147   TRACE("var1_init_decl_c");
       
  1148   /* We have several implementation alternatives here...
       
  1149    *
       
  1150    * The issue is that generation of c code
       
  1151    * based on the abstract syntax tree requires the reversal
       
  1152    * of the symbol order compared to st. For e.g.:
       
  1153    * (ST) a, b, c: INT := 98;
       
  1154    * (C ) int a=98, b=98, c=98;
       
  1155    * The spec_init contains the references to 'INT' and '98'.
       
  1156    * The var1_list contains the references to 'a', 'b', and 'c'.
       
  1157    * The var1_init_decl_c contains the references to spec_init and var1_list.
       
  1158    *
       
  1159    * For c code generation, the var1_init_decl_c visitor
       
  1160    * would need to access the internals of other classes
       
  1161    * (e.g. the simple_spec_init_c).
       
  1162    *
       
  1163    * We can do this using one of three methods:
       
  1164    *   1) Create the abstract syntax tree differently;
       
  1165    *   2) Access other classes from within the var1_init_decl_c;
       
  1166    *   3) Pass info between the visitors using global variables
       
  1167    *       only acessible by this class (private vars)
       
  1168    *
       
  1169    * In 1), the abstract syntax tree would be built so that
       
  1170    * var1_init_decl_c would contain direct references to
       
  1171    * var1_list_c, to the var type 'INT' and to the initialiser '98'
       
  1172    * (as per the example above).
       
  1173    *
       
  1174    * 2) would have several sub-options to obtain the references
       
  1175    * to 'INT' and '98' from within var1_init_decl_c.
       
  1176    *  In 2a), the var1_init_decl_c would use dynamic casts to determine
       
  1177    * the class of spec_init (simple_spec_init_c, subrange_spec_init_c or
       
  1178    * enumerated_spec_init_c), and from there obtain the 'INT' and '98'
       
  1179    *  In 2b) var1_init_decl_c would have one reference for each
       
  1180    * simple_spec_init_c, subrange_spec_init_c and enumerated_spec_init_c,
       
  1181    * the apropriate one being initialised by the var1_init_decl_c constructor.
       
  1182    * Note that the constructor would use dynamic casts. In essence, we
       
  1183    * would merely be changing the location of the code with the
       
  1184    * dynamic casts.
       
  1185    *  In 2c) we would use three overloaded constructors for var1_init_decl_c
       
  1186    * one each for simple_spec_init_c, etc... This implies
       
  1187    * use type specific pointers to each symbol in the bison
       
  1188    * parser, instead of simply using a symbol_c * for all symbols;
       
  1189    *
       
  1190    * In 3), we use two global but private variables with references to
       
  1191    * 'INT' and '98', that would be initiliased by the visitors to
       
  1192    * simple_spec_init_c, subrange_spec_init_c and enumerated_spec_init_c.
       
  1193    * The visitor to var1_list_c would then use the references in the global
       
  1194    * variables.
       
  1195    *
       
  1196    * I (Mario) have chosen to use 3).
       
  1197    */
       
  1198 
       
  1199   /* Start off by setting the current_var_type_symbol and
       
  1200    * current_var_init_symbol private variables...
       
  1201    */
       
  1202   update_type_init(symbol->spec_init);
       
  1203 
       
  1204   /* now to produce the c equivalent... */
       
  1205   symbol->var1_list->accept(*this);
       
  1206 
       
  1207   /* Values no longer in scope, and therefore no longer used.
       
  1208    * Make an effort to keep them set to NULL when not in use
       
  1209    * in order to catch bugs as soon as possible...
       
  1210    */
       
  1211   void_type_init();
       
  1212 
       
  1213   return NULL;
       
  1214 }
       
  1215 
       
  1216 
       
  1217 
       
  1218 void *visit(var1_list_c *symbol) {
       
  1219   TRACE("var1_list_c");
       
  1220   declare_variables(symbol);
       
  1221   return NULL;
       
  1222 }
       
  1223 
       
  1224 
       
  1225 
       
  1226 
       
  1227 /* intermediate helper symbol for:
       
  1228  *  - non_retentive_var_decls
       
  1229  *  - output_declarations
       
  1230  */
       
  1231 void *visit(var_init_decl_list_c *symbol) {
       
  1232   TRACE("var_init_decl_list_c");
       
  1233   return print_list(symbol);
       
  1234   return NULL;
       
  1235 }
       
  1236 
       
  1237 
       
  1238 /**************************************/
       
  1239 /* B.1.5 - Program organization units */
       
  1240 /**************************************/
       
  1241 /***********************/
       
  1242 /* B 1.5.1 - Functions */
       
  1243 /***********************/
       
  1244 /* The missing function_declaration_c
       
  1245  * is handled in derived classes
       
  1246  */
       
  1247 
       
  1248 
       
  1249 
       
  1250 /* intermediate helper symbol for function_declaration */
       
  1251 void *visit(var_declarations_list_c *symbol) {
       
  1252   TRACE("var_declarations_list_c");
       
  1253   return print_list(symbol);
       
  1254 }
       
  1255 
       
  1256 
       
  1257 void *visit(function_var_decls_c *symbol) {
       
  1258   TRACE("function_var_decls_c");
       
  1259 
       
  1260   if ((wanted_vartype & private_vt) != 0) {
       
  1261 /*
       
  1262     // TO DO ...
       
  1263     if (symbol->option != NULL)
       
  1264       symbol->option->accept(*this);
       
  1265 */
       
  1266     current_vartype = private_vt;
       
  1267     symbol->decl_list->accept(*this);
       
  1268     current_vartype = none_vt;
       
  1269   }
       
  1270   return NULL;
       
  1271 }
       
  1272 
       
  1273 
       
  1274 /* intermediate helper symbol for function_var_decls */
       
  1275 void *visit(var2_init_decl_list_c *symbol) {
       
  1276   TRACE("var2_init_decl_list_c");
       
  1277   print_list(symbol);
       
  1278   return NULL;
       
  1279 }
       
  1280 
       
  1281 
       
  1282 /*****************************/
       
  1283 /* B 1.5.2 - Function Blocks */
       
  1284 /*****************************/
       
  1285 
       
  1286 /* The missing function_block_declaration_c
       
  1287  * is handled in derived classes
       
  1288  */
       
  1289 
       
  1290 
       
  1291 /*  VAR_TEMP temp_var_decl_list END_VAR */
       
  1292 void *visit(temp_var_decls_c *symbol) {
       
  1293   TRACE("temp_var_decls_c");
       
  1294   if ((wanted_vartype & temp_vt) != 0) {
       
  1295     current_vartype = temp_vt;
       
  1296     symbol->var_decl_list->accept(*this);
       
  1297     current_vartype = none_vt;
       
  1298   }
       
  1299   return NULL;
       
  1300 }
       
  1301 
       
  1302 /* intermediate helper symbol for temp_var_decls */
       
  1303 void *visit(temp_var_decls_list_c *symbol) {
       
  1304   TRACE("temp_var_decls_list_c");
       
  1305   return print_list(symbol);
       
  1306 }
       
  1307 
       
  1308 /*  VAR NON_RETAIN var_init_decl_list END_VAR */
       
  1309 void *visit(non_retentive_var_decls_c *symbol) {
       
  1310   TRACE("non_retentive_var_decls_c");
       
  1311   // TODO ... guarantee the non-retain semantics!
       
  1312   if ((wanted_vartype & private_vt) != 0) {
       
  1313     current_vartype = private_vt;
       
  1314     symbol->var_decl_list->accept(*this);
       
  1315     current_vartype = none_vt;
       
  1316   }
       
  1317   return NULL;
       
  1318 }
       
  1319 
       
  1320 
       
  1321 
       
  1322 /**********************/
       
  1323 /* B 1.5.3 - Programs */
       
  1324 /**********************/
       
  1325   /* leave for derived classes... */
       
  1326 
       
  1327 /*********************************************/
       
  1328 /* B.1.6  Sequential function chart elements */
       
  1329 /*********************************************/
       
  1330 
       
  1331 /********************************/
       
  1332 /* B 1.7 Configuration elements */
       
  1333 /********************************/
       
  1334   /* Programs instantiated inside configurations are declared as variables!! */
       
  1335 
       
  1336 /*
       
  1337 CONFIGURATION configuration_name
       
  1338    optional_global_var_declarations
       
  1339    (resource_declaration_list | single_resource_declaration)
       
  1340    optional_access_declarations
       
  1341    optional_instance_specific_initializations
       
  1342 END_CONFIGURATION
       
  1343 */
       
  1344 /*
       
  1345 SYM_REF6(configuration_declaration_c, configuration_name, global_var_declarations, resource_declarations, access_declarations, instance_specific_initializations, unused)
       
  1346 */
       
  1347 void *visit(configuration_declaration_c *symbol) {
       
  1348   TRACE("configuration_declaration_c");
       
  1349 
       
  1350   if(symbol->global_var_declarations)
       
  1351     symbol->global_var_declarations->accept(*this); // will contain VAR_GLOBAL declarations!!
       
  1352   symbol->resource_declarations->accept(*this);   // will contain PROGRAM declarations!!
       
  1353   return NULL;
       
  1354 }
       
  1355 
       
  1356 
       
  1357 /* helper symbol for configuration_declaration */
       
  1358 // SYM_LIST(resource_declaration_list_c)
       
  1359 void *visit(resource_declaration_list_c *symbol) {
       
  1360   TRACE("resource_declaration_list_c");
       
  1361 
       
  1362   return print_list(symbol);
       
  1363 }
       
  1364 
       
  1365 /*
       
  1366 RESOURCE resource_name ON resource_type_name
       
  1367    optional_global_var_declarations
       
  1368    single_resource_declaration
       
  1369 END_RESOURCE
       
  1370 */
       
  1371 // SYM_REF4(resource_declaration_c, resource_name, resource_type_name, global_var_declarations, resource_declaration)
       
  1372 void *visit(resource_declaration_c *symbol) {
       
  1373   TRACE("resource_declaration_c");
       
  1374 
       
  1375   if ((wanted_vartype & resource_vt) != 0) {
       
  1376     s4o.print(s4o.indent_spaces + "struct {\n");
       
  1377     s4o.indent_right();
       
  1378 
       
  1379     current_vartype = resource_vt;
       
  1380     if (NULL != symbol->global_var_declarations)
       
  1381       symbol->global_var_declarations->accept(*this); // will contain VAR_GLOBAL declarations!!
       
  1382     if (NULL != symbol->resource_declaration)
       
  1383       symbol->resource_declaration->accept(*this);    // will contain PROGRAM declarations!!
       
  1384     current_vartype = none_vt;
       
  1385 
       
  1386     s4o.indent_left();
       
  1387     s4o.print(s4o.indent_spaces + "} ");
       
  1388     symbol->resource_name->accept(*this);
       
  1389     s4o.print(";\n");
       
  1390   }
       
  1391   return NULL;
       
  1392 }
       
  1393 
       
  1394 /* task_configuration_list program_configuration_list */
       
  1395 // SYM_REF2(single_resource_declaration_c, task_configuration_list, program_configuration_list)
       
  1396 void *visit(single_resource_declaration_c *symbol) {
       
  1397   TRACE("single_resource_declaration_c");
       
  1398 
       
  1399   if ((wanted_vartype & program_vt) != 0) {
       
  1400     unsigned int previous_vartype = current_vartype;
       
  1401       // previous_vartype will be resource_vt
       
  1402     current_vartype = program_vt;
       
  1403     symbol->program_configuration_list->accept(*this);
       
  1404     current_vartype = previous_vartype;
       
  1405   }
       
  1406   return NULL;
       
  1407 }
       
  1408 
       
  1409 
       
  1410 /* helper symbol for single_resource_declaration */
       
  1411 // SYM_LIST(task_configuration_list_c)
       
  1412 
       
  1413 
       
  1414 /* helper symbol for single_resource_declaration */
       
  1415 /* | program_configuration_list program_configuration ';' */
       
  1416 // SYM_LIST(program_configuration_list_c)
       
  1417 void *visit(program_configuration_list_c *symbol) {
       
  1418   TRACE("program_configuration_list_c");
       
  1419 
       
  1420   return print_list(symbol);
       
  1421 }
       
  1422 
       
  1423 /* helper symbol for
       
  1424  *  - access_path
       
  1425  *  - instance_specific_init
       
  1426  */
       
  1427 // SYM_LIST(any_fb_name_list_c)
       
  1428 
       
  1429 /*  [resource_name '.'] global_var_name ['.' structure_element_name] */
       
  1430 // SYM_REF4(global_var_reference_c, resource_name, global_var_name, structure_element_name, unused)
       
  1431 
       
  1432 /*  prev_declared_program_name '.' symbolic_variable */
       
  1433 // SYM_REF2(program_output_reference_c, program_name, symbolic_variable)
       
  1434 
       
  1435 /*  TASK task_name task_initialization */
       
  1436 // SYM_REF2(task_configuration_c, task_name, task_initialization)
       
  1437 
       
  1438 /*  '(' [SINGLE ASSIGN data_source ','] [INTERVAL ASSIGN data_source ','] PRIORITY ASSIGN integer ')' */
       
  1439 // SYM_REF4(task_initialization_c, single_data_source, interval_data_source, priority_data_source, unused)
       
  1440 
       
  1441 /*  PROGRAM [RETAIN | NON_RETAIN] program_name [WITH task_name] ':' program_type_name ['(' prog_conf_elements ')'] */
       
  1442 // SYM_REF6(program_configuration_c, retain_option, program_name, task_name, program_type_name, prog_conf_elements, unused)
       
  1443 private:
       
  1444   /* a helper function to the program_configuration_c visitor... */
       
  1445   void program_constructor_call(program_configuration_c *symbol) {
       
  1446   program_declaration_c *p_decl = program_type_symtable.find_value(symbol->program_type_name);
       
  1447 
       
  1448   if (p_decl == program_type_symtable.end_value())
       
  1449     /* should never occur. The program being called MUST be in the symtable... */
       
  1450     ERROR;
       
  1451 
       
  1452   symbol->program_name->accept(*this);
       
  1453   s4o.print("(");
       
  1454 
       
  1455   /* loop through each function parameter, find the value we should pass
       
  1456    * to it, and then output the c equivalent...
       
  1457    */
       
  1458   function_param_iterator_c fp_iterator(p_decl);
       
  1459   function_call_param_iterator_c function_call_param_iterator(symbol);
       
  1460   identifier_c *param_name;
       
  1461   nv->push("", ", ");
       
  1462   for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) {
       
  1463 
       
  1464     symbol_c *param_type = fp_iterator.param_type();
       
  1465     if (param_type == NULL) ERROR;
       
  1466 
       
  1467     function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
       
  1468 
       
  1469 #if 0
       
  1470     /* Get the value from a foo(<param_name> = <param_value>) style call */
       
  1471     symbol_c *param_value = function_call_param_iterator.search(param_name);
       
  1472 #endif
       
  1473 
       
  1474     switch (param_direction) {
       
  1475       case function_param_iterator_c::direction_in:
       
  1476       case function_param_iterator_c::direction_out:
       
  1477       case function_param_iterator_c::direction_inout:
       
  1478         /* ignore them all!! */
       
  1479         break;
       
  1480 
       
  1481       case function_param_iterator_c::direction_extref:
       
  1482 #if 0
       
  1483         if (param_value == NULL)
       
  1484 	  /* This is illegal in ST and IL languages.
       
  1485 	   * All variables declared in a VAR_EXTERNAL __must__
       
  1486 	   * be initialised to reference a specific VAR_GLOBAL variable!!
       
  1487 	   *
       
  1488 	   * The semantic checker should have caught this, we check again just the
       
  1489 	   * same (especially since the semantic checker has not yet been written!).
       
  1490 	   */
       
  1491 	  ERROR;
       
  1492         s4o.print(nv->get());
       
  1493         s4o.print("&");
       
  1494         param_value->accept(*this);
       
  1495 #endif
       
  1496 	break;
       
  1497 #if 0
       
  1498         if (param_value == NULL) {
       
  1499 	  /* no parameter value given, so we pass a previously declared temporary variable. */
       
  1500           std::string *temp_var_name = temp_var_name_factory.new_name();
       
  1501           s4o.print(*temp_var_name);
       
  1502           delete temp_var_name;
       
  1503 	} else {
       
  1504           param_value->accept(*this);
       
  1505 	}
       
  1506 #endif
       
  1507 	break;
       
  1508     } /* switch */
       
  1509   } /* for(...) */
       
  1510 
       
  1511   // symbol->parameter_assignment->accept(*this);
       
  1512   s4o.print(")");
       
  1513   nv->pop();
       
  1514   return;
       
  1515 }
       
  1516 
       
  1517 
       
  1518 public:
       
  1519 void *visit(program_configuration_c *symbol) {
       
  1520   TRACE("program_configuration_c");
       
  1521 
       
  1522   /* now to produce the c equivalent... */
       
  1523   switch (wanted_varformat) {
       
  1524     case local_vf:
       
  1525     case localinit_vf:
       
  1526       s4o.print(s4o.indent_spaces);
       
  1527       symbol->program_type_name->accept(*this);
       
  1528       s4o.print(" ");
       
  1529       symbol->program_name->accept(*this);
       
  1530       if (wanted_varformat == localinit_vf) {
       
  1531         // TODO...
       
  1532         // program_call(symbol);
       
  1533       }
       
  1534       s4o.print(";\n");
       
  1535       break;
       
  1536 
       
  1537     case constructorinit_vf:
       
  1538       s4o.print(nv->get());
       
  1539       program_constructor_call(symbol);
       
  1540 /*
       
  1541       symbol->program_name->accept(*this);
       
  1542       s4o.print("(");
       
  1543       symbol->prog_conf_elements->accept(*this);
       
  1544       nv->pop();
       
  1545       s4o.print(")");
       
  1546 */
       
  1547       break;
       
  1548 
       
  1549     default:
       
  1550       ERROR; /* not supported, and not needed either... */
       
  1551   }
       
  1552 
       
  1553   return NULL;
       
  1554 }
       
  1555 
       
  1556 
       
  1557 
       
  1558 /* prog_conf_elements ',' prog_conf_element */
       
  1559 //SYM_LIST(prog_conf_elements_c)
       
  1560 void *visit(prog_conf_elements_c *symbol) {
       
  1561   TRACE("prog_conf_elements_c");
       
  1562 
       
  1563   return print_list(symbol);
       
  1564 }
       
  1565 
       
  1566 /*  fb_name WITH task_name */
       
  1567 //SYM_REF2(fb_task_c, fb_name, task_name)
       
  1568 void *visit(fb_task_c *symbol) {
       
  1569   TRACE("fb_task_c");
       
  1570 
       
  1571   /* TODO...
       
  1572    *
       
  1573    * NOTE: Not yet supported...
       
  1574    *       We do not support allocating specific function blocks declared
       
  1575    *       inside a program to be executed by a different task from the one
       
  1576    *       already executing the program itself.
       
  1577    *       This is mostly because I (Mario) simply do not understand the
       
  1578    *       semantics the standard expects us to implement in this case. It is all
       
  1579    *       very confusing, and very poorly defined in the standard!
       
  1580    */
       
  1581   ERROR;
       
  1582   return NULL;
       
  1583 }
       
  1584 
       
  1585 
       
  1586 
       
  1587 
       
  1588 
       
  1589 
       
  1590 
       
  1591 
       
  1592 
       
  1593 
       
  1594 
       
  1595 
       
  1596 
       
  1597 
       
  1598 
       
  1599 
       
  1600 
       
  1601 
       
  1602 
       
  1603 
       
  1604 
       
  1605 
       
  1606 
       
  1607 
       
  1608 
       
  1609 
       
  1610 
       
  1611 /*  any_symbolic_variable ASSIGN prog_data_source */
       
  1612 // SYM_REF2(prog_cnxn_assign_c, symbolic_variable, prog_data_source)
       
  1613 void *visit(prog_cnxn_assign_c *symbol) {
       
  1614   TRACE("prog_cnxn_assign_c");
       
  1615 
       
  1616   /* TODO... */
       
  1617   return NULL;
       
  1618 }
       
  1619 
       
  1620 /* any_symbolic_variable SENDTO data_sink */
       
  1621 // SYM_REF2(prog_cnxn_sendto_c, symbolic_variable, prog_data_source)
       
  1622 void *visit(prog_cnxn_sendto_c *symbol) {
       
  1623   TRACE("prog_cnxn_sendto_c");
       
  1624 
       
  1625   /* TODO... */
       
  1626   return NULL;
       
  1627 }
       
  1628 
       
  1629 #if 0
       
  1630 /* VAR_CONFIG instance_specific_init_list END_VAR_BOGUS */
       
  1631 SYM_REF2(instance_specific_initializations_c, instance_specific_init_list, unused)
       
  1632 
       
  1633 /* helper symbol for instance_specific_initializations */
       
  1634 SYM_LIST(instance_specific_init_list_c)
       
  1635 
       
  1636 /* resource_name '.' program_name '.' {fb_name '.'}
       
  1637     ((variable_name [location] ':' located_var_spec_init) | (fb_name ':' fb_initialization))
       
  1638 */
       
  1639 SYM_REF6(instance_specific_init_c, resource_name, program_name, any_fb_name_list, variable_name, location, initialization)
       
  1640 
       
  1641 /* helper symbol for instance_specific_init */
       
  1642 /* function_block_type_name ':=' structure_initialization */
       
  1643 SYM_REF2(fb_initialization_c, function_block_type_name, structure_initialization)
       
  1644 
       
  1645 #endif
       
  1646 
       
  1647 
       
  1648 
       
  1649 
       
  1650 /****************************************/
       
  1651 /* B.2 - Language IL (Instruction List) */
       
  1652 /****************************************/
       
  1653 /***********************************/
       
  1654 /* B 2.1 Instructions and Operands */
       
  1655 /***********************************/
       
  1656   /* leave for derived classes... */
       
  1657 
       
  1658 /*******************/
       
  1659 /* B 2.2 Operators */
       
  1660 /*******************/
       
  1661   /* leave for derived classes... */
       
  1662 
       
  1663 
       
  1664 /***************************************/
       
  1665 /* B.3 - Language ST (Structured Text) */
       
  1666 /***************************************/
       
  1667 /***********************/
       
  1668 /* B 3.1 - Expressions */
       
  1669 /***********************/
       
  1670   /* leave for derived classes... */
       
  1671 
       
  1672 /********************/
       
  1673 /* B 3.2 Statements */
       
  1674 /********************/
       
  1675   /* leave for derived classes... */
       
  1676 
       
  1677 /*********************************/
       
  1678 /* B 3.2.1 Assignment Statements */
       
  1679 /*********************************/
       
  1680   /* leave for derived classes... */
       
  1681 
       
  1682 /*****************************************/
       
  1683 /* B 3.2.2 Subprogram Control Statements */
       
  1684 /*****************************************/
       
  1685   /* leave for derived classes... */
       
  1686 
       
  1687 /********************************/
       
  1688 /* B 3.2.3 Selection Statements */
       
  1689 /********************************/
       
  1690   /* leave for derived classes... */
       
  1691 
       
  1692 /********************************/
       
  1693 /* B 3.2.4 Iteration Statements */
       
  1694 /********************************/
       
  1695   /* leave for derived classes... */
       
  1696 
       
  1697 
       
  1698 
       
  1699 }; /* generate_cc_vardecl_c */
       
  1700 
       
  1701 
       
  1702