stage3/datatype_functions.cc
changeset 436 f9fd63f64f5c
parent 435 82cb6a64a763
child 443 ff4d26b7e51d
equal deleted inserted replaced
435:82cb6a64a763 436:f9fd63f64f5c
   331 /* A helper function... */
   331 /* A helper function... */
   332 bool is_ANY_STRING_type(symbol_c *type_symbol) {
   332 bool is_ANY_STRING_type(symbol_c *type_symbol) {
   333   if (type_symbol == NULL) {return false;}
   333   if (type_symbol == NULL) {return false;}
   334   if (typeid(*type_symbol) == typeid(string_type_name_c)) {return true;}
   334   if (typeid(*type_symbol) == typeid(string_type_name_c)) {return true;}
   335   if (typeid(*type_symbol) == typeid(wstring_type_name_c)) {return true;}
   335   if (typeid(*type_symbol) == typeid(wstring_type_name_c)) {return true;}
   336 // TODO literal_string ???
       
   337   return false;
   336   return false;
   338 }
   337 }
   339 
   338 
   340 /* A helper function... */
   339 /* A helper function... */
   341 bool is_ANY_SAFESTRING_type(symbol_c *type_symbol) {
   340 bool is_ANY_SAFESTRING_type(symbol_c *type_symbol) {
   404 /* A helper function... */
   403 /* A helper function... */
   405 bool is_ANY_signed_INT_compatible(symbol_c *type_symbol) {
   404 bool is_ANY_signed_INT_compatible(symbol_c *type_symbol) {
   406   if (type_symbol == NULL) {return false;}
   405   if (type_symbol == NULL) {return false;}
   407   if (is_ANY_signed_INT_type    (type_symbol))              {return true;}
   406   if (is_ANY_signed_INT_type    (type_symbol))              {return true;}
   408   if (is_ANY_signed_SAFEINT_type(type_symbol))              {return true;}
   407   if (is_ANY_signed_SAFEINT_type(type_symbol))              {return true;}
   409 //   if (is_literal_integer_type(type_symbol))          {return true;}
       
   410   return false;
   408   return false;
   411 }
   409 }
   412 
   410 
   413 /* A helper function... */
   411 /* A helper function... */
   414 bool is_ANY_INT_compatible(symbol_c *type_symbol) {
   412 bool is_ANY_INT_compatible(symbol_c *type_symbol) {
   415   if (type_symbol == NULL) {return false;}
   413   if (type_symbol == NULL) {return false;}
   416   if (is_ANY_INT_type    (type_symbol))              {return true;}
   414   if (is_ANY_INT_type    (type_symbol))              {return true;}
   417   if (is_ANY_SAFEINT_type(type_symbol))              {return true;}
   415   if (is_ANY_SAFEINT_type(type_symbol))              {return true;}
   418 //   if (is_literal_integer_type(type_symbol))          {return true;}
       
   419   return false;
   416   return false;
   420 }
   417 }
   421 
   418 
   422 /* A helper function... */
   419 /* A helper function... */
   423 bool is_ANY_REAL_type(symbol_c *type_symbol) {
   420 bool is_ANY_REAL_type(symbol_c *type_symbol) {
   438 /* A helper function... */
   435 /* A helper function... */
   439 bool is_ANY_REAL_compatible(symbol_c *type_symbol) {
   436 bool is_ANY_REAL_compatible(symbol_c *type_symbol) {
   440   if (type_symbol == NULL) {return false;}
   437   if (type_symbol == NULL) {return false;}
   441   if (is_ANY_REAL_type    (type_symbol))              {return true;}
   438   if (is_ANY_REAL_type    (type_symbol))              {return true;}
   442   if (is_ANY_SAFEREAL_type(type_symbol))              {return true;}
   439   if (is_ANY_SAFEREAL_type(type_symbol))              {return true;}
   443 //   if (is_literal_real_type(type_symbol))              {return true;}
       
   444   return false;
   440   return false;
   445 }
   441 }
   446 
   442 
   447 /* A helper function... */
   443 /* A helper function... */
   448 bool is_ANY_BIT_type(symbol_c *type_symbol) {
   444 bool is_ANY_BIT_type(symbol_c *type_symbol) {
   469 /* A helper function... */
   465 /* A helper function... */
   470 bool is_ANY_BIT_compatible(symbol_c *type_symbol) {
   466 bool is_ANY_BIT_compatible(symbol_c *type_symbol) {
   471   if (type_symbol == NULL) {return false;}
   467   if (type_symbol == NULL) {return false;}
   472   if (is_ANY_BIT_type    (type_symbol))              {return true;}
   468   if (is_ANY_BIT_type    (type_symbol))              {return true;}
   473   if (is_ANY_SAFEBIT_type(type_symbol))              {return true;}
   469   if (is_ANY_SAFEBIT_type(type_symbol))              {return true;}
   474 //   if (is_nonneg_literal_integer_type(type_symbol))   {return true;}
       
   475 //   if (is_literal_bool_type(type_symbol))             {return true;}
       
   476   return false;
   470   return false;
   477 }
   471 }
   478 
   472 
   479 /* A helper function... */
   473 /* A helper function... */
   480 bool is_BOOL_type(symbol_c *type_symbol) {
   474 bool is_BOOL_type(symbol_c *type_symbol) {
   493 /* A helper function... */
   487 /* A helper function... */
   494 bool is_ANY_BOOL_compatible(symbol_c *type_symbol) {
   488 bool is_ANY_BOOL_compatible(symbol_c *type_symbol) {
   495   if (type_symbol == NULL) {return false;}
   489   if (type_symbol == NULL) {return false;}
   496   if (is_BOOL_type    (type_symbol))              {return true;}
   490   if (is_BOOL_type    (type_symbol))              {return true;}
   497   if (is_SAFEBOOL_type(type_symbol))              {return true;}
   491   if (is_SAFEBOOL_type(type_symbol))              {return true;}
   498 //   if (is_literal_bool_type(type_symbol))              {return true;}
   492   return false;
   499   return false;
   493 }
   500 }
   494 
   501 
   495 
   502 
   496 
   503 
   497 
   504 #if 0
       
   505 /* A helper function... */
       
   506 bool is_literal_integer_type(symbol_c *type_symbol) {
       
   507   if (type_symbol == NULL) {return false;}
       
   508   if (typeid(*type_symbol) == typeid(neg_integer_c))        {return true;}
       
   509   return is_nonneg_literal_integer_type(type_symbol);
       
   510 }
       
   511 
       
   512 
       
   513 /* A helper function... */
       
   514 bool is_nonneg_literal_integer_type(symbol_c *type_symbol) {
       
   515   if (type_symbol == NULL) {return false;}
       
   516   if (typeid(*type_symbol) == typeid(integer_c))        {return true;}
       
   517   if (typeid(*type_symbol) == typeid(binary_integer_c)) {return true;}
       
   518   if (typeid(*type_symbol) == typeid(octal_integer_c))  {return true;}
       
   519   if (typeid(*type_symbol) == typeid(hex_integer_c))    {return true;}
       
   520   return false;
       
   521 }
       
   522 
       
   523 
       
   524 /* A helper function... */
       
   525 bool is_literal_real_type(symbol_c *type_symbol) {
       
   526   if (type_symbol == NULL) {return false;}
       
   527   if (typeid(*type_symbol) == typeid(real_c))     {return true;}
       
   528   if (typeid(*type_symbol) == typeid(neg_real_c)) {return true;}
       
   529   return false;
       
   530 }
       
   531 
       
   532 
       
   533 /* A helper function... */
       
   534 bool is_literal_bool_type(symbol_c *type_symbol) {
       
   535   bool_type_name_c bool_t;
       
   536 
       
   537   if (type_symbol == NULL) {return false;}
       
   538   if (typeid(*type_symbol) == typeid(boolean_true_c))    {return true;}
       
   539   if (typeid(*type_symbol) == typeid(boolean_false_c))   {return true;}
       
   540   if (is_nonneg_literal_integer_type(type_symbol))
       
   541     if (sizeoftype(&bool_t) >= sizeoftype(type_symbol))  {return true;}
       
   542   return false;
       
   543 }
       
   544 
       
   545 /* Determine the common data type between two data types.
       
   546  * If no common data type found, return NULL.
       
   547  *
       
   548  * If data types are identical, return the first (actually any would do...).
       
   549  * If any of the data types is a literal, we confirm that
       
   550  *   the literal uses less bits than the fixed size data type.
       
   551  *   e.g. BYTE and 1024 returns NULL
       
   552  *        BYTE and 255  returns BYTE
       
   553  *
       
   554  * If two literals, then return the literal that requires more bits...
       
   555  */
       
   556 
       
   557 symbol_c *common_type(symbol_c *first_type, symbol_c *second_type) {
       
   558   if (first_type == NULL && second_type == NULL) {return NULL;}
       
   559   if (first_type == NULL)  {return second_type;}
       
   560   if (second_type == NULL) {return first_type;}
       
   561 
       
   562   if (is_literal_integer_type(first_type) && is_literal_integer_type(second_type))
       
   563     {return ((sizeoftype(first_type) > sizeoftype(second_type))? first_type:second_type);}
       
   564 
       
   565   if (is_literal_real_type(first_type) && is_literal_real_type(second_type))
       
   566     {return ((sizeoftype(first_type) > sizeoftype(second_type))? first_type:second_type);}
       
   567 
       
   568   if (is_literal_bool_type(first_type) && is_literal_bool_type(second_type))
       
   569     {return first_type;}
       
   570 
       
   571   /* The following check can only be made after the is_literal_XXXX checks */
       
   572   /* When two literals of the same type, with identical typeid's are checked,
       
   573    * we must return the one that occupies more bits... This is done above.
       
   574    */
       
   575   if (typeid(*first_type) == typeid(*second_type)) {return first_type;}
       
   576 
       
   577   /* NOTE Although a BOOL is also an ANY_BIT, we must check it explicitly since some
       
   578    *       literal bool values are not literal integers...
       
   579    */
       
   580   if (is_BOOL_type(first_type)        && is_literal_bool_type(second_type))    {return first_type;}
       
   581   if (is_BOOL_type(second_type)       && is_literal_bool_type(first_type))     {return second_type;}
       
   582 
       
   583   if (is_SAFEBOOL_type(first_type)    && is_literal_bool_type(second_type))    {return first_type;}
       
   584   if (is_SAFEBOOL_type(second_type)   && is_literal_bool_type(first_type))     {return second_type;}
       
   585 
       
   586   if (is_SAFEBOOL_type(first_type)    && is_BOOL_type(second_type))            {return second_type;}
       
   587   if (is_SAFEBOOL_type(second_type)   && is_BOOL_type(first_type))             {return first_type;}
       
   588 
       
   589   if (is_ANY_BIT_type(first_type)     && is_nonneg_literal_integer_type(second_type))
       
   590     {return ((sizeoftype(first_type)  >= sizeoftype(second_type))? first_type :NULL);}
       
   591   if (is_ANY_BIT_type(second_type)    && is_nonneg_literal_integer_type(first_type))
       
   592     {return ((sizeoftype(second_type) >= sizeoftype(first_type)) ? second_type:NULL);}
       
   593 
       
   594   if (is_ANY_SAFEBIT_type(first_type)     && is_nonneg_literal_integer_type(second_type))
       
   595     {return ((sizeoftype(first_type)  >= sizeoftype(second_type))? first_type :NULL);}
       
   596   if (is_ANY_SAFEBIT_type(second_type)    && is_nonneg_literal_integer_type(first_type))
       
   597     {return ((sizeoftype(second_type) >= sizeoftype(first_type)) ? second_type:NULL);}
       
   598 
       
   599   if  (is_ANY_SAFEBIT_type(first_type)    && is_ANY_BIT_type(second_type))
       
   600     {return ((sizeoftype(first_type) == sizeoftype(second_type))? second_type:NULL);}
       
   601   if  (is_ANY_SAFEBIT_type(second_type)   && is_ANY_BIT_type(first_type))
       
   602     {return ((sizeoftype(first_type) == sizeoftype(second_type))? first_type :NULL);}
       
   603 
       
   604   if (is_ANY_INT_type(first_type)     && is_literal_integer_type(second_type))
       
   605     {return ((sizeoftype(first_type)  >= sizeoftype(second_type))? first_type :NULL);}
       
   606   if (is_ANY_INT_type(second_type)    && is_literal_integer_type(first_type))
       
   607     {return ((sizeoftype(second_type) >= sizeoftype(first_type)) ? second_type:NULL);}
       
   608 
       
   609   if (is_ANY_SAFEINT_type(first_type)     && is_literal_integer_type(second_type))
       
   610     {return ((sizeoftype(first_type)  >= sizeoftype(second_type))? first_type :NULL);}
       
   611   if (is_ANY_SAFEINT_type(second_type)    && is_literal_integer_type(first_type))
       
   612     {return ((sizeoftype(second_type) >= sizeoftype(first_type)) ? second_type:NULL);}
       
   613 
       
   614   if  (is_ANY_SAFEINT_type(first_type)    && is_ANY_INT_type(second_type))
       
   615     {return ((sizeoftype(first_type) == sizeoftype(second_type))? second_type:NULL);}
       
   616   if  (is_ANY_SAFEINT_type(second_type)   && is_ANY_INT_type(first_type))
       
   617     {return ((sizeoftype(first_type) == sizeoftype(second_type))? first_type :NULL);}
       
   618 
       
   619   if (is_ANY_REAL_type(first_type)    && is_literal_real_type(second_type))
       
   620     {return ((sizeoftype(first_type)  >= sizeoftype(second_type))? first_type :NULL);}
       
   621   if (is_ANY_REAL_type(second_type)   && is_literal_real_type(first_type))
       
   622     {return ((sizeoftype(second_type) >= sizeoftype(first_type)) ? second_type:NULL);}
       
   623 
       
   624   if (is_ANY_SAFEREAL_type(first_type)    && is_literal_real_type(second_type))
       
   625     {return ((sizeoftype(first_type)  >= sizeoftype(second_type))? first_type :NULL);}
       
   626   if (is_ANY_SAFEREAL_type(second_type)   && is_literal_real_type(first_type))
       
   627     {return ((sizeoftype(second_type) >= sizeoftype(first_type)) ? second_type:NULL);}
       
   628 
       
   629   if  (is_ANY_SAFEREAL_type(first_type)    && is_ANY_REAL_type(second_type))
       
   630     {return ((sizeoftype(first_type) == sizeoftype(second_type))? second_type:NULL);}
       
   631   if  (is_ANY_SAFEREAL_type(second_type)   && is_ANY_REAL_type(first_type))
       
   632     {return ((sizeoftype(first_type) == sizeoftype(second_type))? first_type :NULL);}
       
   633 
       
   634   /* the Time and Date types... */
       
   635   if (is_type(first_type,  safetime_type_name_c) && is_type(second_type, time_type_name_c))  {return second_type;}
       
   636   if (is_type(second_type, safetime_type_name_c) && is_type( first_type, time_type_name_c))  {return  first_type;}
       
   637 
       
   638   if (is_type(first_type,  safedate_type_name_c) && is_type(second_type, date_type_name_c))  {return second_type;}
       
   639   if (is_type(second_type, safedate_type_name_c) && is_type( first_type, date_type_name_c))  {return  first_type;}
       
   640 
       
   641   if (is_type(first_type,  safedt_type_name_c)   && is_type(second_type, dt_type_name_c))    {return second_type;}
       
   642   if (is_type(second_type, safedt_type_name_c)   && is_type( first_type, dt_type_name_c))    {return  first_type;}
       
   643 
       
   644   if (is_type(first_type,  safetod_type_name_c)  && is_type(second_type, tod_type_name_c))   {return second_type;}
       
   645   if (is_type(second_type, safetod_type_name_c)  && is_type( first_type, tod_type_name_c))   {return  first_type;}
       
   646 
       
   647   /* no common type */
       
   648   return NULL;
       
   649 }
       
   650 
       
   651 /* Return TRUE if the second (value) data type may be assigned to a variable of the first (variable) data type
       
   652  * such as:
       
   653  *     var_type     value_type
       
   654  *    BOOL           BYTE#7     -> returns false
       
   655  *    INT            INT#7      -> returns true
       
   656  *    INT            7          -> returns true
       
   657  *    REAL           7.89       -> returns true
       
   658  *    REAL           7          -> returns true
       
   659  *    INT            7.89       -> returns false
       
   660  *    SAFEBOOL       BOOL#1     -> returns false   !!!
       
   661  *   etc...
       
   662  *
       
   663  * NOTE: It is assumed that the var_type is the data type of an lvalue
       
   664  */
       
   665 bool is_valid_assignment(symbol_c *var_type, symbol_c *value_type) {
       
   666   if (var_type == NULL)   {/* STAGE3_ERROR(value_type, value_type, "Var_type   == NULL"); */ return false;}
       
   667   if (value_type == NULL) {/* STAGE3_ERROR(var_type,   var_type,   "Value_type == NULL"); */ return false;}
       
   668 
       
   669   symbol_c *common_type_symbol = common_type(var_type, value_type);
       
   670   if (NULL == common_type_symbol)
       
   671     return false;
       
   672   return (typeid(*var_type) == typeid(*common_type_symbol));
       
   673 }
       
   674 
       
   675 
       
   676 /* Return TRUE if there is a common data type, otherwise return FALSE
       
   677  * i.e., return TRUE if both data types may be used simultaneously in an expression
       
   678  * such as:
       
   679  *    BOOL#0     AND BYTE#7  -> returns false
       
   680  *    0          AND BYTE#7  -> returns true
       
   681  *    INT#10     AND INT#7   -> returns true
       
   682  *    INT#10     AND 7       -> returns true
       
   683  *    REAL#34.3  AND 7.89    -> returns true
       
   684  *    REAL#34.3  AND 7       -> returns true
       
   685  *    INT#10     AND 7.89    -> returns false
       
   686  *    SAFEBOOL#0 AND BOOL#1  -> returns true   !!!
       
   687  *   etc...
       
   688  */
       
   689 bool is_compatible_type(symbol_c *first_type, symbol_c *second_type) {
       
   690   if (first_type == NULL || second_type == NULL) {return false;}
       
   691   return (NULL != common_type(first_type, second_type));
       
   692 }
       
   693 #endif
       
   694 
   498 
   695 bool is_type_equal(symbol_c *first_type, symbol_c *second_type)
   499 bool is_type_equal(symbol_c *first_type, symbol_c *second_type)
   696 {
   500 {
   697     if (first_type == NULL || second_type == NULL) {
   501     if (first_type == NULL || second_type == NULL) {
   698         return false;
   502         return false;