absyntax_utils/search_expression_type.cc
changeset 270 f1674941c3ad
parent 265 4d222f46f8cc
child 279 c0453b7f99df
equal deleted inserted replaced
256:101c7f148b9a 270:f1674941c3ad
     1 /*
     1 /*
     2  * (c) 2003 Mario de Sousa
     2  *  matiec - a compiler for the programming languages defined in IEC 61131-3
     3  *
     3  *
     4  * Offered to the public under the terms of the GNU General Public License
     4  *  Copyright (C) 2003-2011  Mario de Sousa (msousa@fe.up.pt)
     5  * as published by the Free Software Foundation; either version 2 of the
     5  *
     6  * License, or (at your option) any later version.
     6  *  This program is free software: you can redistribute it and/or modify
     7  *
     7  *  it under the terms of the GNU General Public License as published by
     8  * This program is distributed in the hope that it will be useful, but
     8  *  the Free Software Foundation, either version 3 of the License, or
     9  * WITHOUT ANY WARRANTY; without even the implied warranty of
     9  *  (at your option) any later version.
    10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
    10  *
    11  * Public License for more details.
    11  *  This program is distributed in the hope that it will be useful,
       
    12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    14  *  GNU General Public License for more details.
       
    15  *
       
    16  *  You should have received a copy of the GNU General Public License
       
    17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
       
    18  *
    12  *
    19  *
    13  * This code is made available on the understanding that it will not be
    20  * This code is made available on the understanding that it will not be
    14  * used in safety-critical situations without a full and competent review.
    21  * used in safety-critical situations without a full and competent review.
    15  */
    22  */
    16 
    23 
    17 /*
    24 /*
    18  * An IEC 61131-3 IL and ST compiler.
    25  * An IEC 61131-3 compiler.
    19  *
    26  *
    20  * Based on the
    27  * Based on the
    21  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
    28  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
    22  *
    29  *
    23  */
    30  */
    47 
    54 
    48 /* A helper function... */
    55 /* A helper function... */
    49 bool search_expression_type_c::is_bool_type(symbol_c *type_symbol) {
    56 bool search_expression_type_c::is_bool_type(symbol_c *type_symbol) {
    50   bool_type_name_c tt;
    57   bool_type_name_c tt;
    51   if (type_symbol == NULL) {return true;}
    58   if (type_symbol == NULL) {return true;}
    52   return (typeid(*type_symbol) == typeid(bool_type_name_c));
    59   if (typeid(*type_symbol) == typeid(safebool_type_name_c)) {return true;}
       
    60   if (typeid(*type_symbol) == typeid(bool_type_name_c))     {return true;}
       
    61   if (typeid(*type_symbol) == typeid(boolean_true_c))       {return true;}
       
    62   if (typeid(*type_symbol) == typeid(boolean_false_c))      {return true;}
       
    63   return false;
    53 }
    64 }
    54 
    65 
    55 /* A helper function... */
    66 /* A helper function... */
    56 bool search_expression_type_c::is_time_type(symbol_c *type_symbol) {
    67 bool search_expression_type_c::is_time_type(symbol_c *type_symbol) {
    57   if (type_symbol == NULL) {return true;}
    68   if (type_symbol == NULL) {return true;}
    58   if (typeid(*type_symbol) == typeid(time_type_name_c)) {return true;}
    69   if (typeid(*type_symbol) == typeid(time_type_name_c))     {return true;}
    59   if (typeid(*type_symbol) == typeid(date_type_name_c)) {return true;}
    70   if (typeid(*type_symbol) == typeid(date_type_name_c))     {return true;}
    60   if (typeid(*type_symbol) == typeid(tod_type_name_c)) {return true;}
    71   if (typeid(*type_symbol) == typeid(tod_type_name_c))      {return true;}
    61   if (typeid(*type_symbol) == typeid(dt_type_name_c)) {return true;}
    72   if (typeid(*type_symbol) == typeid(dt_type_name_c))       {return true;}
       
    73   if (typeid(*type_symbol) == typeid(safetime_type_name_c)) {return true;}
       
    74   if (typeid(*type_symbol) == typeid(safedate_type_name_c)) {return true;}
       
    75   if (typeid(*type_symbol) == typeid(safetod_type_name_c))  {return true;}
       
    76   if (typeid(*type_symbol) == typeid(safedt_type_name_c))   {return true;}
    62   return false;
    77   return false;
    63 }
    78 }
    64 
    79 
    65 /* A helper function... */
    80 /* A helper function... */
    66 bool search_expression_type_c::is_string_type(symbol_c *type_symbol) {
    81 bool search_expression_type_c::is_string_type(symbol_c *type_symbol) {
    67   if (type_symbol == NULL) {return true;}
    82   if (type_symbol == NULL) {return true;}
    68   if (typeid(*type_symbol) == typeid(string_type_name_c))  {return true;}
    83   if (typeid(*type_symbol) == typeid(string_type_name_c))      {return true;}
    69   if (typeid(*type_symbol) == typeid(wstring_type_name_c)) {return true;}
    84   if (typeid(*type_symbol) == typeid(wstring_type_name_c))     {return true;}
       
    85   if (typeid(*type_symbol) == typeid(safestring_type_name_c))  {return true;}
       
    86   if (typeid(*type_symbol) == typeid(safewstring_type_name_c)) {return true;}
    70   return false;
    87   return false;
    71 }
    88 }
    72 
    89 
    73 /* A helper function... */
    90 /* A helper function... */
    74 bool search_expression_type_c::is_literal_integer_type(symbol_c *type_symbol) {
    91 bool search_expression_type_c::is_literal_integer_type(symbol_c *type_symbol) {
    75   if (type_symbol == NULL) {return true;}
    92   if (type_symbol == NULL) {return true;}
    76   if (typeid(*type_symbol) == typeid(integer_c))        {return true;}
    93   if (typeid(*type_symbol) == typeid(integer_c))        {return true;}
       
    94   if (typeid(*type_symbol) == typeid(neg_integer_c))    {return true;}
    77   if (typeid(*type_symbol) == typeid(binary_integer_c)) {return true;}
    95   if (typeid(*type_symbol) == typeid(binary_integer_c)) {return true;}
    78   if (typeid(*type_symbol) == typeid(octal_integer_c))  {return true;}
    96   if (typeid(*type_symbol) == typeid(octal_integer_c))  {return true;}
    79   if (typeid(*type_symbol) == typeid(hex_integer_c))    {return true;}
    97   if (typeid(*type_symbol) == typeid(hex_integer_c))    {return true;}
    80   return false;
    98   return false;
    81 }
    99 }
    82 
   100 
    83 /* A helper function... */
   101 /* A helper function... */
    84 bool search_expression_type_c::is_integer_type(symbol_c *type_symbol) {
   102 bool search_expression_type_c::is_integer_type(symbol_c *type_symbol) {
    85   if (type_symbol == NULL) {return true;}
   103   if (type_symbol == NULL) {return true;}
    86   if (typeid(*type_symbol) == typeid(sint_type_name_c))  {return true;}
   104   if (typeid(*type_symbol) == typeid(sint_type_name_c))      {return true;}
    87   if (typeid(*type_symbol) == typeid(int_type_name_c))   {return true;}
   105   if (typeid(*type_symbol) == typeid(int_type_name_c))       {return true;}
    88   if (typeid(*type_symbol) == typeid(dint_type_name_c))  {return true;}
   106   if (typeid(*type_symbol) == typeid(dint_type_name_c))      {return true;}
    89   if (typeid(*type_symbol) == typeid(lint_type_name_c))  {return true;}
   107   if (typeid(*type_symbol) == typeid(lint_type_name_c))      {return true;}
    90   if (typeid(*type_symbol) == typeid(usint_type_name_c)) {return true;}
   108   if (typeid(*type_symbol) == typeid(usint_type_name_c))     {return true;}
    91   if (typeid(*type_symbol) == typeid(uint_type_name_c))  {return true;}
   109   if (typeid(*type_symbol) == typeid(uint_type_name_c))      {return true;}
    92   if (typeid(*type_symbol) == typeid(udint_type_name_c)) {return true;}
   110   if (typeid(*type_symbol) == typeid(udint_type_name_c))     {return true;}
    93   if (typeid(*type_symbol) == typeid(ulint_type_name_c)) {return true;}
   111   if (typeid(*type_symbol) == typeid(ulint_type_name_c))     {return true;}
       
   112   if (typeid(*type_symbol) == typeid(safesint_type_name_c))  {return true;}
       
   113   if (typeid(*type_symbol) == typeid(safeint_type_name_c))   {return true;}
       
   114   if (typeid(*type_symbol) == typeid(safedint_type_name_c))  {return true;}
       
   115   if (typeid(*type_symbol) == typeid(safelint_type_name_c))  {return true;}
       
   116   if (typeid(*type_symbol) == typeid(safeusint_type_name_c)) {return true;}
       
   117   if (typeid(*type_symbol) == typeid(safeuint_type_name_c))  {return true;}
       
   118   if (typeid(*type_symbol) == typeid(safeudint_type_name_c)) {return true;}
       
   119   if (typeid(*type_symbol) == typeid(safeulint_type_name_c)) {return true;}
    94   return is_literal_integer_type(type_symbol);
   120   return is_literal_integer_type(type_symbol);
    95 }
   121 }
    96 
   122 
    97 /* A helper function... */
   123 /* A helper function... */
    98 bool search_expression_type_c::is_literal_real_type(symbol_c *type_symbol) {
   124 bool search_expression_type_c::is_literal_real_type(symbol_c *type_symbol) {
    99   if (type_symbol == NULL) {return true;}
   125   if (type_symbol == NULL) {return true;}
   100   if (typeid(*type_symbol) == typeid(real_c)) {return true;}
   126   if (typeid(*type_symbol) == typeid(real_c))     {return true;}
       
   127   if (typeid(*type_symbol) == typeid(neg_real_c)) {return true;}
   101   return false;
   128   return false;
   102 }
   129 }
   103 
   130 
   104 /* A helper function... */
   131 /* A helper function... */
   105 bool search_expression_type_c::is_real_type(symbol_c *type_symbol) {
   132 bool search_expression_type_c::is_real_type(symbol_c *type_symbol) {
   106   if (type_symbol == NULL) {return true;}
   133   if (type_symbol == NULL) {return true;}
   107   if (typeid(*type_symbol) == typeid(real_type_name_c)) {return true;}
   134   if (typeid(*type_symbol) == typeid(real_type_name_c))      {return true;}
   108   if (typeid(*type_symbol) == typeid(lreal_type_name_c)) {return true;}
   135   if (typeid(*type_symbol) == typeid(lreal_type_name_c))     {return true;}
       
   136   if (typeid(*type_symbol) == typeid(safereal_type_name_c))  {return true;} 
       
   137   if (typeid(*type_symbol) == typeid(safelreal_type_name_c)) {return true;}  
   109   return is_literal_real_type(type_symbol);
   138   return is_literal_real_type(type_symbol);
   110 }
   139 }
   111 
   140 
   112 bool search_expression_type_c::is_num_type(symbol_c *type_symbol) {
   141 bool search_expression_type_c::is_num_type(symbol_c *type_symbol) {
   113   if (type_symbol == NULL) {return true;}
   142   if (type_symbol == NULL) {return true;}
   114   return is_real_type(type_symbol) || is_integer_type(type_symbol);
   143   return is_real_type(type_symbol) || is_integer_type(type_symbol);
   115 }
   144 }
   116 
   145 
   117 bool search_expression_type_c::is_nbinary_type(symbol_c *type_symbol) {
   146 bool search_expression_type_c::is_nbinary_type(symbol_c *type_symbol) {
   118   if (type_symbol == NULL) {return true;}
   147   if (type_symbol == NULL) {return true;}
   119   if (typeid(*type_symbol) == typeid(byte_type_name_c)) {return true;}
   148   if (typeid(*type_symbol) == typeid(byte_type_name_c))      {return true;}
   120   if (typeid(*type_symbol) == typeid(word_type_name_c)) {return true;}
   149   if (typeid(*type_symbol) == typeid(word_type_name_c))      {return true;}
   121   if (typeid(*type_symbol) == typeid(dword_type_name_c)) {return true;}
   150   if (typeid(*type_symbol) == typeid(dword_type_name_c))     {return true;}
   122   if (typeid(*type_symbol) == typeid(lword_type_name_c)) {return true;}
   151   if (typeid(*type_symbol) == typeid(lword_type_name_c))     {return true;}
       
   152   if (typeid(*type_symbol) == typeid(safebyte_type_name_c))  {return true;}
       
   153   if (typeid(*type_symbol) == typeid(safeword_type_name_c))  {return true;}
       
   154   if (typeid(*type_symbol) == typeid(safedword_type_name_c)) {return true;}
       
   155   if (typeid(*type_symbol) == typeid(safelword_type_name_c)) {return true;}
   123   return is_literal_integer_type(type_symbol);
   156   return is_literal_integer_type(type_symbol);
   124 }
   157 }
   125 
   158 
   126 bool search_expression_type_c::is_binary_type(symbol_c *type_symbol) {
   159 bool search_expression_type_c::is_binary_type(symbol_c *type_symbol) {
   127   if (type_symbol == NULL) {return true;}
   160   if (type_symbol == NULL) {return true;}
   128   if (typeid(*type_symbol) == typeid(bool_type_name_c)) {return true;}
   161 //   if (typeid(*type_symbol) == typeid(bool_type_name_c))     {return true;}
   129   return is_nbinary_type(type_symbol);
   162 //   if (typeid(*type_symbol) == typeid(safebool_type_name_c)) {return true;}
       
   163   return (is_nbinary_type(type_symbol) || is_bool_type(type_symbol));
   130 }
   164 }
   131 
   165 
   132 bool search_expression_type_c::is_same_type(symbol_c *first_type, symbol_c *second_type) {
   166 bool search_expression_type_c::is_same_type(symbol_c *first_type, symbol_c *second_type) {
   133   if (first_type == NULL || second_type == NULL) {return true;}
   167   if (first_type == NULL || second_type == NULL) {return true;}
   134   if (typeid(*first_type) == typeid(*second_type)) {return true;}
   168   if (typeid(*first_type) == typeid(*second_type)) {return true;}
   135   if (is_integer_type(first_type) && is_literal_integer_type(second_type)) {return true;}
   169   if (is_bool_type(first_type)            && is_bool_type(second_type))            {return true;}
   136   if (is_literal_integer_type(first_type) && is_integer_type(second_type)) {return true;}
   170   if (is_integer_type(first_type)         && is_literal_integer_type(second_type)) {return true;}
   137   if (is_binary_type(first_type) && is_literal_integer_type(second_type))  {return true;}
   171   if (is_literal_integer_type(first_type) && is_integer_type(second_type))         {return true;}
   138   if (is_literal_integer_type(first_type) && is_binary_type(second_type))  {return true;}
   172   if (is_binary_type(first_type)          && is_literal_integer_type(second_type)) {return true;}
   139   if (is_real_type(first_type) && is_literal_real_type(second_type)) {return true;}
   173   if (is_literal_integer_type(first_type) && is_binary_type(second_type))          {return true;}
   140   if (is_literal_real_type(first_type) && is_real_type(second_type)) {return true;}
   174   if (is_real_type(first_type)            && is_literal_real_type(second_type))    {return true;}
       
   175   if (is_literal_real_type(first_type)    && is_real_type(second_type))            {return true;}
   141   return false;
   176   return false;
   142 }
   177 }
   143 
   178 
   144 symbol_c* search_expression_type_c::common_type(symbol_c *first_type, symbol_c *second_type) {
   179 symbol_c* search_expression_type_c::common_type(symbol_c *first_type, symbol_c *second_type) {
   145   if (first_type == NULL && second_type == NULL) {return NULL;}
   180   if (first_type == NULL && second_type == NULL) {return NULL;}
   146   if (first_type == NULL) {return second_type;}
   181   if (first_type == NULL) {return second_type;}
   147   if (second_type == NULL) {return first_type;}
   182   if (second_type == NULL) {return first_type;}
   148   if (typeid(*first_type) == typeid(*second_type)) {return first_type;}
   183   if (typeid(*first_type) == typeid(*second_type)) {return first_type;}
   149   if (is_integer_type(first_type) && is_literal_integer_type(second_type)) {return first_type;}
   184   if (is_integer_type(first_type)         && is_literal_integer_type(second_type)) {return first_type;}
   150   if (is_literal_integer_type(first_type) && is_integer_type(second_type)) {return second_type;}
   185   if (is_literal_integer_type(first_type) && is_integer_type(second_type))         {return second_type;}
   151   if (is_binary_type(first_type) && is_literal_integer_type(second_type))  {return first_type;}
   186   if (is_binary_type(first_type)          && is_literal_integer_type(second_type)) {return first_type;}
   152   if (is_literal_integer_type(first_type) && is_binary_type(second_type))  {return second_type;}
   187   if (is_literal_integer_type(first_type) && is_binary_type(second_type))          {return second_type;}
   153   if (is_real_type(first_type) && is_literal_real_type(second_type)) {return first_type;}
   188   if (is_real_type(first_type)            && is_literal_real_type(second_type))    {return first_type;}
   154   if (is_literal_real_type(first_type) && is_real_type(second_type)) {return second_type;}
   189   if (is_literal_real_type(first_type)    && is_real_type(second_type))            {return second_type;}
   155   return NULL;
   190   return NULL;
   156 }
   191 }
   157 
   192 
   158 
   193 
   159 integer_c search_expression_type_c::integer("1"); // what default value should we use here ???
   194 integer_c search_expression_type_c::integer("1"); // what default value should we use here ???