stage4/generate_cc/search_constant_type.cc
changeset 70 e1f0ebd2d9ec
parent 69 41cb5b80416e
child 71 c2c867171c07
equal deleted inserted replaced
69:41cb5b80416e 70:e1f0ebd2d9ec
     1 /*
       
     2  * (c) 2003 Mario de Sousa
       
     3  *
       
     4  * Offered to the public under the terms of the GNU General Public License
       
     5  * as published by the Free Software Foundation; either version 2 of the
       
     6  * License, or (at your option) any later version.
       
     7  *
       
     8  * This program is distributed in the hope that it will be useful, but
       
     9  * WITHOUT ANY WARRANTY; without even the implied warranty of
       
    10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
       
    11  * Public License for more details.
       
    12  *
       
    13  * 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.
       
    15  */
       
    16 
       
    17 /*
       
    18  * An IEC 61131-3 IL and ST compiler.
       
    19  *
       
    20  * Based on the
       
    21  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
       
    22  *
       
    23  */
       
    24 
       
    25 
       
    26 
       
    27 /* Determine the data type of a specific constant or variable.
       
    28  * A reference to the relevant type definition is returned.
       
    29  *
       
    30  * For example:
       
    31  *       22          -> returns reference to a int_type_name_c object.
       
    32  *       22.2        -> returns reference to a real_type_name_c object.
       
    33  *       LREAL#22.2  -> returns reference to a lreal_type_name_c object.
       
    34  *       etc...
       
    35  */
       
    36 class search_constant_type_c: public search_visitor_c {
       
    37 
       
    38   public:
       
    39 /**********************/
       
    40 /* B.1.3 - Data types */
       
    41 /**********************/
       
    42 /***********************************/
       
    43 /* B 1.3.1 - Elementary Data Types */
       
    44 /***********************************/
       
    45   static real_type_name_c     real_type_name;
       
    46   static sint_type_name_c     sint_type_name;
       
    47   static lint_type_name_c     lint_type_name;
       
    48   static dint_type_name_c     dint_type_name;
       
    49   static date_type_name_c     date_type_name;
       
    50   static dword_type_name_c     dword_type_name;
       
    51   static dt_type_name_c     dt_type_name;
       
    52   static tod_type_name_c     tod_type_name;
       
    53   static udint_type_name_c     udint_type_name;
       
    54   static word_type_name_c     word_type_name;
       
    55   static wstring_type_name_c     wstring_type_name;
       
    56   static string_type_name_c     string_type_name;
       
    57   static lword_type_name_c     lword_type_name;
       
    58   static uint_type_name_c     uint_type_name;
       
    59   static lreal_type_name_c     lreal_type_name;
       
    60   static byte_type_name_c     byte_type_name;
       
    61   static usint_type_name_c     usint_type_name;
       
    62   static ulint_type_name_c     ulint_type_name;
       
    63   static bool_type_name_c     bool_type_name;
       
    64   static time_type_name_c     time_type_name;
       
    65   static int_type_name_c     int_type_name;
       
    66 
       
    67   static constant_real_type_name_c     constant_real_type_name;
       
    68   static constant_int_type_name_c      constant_int_type_name;
       
    69 
       
    70   public:
       
    71     symbol_c *get_type(symbol_c *constant) {
       
    72       return (symbol_c *)constant->accept(*this);
       
    73     }
       
    74 
       
    75   public:
       
    76 /*********************/
       
    77 /* B 1.2 - Constants */
       
    78 /*********************/
       
    79 
       
    80 /******************************/
       
    81 /* B 1.2.1 - Numeric Literals */
       
    82 /******************************/
       
    83     void *visit(real_c *symbol) {return (void *)&constant_real_type_name;}
       
    84     void *visit(integer_c *symbol) {return (void *)&constant_int_type_name;}
       
    85     void *visit(binary_integer_c *symbol) {return (void *)&constant_int_type_name;}
       
    86     void *visit(octal_integer_c *symbol) {return (void *)&constant_int_type_name;}
       
    87     void *visit(hex_integer_c *symbol) {return (void *)&constant_int_type_name;}
       
    88 
       
    89     void *visit(numeric_literal_c *symbol)
       
    90       {return (void *)((symbol->type!=NULL)?symbol->type:symbol->value->accept(*this));}
       
    91     void *visit(integer_literal_c *symbol)
       
    92       {return (void *)((symbol->type!=NULL)?symbol->type:symbol->value->accept(*this));}
       
    93     void *visit(real_literal_c *symbol)
       
    94       {return (void *)((symbol->type!=NULL)?symbol->type:symbol->value->accept(*this));}
       
    95     void *visit(bit_string_literal_c *symbol)
       
    96       {return (void *)((symbol->type!=NULL)?symbol->type:symbol->value->accept(*this));}
       
    97     void *visit(boolean_literal_c *symbol)
       
    98       {return (void *)((symbol->type!=NULL)?symbol->type:symbol->value->accept(*this));}
       
    99 
       
   100 /*******************************/
       
   101 /* B.1.2.2   Character Strings */
       
   102 /*******************************/
       
   103     void *visit(double_byte_character_string_c *symbol) {return (void *)&wstring_type_name;}
       
   104     void *visit(single_byte_character_string_c *symbol) {return (void *)&string_type_name;}
       
   105 
       
   106 /***************************/
       
   107 /* B 1.2.3 - Time Literals */
       
   108 /***************************/
       
   109 /************************/
       
   110 /* B 1.2.3.1 - Duration */
       
   111 /************************/
       
   112     void *visit(neg_time_c *symbol) {ERROR; return NULL;}  /* this member function should never be called. */
       
   113     void *visit(duration_c *symbol) {return (void *)&time_type_name;}
       
   114     void *visit(fixed_point_c *symbol) {ERROR; return NULL;}  /* this member function should never be called. */
       
   115     void *visit(days_c *symbol) {ERROR; return NULL;}  /* this member function should never be called. */
       
   116     void *visit(hours_c *symbol) {ERROR; return NULL;}  /* this member function should never be called. */
       
   117     void *visit(minutes_c *symbol) {ERROR; return NULL;}  /* this member function should never be called. */
       
   118     void *visit(seconds_c *symbol) {ERROR; return NULL;}  /* this member function should never be called. */
       
   119     void *visit(milliseconds_c *symbol) {ERROR; return NULL;}  /* this member function should never be called. */
       
   120 
       
   121 /************************************/
       
   122 /* B 1.2.3.2 - Time of day and Date */
       
   123 /************************************/
       
   124     void *visit(time_of_day_c *symbol) {return (void *)&tod_type_name;}
       
   125     void *visit(daytime_c *symbol) {ERROR; return NULL;}  /* this member function should never be called. */
       
   126     void *visit(date_c *symbol) {return (void *)&date_type_name;}
       
   127     void *visit(date_literal_c *symbol) {ERROR; return NULL;}  /* this member function should never be called. */
       
   128     void *visit(date_and_time_c *symbol) {return (void *)&dt_type_name;}
       
   129 };
       
   130 
       
   131 
       
   132 real_type_name_c     search_constant_type_c::real_type_name;
       
   133 sint_type_name_c     search_constant_type_c::sint_type_name;
       
   134 lint_type_name_c     search_constant_type_c::lint_type_name;
       
   135 dint_type_name_c     search_constant_type_c::dint_type_name;
       
   136 date_type_name_c     search_constant_type_c::date_type_name;
       
   137 dword_type_name_c     search_constant_type_c::dword_type_name;
       
   138 dt_type_name_c     search_constant_type_c::dt_type_name;
       
   139 tod_type_name_c     search_constant_type_c::tod_type_name;
       
   140 udint_type_name_c     search_constant_type_c::udint_type_name;
       
   141 word_type_name_c     search_constant_type_c::word_type_name;
       
   142 wstring_type_name_c     search_constant_type_c::wstring_type_name;
       
   143 string_type_name_c     search_constant_type_c::string_type_name;
       
   144 lword_type_name_c     search_constant_type_c::lword_type_name;
       
   145 uint_type_name_c     search_constant_type_c::uint_type_name;
       
   146 lreal_type_name_c     search_constant_type_c::lreal_type_name;
       
   147 byte_type_name_c     search_constant_type_c::byte_type_name;
       
   148 usint_type_name_c     search_constant_type_c::usint_type_name;
       
   149 ulint_type_name_c     search_constant_type_c::ulint_type_name;
       
   150 bool_type_name_c     search_constant_type_c::bool_type_name;
       
   151 time_type_name_c     search_constant_type_c::time_type_name;
       
   152 int_type_name_c     search_constant_type_c::int_type_name;
       
   153 
       
   154 constant_real_type_name_c     search_constant_type_c::constant_real_type_name;
       
   155 constant_int_type_name_c      search_constant_type_c::constant_int_type_name;
       
   156 
       
   157 
       
   158 
       
   159 
       
   160 
       
   161