stage3/visit_expression_type.cc
changeset 625 c0bda77b37a0
parent 412 aad38592bdde
parent 624 c2546c6e0cfa
child 626 9f2cefb98e60
equal deleted inserted replaced
412:aad38592bdde 625:c0bda77b37a0
     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 symbol_c *visit_expression_type_c::common_literal(symbol_c *first_type, symbol_c *second_type) {
       
   570   printf("common_literal: %d == %d, %d == %d, %d == %d\n",
       
   571 		 (int)is_ANY_INT_compatible(first_type),
       
   572 		 (int)is_ANY_INT_compatible(second_type),
       
   573 		 (int)is_ANY_REAL_compatible(first_type),
       
   574 		 (int)is_ANY_REAL_compatible(second_type),
       
   575 		 (int)is_ANY_BIT_compatible(first_type),
       
   576 		 (int)is_ANY_BIT_compatible(second_type));
       
   577   if ((is_ANY_INT_compatible(first_type) && is_ANY_INT_compatible(second_type)) ||
       
   578 	  (is_ANY_BIT_compatible(first_type) && is_ANY_BIT_compatible(second_type)))
       
   579 	 return &search_constant_type_c::integer;
       
   580   else if (is_ANY_REAL_compatible(first_type) && is_ANY_REAL_compatible(second_type))
       
   581 	 return &search_constant_type_c::real;
       
   582   return NULL;
       
   583 }
       
   584 
       
   585 symbol_c *visit_expression_type_c::overloaded_return_type(symbol_c *type) {
       
   586   if (is_ANY_INT_compatible(type))
       
   587     return &search_constant_type_c::ulint_type_name;
       
   588   else if (is_ANY_REAL_compatible(type))
       
   589     return &search_constant_type_c::lreal_type_name;
       
   590   else if (is_ANY_BIT_compatible(type))
       
   591       return &search_constant_type_c::lword_type_name;
       
   592   return NULL;
       
   593 }
       
   594 
       
   595 /* Return TRUE if the second (value) data type may be assigned to a variable of the first (variable) data type
       
   596  * such as: 
       
   597  *     var_type     value_type
       
   598  *    BOOL           BYTE#7     -> returns false
       
   599  *    INT            INT#7      -> returns true
       
   600  *    INT            7          -> returns true
       
   601  *    REAL           7.89       -> returns true
       
   602  *    REAL           7          -> returns true
       
   603  *    INT            7.89       -> returns false
       
   604  *    SAFEBOOL       BOOL#1     -> returns false   !!!
       
   605  *   etc...
       
   606  *
       
   607  * NOTE: It is assumed that the var_type is the data type of an lvalue
       
   608  */
       
   609 bool visit_expression_type_c::is_valid_assignment(symbol_c *var_type, symbol_c *value_type) {
       
   610   if (var_type == NULL)   {/* STAGE3_ERROR(value_type, value_type, "Var_type   == NULL"); */ return false;}
       
   611   if (value_type == NULL) {/* STAGE3_ERROR(var_type,   var_type,   "Value_type == NULL"); */ return false;}
       
   612 
       
   613   symbol_c *common_type = common_type__(var_type, value_type);
       
   614   if (NULL == common_type)
       
   615     return false;
       
   616   return (typeid(*var_type) == typeid(*common_type));
       
   617 }
       
   618 
       
   619 
       
   620 /* Return TRUE if there is a common data type, otherwise return FALSE
       
   621  * i.e., return TRUE if both data types may be used simultaneously in an expression
       
   622  * such as:
       
   623  *    BOOL#0     AND BYTE#7  -> returns false
       
   624  *    0          AND BYTE#7  -> returns true
       
   625  *    INT#10     AND INT#7   -> returns true
       
   626  *    INT#10     AND 7       -> returns true
       
   627  *    REAL#34.3  AND 7.89    -> returns true
       
   628  *    REAL#34.3  AND 7       -> returns true
       
   629  *    INT#10     AND 7.89    -> returns false
       
   630  *    SAFEBOOL#0 AND BOOL#1  -> returns true   !!!
       
   631  *   etc...
       
   632  */
       
   633 bool visit_expression_type_c::is_compatible_type(symbol_c *first_type, symbol_c *second_type) {
       
   634   if (first_type == NULL || second_type == NULL) {return false;}
       
   635   return (NULL != common_type__(first_type, second_type));
       
   636 }
       
   637 
       
   638 
       
   639 
       
   640 
       
   641 /* A helper function... */
       
   642 /*
       
   643 symbol_c *visit_expression_type_c::compute_boolean_expression(symbol_c *left_type, symbol_c *right_type,
       
   644                                                               is_data_type_t is_data_type) {
       
   645 */
       
   646 symbol_c *visit_expression_type_c::compute_expression(symbol_c *left_type,      symbol_c *right_type,     is_data_type_t is_data_type,
       
   647 						      symbol_c *left_expr, symbol_c *right_expr) {
       
   648   bool error = false;
       
   649 
       
   650   if (!(this->*is_data_type)(left_type)) {
       
   651     if (debug) printf("visit_expression_type_c::compute_expression(): invalid left_type\n");
       
   652     if (left_expr != NULL)
       
   653       STAGE3_ERROR(left_expr, left_expr, "Invalid data type of operand, or of data resulting from previous IL instructions.");
       
   654     error = true;
       
   655   }
       
   656   if (!(this->*is_data_type)(right_type)) {
       
   657     if (debug) printf("visit_expression_type_c::compute_expression(): invalid right_type\n");
       
   658     if (right_expr != NULL)
       
   659       STAGE3_ERROR(right_expr, right_expr, "Invalid data type of operand.");
       
   660     error = true;
       
   661   }
       
   662   if (!is_compatible_type(left_type, right_type)) {
       
   663     if (debug) printf("visit_expression_type_c::compute_expression(): left_type & right_type are incompatible\n");
       
   664     if ((left_expr != NULL) && (right_expr != NULL))
       
   665       STAGE3_ERROR(left_expr, right_expr, "Type mismatch between operands.");
       
   666     error = true;
       
   667   }
       
   668 
       
   669   if (error)
       
   670     return NULL;
       
   671   else
       
   672     return common_type(left_type, right_type);
       
   673 }
       
   674 
       
   675 
       
   676 
       
   677 
       
   678 /* A helper function... */
       
   679 /* check the semantics of a FB or Function non-formal call */
       
   680 /* e.g. foo(1, 2, 3, 4);  */
       
   681 /* If error_count pointer is != NULL, we do not really print out the errors,
       
   682  * but rather only count how many errors were found.
       
   683  * This is used to support overloaded functions, where we have to check each possible
       
   684  * function, one at a time, untill we find a function call without any errors.
       
   685  */
       
   686 void visit_expression_type_c::check_nonformal_call(symbol_c *f_call, symbol_c *f_decl, bool use_il_defvar, int *error_count) {
       
   687   symbol_c *call_param_value, *call_param_type, *param_type;
       
   688   identifier_c *param_name;
       
   689   function_param_iterator_c       fp_iterator(f_decl);
       
   690   function_call_param_iterator_c fcp_iterator(f_call);
       
   691   int extensible_parameter_highest_index = -1;
       
   692   
       
   693   /* reset error counter */
       
   694   if (error_count != NULL) *error_count = 0;
       
   695   /* if use_il_defvar, then the first parameter for the call comes from the il_default_variable */
       
   696   if (use_il_defvar) {
       
   697     /* The first parameter of the function corresponds to the il_default_variable_type of the function call */
       
   698     do {
       
   699       param_name = fp_iterator.next();
       
   700       if(param_name == NULL) break;
       
   701       /*  The EN and ENO parameters are default parameters.
       
   702        *  In the non-formal invocation of a function there can be no assignment of
       
   703        * values to these parameters. Therefore, we ignore the parameters declared
       
   704        * in the function.
       
   705        */
       
   706     } while ((strcmp(param_name->value, "EN") == 0) || (strcmp(param_name->value, "ENO") == 0));
       
   707     /* If the function does not have any parameters (param_name == NULL)
       
   708      * then we cannot compare its type with the il_default_variable_type.
       
   709      *
       
   710      * However, I (Mario) think this is invalid syntax, as it seems to me all functions must
       
   711      * have at least one parameter.
       
   712      * However, we will make this semantic verification consider it possible, as later
       
   713      * versions of the standard may change that syntax.
       
   714      * So, instead of generating a syntax error message, we simply check whether the call
       
   715      * is passing any more parameters besides the default variable (the il default variable may be ignored
       
   716      * in this case, and not consider it as being a parameter being passed to the function).
       
   717      * If it does, then we have found a semantic error, otherwise the function call is 
       
   718      * correct, and we simply return.
       
   719      */
       
   720     if(param_name == NULL) {
       
   721       if (fcp_iterator.next_nf() != NULL)
       
   722         STAGE3_ERROR(f_call, f_call, "Too many parameters in function/FB call.");
       
   723       return;
       
   724     } else { 
       
   725       /* param_name != NULL */
       
   726       param_type = fp_iterator.param_type();
       
   727       if(!is_valid_assignment(param_type, il_default_variable_type)) {
       
   728         if (error_count != NULL) (*error_count)++;
       
   729         else STAGE3_ERROR(f_call, f_call, "In function/FB call, first parameter has invalid data type.");
       
   730       }
       
   731     }
       
   732     
       
   733     /* the fisrt parameter (il_def_variable) is correct */
       
   734     if (extensible_parameter_highest_index < fp_iterator.extensible_param_index()) {
       
   735       extensible_parameter_highest_index = fp_iterator.extensible_param_index();
       
   736     }
       
   737   } // if (use_il_defvar)
       
   738   
       
   739   
       
   740 
       
   741   /* Iterating through the non-formal parameters of the function call */
       
   742   while((call_param_value = fcp_iterator.next_nf()) != NULL) {
       
   743     /* Obtaining the type of the value being passed in the function call */
       
   744     call_param_type = base_type((symbol_c*)call_param_value->accept(*this));
       
   745     if (call_param_type == NULL) {
       
   746       if (error_count != NULL) (*error_count)++;
       
   747       /* the following error will usually occur when ST code uses an identifier, that could refer to an enumerated constant,
       
   748        * but was not actually used as a constant in any definitions of an enumerated data type
       
   749        */
       
   750       else STAGE3_ERROR(call_param_value, call_param_value, "Could not determine data type of value being passed in function/FB call.");
       
   751       continue;
       
   752     }  
       
   753     
       
   754     /* Iterate to the next parameter of the function being called.
       
   755      * Get the name of that parameter, and ignore if EN or ENO.
       
   756      */
       
   757     do {
       
   758       param_name = fp_iterator.next();
       
   759       /* If there is no other parameter declared, then we are passing too many parameters... */
       
   760       if(param_name == NULL) {
       
   761         if (error_count != NULL) (*error_count)++;
       
   762         /* Note: We don't want to print out the follwoing error message multiple times, so we return instead of continuing with 'break' */
       
   763         else STAGE3_ERROR(f_call, f_call, "Too many parameters in function/FB call."); return;
       
   764       }
       
   765     } while ((strcmp(param_name->value, "EN") == 0) || (strcmp(param_name->value, "ENO") == 0));
       
   766 
       
   767     /* Get the parameter type */
       
   768     param_type = base_type(fp_iterator.param_type());
       
   769     /* If the declared parameter and the parameter from the function call do not have the same type */
       
   770     if(!is_valid_assignment(param_type, call_param_type)) {
       
   771       if (error_count != NULL) (*error_count)++;
       
   772       else STAGE3_ERROR(call_param_value, call_param_value, "Type mismatch in function/FB call parameter.");
       
   773     }
       
   774 
       
   775     if (extensible_parameter_highest_index < fp_iterator.extensible_param_index()) {
       
   776       extensible_parameter_highest_index = fp_iterator.extensible_param_index();
       
   777     }
       
   778   }
       
   779   
       
   780   /* The function call may not have any errors! */
       
   781   /* In the case of a call to an extensible function, we store the highest index 
       
   782    * of the extensible parameters this particular call uses, in the symbol_c object
       
   783    * of the function call itself!
       
   784    * In calls to non-extensible functions, this value will be set to -1.
       
   785    * This information is later used in stage4 to correctly generate the
       
   786    * output code.
       
   787    */
       
   788   int extensible_param_count = -1;
       
   789   if (extensible_parameter_highest_index >=0) /* if call to extensible function */
       
   790     extensible_param_count = 1 + extensible_parameter_highest_index - fp_iterator.first_extensible_param_index();
       
   791   il_function_call_c     *il_function_call = dynamic_cast<il_function_call_c *>(f_call);
       
   792   function_invocation_c  *function_invocation  = dynamic_cast<function_invocation_c  *>(f_call);
       
   793   if      (il_function_call     != NULL) il_function_call   ->extensible_param_count = extensible_param_count;
       
   794   else if (function_invocation  != NULL) function_invocation->extensible_param_count = extensible_param_count;
       
   795   //   else ERROR;  /* this function is also called by Function Blocks, so this is not an error! */
       
   796 }
       
   797 
       
   798 
       
   799 /* check semantics of FB call in the IL language using input operators */
       
   800 /* e.g. CU, CLK, IN, PT, SR, ...                                       */
       
   801 void visit_expression_type_c::check_il_fbcall(symbol_c *il_operator, const char *il_operator_str) {
       
   802   symbol_c *call_param_type = il_default_variable_type;
       
   803   symbol_c *fb_decl = il_operand_type;
       
   804     /* The following should never occur. The function block must be defined, 
       
   805      * and the FB type being called MUST be in the symtable... 
       
   806      * This was all already checked at stage 2!
       
   807      */
       
   808   if (NULL == fb_decl) ERROR;
       
   809   if (call_param_type == NULL) ERROR;
       
   810 
       
   811   /* We also create an identifier_c object, so we can later use it to find the equivalent FB parameter */
       
   812   /* Note however that this symbol does not have the correct location (file name and line numbers) 
       
   813    * so any error messages must use the il_operator symbol to generate the error location
       
   814    */
       
   815   identifier_c call_param_name(il_operator_str);
       
   816 
       
   817   /* Obtaining the type of the value being passed in the function call */
       
   818   call_param_type = base_type(call_param_type);
       
   819   if (call_param_type == NULL) STAGE3_ERROR(il_operator, il_operator, "Could not determine data type of value being passed in FB call.");
       
   820 
       
   821   /* Find the corresponding parameter of the function being called */
       
   822   function_param_iterator_c fp_iterator(fb_decl);
       
   823   if(fp_iterator.search(&call_param_name) == NULL) {
       
   824     STAGE3_ERROR(il_operand, il_operand, "Called FB does not have an input parameter named %s.", il_operator_str);
       
   825   } else {
       
   826     /* Get the parameter type */
       
   827     symbol_c *param_type = base_type(fp_iterator.param_type());
       
   828     /* If the declared parameter and the parameter from the function call have the same type */
       
   829     if(!is_valid_assignment(param_type, call_param_type)) STAGE3_ERROR(il_operator, il_operator, "Type mismatch in FB call parameter.");
       
   830   }
       
   831 }
       
   832 
       
   833 
       
   834 /* A helper function... */
       
   835 /* check the semantics of a FB or Function formal call */
       
   836 /* e.g. foo(IN1 := 1, OUT1 =>x, EN := true);  */
       
   837 /* If error_count pointer is != NULL, we do not really print out the errors,
       
   838  * but rather only count how many errors were found.
       
   839  * This is used to support overloaded functions, where we have to check each possible
       
   840  * function, one at a time, untill we find a function call without any errors.
       
   841  */
       
   842 void visit_expression_type_c::check_formal_call(symbol_c *f_call, symbol_c *f_decl, int *error_count) {
       
   843   symbol_c *call_param_value, *call_param_type, *call_param_name, *param_type;
       
   844   symbol_c *verify_duplicate_param;
       
   845   identifier_c *param_name;
       
   846   function_param_iterator_c       fp_iterator(f_decl);
       
   847   function_call_param_iterator_c fcp_iterator(f_call);
       
   848   int extensible_parameter_highest_index = -1;
       
   849   identifier_c *extensible_parameter_name;
       
   850 
       
   851   /* reset error counter */
       
   852   if (error_count != NULL) *error_count = 0;
       
   853 
       
   854   /* Iterating through the formal parameters of the function call */
       
   855   while((call_param_name = fcp_iterator.next_f()) != NULL) {
       
   856         
       
   857     /* Obtaining the value being passed in the function call */
       
   858     call_param_value = fcp_iterator.get_current_value();
       
   859     /* the following should never occur. If it does, then we have a bug in our code... */
       
   860     if (NULL == call_param_value) ERROR;
       
   861 
       
   862     /* Checking if there are duplicated parameter values */
       
   863     verify_duplicate_param = fcp_iterator.search_f(call_param_name);
       
   864     if(verify_duplicate_param != call_param_value){
       
   865       if (error_count != NULL) (*error_count)++;
       
   866       else STAGE3_ERROR(call_param_name, verify_duplicate_param, "Duplicated parameter values.");
       
   867     }   
       
   868 
       
   869     /* Obtaining the type of the value being passed in the function call */
       
   870     call_param_type = (symbol_c*)call_param_value->accept(*this);
       
   871     if (call_param_type == NULL) {
       
   872       if (error_count != NULL) (*error_count)++;
       
   873       else STAGE3_ERROR(call_param_name, call_param_value, "Could not determine data type of value being passed in function/FB call.");
       
   874       /* The data value being passed is possibly any enumerated type value.
       
   875        * We do not yet handle semantic verification of enumerated types.
       
   876        */
       
   877       ERROR;
       
   878     }
       
   879     call_param_type = base_type(call_param_type);
       
   880     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.");
       
   881 
       
   882     /* Find the corresponding parameter of the function being called */
       
   883     param_name = fp_iterator.search(call_param_name);
       
   884     if(param_name == NULL) {
       
   885       if (error_count != NULL) (*error_count)++;
       
   886       else STAGE3_ERROR(call_param_name, call_param_name, "Invalid parameter in function/FB call.");
       
   887     } else {
       
   888       /* Get the parameter type */
       
   889       param_type = base_type(fp_iterator.param_type());
       
   890       /* If the declared parameter and the parameter from the function call have the same type */
       
   891       if(!is_valid_assignment(param_type, call_param_type)) {
       
   892         if (error_count != NULL) (*error_count)++;
       
   893         else STAGE3_ERROR(call_param_name, call_param_value, "Type mismatch function/FB call parameter.");
       
   894       }
       
   895       if (extensible_parameter_highest_index < fp_iterator.extensible_param_index()) {
       
   896         extensible_parameter_highest_index = fp_iterator.extensible_param_index();
       
   897         extensible_parameter_name = param_name;
       
   898       }
       
   899     }
       
   900   }
       
   901   
       
   902   /* In the case of a call to an extensible function, we store the highest index 
       
   903    * of the extensible parameters this particular call uses, in the symbol_c object
       
   904    * of the function call itself!
       
   905    * In calls to non-extensible functions, this value will be set to -1.
       
   906    * This information is later used in stage4 to correctly generate the
       
   907    * output code.
       
   908    */
       
   909   int extensible_param_count = -1;
       
   910   if (extensible_parameter_highest_index >=0) /* if call to extensible function */
       
   911     extensible_param_count = 1 + extensible_parameter_highest_index - fp_iterator.first_extensible_param_index();
       
   912   il_formal_funct_call_c *il_formal_funct_call = dynamic_cast<il_formal_funct_call_c *>(f_call);
       
   913   function_invocation_c  *function_invocation  = dynamic_cast<function_invocation_c  *>(f_call);
       
   914   if      (il_formal_funct_call != NULL) il_formal_funct_call->extensible_param_count = extensible_param_count;
       
   915   else if (function_invocation  != NULL) function_invocation->extensible_param_count  = extensible_param_count;
       
   916 //   else ERROR;  /* this function is also called by Function Blocks, so this is not an error! */
       
   917 
       
   918   /* We have iterated through all the formal parameters of the function call,
       
   919    * and everything seems fine. 
       
   920    * If the function being called in an extensible function, we now check
       
   921    * whether the extensible paramters in the formal invocation do not skip
       
   922    * any indexes...
       
   923    *
       
   924    * f(in1:=0, in2:=0, in4:=0) --> ERROR!!
       
   925    */
       
   926   if (extensible_parameter_highest_index >=0) { /* if call to extensible function */
       
   927     for (int i=fp_iterator.first_extensible_param_index(); i < extensible_parameter_highest_index; i++) {
       
   928       char tmp[256];
       
   929       if (snprintf(tmp, 256, "%s%d", extensible_parameter_name->value, i) >= 256) ERROR;
       
   930       if (fcp_iterator.search_f(tmp) == NULL) {
       
   931         /* error in invocation of extensible function */
       
   932         if (error_count != NULL) (*error_count)++;
       
   933         else STAGE3_ERROR(f_call, f_call, "Missing extensible parameters in call to extensible function.");
       
   934       }  
       
   935     }    
       
   936   }  
       
   937 }
       
   938 
       
   939 
       
   940 
       
   941 
       
   942 /* a helper function... */
       
   943 symbol_c *visit_expression_type_c::base_type(symbol_c *symbol) {
       
   944   /* NOTE: symbol == NULL is valid. It will occur when, for e.g., an undefined/undeclared symbolic_variable is used
       
   945    *       in the code.
       
   946    */
       
   947   if (symbol == NULL) return NULL;
       
   948   return (symbol_c *)symbol->accept(search_base_type);
       
   949 }
       
   950 
       
   951 
       
   952 /* a helper function... */
       
   953 void *visit_expression_type_c::verify_null(symbol_c *symbol){
       
   954   if(il_default_variable_type == NULL){
       
   955     STAGE3_ERROR(symbol, symbol, "Missing LD instruction (or equivalent) before this instruction.");
       
   956   }
       
   957   if(il_operand_type == NULL){
       
   958     STAGE3_ERROR(symbol, symbol, "This instruction requires an operand.");
       
   959   }
       
   960   return NULL;
       
   961 }
       
   962 
       
   963 
       
   964 /********************************/
       
   965 /* B 1.3.3 - Derived data types */
       
   966 /********************************/
       
   967 void *visit_expression_type_c::visit(data_type_declaration_c *symbol) {
       
   968   // TODO !!!
       
   969   /* for the moment we must return NULL so semantic analysis of remaining code is not interrupted! */
       
   970   return NULL;
       
   971 }
       
   972 
       
   973 
       
   974 /*********************/
       
   975 /* B 1.4 - Variables */
       
   976 /*********************/
       
   977 
       
   978 void *visit_expression_type_c::visit(symbolic_variable_c *symbol) {
       
   979   return search_varfb_instance_type->get_basetype_decl(symbol);
       
   980 }
       
   981 
       
   982 /********************************************/
       
   983 /* B 1.4.1 - Directly Represented Variables */
       
   984 /********************************************/
       
   985 void *visit_expression_type_c::visit(direct_variable_c *symbol) {
       
   986   switch (symbol->value[2]) {
       
   987     case 'X': // bit - 1 bit
       
   988       return (void *)&bool_type_name;
       
   989     case 'B': // byte - 8 bits
       
   990       return (void *)&byte_type_name;
       
   991     case 'W': // word - 16 bits
       
   992       return (void *)&word_type_name;
       
   993     case 'D': // double word - 32 bits
       
   994       return (void *)&dword_type_name;
       
   995     case 'L': // long word - 64 bits
       
   996       return (void *)&lword_type_name;
       
   997     default:  // if none of the above, then the empty string was used <=> boolean 
       
   998       return (void *)&bool_type_name;
       
   999    }
       
  1000 }
       
  1001 
       
  1002 /*************************************/
       
  1003 /* B 1.4.2 - Multi-element variables */
       
  1004 /*************************************/
       
  1005 void *visit_expression_type_c::visit(array_variable_c *symbol) {
       
  1006   return search_varfb_instance_type->get_basetype_decl(symbol);
       
  1007 }
       
  1008 
       
  1009 void *visit_expression_type_c::visit(structured_variable_c *symbol) {
       
  1010   return search_varfb_instance_type->get_basetype_decl(symbol);
       
  1011 }
       
  1012 
       
  1013 
       
  1014 
       
  1015 /********************************/
       
  1016 /* B 1.7 Configuration elements */
       
  1017 /********************************/
       
  1018 void *visit_expression_type_c::visit(configuration_declaration_c *symbol) {
       
  1019   // TODO !!!
       
  1020   /* for the moment we must return NULL so semantic analysis of remaining code is not interrupted! */
       
  1021   return NULL;
       
  1022 }
       
  1023 
       
  1024 
       
  1025 /****************************************/
       
  1026 /* B.2 - Language IL (Instruction List) */
       
  1027 /****************************************/
       
  1028 /***********************************/
       
  1029 /* B 2.1 Instructions and Operands */
       
  1030 /***********************************/
       
  1031 /*| instruction_list il_instruction */
       
  1032 /* The visitor of the base class search_visitor_c will handle calling each instruction in the list.
       
  1033  * We do not need to do anything here...
       
  1034  */
       
  1035 // void *visit_expression_type_c::visit(instruction_list_c *symbol)
       
  1036 
       
  1037 /* | label ':' [il_incomplete_instruction] eol_list */
       
  1038 //SYM_REF2(il_instruction_c, label, il_instruction)
       
  1039 // void *visit_expression_type_c::visit(il_instruction_c *symbol);
       
  1040 
       
  1041 
       
  1042 /* | il_simple_operator [il_operand] */
       
  1043 // SYM_REF2(il_simple_operation_c, il_simple_operator, il_operand)
       
  1044 void *visit_expression_type_c::visit(il_simple_operation_c *symbol) {
       
  1045   if (il_error)
       
  1046     return NULL;
       
  1047 
       
  1048   /* determine the data type of the operand */
       
  1049   il_operand = symbol->il_operand;
       
  1050   if (symbol->il_operand != NULL){
       
  1051     il_operand_type = base_type((symbol_c *)symbol->il_operand->accept(*this));
       
  1052   } else {
       
  1053     il_operand_type = NULL;
       
  1054   }
       
  1055   /* recursive call to see whether data types are compatible */
       
  1056   symbol->il_simple_operator->accept(*this);
       
  1057 
       
  1058   il_operand_type = NULL;
       
  1059   il_operand = NULL;
       
  1060   return NULL;
       
  1061 }
       
  1062 
       
  1063 // | function_name [il_operand_list] */
       
  1064 //SYM_REF2(il_function_call_c, function_name, il_operand_list)
       
  1065 void *visit_expression_type_c::visit(il_function_call_c *symbol) {
       
  1066   if (il_error)
       
  1067     return NULL;
       
  1068 
       
  1069   symbol_c *return_data_type = NULL;
       
  1070   symbol_c* fdecl_return_type;
       
  1071   symbol_c* overloaded_data_type = NULL;
       
  1072   int extensible_param_count = -1;
       
  1073   symbol->called_function_declaration = NULL;
       
  1074 
       
  1075   /* First find the declaration of the function being called! */
       
  1076   function_symtable_t::iterator lower = function_symtable.lower_bound(symbol->function_name);
       
  1077   function_symtable_t::iterator upper = function_symtable.upper_bound(symbol->function_name);
       
  1078   function_symtable_t::iterator current;
       
  1079   if (lower == function_symtable.end()) ERROR;
       
  1080 
       
  1081   int error_count = 0; 
       
  1082   int *error_count_ptr = NULL;
       
  1083 
       
  1084   function_symtable_t::iterator second = lower;
       
  1085   second++;
       
  1086   if (second != upper) 
       
  1087     /* This is a call to an overloaded function... */  
       
  1088     error_count_ptr = &error_count;
       
  1089 
       
  1090   for(current = lower; current != upper; current++) {
       
  1091     function_declaration_c *f_decl = function_symtable.get_value(current);
       
  1092     
       
  1093     check_nonformal_call(symbol, f_decl, true, error_count_ptr);
       
  1094     
       
  1095     if (0 == error_count) {
       
  1096       /* Either: 
       
  1097        * (i) we have a call to a non-overloaded function (error_cnt_ptr is NULL!, so error_count won't change!)  
       
  1098        * (ii) we have a call to an overloaded function, with no errors!
       
  1099        */
       
  1100 
       
  1101       fdecl_return_type = base_type(f_decl->type_name);
       
  1102 
       
  1103       if (symbol->called_function_declaration == NULL) {
       
  1104         /* Store the pointer to the declaration of the function being called.
       
  1105          * This data will be used by stage 4 to call the correct function.
       
  1106          * Mostly needed to disambiguate overloaded functions...
       
  1107          * See comments in absyntax.def for more details
       
  1108          */
       
  1109         symbol->called_function_declaration = f_decl;
       
  1110         extensible_param_count = symbol->extensible_param_count;
       
  1111 
       
  1112         /* determine the base data type returned by the function being called... */
       
  1113         return_data_type = fdecl_return_type;
       
  1114       }
       
  1115       else if (typeid(*return_data_type) != typeid(*fdecl_return_type)){
       
  1116         return_data_type = common_literal(return_data_type, fdecl_return_type);
       
  1117         overloaded_data_type = overloaded_return_type(return_data_type);
       
  1118       }
       
  1119       
       
  1120       if (NULL == return_data_type) ERROR;
       
  1121     }
       
  1122   }
       
  1123 
       
  1124   if (overloaded_data_type != NULL) {
       
  1125     for(current = lower; current != upper; current++) {
       
  1126       function_declaration_c *f_decl = function_symtable.get_value(current);
       
  1127 
       
  1128       /* check semantics of data passed in the function call... */
       
  1129       check_nonformal_call(symbol, f_decl, true, error_count_ptr);
       
  1130 
       
  1131       if (0 == error_count) {
       
  1132 
       
  1133         fdecl_return_type = base_type(f_decl->type_name);
       
  1134 
       
  1135         if (typeid(*overloaded_data_type) == typeid(*fdecl_return_type)){
       
  1136           /* Store the pointer to the declaration of the function being called.
       
  1137            * This data will be used by stage 4 to call the correct function.
       
  1138            * Mostly needed to disambiguate overloaded functions...
       
  1139            * See comments in absyntax.def for more details
       
  1140            */
       
  1141           symbol->called_function_declaration = f_decl;
       
  1142           extensible_param_count = symbol->extensible_param_count;
       
  1143         }
       
  1144       }
       
  1145     }
       
  1146   }
       
  1147 
       
  1148   if (NULL == return_data_type) {
       
  1149     /* No compatible function was found for this function call */
       
  1150     STAGE3_ERROR(symbol, symbol, "Call to an overloaded function with invalid parameter type.");
       
  1151   }
       
  1152   else {
       
  1153     symbol->extensible_param_count = extensible_param_count;
       
  1154     /* set the new data type of the default variable for the following verifications... */
       
  1155     il_default_variable_type = return_data_type;
       
  1156   }
       
  1157 
       
  1158   return NULL;
       
  1159 }
       
  1160 
       
  1161 
       
  1162 /* | il_expr_operator '(' [il_operand] eol_list [simple_instr_list] ')' */
       
  1163 // SYM_REF3(il_expression_c, il_expr_operator, il_operand, simple_instr_list);
       
  1164 void *visit_expression_type_c::visit(il_expression_c *symbol) {
       
  1165   if (il_error)
       
  1166     return NULL;
       
  1167 
       
  1168   symbol_c *il_default_variable_type_back = il_default_variable_type;
       
  1169 
       
  1170   il_parenthesis_level++;
       
  1171 
       
  1172   if(symbol->il_operand != NULL) {
       
  1173      il_default_variable_type = base_type((symbol_c *)symbol->il_operand->accept(*this));
       
  1174   } else {
       
  1175      il_default_variable_type = NULL;
       
  1176   }
       
  1177 
       
  1178   if(symbol->simple_instr_list != NULL) {
       
  1179     symbol->simple_instr_list->accept(*this);
       
  1180   }
       
  1181 
       
  1182   il_parenthesis_level--;
       
  1183   if (il_parenthesis_level < 0) ERROR;
       
  1184 
       
  1185   il_operand = symbol->simple_instr_list;
       
  1186   il_operand_type = il_default_variable_type;
       
  1187   il_default_variable_type = il_default_variable_type_back;
       
  1188 
       
  1189   /* Now check the if the data type semantics of operation are correct,
       
  1190    * but only if no previous error has been found...
       
  1191    */
       
  1192   if (!il_error)
       
  1193     symbol->il_expr_operator->accept(*this);
       
  1194 
       
  1195   il_operand_type = NULL;
       
  1196   il_operand = NULL;
       
  1197   return NULL;
       
  1198 }
       
  1199 
       
  1200 
       
  1201 #if 0
       
  1202 /*  il_jump_operator label */
       
  1203 SYM_REF2(il_jump_operation_c, il_jump_operator, label)
       
  1204 void *visit_expression_type_c::visit(il_jump_operation_c *symbol);
       
  1205 #endif
       
  1206 
       
  1207 
       
  1208 /*   il_call_operator prev_declared_fb_name
       
  1209  * | il_call_operator prev_declared_fb_name '(' ')'
       
  1210  * | il_call_operator prev_declared_fb_name '(' eol_list ')'
       
  1211  * | il_call_operator prev_declared_fb_name '(' il_operand_list ')'
       
  1212  * | il_call_operator prev_declared_fb_name '(' eol_list il_param_list ')'
       
  1213  */
       
  1214 /* SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list) */
       
  1215 void *visit_expression_type_c::visit(il_fb_call_c *symbol) {
       
  1216   if (il_error)
       
  1217     return NULL;
       
  1218 
       
  1219   /* first check whether the il_default_variable is of the correct type
       
  1220    * for the CAL / CALC / CALCN operator being used...
       
  1221    */
       
  1222   symbol->il_call_operator->accept(*this);
       
  1223 
       
  1224   /* Now check the FB call itself... */
       
  1225 
       
  1226   /* First we find the declaration of the FB type of the FB instance being called... */
       
  1227   /* e.g.  Function_block foo_fb_type
       
  1228    *         ...
       
  1229    *       End_Function_Block
       
  1230    *
       
  1231    *       Program test
       
  1232    *         var fb1 : foo_fb_type; end_var
       
  1233    *         fb1(...)
       
  1234    *       End_Program
       
  1235    *
       
  1236    *    search_varfb_instance_type->get_basetype_decl( identifier_c("fb1") )
       
  1237    *    in the scope of Program 'test'
       
  1238    *    will return the fb declaration of foo_fb_type !!
       
  1239    */
       
  1240 #if 0
       
  1241   symbol_c *fb_decl_symbol = search_varfb_instance_type->get_basetype_decl(symbol->fb_name);
       
  1242     /* The following should never occur. The function block must be defined, 
       
  1243      * and the FB type being called MUST be in the symtable... 
       
  1244      * This was all already checked at stage 2!
       
  1245      */
       
  1246   if (NULL == fb_decl_symbol) ERROR;
       
  1247 
       
  1248   function_block_declaration_c *fb_decl = dynamic_cast<function_block_declaration_c *>(fb_decl_symbol);
       
  1249     /* should never occur. ... */
       
  1250   if (NULL == fb_decl) ERROR;
       
  1251 #endif
       
  1252   symbol_c *fb_decl = search_varfb_instance_type->get_basetype_decl(symbol->fb_name);
       
  1253     /* The following should never occur. The function block must be defined, 
       
  1254      * and the FB type being called MUST be in the symtable... 
       
  1255      * This was all already checked at stage 2!
       
  1256      */
       
  1257   if (NULL == fb_decl) ERROR;
       
  1258 
       
  1259   /* now check the semantics of the fb call... */
       
  1260   /* If the syntax parser is working correctly, exactly one of the 
       
  1261    * following two symbols will be NULL, while the other is != NULL.
       
  1262    */
       
  1263   if (NULL != symbol->il_operand_list)  check_nonformal_call(symbol, fb_decl);
       
  1264   if (NULL != symbol->il_param_list)    check_formal_call   (symbol, fb_decl);
       
  1265 
       
  1266   return NULL;
       
  1267 }
       
  1268 
       
  1269 
       
  1270 
       
  1271 /* | function_name '(' eol_list [il_param_list] ')' */
       
  1272 /* SYM_REF2(il_formal_funct_call_c, function_name, il_param_list) */
       
  1273 void *visit_expression_type_c::visit(il_formal_funct_call_c *symbol) {
       
  1274   if (il_error)
       
  1275     return NULL;
       
  1276 
       
  1277   symbol_c *return_data_type = NULL;
       
  1278   symbol_c* fdecl_return_type;
       
  1279   symbol_c *overloaded_data_type = NULL;
       
  1280   int extensible_param_count = -1;
       
  1281   symbol->called_function_declaration = NULL;
       
  1282 
       
  1283   function_symtable_t::iterator lower = function_symtable.lower_bound(symbol->function_name);
       
  1284   function_symtable_t::iterator upper = function_symtable.upper_bound(symbol->function_name);
       
  1285   function_symtable_t::iterator current;
       
  1286   
       
  1287   if (lower == function_symtable.end()) {
       
  1288     function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name);
       
  1289     if (current_function_type == function_none) ERROR;
       
  1290     return NULL;
       
  1291   }
       
  1292 
       
  1293   int error_count = 0; 
       
  1294   int *error_count_ptr = NULL;
       
  1295 
       
  1296   function_symtable_t::iterator second = lower;
       
  1297   second++;
       
  1298   if (second != upper) 
       
  1299     /* This is a call to an overloaded function... */  
       
  1300     error_count_ptr = &error_count;
       
  1301 
       
  1302   for(current = lower; current != upper; current++) {
       
  1303     function_declaration_c *f_decl = function_symtable.get_value(current);
       
  1304   
       
  1305     /* check semantics of data passed in the function call... */
       
  1306     check_formal_call(symbol, f_decl, error_count_ptr);
       
  1307 
       
  1308     if (0 == error_count) {
       
  1309       /* Either: 
       
  1310        * (i) we have a call to a non-overloaded function (error_cnt_ptr is NULL!, so error_count won't change!)  
       
  1311        * (ii) we have a call to an overloaded function, with no errors!
       
  1312        */
       
  1313 
       
  1314       fdecl_return_type = base_type(f_decl->type_name);
       
  1315 
       
  1316       if (symbol->called_function_declaration == NULL) {
       
  1317         /* Store the pointer to the declaration of the function being called.
       
  1318          * This data will be used by stage 4 to call the correct function.
       
  1319          * Mostly needed to disambiguate overloaded functions...
       
  1320          * See comments in absyntax.def for more details
       
  1321          */
       
  1322         symbol->called_function_declaration = f_decl;
       
  1323         extensible_param_count = symbol->extensible_param_count;
       
  1324 
       
  1325         /* determine the base data type returned by the function being called... */
       
  1326         return_data_type = fdecl_return_type;
       
  1327       }
       
  1328       else if (typeid(*return_data_type) != typeid(*fdecl_return_type)){
       
  1329         return_data_type = common_literal(return_data_type, fdecl_return_type);
       
  1330         overloaded_data_type = overloaded_return_type(return_data_type);
       
  1331       }
       
  1332 
       
  1333       /* the following should never occur. If it does, then we have a bug in the syntax parser (stage 2)... */
       
  1334       if (NULL == return_data_type) ERROR;
       
  1335 
       
  1336     }
       
  1337   }
       
  1338   
       
  1339   if (overloaded_data_type != NULL) {
       
  1340     for(current = lower; current != upper; current++) {
       
  1341       function_declaration_c *f_decl = function_symtable.get_value(current);
       
  1342 
       
  1343       /* check semantics of data passed in the function call... */
       
  1344       check_formal_call(symbol, f_decl, error_count_ptr);
       
  1345 
       
  1346       if (0 == error_count) {
       
  1347 
       
  1348         fdecl_return_type = base_type(f_decl->type_name);
       
  1349 
       
  1350         if (typeid(*overloaded_data_type) == typeid(*fdecl_return_type)){
       
  1351           /* Store the pointer to the declaration of the function being called.
       
  1352            * This data will be used by stage 4 to call the correct function.
       
  1353            * Mostly needed to disambiguate overloaded functions...
       
  1354            * See comments in absyntax.def for more details
       
  1355            */
       
  1356           symbol->called_function_declaration = f_decl;
       
  1357           extensible_param_count = symbol->extensible_param_count;
       
  1358         }
       
  1359       }
       
  1360     }
       
  1361   }
       
  1362 
       
  1363   if (NULL == return_data_type) {
       
  1364     /* No compatible function was found for this function call */
       
  1365     STAGE3_ERROR(symbol, symbol, "Call to an overloaded function with invalid parameter type.");
       
  1366   }
       
  1367   else {
       
  1368     symbol->extensible_param_count = extensible_param_count;
       
  1369     /* the data type of the data returned by the function, and stored in the il default variable... */
       
  1370     il_default_variable_type = return_data_type;
       
  1371   }
       
  1372 
       
  1373   return NULL;
       
  1374 }
       
  1375 
       
  1376 
       
  1377 #if 0
       
  1378 /* | il_operand_list ',' il_operand */
       
  1379 SYM_LIST(il_operand_list_c)
       
  1380 void *visit_expression_type_c::visit(il_operand_list_c *symbol);
       
  1381 
       
  1382 /* | simple_instr_list il_simple_instruction */
       
  1383 SYM_LIST(simple_instr_list_c)
       
  1384 void *visit_expression_type_c::visit(simple_instr_list_c *symbol);
       
  1385 
       
  1386 /* | il_initial_param_list il_param_instruction */
       
  1387 SYM_LIST(il_param_list_c)
       
  1388 void *visit_expression_type_c::visit(il_param_list_c *symbol);
       
  1389 
       
  1390 /*  il_assign_operator il_operand
       
  1391  * | il_assign_operator '(' eol_list simple_instr_list ')'
       
  1392  */
       
  1393 SYM_REF3(il_param_assignment_c, il_assign_operator, il_operand, simple_instr_list)
       
  1394 void *visit_expression_type_c::visit(il_param_assignment_c *symbol);
       
  1395 /*  il_assign_out_operator variable */
       
  1396 SYM_REF2(il_param_out_assignment_c, il_assign_out_operator, variable)
       
  1397 void *visit_expression_type_c::visit(il_param_out_assignment_c *symbol);
       
  1398 
       
  1399 #endif
       
  1400 
       
  1401 
       
  1402 /*******************/
       
  1403 /* B 2.2 Operators */
       
  1404 /*******************/
       
  1405 
       
  1406 //SYM_REF0(LD_operator_c)
       
  1407 void *visit_expression_type_c::visit(LD_operator_c *symbol) {
       
  1408   if (0 == il_parenthesis_level)
       
  1409     il_error = false;
       
  1410 
       
  1411   if(il_operand_type == NULL)
       
  1412       STAGE3_ERROR(symbol, symbol, "LD operator requires an operand.");
       
  1413   il_default_variable_type = il_operand_type;
       
  1414   return NULL;
       
  1415 }
       
  1416 
       
  1417 // SYM_REF0(LDN_operator_c)
       
  1418 void *visit_expression_type_c::visit(LDN_operator_c *symbol) {
       
  1419   if(il_operand_type == NULL)
       
  1420       STAGE3_ERROR(symbol, symbol, "LDN operator requires an operand.");
       
  1421   if(!is_ANY_BIT_compatible(il_operand_type))
       
  1422       STAGE3_ERROR(symbol, il_operand, "invalid data type of LDN operand, should be of type ANY_BIT.");
       
  1423   il_default_variable_type = il_operand_type;
       
  1424   return NULL;
       
  1425 }
       
  1426 
       
  1427 // SYM_REF0(ST_operator_c)
       
  1428 void *visit_expression_type_c::visit(ST_operator_c *symbol) {
       
  1429   verify_null(symbol);
       
  1430 
       
  1431   if(!is_valid_assignment(il_operand_type, il_default_variable_type))
       
  1432     STAGE3_ERROR(symbol, symbol, "Type mismatch in ST operation.");
       
  1433   /* TODO: check whether il_operand_type is an LVALUE !! */
       
  1434   /* data type of il_default_variable_type is unchanged... */
       
  1435   // il_default_variable_type = il_default_variable_type;
       
  1436   return NULL;
       
  1437 }
       
  1438 
       
  1439 // SYM_REF0(STN_operator_c)
       
  1440  void *visit_expression_type_c::visit(STN_operator_c *symbol) {
       
  1441   verify_null(symbol);
       
  1442   if(!is_valid_assignment(il_operand_type, il_default_variable_type))
       
  1443     STAGE3_ERROR(symbol, symbol, "Type mismatch in ST operation.");
       
  1444   /* TODO: check whether il_operand_type is an LVALUE !! */
       
  1445   if(!is_ANY_BIT_compatible(il_default_variable_type))
       
  1446       STAGE3_ERROR(symbol, symbol, "invalid data type of il_default_variable for STN operand, should be of type ANY_BIT.");
       
  1447   if(!is_ANY_BIT_compatible(il_operand_type))
       
  1448       STAGE3_ERROR(symbol, il_operand, "invalid data type of STN operand, should be of type ANY_BIT.");
       
  1449   /* data type of il_default_variable_type is unchanged... */
       
  1450   // il_default_variable_type = il_default_variable_type;
       
  1451   return NULL;
       
  1452 }
       
  1453 
       
  1454 //SYM_REF0(NOT_operator_c)
       
  1455 void *visit_expression_type_c::visit(NOT_operator_c *symbol) {
       
  1456   if(il_operand_type != NULL){
       
  1457     STAGE3_ERROR(symbol, il_operand, "NOT operator may not have an operand.");
       
  1458     return NULL;
       
  1459   }
       
  1460   if(il_default_variable_type == NULL) {
       
  1461     STAGE3_ERROR(symbol, symbol, "Il default variable should not be NULL.");
       
  1462     return NULL;
       
  1463   }
       
  1464   if(!is_ANY_BIT_compatible(il_default_variable_type)) {
       
  1465     STAGE3_ERROR(symbol, symbol, "Il default variable should be of type ANY_BIT.");
       
  1466     return NULL;
       
  1467   }
       
  1468   /* data type of il_default_variable_type is unchanged... */
       
  1469   // il_default_variable_type = il_default_variable_type;
       
  1470   return NULL;
       
  1471 }
       
  1472 
       
  1473 // SYM_REF0(S_operator_c)
       
  1474 void *visit_expression_type_c::visit(S_operator_c *symbol) {
       
  1475   verify_null(symbol);
       
  1476   if (!is_BOOL_type(il_default_variable_type)) {STAGE3_ERROR(symbol, symbol, "IL default variable should be BOOL type.");}
       
  1477   if (!is_BOOL_type(il_operand_type)) {STAGE3_ERROR(symbol, il_operand, "operator S requires operand of type BOOL.");}
       
  1478   /* TODO: check whether il_operand_type is an LVALUE !! */
       
  1479   /* data type of il_default_variable_type is unchanged... */
       
  1480   // il_default_variable_type = il_default_variable_type;
       
  1481   return NULL;
       
  1482 }
       
  1483 
       
  1484 // SYM_REF0(R_operator_c)
       
  1485 void *visit_expression_type_c::visit(R_operator_c *symbol) {
       
  1486   verify_null(symbol);
       
  1487   if (!is_BOOL_type(il_default_variable_type)) {STAGE3_ERROR(symbol, symbol, "IL default variable should be BOOL type.");}
       
  1488   if (!is_BOOL_type(il_operand_type)) {STAGE3_ERROR(symbol, il_operand, "operator R requires operand of type BOOL.");}
       
  1489   /* TODO: check whether il_operand_type is an LVALUE !! */
       
  1490   /* data type of il_default_variable_type is unchanged... */
       
  1491   // il_default_variable_type = il_default_variable_type;
       
  1492   return NULL;
       
  1493 }
       
  1494 
       
  1495 
       
  1496 // SYM_REF0(S1_operator_c)
       
  1497 void *visit_expression_type_c::visit(S1_operator_c *symbol){
       
  1498   check_il_fbcall(symbol, "S1");
       
  1499   return NULL;
       
  1500 }
       
  1501 
       
  1502 // SYM_REF0(R1_operator_c)
       
  1503 void *visit_expression_type_c::visit(R1_operator_c *symbol) {
       
  1504   check_il_fbcall(symbol, "R1");
       
  1505   return NULL;
       
  1506 }
       
  1507 
       
  1508 // SYM_REF0(CLK_operator_c)
       
  1509 void *visit_expression_type_c::visit(CLK_operator_c *symbol) {
       
  1510   check_il_fbcall(symbol, "CLK");
       
  1511   return NULL;
       
  1512 }
       
  1513 
       
  1514 // SYM_REF0(CU_operator_c)
       
  1515 void *visit_expression_type_c::visit(CU_operator_c *symbol) {
       
  1516   check_il_fbcall(symbol, "CU");
       
  1517   return NULL;
       
  1518 }
       
  1519 
       
  1520 // SYM_REF0(CD_operator_c)
       
  1521 void *visit_expression_type_c::visit(CD_operator_c *symbol) {
       
  1522   check_il_fbcall(symbol, "CD");
       
  1523   return NULL;
       
  1524 }
       
  1525 
       
  1526 // SYM_REF0(PV_operator_c)
       
  1527 void *visit_expression_type_c::visit(PV_operator_c *symbol) {
       
  1528   check_il_fbcall(symbol, "PV");
       
  1529   return NULL;
       
  1530 }
       
  1531 
       
  1532 // SYM_REF0(IN_operator_c)
       
  1533 void *visit_expression_type_c::visit(IN_operator_c *symbol) {
       
  1534   check_il_fbcall(symbol, "IN");
       
  1535   return NULL;
       
  1536 }
       
  1537 
       
  1538 // SYM_REF0(PT_operator_c)
       
  1539 void *visit_expression_type_c::visit(PT_operator_c *symbol) {
       
  1540   check_il_fbcall(symbol, "PT");
       
  1541   return NULL;
       
  1542 }
       
  1543 
       
  1544 //SYM_REF0(AND_operator_c)
       
  1545 void *visit_expression_type_c::visit(AND_operator_c *symbol) {
       
  1546   verify_null(symbol);
       
  1547   il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_BIT_compatible,
       
  1548                                                 symbol                  , il_operand);
       
  1549   return NULL;
       
  1550 }
       
  1551 
       
  1552 //SYM_REF0(OR_operator_c)
       
  1553 void *visit_expression_type_c::visit(OR_operator_c *symbol) {
       
  1554   verify_null(symbol);
       
  1555   il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_BIT_compatible,
       
  1556                                                 symbol                  , il_operand);
       
  1557   return NULL;
       
  1558 }
       
  1559 
       
  1560 //SYM_REF0(XOR_operator_c)
       
  1561 void *visit_expression_type_c::visit(XOR_operator_c *symbol) {
       
  1562   verify_null(symbol);
       
  1563   il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_BIT_compatible,
       
  1564                                                 symbol                  , il_operand);
       
  1565   return NULL;
       
  1566 }
       
  1567 
       
  1568 // SYM_REF0(ANDN_operator_c)
       
  1569 void *visit_expression_type_c::visit(ANDN_operator_c *symbol) {
       
  1570   verify_null(symbol);
       
  1571   il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_BIT_compatible,
       
  1572                                                 symbol                  , il_operand);
       
  1573   return NULL;
       
  1574 }
       
  1575 
       
  1576 // SYM_REF0(ORN_operator_c)
       
  1577 void *visit_expression_type_c::visit(ORN_operator_c *symbol) {
       
  1578   verify_null(symbol);
       
  1579   il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_BIT_compatible,
       
  1580                                                 symbol                  , il_operand);
       
  1581   return NULL;
       
  1582 }
       
  1583 
       
  1584 // SYM_REF0(XORN_operator_c)
       
  1585 void *visit_expression_type_c::visit(XORN_operator_c *symbol) {
       
  1586   verify_null(symbol);
       
  1587   il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_BIT_compatible,
       
  1588                                                 symbol                  , il_operand);
       
  1589   return NULL;
       
  1590 }
       
  1591 
       
  1592 // SYM_REF0(ADD_operator_c)
       
  1593 void *visit_expression_type_c::visit(ADD_operator_c *symbol) {
       
  1594   verify_null(symbol);
       
  1595   symbol_c *left_type  = il_default_variable_type;
       
  1596   symbol_c *right_type = il_operand_type;
       
  1597 
       
  1598 /* The following is not required, it is already handled by compute_expression() ... */
       
  1599 /*
       
  1600   if      (is_type(left_type, time_type_name_c) && is_type(right_type, time_type_name_c))
       
  1601     il_default_variable_type = &time_type_name;
       
  1602 */
       
  1603 
       
  1604   if      (is_type(left_type, tod_type_name_c)      && is_type(right_type, time_type_name_c))
       
  1605     il_default_variable_type = &tod_type_name;
       
  1606   else if (is_type(left_type, safetod_type_name_c)  && is_type(right_type, time_type_name_c))
       
  1607     il_default_variable_type = &tod_type_name;
       
  1608   else if (is_type(left_type, tod_type_name_c)      && is_type(right_type, safetime_type_name_c))
       
  1609     il_default_variable_type = &tod_type_name;
       
  1610   else if (is_type(left_type, safetod_type_name_c)  && is_type(right_type, safetime_type_name_c))
       
  1611     il_default_variable_type = &safetod_type_name;
       
  1612 
       
  1613   else if (is_type(left_type, dt_type_name_c)       && is_type(right_type, time_type_name_c))
       
  1614     il_default_variable_type = &dt_type_name;
       
  1615   else if (is_type(left_type, safedt_type_name_c)   && is_type(right_type, time_type_name_c))
       
  1616     il_default_variable_type = &dt_type_name;
       
  1617   else if (is_type(left_type, dt_type_name_c)       && is_type(right_type, safetime_type_name_c))
       
  1618     il_default_variable_type = &dt_type_name;
       
  1619   else if (is_type(left_type, safedt_type_name_c)   && is_type(right_type, safetime_type_name_c))
       
  1620     il_default_variable_type = &safedt_type_name;
       
  1621 
       
  1622   else il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_MAGNITUDE_compatible,
       
  1623                                                      symbol                  , il_operand);
       
  1624   return NULL;
       
  1625 }
       
  1626 
       
  1627 // SYM_REF0(SUB_operator_c)
       
  1628 void *visit_expression_type_c::visit(SUB_operator_c *symbol) {
       
  1629   verify_null(symbol);
       
  1630   symbol_c *left_type = il_default_variable_type;
       
  1631   symbol_c *right_type = il_operand_type;;
       
  1632 
       
  1633 /* The following is not required, it is already handled by compute_expression() ... */
       
  1634 /*
       
  1635   if      (typeid(*left_type) == typeid(time_type_name_c) && typeid(*right_type) == typeid(time_type_name_c))
       
  1636     il_default_variable_type = &time_type_name;
       
  1637 */
       
  1638 
       
  1639   if      (is_type(left_type, tod_type_name_c)       && is_type(right_type, time_type_name_c))
       
  1640     il_default_variable_type = &tod_type_name;
       
  1641   else if (is_type(left_type, safetod_type_name_c)   && is_type(right_type, time_type_name_c))
       
  1642     il_default_variable_type = &tod_type_name;
       
  1643   else if (is_type(left_type, tod_type_name_c)       && is_type(right_type, safetime_type_name_c))
       
  1644     il_default_variable_type = &tod_type_name;
       
  1645   else if (is_type(left_type, safetod_type_name_c)   && is_type(right_type, safetime_type_name_c))
       
  1646     il_default_variable_type = &safetod_type_name;
       
  1647 
       
  1648   else if (is_type(left_type, dt_type_name_c)       && is_type(right_type, time_type_name_c))
       
  1649     il_default_variable_type = &dt_type_name;
       
  1650   else if (is_type(left_type, safedt_type_name_c)   && is_type(right_type, time_type_name_c))
       
  1651     il_default_variable_type = &dt_type_name;
       
  1652   else if (is_type(left_type, dt_type_name_c)       && is_type(right_type, safetime_type_name_c))
       
  1653     il_default_variable_type = &dt_type_name;
       
  1654   else if (is_type(left_type, safedt_type_name_c)   && is_type(right_type, safetime_type_name_c))
       
  1655     il_default_variable_type = &safedt_type_name;
       
  1656 
       
  1657   else if (is_type(left_type, date_type_name_c)     && is_type(right_type, date_type_name_c))
       
  1658     il_default_variable_type = &time_type_name;
       
  1659   else if (is_type(left_type, safedate_type_name_c) && is_type(right_type, date_type_name_c))
       
  1660     il_default_variable_type = &time_type_name;
       
  1661   else if (is_type(left_type, date_type_name_c)     && is_type(right_type, safedate_type_name_c))
       
  1662     il_default_variable_type = &time_type_name;
       
  1663   else if (is_type(left_type, safedate_type_name_c) && is_type(right_type, safedate_type_name_c))
       
  1664     il_default_variable_type = &safetime_type_name;
       
  1665 
       
  1666   else if (is_type(left_type, tod_type_name_c)      && is_type(right_type, tod_type_name_c))
       
  1667     il_default_variable_type = &time_type_name;
       
  1668   else if (is_type(left_type, safetod_type_name_c)  && is_type(right_type, tod_type_name_c))
       
  1669     il_default_variable_type = &time_type_name;
       
  1670   else if (is_type(left_type, tod_type_name_c)      && is_type(right_type, safetod_type_name_c))
       
  1671     il_default_variable_type = &time_type_name;
       
  1672   else if (is_type(left_type, safetod_type_name_c)  && is_type(right_type, safetod_type_name_c))
       
  1673     il_default_variable_type = &safetime_type_name;
       
  1674 
       
  1675   else if (is_type(left_type, dt_type_name_c)       && is_type(right_type, dt_type_name_c))
       
  1676     il_default_variable_type = &time_type_name;
       
  1677   else if (is_type(left_type, safedt_type_name_c)   && is_type(right_type, dt_type_name_c))
       
  1678     il_default_variable_type = &time_type_name;
       
  1679   else if (is_type(left_type, dt_type_name_c)       && is_type(right_type, safedt_type_name_c))
       
  1680     il_default_variable_type = &time_type_name;
       
  1681   else if (is_type(left_type, safedt_type_name_c)   && is_type(right_type, safedt_type_name_c))
       
  1682     il_default_variable_type = &safetime_type_name;
       
  1683 
       
  1684   else il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_MAGNITUDE_compatible,
       
  1685                                                      symbol                  , il_operand);
       
  1686   return NULL;
       
  1687 }
       
  1688 
       
  1689 // SYM_REF0(MUL_operator_c)
       
  1690 void *visit_expression_type_c::visit(MUL_operator_c *symbol) {
       
  1691   verify_null(symbol);
       
  1692   symbol_c *left_type = il_default_variable_type;
       
  1693   symbol_c *right_type = il_operand_type;
       
  1694 
       
  1695   if      (is_type(left_type, time_type_name_c)     && is_ANY_NUM_compatible(right_type))
       
  1696     il_default_variable_type = &time_type_name;
       
  1697   else if (is_type(left_type, safetime_type_name_c) && is_ANY_NUM_type(right_type))
       
  1698     il_default_variable_type = &time_type_name;
       
  1699   else if (is_type(left_type, safetime_type_name_c) && is_ANY_SAFENUM_type(right_type))
       
  1700     il_default_variable_type = &safetime_type_name;
       
  1701   /* Since we have already checked for ANY_NUM_type and ANY_SAFENUM_type in the previous lines,
       
  1702    * this next line is really only to check for integers/reals of undefined type on 'right_type'... 
       
  1703    */
       
  1704   else if (is_type(left_type, safetime_type_name_c) && is_ANY_NUM_compatible(right_type))
       
  1705     il_default_variable_type = &safetime_type_name;
       
  1706 
       
  1707   else il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_NUM_compatible,
       
  1708                                                      symbol                  , il_operand);
       
  1709   return NULL;
       
  1710 }
       
  1711 
       
  1712 // SYM_REF0(DIV_operator_c)
       
  1713 void *visit_expression_type_c::visit(DIV_operator_c *symbol) {
       
  1714   verify_null(symbol);
       
  1715   symbol_c *left_type = il_default_variable_type;
       
  1716   symbol_c *right_type = il_operand_type;
       
  1717 
       
  1718   if      (is_type(left_type, time_type_name_c)     && is_ANY_NUM_compatible(right_type))
       
  1719     il_default_variable_type = &time_type_name;
       
  1720   else if (is_type(left_type, safetime_type_name_c) && is_ANY_NUM_type(right_type))
       
  1721     il_default_variable_type = &time_type_name;
       
  1722   else if (is_type(left_type, safetime_type_name_c) && is_ANY_SAFENUM_type(right_type))
       
  1723     il_default_variable_type = &safetime_type_name;
       
  1724   /* Since we have already checked for ANY_NUM_type and ANY_SAFENUM_type in the previous lines,
       
  1725    * this next line is really only to check for integers/reals of undefined type on 'right_type'... 
       
  1726    */
       
  1727   else if (is_type(left_type, safetime_type_name_c) && is_ANY_NUM_compatible(right_type))
       
  1728     il_default_variable_type = &safetime_type_name;
       
  1729 
       
  1730   else il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_NUM_compatible,
       
  1731                                                      symbol                  , il_operand);
       
  1732   return NULL;
       
  1733 }
       
  1734 
       
  1735 // SYM_REF0(MOD_operator_c)
       
  1736 void *visit_expression_type_c::visit(MOD_operator_c *symbol) {
       
  1737   verify_null(symbol);
       
  1738   il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_INT_compatible,
       
  1739                                                 symbol                  , il_operand);
       
  1740   return NULL;
       
  1741 }
       
  1742 
       
  1743 // SYM_REF0(GT_operator_c)
       
  1744 void *visit_expression_type_c::visit(GT_operator_c *symbol) {
       
  1745   verify_null(symbol);
       
  1746   compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible,
       
  1747                      symbol                  , il_operand);
       
  1748   il_default_variable_type = &search_expression_type_c::bool_type_name;
       
  1749   return NULL;
       
  1750 }
       
  1751 
       
  1752 //SYM_REF0(GE_operator_c)
       
  1753 void *visit_expression_type_c::visit(GE_operator_c *symbol) {
       
  1754   verify_null(symbol);
       
  1755   compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible,
       
  1756                      symbol                  , il_operand);
       
  1757   il_default_variable_type = &search_expression_type_c::bool_type_name;
       
  1758   return NULL;
       
  1759 }
       
  1760 
       
  1761 //SYM_REF0(EQ_operator_c)
       
  1762 void *visit_expression_type_c::visit(EQ_operator_c *symbol) {
       
  1763   verify_null(symbol);
       
  1764   compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible,
       
  1765                      symbol                  , il_operand);
       
  1766   il_default_variable_type = &search_expression_type_c::bool_type_name;
       
  1767   return NULL;
       
  1768 }
       
  1769 
       
  1770 //SYM_REF0(LT_operator_c)
       
  1771 void *visit_expression_type_c::visit(LT_operator_c *symbol) {
       
  1772   verify_null(symbol);
       
  1773   compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible,
       
  1774                      symbol                  , il_operand);
       
  1775   il_default_variable_type = &search_expression_type_c::bool_type_name;
       
  1776   return NULL;
       
  1777 }
       
  1778 
       
  1779 //SYM_REF0(LE_operator_c)
       
  1780 void *visit_expression_type_c::visit(LE_operator_c *symbol) {
       
  1781   verify_null(symbol);
       
  1782   compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible,
       
  1783                      symbol                  , il_operand);
       
  1784   il_default_variable_type = &search_expression_type_c::bool_type_name;
       
  1785   return NULL;
       
  1786 }
       
  1787 
       
  1788 //SYM_REF0(NE_operator_c)
       
  1789 void *visit_expression_type_c::visit(NE_operator_c *symbol) {
       
  1790   verify_null(symbol);
       
  1791   compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible,
       
  1792                      symbol                  , il_operand);
       
  1793   il_default_variable_type = &search_expression_type_c::bool_type_name;
       
  1794   return NULL;
       
  1795 }
       
  1796 
       
  1797 // SYM_REF0(CAL_operator_c)
       
  1798 void *visit_expression_type_c::visit(CAL_operator_c *symbol) {
       
  1799   return NULL;
       
  1800 }
       
  1801 
       
  1802 // SYM_REF0(CALC_operator_c)
       
  1803 void *visit_expression_type_c::visit(CALC_operator_c *symbol) {
       
  1804   if(il_default_variable_type == NULL)
       
  1805     STAGE3_ERROR(symbol, symbol, "CALC: il default variable should not be NULL.");
       
  1806   if (!is_BOOL_type(il_default_variable_type))
       
  1807     STAGE3_ERROR(symbol, symbol, "CALC operator requires il_default_variable to be of type BOOL.");
       
  1808   return NULL;
       
  1809 }
       
  1810 
       
  1811 // SYM_REF0(CALCN_operator_c)
       
  1812 void *visit_expression_type_c::visit(CALCN_operator_c *symbol) {
       
  1813   if(il_default_variable_type == NULL)
       
  1814     STAGE3_ERROR(symbol, symbol, "CALCN: il_default_variable should not be NULL.");
       
  1815   if (!is_BOOL_type(il_default_variable_type))
       
  1816     STAGE3_ERROR(symbol, symbol, "CALCN operator requires il_default_variable to be of type BOOL.");
       
  1817   return NULL;
       
  1818 }
       
  1819 
       
  1820 // SYM_REF0(RET_operator_c)
       
  1821 void *visit_expression_type_c::visit(RET_operator_c *symbol) {
       
  1822   return NULL;
       
  1823 }
       
  1824 
       
  1825 // SYM_REF0(RETC_operator_c)
       
  1826 void *visit_expression_type_c::visit(RETC_operator_c *symbol) {
       
  1827   if(il_default_variable_type == NULL)
       
  1828     STAGE3_ERROR(symbol, symbol, "RETC: il default variable should not be NULL.");
       
  1829   if (!is_BOOL_type(il_default_variable_type))
       
  1830     STAGE3_ERROR(symbol, symbol, "RETC operator requires il_default_variable to be of type BOOL.");
       
  1831   return NULL;
       
  1832 }
       
  1833 
       
  1834 // SYM_REF0(RETCN_operator_c)
       
  1835 void *visit_expression_type_c::visit(RETCN_operator_c *symbol) {
       
  1836   if(il_default_variable_type == NULL)
       
  1837     STAGE3_ERROR(symbol, symbol, "RETCN: il_default_variable should not be NULL.");
       
  1838   if (!is_BOOL_type(il_default_variable_type))
       
  1839     STAGE3_ERROR(symbol, symbol, "RETCN operator requires il_default_variable to be of type BOOL.");
       
  1840   return NULL;
       
  1841 }
       
  1842 
       
  1843 // SYM_REF0(JMP_operator_c)
       
  1844 void *visit_expression_type_c::visit(JMP_operator_c *symbol){
       
  1845   return NULL;
       
  1846 }
       
  1847 
       
  1848 // SYM_REF0(JMPC_operator_c)
       
  1849 void *visit_expression_type_c::visit(JMPC_operator_c *symbol) {
       
  1850   if(il_default_variable_type == NULL)
       
  1851     STAGE3_ERROR(symbol, symbol, "JMPC: il default variable should not be NULL.");
       
  1852   if (!is_BOOL_type(il_default_variable_type))
       
  1853     STAGE3_ERROR(symbol, symbol, "JMPC operator requires il_default_variable to be of type BOOL.");
       
  1854   return NULL;
       
  1855 }
       
  1856 
       
  1857 // SYM_REF0(JMPCN_operator_c)
       
  1858 void *visit_expression_type_c::visit(JMPCN_operator_c *symbol) {
       
  1859   if(il_default_variable_type == NULL)
       
  1860     STAGE3_ERROR(symbol, symbol, "JMPCN: il_default_variable should not be NULL.");
       
  1861   if (!is_BOOL_type(il_default_variable_type))
       
  1862     STAGE3_ERROR(symbol, symbol, "JMPCN operator requires il_default_variable to be of type BOOL.");
       
  1863   return NULL;
       
  1864 }
       
  1865 
       
  1866 /* Symbol class handled together with function call checks */
       
  1867 /*  any_identifier ASSIGN */
       
  1868 // SYM_REF1(il_assign_operator_c, variable_name)
       
  1869 // void *visit_expression_type_c::visit(il_assign_operator_c *symbol, variable_name);
       
  1870 
       
  1871 /* Symbol class handled together with function call checks */
       
  1872 /*| [NOT] any_identifier SENDTO */
       
  1873 // SYM_REF2(il_assign_out_operator_c, option, variable_name)
       
  1874 // void *visit_expression_type_c::visit(il_assign_operator_c *symbol, option, variable_name);
       
  1875 
       
  1876 
       
  1877 
       
  1878 
       
  1879 
       
  1880 /***************************************/
       
  1881 /* B.3 - Language ST (Structured Text) */
       
  1882 /***************************************/
       
  1883 /***********************/
       
  1884 /* B 3.1 - Expressions */
       
  1885 /***********************/
       
  1886 
       
  1887 void *visit_expression_type_c::visit(or_expression_c *symbol) {
       
  1888   symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this));
       
  1889   symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
       
  1890   return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_BIT_compatible, symbol->l_exp, symbol->r_exp);
       
  1891 }
       
  1892 
       
  1893 
       
  1894 void *visit_expression_type_c::visit(xor_expression_c *symbol) {
       
  1895   symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this));
       
  1896   symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
       
  1897   return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_BIT_compatible, symbol->l_exp, symbol->r_exp);
       
  1898 }
       
  1899 
       
  1900 
       
  1901 void *visit_expression_type_c::visit(and_expression_c *symbol) {
       
  1902   symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this));
       
  1903   symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
       
  1904   return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_BIT_compatible, symbol->l_exp, symbol->r_exp);
       
  1905 }
       
  1906 
       
  1907 
       
  1908 void *visit_expression_type_c::visit(equ_expression_c *symbol) {
       
  1909   symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this));
       
  1910   symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
       
  1911   compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_ELEMENTARY_OR_ENUMERATED_compatible, symbol->l_exp, symbol->r_exp);
       
  1912   return &search_expression_type_c::bool_type_name;
       
  1913 }
       
  1914 
       
  1915 
       
  1916 void *visit_expression_type_c::visit(notequ_expression_c *symbol)  {
       
  1917   symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this));
       
  1918   symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
       
  1919   compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_ELEMENTARY_OR_ENUMERATED_compatible, symbol->l_exp, symbol->r_exp);
       
  1920   return &search_expression_type_c::bool_type_name;
       
  1921 }
       
  1922 
       
  1923 
       
  1924 void *visit_expression_type_c::visit(lt_expression_c *symbol) {
       
  1925   symbol_c *left_type  = base_type((symbol_c *)symbol->l_exp->accept(*this));
       
  1926   symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
       
  1927   compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible, symbol->l_exp, symbol->r_exp);
       
  1928   return &search_expression_type_c::bool_type_name;
       
  1929 }
       
  1930 
       
  1931 
       
  1932 void *visit_expression_type_c::visit(gt_expression_c *symbol) {
       
  1933   symbol_c *left_type  = base_type((symbol_c *)symbol->l_exp->accept(*this));
       
  1934   symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
       
  1935   compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible, symbol->l_exp, symbol->r_exp);
       
  1936   return &search_expression_type_c::bool_type_name;
       
  1937 }
       
  1938 
       
  1939 
       
  1940 void *visit_expression_type_c::visit(le_expression_c *symbol) {
       
  1941   symbol_c *left_type  = base_type((symbol_c *)symbol->l_exp->accept(*this));
       
  1942   symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
       
  1943   compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible, symbol->l_exp, symbol->r_exp);
       
  1944   return &search_expression_type_c::bool_type_name;
       
  1945 }
       
  1946 
       
  1947 
       
  1948 void *visit_expression_type_c::visit(ge_expression_c *symbol) {
       
  1949   symbol_c *left_type  = base_type((symbol_c *)symbol->l_exp->accept(*this));
       
  1950   symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
       
  1951   compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible, symbol->l_exp, symbol->r_exp);
       
  1952   return &search_expression_type_c::bool_type_name;
       
  1953 }
       
  1954 
       
  1955 
       
  1956 void *visit_expression_type_c::visit(add_expression_c *symbol) {
       
  1957   symbol_c *left_type  = base_type((symbol_c *)symbol->l_exp->accept(*this));
       
  1958   symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
       
  1959 
       
  1960 /* The following is already checked in compute_expression */
       
  1961 /*
       
  1962   if (is_type(left_type, time_type_name_c) && is_type(right_type, time_type_name_c)) 
       
  1963     return (void *)&time_type_name;
       
  1964 */
       
  1965 
       
  1966   if (is_type(left_type, tod_type_name_c)      && is_type(right_type, time_type_name_c)) 
       
  1967     return (void *)&tod_type_name;
       
  1968   if (is_type(left_type, safetod_type_name_c)  && is_type(right_type, time_type_name_c)) 
       
  1969     return (void *)&tod_type_name;
       
  1970   if (is_type(left_type, tod_type_name_c)      && is_type(right_type, safetime_type_name_c)) 
       
  1971     return (void *)&tod_type_name;
       
  1972   if (is_type(left_type, safetod_type_name_c)  && is_type(right_type, safetime_type_name_c)) 
       
  1973     return (void *)&safetod_type_name;
       
  1974 
       
  1975   if (is_type(left_type, dt_type_name_c)       && is_type(right_type, time_type_name_c)) 
       
  1976     return (void *)&dt_type_name;
       
  1977   if (is_type(left_type, safedt_type_name_c)   && is_type(right_type, time_type_name_c)) 
       
  1978     return (void *)&dt_type_name;
       
  1979   if (is_type(left_type, dt_type_name_c)       && is_type(right_type, safetime_type_name_c)) 
       
  1980     return (void *)&dt_type_name;
       
  1981   if (is_type(left_type, safedt_type_name_c)   && is_type(right_type, safetime_type_name_c)) 
       
  1982     return (void *)&safedt_type_name;
       
  1983 
       
  1984   return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_MAGNITUDE_compatible, symbol->l_exp, symbol->r_exp);
       
  1985 }
       
  1986 
       
  1987 
       
  1988 void *visit_expression_type_c::visit(sub_expression_c *symbol) {
       
  1989   symbol_c *left_type  = base_type((symbol_c *)symbol->l_exp->accept(*this));
       
  1990   symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
       
  1991 
       
  1992 /* The following is already checked in compute_expression */
       
  1993 /*
       
  1994   if (is_type(left_type, time_type_name_c) && is_type(right_type, time_type_name_c))
       
  1995     return (void *)&time_type_name;
       
  1996 */
       
  1997 
       
  1998   if (is_type(left_type, tod_type_name_c)     && is_type(right_type, time_type_name_c))
       
  1999     return (void *)&tod_type_name;
       
  2000   if (is_type(left_type, safetod_type_name_c) && is_type(right_type, time_type_name_c))
       
  2001     return (void *)&tod_type_name;
       
  2002   if (is_type(left_type, tod_type_name_c)     && is_type(right_type, safetime_type_name_c))
       
  2003     return (void *)&tod_type_name;
       
  2004   if (is_type(left_type, safetod_type_name_c) && is_type(right_type, safetime_type_name_c))
       
  2005     return (void *)&safetod_type_name;
       
  2006 
       
  2007   if (is_type(left_type, dt_type_name_c)     && is_type(right_type, time_type_name_c))
       
  2008     return (void *)&dt_type_name;
       
  2009   if (is_type(left_type, safedt_type_name_c) && is_type(right_type, time_type_name_c))
       
  2010     return (void *)&dt_type_name;
       
  2011   if (is_type(left_type, dt_type_name_c)     && is_type(right_type, safetime_type_name_c))
       
  2012     return (void *)&dt_type_name;
       
  2013   if (is_type(left_type, safedt_type_name_c) && is_type(right_type, safetime_type_name_c))
       
  2014     return (void *)&safedt_type_name;
       
  2015 
       
  2016   if (is_type(left_type, tod_type_name_c)     && is_type(right_type, tod_type_name_c))
       
  2017     return (void *)&time_type_name;
       
  2018   if (is_type(left_type, safetod_type_name_c) && is_type(right_type, tod_type_name_c))
       
  2019     return (void *)&time_type_name;
       
  2020   if (is_type(left_type, tod_type_name_c)     && is_type(right_type, safetod_type_name_c))
       
  2021     return (void *)&time_type_name;
       
  2022   if (is_type(left_type, safetod_type_name_c) && is_type(right_type, safetod_type_name_c))
       
  2023     return (void *)&safetime_type_name;
       
  2024 
       
  2025   if (is_type(left_type, date_type_name_c)     && is_type(right_type, date_type_name_c))
       
  2026     return (void *)&time_type_name;
       
  2027   if (is_type(left_type, safedate_type_name_c) && is_type(right_type, date_type_name_c))
       
  2028     return (void *)&time_type_name;
       
  2029   if (is_type(left_type, date_type_name_c)     && is_type(right_type, safedate_type_name_c))
       
  2030     return (void *)&time_type_name;
       
  2031   if (is_type(left_type, safedate_type_name_c) && is_type(right_type, safedate_type_name_c))
       
  2032     return (void *)&safetime_type_name;
       
  2033 
       
  2034   if (is_type(left_type, dt_type_name_c)     && is_type(right_type, dt_type_name_c))
       
  2035     return (void *)&time_type_name;
       
  2036   if (is_type(left_type, safedt_type_name_c) && is_type(right_type, dt_type_name_c))
       
  2037     return (void *)&time_type_name;
       
  2038   if (is_type(left_type, dt_type_name_c)     && is_type(right_type, safedt_type_name_c))
       
  2039     return (void *)&time_type_name;
       
  2040   if (is_type(left_type, safedt_type_name_c) && is_type(right_type, safedt_type_name_c))
       
  2041     return (void *)&safetime_type_name;
       
  2042 
       
  2043   return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_MAGNITUDE_compatible, symbol->l_exp, symbol->r_exp);
       
  2044 }
       
  2045 
       
  2046 
       
  2047 void *visit_expression_type_c::visit(mul_expression_c *symbol) {
       
  2048   symbol_c *left_type  = base_type((symbol_c *)symbol->l_exp->accept(*this));
       
  2049   symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
       
  2050 
       
  2051   if (is_type(left_type, time_type_name_c)     && is_ANY_NUM_compatible(right_type)) 
       
  2052     return (void *)&time_type_name;
       
  2053   if (is_type(left_type, safetime_type_name_c) && is_ANY_NUM_type(right_type)) 
       
  2054     return (void *)&time_type_name;
       
  2055   if (is_type(left_type, safetime_type_name_c) && is_ANY_SAFENUM_type(right_type)) 
       
  2056     return (void *)&safetime_type_name;
       
  2057   /* Since we have already checked for ANY_NUM_type and ANY_SAFENUM_type in the previous lines,
       
  2058    * this next line is really only to check for integers/reals of undefined type on 'right_type'... 
       
  2059    */
       
  2060   if (is_type(left_type, safetime_type_name_c) && is_ANY_NUM_compatible(right_type)) 
       
  2061     return (void *)&safetime_type_name;
       
  2062 
       
  2063   return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_NUM_compatible, symbol->l_exp, symbol->r_exp);
       
  2064 }
       
  2065 
       
  2066 
       
  2067 void *visit_expression_type_c::visit(div_expression_c *symbol) {
       
  2068   symbol_c *left_type  = base_type((symbol_c *)symbol->l_exp->accept(*this));
       
  2069   symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
       
  2070 
       
  2071   if (is_type(left_type, time_type_name_c)     && is_ANY_NUM_compatible(right_type)) 
       
  2072     return (void *)&time_type_name;
       
  2073   if (is_type(left_type, safetime_type_name_c) && is_ANY_NUM_type(right_type)) 
       
  2074     return (void *)&time_type_name;
       
  2075   if (is_type(left_type, safetime_type_name_c) && is_ANY_SAFENUM_type(right_type)) 
       
  2076     return (void *)&safetime_type_name;
       
  2077   /* Since we have already checked for ANY_NUM_type and ANY_SAFENUM_type in the previous lines,
       
  2078    * this next line is really only to check for integers/reals of undefined type on 'right_type'... 
       
  2079    */
       
  2080   if (is_type(left_type, safetime_type_name_c) && is_ANY_NUM_compatible(right_type)) 
       
  2081     return (void *)&safetime_type_name;
       
  2082 
       
  2083   return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_NUM_compatible, symbol->l_exp, symbol->r_exp);
       
  2084 }
       
  2085 
       
  2086 
       
  2087 void *visit_expression_type_c::visit(mod_expression_c *symbol) {
       
  2088   symbol_c *left_type  = base_type((symbol_c *)symbol->l_exp->accept(*this));
       
  2089   symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
       
  2090   return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_INT_compatible, symbol->l_exp, symbol->r_exp);
       
  2091 }
       
  2092 
       
  2093 
       
  2094 void *visit_expression_type_c::visit(power_expression_c *symbol) {
       
  2095   symbol_c *left_type  = base_type((symbol_c *)symbol->l_exp->accept(*this));
       
  2096   symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
       
  2097   if (!is_ANY_REAL_compatible(left_type))
       
  2098     STAGE3_ERROR(symbol->l_exp, symbol->l_exp, "first operand of ** operator has invalid data type, should be of type ANY_REAL.");
       
  2099   if (!is_ANY_NUM_compatible(right_type))
       
  2100     STAGE3_ERROR(symbol->r_exp, symbol->r_exp, "second operand of ** operator has invalid data type, should be of type ANY_NUM.");
       
  2101 
       
  2102   return (void *)left_type;
       
  2103 }
       
  2104 
       
  2105 
       
  2106 void *visit_expression_type_c::visit(neg_expression_c *symbol) {
       
  2107   symbol_c *exp_type = base_type((symbol_c *)symbol->exp->accept(*this));
       
  2108   if (!is_ANY_MAGNITUDE_compatible(exp_type))
       
  2109     STAGE3_ERROR(symbol, symbol, "operand of negate expression '-' has invalid data type, should be of type ANY_MAGNITUDE.");
       
  2110 
       
  2111   return exp_type;
       
  2112 }
       
  2113 
       
  2114 
       
  2115 void *visit_expression_type_c::visit(not_expression_c *symbol) {
       
  2116   symbol_c *type = base_type((symbol_c *)symbol->exp->accept(*this));
       
  2117   return compute_expression(type, type, &visit_expression_type_c::is_ANY_BIT_compatible, NULL, symbol->exp);
       
  2118 }
       
  2119 
       
  2120 
       
  2121 void *visit_expression_type_c::visit(function_invocation_c *symbol) {
       
  2122   function_symtable_t::iterator lower = function_symtable.lower_bound(symbol->function_name);
       
  2123   function_symtable_t::iterator upper = function_symtable.upper_bound(symbol->function_name);
       
  2124   function_symtable_t::iterator current;
       
  2125   if (lower == function_symtable.end()) ERROR;
       
  2126 
       
  2127   symbol_c* return_data_type;
       
  2128   symbol_c* fdecl_return_type;
       
  2129   symbol_c* overloaded_data_type = NULL;
       
  2130   int extensible_param_count = -1;
       
  2131   symbol->called_function_declaration = NULL;
       
  2132 
       
  2133   function_symtable_t::iterator second = lower;
       
  2134   second++;
       
  2135   if (second == upper) {
       
  2136     /* call to a function that is not overloaded. */
       
  2137     /* now check the semantics of the function call... */
       
  2138     /* If the syntax parser is working correctly, exactly one of the 
       
  2139      * following two symbols will be NULL, while the other is != NULL.
       
  2140      */
       
  2141     function_declaration_c *f_decl = function_symtable.get_value(lower);
       
  2142     if (symbol->   formal_param_list != NULL) check_formal_call   (symbol, f_decl);
       
  2143     if (symbol->nonformal_param_list != NULL) check_nonformal_call(symbol, f_decl);
       
  2144     /* Store the pointer to the declaration of the function being called.
       
  2145      * This data will be used by stage 4 to call the correct function.
       
  2146      * Mostly needed to disambiguate overloaded functions...
       
  2147      * See comments in absyntax.def for more details
       
  2148      */
       
  2149     symbol->called_function_declaration = f_decl;
       
  2150     return base_type(f_decl->type_name);
       
  2151   }  
       
  2152 
       
  2153   /* This is a call to an overloaded function... */
       
  2154   if (debug) printf("visit_expression_type_c::visit(function_invocation_c *symbol): FOUND CALL TO OVERLOADED FUNCTION!!\n");
       
  2155   for(current = lower; current != upper; current++) {
       
  2156     if (debug) printf("visit_expression_type_c::visit(function_invocation_c *symbol): FOUND CALL TO OVERLOADED FUNCTION!! iterating...\n");
       
  2157     int error_count = 0; 
       
  2158     function_declaration_c *f_decl = function_symtable.get_value(current);
       
  2159     if (symbol->   formal_param_list != NULL) check_formal_call   (symbol, f_decl, &error_count);
       
  2160     if (symbol->nonformal_param_list != NULL) check_nonformal_call(symbol, f_decl, false, &error_count);
       
  2161     if (0 == error_count) {
       
  2162 
       
  2163       fdecl_return_type = base_type(f_decl->type_name);
       
  2164 
       
  2165       if (symbol->called_function_declaration == NULL) {
       
  2166         /* Store the pointer to the declaration of the function being called.
       
  2167          * This data will be used by stage 4 to call the correct function.
       
  2168          * Mostly needed to disambiguate overloaded functions...
       
  2169          * See comments in absyntax.def for more details
       
  2170          */
       
  2171         symbol->called_function_declaration = f_decl;
       
  2172         extensible_param_count = symbol->extensible_param_count;
       
  2173 
       
  2174         /* determine the base data type returned by the function being called... */
       
  2175         return_data_type = fdecl_return_type;
       
  2176       }
       
  2177       else if (typeid(*return_data_type) != typeid(*fdecl_return_type)){
       
  2178           return_data_type = common_literal(return_data_type, fdecl_return_type);
       
  2179           overloaded_data_type = overloaded_return_type(return_data_type);
       
  2180       }
       
  2181 
       
  2182       if (NULL == return_data_type) ERROR;
       
  2183     }
       
  2184   }
       
  2185 
       
  2186   if (overloaded_data_type != NULL) {
       
  2187     for(current = lower; current != upper; current++) {
       
  2188       function_declaration_c *f_decl = function_symtable.get_value(current);
       
  2189       int error_count = 0;
       
  2190       if (symbol->   formal_param_list != NULL) check_formal_call   (symbol, f_decl, &error_count);
       
  2191       if (symbol->nonformal_param_list != NULL) check_nonformal_call(symbol, f_decl, false, &error_count);
       
  2192       if (0 == error_count) {
       
  2193 
       
  2194         fdecl_return_type = base_type(f_decl->type_name);
       
  2195 
       
  2196         if (typeid(*overloaded_data_type) == typeid(*fdecl_return_type)){
       
  2197           /* Store the pointer to the declaration of the function being called.
       
  2198            * This data will be used by stage 4 to call the correct function.
       
  2199            * Mostly needed to disambiguate overloaded functions...
       
  2200            * See comments in absyntax.def for more details
       
  2201            */
       
  2202           symbol->called_function_declaration = f_decl;
       
  2203           extensible_param_count = symbol->extensible_param_count;
       
  2204         }
       
  2205       }
       
  2206     }
       
  2207   }
       
  2208 
       
  2209   if (return_data_type != NULL) {
       
  2210 	symbol->extensible_param_count = extensible_param_count;
       
  2211 	return return_data_type;
       
  2212   }
       
  2213 
       
  2214   /* No compatible function was found for this function call */
       
  2215   STAGE3_ERROR(symbol, symbol, "Call to an overloaded function with invalid parameter type.");
       
  2216   return NULL;
       
  2217 }
       
  2218 
       
  2219 /********************/
       
  2220 /* B 3.2 Statements */
       
  2221 /********************/
       
  2222 // SYM_LIST(statement_list_c)
       
  2223 /* The visitor of the base class search_visitor_c will handle calling each instruction in the list.
       
  2224  * We do not need to do anything here...
       
  2225  */
       
  2226 // void *visit_expression_type_c::visit(statement_list_c *symbol)
       
  2227 
       
  2228 
       
  2229 /*********************************/
       
  2230 /* B 3.2.1 Assignment Statements */
       
  2231 /*********************************/
       
  2232 
       
  2233 void *visit_expression_type_c::visit(assignment_statement_c *symbol) {
       
  2234   symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this));
       
  2235   symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
       
  2236 
       
  2237   if (debug) {
       
  2238     printf("visit_expression_type_c::visit(assignment_statement_c) called. Checking --->");  
       
  2239     symbolic_variable_c *hi = dynamic_cast<symbolic_variable_c *>(symbol->l_exp);  
       
  2240     if (hi != NULL) {
       
  2241       identifier_c *hi1 = dynamic_cast<identifier_c *>(hi->var_name);  
       
  2242       if (hi1 != NULL) printf("%s", hi1->value);
       
  2243     }
       
  2244     printf(" := ");
       
  2245     hex_integer_c *hi2 = dynamic_cast<hex_integer_c *>(symbol->r_exp);  
       
  2246     if (hi2 != NULL) printf("%s", hi2->value);
       
  2247     printf("\n");
       
  2248   } // if (debug)
       
  2249 
       
  2250   if        (NULL == left_type) {
       
  2251     STAGE3_ERROR(symbol->l_exp, symbol->l_exp, "Could not determine data type of expression (undefined variable, constant, or structure element?).\n");
       
  2252   } else if (NULL == right_type) {
       
  2253     STAGE3_ERROR(symbol->r_exp, symbol->r_exp, "Could not determine data type of expression (undefined variable, constant, or structure element?).\n");
       
  2254   } else if (!is_valid_assignment(left_type, right_type))
       
  2255     STAGE3_ERROR(symbol, symbol, "data type mismatch in assignment statement!\n");
       
  2256 
       
  2257   return NULL;
       
  2258 }
       
  2259 
       
  2260 
       
  2261 
       
  2262 /*****************************************/
       
  2263 /* B 3.2.2 Subprogram Control Statements */
       
  2264 /*****************************************/
       
  2265 
       
  2266 /*  RETURN */
       
  2267 // SYM_REF0(return_statement_c)
       
  2268 
       
  2269 
       
  2270 /* fb_name '(' [param_assignment_list] ')' */
       
  2271 /* param_assignment_list -> may be NULL ! */
       
  2272 // SYM_REF3(fb_invocation_c, fb_name, formal_param_list, nonformal_param_list)
       
  2273 void *visit_expression_type_c::visit(fb_invocation_c *symbol) {
       
  2274   symbol_c *fb_decl = search_varfb_instance_type->get_basetype_decl(symbol->fb_name);
       
  2275     /* The following should never occur. The function block must be defined, 
       
  2276      * and the FB type being called MUST be in the symtable... 
       
  2277      * This was all already checked at stage 2!
       
  2278      */
       
  2279   if (NULL == fb_decl) ERROR;
       
  2280 
       
  2281   /* now check the semantics of the fb call... */
       
  2282   /* If the syntax parser is working correctly, exactly one of the 
       
  2283    * following two symbols will be NULL, while the other is != NULL.
       
  2284    */
       
  2285   if (symbol->   formal_param_list != NULL) check_formal_call   (symbol, fb_decl);
       
  2286   if (symbol->nonformal_param_list != NULL) check_nonformal_call(symbol, fb_decl);
       
  2287 
       
  2288   return NULL;
       
  2289 }
       
  2290 
       
  2291 
       
  2292 #if 0
       
  2293 /* helper symbol for fb_invocation */
       
  2294 /* param_assignment_list ',' param_assignment */
       
  2295 SYM_LIST(param_assignment_list_c)
       
  2296 
       
  2297 /*  variable_name ASSIGN expression */
       
  2298 SYM_REF2(input_variable_param_assignment_c, variable_name, expression)
       
  2299 
       
  2300 /* [NOT] variable_name '=>' variable */
       
  2301 SYM_REF3(output_variable_param_assignment_c, not_param, variable_name, variable)
       
  2302 
       
  2303 /* helper CLASS for output_variable_param_assignment */
       
  2304 SYM_REF0(not_paramassign_c)
       
  2305 #endif
       
  2306 
       
  2307 /********************************/
       
  2308 /* B 3.2.3 Selection Statements */
       
  2309 /********************************/
       
  2310 
       
  2311 /* IF expression THEN statement_list elseif_statement_list ELSE statement_list END_IF */
       
  2312 // SYM_REF4(if_statement_c, expression, statement_list, elseif_statement_list, else_statement_list)
       
  2313 void *visit_expression_type_c::visit(if_statement_c *symbol) {
       
  2314   symbol_c *expr_type = base_type((symbol_c*)symbol->expression->accept(*this));
       
  2315   if (!is_BOOL_type(expr_type)) STAGE3_ERROR(symbol->expression,symbol->expression,"IF conditional expression is not of boolean type.");
       
  2316   if (NULL != symbol->statement_list)
       
  2317     symbol->statement_list->accept(*this); 
       
  2318   if (NULL != symbol->elseif_statement_list)  
       
  2319     symbol->elseif_statement_list->accept(*this);
       
  2320   if (NULL != symbol->else_statement_list)  
       
  2321     symbol->else_statement_list->accept(*this);  
       
  2322   return NULL;
       
  2323 }
       
  2324 
       
  2325 /* helper symbol for if_statement */
       
  2326 // SYM_LIST(elseif_statement_list_c)
       
  2327 // void *visit_expression_type_c::visit(elseif_statement_list_c *symbol) { }
       
  2328 
       
  2329 /* helper symbol for elseif_statement_list */
       
  2330 /* ELSIF expression THEN statement_list    */
       
  2331 // SYM_REF2(elseif_statement_c, expression, statement_list)
       
  2332 void *visit_expression_type_c::visit(elseif_statement_c *symbol) {
       
  2333   symbol_c *elseif_expr_type = base_type((symbol_c*)symbol->expression->accept(*this));
       
  2334   if(!is_BOOL_type(elseif_expr_type)) STAGE3_ERROR(symbol->expression,symbol->expression,"ELSIF conditional expression is not of boolean type.");
       
  2335   if (NULL != symbol->statement_list)
       
  2336     symbol->statement_list->accept(*this); 
       
  2337   return NULL;
       
  2338 }
       
  2339 
       
  2340 
       
  2341 /* CASE expression OF case_element_list ELSE statement_list END_CASE */
       
  2342 // SYM_REF3(case_statement_c, expression, case_element_list, statement_list)
       
  2343 void *visit_expression_type_c::visit(case_statement_c *symbol) {
       
  2344   case_expression_type = base_type((symbol_c*)symbol->expression->accept(*this));
       
  2345   if (NULL != case_expression_type) {
       
  2346     if (NULL != symbol->case_element_list)
       
  2347       symbol->case_element_list->accept(*this);
       
  2348   }
       
  2349   if (NULL != symbol->statement_list)
       
  2350     symbol->statement_list->accept(*this);
       
  2351   return NULL;
       
  2352 }
       
  2353 
       
  2354 #if 0
       
  2355 /* helper symbol for case_statement */
       
  2356 // SYM_LIST(case_element_list_c)
       
  2357 // void *visit_expression_type_c::visit(case_element_list_c *symbol);
       
  2358 
       
  2359 /*  case_list ':' statement_list */
       
  2360 // SYM_REF2(case_element_c, case_list, statement_list)
       
  2361 void *visit_expression_type_c::visit(case_element_c *symbol);  
       
  2362 #endif
       
  2363 
       
  2364 // SYM_LIST(case_list_c)
       
  2365 void *visit_expression_type_c::visit(case_list_c *symbol) {
       
  2366   symbol_c *element_type;
       
  2367   for(int i = 0; i < symbol->n; i++) {
       
  2368     element_type = (symbol_c *)symbol->elements[i]->accept(*this);
       
  2369     if (NULL == element_type) {
       
  2370       STAGE3_ERROR(symbol->elements[i], symbol->elements[i], "Case list element has undefined data type.");
       
  2371     } else {
       
  2372       element_type = base_type(element_type);
       
  2373       if (NULL != element_type){
       
  2374         /* The CASE value is only used for comparison (and not assingment), so we only check for compatibility! */ 
       
  2375         if (!is_compatible_type(case_expression_type, element_type))
       
  2376           STAGE3_ERROR(symbol->elements[i], symbol->elements[i], "Invalid data type of case list element.");
       
  2377       }
       
  2378     }
       
  2379   }
       
  2380   return NULL;
       
  2381 }
       
  2382 
       
  2383 /********************************/
       
  2384 /* B 3.2.4 Iteration Statements */
       
  2385 /********************************/
       
  2386 
       
  2387 /*  FOR control_variable ASSIGN expression TO expression [BY expression] DO statement_list END_FOR */
       
  2388 // SYM_REF5(for_statement_c, control_variable, beg_expression, end_expression, by_expression, statement_list)
       
  2389 void *visit_expression_type_c::visit(for_statement_c *symbol) {
       
  2390   symbol_c *var_type = (symbol_c*)symbol->control_variable->accept(*this);
       
  2391   if (NULL == var_type) ERROR;
       
  2392   var_type = base_type(var_type);
       
  2393   if (NULL == var_type) ERROR;
       
  2394   // ASSIGN
       
  2395   symbol_c *beg_expr_type = base_type((symbol_c*)symbol->beg_expression->accept(*this));
       
  2396   if (NULL != beg_expr_type) {
       
  2397     /* The BEG value is assigned to the variable, so we check for assignment validity! */ 
       
  2398     if(!is_valid_assignment(var_type, beg_expr_type)) 
       
  2399       STAGE3_ERROR(symbol->beg_expression, symbol->beg_expression, "Data type mismatch between control variable and initial value.");
       
  2400   }
       
  2401   // TO
       
  2402   symbol_c *end_expr_type = base_type((symbol_c*)symbol->end_expression->accept(*this));
       
  2403   if (NULL != end_expr_type) { 
       
  2404     /* The TO value is only used for comparison, so we only check for compatibility! */ 
       
  2405     if(!is_compatible_type(var_type, end_expr_type)) 
       
  2406       STAGE3_ERROR(symbol->end_expression, symbol->end_expression, "Data type mismatch between control variable and final value.");
       
  2407   }
       
  2408   // BY
       
  2409   if(symbol->by_expression != NULL) {
       
  2410     symbol_c *by_expr_type = base_type((symbol_c*)symbol->by_expression->accept(*this));
       
  2411     if (NULL != end_expr_type) {   
       
  2412       /* The BY value is used in an expression (add, sub, ...), so we only check for compatibility! */ 
       
  2413       if(!is_compatible_type(var_type, by_expr_type)) 
       
  2414         STAGE3_ERROR(symbol->by_expression, symbol->by_expression, "Data type mismatch between control variable and BY value.");
       
  2415     }
       
  2416   }
       
  2417   // DO
       
  2418   if (NULL != symbol->statement_list)
       
  2419     symbol->statement_list->accept(*this); 
       
  2420   return NULL;
       
  2421 }
       
  2422 
       
  2423 
       
  2424 /*  WHILE expression DO statement_list END_WHILE */
       
  2425 // SYM_REF2(while_statement_c, expression, statement_list)
       
  2426 void *visit_expression_type_c::visit(while_statement_c *symbol) {
       
  2427   symbol_c *expr_type = base_type((symbol_c*)symbol->expression->accept(*this));
       
  2428   if (NULL != expr_type) {
       
  2429     if(!is_BOOL_type(expr_type)) 
       
  2430       STAGE3_ERROR(symbol->expression,symbol->expression,"WHILE conditional expression is not of boolean type.");
       
  2431   }
       
  2432  
       
  2433   if (NULL != symbol->statement_list)
       
  2434     symbol->statement_list->accept(*this); 
       
  2435   return NULL;
       
  2436 }
       
  2437 
       
  2438 /*  REPEAT statement_list UNTIL expression END_REPEAT */
       
  2439 // SYM_REF2(repeat_statement_c, statement_list, expression)
       
  2440 void *visit_expression_type_c::visit(repeat_statement_c *symbol) {
       
  2441   if (NULL != symbol->statement_list)
       
  2442     symbol->statement_list->accept(*this); 
       
  2443  
       
  2444   symbol_c *expr_type = base_type((symbol_c*)symbol->expression->accept(*this));
       
  2445   if (NULL != expr_type) {
       
  2446     if(!is_BOOL_type(expr_type)) 
       
  2447       STAGE3_ERROR(symbol->expression,symbol->expression,"REPEAT conditional expression is not of boolean type.");
       
  2448   }
       
  2449   return NULL;
       
  2450 }
       
  2451 
       
  2452 /*  EXIT */
       
  2453 // SYM_REF0(exit_statement_c)
       
  2454 
       
  2455 
       
  2456