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