stage3/visit_expression_type.cc
changeset 417 d48f53715f77
parent 416 0c2ef191b22a
child 418 2ac41d2cba91
equal deleted inserted replaced
416:0c2ef191b22a 417:d48f53715f77
     1 /*
       
     2  *  matiec - a compiler for the programming languages defined in IEC 61131-3
       
     3  *
       
     4  *  Copyright (C) 2009-2011  Mario de Sousa (msousa@fe.up.pt)
       
     5  *  Copyright (C) 2007-2011  Laurent Bessard and Edouard Tisserant
       
     6  *
       
     7  *  This program is free software: you can redistribute it and/or modify
       
     8  *  it under the terms of the GNU General Public License as published by
       
     9  *  the Free Software Foundation, either version 3 of the License, or
       
    10  *  (at your option) any later version.
       
    11  *
       
    12  *  This program is distributed in the hope that it will be useful,
       
    13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    15  *  GNU General Public License for more details.
       
    16  *
       
    17  *  You should have received a copy of the GNU General Public License
       
    18  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
       
    19  *
       
    20  *
       
    21  * This code is made available on the understanding that it will not be
       
    22  * used in safety-critical situations without a full and competent review.
       
    23  */
       
    24 
       
    25 /*
       
    26  * An IEC 61131-3 compiler.
       
    27  *
       
    28  * Based on the
       
    29  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
       
    30  *
       
    31  */
       
    32 
       
    33 
       
    34 /* Verify whether the semantic rules of data type compatibility are being followed.
       
    35  *
       
    36  * For example:
       
    37  */
       
    38 
       
    39 #include "visit_expression_type.hh"
       
    40 #include <typeinfo>
       
    41 #include <list>
       
    42 #include <string>
       
    43 #include <string.h>
       
    44 #include <strings.h>
       
    45 
       
    46 
       
    47 #define FIRST_(symbol1, symbol2) (((symbol1)->first_order < (symbol2)->first_order)   ? (symbol1) : (symbol2))
       
    48 #define  LAST_(symbol1, symbol2) (((symbol1)->last_order  > (symbol2)->last_order)    ? (symbol1) : (symbol2))
       
    49 
       
    50 #define STAGE3_ERROR(symbol1, symbol2, ...) {                                          \
       
    51     fprintf(stderr, "%s:%d-%d..%d-%d: error : ",                                       \
       
    52            FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column, \
       
    53                                                 LAST_(symbol1,symbol2) ->last_line,  LAST_(symbol1,symbol2) ->last_column); \
       
    54     fprintf(stderr, __VA_ARGS__);                                                      \
       
    55     fprintf(stderr, "\n");                                                             \
       
    56     il_error = true;                                                                   \
       
    57     error_found = true;                                                                \
       
    58   }
       
    59 
       
    60 
       
    61 /* set to 1 to see debug info during execution */
       
    62 static int debug = 0;
       
    63 
       
    64 
       
    65 void *visit_expression_type_c::visit(program_declaration_c *symbol) {
       
    66   search_varfb_instance_type = new search_varfb_instance_type_c(symbol);
       
    67   symbol->var_declarations->accept(*this);
       
    68   if (debug) printf("checking semantics in body of program %s\n", ((token_c *)(symbol->program_type_name))->value);
       
    69   il_parenthesis_level = 0;
       
    70   il_error = false;
       
    71   il_default_variable_type = NULL;
       
    72   symbol->function_block_body->accept(*this);
       
    73   il_default_variable_type = NULL;
       
    74   delete search_varfb_instance_type;
       
    75   search_varfb_instance_type = NULL;
       
    76   return NULL;
       
    77 }
       
    78 
       
    79 void *visit_expression_type_c::visit(function_declaration_c *symbol) {
       
    80   search_varfb_instance_type = new search_varfb_instance_type_c(symbol);
       
    81   symbol->var_declarations_list->accept(*this);
       
    82   if (debug) printf("checking semantics in body of function %s\n", ((token_c *)(symbol->derived_function_name))->value);
       
    83   il_parenthesis_level = 0;
       
    84   il_error = false;
       
    85   il_default_variable_type = NULL;
       
    86   symbol->function_body->accept(*this);
       
    87   il_default_variable_type = NULL;
       
    88   delete search_varfb_instance_type;
       
    89   search_varfb_instance_type = NULL;
       
    90   return NULL;
       
    91 }
       
    92 
       
    93 void *visit_expression_type_c::visit(function_block_declaration_c *symbol) {
       
    94   search_varfb_instance_type = new search_varfb_instance_type_c(symbol);
       
    95   symbol->var_declarations->accept(*this);
       
    96   if (debug) printf("checking semantics in body of FB %s\n", ((token_c *)(symbol->fblock_name))->value);
       
    97   il_parenthesis_level = 0;
       
    98   il_error = false;
       
    99   il_default_variable_type = NULL;
       
   100   symbol->fblock_body->accept(*this);
       
   101   il_default_variable_type = NULL;
       
   102   delete search_varfb_instance_type;
       
   103   search_varfb_instance_type = NULL;
       
   104   return NULL;
       
   105 }
       
   106 
       
   107 
       
   108 
       
   109 
       
   110 
       
   111 
       
   112 
       
   113 
       
   114 
       
   115 visit_expression_type_c::visit_expression_type_c(symbol_c *ignore) {
       
   116   error_found = false;
       
   117 }
       
   118 
       
   119 visit_expression_type_c::~visit_expression_type_c(void) {
       
   120 }
       
   121 
       
   122 bool visit_expression_type_c::get_error_found(void) {
       
   123   return error_found;
       
   124 }
       
   125 
       
   126 
       
   127 
       
   128 /* NOTE on data type handling and literals...
       
   129  * ==========================================
       
   130  *
       
   131  * Literals that are explicitly type cast 
       
   132  *   e.g.:   BYTE#42
       
   133  *           INT#65
       
   134  *           TIME#45h23m
       
   135  *               etc...
       
   136  *  are NOT considered literals in the following code.
       
   137  *  Since they are type cast, and their data type is fixed and well known,
       
   138  *  they are treated as a variable of that data type (except when determining lvalues)
       
   139  *  In other words, when calling search_constant_type_c on these constants, it returns
       
   140  *  a xxxxx_type_name_c, and not one of the xxxx_literal_c ! 
       
   141  *
       
   142  *  When the following code handles a literal, it is really a literal of unknown data type.
       
   143  *    e.g.   42, may be considered an int, a byte, a word, etc... 
       
   144  *
       
   145  * NOTE: type_symbol == NULL is valid!
       
   146  *       This will occur, for example, when and undefined/undeclared symbolic_variable is used in the program.
       
   147  *       This will not be of any type, so we always return false.
       
   148  */
       
   149 
       
   150 /* A helper function... */
       
   151 bool visit_expression_type_c::is_ANY_ELEMENTARY_type(symbol_c *type_symbol) {
       
   152   if (type_symbol == NULL) {return false;}
       
   153   return is_ANY_MAGNITUDE_type(type_symbol)
       
   154       || is_ANY_BIT_type      (type_symbol)
       
   155       || is_ANY_STRING_type   (type_symbol)
       
   156       || is_ANY_DATE_type     (type_symbol);
       
   157 }
       
   158 
       
   159 /* A helper function... */
       
   160 bool visit_expression_type_c::is_ANY_SAFEELEMENTARY_type(symbol_c *type_symbol) {
       
   161   if (type_symbol == NULL) {return false;}
       
   162   return is_ANY_SAFEMAGNITUDE_type(type_symbol)
       
   163       || is_ANY_SAFEBIT_type      (type_symbol)
       
   164       || is_ANY_SAFESTRING_type   (type_symbol)
       
   165       || is_ANY_SAFEDATE_type     (type_symbol);
       
   166 }
       
   167 
       
   168 /* A helper function... */
       
   169 bool visit_expression_type_c::is_ANY_ELEMENTARY_compatible(symbol_c *type_symbol) {
       
   170   if (type_symbol == NULL) {return false;}
       
   171   /* NOTE: doing 
       
   172    *          return is_ANY_SAFEELEMENTARY_type() || is_ANY_ELEMENTARY_type()
       
   173    *       is incorrect, as the literals would never be considered compatible...
       
   174    */
       
   175   return is_ANY_MAGNITUDE_compatible(type_symbol)
       
   176       || is_ANY_BIT_compatible      (type_symbol)
       
   177       || is_ANY_STRING_compatible   (type_symbol)
       
   178       || is_ANY_DATE_compatible     (type_symbol);
       
   179 }
       
   180 
       
   181 
       
   182 /* A helper function... */
       
   183 bool visit_expression_type_c::is_ANY_MAGNITUDE_type(symbol_c *type_symbol) {
       
   184   if (type_symbol == NULL) {return false;}
       
   185   if (typeid(*type_symbol) == typeid(time_type_name_c)) {return true;}
       
   186   return is_ANY_NUM_type(type_symbol);
       
   187 }
       
   188 
       
   189 /* A helper function... */
       
   190 bool visit_expression_type_c::is_ANY_SAFEMAGNITUDE_type(symbol_c *type_symbol) {
       
   191   if (type_symbol == NULL) {return false;}
       
   192   if (typeid(*type_symbol) == typeid(safetime_type_name_c)) {return true;}
       
   193   return is_ANY_SAFENUM_type(type_symbol);
       
   194 }
       
   195 
       
   196 /* A helper function... */
       
   197 bool visit_expression_type_c::is_ANY_MAGNITUDE_compatible(symbol_c *type_symbol) {
       
   198   if (type_symbol == NULL) {return false;}
       
   199   if (is_ANY_MAGNITUDE_type    (type_symbol))              {return true;}
       
   200   if (is_ANY_SAFEMAGNITUDE_type(type_symbol))              {return true;}
       
   201 
       
   202   return is_ANY_NUM_compatible(type_symbol);
       
   203 }
       
   204 
       
   205 /* A helper function... */
       
   206 bool visit_expression_type_c::is_ANY_NUM_type(symbol_c *type_symbol) {
       
   207   if (type_symbol == NULL) {return false;}
       
   208   if (is_ANY_REAL_type(type_symbol))                       {return true;}
       
   209   if (is_ANY_INT_type(type_symbol))                        {return true;}
       
   210   return false;
       
   211 }
       
   212 
       
   213 /* A helper function... */
       
   214 bool visit_expression_type_c::is_ANY_SAFENUM_type(symbol_c *type_symbol) {
       
   215   if (type_symbol == NULL) {return false;}
       
   216   return is_ANY_SAFEREAL_type(type_symbol) 
       
   217       || is_ANY_SAFEINT_type (type_symbol);
       
   218 }
       
   219 
       
   220 /* A helper function... */
       
   221 bool visit_expression_type_c::is_ANY_NUM_compatible(symbol_c *type_symbol) {
       
   222   if (type_symbol == NULL) {return false;}
       
   223   if (is_ANY_REAL_compatible(type_symbol))                       {return true;}
       
   224   if (is_ANY_INT_compatible(type_symbol))                        {return true;}
       
   225   return false;  
       
   226 }
       
   227 
       
   228 /* A helper function... */
       
   229 bool visit_expression_type_c::is_ANY_DATE_type(symbol_c *type_symbol) {
       
   230   if (type_symbol == NULL) {return false;}
       
   231   if (typeid(*type_symbol) == typeid(date_type_name_c)) {return true;}
       
   232   if (typeid(*type_symbol) == typeid(tod_type_name_c))  {return true;}
       
   233   if (typeid(*type_symbol) == typeid(dt_type_name_c))   {return true;}
       
   234   return false;
       
   235 }
       
   236 
       
   237 /* A helper function... */
       
   238 bool visit_expression_type_c::is_ANY_SAFEDATE_type(symbol_c *type_symbol) {
       
   239   if (type_symbol == NULL) {return false;}
       
   240   if (typeid(*type_symbol) == typeid(safedate_type_name_c)) {return true;}
       
   241   if (typeid(*type_symbol) == typeid(safetod_type_name_c))  {return true;}
       
   242   if (typeid(*type_symbol) == typeid(safedt_type_name_c))   {return true;}
       
   243   return false;
       
   244 }
       
   245 
       
   246 /* A helper function... */
       
   247 bool visit_expression_type_c::is_ANY_DATE_compatible(symbol_c *type_symbol) {
       
   248   if (type_symbol == NULL) {return false;}
       
   249   if (is_ANY_DATE_type    (type_symbol))              {return true;}
       
   250   if (is_ANY_SAFEDATE_type(type_symbol))              {return true;}
       
   251   return false;
       
   252 }
       
   253 
       
   254 /* A helper function... */
       
   255 bool visit_expression_type_c::is_ANY_STRING_type(symbol_c *type_symbol) {
       
   256   if (type_symbol == NULL) {return false;}
       
   257   if (typeid(*type_symbol) == typeid(string_type_name_c)) {return true;}
       
   258   if (typeid(*type_symbol) == typeid(wstring_type_name_c)) {return true;}
       
   259 // TODO literal_string ???
       
   260   return false;
       
   261 }
       
   262 
       
   263 /* A helper function... */
       
   264 bool visit_expression_type_c::is_ANY_SAFESTRING_type(symbol_c *type_symbol) {
       
   265   if (type_symbol == NULL) {return false;}
       
   266   if (typeid(*type_symbol) == typeid(safestring_type_name_c)) {return true;}
       
   267   if (typeid(*type_symbol) == typeid(safewstring_type_name_c)) {return true;}
       
   268   return false;
       
   269 }
       
   270 
       
   271 /* A helper function... */
       
   272 bool visit_expression_type_c::is_ANY_STRING_compatible(symbol_c *type_symbol) {
       
   273   if (type_symbol == NULL) {return false;}
       
   274   if (is_ANY_STRING_type    (type_symbol))              {return true;}
       
   275   if (is_ANY_SAFESTRING_type(type_symbol))              {return true;}
       
   276   return false;
       
   277 }
       
   278 
       
   279 /* A helper function... */
       
   280 bool visit_expression_type_c::is_ANY_INT_type(symbol_c *type_symbol) {
       
   281   if (type_symbol == NULL) {return false;}
       
   282   if (typeid(*type_symbol) == typeid(sint_type_name_c))  {return true;}
       
   283   if (typeid(*type_symbol) == typeid(int_type_name_c))   {return true;}
       
   284   if (typeid(*type_symbol) == typeid(dint_type_name_c))  {return true;}
       
   285   if (typeid(*type_symbol) == typeid(lint_type_name_c))  {return true;}
       
   286   if (typeid(*type_symbol) == typeid(usint_type_name_c)) {return true;}
       
   287   if (typeid(*type_symbol) == typeid(uint_type_name_c))  {return true;}
       
   288   if (typeid(*type_symbol) == typeid(udint_type_name_c)) {return true;}
       
   289   if (typeid(*type_symbol) == typeid(ulint_type_name_c)) {return true;}
       
   290   return false;
       
   291 }
       
   292 
       
   293 /* A helper function... */
       
   294 bool visit_expression_type_c::is_ANY_SAFEINT_type(symbol_c *type_symbol) {
       
   295   if (type_symbol == NULL) {return false;}
       
   296   if (typeid(*type_symbol) == typeid(safesint_type_name_c))  {return true;}
       
   297   if (typeid(*type_symbol) == typeid(safeint_type_name_c))   {return true;}
       
   298   if (typeid(*type_symbol) == typeid(safedint_type_name_c))  {return true;}
       
   299   if (typeid(*type_symbol) == typeid(safelint_type_name_c))  {return true;}
       
   300   if (typeid(*type_symbol) == typeid(safeusint_type_name_c)) {return true;}
       
   301   if (typeid(*type_symbol) == typeid(safeuint_type_name_c))  {return true;}
       
   302   if (typeid(*type_symbol) == typeid(safeudint_type_name_c)) {return true;}
       
   303   if (typeid(*type_symbol) == typeid(safeulint_type_name_c)) {return true;}
       
   304   return false;
       
   305 }
       
   306 
       
   307 /* A helper function... */
       
   308 bool visit_expression_type_c::is_ANY_INT_compatible(symbol_c *type_symbol) {
       
   309   if (type_symbol == NULL) {return false;}
       
   310   if (is_ANY_INT_type    (type_symbol))              {return true;}
       
   311   if (is_ANY_SAFEINT_type(type_symbol))              {return true;}
       
   312   if (is_literal_integer_type(type_symbol))          {return true;}
       
   313   return false;
       
   314 }
       
   315 
       
   316 /* A helper function... */
       
   317 bool visit_expression_type_c::is_ANY_REAL_type(symbol_c *type_symbol) {
       
   318   if (type_symbol == NULL) {return false;}
       
   319   if (typeid(*type_symbol) == typeid(real_type_name_c))  {return true;}
       
   320   if (typeid(*type_symbol) == typeid(lreal_type_name_c)) {return true;}
       
   321   return false;
       
   322 }
       
   323 
       
   324 /* A helper function... */
       
   325 bool visit_expression_type_c::is_ANY_SAFEREAL_type(symbol_c *type_symbol) {
       
   326   if (type_symbol == NULL) {return false;}
       
   327   if (typeid(*type_symbol) == typeid(safereal_type_name_c))  {return true;}
       
   328   if (typeid(*type_symbol) == typeid(safelreal_type_name_c)) {return true;}
       
   329   return false;
       
   330 }
       
   331 
       
   332 /* A helper function... */
       
   333 bool visit_expression_type_c::is_ANY_REAL_compatible(symbol_c *type_symbol) {
       
   334   if (type_symbol == NULL) {return false;}
       
   335   if (is_ANY_REAL_type    (type_symbol))              {return true;}
       
   336   if (is_ANY_SAFEREAL_type(type_symbol))              {return true;}
       
   337   if (is_literal_real_type(type_symbol))              {return true;}
       
   338   return false;
       
   339 }
       
   340 
       
   341 /* A helper function... */
       
   342 bool visit_expression_type_c::is_ANY_BIT_type(symbol_c *type_symbol) {
       
   343   if (type_symbol == NULL) {return false;}
       
   344   if (typeid(*type_symbol) == typeid(bool_type_name_c))     {return true;}
       
   345   if (typeid(*type_symbol) == typeid(byte_type_name_c))     {return true;}
       
   346   if (typeid(*type_symbol) == typeid(word_type_name_c))     {return true;}
       
   347   if (typeid(*type_symbol) == typeid(dword_type_name_c))    {return true;}
       
   348   if (typeid(*type_symbol) == typeid(lword_type_name_c))    {return true;}
       
   349   return false;
       
   350 }
       
   351 
       
   352 /* A helper function... */
       
   353 bool visit_expression_type_c::is_ANY_SAFEBIT_type(symbol_c *type_symbol) {
       
   354   if (type_symbol == NULL) {return false;}
       
   355   if (typeid(*type_symbol) == typeid(safebool_type_name_c))     {return true;}
       
   356   if (typeid(*type_symbol) == typeid(safebyte_type_name_c))     {return true;}
       
   357   if (typeid(*type_symbol) == typeid(safeword_type_name_c))     {return true;}
       
   358   if (typeid(*type_symbol) == typeid(safedword_type_name_c))    {return true;}
       
   359   if (typeid(*type_symbol) == typeid(safelword_type_name_c))    {return true;}
       
   360   return false;
       
   361 }
       
   362 
       
   363 /* A helper function... */
       
   364 bool visit_expression_type_c::is_ANY_BIT_compatible(symbol_c *type_symbol) {
       
   365   if (type_symbol == NULL) {return false;}
       
   366   if (is_ANY_BIT_type    (type_symbol))              {return true;}
       
   367   if (is_ANY_SAFEBIT_type(type_symbol))              {return true;}
       
   368   if (is_nonneg_literal_integer_type(type_symbol))   {return true;}
       
   369   if (is_literal_bool_type(type_symbol))             {return true;}
       
   370   return false;
       
   371 }
       
   372 
       
   373 /* A helper function... */
       
   374 bool visit_expression_type_c::is_BOOL_type(symbol_c *type_symbol) {
       
   375   if (type_symbol == NULL) {return false;}
       
   376   if (typeid(*type_symbol) == typeid(bool_type_name_c))      {return true;}
       
   377   return false;
       
   378 }
       
   379 
       
   380 /* A helper function... */
       
   381 bool visit_expression_type_c::is_SAFEBOOL_type(symbol_c *type_symbol){
       
   382   if (type_symbol == NULL) {return false;}
       
   383   if (typeid(*type_symbol) == typeid(safebool_type_name_c))  {return true;}
       
   384   return false;  
       
   385 }
       
   386 
       
   387 /* A helper function... */
       
   388 bool visit_expression_type_c::is_ANY_BOOL_compatible(symbol_c *type_symbol) {
       
   389   if (type_symbol == NULL) {return false;}
       
   390   if (is_BOOL_type    (type_symbol))              {return true;}
       
   391   if (is_SAFEBOOL_type(type_symbol))              {return true;}
       
   392   if (is_literal_bool_type(type_symbol))              {return true;}
       
   393   return false;
       
   394 }
       
   395 
       
   396 
       
   397 #define is_type(type_name_symbol, type_name_class)  ((type_name_symbol == NULL) ? false : (typeid(*type_name_symbol) == typeid(type_name_class)))
       
   398 
       
   399 
       
   400 #define sizeoftype(symbol) get_sizeof_datatype_c::getsize(symbol)
       
   401 
       
   402 
       
   403 /* A helper function... */
       
   404 bool visit_expression_type_c::is_literal_integer_type(symbol_c *type_symbol) {
       
   405   if (type_symbol == NULL) {return false;}
       
   406   if (typeid(*type_symbol) == typeid(neg_integer_c))        {return true;}
       
   407   return is_nonneg_literal_integer_type(type_symbol);
       
   408 }
       
   409 
       
   410 
       
   411 /* A helper function... */
       
   412 bool visit_expression_type_c::is_nonneg_literal_integer_type(symbol_c *type_symbol) {
       
   413   if (type_symbol == NULL) {return false;}
       
   414   if (typeid(*type_symbol) == typeid(integer_c))        {return true;}
       
   415   if (typeid(*type_symbol) == typeid(binary_integer_c)) {return true;}
       
   416   if (typeid(*type_symbol) == typeid(octal_integer_c))  {return true;}
       
   417   if (typeid(*type_symbol) == typeid(hex_integer_c))    {return true;}
       
   418   return false;
       
   419 }
       
   420 
       
   421 
       
   422 /* A helper function... */
       
   423 bool visit_expression_type_c::is_literal_real_type(symbol_c *type_symbol) {
       
   424   if (type_symbol == NULL) {return false;}
       
   425   if (typeid(*type_symbol) == typeid(real_c))     {return true;}
       
   426   if (typeid(*type_symbol) == typeid(neg_real_c)) {return true;}
       
   427   return false;
       
   428 }
       
   429 
       
   430 
       
   431 /* A helper function... */
       
   432 bool visit_expression_type_c::is_literal_bool_type(symbol_c *type_symbol) {
       
   433   bool_type_name_c bool_t;
       
   434 
       
   435   if (type_symbol == NULL) {return false;}
       
   436   if (typeid(*type_symbol) == typeid(boolean_true_c))    {return true;}
       
   437   if (typeid(*type_symbol) == typeid(boolean_false_c))   {return true;}
       
   438   if (is_nonneg_literal_integer_type(type_symbol))
       
   439     if (sizeoftype(&bool_t) >= sizeoftype(type_symbol))  {return true;}
       
   440   return false;
       
   441 }
       
   442 
       
   443 bool visit_expression_type_c::is_ANY_ELEMENTARY_OR_ENUMERATED_compatible(symbol_c *type_symbol) {
       
   444   if (type_symbol == NULL) {return false;}
       
   445   if (search_base_type.type_is_enumerated(type_symbol)) {return true;}
       
   446   return is_ANY_ELEMENTARY_compatible(type_symbol);
       
   447 }
       
   448 
       
   449 
       
   450 /* Determine the common data type between two data types.
       
   451  * If no common data type found, return NULL.
       
   452  *
       
   453  * If data types are identical, return the first (actually any would do...).
       
   454  * If any of the data types is a literal, we confirm that 
       
   455  *   the literal uses less bits than the fixed size data type.
       
   456  *   e.g. BYTE and 1024 returns NULL
       
   457  *        BYTE and 255  returns BYTE
       
   458  *
       
   459  * If two literals, then return the literal that requires more bits...
       
   460  */
       
   461 
       
   462 symbol_c *visit_expression_type_c::common_type__(symbol_c *first_type, symbol_c *second_type) {
       
   463   if (first_type == NULL && second_type == NULL) {return NULL;}
       
   464   if (first_type == NULL)  {return second_type;}
       
   465   if (second_type == NULL) {return first_type;}
       
   466 
       
   467   if (is_literal_integer_type(first_type) && is_literal_integer_type(second_type))
       
   468     {return ((sizeoftype(first_type) > sizeoftype(second_type))? first_type:second_type);}
       
   469 
       
   470   if (is_literal_real_type(first_type) && is_literal_real_type(second_type))
       
   471     {return ((sizeoftype(first_type) > sizeoftype(second_type))? first_type:second_type);}
       
   472 
       
   473   if (is_literal_bool_type(first_type) && is_literal_bool_type(second_type))
       
   474     {return first_type;}
       
   475 
       
   476   /* The following check can only be made after the is_literal_XXXX checks */
       
   477   /* When two literals of the same type, with identical typeid's are checked,
       
   478    * we must return the one that occupies more bits... This is done above.
       
   479    */
       
   480   if (typeid(*first_type) == typeid(*second_type)) {return first_type;}
       
   481 
       
   482   /* NOTE Although a BOOL is also an ANY_BIT, we must check it explicitly since some
       
   483    *       literal bool values are not literal integers...
       
   484    */ 
       
   485   if (is_BOOL_type(first_type)        && is_literal_bool_type(second_type))    {return first_type;}
       
   486   if (is_BOOL_type(second_type)       && is_literal_bool_type(first_type))     {return second_type;}
       
   487 
       
   488   if (is_SAFEBOOL_type(first_type)    && is_literal_bool_type(second_type))    {return first_type;}
       
   489   if (is_SAFEBOOL_type(second_type)   && is_literal_bool_type(first_type))     {return second_type;}
       
   490 
       
   491   if (is_SAFEBOOL_type(first_type)    && is_BOOL_type(second_type))            {return second_type;}
       
   492   if (is_SAFEBOOL_type(second_type)   && is_BOOL_type(first_type))             {return first_type;}
       
   493 
       
   494   if (is_ANY_BIT_type(first_type)     && is_nonneg_literal_integer_type(second_type))
       
   495     {return ((sizeoftype(first_type)  >= sizeoftype(second_type))? first_type :NULL);}
       
   496   if (is_ANY_BIT_type(second_type)    && is_nonneg_literal_integer_type(first_type))
       
   497     {return ((sizeoftype(second_type) >= sizeoftype(first_type)) ? second_type:NULL);}
       
   498 
       
   499   if (is_ANY_SAFEBIT_type(first_type)     && is_nonneg_literal_integer_type(second_type))
       
   500     {return ((sizeoftype(first_type)  >= sizeoftype(second_type))? first_type :NULL);}
       
   501   if (is_ANY_SAFEBIT_type(second_type)    && is_nonneg_literal_integer_type(first_type))
       
   502     {return ((sizeoftype(second_type) >= sizeoftype(first_type)) ? second_type:NULL);}
       
   503 
       
   504   if  (is_ANY_SAFEBIT_type(first_type)    && is_ANY_BIT_type(second_type))
       
   505     {return ((sizeoftype(first_type) == sizeoftype(second_type))? second_type:NULL);}
       
   506   if  (is_ANY_SAFEBIT_type(second_type)   && is_ANY_BIT_type(first_type))
       
   507     {return ((sizeoftype(first_type) == sizeoftype(second_type))? first_type :NULL);}
       
   508 
       
   509   if (is_ANY_INT_type(first_type)     && is_literal_integer_type(second_type))
       
   510     {return ((sizeoftype(first_type)  >= sizeoftype(second_type))? first_type :NULL);}
       
   511   if (is_ANY_INT_type(second_type)    && is_literal_integer_type(first_type))
       
   512     {return ((sizeoftype(second_type) >= sizeoftype(first_type)) ? second_type:NULL);}
       
   513 
       
   514   if (is_ANY_SAFEINT_type(first_type)     && is_literal_integer_type(second_type))
       
   515     {return ((sizeoftype(first_type)  >= sizeoftype(second_type))? first_type :NULL);}
       
   516   if (is_ANY_SAFEINT_type(second_type)    && is_literal_integer_type(first_type))
       
   517     {return ((sizeoftype(second_type) >= sizeoftype(first_type)) ? second_type:NULL);}
       
   518 
       
   519   if  (is_ANY_SAFEINT_type(first_type)    && is_ANY_INT_type(second_type))
       
   520     {return ((sizeoftype(first_type) == sizeoftype(second_type))? second_type:NULL);}
       
   521   if  (is_ANY_SAFEINT_type(second_type)   && is_ANY_INT_type(first_type))
       
   522     {return ((sizeoftype(first_type) == sizeoftype(second_type))? first_type :NULL);}
       
   523 
       
   524   if (is_ANY_REAL_type(first_type)    && is_literal_real_type(second_type))
       
   525     {return ((sizeoftype(first_type)  >= sizeoftype(second_type))? first_type :NULL);}
       
   526   if (is_ANY_REAL_type(second_type)   && is_literal_real_type(first_type))
       
   527     {return ((sizeoftype(second_type) >= sizeoftype(first_type)) ? second_type:NULL);}
       
   528 
       
   529   if (is_ANY_SAFEREAL_type(first_type)    && is_literal_real_type(second_type))
       
   530     {return ((sizeoftype(first_type)  >= sizeoftype(second_type))? first_type :NULL);}
       
   531   if (is_ANY_SAFEREAL_type(second_type)   && is_literal_real_type(first_type))
       
   532     {return ((sizeoftype(second_type) >= sizeoftype(first_type)) ? second_type:NULL);}
       
   533 
       
   534   if  (is_ANY_SAFEREAL_type(first_type)    && is_ANY_REAL_type(second_type))
       
   535     {return ((sizeoftype(first_type) == sizeoftype(second_type))? second_type:NULL);}
       
   536   if  (is_ANY_SAFEREAL_type(second_type)   && is_ANY_REAL_type(first_type))
       
   537     {return ((sizeoftype(first_type) == sizeoftype(second_type))? first_type :NULL);}
       
   538 
       
   539   /* the Time and Date types... */
       
   540   if (is_type(first_type,  safetime_type_name_c) && is_type(second_type, time_type_name_c))  {return second_type;}
       
   541   if (is_type(second_type, safetime_type_name_c) && is_type( first_type, time_type_name_c))  {return  first_type;}
       
   542 
       
   543   if (is_type(first_type,  safedate_type_name_c) && is_type(second_type, date_type_name_c))  {return second_type;}
       
   544   if (is_type(second_type, safedate_type_name_c) && is_type( first_type, date_type_name_c))  {return  first_type;}
       
   545 
       
   546   if (is_type(first_type,  safedt_type_name_c)   && is_type(second_type, dt_type_name_c))    {return second_type;}
       
   547   if (is_type(second_type, safedt_type_name_c)   && is_type( first_type, dt_type_name_c))    {return  first_type;}
       
   548 
       
   549   if (is_type(first_type,  safetod_type_name_c)  && is_type(second_type, tod_type_name_c))   {return second_type;}
       
   550   if (is_type(second_type, safetod_type_name_c)  && is_type( first_type, tod_type_name_c))   {return  first_type;}
       
   551 
       
   552   /* no common type */
       
   553   return NULL;
       
   554 }
       
   555 
       
   556 /* Determine the common data type between two data types.
       
   557  * Unlike the common_type__() function, we stop the compiler with an ERROR
       
   558  *  if no common data type is found.
       
   559  */
       
   560 symbol_c *visit_expression_type_c::common_type(symbol_c *first_type, symbol_c *second_type) {
       
   561 /*  
       
   562   symbol_c *res = common_type__(first_type, second_type);
       
   563   if (NULL == res) ERROR;
       
   564   return res;
       
   565 */
       
   566   return common_type__(first_type, second_type);
       
   567 }
       
   568 
       
   569 
       
   570 /* Return TRUE if the second (value) data type may be assigned to a variable of the first (variable) data type
       
   571  * such as: 
       
   572  *     var_type     value_type
       
   573  *    BOOL           BYTE#7     -> returns false
       
   574  *    INT            INT#7      -> returns true
       
   575  *    INT            7          -> returns true
       
   576  *    REAL           7.89       -> returns true
       
   577  *    REAL           7          -> returns true
       
   578  *    INT            7.89       -> returns false
       
   579  *    SAFEBOOL       BOOL#1     -> returns false   !!!
       
   580  *   etc...
       
   581  *
       
   582  * NOTE: It is assumed that the var_type is the data type of an lvalue
       
   583  */
       
   584 bool visit_expression_type_c::is_valid_assignment(symbol_c *var_type, symbol_c *value_type) {
       
   585   if (var_type == NULL)   {/* STAGE3_ERROR(value_type, value_type, "Var_type   == NULL"); */ return false;}
       
   586   if (value_type == NULL) {/* STAGE3_ERROR(var_type,   var_type,   "Value_type == NULL"); */ return false;}
       
   587 
       
   588   symbol_c *common_type = common_type__(var_type, value_type);
       
   589   if (NULL == common_type)
       
   590     return false;
       
   591   return (typeid(*var_type) == typeid(*common_type));
       
   592 }
       
   593 
       
   594 
       
   595 /* Return TRUE if there is a common data type, otherwise return FALSE
       
   596  * i.e., return TRUE if both data types may be used simultaneously in an expression
       
   597  * such as:
       
   598  *    BOOL#0     AND BYTE#7  -> returns false
       
   599  *    0          AND BYTE#7  -> returns true
       
   600  *    INT#10     AND INT#7   -> returns true
       
   601  *    INT#10     AND 7       -> returns true
       
   602  *    REAL#34.3  AND 7.89    -> returns true
       
   603  *    REAL#34.3  AND 7       -> returns true
       
   604  *    INT#10     AND 7.89    -> returns false
       
   605  *    SAFEBOOL#0 AND BOOL#1  -> returns true   !!!
       
   606  *   etc...
       
   607  */
       
   608 bool visit_expression_type_c::is_compatible_type(symbol_c *first_type, symbol_c *second_type) {
       
   609   if (first_type == NULL || second_type == NULL) {return false;}
       
   610   return (NULL != common_type__(first_type, second_type));
       
   611 }
       
   612 
       
   613 
       
   614 
       
   615 
       
   616 /* A helper function... */
       
   617 /*
       
   618 symbol_c *visit_expression_type_c::compute_boolean_expression(symbol_c *left_type, symbol_c *right_type,
       
   619                                                               is_data_type_t is_data_type) {
       
   620 */
       
   621 symbol_c *visit_expression_type_c::compute_expression(symbol_c *left_type,      symbol_c *right_type,     is_data_type_t is_data_type,
       
   622 						      symbol_c *left_expr, symbol_c *right_expr) {
       
   623   bool error = false;
       
   624 
       
   625   if (!(this->*is_data_type)(left_type)) {
       
   626     if (debug) printf("visit_expression_type_c::compute_expression(): invalid left_type\n");
       
   627     if (left_expr != NULL)
       
   628       STAGE3_ERROR(left_expr, left_expr, "Invalid data type of operand, or of data resulting from previous IL instructions.");
       
   629     error = true;
       
   630   }
       
   631   if (!(this->*is_data_type)(right_type)) {
       
   632     if (debug) printf("visit_expression_type_c::compute_expression(): invalid right_type\n");
       
   633     if (right_expr != NULL)
       
   634       STAGE3_ERROR(right_expr, right_expr, "Invalid data type of operand.");
       
   635     error = true;
       
   636   }
       
   637   if (!is_compatible_type(left_type, right_type)) {
       
   638     if (debug) printf("visit_expression_type_c::compute_expression(): left_type & right_type are incompatible\n");
       
   639     if ((left_expr != NULL) && (right_expr != NULL))
       
   640       STAGE3_ERROR(left_expr, right_expr, "Type mismatch between operands.");
       
   641     error = true;
       
   642   }
       
   643 
       
   644   if (error)
       
   645     return NULL;
       
   646   else
       
   647     return common_type(left_type, right_type);
       
   648 }
       
   649 
       
   650 
       
   651 
       
   652 
       
   653 /* A helper function... */
       
   654 /* check the semantics of a FB or Function non-formal call */
       
   655 /* e.g. foo(1, 2, 3, 4);  */
       
   656 /* If error_count pointer is != NULL, we do not really print out the errors,
       
   657  * but rather only count how many errors were found.
       
   658  * This is used to support overloaded functions, where we have to check each possible
       
   659  * function, one at a time, untill we find a function call without any errors.
       
   660  */
       
   661 void visit_expression_type_c::check_nonformal_call(symbol_c *f_call, symbol_c *f_decl, bool use_il_defvar, int *error_count) {
       
   662   symbol_c *call_param_value, *call_param_type, *param_type;
       
   663   identifier_c *param_name;
       
   664   function_param_iterator_c       fp_iterator(f_decl);
       
   665   function_call_param_iterator_c fcp_iterator(f_call);
       
   666   int extensible_parameter_highest_index = -1;
       
   667   
       
   668   /* reset error counter */
       
   669   if (error_count != NULL) *error_count = 0;
       
   670   /* if use_il_defvar, then the first parameter for the call comes from the il_default_variable */
       
   671   if (use_il_defvar) {
       
   672     /* The first parameter of the function corresponds to the il_default_variable_type of the function call */
       
   673     do {
       
   674       param_name = fp_iterator.next();
       
   675       if(param_name == NULL) break;
       
   676       /*  The EN and ENO parameters are default parameters.
       
   677        *  In the non-formal invocation of a function there can be no assignment of
       
   678        * values to these parameters. Therefore, we ignore the parameters declared
       
   679        * in the function.
       
   680        */
       
   681     } while ((strcmp(param_name->value, "EN") == 0) || (strcmp(param_name->value, "ENO") == 0));
       
   682     /* If the function does not have any parameters (param_name == NULL)
       
   683      * then we cannot compare its type with the il_default_variable_type.
       
   684      *
       
   685      * However, I (Mario) think this is invalid syntax, as it seems to me all functions must
       
   686      * have at least one parameter.
       
   687      * However, we will make this semantic verification consider it possible, as later
       
   688      * versions of the standard may change that syntax.
       
   689      * So, instead of generating a syntax error message, we simply check whether the call
       
   690      * is passing any more parameters besides the default variable (the il default variable may be ignored
       
   691      * in this case, and not consider it as being a parameter being passed to the function).
       
   692      * If it does, then we have found a semantic error, otherwise the function call is 
       
   693      * correct, and we simply return.
       
   694      */
       
   695     if(param_name == NULL) {
       
   696       if (fcp_iterator.next_nf() != NULL)
       
   697         STAGE3_ERROR(f_call, f_call, "Too many parameters in function/FB call.");
       
   698       return;
       
   699     } else { 
       
   700       /* param_name != NULL */
       
   701       param_type = fp_iterator.param_type();
       
   702       if(!is_valid_assignment(param_type, il_default_variable_type)) {
       
   703         if (error_count != NULL) (*error_count)++;
       
   704         else STAGE3_ERROR(f_call, f_call, "In function/FB call, first parameter has invalid data type.");
       
   705       }
       
   706     }
       
   707     
       
   708     /* the fisrt parameter (il_def_variable) is correct */
       
   709     if (extensible_parameter_highest_index < fp_iterator.extensible_param_index()) {
       
   710       extensible_parameter_highest_index = fp_iterator.extensible_param_index();
       
   711     }
       
   712   } // if (use_il_defvar)
       
   713   
       
   714   
       
   715 
       
   716   /* Iterating through the non-formal parameters of the function call */
       
   717   while((call_param_value = fcp_iterator.next_nf()) != NULL) {
       
   718     /* Obtaining the type of the value being passed in the function call */
       
   719     call_param_type = base_type((symbol_c*)call_param_value->accept(*this));
       
   720     if (call_param_type == NULL) {
       
   721       if (error_count != NULL) (*error_count)++;
       
   722       /* the following error will usually occur when ST code uses an identifier, that could refer to an enumerated constant,
       
   723        * but was not actually used as a constant in any definitions of an enumerated data type
       
   724        */
       
   725       else STAGE3_ERROR(call_param_value, call_param_value, "Could not determine data type of value being passed in function/FB call.");
       
   726       continue;
       
   727     }  
       
   728     
       
   729     /* Iterate to the next parameter of the function being called.
       
   730      * Get the name of that parameter, and ignore if EN or ENO.
       
   731      */
       
   732     do {
       
   733       param_name = fp_iterator.next();
       
   734       /* If there is no other parameter declared, then we are passing too many parameters... */
       
   735       if(param_name == NULL) {
       
   736         if (error_count != NULL) (*error_count)++;
       
   737         /* Note: We don't want to print out the follwoing error message multiple times, so we return instead of continuing with 'break' */
       
   738         else STAGE3_ERROR(f_call, f_call, "Too many parameters in function/FB call."); return;
       
   739       }
       
   740     } while ((strcmp(param_name->value, "EN") == 0) || (strcmp(param_name->value, "ENO") == 0));
       
   741 
       
   742     /* Get the parameter type */
       
   743     param_type = base_type(fp_iterator.param_type());
       
   744     /* If the declared parameter and the parameter from the function call do not have the same type */
       
   745     if(!is_valid_assignment(param_type, call_param_type)) {
       
   746       if (error_count != NULL) (*error_count)++;
       
   747       else STAGE3_ERROR(call_param_value, call_param_value, "Type mismatch in function/FB call parameter.");
       
   748     }
       
   749 
       
   750     if (extensible_parameter_highest_index < fp_iterator.extensible_param_index()) {
       
   751       extensible_parameter_highest_index = fp_iterator.extensible_param_index();
       
   752     }
       
   753   }
       
   754   
       
   755   /* The function call may not have any errors! */
       
   756   /* In the case of a call to an extensible function, we store the highest index 
       
   757    * of the extensible parameters this particular call uses, in the symbol_c object
       
   758    * of the function call itself!
       
   759    * In calls to non-extensible functions, this value will be set to -1.
       
   760    * This information is later used in stage4 to correctly generate the
       
   761    * output code.
       
   762    */
       
   763   int extensible_param_count = -1;
       
   764   if (extensible_parameter_highest_index >=0) /* if call to extensible function */
       
   765     extensible_param_count = 1 + extensible_parameter_highest_index - fp_iterator.first_extensible_param_index();
       
   766   il_function_call_c     *il_function_call = dynamic_cast<il_function_call_c *>(f_call);
       
   767   function_invocation_c  *function_invocation  = dynamic_cast<function_invocation_c  *>(f_call);
       
   768   if      (il_function_call     != NULL) il_function_call   ->extensible_param_count = extensible_param_count;
       
   769   else if (function_invocation  != NULL) function_invocation->extensible_param_count = extensible_param_count;
       
   770   //   else ERROR;  /* this function is also called by Function Blocks, so this is not an error! */
       
   771 }
       
   772 
       
   773 
       
   774 /* check semantics of FB call in the IL language using input operators */
       
   775 /* e.g. CU, CLK, IN, PT, SR, ...                                       */
       
   776 void visit_expression_type_c::check_il_fbcall(symbol_c *il_operator, const char *il_operator_str) {
       
   777   symbol_c *call_param_type = il_default_variable_type;
       
   778   symbol_c *fb_decl = il_operand_type;
       
   779     /* The following should never occur. The function block must be defined, 
       
   780      * and the FB type being called MUST be in the symtable... 
       
   781      * This was all already checked at stage 2!
       
   782      */
       
   783   if (NULL == fb_decl) ERROR;
       
   784   if (call_param_type == NULL) ERROR;
       
   785 
       
   786   /* We also create an identifier_c object, so we can later use it to find the equivalent FB parameter */
       
   787   /* Note however that this symbol does not have the correct location (file name and line numbers) 
       
   788    * so any error messages must use the il_operator symbol to generate the error location
       
   789    */
       
   790   identifier_c call_param_name(il_operator_str);
       
   791 
       
   792   /* Obtaining the type of the value being passed in the function call */
       
   793   call_param_type = base_type(call_param_type);
       
   794   if (call_param_type == NULL) STAGE3_ERROR(il_operator, il_operator, "Could not determine data type of value being passed in FB call.");
       
   795 
       
   796   /* Find the corresponding parameter of the function being called */
       
   797   function_param_iterator_c fp_iterator(fb_decl);
       
   798   if(fp_iterator.search(&call_param_name) == NULL) {
       
   799     STAGE3_ERROR(il_operand, il_operand, "Called FB does not have an input parameter named %s.", il_operator_str);
       
   800   } else {
       
   801     /* Get the parameter type */
       
   802     symbol_c *param_type = base_type(fp_iterator.param_type());
       
   803     /* If the declared parameter and the parameter from the function call have the same type */
       
   804     if(!is_valid_assignment(param_type, call_param_type)) STAGE3_ERROR(il_operator, il_operator, "Type mismatch in FB call parameter.");
       
   805   }
       
   806 }
       
   807 
       
   808 
       
   809 /* A helper function... */
       
   810 /* check the semantics of a FB or Function formal call */
       
   811 /* e.g. foo(IN1 := 1, OUT1 =>x, EN := true);  */
       
   812 /* If error_count pointer is != NULL, we do not really print out the errors,
       
   813  * but rather only count how many errors were found.
       
   814  * This is used to support overloaded functions, where we have to check each possible
       
   815  * function, one at a time, untill we find a function call without any errors.
       
   816  */
       
   817 void visit_expression_type_c::check_formal_call(symbol_c *f_call, symbol_c *f_decl, int *error_count) {
       
   818   symbol_c *call_param_value, *call_param_type, *call_param_name, *param_type;
       
   819   symbol_c *verify_duplicate_param;
       
   820   identifier_c *param_name;
       
   821   function_param_iterator_c       fp_iterator(f_decl);
       
   822   function_call_param_iterator_c fcp_iterator(f_call);
       
   823   int extensible_parameter_highest_index = -1;
       
   824   identifier_c *extensible_parameter_name;
       
   825 
       
   826   /* reset error counter */
       
   827   if (error_count != NULL) *error_count = 0;
       
   828 
       
   829   /* Iterating through the formal parameters of the function call */
       
   830   while((call_param_name = fcp_iterator.next_f()) != NULL) {
       
   831         
       
   832     /* Obtaining the value being passed in the function call */
       
   833     call_param_value = fcp_iterator.get_current_value();
       
   834     /* the following should never occur. If it does, then we have a bug in our code... */
       
   835     if (NULL == call_param_value) ERROR;
       
   836 
       
   837     /* Checking if there are duplicated parameter values */
       
   838     verify_duplicate_param = fcp_iterator.search_f(call_param_name);
       
   839     if(verify_duplicate_param != call_param_value){
       
   840       if (error_count != NULL) (*error_count)++;
       
   841       else STAGE3_ERROR(call_param_name, verify_duplicate_param, "Duplicated parameter values.");
       
   842     }   
       
   843 
       
   844     /* Obtaining the type of the value being passed in the function call */
       
   845     call_param_type = (symbol_c*)call_param_value->accept(*this);
       
   846     if (call_param_type == NULL) {
       
   847       if (error_count != NULL) (*error_count)++;
       
   848       else STAGE3_ERROR(call_param_name, call_param_value, "Could not determine data type of value being passed in function/FB call.");
       
   849       /* The data value being passed is possibly any enumerated type value.
       
   850        * We do not yet handle semantic verification of enumerated types.
       
   851        */
       
   852       ERROR;
       
   853     }
       
   854     call_param_type = base_type(call_param_type);
       
   855     if (call_param_type == NULL) STAGE3_ERROR(call_param_name, call_param_value, "Could not determine data type of value being passed in function/FB call.");
       
   856 
       
   857     /* Find the corresponding parameter of the function being called */
       
   858     param_name = fp_iterator.search(call_param_name);
       
   859     if(param_name == NULL) {
       
   860       if (error_count != NULL) (*error_count)++;
       
   861       else STAGE3_ERROR(call_param_name, call_param_name, "Invalid parameter in function/FB call.");
       
   862     } else {
       
   863       /* Get the parameter type */
       
   864       param_type = base_type(fp_iterator.param_type());
       
   865       /* If the declared parameter and the parameter from the function call have the same type */
       
   866       if(!is_valid_assignment(param_type, call_param_type)) {
       
   867         if (error_count != NULL) (*error_count)++;
       
   868         else STAGE3_ERROR(call_param_name, call_param_value, "Type mismatch function/FB call parameter.");
       
   869       }
       
   870       if (extensible_parameter_highest_index < fp_iterator.extensible_param_index()) {
       
   871         extensible_parameter_highest_index = fp_iterator.extensible_param_index();
       
   872         extensible_parameter_name = param_name;
       
   873       }
       
   874     }
       
   875   }
       
   876   
       
   877   /* In the case of a call to an extensible function, we store the highest index 
       
   878    * of the extensible parameters this particular call uses, in the symbol_c object
       
   879    * of the function call itself!
       
   880    * In calls to non-extensible functions, this value will be set to -1.
       
   881    * This information is later used in stage4 to correctly generate the
       
   882    * output code.
       
   883    */
       
   884   int extensible_param_count = -1;
       
   885   if (extensible_parameter_highest_index >=0) /* if call to extensible function */
       
   886     extensible_param_count = 1 + extensible_parameter_highest_index - fp_iterator.first_extensible_param_index();
       
   887   il_formal_funct_call_c *il_formal_funct_call = dynamic_cast<il_formal_funct_call_c *>(f_call);
       
   888   function_invocation_c  *function_invocation  = dynamic_cast<function_invocation_c  *>(f_call);
       
   889   if      (il_formal_funct_call != NULL) il_formal_funct_call->extensible_param_count = extensible_param_count;
       
   890   else if (function_invocation  != NULL) function_invocation->extensible_param_count  = extensible_param_count;
       
   891 //   else ERROR;  /* this function is also called by Function Blocks, so this is not an error! */
       
   892 
       
   893   /* We have iterated through all the formal parameters of the function call,
       
   894    * and everything seems fine. 
       
   895    * If the function being called in an extensible function, we now check
       
   896    * whether the extensible paramters in the formal invocation do not skip
       
   897    * any indexes...
       
   898    *
       
   899    * f(in1:=0, in2:=0, in4:=0) --> ERROR!!
       
   900    */
       
   901   if (extensible_parameter_highest_index >=0) { /* if call to extensible function */
       
   902     for (int i=fp_iterator.first_extensible_param_index(); i < extensible_parameter_highest_index; i++) {
       
   903       char tmp[256];
       
   904       if (snprintf(tmp, 256, "%s%d", extensible_parameter_name->value, i) >= 256) ERROR;
       
   905       if (fcp_iterator.search_f(tmp) == NULL) {
       
   906         /* error in invocation of extensible function */
       
   907         if (error_count != NULL) (*error_count)++;
       
   908         else STAGE3_ERROR(f_call, f_call, "Missing extensible parameters in call to extensible function.");
       
   909       }  
       
   910     }    
       
   911   }  
       
   912 }
       
   913 
       
   914 
       
   915 
       
   916 
       
   917 /* a helper function... */
       
   918 symbol_c *visit_expression_type_c::base_type(symbol_c *symbol) {
       
   919   /* NOTE: symbol == NULL is valid. It will occur when, for e.g., an undefined/undeclared symbolic_variable is used
       
   920    *       in the code.
       
   921    */
       
   922   if (symbol == NULL) return NULL;
       
   923   return (symbol_c *)symbol->accept(search_base_type);
       
   924 }
       
   925 
       
   926 
       
   927 /* a helper function... */
       
   928 void *visit_expression_type_c::verify_null(symbol_c *symbol){
       
   929   if(il_default_variable_type == NULL){
       
   930     STAGE3_ERROR(symbol, symbol, "Missing LD instruction (or equivalent) before this instruction.");
       
   931   }
       
   932   if(il_operand_type == NULL){
       
   933     STAGE3_ERROR(symbol, symbol, "This instruction requires an operand.");
       
   934   }
       
   935   return NULL;
       
   936 }
       
   937 
       
   938 
       
   939 /********************************/
       
   940 /* B 1.3.3 - Derived data types */
       
   941 /********************************/
       
   942 void *visit_expression_type_c::visit(data_type_declaration_c *symbol) {
       
   943   // TODO !!!
       
   944   /* for the moment we must return NULL so semantic analysis of remaining code is not interrupted! */
       
   945   return NULL;
       
   946 }
       
   947 
       
   948 
       
   949 /*********************/
       
   950 /* B 1.4 - Variables */
       
   951 /*********************/
       
   952 
       
   953 void *visit_expression_type_c::visit(symbolic_variable_c *symbol) {
       
   954   return search_varfb_instance_type->get_basetype_decl(symbol);
       
   955 }
       
   956 
       
   957 /********************************************/
       
   958 /* B 1.4.1 - Directly Represented Variables */
       
   959 /********************************************/
       
   960 void *visit_expression_type_c::visit(direct_variable_c *symbol) {
       
   961   switch (symbol->value[2]) {
       
   962     case 'X': // bit - 1 bit
       
   963       return (void *)&bool_type_name;
       
   964     case 'B': // byte - 8 bits
       
   965       return (void *)&byte_type_name;
       
   966     case 'W': // word - 16 bits
       
   967       return (void *)&word_type_name;
       
   968     case 'D': // double word - 32 bits
       
   969       return (void *)&dword_type_name;
       
   970     case 'L': // long word - 64 bits
       
   971       return (void *)&lword_type_name;
       
   972     default:  // if none of the above, then the empty string was used <=> boolean 
       
   973       return (void *)&bool_type_name;
       
   974    }
       
   975 }
       
   976 
       
   977 /*************************************/
       
   978 /* B 1.4.2 - Multi-element variables */
       
   979 /*************************************/
       
   980 void *visit_expression_type_c::visit(array_variable_c *symbol) {
       
   981   return search_varfb_instance_type->get_basetype_decl(symbol);
       
   982 }
       
   983 
       
   984 void *visit_expression_type_c::visit(structured_variable_c *symbol) {
       
   985   return search_varfb_instance_type->get_basetype_decl(symbol);
       
   986 }
       
   987 
       
   988 
       
   989 
       
   990 /********************************/
       
   991 /* B 1.7 Configuration elements */
       
   992 /********************************/
       
   993 void *visit_expression_type_c::visit(configuration_declaration_c *symbol) {
       
   994   // TODO !!!
       
   995   /* for the moment we must return NULL so semantic analysis of remaining code is not interrupted! */
       
   996   return NULL;
       
   997 }
       
   998 
       
   999 
       
  1000 /****************************************/
       
  1001 /* B.2 - Language IL (Instruction List) */
       
  1002 /****************************************/
       
  1003 /***********************************/
       
  1004 /* B 2.1 Instructions and Operands */
       
  1005 /***********************************/
       
  1006 /*| instruction_list il_instruction */
       
  1007 /* The visitor of the base class search_visitor_c will handle calling each instruction in the list.
       
  1008  * We do not need to do anything here...
       
  1009  */
       
  1010 // void *visit_expression_type_c::visit(instruction_list_c *symbol)
       
  1011 
       
  1012 /* | label ':' [il_incomplete_instruction] eol_list */
       
  1013 //SYM_REF2(il_instruction_c, label, il_instruction)
       
  1014 // void *visit_expression_type_c::visit(il_instruction_c *symbol);
       
  1015 
       
  1016 
       
  1017 /* | il_simple_operator [il_operand] */
       
  1018 // SYM_REF2(il_simple_operation_c, il_simple_operator, il_operand)
       
  1019 void *visit_expression_type_c::visit(il_simple_operation_c *symbol) {
       
  1020   if (il_error)
       
  1021     return NULL;
       
  1022 
       
  1023   /* determine the data type of the operand */
       
  1024   il_operand = symbol->il_operand;
       
  1025   if (symbol->il_operand != NULL){
       
  1026     il_operand_type = base_type((symbol_c *)symbol->il_operand->accept(*this));
       
  1027   } else {
       
  1028     il_operand_type = NULL;
       
  1029   }
       
  1030   /* recursive call to see whether data types are compatible */
       
  1031   symbol->il_simple_operator->accept(*this);
       
  1032 
       
  1033   il_operand_type = NULL;
       
  1034   il_operand = NULL;
       
  1035   return NULL;
       
  1036 }
       
  1037 
       
  1038 // | function_name [il_operand_list] */
       
  1039 //SYM_REF2(il_function_call_c, function_name, il_operand_list)
       
  1040 void *visit_expression_type_c::visit(il_function_call_c *symbol) {
       
  1041   if (il_error)
       
  1042     return NULL;
       
  1043 
       
  1044   symbol_c *return_data_type = NULL;
       
  1045 
       
  1046   /* First find the declaration of the function being called! */
       
  1047   function_symtable_t::iterator lower = function_symtable.lower_bound(symbol->function_name);
       
  1048   function_symtable_t::iterator upper = function_symtable.upper_bound(symbol->function_name);
       
  1049   if (lower == function_symtable.end()) ERROR;
       
  1050 
       
  1051   int error_count = 0; 
       
  1052   int *error_count_ptr = NULL;
       
  1053 
       
  1054   function_symtable_t::iterator second = lower;
       
  1055   second++;
       
  1056   if (second != upper) 
       
  1057     /* This is a call to an overloaded function... */  
       
  1058     error_count_ptr = &error_count;
       
  1059 
       
  1060   for(; lower != upper; lower++) {
       
  1061     function_declaration_c *f_decl = function_symtable.get_value(lower);
       
  1062     
       
  1063     check_nonformal_call(symbol, f_decl, true, error_count_ptr);
       
  1064     
       
  1065     if (0 == error_count) {
       
  1066       /* Either: 
       
  1067        * (i) we have a call to a non-overloaded function (error_cnt_ptr is NULL!, so error_count won't change!)  
       
  1068        * (ii) we have a call to an overloaded function, with no errors!
       
  1069        */
       
  1070       
       
  1071       /* Store the pointer to the declaration of the function being called.
       
  1072        * This data will be used by stage 4 to call the correct function.
       
  1073        * Mostly needed to disambiguate overloaded functions...
       
  1074        * See comments in absyntax.def for more details
       
  1075        */
       
  1076       symbol->called_function_declaration = f_decl;
       
  1077       /* determine the base data type returned by the function being called... */
       
  1078       return_data_type = base_type(f_decl->type_name);
       
  1079       /* If the following occurs, then we must have some big bug in the syntax parser (stage 2)... */
       
  1080       if (NULL == return_data_type) ERROR;
       
  1081       /* set the new data type of the default variable for the following verifications... */
       
  1082       il_default_variable_type = return_data_type;
       
  1083       return NULL;
       
  1084     }
       
  1085   }
       
  1086 
       
  1087   /* No compatible function was found for this function call */
       
  1088   STAGE3_ERROR(symbol, symbol, "Call to an overloaded function with invalid parameter type.");
       
  1089   return NULL;
       
  1090 }
       
  1091 
       
  1092 
       
  1093 /* | il_expr_operator '(' [il_operand] eol_list [simple_instr_list] ')' */
       
  1094 // SYM_REF3(il_expression_c, il_expr_operator, il_operand, simple_instr_list);
       
  1095 void *visit_expression_type_c::visit(il_expression_c *symbol) {
       
  1096   if (il_error)
       
  1097     return NULL;
       
  1098 
       
  1099   symbol_c *il_default_variable_type_back = il_default_variable_type;
       
  1100 
       
  1101   il_parenthesis_level++;
       
  1102 
       
  1103   if(symbol->il_operand != NULL) {
       
  1104      il_default_variable_type = base_type((symbol_c *)symbol->il_operand->accept(*this));
       
  1105   } else {
       
  1106      il_default_variable_type = NULL;
       
  1107   }
       
  1108 
       
  1109   if(symbol->simple_instr_list != NULL) {
       
  1110     symbol->simple_instr_list->accept(*this);
       
  1111   }
       
  1112 
       
  1113   il_parenthesis_level--;
       
  1114   if (il_parenthesis_level < 0) ERROR;
       
  1115 
       
  1116   il_operand = symbol->simple_instr_list;
       
  1117   il_operand_type = il_default_variable_type;
       
  1118   il_default_variable_type = il_default_variable_type_back;
       
  1119 
       
  1120   /* Now check the if the data type semantics of operation are correct,
       
  1121    * but only if no previous error has been found...
       
  1122    */
       
  1123   if (!il_error)
       
  1124     symbol->il_expr_operator->accept(*this);
       
  1125 
       
  1126   il_operand_type = NULL;
       
  1127   il_operand = NULL;
       
  1128   return NULL;
       
  1129 }
       
  1130 
       
  1131 
       
  1132 #if 0
       
  1133 /*  il_jump_operator label */
       
  1134 SYM_REF2(il_jump_operation_c, il_jump_operator, label)
       
  1135 void *visit_expression_type_c::visit(il_jump_operation_c *symbol);
       
  1136 #endif
       
  1137 
       
  1138 
       
  1139 /*   il_call_operator prev_declared_fb_name
       
  1140  * | il_call_operator prev_declared_fb_name '(' ')'
       
  1141  * | il_call_operator prev_declared_fb_name '(' eol_list ')'
       
  1142  * | il_call_operator prev_declared_fb_name '(' il_operand_list ')'
       
  1143  * | il_call_operator prev_declared_fb_name '(' eol_list il_param_list ')'
       
  1144  */
       
  1145 /* SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list) */
       
  1146 void *visit_expression_type_c::visit(il_fb_call_c *symbol) {
       
  1147   if (il_error)
       
  1148     return NULL;
       
  1149 
       
  1150   /* first check whether the il_default_variable is of the correct type
       
  1151    * for the CAL / CALC / CALCN operator being used...
       
  1152    */
       
  1153   symbol->il_call_operator->accept(*this);
       
  1154 
       
  1155   /* Now check the FB call itself... */
       
  1156 
       
  1157   /* First we find the declaration of the FB type of the FB instance being called... */
       
  1158   /* e.g.  Function_block foo_fb_type
       
  1159    *         ...
       
  1160    *       End_Function_Block
       
  1161    *
       
  1162    *       Program test
       
  1163    *         var fb1 : foo_fb_type; end_var
       
  1164    *         fb1(...)
       
  1165    *       End_Program
       
  1166    *
       
  1167    *    search_varfb_instance_type->get_basetype_decl( identifier_c("fb1") )
       
  1168    *    in the scope of Program 'test'
       
  1169    *    will return the fb declaration of foo_fb_type !!
       
  1170    */
       
  1171 #if 0
       
  1172   symbol_c *fb_decl_symbol = search_varfb_instance_type->get_basetype_decl(symbol->fb_name);
       
  1173     /* The following should never occur. The function block must be defined, 
       
  1174      * and the FB type being called MUST be in the symtable... 
       
  1175      * This was all already checked at stage 2!
       
  1176      */
       
  1177   if (NULL == fb_decl_symbol) ERROR;
       
  1178 
       
  1179   function_block_declaration_c *fb_decl = dynamic_cast<function_block_declaration_c *>(fb_decl_symbol);
       
  1180     /* should never occur. ... */
       
  1181   if (NULL == fb_decl) ERROR;
       
  1182 #endif
       
  1183   symbol_c *fb_decl = search_varfb_instance_type->get_basetype_decl(symbol->fb_name);
       
  1184     /* The following should never occur. The function block must be defined, 
       
  1185      * and the FB type being called MUST be in the symtable... 
       
  1186      * This was all already checked at stage 2!
       
  1187      */
       
  1188   if (NULL == fb_decl) ERROR;
       
  1189 
       
  1190   /* now check the semantics of the fb call... */
       
  1191   /* If the syntax parser is working correctly, exactly one of the 
       
  1192    * following two symbols will be NULL, while the other is != NULL.
       
  1193    */
       
  1194   if (NULL != symbol->il_operand_list)  check_nonformal_call(symbol, fb_decl);
       
  1195   if (NULL != symbol->il_param_list)    check_formal_call   (symbol, fb_decl);
       
  1196 
       
  1197   return NULL;
       
  1198 }
       
  1199 
       
  1200 
       
  1201 
       
  1202 /* | function_name '(' eol_list [il_param_list] ')' */
       
  1203 /* SYM_REF2(il_formal_funct_call_c, function_name, il_param_list) */
       
  1204 void *visit_expression_type_c::visit(il_formal_funct_call_c *symbol) {
       
  1205   if (il_error)
       
  1206     return NULL;
       
  1207 
       
  1208   symbol_c *return_data_type = NULL;
       
  1209   function_symtable_t::iterator lower = function_symtable.lower_bound(symbol->function_name);
       
  1210   function_symtable_t::iterator upper = function_symtable.upper_bound(symbol->function_name);
       
  1211   
       
  1212   if (lower == function_symtable.end()) {
       
  1213     function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name);
       
  1214     if (current_function_type == function_none) ERROR;
       
  1215     return NULL;
       
  1216   }
       
  1217 
       
  1218   int error_count = 0; 
       
  1219   int *error_count_ptr = NULL;
       
  1220 
       
  1221   function_symtable_t::iterator second = lower;
       
  1222   second++;
       
  1223   if (second != upper) 
       
  1224     /* This is a call to an overloaded function... */  
       
  1225     error_count_ptr = &error_count;
       
  1226 
       
  1227   for(; lower != upper; lower++) {
       
  1228     function_declaration_c *f_decl = function_symtable.get_value(lower);
       
  1229   
       
  1230     /* check semantics of data passed in the function call... */
       
  1231     check_formal_call(symbol, f_decl, error_count_ptr);
       
  1232 
       
  1233     if (0 == error_count) {
       
  1234       /* Either: 
       
  1235        * (i) we have a call to a non-overloaded function (error_cnt_ptr is NULL!, so error_count won't change!)  
       
  1236        * (ii) we have a call to an overloaded function, with no errors!
       
  1237        */
       
  1238       
       
  1239       /* Store the pointer to the declaration of the function being called.
       
  1240        * This data will be used by stage 4 to call the correct function.
       
  1241        * Mostly needed to disambiguate overloaded functions...
       
  1242        * See comments in absyntax.def for more details
       
  1243        */
       
  1244       symbol->called_function_declaration = f_decl;
       
  1245       /* determine the base data type returned by the function being called... */
       
  1246       return_data_type = base_type(f_decl->type_name);
       
  1247       /* the following should never occur. If it does, then we have a bug in the syntax parser (stage 2)... */
       
  1248       if (NULL == return_data_type) ERROR;
       
  1249       /* the data type of the data returned by the function, and stored in the il default variable... */
       
  1250       il_default_variable_type = return_data_type;
       
  1251       return NULL;
       
  1252     }  
       
  1253   }
       
  1254   
       
  1255   /* No compatible function was found for this function call */
       
  1256   STAGE3_ERROR(symbol, symbol, "Call to an overloaded function with invalid parameter type.");
       
  1257   return NULL;
       
  1258 }
       
  1259 
       
  1260 
       
  1261 #if 0
       
  1262 /* | il_operand_list ',' il_operand */
       
  1263 SYM_LIST(il_operand_list_c)
       
  1264 void *visit_expression_type_c::visit(il_operand_list_c *symbol);
       
  1265 
       
  1266 /* | simple_instr_list il_simple_instruction */
       
  1267 SYM_LIST(simple_instr_list_c)
       
  1268 void *visit_expression_type_c::visit(simple_instr_list_c *symbol);
       
  1269 
       
  1270 /* | il_initial_param_list il_param_instruction */
       
  1271 SYM_LIST(il_param_list_c)
       
  1272 void *visit_expression_type_c::visit(il_param_list_c *symbol);
       
  1273 
       
  1274 /*  il_assign_operator il_operand
       
  1275  * | il_assign_operator '(' eol_list simple_instr_list ')'
       
  1276  */
       
  1277 SYM_REF3(il_param_assignment_c, il_assign_operator, il_operand, simple_instr_list)
       
  1278 void *visit_expression_type_c::visit(il_param_assignment_c *symbol);
       
  1279 /*  il_assign_out_operator variable */
       
  1280 SYM_REF2(il_param_out_assignment_c, il_assign_out_operator, variable)
       
  1281 void *visit_expression_type_c::visit(il_param_out_assignment_c *symbol);
       
  1282 
       
  1283 #endif
       
  1284 
       
  1285 
       
  1286 /*******************/
       
  1287 /* B 2.2 Operators */
       
  1288 /*******************/
       
  1289 
       
  1290 //SYM_REF0(LD_operator_c)
       
  1291 void *visit_expression_type_c::visit(LD_operator_c *symbol) {
       
  1292   if (0 == il_parenthesis_level)
       
  1293     il_error = false;
       
  1294 
       
  1295   if(il_operand_type == NULL)
       
  1296       STAGE3_ERROR(symbol, symbol, "LD operator requires an operand.");
       
  1297   il_default_variable_type = il_operand_type;
       
  1298   return NULL;
       
  1299 }
       
  1300 
       
  1301 // SYM_REF0(LDN_operator_c)
       
  1302 void *visit_expression_type_c::visit(LDN_operator_c *symbol) {
       
  1303   if(il_operand_type == NULL)
       
  1304       STAGE3_ERROR(symbol, symbol, "LDN operator requires an operand.");
       
  1305   if(!is_ANY_BIT_compatible(il_operand_type))
       
  1306       STAGE3_ERROR(symbol, il_operand, "invalid data type of LDN operand, should be of type ANY_BIT.");
       
  1307   il_default_variable_type = il_operand_type;
       
  1308   return NULL;
       
  1309 }
       
  1310 
       
  1311 // SYM_REF0(ST_operator_c)
       
  1312 void *visit_expression_type_c::visit(ST_operator_c *symbol) {
       
  1313   verify_null(symbol);
       
  1314 
       
  1315   if(!is_valid_assignment(il_operand_type, il_default_variable_type))
       
  1316     STAGE3_ERROR(symbol, symbol, "Type mismatch in ST operation.");
       
  1317   /* TODO: check whether il_operand_type is an LVALUE !! */
       
  1318   /* data type of il_default_variable_type is unchanged... */
       
  1319   // il_default_variable_type = il_default_variable_type;
       
  1320   return NULL;
       
  1321 }
       
  1322 
       
  1323 // SYM_REF0(STN_operator_c)
       
  1324  void *visit_expression_type_c::visit(STN_operator_c *symbol) {
       
  1325   verify_null(symbol);
       
  1326   if(!is_valid_assignment(il_operand_type, il_default_variable_type))
       
  1327     STAGE3_ERROR(symbol, symbol, "Type mismatch in ST operation.");
       
  1328   /* TODO: check whether il_operand_type is an LVALUE !! */
       
  1329   if(!is_ANY_BIT_compatible(il_default_variable_type))
       
  1330       STAGE3_ERROR(symbol, symbol, "invalid data type of il_default_variable for STN operand, should be of type ANY_BIT.");
       
  1331   if(!is_ANY_BIT_compatible(il_operand_type))
       
  1332       STAGE3_ERROR(symbol, il_operand, "invalid data type of STN operand, should be of type ANY_BIT.");
       
  1333   /* data type of il_default_variable_type is unchanged... */
       
  1334   // il_default_variable_type = il_default_variable_type;
       
  1335   return NULL;
       
  1336 }
       
  1337 
       
  1338 //SYM_REF0(NOT_operator_c)
       
  1339 void *visit_expression_type_c::visit(NOT_operator_c *symbol) {
       
  1340   if(il_operand_type != NULL){
       
  1341     STAGE3_ERROR(symbol, il_operand, "NOT operator may not have an operand.");
       
  1342     return NULL;
       
  1343   }
       
  1344   if(il_default_variable_type == NULL) {
       
  1345     STAGE3_ERROR(symbol, symbol, "Il default variable should not be NULL.");
       
  1346     return NULL;
       
  1347   }
       
  1348   if(!is_ANY_BIT_compatible(il_default_variable_type)) {
       
  1349     STAGE3_ERROR(symbol, symbol, "Il default variable should be of type ANY_BIT.");
       
  1350     return NULL;
       
  1351   }
       
  1352   /* data type of il_default_variable_type is unchanged... */
       
  1353   // il_default_variable_type = il_default_variable_type;
       
  1354   return NULL;
       
  1355 }
       
  1356 
       
  1357 // SYM_REF0(S_operator_c)
       
  1358 void *visit_expression_type_c::visit(S_operator_c *symbol) {
       
  1359   verify_null(symbol);
       
  1360   if (!is_BOOL_type(il_default_variable_type)) {STAGE3_ERROR(symbol, symbol, "IL default variable should be BOOL type.");}
       
  1361   if (!is_BOOL_type(il_operand_type)) {STAGE3_ERROR(symbol, il_operand, "operator S requires operand of type BOOL.");}
       
  1362   /* TODO: check whether il_operand_type is an LVALUE !! */
       
  1363   /* data type of il_default_variable_type is unchanged... */
       
  1364   // il_default_variable_type = il_default_variable_type;
       
  1365   return NULL;
       
  1366 }
       
  1367 
       
  1368 // SYM_REF0(R_operator_c)
       
  1369 void *visit_expression_type_c::visit(R_operator_c *symbol) {
       
  1370   verify_null(symbol);
       
  1371   if (!is_BOOL_type(il_default_variable_type)) {STAGE3_ERROR(symbol, symbol, "IL default variable should be BOOL type.");}
       
  1372   if (!is_BOOL_type(il_operand_type)) {STAGE3_ERROR(symbol, il_operand, "operator R requires operand of type BOOL.");}
       
  1373   /* TODO: check whether il_operand_type is an LVALUE !! */
       
  1374   /* data type of il_default_variable_type is unchanged... */
       
  1375   // il_default_variable_type = il_default_variable_type;
       
  1376   return NULL;
       
  1377 }
       
  1378 
       
  1379 
       
  1380 // SYM_REF0(S1_operator_c)
       
  1381 void *visit_expression_type_c::visit(S1_operator_c *symbol){
       
  1382   check_il_fbcall(symbol, "S1");
       
  1383   return NULL;
       
  1384 }
       
  1385 
       
  1386 // SYM_REF0(R1_operator_c)
       
  1387 void *visit_expression_type_c::visit(R1_operator_c *symbol) {
       
  1388   check_il_fbcall(symbol, "R1");
       
  1389   return NULL;
       
  1390 }
       
  1391 
       
  1392 // SYM_REF0(CLK_operator_c)
       
  1393 void *visit_expression_type_c::visit(CLK_operator_c *symbol) {
       
  1394   check_il_fbcall(symbol, "CLK");
       
  1395   return NULL;
       
  1396 }
       
  1397 
       
  1398 // SYM_REF0(CU_operator_c)
       
  1399 void *visit_expression_type_c::visit(CU_operator_c *symbol) {
       
  1400   check_il_fbcall(symbol, "CU");
       
  1401   return NULL;
       
  1402 }
       
  1403 
       
  1404 // SYM_REF0(CD_operator_c)
       
  1405 void *visit_expression_type_c::visit(CD_operator_c *symbol) {
       
  1406   check_il_fbcall(symbol, "CD");
       
  1407   return NULL;
       
  1408 }
       
  1409 
       
  1410 // SYM_REF0(PV_operator_c)
       
  1411 void *visit_expression_type_c::visit(PV_operator_c *symbol) {
       
  1412   check_il_fbcall(symbol, "PV");
       
  1413   return NULL;
       
  1414 }
       
  1415 
       
  1416 // SYM_REF0(IN_operator_c)
       
  1417 void *visit_expression_type_c::visit(IN_operator_c *symbol) {
       
  1418   check_il_fbcall(symbol, "IN");
       
  1419   return NULL;
       
  1420 }
       
  1421 
       
  1422 // SYM_REF0(PT_operator_c)
       
  1423 void *visit_expression_type_c::visit(PT_operator_c *symbol) {
       
  1424   check_il_fbcall(symbol, "PT");
       
  1425   return NULL;
       
  1426 }
       
  1427 
       
  1428 //SYM_REF0(AND_operator_c)
       
  1429 void *visit_expression_type_c::visit(AND_operator_c *symbol) {
       
  1430   verify_null(symbol);
       
  1431   il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_BIT_compatible,
       
  1432                                                 symbol                  , il_operand);
       
  1433   return NULL;
       
  1434 }
       
  1435 
       
  1436 //SYM_REF0(OR_operator_c)
       
  1437 void *visit_expression_type_c::visit(OR_operator_c *symbol) {
       
  1438   verify_null(symbol);
       
  1439   il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_BIT_compatible,
       
  1440                                                 symbol                  , il_operand);
       
  1441   return NULL;
       
  1442 }
       
  1443 
       
  1444 //SYM_REF0(XOR_operator_c)
       
  1445 void *visit_expression_type_c::visit(XOR_operator_c *symbol) {
       
  1446   verify_null(symbol);
       
  1447   il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_BIT_compatible,
       
  1448                                                 symbol                  , il_operand);
       
  1449   return NULL;
       
  1450 }
       
  1451 
       
  1452 // SYM_REF0(ANDN_operator_c)
       
  1453 void *visit_expression_type_c::visit(ANDN_operator_c *symbol) {
       
  1454   verify_null(symbol);
       
  1455   il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_BIT_compatible,
       
  1456                                                 symbol                  , il_operand);
       
  1457   return NULL;
       
  1458 }
       
  1459 
       
  1460 // SYM_REF0(ORN_operator_c)
       
  1461 void *visit_expression_type_c::visit(ORN_operator_c *symbol) {
       
  1462   verify_null(symbol);
       
  1463   il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_BIT_compatible,
       
  1464                                                 symbol                  , il_operand);
       
  1465   return NULL;
       
  1466 }
       
  1467 
       
  1468 // SYM_REF0(XORN_operator_c)
       
  1469 void *visit_expression_type_c::visit(XORN_operator_c *symbol) {
       
  1470   verify_null(symbol);
       
  1471   il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_BIT_compatible,
       
  1472                                                 symbol                  , il_operand);
       
  1473   return NULL;
       
  1474 }
       
  1475 
       
  1476 // SYM_REF0(ADD_operator_c)
       
  1477 void *visit_expression_type_c::visit(ADD_operator_c *symbol) {
       
  1478   verify_null(symbol);
       
  1479   symbol_c *left_type  = il_default_variable_type;
       
  1480   symbol_c *right_type = il_operand_type;
       
  1481 
       
  1482 /* The following is not required, it is already handled by compute_expression() ... */
       
  1483 /*
       
  1484   if      (is_type(left_type, time_type_name_c) && is_type(right_type, time_type_name_c))
       
  1485     il_default_variable_type = &time_type_name;
       
  1486 */
       
  1487 
       
  1488   if      (is_type(left_type, tod_type_name_c)      && is_type(right_type, time_type_name_c))
       
  1489     il_default_variable_type = &tod_type_name;
       
  1490   else if (is_type(left_type, safetod_type_name_c)  && is_type(right_type, time_type_name_c))
       
  1491     il_default_variable_type = &tod_type_name;
       
  1492   else if (is_type(left_type, tod_type_name_c)      && is_type(right_type, safetime_type_name_c))
       
  1493     il_default_variable_type = &tod_type_name;
       
  1494   else if (is_type(left_type, safetod_type_name_c)  && is_type(right_type, safetime_type_name_c))
       
  1495     il_default_variable_type = &safetod_type_name;
       
  1496 
       
  1497   else if (is_type(left_type, dt_type_name_c)       && is_type(right_type, time_type_name_c))
       
  1498     il_default_variable_type = &dt_type_name;
       
  1499   else if (is_type(left_type, safedt_type_name_c)   && is_type(right_type, time_type_name_c))
       
  1500     il_default_variable_type = &dt_type_name;
       
  1501   else if (is_type(left_type, dt_type_name_c)       && is_type(right_type, safetime_type_name_c))
       
  1502     il_default_variable_type = &dt_type_name;
       
  1503   else if (is_type(left_type, safedt_type_name_c)   && is_type(right_type, safetime_type_name_c))
       
  1504     il_default_variable_type = &safedt_type_name;
       
  1505 
       
  1506   else il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_MAGNITUDE_compatible,
       
  1507                                                      symbol                  , il_operand);
       
  1508   return NULL;
       
  1509 }
       
  1510 
       
  1511 // SYM_REF0(SUB_operator_c)
       
  1512 void *visit_expression_type_c::visit(SUB_operator_c *symbol) {
       
  1513   verify_null(symbol);
       
  1514   symbol_c *left_type = il_default_variable_type;
       
  1515   symbol_c *right_type = il_operand_type;;
       
  1516 
       
  1517 /* The following is not required, it is already handled by compute_expression() ... */
       
  1518 /*
       
  1519   if      (typeid(*left_type) == typeid(time_type_name_c) && typeid(*right_type) == typeid(time_type_name_c))
       
  1520     il_default_variable_type = &time_type_name;
       
  1521 */
       
  1522 
       
  1523   if      (is_type(left_type, tod_type_name_c)       && is_type(right_type, time_type_name_c))
       
  1524     il_default_variable_type = &tod_type_name;
       
  1525   else if (is_type(left_type, safetod_type_name_c)   && is_type(right_type, time_type_name_c))
       
  1526     il_default_variable_type = &tod_type_name;
       
  1527   else if (is_type(left_type, tod_type_name_c)       && is_type(right_type, safetime_type_name_c))
       
  1528     il_default_variable_type = &tod_type_name;
       
  1529   else if (is_type(left_type, safetod_type_name_c)   && is_type(right_type, safetime_type_name_c))
       
  1530     il_default_variable_type = &safetod_type_name;
       
  1531 
       
  1532   else if (is_type(left_type, dt_type_name_c)       && is_type(right_type, time_type_name_c))
       
  1533     il_default_variable_type = &dt_type_name;
       
  1534   else if (is_type(left_type, safedt_type_name_c)   && is_type(right_type, time_type_name_c))
       
  1535     il_default_variable_type = &dt_type_name;
       
  1536   else if (is_type(left_type, dt_type_name_c)       && is_type(right_type, safetime_type_name_c))
       
  1537     il_default_variable_type = &dt_type_name;
       
  1538   else if (is_type(left_type, safedt_type_name_c)   && is_type(right_type, safetime_type_name_c))
       
  1539     il_default_variable_type = &safedt_type_name;
       
  1540 
       
  1541   else if (is_type(left_type, date_type_name_c)     && is_type(right_type, date_type_name_c))
       
  1542     il_default_variable_type = &time_type_name;
       
  1543   else if (is_type(left_type, safedate_type_name_c) && is_type(right_type, date_type_name_c))
       
  1544     il_default_variable_type = &time_type_name;
       
  1545   else if (is_type(left_type, date_type_name_c)     && is_type(right_type, safedate_type_name_c))
       
  1546     il_default_variable_type = &time_type_name;
       
  1547   else if (is_type(left_type, safedate_type_name_c) && is_type(right_type, safedate_type_name_c))
       
  1548     il_default_variable_type = &safetime_type_name;
       
  1549 
       
  1550   else if (is_type(left_type, tod_type_name_c)      && is_type(right_type, tod_type_name_c))
       
  1551     il_default_variable_type = &time_type_name;
       
  1552   else if (is_type(left_type, safetod_type_name_c)  && is_type(right_type, tod_type_name_c))
       
  1553     il_default_variable_type = &time_type_name;
       
  1554   else if (is_type(left_type, tod_type_name_c)      && is_type(right_type, safetod_type_name_c))
       
  1555     il_default_variable_type = &time_type_name;
       
  1556   else if (is_type(left_type, safetod_type_name_c)  && is_type(right_type, safetod_type_name_c))
       
  1557     il_default_variable_type = &safetime_type_name;
       
  1558 
       
  1559   else if (is_type(left_type, dt_type_name_c)       && is_type(right_type, dt_type_name_c))
       
  1560     il_default_variable_type = &time_type_name;
       
  1561   else if (is_type(left_type, safedt_type_name_c)   && is_type(right_type, dt_type_name_c))
       
  1562     il_default_variable_type = &time_type_name;
       
  1563   else if (is_type(left_type, dt_type_name_c)       && is_type(right_type, safedt_type_name_c))
       
  1564     il_default_variable_type = &time_type_name;
       
  1565   else if (is_type(left_type, safedt_type_name_c)   && is_type(right_type, safedt_type_name_c))
       
  1566     il_default_variable_type = &safetime_type_name;
       
  1567 
       
  1568   else il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_MAGNITUDE_compatible,
       
  1569                                                      symbol                  , il_operand);
       
  1570   return NULL;
       
  1571 }
       
  1572 
       
  1573 // SYM_REF0(MUL_operator_c)
       
  1574 void *visit_expression_type_c::visit(MUL_operator_c *symbol) {
       
  1575   verify_null(symbol);
       
  1576   symbol_c *left_type = il_default_variable_type;
       
  1577   symbol_c *right_type = il_operand_type;
       
  1578 
       
  1579   if      (is_type(left_type, time_type_name_c)     && is_ANY_NUM_compatible(right_type))
       
  1580     il_default_variable_type = &time_type_name;
       
  1581   else if (is_type(left_type, safetime_type_name_c) && is_ANY_NUM_type(right_type))
       
  1582     il_default_variable_type = &time_type_name;
       
  1583   else if (is_type(left_type, safetime_type_name_c) && is_ANY_SAFENUM_type(right_type))
       
  1584     il_default_variable_type = &safetime_type_name;
       
  1585   /* Since we have already checked for ANY_NUM_type and ANY_SAFENUM_type in the previous lines,
       
  1586    * this next line is really only to check for integers/reals of undefined type on 'right_type'... 
       
  1587    */
       
  1588   else if (is_type(left_type, safetime_type_name_c) && is_ANY_NUM_compatible(right_type))
       
  1589     il_default_variable_type = &safetime_type_name;
       
  1590 
       
  1591   else il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_NUM_compatible,
       
  1592                                                      symbol                  , il_operand);
       
  1593   return NULL;
       
  1594 }
       
  1595 
       
  1596 // SYM_REF0(DIV_operator_c)
       
  1597 void *visit_expression_type_c::visit(DIV_operator_c *symbol) {
       
  1598   verify_null(symbol);
       
  1599   symbol_c *left_type = il_default_variable_type;
       
  1600   symbol_c *right_type = il_operand_type;
       
  1601 
       
  1602   if      (is_type(left_type, time_type_name_c)     && is_ANY_NUM_compatible(right_type))
       
  1603     il_default_variable_type = &time_type_name;
       
  1604   else if (is_type(left_type, safetime_type_name_c) && is_ANY_NUM_type(right_type))
       
  1605     il_default_variable_type = &time_type_name;
       
  1606   else if (is_type(left_type, safetime_type_name_c) && is_ANY_SAFENUM_type(right_type))
       
  1607     il_default_variable_type = &safetime_type_name;
       
  1608   /* Since we have already checked for ANY_NUM_type and ANY_SAFENUM_type in the previous lines,
       
  1609    * this next line is really only to check for integers/reals of undefined type on 'right_type'... 
       
  1610    */
       
  1611   else if (is_type(left_type, safetime_type_name_c) && is_ANY_NUM_compatible(right_type))
       
  1612     il_default_variable_type = &safetime_type_name;
       
  1613 
       
  1614   else il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_NUM_compatible,
       
  1615                                                      symbol                  , il_operand);
       
  1616   return NULL;
       
  1617 }
       
  1618 
       
  1619 // SYM_REF0(MOD_operator_c)
       
  1620 void *visit_expression_type_c::visit(MOD_operator_c *symbol) {
       
  1621   verify_null(symbol);
       
  1622   il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_INT_compatible,
       
  1623                                                 symbol                  , il_operand);
       
  1624   return NULL;
       
  1625 }
       
  1626 
       
  1627 // SYM_REF0(GT_operator_c)
       
  1628 void *visit_expression_type_c::visit(GT_operator_c *symbol) {
       
  1629   verify_null(symbol);
       
  1630   compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible,
       
  1631                      symbol                  , il_operand);
       
  1632   il_default_variable_type = &search_expression_type_c::bool_type_name;
       
  1633   return NULL;
       
  1634 }
       
  1635 
       
  1636 //SYM_REF0(GE_operator_c)
       
  1637 void *visit_expression_type_c::visit(GE_operator_c *symbol) {
       
  1638   verify_null(symbol);
       
  1639   compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible,
       
  1640                      symbol                  , il_operand);
       
  1641   il_default_variable_type = &search_expression_type_c::bool_type_name;
       
  1642   return NULL;
       
  1643 }
       
  1644 
       
  1645 //SYM_REF0(EQ_operator_c)
       
  1646 void *visit_expression_type_c::visit(EQ_operator_c *symbol) {
       
  1647   verify_null(symbol);
       
  1648   compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible,
       
  1649                      symbol                  , il_operand);
       
  1650   il_default_variable_type = &search_expression_type_c::bool_type_name;
       
  1651   return NULL;
       
  1652 }
       
  1653 
       
  1654 //SYM_REF0(LT_operator_c)
       
  1655 void *visit_expression_type_c::visit(LT_operator_c *symbol) {
       
  1656   verify_null(symbol);
       
  1657   compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible,
       
  1658                      symbol                  , il_operand);
       
  1659   il_default_variable_type = &search_expression_type_c::bool_type_name;
       
  1660   return NULL;
       
  1661 }
       
  1662 
       
  1663 //SYM_REF0(LE_operator_c)
       
  1664 void *visit_expression_type_c::visit(LE_operator_c *symbol) {
       
  1665   verify_null(symbol);
       
  1666   compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible,
       
  1667                      symbol                  , il_operand);
       
  1668   il_default_variable_type = &search_expression_type_c::bool_type_name;
       
  1669   return NULL;
       
  1670 }
       
  1671 
       
  1672 //SYM_REF0(NE_operator_c)
       
  1673 void *visit_expression_type_c::visit(NE_operator_c *symbol) {
       
  1674   verify_null(symbol);
       
  1675   compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible,
       
  1676                      symbol                  , il_operand);
       
  1677   il_default_variable_type = &search_expression_type_c::bool_type_name;
       
  1678   return NULL;
       
  1679 }
       
  1680 
       
  1681 // SYM_REF0(CAL_operator_c)
       
  1682 void *visit_expression_type_c::visit(CAL_operator_c *symbol) {
       
  1683   return NULL;
       
  1684 }
       
  1685 
       
  1686 // SYM_REF0(CALC_operator_c)
       
  1687 void *visit_expression_type_c::visit(CALC_operator_c *symbol) {
       
  1688   if(il_default_variable_type == NULL)
       
  1689     STAGE3_ERROR(symbol, symbol, "CALC: il default variable should not be NULL.");
       
  1690   if (!is_BOOL_type(il_default_variable_type))
       
  1691     STAGE3_ERROR(symbol, symbol, "CALC operator requires il_default_variable to be of type BOOL.");
       
  1692   return NULL;
       
  1693 }
       
  1694 
       
  1695 // SYM_REF0(CALCN_operator_c)
       
  1696 void *visit_expression_type_c::visit(CALCN_operator_c *symbol) {
       
  1697   if(il_default_variable_type == NULL)
       
  1698     STAGE3_ERROR(symbol, symbol, "CALCN: il_default_variable should not be NULL.");
       
  1699   if (!is_BOOL_type(il_default_variable_type))
       
  1700     STAGE3_ERROR(symbol, symbol, "CALCN operator requires il_default_variable to be of type BOOL.");
       
  1701   return NULL;
       
  1702 }
       
  1703 
       
  1704 // SYM_REF0(RET_operator_c)
       
  1705 void *visit_expression_type_c::visit(RET_operator_c *symbol) {
       
  1706   return NULL;
       
  1707 }
       
  1708 
       
  1709 // SYM_REF0(RETC_operator_c)
       
  1710 void *visit_expression_type_c::visit(RETC_operator_c *symbol) {
       
  1711   if(il_default_variable_type == NULL)
       
  1712     STAGE3_ERROR(symbol, symbol, "RETC: il default variable should not be NULL.");
       
  1713   if (!is_BOOL_type(il_default_variable_type))
       
  1714     STAGE3_ERROR(symbol, symbol, "RETC operator requires il_default_variable to be of type BOOL.");
       
  1715   return NULL;
       
  1716 }
       
  1717 
       
  1718 // SYM_REF0(RETCN_operator_c)
       
  1719 void *visit_expression_type_c::visit(RETCN_operator_c *symbol) {
       
  1720   if(il_default_variable_type == NULL)
       
  1721     STAGE3_ERROR(symbol, symbol, "RETCN: il_default_variable should not be NULL.");
       
  1722   if (!is_BOOL_type(il_default_variable_type))
       
  1723     STAGE3_ERROR(symbol, symbol, "RETCN operator requires il_default_variable to be of type BOOL.");
       
  1724   return NULL;
       
  1725 }
       
  1726 
       
  1727 // SYM_REF0(JMP_operator_c)
       
  1728 void *visit_expression_type_c::visit(JMP_operator_c *symbol){
       
  1729   return NULL;
       
  1730 }
       
  1731 
       
  1732 // SYM_REF0(JMPC_operator_c)
       
  1733 void *visit_expression_type_c::visit(JMPC_operator_c *symbol) {
       
  1734   if(il_default_variable_type == NULL)
       
  1735     STAGE3_ERROR(symbol, symbol, "JMPC: il default variable should not be NULL.");
       
  1736   if (!is_BOOL_type(il_default_variable_type))
       
  1737     STAGE3_ERROR(symbol, symbol, "JMPC operator requires il_default_variable to be of type BOOL.");
       
  1738   return NULL;
       
  1739 }
       
  1740 
       
  1741 // SYM_REF0(JMPCN_operator_c)
       
  1742 void *visit_expression_type_c::visit(JMPCN_operator_c *symbol) {
       
  1743   if(il_default_variable_type == NULL)
       
  1744     STAGE3_ERROR(symbol, symbol, "JMPCN: il_default_variable should not be NULL.");
       
  1745   if (!is_BOOL_type(il_default_variable_type))
       
  1746     STAGE3_ERROR(symbol, symbol, "JMPCN operator requires il_default_variable to be of type BOOL.");
       
  1747   return NULL;
       
  1748 }
       
  1749 
       
  1750 /* Symbol class handled together with function call checks */
       
  1751 /*  any_identifier ASSIGN */
       
  1752 // SYM_REF1(il_assign_operator_c, variable_name)
       
  1753 // void *visit_expression_type_c::visit(il_assign_operator_c *symbol, variable_name);
       
  1754 
       
  1755 /* Symbol class handled together with function call checks */
       
  1756 /*| [NOT] any_identifier SENDTO */
       
  1757 // SYM_REF2(il_assign_out_operator_c, option, variable_name)
       
  1758 // void *visit_expression_type_c::visit(il_assign_operator_c *symbol, option, variable_name);
       
  1759 
       
  1760 
       
  1761 
       
  1762 
       
  1763 
       
  1764 /***************************************/
       
  1765 /* B.3 - Language ST (Structured Text) */
       
  1766 /***************************************/
       
  1767 /***********************/
       
  1768 /* B 3.1 - Expressions */
       
  1769 /***********************/
       
  1770 
       
  1771 void *visit_expression_type_c::visit(or_expression_c *symbol) {
       
  1772   symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this));
       
  1773   symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
       
  1774   return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_BIT_compatible, symbol->l_exp, symbol->r_exp);
       
  1775 }
       
  1776 
       
  1777 
       
  1778 void *visit_expression_type_c::visit(xor_expression_c *symbol) {
       
  1779   symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this));
       
  1780   symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
       
  1781   return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_BIT_compatible, symbol->l_exp, symbol->r_exp);
       
  1782 }
       
  1783 
       
  1784 
       
  1785 void *visit_expression_type_c::visit(and_expression_c *symbol) {
       
  1786   symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this));
       
  1787   symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
       
  1788   return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_BIT_compatible, symbol->l_exp, symbol->r_exp);
       
  1789 }
       
  1790 
       
  1791 
       
  1792 void *visit_expression_type_c::visit(equ_expression_c *symbol) {
       
  1793   symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this));
       
  1794   symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
       
  1795   compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_ELEMENTARY_OR_ENUMERATED_compatible, symbol->l_exp, symbol->r_exp);
       
  1796   return &search_expression_type_c::bool_type_name;
       
  1797 }
       
  1798 
       
  1799 
       
  1800 void *visit_expression_type_c::visit(notequ_expression_c *symbol)  {
       
  1801   symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this));
       
  1802   symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
       
  1803   compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_ELEMENTARY_OR_ENUMERATED_compatible, symbol->l_exp, symbol->r_exp);
       
  1804   return &search_expression_type_c::bool_type_name;
       
  1805 }
       
  1806 
       
  1807 
       
  1808 void *visit_expression_type_c::visit(lt_expression_c *symbol) {
       
  1809   symbol_c *left_type  = base_type((symbol_c *)symbol->l_exp->accept(*this));
       
  1810   symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
       
  1811   compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible, symbol->l_exp, symbol->r_exp);
       
  1812   return &search_expression_type_c::bool_type_name;
       
  1813 }
       
  1814 
       
  1815 
       
  1816 void *visit_expression_type_c::visit(gt_expression_c *symbol) {
       
  1817   symbol_c *left_type  = base_type((symbol_c *)symbol->l_exp->accept(*this));
       
  1818   symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
       
  1819   compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible, symbol->l_exp, symbol->r_exp);
       
  1820   return &search_expression_type_c::bool_type_name;
       
  1821 }
       
  1822 
       
  1823 
       
  1824 void *visit_expression_type_c::visit(le_expression_c *symbol) {
       
  1825   symbol_c *left_type  = base_type((symbol_c *)symbol->l_exp->accept(*this));
       
  1826   symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
       
  1827   compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible, symbol->l_exp, symbol->r_exp);
       
  1828   return &search_expression_type_c::bool_type_name;
       
  1829 }
       
  1830 
       
  1831 
       
  1832 void *visit_expression_type_c::visit(ge_expression_c *symbol) {
       
  1833   symbol_c *left_type  = base_type((symbol_c *)symbol->l_exp->accept(*this));
       
  1834   symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
       
  1835   compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible, symbol->l_exp, symbol->r_exp);
       
  1836   return &search_expression_type_c::bool_type_name;
       
  1837 }
       
  1838 
       
  1839 
       
  1840 void *visit_expression_type_c::visit(add_expression_c *symbol) {
       
  1841   symbol_c *left_type  = base_type((symbol_c *)symbol->l_exp->accept(*this));
       
  1842   symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
       
  1843 
       
  1844 /* The following is already checked in compute_expression */
       
  1845 /*
       
  1846   if (is_type(left_type, time_type_name_c) && is_type(right_type, time_type_name_c)) 
       
  1847     return (void *)&time_type_name;
       
  1848 */
       
  1849 
       
  1850   if (is_type(left_type, tod_type_name_c)      && is_type(right_type, time_type_name_c)) 
       
  1851     return (void *)&tod_type_name;
       
  1852   if (is_type(left_type, safetod_type_name_c)  && is_type(right_type, time_type_name_c)) 
       
  1853     return (void *)&tod_type_name;
       
  1854   if (is_type(left_type, tod_type_name_c)      && is_type(right_type, safetime_type_name_c)) 
       
  1855     return (void *)&tod_type_name;
       
  1856   if (is_type(left_type, safetod_type_name_c)  && is_type(right_type, safetime_type_name_c)) 
       
  1857     return (void *)&safetod_type_name;
       
  1858 
       
  1859   if (is_type(left_type, dt_type_name_c)       && is_type(right_type, time_type_name_c)) 
       
  1860     return (void *)&dt_type_name;
       
  1861   if (is_type(left_type, safedt_type_name_c)   && is_type(right_type, time_type_name_c)) 
       
  1862     return (void *)&dt_type_name;
       
  1863   if (is_type(left_type, dt_type_name_c)       && is_type(right_type, safetime_type_name_c)) 
       
  1864     return (void *)&dt_type_name;
       
  1865   if (is_type(left_type, safedt_type_name_c)   && is_type(right_type, safetime_type_name_c)) 
       
  1866     return (void *)&safedt_type_name;
       
  1867 
       
  1868   return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_MAGNITUDE_compatible, symbol->l_exp, symbol->r_exp);
       
  1869 }
       
  1870 
       
  1871 
       
  1872 void *visit_expression_type_c::visit(sub_expression_c *symbol) {
       
  1873   symbol_c *left_type  = base_type((symbol_c *)symbol->l_exp->accept(*this));
       
  1874   symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
       
  1875 
       
  1876 /* The following is already checked in compute_expression */
       
  1877 /*
       
  1878   if (is_type(left_type, time_type_name_c) && is_type(right_type, time_type_name_c))
       
  1879     return (void *)&time_type_name;
       
  1880 */
       
  1881 
       
  1882   if (is_type(left_type, tod_type_name_c)     && is_type(right_type, time_type_name_c))
       
  1883     return (void *)&tod_type_name;
       
  1884   if (is_type(left_type, safetod_type_name_c) && is_type(right_type, time_type_name_c))
       
  1885     return (void *)&tod_type_name;
       
  1886   if (is_type(left_type, tod_type_name_c)     && is_type(right_type, safetime_type_name_c))
       
  1887     return (void *)&tod_type_name;
       
  1888   if (is_type(left_type, safetod_type_name_c) && is_type(right_type, safetime_type_name_c))
       
  1889     return (void *)&safetod_type_name;
       
  1890 
       
  1891   if (is_type(left_type, dt_type_name_c)     && is_type(right_type, time_type_name_c))
       
  1892     return (void *)&dt_type_name;
       
  1893   if (is_type(left_type, safedt_type_name_c) && is_type(right_type, time_type_name_c))
       
  1894     return (void *)&dt_type_name;
       
  1895   if (is_type(left_type, dt_type_name_c)     && is_type(right_type, safetime_type_name_c))
       
  1896     return (void *)&dt_type_name;
       
  1897   if (is_type(left_type, safedt_type_name_c) && is_type(right_type, safetime_type_name_c))
       
  1898     return (void *)&safedt_type_name;
       
  1899 
       
  1900   if (is_type(left_type, tod_type_name_c)     && is_type(right_type, tod_type_name_c))
       
  1901     return (void *)&time_type_name;
       
  1902   if (is_type(left_type, safetod_type_name_c) && is_type(right_type, tod_type_name_c))
       
  1903     return (void *)&time_type_name;
       
  1904   if (is_type(left_type, tod_type_name_c)     && is_type(right_type, safetod_type_name_c))
       
  1905     return (void *)&time_type_name;
       
  1906   if (is_type(left_type, safetod_type_name_c) && is_type(right_type, safetod_type_name_c))
       
  1907     return (void *)&safetime_type_name;
       
  1908 
       
  1909   if (is_type(left_type, date_type_name_c)     && is_type(right_type, date_type_name_c))
       
  1910     return (void *)&time_type_name;
       
  1911   if (is_type(left_type, safedate_type_name_c) && is_type(right_type, date_type_name_c))
       
  1912     return (void *)&time_type_name;
       
  1913   if (is_type(left_type, date_type_name_c)     && is_type(right_type, safedate_type_name_c))
       
  1914     return (void *)&time_type_name;
       
  1915   if (is_type(left_type, safedate_type_name_c) && is_type(right_type, safedate_type_name_c))
       
  1916     return (void *)&safetime_type_name;
       
  1917 
       
  1918   if (is_type(left_type, dt_type_name_c)     && is_type(right_type, dt_type_name_c))
       
  1919     return (void *)&time_type_name;
       
  1920   if (is_type(left_type, safedt_type_name_c) && is_type(right_type, dt_type_name_c))
       
  1921     return (void *)&time_type_name;
       
  1922   if (is_type(left_type, dt_type_name_c)     && is_type(right_type, safedt_type_name_c))
       
  1923     return (void *)&time_type_name;
       
  1924   if (is_type(left_type, safedt_type_name_c) && is_type(right_type, safedt_type_name_c))
       
  1925     return (void *)&safetime_type_name;
       
  1926 
       
  1927   return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_MAGNITUDE_compatible, symbol->l_exp, symbol->r_exp);
       
  1928 }
       
  1929 
       
  1930 
       
  1931 void *visit_expression_type_c::visit(mul_expression_c *symbol) {
       
  1932   symbol_c *left_type  = base_type((symbol_c *)symbol->l_exp->accept(*this));
       
  1933   symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
       
  1934 
       
  1935   if (is_type(left_type, time_type_name_c)     && is_ANY_NUM_compatible(right_type)) 
       
  1936     return (void *)&time_type_name;
       
  1937   if (is_type(left_type, safetime_type_name_c) && is_ANY_NUM_type(right_type)) 
       
  1938     return (void *)&time_type_name;
       
  1939   if (is_type(left_type, safetime_type_name_c) && is_ANY_SAFENUM_type(right_type)) 
       
  1940     return (void *)&safetime_type_name;
       
  1941   /* Since we have already checked for ANY_NUM_type and ANY_SAFENUM_type in the previous lines,
       
  1942    * this next line is really only to check for integers/reals of undefined type on 'right_type'... 
       
  1943    */
       
  1944   if (is_type(left_type, safetime_type_name_c) && is_ANY_NUM_compatible(right_type)) 
       
  1945     return (void *)&safetime_type_name;
       
  1946 
       
  1947   return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_NUM_compatible, symbol->l_exp, symbol->r_exp);
       
  1948 }
       
  1949 
       
  1950 
       
  1951 void *visit_expression_type_c::visit(div_expression_c *symbol) {
       
  1952   symbol_c *left_type  = base_type((symbol_c *)symbol->l_exp->accept(*this));
       
  1953   symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
       
  1954 
       
  1955   if (is_type(left_type, time_type_name_c)     && is_ANY_NUM_compatible(right_type)) 
       
  1956     return (void *)&time_type_name;
       
  1957   if (is_type(left_type, safetime_type_name_c) && is_ANY_NUM_type(right_type)) 
       
  1958     return (void *)&time_type_name;
       
  1959   if (is_type(left_type, safetime_type_name_c) && is_ANY_SAFENUM_type(right_type)) 
       
  1960     return (void *)&safetime_type_name;
       
  1961   /* Since we have already checked for ANY_NUM_type and ANY_SAFENUM_type in the previous lines,
       
  1962    * this next line is really only to check for integers/reals of undefined type on 'right_type'... 
       
  1963    */
       
  1964   if (is_type(left_type, safetime_type_name_c) && is_ANY_NUM_compatible(right_type)) 
       
  1965     return (void *)&safetime_type_name;
       
  1966 
       
  1967   return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_NUM_compatible, symbol->l_exp, symbol->r_exp);
       
  1968 }
       
  1969 
       
  1970 
       
  1971 void *visit_expression_type_c::visit(mod_expression_c *symbol) {
       
  1972   symbol_c *left_type  = base_type((symbol_c *)symbol->l_exp->accept(*this));
       
  1973   symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
       
  1974   return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_INT_compatible, symbol->l_exp, symbol->r_exp);
       
  1975 }
       
  1976 
       
  1977 
       
  1978 void *visit_expression_type_c::visit(power_expression_c *symbol) {
       
  1979   symbol_c *left_type  = base_type((symbol_c *)symbol->l_exp->accept(*this));
       
  1980   symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
       
  1981   if (!is_ANY_REAL_compatible(left_type))
       
  1982     STAGE3_ERROR(symbol->l_exp, symbol->l_exp, "first operand of ** operator has invalid data type, should be of type ANY_REAL.");
       
  1983   if (!is_ANY_NUM_compatible(right_type))
       
  1984     STAGE3_ERROR(symbol->r_exp, symbol->r_exp, "second operand of ** operator has invalid data type, should be of type ANY_NUM.");
       
  1985 
       
  1986   return (void *)left_type;
       
  1987 }
       
  1988 
       
  1989 
       
  1990 void *visit_expression_type_c::visit(neg_expression_c *symbol) {
       
  1991   symbol_c *exp_type = base_type((symbol_c *)symbol->exp->accept(*this));
       
  1992   if (!is_ANY_MAGNITUDE_compatible(exp_type))
       
  1993     STAGE3_ERROR(symbol, symbol, "operand of negate expression '-' has invalid data type, should be of type ANY_MAGNITUDE.");
       
  1994 
       
  1995   return exp_type;
       
  1996 }
       
  1997 
       
  1998 
       
  1999 void *visit_expression_type_c::visit(not_expression_c *symbol) {
       
  2000   symbol_c *type = base_type((symbol_c *)symbol->exp->accept(*this));
       
  2001   return compute_expression(type, type, &visit_expression_type_c::is_ANY_BIT_compatible, NULL, symbol->exp);
       
  2002 }
       
  2003 
       
  2004 
       
  2005 void *visit_expression_type_c::visit(function_invocation_c *symbol) {
       
  2006   function_symtable_t::iterator lower = function_symtable.lower_bound(symbol->function_name);
       
  2007   function_symtable_t::iterator upper = function_symtable.upper_bound(symbol->function_name);
       
  2008   if (lower == function_symtable.end()) ERROR;
       
  2009 
       
  2010   function_symtable_t::iterator second = lower;
       
  2011   second++;
       
  2012   if (second == upper) {
       
  2013     /* call to a function that is not overloaded. */	  
       
  2014     /* now check the semantics of the function call... */
       
  2015     /* If the syntax parser is working correctly, exactly one of the 
       
  2016      * following two symbols will be NULL, while the other is != NULL.
       
  2017      */
       
  2018     function_declaration_c *f_decl = function_symtable.get_value(lower);
       
  2019     if (symbol->   formal_param_list != NULL) check_formal_call   (symbol, f_decl);
       
  2020     if (symbol->nonformal_param_list != NULL) check_nonformal_call(symbol, f_decl);
       
  2021     /* Store the pointer to the declaration of the function being called.
       
  2022      * This data will be used by stage 4 to call the correct function.
       
  2023      * Mostly needed to disambiguate overloaded functions...
       
  2024      * See comments in absyntax.def for more details
       
  2025      */
       
  2026     symbol->called_function_declaration = f_decl;
       
  2027     return base_type(f_decl->type_name);
       
  2028   }  
       
  2029 
       
  2030   /* This is a call to an overloaded function... */
       
  2031   if (debug) printf("visit_expression_type_c::visit(function_invocation_c *symbol): FOUND CALL TO OVERLOADED FUNCTION!!\n");
       
  2032   for(; lower != upper; lower++) {
       
  2033     if (debug) printf("visit_expression_type_c::visit(function_invocation_c *symbol): FOUND CALL TO OVERLOADED FUNCTION!! iterating...\n");
       
  2034     int error_count = 0; 
       
  2035     function_declaration_c *f_decl = function_symtable.get_value(lower);
       
  2036     if (symbol->   formal_param_list != NULL) check_formal_call   (symbol, f_decl, &error_count);
       
  2037     if (symbol->nonformal_param_list != NULL) check_nonformal_call(symbol, f_decl, false, &error_count);
       
  2038     if (0 == error_count) {
       
  2039       /* Store the pointer to the declaration of the function being called.
       
  2040        * This data will be used by stage 4 to call the correct function.
       
  2041        * Mostly needed to disambiguate overloaded functions...
       
  2042        * See comments in absyntax.def for more details
       
  2043        */
       
  2044       symbol->called_function_declaration = f_decl;
       
  2045       return base_type(f_decl->type_name);
       
  2046     }
       
  2047   }
       
  2048 
       
  2049   /* No compatible function was found for this function call */
       
  2050   STAGE3_ERROR(symbol, symbol, "Call to an overloaded function with invalid parameter type.");
       
  2051   return NULL;
       
  2052 }
       
  2053 
       
  2054 /********************/
       
  2055 /* B 3.2 Statements */
       
  2056 /********************/
       
  2057 // SYM_LIST(statement_list_c)
       
  2058 /* The visitor of the base class search_visitor_c will handle calling each instruction in the list.
       
  2059  * We do not need to do anything here...
       
  2060  */
       
  2061 // void *visit_expression_type_c::visit(statement_list_c *symbol)
       
  2062 
       
  2063 
       
  2064 /*********************************/
       
  2065 /* B 3.2.1 Assignment Statements */
       
  2066 /*********************************/
       
  2067 
       
  2068 void *visit_expression_type_c::visit(assignment_statement_c *symbol) {
       
  2069   symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this));
       
  2070   symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
       
  2071 
       
  2072   if (debug) {
       
  2073     printf("visit_expression_type_c::visit(assignment_statement_c) called. Checking --->");  
       
  2074     symbolic_variable_c *hi = dynamic_cast<symbolic_variable_c *>(symbol->l_exp);  
       
  2075     if (hi != NULL) {
       
  2076       identifier_c *hi1 = dynamic_cast<identifier_c *>(hi->var_name);  
       
  2077       if (hi1 != NULL) printf("%s", hi1->value);
       
  2078     }
       
  2079     printf(" := ");
       
  2080     hex_integer_c *hi2 = dynamic_cast<hex_integer_c *>(symbol->r_exp);  
       
  2081     if (hi2 != NULL) printf("%s", hi2->value);
       
  2082     printf("\n");
       
  2083   } // if (debug)
       
  2084 
       
  2085   if        (NULL == left_type) {
       
  2086     STAGE3_ERROR(symbol->l_exp, symbol->l_exp, "Could not determine data type of expression (undefined variable, constant, or structure element?).\n");
       
  2087   } else if (NULL == right_type) {
       
  2088     STAGE3_ERROR(symbol->r_exp, symbol->r_exp, "Could not determine data type of expression (undefined variable, constant, or structure element?).\n");
       
  2089   } else if (!is_valid_assignment(left_type, right_type))
       
  2090     STAGE3_ERROR(symbol, symbol, "data type mismatch in assignment statement!\n");
       
  2091 
       
  2092   return NULL;
       
  2093 }
       
  2094 
       
  2095 
       
  2096 
       
  2097 /*****************************************/
       
  2098 /* B 3.2.2 Subprogram Control Statements */
       
  2099 /*****************************************/
       
  2100 
       
  2101 /*  RETURN */
       
  2102 // SYM_REF0(return_statement_c)
       
  2103 
       
  2104 
       
  2105 /* fb_name '(' [param_assignment_list] ')' */
       
  2106 /* param_assignment_list -> may be NULL ! */
       
  2107 // SYM_REF3(fb_invocation_c, fb_name, formal_param_list, nonformal_param_list)
       
  2108 void *visit_expression_type_c::visit(fb_invocation_c *symbol) {
       
  2109   symbol_c *fb_decl = search_varfb_instance_type->get_basetype_decl(symbol->fb_name);
       
  2110     /* The following should never occur. The function block must be defined, 
       
  2111      * and the FB type being called MUST be in the symtable... 
       
  2112      * This was all already checked at stage 2!
       
  2113      */
       
  2114   if (NULL == fb_decl) ERROR;
       
  2115 
       
  2116   /* now check the semantics of the fb call... */
       
  2117   /* If the syntax parser is working correctly, exactly one of the 
       
  2118    * following two symbols will be NULL, while the other is != NULL.
       
  2119    */
       
  2120   if (symbol->   formal_param_list != NULL) check_formal_call   (symbol, fb_decl);
       
  2121   if (symbol->nonformal_param_list != NULL) check_nonformal_call(symbol, fb_decl);
       
  2122 
       
  2123   return NULL;
       
  2124 }
       
  2125 
       
  2126 
       
  2127 #if 0
       
  2128 /* helper symbol for fb_invocation */
       
  2129 /* param_assignment_list ',' param_assignment */
       
  2130 SYM_LIST(param_assignment_list_c)
       
  2131 
       
  2132 /*  variable_name ASSIGN expression */
       
  2133 SYM_REF2(input_variable_param_assignment_c, variable_name, expression)
       
  2134 
       
  2135 /* [NOT] variable_name '=>' variable */
       
  2136 SYM_REF3(output_variable_param_assignment_c, not_param, variable_name, variable)
       
  2137 
       
  2138 /* helper CLASS for output_variable_param_assignment */
       
  2139 SYM_REF0(not_paramassign_c)
       
  2140 #endif
       
  2141 
       
  2142 /********************************/
       
  2143 /* B 3.2.3 Selection Statements */
       
  2144 /********************************/
       
  2145 
       
  2146 /* IF expression THEN statement_list elseif_statement_list ELSE statement_list END_IF */
       
  2147 // SYM_REF4(if_statement_c, expression, statement_list, elseif_statement_list, else_statement_list)
       
  2148 void *visit_expression_type_c::visit(if_statement_c *symbol) {
       
  2149   symbol_c *expr_type = base_type((symbol_c*)symbol->expression->accept(*this));
       
  2150   if (!is_BOOL_type(expr_type)) STAGE3_ERROR(symbol->expression,symbol->expression,"IF conditional expression is not of boolean type.");
       
  2151   if (NULL != symbol->statement_list)
       
  2152     symbol->statement_list->accept(*this); 
       
  2153   if (NULL != symbol->elseif_statement_list)  
       
  2154     symbol->elseif_statement_list->accept(*this);
       
  2155   if (NULL != symbol->else_statement_list)  
       
  2156     symbol->else_statement_list->accept(*this);  
       
  2157   return NULL;
       
  2158 }
       
  2159 
       
  2160 /* helper symbol for if_statement */
       
  2161 // SYM_LIST(elseif_statement_list_c)
       
  2162 // void *visit_expression_type_c::visit(elseif_statement_list_c *symbol) { }
       
  2163 
       
  2164 /* helper symbol for elseif_statement_list */
       
  2165 /* ELSIF expression THEN statement_list    */
       
  2166 // SYM_REF2(elseif_statement_c, expression, statement_list)
       
  2167 void *visit_expression_type_c::visit(elseif_statement_c *symbol) {
       
  2168   symbol_c *elseif_expr_type = base_type((symbol_c*)symbol->expression->accept(*this));
       
  2169   if(!is_BOOL_type(elseif_expr_type)) STAGE3_ERROR(symbol->expression,symbol->expression,"ELSIF conditional expression is not of boolean type.");
       
  2170   if (NULL != symbol->statement_list)
       
  2171     symbol->statement_list->accept(*this); 
       
  2172   return NULL;
       
  2173 }
       
  2174 
       
  2175 
       
  2176 /* CASE expression OF case_element_list ELSE statement_list END_CASE */
       
  2177 // SYM_REF3(case_statement_c, expression, case_element_list, statement_list)
       
  2178 void *visit_expression_type_c::visit(case_statement_c *symbol) {
       
  2179   case_expression_type = base_type((symbol_c*)symbol->expression->accept(*this));
       
  2180   if (NULL != case_expression_type) {
       
  2181     if (NULL != symbol->case_element_list)
       
  2182       symbol->case_element_list->accept(*this);
       
  2183   }
       
  2184   if (NULL != symbol->statement_list)
       
  2185     symbol->statement_list->accept(*this);
       
  2186   return NULL;
       
  2187 }
       
  2188 
       
  2189 #if 0
       
  2190 /* helper symbol for case_statement */
       
  2191 // SYM_LIST(case_element_list_c)
       
  2192 // void *visit_expression_type_c::visit(case_element_list_c *symbol);
       
  2193 
       
  2194 /*  case_list ':' statement_list */
       
  2195 // SYM_REF2(case_element_c, case_list, statement_list)
       
  2196 void *visit_expression_type_c::visit(case_element_c *symbol);  
       
  2197 #endif
       
  2198 
       
  2199 // SYM_LIST(case_list_c)
       
  2200 void *visit_expression_type_c::visit(case_list_c *symbol) {
       
  2201   symbol_c *element_type;
       
  2202   for(int i = 0; i < symbol->n; i++) {
       
  2203     element_type = (symbol_c *)symbol->elements[i]->accept(*this);
       
  2204     if (NULL == element_type) {
       
  2205       STAGE3_ERROR(symbol->elements[i], symbol->elements[i], "Case list element has undefined data type.");
       
  2206     } else {
       
  2207       element_type = base_type(element_type);
       
  2208       if (NULL != element_type){
       
  2209         /* The CASE value is only used for comparison (and not assingment), so we only check for compatibility! */ 
       
  2210         if (!is_compatible_type(case_expression_type, element_type))
       
  2211           STAGE3_ERROR(symbol->elements[i], symbol->elements[i], "Invalid data type of case list element.");
       
  2212       }
       
  2213     }
       
  2214   }
       
  2215   return NULL;
       
  2216 }
       
  2217 
       
  2218 /********************************/
       
  2219 /* B 3.2.4 Iteration Statements */
       
  2220 /********************************/
       
  2221 
       
  2222 /*  FOR control_variable ASSIGN expression TO expression [BY expression] DO statement_list END_FOR */
       
  2223 // SYM_REF5(for_statement_c, control_variable, beg_expression, end_expression, by_expression, statement_list)
       
  2224 void *visit_expression_type_c::visit(for_statement_c *symbol) {
       
  2225   symbol_c *var_type = (symbol_c*)symbol->control_variable->accept(*this);
       
  2226   if (NULL == var_type) ERROR;
       
  2227   var_type = base_type(var_type);
       
  2228   if (NULL == var_type) ERROR;
       
  2229   // ASSIGN
       
  2230   symbol_c *beg_expr_type = base_type((symbol_c*)symbol->beg_expression->accept(*this));
       
  2231   if (NULL != beg_expr_type) {
       
  2232     /* The BEG value is assigned to the variable, so we check for assignment validity! */ 
       
  2233     if(!is_valid_assignment(var_type, beg_expr_type)) 
       
  2234       STAGE3_ERROR(symbol->beg_expression, symbol->beg_expression, "Data type mismatch between control variable and initial value.");
       
  2235   }
       
  2236   // TO
       
  2237   symbol_c *end_expr_type = base_type((symbol_c*)symbol->end_expression->accept(*this));
       
  2238   if (NULL != end_expr_type) { 
       
  2239     /* The TO value is only used for comparison, so we only check for compatibility! */ 
       
  2240     if(!is_compatible_type(var_type, end_expr_type)) 
       
  2241       STAGE3_ERROR(symbol->end_expression, symbol->end_expression, "Data type mismatch between control variable and final value.");
       
  2242   }
       
  2243   // BY
       
  2244   if(symbol->by_expression != NULL) {
       
  2245     symbol_c *by_expr_type = base_type((symbol_c*)symbol->by_expression->accept(*this));
       
  2246     if (NULL != end_expr_type) {   
       
  2247       /* The BY value is used in an expression (add, sub, ...), so we only check for compatibility! */ 
       
  2248       if(!is_compatible_type(var_type, by_expr_type)) 
       
  2249         STAGE3_ERROR(symbol->by_expression, symbol->by_expression, "Data type mismatch between control variable and BY value.");
       
  2250     }
       
  2251   }
       
  2252   // DO
       
  2253   if (NULL != symbol->statement_list)
       
  2254     symbol->statement_list->accept(*this); 
       
  2255   return NULL;
       
  2256 }
       
  2257 
       
  2258 
       
  2259 /*  WHILE expression DO statement_list END_WHILE */
       
  2260 // SYM_REF2(while_statement_c, expression, statement_list)
       
  2261 void *visit_expression_type_c::visit(while_statement_c *symbol) {
       
  2262   symbol_c *expr_type = base_type((symbol_c*)symbol->expression->accept(*this));
       
  2263   if (NULL != expr_type) {
       
  2264     if(!is_BOOL_type(expr_type)) 
       
  2265       STAGE3_ERROR(symbol->expression,symbol->expression,"WHILE conditional expression is not of boolean type.");
       
  2266   }
       
  2267  
       
  2268   if (NULL != symbol->statement_list)
       
  2269     symbol->statement_list->accept(*this); 
       
  2270   return NULL;
       
  2271 }
       
  2272 
       
  2273 /*  REPEAT statement_list UNTIL expression END_REPEAT */
       
  2274 // SYM_REF2(repeat_statement_c, statement_list, expression)
       
  2275 void *visit_expression_type_c::visit(repeat_statement_c *symbol) {
       
  2276   if (NULL != symbol->statement_list)
       
  2277     symbol->statement_list->accept(*this); 
       
  2278  
       
  2279   symbol_c *expr_type = base_type((symbol_c*)symbol->expression->accept(*this));
       
  2280   if (NULL != expr_type) {
       
  2281     if(!is_BOOL_type(expr_type)) 
       
  2282       STAGE3_ERROR(symbol->expression,symbol->expression,"REPEAT conditional expression is not of boolean type.");
       
  2283   }
       
  2284   return NULL;
       
  2285 }
       
  2286 
       
  2287 /*  EXIT */
       
  2288 // SYM_REF0(exit_statement_c)
       
  2289 
       
  2290 
       
  2291