stage3/datatype_functions.cc
changeset 417 d48f53715f77
child 425 c8e6cf57324a
equal deleted inserted replaced
416:0c2ef191b22a 417:d48f53715f77
       
     1 /*
       
     2  *  matiec - a compiler for the programming languages defined in IEC 61131-3
       
     3  *
       
     4  *  Copyright (C) 2009-2012  Mario de Sousa (msousa@fe.up.pt)
       
     5  *  Copyright (C) 2012       Manuele Conti  (conti.ma@alice.it)
       
     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 #include "datatype_functions.hh"
       
    26 #include "../absyntax_utils/absyntax_utils.hh"
       
    27 
       
    28 /*
       
    29  * 2.5.1.5.6 Functions of time data types
       
    30  * Table 30 - page 64
       
    31  */
       
    32 
       
    33 const struct widen_entry widen_ADD_table[] = {
       
    34     { &search_constant_type_c::time_type_name,          &search_constant_type_c::time_type_name,            &search_constant_type_c::time_type_name     },
       
    35     { &search_constant_type_c::tod_type_name,           &search_constant_type_c::tod_type_name,             &search_constant_type_c::tod_type_name      },
       
    36     { &search_constant_type_c::dt_type_name,            &search_constant_type_c::dt_type_name,              &search_constant_type_c::dt_type_name       },
       
    37     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::safetime_type_name,        &search_constant_type_c::safetime_type_name },
       
    38     { &search_constant_type_c::safetod_type_name,       &search_constant_type_c::safetod_type_name,         &search_constant_type_c::safetod_type_name  },
       
    39     { &search_constant_type_c::safedt_type_name,        &search_constant_type_c::safedt_type_name,          &search_constant_type_c::safedt_type_name   },
       
    40     { &search_constant_type_c::tod_type_name,           &search_constant_type_c::time_type_name,            &search_constant_type_c::tod_type_name      },
       
    41     { &search_constant_type_c::safetod_type_name,       &search_constant_type_c::time_type_name,            &search_constant_type_c::tod_type_name      },
       
    42     { &search_constant_type_c::tod_type_name,           &search_constant_type_c::safetime_type_name,        &search_constant_type_c::tod_type_name      },
       
    43     { &search_constant_type_c::safetod_type_name,       &search_constant_type_c::safetime_type_name,        &search_constant_type_c::safetod_type_name  },
       
    44     { &search_constant_type_c::dt_type_name,            &search_constant_type_c::time_type_name,            &search_constant_type_c::dt_type_name       },
       
    45     { &search_constant_type_c::safedt_type_name,        &search_constant_type_c::time_type_name,            &search_constant_type_c::dt_type_name       },
       
    46     { &search_constant_type_c::dt_type_name,            &search_constant_type_c::safetime_type_name,        &search_constant_type_c::dt_type_name       },
       
    47     { &search_constant_type_c::safedt_type_name,        &search_constant_type_c::safetime_type_name,        &search_constant_type_c::safedt_type_name   },
       
    48     { NULL, NULL, NULL },
       
    49 };
       
    50 
       
    51 
       
    52 const struct widen_entry widen_SUB_table[] = {
       
    53 	{ &search_constant_type_c::time_type_name,          &search_constant_type_c::time_type_name,            &search_constant_type_c::time_type_name     },
       
    54 	{ &search_constant_type_c::tod_type_name,           &search_constant_type_c::tod_type_name,             &search_constant_type_c::tod_type_name      },
       
    55 	{ &search_constant_type_c::dt_type_name,            &search_constant_type_c::dt_type_name,              &search_constant_type_c::dt_type_name       },
       
    56 	{ &search_constant_type_c::safetime_type_name,      &search_constant_type_c::safetime_type_name,        &search_constant_type_c::safetime_type_name },
       
    57 	{ &search_constant_type_c::safetod_type_name,       &search_constant_type_c::safetod_type_name,         &search_constant_type_c::safetod_type_name  },
       
    58 	{ &search_constant_type_c::safedt_type_name,        &search_constant_type_c::safedt_type_name,          &search_constant_type_c::safedt_type_name   },
       
    59 
       
    60 	{ &search_constant_type_c::tod_type_name,           &search_constant_type_c::time_type_name,            &search_constant_type_c::tod_type_name      },
       
    61     { &search_constant_type_c::safetod_type_name,       &search_constant_type_c::time_type_name,            &search_constant_type_c::tod_type_name      },
       
    62     { &search_constant_type_c::tod_type_name,           &search_constant_type_c::safetime_type_name,        &search_constant_type_c::tod_type_name      },
       
    63     { &search_constant_type_c::safetod_type_name,       &search_constant_type_c::safetime_type_name,        &search_constant_type_c::safetod_type_name  },
       
    64     { &search_constant_type_c::dt_type_name,            &search_constant_type_c::time_type_name,            &search_constant_type_c::dt_type_name       },
       
    65     { &search_constant_type_c::safedt_type_name,        &search_constant_type_c::time_type_name,            &search_constant_type_c::dt_type_name       },
       
    66     { &search_constant_type_c::dt_type_name,            &search_constant_type_c::safetime_type_name,        &search_constant_type_c::dt_type_name       },
       
    67     { &search_constant_type_c::safedt_type_name,        &search_constant_type_c::safetime_type_name,        &search_constant_type_c::safedt_type_name   },
       
    68     { &search_constant_type_c::tod_type_name,           &search_constant_type_c::tod_type_name,             &search_constant_type_c::time_type_name     },
       
    69     { &search_constant_type_c::safetod_type_name,       &search_constant_type_c::tod_type_name,             &search_constant_type_c::time_type_name     },
       
    70     { &search_constant_type_c::tod_type_name,           &search_constant_type_c::safetod_type_name,         &search_constant_type_c::time_type_name     },
       
    71     { &search_constant_type_c::safetod_type_name,       &search_constant_type_c::safetod_type_name,         &search_constant_type_c::safetime_type_name },
       
    72     { &search_constant_type_c::date_type_name,          &search_constant_type_c::date_type_name,            &search_constant_type_c::time_type_name     },
       
    73     { &search_constant_type_c::safedate_type_name,      &search_constant_type_c::date_type_name,            &search_constant_type_c::time_type_name     },
       
    74     { &search_constant_type_c::date_type_name,          &search_constant_type_c::safedate_type_name,        &search_constant_type_c::time_type_name     },
       
    75     { &search_constant_type_c::safedate_type_name,      &search_constant_type_c::safedate_type_name,        &search_constant_type_c::safetime_type_name },
       
    76     { &search_constant_type_c::dt_type_name,            &search_constant_type_c::dt_type_name,              &search_constant_type_c::time_type_name     },
       
    77     { &search_constant_type_c::safedt_type_name,        &search_constant_type_c::dt_type_name,              &search_constant_type_c::time_type_name     },
       
    78     { &search_constant_type_c::dt_type_name,            &search_constant_type_c::safedt_type_name,          &search_constant_type_c::time_type_name     },
       
    79     { &search_constant_type_c::safedt_type_name,        &search_constant_type_c::safedt_type_name,          &search_constant_type_c::safetime_type_name },
       
    80     { NULL, NULL, NULL },
       
    81 };
       
    82 
       
    83 const struct widen_entry widen_MUL_table[] = {
       
    84     { &search_constant_type_c::time_type_name,          &search_constant_type_c::lreal_type_name,           &search_constant_type_c::time_type_name     },
       
    85     { &search_constant_type_c::time_type_name,          &search_constant_type_c::real_type_name,            &search_constant_type_c::time_type_name     },
       
    86     { &search_constant_type_c::time_type_name,          &search_constant_type_c::dint_type_name,            &search_constant_type_c::time_type_name     },
       
    87     { &search_constant_type_c::time_type_name,          &search_constant_type_c::int_type_name,             &search_constant_type_c::time_type_name     },
       
    88     { &search_constant_type_c::time_type_name,          &search_constant_type_c::sint_type_name,            &search_constant_type_c::time_type_name     },
       
    89     { &search_constant_type_c::time_type_name,          &search_constant_type_c::ulint_type_name,           &search_constant_type_c::time_type_name     },
       
    90     { &search_constant_type_c::time_type_name,          &search_constant_type_c::udint_type_name,           &search_constant_type_c::time_type_name     },
       
    91     { &search_constant_type_c::time_type_name,          &search_constant_type_c::uint_type_name,            &search_constant_type_c::time_type_name     },
       
    92     { &search_constant_type_c::time_type_name,          &search_constant_type_c::usint_type_name,           &search_constant_type_c::time_type_name     },
       
    93 
       
    94     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::lreal_type_name,           &search_constant_type_c::safetime_type_name },
       
    95     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::real_type_name,            &search_constant_type_c::safetime_type_name },
       
    96     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::dint_type_name,            &search_constant_type_c::safetime_type_name },
       
    97     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::int_type_name,             &search_constant_type_c::safetime_type_name },
       
    98     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::sint_type_name,            &search_constant_type_c::safetime_type_name },
       
    99     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::ulint_type_name,           &search_constant_type_c::safetime_type_name },
       
   100     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::udint_type_name,           &search_constant_type_c::safetime_type_name },
       
   101     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::uint_type_name,            &search_constant_type_c::safetime_type_name },
       
   102     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::usint_type_name,           &search_constant_type_c::safetime_type_name },
       
   103 
       
   104     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::safelreal_type_name,       &search_constant_type_c::safetime_type_name },
       
   105     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::safereal_type_name,        &search_constant_type_c::safetime_type_name },
       
   106     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::safedint_type_name,        &search_constant_type_c::safetime_type_name },
       
   107     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::safeint_type_name,         &search_constant_type_c::safetime_type_name },
       
   108     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::safesint_type_name,        &search_constant_type_c::safetime_type_name },
       
   109     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::safeulint_type_name,       &search_constant_type_c::safetime_type_name },
       
   110     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::safeudint_type_name,       &search_constant_type_c::safetime_type_name },
       
   111     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::safeuint_type_name,        &search_constant_type_c::safetime_type_name },
       
   112     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::safeusint_type_name,       &search_constant_type_c::safetime_type_name },
       
   113 
       
   114     { NULL, NULL, NULL },
       
   115 };
       
   116 
       
   117 const struct widen_entry widen_DIV_table[] = {
       
   118 	{ &search_constant_type_c::time_type_name,          &search_constant_type_c::lreal_type_name,           &search_constant_type_c::time_type_name     },
       
   119     { &search_constant_type_c::time_type_name,          &search_constant_type_c::real_type_name,            &search_constant_type_c::time_type_name     },
       
   120     { &search_constant_type_c::time_type_name,          &search_constant_type_c::dint_type_name,            &search_constant_type_c::time_type_name     },
       
   121     { &search_constant_type_c::time_type_name,          &search_constant_type_c::int_type_name,             &search_constant_type_c::time_type_name     },
       
   122     { &search_constant_type_c::time_type_name,          &search_constant_type_c::sint_type_name,            &search_constant_type_c::time_type_name     },
       
   123     { &search_constant_type_c::time_type_name,          &search_constant_type_c::ulint_type_name,           &search_constant_type_c::time_type_name     },
       
   124     { &search_constant_type_c::time_type_name,          &search_constant_type_c::udint_type_name,           &search_constant_type_c::time_type_name     },
       
   125     { &search_constant_type_c::time_type_name,          &search_constant_type_c::uint_type_name,            &search_constant_type_c::time_type_name     },
       
   126     { &search_constant_type_c::time_type_name,          &search_constant_type_c::usint_type_name,           &search_constant_type_c::time_type_name     },
       
   127 
       
   128     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::lreal_type_name,           &search_constant_type_c::safetime_type_name },
       
   129     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::real_type_name,            &search_constant_type_c::safetime_type_name },
       
   130     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::dint_type_name,            &search_constant_type_c::safetime_type_name },
       
   131     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::int_type_name,             &search_constant_type_c::safetime_type_name },
       
   132     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::sint_type_name,            &search_constant_type_c::safetime_type_name },
       
   133     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::ulint_type_name,           &search_constant_type_c::safetime_type_name },
       
   134     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::udint_type_name,           &search_constant_type_c::safetime_type_name },
       
   135     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::uint_type_name,            &search_constant_type_c::safetime_type_name },
       
   136     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::usint_type_name,           &search_constant_type_c::safetime_type_name },
       
   137 
       
   138     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::safelreal_type_name,       &search_constant_type_c::safetime_type_name },
       
   139     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::safereal_type_name,        &search_constant_type_c::safetime_type_name },
       
   140     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::safedint_type_name,        &search_constant_type_c::safetime_type_name },
       
   141     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::safeint_type_name,         &search_constant_type_c::safetime_type_name },
       
   142     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::safesint_type_name,        &search_constant_type_c::safetime_type_name },
       
   143     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::safeulint_type_name,       &search_constant_type_c::safetime_type_name },
       
   144     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::safeudint_type_name,       &search_constant_type_c::safetime_type_name },
       
   145     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::safeuint_type_name,        &search_constant_type_c::safetime_type_name },
       
   146     { &search_constant_type_c::safetime_type_name,      &search_constant_type_c::safeusint_type_name,       &search_constant_type_c::safetime_type_name },
       
   147 
       
   148     { NULL, NULL, NULL },
       
   149 };
       
   150 
       
   151 /* NOTE on data type handling and literals...
       
   152  * ==========================================
       
   153  *
       
   154  * Literals that are explicitly type cast
       
   155  *   e.g.:   BYTE#42
       
   156  *           INT#65
       
   157  *           TIME#45h23m
       
   158  *               etc...
       
   159  *  are NOT considered literals in the following code.
       
   160  *  Since they are type cast, and their data type is fixed and well known,
       
   161  *  they are treated as a variable of that data type (except when determining lvalues)
       
   162  *  In other words, when calling search_constant_type_c on these constants, it returns
       
   163  *  a xxxxx_type_name_c, and not one of the xxxx_literal_c !
       
   164  *
       
   165  *  When the following code handles a literal, it is really a literal of unknown data type.
       
   166  *    e.g.   42, may be considered an int, a byte, a word, etc...
       
   167  *
       
   168  * NOTE: type_symbol == NULL is valid!
       
   169  *       This will occur, for example, when and undefined/undeclared symbolic_variable is used in the program.
       
   170  *       This will not be of any type, so we always return false.
       
   171  */
       
   172 
       
   173 /* A helper function... */
       
   174 bool is_ANY_ELEMENTARY_type(symbol_c *type_symbol) {
       
   175   if (type_symbol == NULL) {return false;}
       
   176   return is_ANY_MAGNITUDE_type(type_symbol)
       
   177       || is_ANY_BIT_type      (type_symbol)
       
   178       || is_ANY_STRING_type   (type_symbol)
       
   179       || is_ANY_DATE_type     (type_symbol);
       
   180 }
       
   181 
       
   182 /* A helper function... */
       
   183 bool is_ANY_SAFEELEMENTARY_type(symbol_c *type_symbol) {
       
   184   if (type_symbol == NULL) {return false;}
       
   185   return is_ANY_SAFEMAGNITUDE_type(type_symbol)
       
   186       || is_ANY_SAFEBIT_type      (type_symbol)
       
   187       || is_ANY_SAFESTRING_type   (type_symbol)
       
   188       || is_ANY_SAFEDATE_type     (type_symbol);
       
   189 }
       
   190 
       
   191 /* A helper function... */
       
   192 bool is_ANY_ELEMENTARY_compatible(symbol_c *type_symbol) {
       
   193   if (type_symbol == NULL) {return false;}
       
   194   /* NOTE: doing
       
   195    *          return is_ANY_SAFEELEMENTARY_type() || is_ANY_ELEMENTARY_type()
       
   196    *       is incorrect, as the literals would never be considered compatible...
       
   197    */
       
   198   return is_ANY_MAGNITUDE_compatible(type_symbol)
       
   199       || is_ANY_BIT_compatible      (type_symbol)
       
   200       || is_ANY_STRING_compatible   (type_symbol)
       
   201       || is_ANY_DATE_compatible     (type_symbol);
       
   202 }
       
   203 
       
   204 
       
   205 /* A helper function... */
       
   206 bool is_ANY_MAGNITUDE_type(symbol_c *type_symbol) {
       
   207   if (type_symbol == NULL) {return false;}
       
   208   if (typeid(*type_symbol) == typeid(time_type_name_c)) {return true;}
       
   209   return is_ANY_NUM_type(type_symbol);
       
   210 }
       
   211 
       
   212 /* A helper function... */
       
   213 bool is_ANY_SAFEMAGNITUDE_type(symbol_c *type_symbol) {
       
   214   if (type_symbol == NULL) {return false;}
       
   215   if (typeid(*type_symbol) == typeid(safetime_type_name_c)) {return true;}
       
   216   return is_ANY_SAFENUM_type(type_symbol);
       
   217 }
       
   218 
       
   219 /* A helper function... */
       
   220 bool is_ANY_MAGNITUDE_compatible(symbol_c *type_symbol) {
       
   221   if (type_symbol == NULL) {return false;}
       
   222   if (is_ANY_MAGNITUDE_type    (type_symbol))              {return true;}
       
   223   if (is_ANY_SAFEMAGNITUDE_type(type_symbol))              {return true;}
       
   224 
       
   225   return is_ANY_NUM_compatible(type_symbol);
       
   226 }
       
   227 
       
   228 /* A helper function... */
       
   229 bool is_ANY_NUM_type(symbol_c *type_symbol) {
       
   230   if (type_symbol == NULL) {return false;}
       
   231   if (is_ANY_REAL_type(type_symbol))                       {return true;}
       
   232   if (is_ANY_INT_type(type_symbol))                        {return true;}
       
   233   return false;
       
   234 }
       
   235 
       
   236 /* A helper function... */
       
   237 bool is_ANY_SAFENUM_type(symbol_c *type_symbol) {
       
   238   if (type_symbol == NULL) {return false;}
       
   239   return is_ANY_SAFEREAL_type(type_symbol)
       
   240       || is_ANY_SAFEINT_type (type_symbol);
       
   241 }
       
   242 
       
   243 /* A helper function... */
       
   244 bool is_ANY_NUM_compatible(symbol_c *type_symbol) {
       
   245   if (type_symbol == NULL) {return false;}
       
   246   if (is_ANY_REAL_compatible(type_symbol))                       {return true;}
       
   247   if (is_ANY_INT_compatible(type_symbol))                        {return true;}
       
   248   return false;
       
   249 }
       
   250 
       
   251 /* A helper function... */
       
   252 bool is_ANY_DATE_type(symbol_c *type_symbol) {
       
   253   if (type_symbol == NULL) {return false;}
       
   254   if (typeid(*type_symbol) == typeid(date_type_name_c)) {return true;}
       
   255   if (typeid(*type_symbol) == typeid(tod_type_name_c))  {return true;}
       
   256   if (typeid(*type_symbol) == typeid(dt_type_name_c))   {return true;}
       
   257   return false;
       
   258 }
       
   259 
       
   260 /* A helper function... */
       
   261 bool is_ANY_SAFEDATE_type(symbol_c *type_symbol) {
       
   262   if (type_symbol == NULL) {return false;}
       
   263   if (typeid(*type_symbol) == typeid(safedate_type_name_c)) {return true;}
       
   264   if (typeid(*type_symbol) == typeid(safetod_type_name_c))  {return true;}
       
   265   if (typeid(*type_symbol) == typeid(safedt_type_name_c))   {return true;}
       
   266   return false;
       
   267 }
       
   268 
       
   269 /* A helper function... */
       
   270 bool is_ANY_DATE_compatible(symbol_c *type_symbol) {
       
   271   if (type_symbol == NULL) {return false;}
       
   272   if (is_ANY_DATE_type    (type_symbol))              {return true;}
       
   273   if (is_ANY_SAFEDATE_type(type_symbol))              {return true;}
       
   274   return false;
       
   275 }
       
   276 
       
   277 /* A helper function... */
       
   278 bool is_ANY_STRING_type(symbol_c *type_symbol) {
       
   279   if (type_symbol == NULL) {return false;}
       
   280   if (typeid(*type_symbol) == typeid(string_type_name_c)) {return true;}
       
   281   if (typeid(*type_symbol) == typeid(wstring_type_name_c)) {return true;}
       
   282 // TODO literal_string ???
       
   283   return false;
       
   284 }
       
   285 
       
   286 /* A helper function... */
       
   287 bool is_ANY_SAFESTRING_type(symbol_c *type_symbol) {
       
   288   if (type_symbol == NULL) {return false;}
       
   289   if (typeid(*type_symbol) == typeid(safestring_type_name_c)) {return true;}
       
   290   if (typeid(*type_symbol) == typeid(safewstring_type_name_c)) {return true;}
       
   291   return false;
       
   292 }
       
   293 
       
   294 /* A helper function... */
       
   295 bool is_ANY_STRING_compatible(symbol_c *type_symbol) {
       
   296   if (type_symbol == NULL) {return false;}
       
   297   if (is_ANY_STRING_type    (type_symbol))              {return true;}
       
   298   if (is_ANY_SAFESTRING_type(type_symbol))              {return true;}
       
   299   return false;
       
   300 }
       
   301 
       
   302 /* A helper function... */
       
   303 bool is_ANY_INT_type(symbol_c *type_symbol) {
       
   304   if (type_symbol == NULL) {return false;}
       
   305   if (typeid(*type_symbol) == typeid(sint_type_name_c))  {return true;}
       
   306   if (typeid(*type_symbol) == typeid(int_type_name_c))   {return true;}
       
   307   if (typeid(*type_symbol) == typeid(dint_type_name_c))  {return true;}
       
   308   if (typeid(*type_symbol) == typeid(lint_type_name_c))  {return true;}
       
   309   if (typeid(*type_symbol) == typeid(usint_type_name_c)) {return true;}
       
   310   if (typeid(*type_symbol) == typeid(uint_type_name_c))  {return true;}
       
   311   if (typeid(*type_symbol) == typeid(udint_type_name_c)) {return true;}
       
   312   if (typeid(*type_symbol) == typeid(ulint_type_name_c)) {return true;}
       
   313   return false;
       
   314 }
       
   315 
       
   316 /* A helper function... */
       
   317 bool is_ANY_SAFEINT_type(symbol_c *type_symbol) {
       
   318   if (type_symbol == NULL) {return false;}
       
   319   if (typeid(*type_symbol) == typeid(safesint_type_name_c))  {return true;}
       
   320   if (typeid(*type_symbol) == typeid(safeint_type_name_c))   {return true;}
       
   321   if (typeid(*type_symbol) == typeid(safedint_type_name_c))  {return true;}
       
   322   if (typeid(*type_symbol) == typeid(safelint_type_name_c))  {return true;}
       
   323   if (typeid(*type_symbol) == typeid(safeusint_type_name_c)) {return true;}
       
   324   if (typeid(*type_symbol) == typeid(safeuint_type_name_c))  {return true;}
       
   325   if (typeid(*type_symbol) == typeid(safeudint_type_name_c)) {return true;}
       
   326   if (typeid(*type_symbol) == typeid(safeulint_type_name_c)) {return true;}
       
   327   return false;
       
   328 }
       
   329 
       
   330 /* A helper function... */
       
   331 bool is_ANY_INT_compatible(symbol_c *type_symbol) {
       
   332   if (type_symbol == NULL) {return false;}
       
   333   if (is_ANY_INT_type    (type_symbol))              {return true;}
       
   334   if (is_ANY_SAFEINT_type(type_symbol))              {return true;}
       
   335   if (is_literal_integer_type(type_symbol))          {return true;}
       
   336   return false;
       
   337 }
       
   338 
       
   339 /* A helper function... */
       
   340 bool is_ANY_REAL_type(symbol_c *type_symbol) {
       
   341   if (type_symbol == NULL) {return false;}
       
   342   if (typeid(*type_symbol) == typeid(real_type_name_c))  {return true;}
       
   343   if (typeid(*type_symbol) == typeid(lreal_type_name_c)) {return true;}
       
   344   return false;
       
   345 }
       
   346 
       
   347 /* A helper function... */
       
   348 bool is_ANY_SAFEREAL_type(symbol_c *type_symbol) {
       
   349   if (type_symbol == NULL) {return false;}
       
   350   if (typeid(*type_symbol) == typeid(safereal_type_name_c))  {return true;}
       
   351   if (typeid(*type_symbol) == typeid(safelreal_type_name_c)) {return true;}
       
   352   return false;
       
   353 }
       
   354 
       
   355 /* A helper function... */
       
   356 bool is_ANY_REAL_compatible(symbol_c *type_symbol) {
       
   357   if (type_symbol == NULL) {return false;}
       
   358   if (is_ANY_REAL_type    (type_symbol))              {return true;}
       
   359   if (is_ANY_SAFEREAL_type(type_symbol))              {return true;}
       
   360   if (is_literal_real_type(type_symbol))              {return true;}
       
   361   return false;
       
   362 }
       
   363 
       
   364 /* A helper function... */
       
   365 bool is_ANY_BIT_type(symbol_c *type_symbol) {
       
   366   if (type_symbol == NULL) {return false;}
       
   367   if (typeid(*type_symbol) == typeid(bool_type_name_c))     {return true;}
       
   368   if (typeid(*type_symbol) == typeid(byte_type_name_c))     {return true;}
       
   369   if (typeid(*type_symbol) == typeid(word_type_name_c))     {return true;}
       
   370   if (typeid(*type_symbol) == typeid(dword_type_name_c))    {return true;}
       
   371   if (typeid(*type_symbol) == typeid(lword_type_name_c))    {return true;}
       
   372   return false;
       
   373 }
       
   374 
       
   375 /* A helper function... */
       
   376 bool is_ANY_SAFEBIT_type(symbol_c *type_symbol) {
       
   377   if (type_symbol == NULL) {return false;}
       
   378   if (typeid(*type_symbol) == typeid(safebool_type_name_c))     {return true;}
       
   379   if (typeid(*type_symbol) == typeid(safebyte_type_name_c))     {return true;}
       
   380   if (typeid(*type_symbol) == typeid(safeword_type_name_c))     {return true;}
       
   381   if (typeid(*type_symbol) == typeid(safedword_type_name_c))    {return true;}
       
   382   if (typeid(*type_symbol) == typeid(safelword_type_name_c))    {return true;}
       
   383   return false;
       
   384 }
       
   385 
       
   386 /* A helper function... */
       
   387 bool is_ANY_BIT_compatible(symbol_c *type_symbol) {
       
   388   if (type_symbol == NULL) {return false;}
       
   389   if (is_ANY_BIT_type    (type_symbol))              {return true;}
       
   390   if (is_ANY_SAFEBIT_type(type_symbol))              {return true;}
       
   391   if (is_nonneg_literal_integer_type(type_symbol))   {return true;}
       
   392   if (is_literal_bool_type(type_symbol))             {return true;}
       
   393   return false;
       
   394 }
       
   395 
       
   396 /* A helper function... */
       
   397 bool is_BOOL_type(symbol_c *type_symbol) {
       
   398   if (type_symbol == NULL) {return false;}
       
   399   if (typeid(*type_symbol) == typeid(bool_type_name_c))      {return true;}
       
   400   return false;
       
   401 }
       
   402 
       
   403 /* A helper function... */
       
   404 bool is_SAFEBOOL_type(symbol_c *type_symbol){
       
   405   if (type_symbol == NULL) {return false;}
       
   406   if (typeid(*type_symbol) == typeid(safebool_type_name_c))  {return true;}
       
   407   return false;
       
   408 }
       
   409 
       
   410 /* A helper function... */
       
   411 bool is_ANY_BOOL_compatible(symbol_c *type_symbol) {
       
   412   if (type_symbol == NULL) {return false;}
       
   413   if (is_BOOL_type    (type_symbol))              {return true;}
       
   414   if (is_SAFEBOOL_type(type_symbol))              {return true;}
       
   415   if (is_literal_bool_type(type_symbol))              {return true;}
       
   416   return false;
       
   417 }
       
   418 
       
   419 /* A helper function... */
       
   420 bool is_literal_integer_type(symbol_c *type_symbol) {
       
   421   if (type_symbol == NULL) {return false;}
       
   422   if (typeid(*type_symbol) == typeid(neg_integer_c))        {return true;}
       
   423   return is_nonneg_literal_integer_type(type_symbol);
       
   424 }
       
   425 
       
   426 
       
   427 /* A helper function... */
       
   428 bool is_nonneg_literal_integer_type(symbol_c *type_symbol) {
       
   429   if (type_symbol == NULL) {return false;}
       
   430   if (typeid(*type_symbol) == typeid(integer_c))        {return true;}
       
   431   if (typeid(*type_symbol) == typeid(binary_integer_c)) {return true;}
       
   432   if (typeid(*type_symbol) == typeid(octal_integer_c))  {return true;}
       
   433   if (typeid(*type_symbol) == typeid(hex_integer_c))    {return true;}
       
   434   return false;
       
   435 }
       
   436 
       
   437 
       
   438 /* A helper function... */
       
   439 bool is_literal_real_type(symbol_c *type_symbol) {
       
   440   if (type_symbol == NULL) {return false;}
       
   441   if (typeid(*type_symbol) == typeid(real_c))     {return true;}
       
   442   if (typeid(*type_symbol) == typeid(neg_real_c)) {return true;}
       
   443   return false;
       
   444 }
       
   445 
       
   446 
       
   447 /* A helper function... */
       
   448 bool is_literal_bool_type(symbol_c *type_symbol) {
       
   449   bool_type_name_c bool_t;
       
   450 
       
   451   if (type_symbol == NULL) {return false;}
       
   452   if (typeid(*type_symbol) == typeid(boolean_true_c))    {return true;}
       
   453   if (typeid(*type_symbol) == typeid(boolean_false_c))   {return true;}
       
   454   if (is_nonneg_literal_integer_type(type_symbol))
       
   455     if (sizeoftype(&bool_t) >= sizeoftype(type_symbol))  {return true;}
       
   456   return false;
       
   457 }
       
   458 
       
   459 /* Determine the common data type between two data types.
       
   460  * If no common data type found, return NULL.
       
   461  *
       
   462  * If data types are identical, return the first (actually any would do...).
       
   463  * If any of the data types is a literal, we confirm that
       
   464  *   the literal uses less bits than the fixed size data type.
       
   465  *   e.g. BYTE and 1024 returns NULL
       
   466  *        BYTE and 255  returns BYTE
       
   467  *
       
   468  * If two literals, then return the literal that requires more bits...
       
   469  */
       
   470 
       
   471 symbol_c *common_type(symbol_c *first_type, symbol_c *second_type) {
       
   472   if (first_type == NULL && second_type == NULL) {return NULL;}
       
   473   if (first_type == NULL)  {return second_type;}
       
   474   if (second_type == NULL) {return first_type;}
       
   475 
       
   476   if (is_literal_integer_type(first_type) && is_literal_integer_type(second_type))
       
   477     {return ((sizeoftype(first_type) > sizeoftype(second_type))? first_type:second_type);}
       
   478 
       
   479   if (is_literal_real_type(first_type) && is_literal_real_type(second_type))
       
   480     {return ((sizeoftype(first_type) > sizeoftype(second_type))? first_type:second_type);}
       
   481 
       
   482   if (is_literal_bool_type(first_type) && is_literal_bool_type(second_type))
       
   483     {return first_type;}
       
   484 
       
   485   /* The following check can only be made after the is_literal_XXXX checks */
       
   486   /* When two literals of the same type, with identical typeid's are checked,
       
   487    * we must return the one that occupies more bits... This is done above.
       
   488    */
       
   489   if (typeid(*first_type) == typeid(*second_type)) {return first_type;}
       
   490 
       
   491   /* NOTE Although a BOOL is also an ANY_BIT, we must check it explicitly since some
       
   492    *       literal bool values are not literal integers...
       
   493    */
       
   494   if (is_BOOL_type(first_type)        && is_literal_bool_type(second_type))    {return first_type;}
       
   495   if (is_BOOL_type(second_type)       && is_literal_bool_type(first_type))     {return second_type;}
       
   496 
       
   497   if (is_SAFEBOOL_type(first_type)    && is_literal_bool_type(second_type))    {return first_type;}
       
   498   if (is_SAFEBOOL_type(second_type)   && is_literal_bool_type(first_type))     {return second_type;}
       
   499 
       
   500   if (is_SAFEBOOL_type(first_type)    && is_BOOL_type(second_type))            {return second_type;}
       
   501   if (is_SAFEBOOL_type(second_type)   && is_BOOL_type(first_type))             {return first_type;}
       
   502 
       
   503   if (is_ANY_BIT_type(first_type)     && is_nonneg_literal_integer_type(second_type))
       
   504     {return ((sizeoftype(first_type)  >= sizeoftype(second_type))? first_type :NULL);}
       
   505   if (is_ANY_BIT_type(second_type)    && is_nonneg_literal_integer_type(first_type))
       
   506     {return ((sizeoftype(second_type) >= sizeoftype(first_type)) ? second_type:NULL);}
       
   507 
       
   508   if (is_ANY_SAFEBIT_type(first_type)     && is_nonneg_literal_integer_type(second_type))
       
   509     {return ((sizeoftype(first_type)  >= sizeoftype(second_type))? first_type :NULL);}
       
   510   if (is_ANY_SAFEBIT_type(second_type)    && is_nonneg_literal_integer_type(first_type))
       
   511     {return ((sizeoftype(second_type) >= sizeoftype(first_type)) ? second_type:NULL);}
       
   512 
       
   513   if  (is_ANY_SAFEBIT_type(first_type)    && is_ANY_BIT_type(second_type))
       
   514     {return ((sizeoftype(first_type) == sizeoftype(second_type))? second_type:NULL);}
       
   515   if  (is_ANY_SAFEBIT_type(second_type)   && is_ANY_BIT_type(first_type))
       
   516     {return ((sizeoftype(first_type) == sizeoftype(second_type))? first_type :NULL);}
       
   517 
       
   518   if (is_ANY_INT_type(first_type)     && is_literal_integer_type(second_type))
       
   519     {return ((sizeoftype(first_type)  >= sizeoftype(second_type))? first_type :NULL);}
       
   520   if (is_ANY_INT_type(second_type)    && is_literal_integer_type(first_type))
       
   521     {return ((sizeoftype(second_type) >= sizeoftype(first_type)) ? second_type:NULL);}
       
   522 
       
   523   if (is_ANY_SAFEINT_type(first_type)     && is_literal_integer_type(second_type))
       
   524     {return ((sizeoftype(first_type)  >= sizeoftype(second_type))? first_type :NULL);}
       
   525   if (is_ANY_SAFEINT_type(second_type)    && is_literal_integer_type(first_type))
       
   526     {return ((sizeoftype(second_type) >= sizeoftype(first_type)) ? second_type:NULL);}
       
   527 
       
   528   if  (is_ANY_SAFEINT_type(first_type)    && is_ANY_INT_type(second_type))
       
   529     {return ((sizeoftype(first_type) == sizeoftype(second_type))? second_type:NULL);}
       
   530   if  (is_ANY_SAFEINT_type(second_type)   && is_ANY_INT_type(first_type))
       
   531     {return ((sizeoftype(first_type) == sizeoftype(second_type))? first_type :NULL);}
       
   532 
       
   533   if (is_ANY_REAL_type(first_type)    && is_literal_real_type(second_type))
       
   534     {return ((sizeoftype(first_type)  >= sizeoftype(second_type))? first_type :NULL);}
       
   535   if (is_ANY_REAL_type(second_type)   && is_literal_real_type(first_type))
       
   536     {return ((sizeoftype(second_type) >= sizeoftype(first_type)) ? second_type:NULL);}
       
   537 
       
   538   if (is_ANY_SAFEREAL_type(first_type)    && is_literal_real_type(second_type))
       
   539     {return ((sizeoftype(first_type)  >= sizeoftype(second_type))? first_type :NULL);}
       
   540   if (is_ANY_SAFEREAL_type(second_type)   && is_literal_real_type(first_type))
       
   541     {return ((sizeoftype(second_type) >= sizeoftype(first_type)) ? second_type:NULL);}
       
   542 
       
   543   if  (is_ANY_SAFEREAL_type(first_type)    && is_ANY_REAL_type(second_type))
       
   544     {return ((sizeoftype(first_type) == sizeoftype(second_type))? second_type:NULL);}
       
   545   if  (is_ANY_SAFEREAL_type(second_type)   && is_ANY_REAL_type(first_type))
       
   546     {return ((sizeoftype(first_type) == sizeoftype(second_type))? first_type :NULL);}
       
   547 
       
   548   /* the Time and Date types... */
       
   549   if (is_type(first_type,  safetime_type_name_c) && is_type(second_type, time_type_name_c))  {return second_type;}
       
   550   if (is_type(second_type, safetime_type_name_c) && is_type( first_type, time_type_name_c))  {return  first_type;}
       
   551 
       
   552   if (is_type(first_type,  safedate_type_name_c) && is_type(second_type, date_type_name_c))  {return second_type;}
       
   553   if (is_type(second_type, safedate_type_name_c) && is_type( first_type, date_type_name_c))  {return  first_type;}
       
   554 
       
   555   if (is_type(first_type,  safedt_type_name_c)   && is_type(second_type, dt_type_name_c))    {return second_type;}
       
   556   if (is_type(second_type, safedt_type_name_c)   && is_type( first_type, dt_type_name_c))    {return  first_type;}
       
   557 
       
   558   if (is_type(first_type,  safetod_type_name_c)  && is_type(second_type, tod_type_name_c))   {return second_type;}
       
   559   if (is_type(second_type, safetod_type_name_c)  && is_type( first_type, tod_type_name_c))   {return  first_type;}
       
   560 
       
   561   /* no common type */
       
   562   return NULL;
       
   563 }
       
   564 
       
   565 /* Return TRUE if the second (value) data type may be assigned to a variable of the first (variable) data type
       
   566  * such as:
       
   567  *     var_type     value_type
       
   568  *    BOOL           BYTE#7     -> returns false
       
   569  *    INT            INT#7      -> returns true
       
   570  *    INT            7          -> returns true
       
   571  *    REAL           7.89       -> returns true
       
   572  *    REAL           7          -> returns true
       
   573  *    INT            7.89       -> returns false
       
   574  *    SAFEBOOL       BOOL#1     -> returns false   !!!
       
   575  *   etc...
       
   576  *
       
   577  * NOTE: It is assumed that the var_type is the data type of an lvalue
       
   578  */
       
   579 bool is_valid_assignment(symbol_c *var_type, symbol_c *value_type) {
       
   580   if (var_type == NULL)   {/* STAGE3_ERROR(value_type, value_type, "Var_type   == NULL"); */ return false;}
       
   581   if (value_type == NULL) {/* STAGE3_ERROR(var_type,   var_type,   "Value_type == NULL"); */ return false;}
       
   582 
       
   583   symbol_c *common_type_symbol = common_type(var_type, value_type);
       
   584   if (NULL == common_type_symbol)
       
   585     return false;
       
   586   return (typeid(*var_type) == typeid(*common_type_symbol));
       
   587 }
       
   588 
       
   589 
       
   590 /* Return TRUE if there is a common data type, otherwise return FALSE
       
   591  * i.e., return TRUE if both data types may be used simultaneously in an expression
       
   592  * such as:
       
   593  *    BOOL#0     AND BYTE#7  -> returns false
       
   594  *    0          AND BYTE#7  -> returns true
       
   595  *    INT#10     AND INT#7   -> returns true
       
   596  *    INT#10     AND 7       -> returns true
       
   597  *    REAL#34.3  AND 7.89    -> returns true
       
   598  *    REAL#34.3  AND 7       -> returns true
       
   599  *    INT#10     AND 7.89    -> returns false
       
   600  *    SAFEBOOL#0 AND BOOL#1  -> returns true   !!!
       
   601  *   etc...
       
   602  */
       
   603 bool is_compatible_type(symbol_c *first_type, symbol_c *second_type) {
       
   604   if (first_type == NULL || second_type == NULL) {return false;}
       
   605   return (NULL != common_type(first_type, second_type));
       
   606 }
       
   607 
       
   608 bool is_type_equal(symbol_c *first_type, symbol_c *second_type)
       
   609 {
       
   610     if (first_type == NULL || second_type == NULL) {
       
   611         return false;
       
   612     }
       
   613     if (is_ANY_ELEMENTARY_type(first_type)) {
       
   614         if (typeid(*first_type) == typeid(*second_type))
       
   615             return true;
       
   616     } else   /* ANY_DERIVED */
       
   617         return (first_type == second_type);
       
   618 
       
   619     return false;
       
   620 }