msousa@417: /* msousa@417: * matiec - a compiler for the programming languages defined in IEC 61131-3 msousa@417: * msousa@417: * Copyright (C) 2009-2012 Mario de Sousa (msousa@fe.up.pt) msousa@417: * Copyright (C) 2012 Manuele Conti (conti.ma@alice.it) msousa@417: * msousa@417: * This program is free software: you can redistribute it and/or modify msousa@417: * it under the terms of the GNU General Public License as published by msousa@417: * the Free Software Foundation, either version 3 of the License, or msousa@417: * (at your option) any later version. msousa@417: * msousa@417: * This program is distributed in the hope that it will be useful, msousa@417: * but WITHOUT ANY WARRANTY; without even the implied warranty of msousa@417: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the msousa@417: * GNU General Public License for more details. msousa@417: * msousa@417: * You should have received a copy of the GNU General Public License msousa@417: * along with this program. If not, see . msousa@417: * msousa@417: * msousa@417: * This code is made available on the understanding that it will not be msousa@417: * used in safety-critical situations without a full and competent review. msousa@417: */ msousa@417: msousa@417: #ifndef _HELPER_FUNCTIONS_HH_ msousa@417: #define _HELPER_FUNCTIONS_HH_ msousa@417: msousa@417: #include "../absyntax/visitor.hh" msousa@417: #include msousa@417: msousa@417: #define is_type(type_name_symbol, type_name_class) ((type_name_symbol == NULL) ? false : (typeid(*type_name_symbol) == typeid(type_name_class))) msousa@417: #define sizeoftype(symbol) get_sizeof_datatype_c::getsize(symbol) msousa@417: msousa@417: /* Widening Primitive Conversion */ msousa@417: struct widen_entry { msousa@417: symbol_c *left; msousa@417: symbol_c *right; msousa@417: symbol_c *result; msousa@417: }; msousa@417: /* msousa@417: * 2.5.1.5.6 Functions of time data types msousa@417: * Table 30 - page 64 msousa@417: */ msousa@417: extern const struct widen_entry widen_ADD_table[]; msousa@417: extern const struct widen_entry widen_SUB_table[]; msousa@417: extern const struct widen_entry widen_MUL_table[]; msousa@417: extern const struct widen_entry widen_DIV_table[]; msousa@417: msousa@417: /* A helper function... */ msousa@417: bool is_ANY_ELEMENTARY_type (symbol_c *type_symbol); msousa@417: bool is_ANY_SAFEELEMENTARY_type (symbol_c *type_symbol); msousa@417: bool is_ANY_ELEMENTARY_compatible (symbol_c *type_symbol); msousa@417: msousa@417: bool is_ANY_MAGNITUDE_type (symbol_c *type_symbol); msousa@417: bool is_ANY_SAFEMAGNITUDE_type (symbol_c *type_symbol); msousa@417: bool is_ANY_MAGNITUDE_compatible (symbol_c *type_symbol); msousa@417: msousa@417: bool is_ANY_DATE_type (symbol_c *type_symbol); msousa@417: bool is_ANY_SAFEDATE_type (symbol_c *type_symbol); msousa@417: bool is_ANY_DATE_compatible (symbol_c *type_symbol); msousa@417: msousa@417: bool is_ANY_STRING_type (symbol_c *type_symbol); msousa@417: bool is_ANY_SAFESTRING_type (symbol_c *type_symbol); msousa@417: bool is_ANY_STRING_compatible (symbol_c *type_symbol); msousa@417: msousa@417: bool is_ANY_INT_type (symbol_c *type_symbol); msousa@417: bool is_ANY_SAFEINT_type (symbol_c *type_symbol); msousa@417: bool is_ANY_INT_compatible (symbol_c *type_symbol); msousa@417: msousa@417: bool is_ANY_REAL_type (symbol_c *type_symbol); msousa@417: bool is_ANY_SAFEREAL_type (symbol_c *type_symbol); msousa@417: bool is_ANY_REAL_compatible (symbol_c *type_symbol); msousa@417: msousa@417: bool is_ANY_NUM_type (symbol_c *type_symbol); msousa@417: bool is_ANY_SAFENUM_type (symbol_c *type_symbol); msousa@417: bool is_ANY_NUM_compatible (symbol_c *type_symbol); msousa@417: msousa@417: bool is_ANY_BIT_type (symbol_c *type_symbol); msousa@417: bool is_ANY_SAFEBIT_type (symbol_c *type_symbol); msousa@417: bool is_ANY_BIT_compatible (symbol_c *type_symbol); msousa@417: msousa@417: bool is_BOOL_type (symbol_c *type_symbol); msousa@417: bool is_SAFEBOOL_type (symbol_c *type_symbol); msousa@417: bool is_ANY_BOOL_compatible (symbol_c *type_symbol); msousa@417: msousa@417: bool is_nonneg_literal_integer_type (symbol_c *type_symbol); msousa@417: bool is_literal_integer_type (symbol_c *type_symbol); msousa@417: bool is_literal_real_type (symbol_c *type_symbol); msousa@417: bool is_literal_bool_type (symbol_c *type_symbol); msousa@417: msousa@417: /* Determine the common data type between two data types. msousa@417: * If no common data type found, return NULL. msousa@417: * msousa@417: * If data types are identical, return the first (any would do...). msousa@417: * If any of the data types is a literal, we confirm that msousa@417: * the literal uses less bits than the fixed size data type. msousa@417: * e.g. BYTE and 1024 returns NULL msousa@417: * BYTE and 255 returns BYTE msousa@417: * msousa@417: * If two literals, then return the literal that requires more bits... msousa@417: */ msousa@417: symbol_c *common_type(symbol_c *first_type, symbol_c *second_type); msousa@417: bool is_valid_assignment(symbol_c *var_type, symbol_c *value_type); msousa@417: bool is_compatible_type(symbol_c *first_type, symbol_c *second_type); msousa@417: bool is_type_equal(symbol_c *first_type, symbol_c *second_type); msousa@417: msousa@417: typedef bool (*helper_function_t) (symbol_c *type_symbol); /* a pointer to a function! */ msousa@417: msousa@417: msousa@417: msousa@417: #endif /* _HELPER_FUNCTIONS_HH_ */