stage3/datatype_functions.hh
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 #ifndef _HELPER_FUNCTIONS_HH_
       
    26 #define _HELPER_FUNCTIONS_HH_
       
    27 
       
    28 #include "../absyntax/visitor.hh"
       
    29 #include <typeinfo>
       
    30 
       
    31 #define is_type(type_name_symbol, type_name_class)  ((type_name_symbol == NULL) ? false : (typeid(*type_name_symbol) == typeid(type_name_class)))
       
    32 #define sizeoftype(symbol) get_sizeof_datatype_c::getsize(symbol)
       
    33 
       
    34 /* Widening Primitive Conversion */
       
    35 struct widen_entry {
       
    36 	symbol_c *left;
       
    37 	symbol_c *right;
       
    38 	symbol_c *result;
       
    39 };
       
    40 /*
       
    41  * 2.5.1.5.6 Functions of time data types
       
    42  * Table 30 - page 64
       
    43  */
       
    44 extern const struct widen_entry widen_ADD_table[];
       
    45 extern const struct widen_entry widen_SUB_table[];
       
    46 extern const struct widen_entry widen_MUL_table[];
       
    47 extern const struct widen_entry widen_DIV_table[];
       
    48 
       
    49 /* A helper function... */
       
    50 bool is_ANY_ELEMENTARY_type         (symbol_c *type_symbol);
       
    51 bool is_ANY_SAFEELEMENTARY_type     (symbol_c *type_symbol);
       
    52 bool is_ANY_ELEMENTARY_compatible   (symbol_c *type_symbol);
       
    53 
       
    54 bool is_ANY_MAGNITUDE_type          (symbol_c *type_symbol);
       
    55 bool is_ANY_SAFEMAGNITUDE_type      (symbol_c *type_symbol);
       
    56 bool is_ANY_MAGNITUDE_compatible    (symbol_c *type_symbol);
       
    57 
       
    58 bool is_ANY_DATE_type               (symbol_c *type_symbol);
       
    59 bool is_ANY_SAFEDATE_type           (symbol_c *type_symbol);
       
    60 bool is_ANY_DATE_compatible         (symbol_c *type_symbol);
       
    61 
       
    62 bool is_ANY_STRING_type             (symbol_c *type_symbol);
       
    63 bool is_ANY_SAFESTRING_type         (symbol_c *type_symbol);
       
    64 bool is_ANY_STRING_compatible       (symbol_c *type_symbol);
       
    65 
       
    66 bool is_ANY_INT_type                (symbol_c *type_symbol);
       
    67 bool is_ANY_SAFEINT_type            (symbol_c *type_symbol);
       
    68 bool is_ANY_INT_compatible          (symbol_c *type_symbol);
       
    69 
       
    70 bool is_ANY_REAL_type               (symbol_c *type_symbol);
       
    71 bool is_ANY_SAFEREAL_type           (symbol_c *type_symbol);
       
    72 bool is_ANY_REAL_compatible         (symbol_c *type_symbol);
       
    73 
       
    74 bool is_ANY_NUM_type                (symbol_c *type_symbol);
       
    75 bool is_ANY_SAFENUM_type            (symbol_c *type_symbol);
       
    76 bool is_ANY_NUM_compatible          (symbol_c *type_symbol);
       
    77 
       
    78 bool is_ANY_BIT_type                (symbol_c *type_symbol);
       
    79 bool is_ANY_SAFEBIT_type            (symbol_c *type_symbol);
       
    80 bool is_ANY_BIT_compatible          (symbol_c *type_symbol);
       
    81 
       
    82 bool is_BOOL_type                   (symbol_c *type_symbol);
       
    83 bool is_SAFEBOOL_type               (symbol_c *type_symbol);
       
    84 bool is_ANY_BOOL_compatible         (symbol_c *type_symbol);
       
    85 
       
    86 bool is_nonneg_literal_integer_type (symbol_c *type_symbol);
       
    87 bool is_literal_integer_type        (symbol_c *type_symbol);
       
    88 bool is_literal_real_type           (symbol_c *type_symbol);
       
    89 bool is_literal_bool_type           (symbol_c *type_symbol);
       
    90 
       
    91 /* Determine the common data type between two data types.
       
    92  * If no common data type found, return NULL.
       
    93  *
       
    94  * If data types are identical, return the first (any would do...).
       
    95  * If any of the data types is a literal, we confirm that
       
    96  *   the literal uses less bits than the fixed size data type.
       
    97  *   e.g. BYTE and 1024 returns NULL
       
    98  *        BYTE and 255  returns BYTE
       
    99  *
       
   100  * If two literals, then return the literal that requires more bits...
       
   101  */
       
   102 symbol_c *common_type(symbol_c *first_type, symbol_c *second_type);
       
   103 bool is_valid_assignment(symbol_c *var_type, symbol_c *value_type);
       
   104 bool is_compatible_type(symbol_c *first_type, symbol_c *second_type);
       
   105 bool is_type_equal(symbol_c *first_type, symbol_c *second_type);
       
   106 
       
   107 typedef bool (*helper_function_t) (symbol_c *type_symbol);  /* a pointer to a function! */
       
   108 
       
   109 
       
   110 
       
   111 #endif /* _HELPER_FUNCTIONS_HH_ */