absyntax_utils/search_constant_type.cc
changeset 698 ec8df1de3e08
parent 697 bb4511694c0e
child 699 0307fa16db3f
equal deleted inserted replaced
697:bb4511694c0e 698:ec8df1de3e08
     1 /*
       
     2  *  matiec - a compiler for the programming languages defined in IEC 61131-3
       
     3  *
       
     4  *  Copyright (C) 2003-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 /* Determine the data type of a specific constant or variable.
       
    34  * A reference to the relevant type definition is returned.
       
    35  *
       
    36  * For example:
       
    37  *       22          -> returns reference to a int_type_name_c object.
       
    38  *       22.2        -> returns reference to a real_type_name_c object.
       
    39  *       LREAL#22.2  -> returns reference to a lreal_type_name_c object.
       
    40  *       etc...
       
    41  */
       
    42 
       
    43 
       
    44 #include "../util/symtable.hh"
       
    45 #include "search_constant_type.hh"
       
    46 #include "absyntax_utils.hh"
       
    47 #include "../main.hh" // required for ERROR() and ERROR_MSG() macros.
       
    48 
       
    49 
       
    50 
       
    51 symbol_c *search_constant_type_c::get_type(symbol_c *constant) {
       
    52   return (symbol_c *)constant->accept(*this);
       
    53 }
       
    54 
       
    55 
       
    56 /*********************/
       
    57 /* B 1.2 - Constants */
       
    58 /*********************/
       
    59 
       
    60 /******************************/
       
    61 /* B 1.2.1 - Numeric Literals */
       
    62 /******************************/
       
    63 /* Numeric literals without any explicit type cast have unknown data type, 
       
    64   * so we continue considering them as their own basic data types until
       
    65   * they can be resolved (for example, when using '30+x' where 'x' is a LINT variable, the
       
    66   * numeric literal '30' must then be considered a LINT so the ADD function may be called
       
    67   * with all inputs of the same data type.
       
    68   * If 'x' were a SINT, then the '30' would have to be a SINT too!
       
    69   */
       
    70 void *search_constant_type_c::visit(real_c *symbol)           {return (void *)symbol;}
       
    71 void *search_constant_type_c::visit(neg_real_c *symbol)       {return (void *)symbol;}
       
    72 void *search_constant_type_c::visit(integer_c *symbol)        {return (void *)symbol;}
       
    73 void *search_constant_type_c::visit(neg_integer_c *symbol)    {return (void *)symbol;}
       
    74 void *search_constant_type_c::visit(binary_integer_c *symbol) {return (void *)symbol;}
       
    75 void *search_constant_type_c::visit(octal_integer_c *symbol)  {return (void *)symbol;}
       
    76 void *search_constant_type_c::visit(hex_integer_c *symbol)    {return (void *)symbol;}
       
    77 
       
    78 void *search_constant_type_c::visit(integer_literal_c *symbol)
       
    79   {return (void *)((symbol->type!=NULL)?symbol->type:symbol->value->accept(*this));}
       
    80 void *search_constant_type_c::visit(real_literal_c *symbol)
       
    81   {return (void *)((symbol->type!=NULL)?symbol->type:symbol->value->accept(*this));}
       
    82 void *search_constant_type_c::visit(bit_string_literal_c *symbol)
       
    83   {return (void *)((symbol->type!=NULL)?symbol->type:symbol->value->accept(*this));}
       
    84 void *search_constant_type_c::visit(boolean_literal_c *symbol)
       
    85   {return (void *)((symbol->type!=NULL)?symbol->type:symbol->value->accept(*this));}
       
    86 
       
    87 void *search_constant_type_c::visit(boolean_true_c *symbol)   {return (void *)symbol;}
       
    88 void *search_constant_type_c::visit(boolean_false_c *symbol)  {return (void *)symbol;}
       
    89 
       
    90 
       
    91 /*******************************/
       
    92 /* B.1.2.2   Character Strings */
       
    93 /*******************************/
       
    94 void *search_constant_type_c::visit(double_byte_character_string_c *symbol) {return (void *)&wstring_type_name;}
       
    95 void *search_constant_type_c::visit(single_byte_character_string_c *symbol) {return (void *)&string_type_name;}
       
    96 
       
    97 /***************************/
       
    98 /* B 1.2.3 - Time Literals */
       
    99 /***************************/
       
   100 /************************/
       
   101 /* B 1.2.3.1 - Duration */
       
   102 /************************/
       
   103 void *search_constant_type_c::visit(neg_time_c *symbol) {ERROR; return NULL;}  /* this member function should never be called. */
       
   104 void *search_constant_type_c::visit(duration_c *symbol) {return (void *)(symbol->type_name);}
       
   105 void *search_constant_type_c::visit(fixed_point_c *symbol) {ERROR; return NULL;}  /* this member function should never be called. */
       
   106 void *search_constant_type_c::visit(interval_c *symbol) {ERROR; return NULL;}  /* this member function should never be called. */
       
   107 
       
   108 /************************************/
       
   109 /* B 1.2.3.2 - Time of day and Date */
       
   110 /************************************/
       
   111 void *search_constant_type_c::visit(time_of_day_c *symbol) {return (void *)(symbol->type_name);}
       
   112 void *search_constant_type_c::visit(daytime_c *symbol) {ERROR; return NULL;}  /* this member function should never be called. */
       
   113 void *search_constant_type_c::visit(date_c *symbol) {return (void *)(symbol->type_name);}
       
   114 void *search_constant_type_c::visit(date_literal_c *symbol) {ERROR; return NULL;}  /* this member function should never be called. */
       
   115 void *search_constant_type_c::visit(date_and_time_c *symbol) {return (void *)(symbol->type_name);}
       
   116 
       
   117 /********************************/
       
   118 /* B 1.3.3 - Derived data types */
       
   119 /********************************/
       
   120 void *search_constant_type_c::visit(enumerated_value_c *symbol) {
       
   121   if (symbol->type != NULL)
       
   122     return (void *)(symbol->type);
       
   123 
       
   124   symbol_c *value_type = enumerated_value_symtable.find_value(symbol->value);
       
   125   if (value_type == enumerated_value_symtable.end_value())
       
   126     return NULL;
       
   127   return (void *)value_type;
       
   128 }
       
   129 
       
   130 
       
   131 
       
   132 
       
   133 invalid_type_name_c  search_constant_type_c::invalid_type_name;
       
   134 
       
   135 
       
   136 real_type_name_c     search_constant_type_c::real_type_name;
       
   137 sint_type_name_c     search_constant_type_c::sint_type_name;
       
   138 lint_type_name_c     search_constant_type_c::lint_type_name;
       
   139 dint_type_name_c     search_constant_type_c::dint_type_name;
       
   140 date_type_name_c     search_constant_type_c::date_type_name;
       
   141 dword_type_name_c    search_constant_type_c::dword_type_name;
       
   142 dt_type_name_c       search_constant_type_c::dt_type_name;
       
   143 tod_type_name_c      search_constant_type_c::tod_type_name;
       
   144 udint_type_name_c    search_constant_type_c::udint_type_name;
       
   145 word_type_name_c     search_constant_type_c::word_type_name;
       
   146 wstring_type_name_c  search_constant_type_c::wstring_type_name;
       
   147 string_type_name_c   search_constant_type_c::string_type_name;
       
   148 lword_type_name_c    search_constant_type_c::lword_type_name;
       
   149 uint_type_name_c     search_constant_type_c::uint_type_name;
       
   150 lreal_type_name_c    search_constant_type_c::lreal_type_name;
       
   151 byte_type_name_c     search_constant_type_c::byte_type_name;
       
   152 usint_type_name_c    search_constant_type_c::usint_type_name;
       
   153 ulint_type_name_c    search_constant_type_c::ulint_type_name;
       
   154 bool_type_name_c     search_constant_type_c::bool_type_name;
       
   155 time_type_name_c     search_constant_type_c::time_type_name;
       
   156 int_type_name_c      search_constant_type_c::int_type_name;
       
   157 
       
   158 safetime_type_name_c     search_constant_type_c::safetime_type_name;
       
   159 safetod_type_name_c      search_constant_type_c::safetod_type_name;
       
   160 safedt_type_name_c       search_constant_type_c::safedt_type_name;
       
   161 safedate_type_name_c     search_constant_type_c::safedate_type_name;
       
   162 safereal_type_name_c     search_constant_type_c::safereal_type_name;
       
   163 safesint_type_name_c     search_constant_type_c::safesint_type_name;
       
   164 safelint_type_name_c     search_constant_type_c::safelint_type_name;
       
   165 safedint_type_name_c     search_constant_type_c::safedint_type_name;
       
   166 safedword_type_name_c    search_constant_type_c::safedword_type_name;
       
   167 safeudint_type_name_c    search_constant_type_c::safeudint_type_name;
       
   168 safeword_type_name_c     search_constant_type_c::safeword_type_name;
       
   169 safewstring_type_name_c  search_constant_type_c::safewstring_type_name;
       
   170 safestring_type_name_c   search_constant_type_c::safestring_type_name;
       
   171 safelword_type_name_c    search_constant_type_c::safelword_type_name;
       
   172 safeuint_type_name_c     search_constant_type_c::safeuint_type_name;
       
   173 safelreal_type_name_c    search_constant_type_c::safelreal_type_name;
       
   174 safebyte_type_name_c     search_constant_type_c::safebyte_type_name;
       
   175 safeusint_type_name_c    search_constant_type_c::safeusint_type_name;
       
   176 safeulint_type_name_c    search_constant_type_c::safeulint_type_name;
       
   177 safebool_type_name_c     search_constant_type_c::safebool_type_name;
       
   178 safeint_type_name_c      search_constant_type_c::safeint_type_name;
       
   179 
       
   180