ccb@204: /* ccb@204: * (c) 2009 Mario de Sousa ccb@204: * ccb@204: * Offered to the public under the terms of the GNU General Public License ccb@204: * as published by the Free Software Foundation; either version 2 of the ccb@204: * License, or (at your option) any later version. ccb@204: * ccb@204: * This program is distributed in the hope that it will be useful, but ccb@204: * WITHOUT ANY WARRANTY; without even the implied warranty of ccb@204: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General ccb@204: * Public License for more details. ccb@204: * ccb@204: * This code is made available on the understanding that it will not be ccb@204: * used in safety-critical situations without a full and competent review. ccb@204: */ ccb@204: ccb@204: /* ccb@204: * An IEC 61131-3 IL and ST compiler. ccb@204: * ccb@204: * Based on the ccb@204: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) ccb@204: * ccb@204: */ ccb@204: ccb@204: ccb@204: /* Verify whether the semantic rules of data type compatibility are being followed. ccb@204: * ccb@204: * For example: ccb@204: */ ccb@204: ccb@204: #include "visit_expression_type.hh" ccb@204: #include ccb@204: #include ccb@204: #include ccb@204: #include ccb@204: #include ccb@204: ccb@204: ccb@204: ccb@204: #define FIRST_(symbol1, symbol2) (((symbol1)->first_line < (symbol2)->first_line) ? (symbol1) : \ ccb@204: ((symbol1)->first_line > (symbol2)->first_line) ? (symbol2) : \ ccb@204: ((symbol1)->first_column < (symbol2)->first_column) ? (symbol1) : \ ccb@204: ((symbol1)->first_column > (symbol2)->first_column) ? (symbol2) : \ ccb@204: (symbol1)) ccb@204: ccb@204: #define LAST_(symbol1, symbol2) (((symbol1)->last_line < (symbol2)->last_line) ? (symbol2) : \ ccb@204: ((symbol1)->last_line > (symbol2)->last_line) ? (symbol1) : \ ccb@204: ((symbol1)->last_column < (symbol2)->last_column) ? (symbol2) : \ ccb@204: ((symbol1)->last_column > (symbol2)->last_column) ? (symbol1) : \ ccb@204: (symbol1)) ccb@204: ccb@204: #define STAGE3_ERROR(symbol1, symbol2, msg) { \ laurent@210: /*printf("semantic error between (%d:%d) and (%d:%d): %s\n", \ ccb@204: FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column, \ ccb@204: LAST_(symbol1,symbol2) ->last_line, LAST_(symbol1,symbol2) ->last_column, \ laurent@210: msg);*/ \ ccb@204: il_error = true; \ ccb@204: } ccb@204: ccb@204: ccb@204: ccb@204: ccb@204: ccb@204: void *visit_expression_type_c::visit(program_declaration_c *symbol) { ccb@204: search_varfb_instance_type = new search_varfb_instance_type_c(symbol); ccb@204: symbol->var_declarations->accept(*this); laurent@210: //printf("checking semantics in body of program %s\n", ((token_c *)(symbol->program_type_name))->value); ccb@204: il_parenthesis_level = 0; ccb@204: il_error = false; ccb@204: il_default_variable_type = NULL; ccb@204: symbol->function_block_body->accept(*this); ccb@204: il_default_variable_type = NULL; ccb@204: delete search_varfb_instance_type; msousa@257: search_varfb_instance_type = NULL; ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: void *visit_expression_type_c::visit(function_declaration_c *symbol) { ccb@204: search_varfb_instance_type = new search_varfb_instance_type_c(symbol); ccb@204: symbol->var_declarations_list->accept(*this); laurent@210: //printf("checking semantics in body of function %s\n", ((token_c *)(symbol->derived_function_name))->value); ccb@204: il_parenthesis_level = 0; ccb@204: il_error = false; ccb@204: il_default_variable_type = NULL; ccb@204: symbol->function_body->accept(*this); ccb@204: il_default_variable_type = NULL; ccb@204: delete search_varfb_instance_type; msousa@257: search_varfb_instance_type = NULL; ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: void *visit_expression_type_c::visit(function_block_declaration_c *symbol) { ccb@204: search_varfb_instance_type = new search_varfb_instance_type_c(symbol); ccb@204: symbol->var_declarations->accept(*this); laurent@210: //printf("checking semantics in body of FB %s\n", ((token_c *)(symbol->fblock_name))->value); ccb@204: il_parenthesis_level = 0; ccb@204: il_error = false; ccb@204: il_default_variable_type = NULL; ccb@204: symbol->fblock_body->accept(*this); ccb@204: il_default_variable_type = NULL; ccb@204: delete search_varfb_instance_type; msousa@257: search_varfb_instance_type = NULL; ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: ccb@204: ccb@204: ccb@204: ccb@204: ccb@204: ccb@204: ccb@204: ccb@204: visit_expression_type_c::visit_expression_type_c(symbol_c *search_scope) { ccb@204: } ccb@204: ccb@204: visit_expression_type_c::~visit_expression_type_c(void) { ccb@204: } ccb@204: ccb@204: msousa@257: msousa@257: msousa@257: /* NOTE on data type handling and literals... msousa@257: * ========================================== msousa@257: * msousa@257: * Literals that are explicitly type cast msousa@257: * e.g.: BYTE#42 msousa@257: * INT#65 msousa@257: * TIME#45h23m msousa@257: * etc... msousa@257: * are NOT considered literals in the following code. msousa@257: * Since they are type cast, and their data type is fixed and well known, msousa@257: * they are treated as a variable of that data type (except when determining lvalues) msousa@257: * In other words, when calling search_constant_type_c on these constants, it returns msousa@257: * a xxxxx_type_name_c, and not one of the xxxx_literal_c ! msousa@257: * msousa@257: * When the following code handles a literal, it is really a literal of unknown data type. msousa@257: * e.g. 42, may be considered an int, a byte, a word, etc... msousa@257: */ msousa@257: ccb@204: /* A helper function... */ ccb@204: bool visit_expression_type_c::is_ANY_ELEMENTARY_type(symbol_c *type_symbol) { ccb@204: if (type_symbol == NULL) {ERROR;} ccb@204: return is_ANY_MAGNITUDE_type(type_symbol) msousa@257: || is_ANY_BIT_type (type_symbol) msousa@257: || is_ANY_STRING_type (type_symbol) msousa@257: || is_ANY_DATE_type (type_symbol); msousa@257: } msousa@257: msousa@257: /* A helper function... */ msousa@257: bool visit_expression_type_c::is_ANY_SAFEELEMENTARY_type(symbol_c *type_symbol) { msousa@257: if (type_symbol == NULL) {ERROR;} msousa@257: return is_ANY_SAFEMAGNITUDE_type(type_symbol) msousa@257: || is_ANY_SAFEBIT_type (type_symbol) msousa@257: || is_ANY_SAFESTRING_type (type_symbol) msousa@257: || is_ANY_SAFEDATE_type (type_symbol); msousa@257: } msousa@257: msousa@257: /* A helper function... */ msousa@257: bool visit_expression_type_c::is_ANY_ELEMENTARY_compatible(symbol_c *type_symbol) { msousa@257: if (type_symbol == NULL) {ERROR;} msousa@257: /* NOTE: doing msousa@257: * return is_ANY_SAFEELEMENTARY_type() || is_ANY_ELEMENTARY_type() msousa@257: * is incorrect, as the literals would never be considered compatible... msousa@257: */ msousa@257: return is_ANY_MAGNITUDE_compatible(type_symbol) msousa@257: || is_ANY_BIT_compatible (type_symbol) msousa@257: || is_ANY_STRING_compatible (type_symbol) msousa@257: || is_ANY_DATE_compatible (type_symbol); ccb@204: } ccb@204: ccb@204: ccb@204: /* A helper function... */ ccb@204: bool visit_expression_type_c::is_ANY_MAGNITUDE_type(symbol_c *type_symbol) { ccb@204: if (type_symbol == NULL) {ERROR;} ccb@204: if (typeid(*type_symbol) == typeid(time_type_name_c)) {return true;} ccb@204: return is_ANY_NUM_type(type_symbol); ccb@204: } ccb@204: msousa@257: /* A helper function... */ msousa@257: bool visit_expression_type_c::is_ANY_SAFEMAGNITUDE_type(symbol_c *type_symbol) { msousa@257: if (type_symbol == NULL) {ERROR;} msousa@257: if (typeid(*type_symbol) == typeid(safetime_type_name_c)) {return true;} msousa@257: return is_ANY_SAFENUM_type(type_symbol); msousa@257: } msousa@257: msousa@257: /* A helper function... */ msousa@257: bool visit_expression_type_c::is_ANY_MAGNITUDE_compatible(symbol_c *type_symbol) { msousa@257: if (type_symbol == NULL) {ERROR;} msousa@257: if (is_ANY_MAGNITUDE_type (type_symbol)) {return true;} msousa@257: if (is_ANY_SAFEMAGNITUDE_type(type_symbol)) {return true;} msousa@257: msousa@257: return is_ANY_NUM_compatible(type_symbol); msousa@257: } ccb@204: ccb@204: /* A helper function... */ ccb@204: bool visit_expression_type_c::is_ANY_NUM_type(symbol_c *type_symbol) { ccb@204: if (type_symbol == NULL) {ERROR;} msousa@257: if (is_ANY_REAL_type(type_symbol)) {return true;} msousa@257: if (is_ANY_INT_type(type_symbol)) {return true;} msousa@257: return false; msousa@257: } msousa@257: msousa@257: /* A helper function... */ msousa@257: bool visit_expression_type_c::is_ANY_SAFENUM_type(symbol_c *type_symbol) { msousa@257: if (type_symbol == NULL) {ERROR;} msousa@257: return is_ANY_SAFEREAL_type(type_symbol) msousa@257: || is_ANY_SAFEINT_type (type_symbol); msousa@257: } msousa@257: msousa@257: /* A helper function... */ msousa@257: bool visit_expression_type_c::is_ANY_NUM_compatible(symbol_c *type_symbol) { msousa@257: if (type_symbol == NULL) {ERROR;} msousa@257: if (is_ANY_REAL_compatible(type_symbol)) {return true;} msousa@257: if (is_ANY_INT_compatible(type_symbol)) {return true;} msousa@257: return false; msousa@257: } ccb@204: ccb@204: /* A helper function... */ ccb@204: bool visit_expression_type_c::is_ANY_DATE_type(symbol_c *type_symbol) { ccb@204: if (type_symbol == NULL) {ERROR;} ccb@204: if (typeid(*type_symbol) == typeid(date_type_name_c)) {return true;} msousa@257: if (typeid(*type_symbol) == typeid(tod_type_name_c)) {return true;} msousa@257: if (typeid(*type_symbol) == typeid(dt_type_name_c)) {return true;} ccb@204: return false; ccb@204: } ccb@204: msousa@257: /* A helper function... */ msousa@257: bool visit_expression_type_c::is_ANY_SAFEDATE_type(symbol_c *type_symbol) { msousa@257: if (type_symbol == NULL) {ERROR;} msousa@257: if (typeid(*type_symbol) == typeid(safedate_type_name_c)) {return true;} msousa@257: if (typeid(*type_symbol) == typeid(safetod_type_name_c)) {return true;} msousa@257: if (typeid(*type_symbol) == typeid(safedt_type_name_c)) {return true;} msousa@257: return false; msousa@257: } msousa@257: msousa@257: /* A helper function... */ msousa@257: bool visit_expression_type_c::is_ANY_DATE_compatible(symbol_c *type_symbol) { msousa@257: if (type_symbol == NULL) {ERROR;} msousa@257: if (is_ANY_DATE_type (type_symbol)) {return true;} msousa@257: if (is_ANY_SAFEDATE_type(type_symbol)) {return true;} msousa@257: return false; msousa@257: } ccb@204: ccb@204: /* A helper function... */ ccb@204: bool visit_expression_type_c::is_ANY_STRING_type(symbol_c *type_symbol) { ccb@204: if (type_symbol == NULL) {ERROR;} ccb@204: if (typeid(*type_symbol) == typeid(string_type_name_c)) {return true;} ccb@204: if (typeid(*type_symbol) == typeid(wstring_type_name_c)) {return true;} msousa@257: // TODO literal_string ??? ccb@204: return false; ccb@204: } ccb@204: msousa@257: /* A helper function... */ msousa@257: bool visit_expression_type_c::is_ANY_SAFESTRING_type(symbol_c *type_symbol) { msousa@257: if (type_symbol == NULL) {ERROR;} msousa@257: if (typeid(*type_symbol) == typeid(safestring_type_name_c)) {return true;} msousa@257: if (typeid(*type_symbol) == typeid(safewstring_type_name_c)) {return true;} msousa@257: return false; msousa@257: } msousa@257: msousa@257: /* A helper function... */ msousa@257: bool visit_expression_type_c::is_ANY_STRING_compatible(symbol_c *type_symbol) { msousa@257: if (type_symbol == NULL) {ERROR;} msousa@257: if (is_ANY_STRING_type (type_symbol)) {return true;} msousa@257: if (is_ANY_SAFESTRING_type(type_symbol)) {return true;} msousa@257: return false; msousa@257: } ccb@204: ccb@204: /* A helper function... */ ccb@204: bool visit_expression_type_c::is_ANY_INT_type(symbol_c *type_symbol) { ccb@204: if (type_symbol == NULL) {ERROR;} ccb@204: if (typeid(*type_symbol) == typeid(sint_type_name_c)) {return true;} ccb@204: if (typeid(*type_symbol) == typeid(int_type_name_c)) {return true;} ccb@204: if (typeid(*type_symbol) == typeid(dint_type_name_c)) {return true;} ccb@204: if (typeid(*type_symbol) == typeid(lint_type_name_c)) {return true;} ccb@204: if (typeid(*type_symbol) == typeid(usint_type_name_c)) {return true;} ccb@204: if (typeid(*type_symbol) == typeid(uint_type_name_c)) {return true;} ccb@204: if (typeid(*type_symbol) == typeid(udint_type_name_c)) {return true;} ccb@204: if (typeid(*type_symbol) == typeid(ulint_type_name_c)) {return true;} ccb@204: return false; ccb@204: } ccb@204: msousa@257: /* A helper function... */ msousa@257: bool visit_expression_type_c::is_ANY_SAFEINT_type(symbol_c *type_symbol) { msousa@257: if (type_symbol == NULL) {ERROR;} msousa@257: if (typeid(*type_symbol) == typeid(safesint_type_name_c)) {return true;} msousa@257: if (typeid(*type_symbol) == typeid(safeint_type_name_c)) {return true;} msousa@257: if (typeid(*type_symbol) == typeid(safedint_type_name_c)) {return true;} msousa@257: if (typeid(*type_symbol) == typeid(safelint_type_name_c)) {return true;} msousa@257: if (typeid(*type_symbol) == typeid(safeusint_type_name_c)) {return true;} msousa@257: if (typeid(*type_symbol) == typeid(safeuint_type_name_c)) {return true;} msousa@257: if (typeid(*type_symbol) == typeid(safeudint_type_name_c)) {return true;} msousa@257: if (typeid(*type_symbol) == typeid(safeulint_type_name_c)) {return true;} msousa@257: return false; msousa@257: } msousa@257: msousa@257: /* A helper function... */ msousa@257: bool visit_expression_type_c::is_ANY_INT_compatible(symbol_c *type_symbol) { msousa@257: if (type_symbol == NULL) {ERROR;} msousa@257: if (is_ANY_INT_type (type_symbol)) {return true;} msousa@257: if (is_ANY_SAFEINT_type(type_symbol)) {return true;} msousa@257: if (is_literal_integer_type(type_symbol)) {return true;} msousa@257: return false; msousa@257: } ccb@204: ccb@204: /* A helper function... */ ccb@204: bool visit_expression_type_c::is_ANY_REAL_type(symbol_c *type_symbol) { ccb@204: if (type_symbol == NULL) {ERROR;} ccb@204: if (typeid(*type_symbol) == typeid(real_type_name_c)) {return true;} ccb@204: if (typeid(*type_symbol) == typeid(lreal_type_name_c)) {return true;} ccb@204: return false; ccb@204: } ccb@204: msousa@257: /* A helper function... */ msousa@257: bool visit_expression_type_c::is_ANY_SAFEREAL_type(symbol_c *type_symbol) { msousa@257: if (type_symbol == NULL) {ERROR;} msousa@257: if (typeid(*type_symbol) == typeid(safereal_type_name_c)) {return true;} msousa@257: if (typeid(*type_symbol) == typeid(safelreal_type_name_c)) {return true;} msousa@257: return false; msousa@257: } msousa@257: msousa@257: /* A helper function... */ msousa@257: bool visit_expression_type_c::is_ANY_REAL_compatible(symbol_c *type_symbol) { msousa@257: if (type_symbol == NULL) {ERROR;} msousa@257: if (is_ANY_REAL_type (type_symbol)) {return true;} msousa@257: if (is_ANY_SAFEREAL_type(type_symbol)) {return true;} msousa@257: if (is_literal_real_type(type_symbol)) {return true;} msousa@257: return false; msousa@257: } ccb@204: ccb@204: /* A helper function... */ ccb@204: bool visit_expression_type_c::is_ANY_BIT_type(symbol_c *type_symbol) { ccb@204: if (type_symbol == NULL) {ERROR;} msousa@257: if (typeid(*type_symbol) == typeid(bool_type_name_c)) {return true;} msousa@257: if (typeid(*type_symbol) == typeid(byte_type_name_c)) {return true;} msousa@257: if (typeid(*type_symbol) == typeid(word_type_name_c)) {return true;} msousa@257: if (typeid(*type_symbol) == typeid(dword_type_name_c)) {return true;} msousa@257: if (typeid(*type_symbol) == typeid(lword_type_name_c)) {return true;} ccb@204: return false; ccb@204: } ccb@204: msousa@257: /* A helper function... */ msousa@257: bool visit_expression_type_c::is_ANY_SAFEBIT_type(symbol_c *type_symbol) { msousa@257: if (type_symbol == NULL) {ERROR;} msousa@257: if (typeid(*type_symbol) == typeid(safebool_type_name_c)) {return true;} msousa@257: if (typeid(*type_symbol) == typeid(safebyte_type_name_c)) {return true;} msousa@257: if (typeid(*type_symbol) == typeid(safeword_type_name_c)) {return true;} msousa@257: if (typeid(*type_symbol) == typeid(safedword_type_name_c)) {return true;} msousa@257: if (typeid(*type_symbol) == typeid(safelword_type_name_c)) {return true;} msousa@257: return false; msousa@257: } msousa@257: msousa@257: /* A helper function... */ msousa@257: bool visit_expression_type_c::is_ANY_BIT_compatible(symbol_c *type_symbol) { msousa@257: if (type_symbol == NULL) {ERROR;} msousa@257: if (is_ANY_BIT_type (type_symbol)) {return true;} msousa@257: if (is_ANY_SAFEBIT_type(type_symbol)) {return true;} msousa@257: if (is_nonneg_literal_integer_type(type_symbol)) {return true;} msousa@257: if (is_literal_bool_type(type_symbol)) {return true;} msousa@257: return false; msousa@257: } ccb@204: ccb@204: /* A helper function... */ ccb@204: bool visit_expression_type_c::is_BOOL_type(symbol_c *type_symbol) { ccb@204: if (type_symbol == NULL) {ERROR;} msousa@257: if (typeid(*type_symbol) == typeid(bool_type_name_c)) {return true;} ccb@204: return false; ccb@204: } ccb@204: msousa@257: /* A helper function... */ msousa@257: bool visit_expression_type_c::is_SAFEBOOL_type(symbol_c *type_symbol){ msousa@257: if (type_symbol == NULL) {ERROR;} msousa@257: if (typeid(*type_symbol) == typeid(safebool_type_name_c)) {return true;} msousa@257: return false; msousa@257: } msousa@257: msousa@257: /* A helper function... */ msousa@257: bool visit_expression_type_c::is_ANY_BOOL_compatible(symbol_c *type_symbol) { msousa@257: if (type_symbol == NULL) {ERROR;} msousa@257: if (is_BOOL_type (type_symbol)) {return true;} msousa@257: if (is_SAFEBOOL_type(type_symbol)) {return true;} msousa@257: if (is_literal_bool_type(type_symbol)) {return true;} msousa@257: return false; msousa@257: } msousa@257: msousa@257: msousa@257: #define is_type(type_name_symbol, type_name_class) (typeid(*type_name_symbol) == typeid(type_name_class)) msousa@257: ccb@204: ccb@204: #define sizeoftype(symbol) get_sizeof_datatype_c::getsize(symbol) ccb@204: ccb@204: ccb@204: /* A helper function... */ ccb@204: bool visit_expression_type_c::is_literal_integer_type(symbol_c *type_symbol) { msousa@257: if (type_symbol == NULL) {ERROR;} msousa@257: if (typeid(*type_symbol) == typeid(neg_integer_c)) {return true;} msousa@257: return is_nonneg_literal_integer_type(type_symbol); msousa@257: } msousa@257: msousa@257: msousa@257: /* A helper function... */ msousa@257: bool visit_expression_type_c::is_nonneg_literal_integer_type(symbol_c *type_symbol) { msousa@257: if (type_symbol == NULL) {ERROR;} ccb@204: if (typeid(*type_symbol) == typeid(integer_c)) {return true;} ccb@204: if (typeid(*type_symbol) == typeid(binary_integer_c)) {return true;} ccb@204: if (typeid(*type_symbol) == typeid(octal_integer_c)) {return true;} ccb@204: if (typeid(*type_symbol) == typeid(hex_integer_c)) {return true;} ccb@204: return false; ccb@204: } ccb@204: ccb@204: ccb@204: /* A helper function... */ ccb@204: bool visit_expression_type_c::is_literal_real_type(symbol_c *type_symbol) { msousa@257: if (type_symbol == NULL) {ERROR;} msousa@257: if (typeid(*type_symbol) == typeid(real_c)) {return true;} msousa@257: if (typeid(*type_symbol) == typeid(neg_real_c)) {return true;} ccb@204: return false; ccb@204: } ccb@204: ccb@204: ccb@204: /* A helper function... */ ccb@204: bool visit_expression_type_c::is_literal_bool_type(symbol_c *type_symbol) { ccb@204: bool_type_name_c bool_t; ccb@204: msousa@257: if (type_symbol == NULL) {ERROR;} ccb@204: if (typeid(*type_symbol) == typeid(boolean_true_c)) {return true;} ccb@204: if (typeid(*type_symbol) == typeid(boolean_false_c)) {return true;} msousa@257: if (is_nonneg_literal_integer_type(type_symbol)) ccb@204: if (sizeoftype(&bool_t) >= sizeoftype(type_symbol)) {return true;} ccb@204: return false; ccb@204: } ccb@204: ccb@204: /* Determine the common data type between two data types. ccb@204: * If no common data type found, return NULL. ccb@204: * ccb@204: * If data types are identical, return the first (actually any would do...). ccb@204: * If any of the data types is a literal, we confirm that ccb@204: * the literal uses less bits than the fixed size data type. ccb@204: * e.g. BYTE and 1024 returns NULL ccb@204: * BYTE and 255 returns BYTE ccb@204: * ccb@204: * If two literals, then return the literal that requires more bits... ccb@204: */ msousa@257: ccb@204: symbol_c *visit_expression_type_c::common_type__(symbol_c *first_type, symbol_c *second_type) { ccb@204: if (first_type == NULL && second_type == NULL) {ERROR;} ccb@204: if (first_type == NULL) {return second_type;} ccb@204: if (second_type == NULL) {return first_type;} ccb@204: ccb@204: if (is_literal_integer_type(first_type) && is_literal_integer_type(second_type)) ccb@204: {return ((sizeoftype(first_type) > sizeoftype(second_type))? first_type:second_type);} ccb@204: ccb@204: if (is_literal_real_type(first_type) && is_literal_real_type(second_type)) ccb@204: {return ((sizeoftype(first_type) > sizeoftype(second_type))? first_type:second_type);} ccb@204: ccb@204: if (is_literal_bool_type(first_type) && is_literal_bool_type(second_type)) ccb@204: {return first_type;} ccb@204: msousa@257: /* The following check can only be made after the is_literal_XXXX checks */ ccb@204: /* When two literals of the same type, with identical typeid's are checked, msousa@257: * we must return the one that occupies more bits... This is done above. ccb@204: */ ccb@204: if (typeid(*first_type) == typeid(*second_type)) {return first_type;} ccb@204: msousa@257: /* NOTE Although a BOOL is also an ANY_BIT, we must check it explicitly since some msousa@257: * literal bool values are not literal integers... msousa@257: */ msousa@257: if (is_BOOL_type(first_type) && is_literal_bool_type(second_type)) {return first_type;} msousa@257: if (is_BOOL_type(second_type) && is_literal_bool_type(first_type)) {return second_type;} msousa@257: msousa@257: if (is_SAFEBOOL_type(first_type) && is_literal_bool_type(second_type)) {return first_type;} msousa@257: if (is_SAFEBOOL_type(second_type) && is_literal_bool_type(first_type)) {return second_type;} msousa@257: msousa@257: if (is_SAFEBOOL_type(first_type) && is_BOOL_type(second_type)) {return second_type;} msousa@257: if (is_SAFEBOOL_type(second_type) && is_BOOL_type(first_type)) {return first_type;} msousa@257: msousa@257: if (is_ANY_BIT_type(first_type) && is_nonneg_literal_integer_type(second_type)) ccb@204: {return ((sizeoftype(first_type) >= sizeoftype(second_type))? first_type :NULL);} msousa@257: if (is_ANY_BIT_type(second_type) && is_nonneg_literal_integer_type(first_type)) ccb@204: {return ((sizeoftype(second_type) >= sizeoftype(first_type)) ? second_type:NULL);} ccb@204: msousa@257: if (is_ANY_SAFEBIT_type(first_type) && is_nonneg_literal_integer_type(second_type)) ccb@204: {return ((sizeoftype(first_type) >= sizeoftype(second_type))? first_type :NULL);} msousa@257: if (is_ANY_SAFEBIT_type(second_type) && is_nonneg_literal_integer_type(first_type)) ccb@204: {return ((sizeoftype(second_type) >= sizeoftype(first_type)) ? second_type:NULL);} ccb@204: msousa@257: if (is_ANY_SAFEBIT_type(first_type) && is_ANY_BIT_type(second_type)) msousa@257: {return ((sizeoftype(first_type) == sizeoftype(second_type))? second_type:NULL);} msousa@257: if (is_ANY_SAFEBIT_type(second_type) && is_ANY_BIT_type(first_type)) msousa@257: {return ((sizeoftype(first_type) == sizeoftype(second_type))? first_type :NULL);} msousa@257: msousa@257: if (is_ANY_INT_type(first_type) && is_literal_integer_type(second_type)) ccb@204: {return ((sizeoftype(first_type) >= sizeoftype(second_type))? first_type :NULL);} msousa@257: if (is_ANY_INT_type(second_type) && is_literal_integer_type(first_type)) ccb@204: {return ((sizeoftype(second_type) >= sizeoftype(first_type)) ? second_type:NULL);} ccb@204: msousa@257: if (is_ANY_SAFEINT_type(first_type) && is_literal_integer_type(second_type)) msousa@257: {return ((sizeoftype(first_type) >= sizeoftype(second_type))? first_type :NULL);} msousa@257: if (is_ANY_SAFEINT_type(second_type) && is_literal_integer_type(first_type)) msousa@257: {return ((sizeoftype(second_type) >= sizeoftype(first_type)) ? second_type:NULL);} msousa@257: msousa@257: if (is_ANY_SAFEINT_type(first_type) && is_ANY_INT_type(second_type)) msousa@257: {return ((sizeoftype(first_type) == sizeoftype(second_type))? second_type:NULL);} msousa@257: if (is_ANY_SAFEINT_type(second_type) && is_ANY_INT_type(first_type)) msousa@257: {return ((sizeoftype(first_type) == sizeoftype(second_type))? first_type :NULL);} msousa@257: msousa@257: if (is_ANY_REAL_type(first_type) && is_literal_real_type(second_type)) msousa@257: {return ((sizeoftype(first_type) >= sizeoftype(second_type))? first_type :NULL);} msousa@257: if (is_ANY_REAL_type(second_type) && is_literal_real_type(first_type)) msousa@257: {return ((sizeoftype(second_type) >= sizeoftype(first_type)) ? second_type:NULL);} msousa@257: msousa@257: if (is_ANY_SAFEREAL_type(first_type) && is_literal_real_type(second_type)) msousa@257: {return ((sizeoftype(first_type) >= sizeoftype(second_type))? first_type :NULL);} msousa@257: if (is_ANY_SAFEREAL_type(second_type) && is_literal_real_type(first_type)) msousa@257: {return ((sizeoftype(second_type) >= sizeoftype(first_type)) ? second_type:NULL);} msousa@257: msousa@257: if (is_ANY_SAFEREAL_type(first_type) && is_ANY_REAL_type(second_type)) msousa@257: {return ((sizeoftype(first_type) == sizeoftype(second_type))? second_type:NULL);} msousa@257: if (is_ANY_SAFEREAL_type(second_type) && is_ANY_REAL_type(first_type)) msousa@257: {return ((sizeoftype(first_type) == sizeoftype(second_type))? first_type :NULL);} msousa@257: msousa@257: /* the Time and Date types... */ msousa@257: if (is_type(first_type, safetime_type_name_c) && is_type(second_type, time_type_name_c)) {return second_type;} msousa@257: if (is_type(second_type, safetime_type_name_c) && is_type( first_type, time_type_name_c)) {return first_type;} msousa@257: msousa@257: if (is_type(first_type, safedate_type_name_c) && is_type(second_type, date_type_name_c)) {return second_type;} msousa@257: if (is_type(second_type, safedate_type_name_c) && is_type( first_type, date_type_name_c)) {return first_type;} msousa@257: msousa@257: if (is_type(first_type, safedt_type_name_c) && is_type(second_type, dt_type_name_c)) {return second_type;} msousa@257: if (is_type(second_type, safedt_type_name_c) && is_type( first_type, dt_type_name_c)) {return first_type;} msousa@257: msousa@257: if (is_type(first_type, safetod_type_name_c) && is_type(second_type, tod_type_name_c)) {return second_type;} msousa@257: if (is_type(second_type, safetod_type_name_c) && is_type( first_type, tod_type_name_c)) {return first_type;} msousa@257: ccb@204: /* no common type */ ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: /* Determine the common data type between two data types. ccb@204: * Unlike the common_type__() function, we stop the compiler with an ERROR ccb@204: * if no common data type is found. ccb@204: */ ccb@204: symbol_c *visit_expression_type_c::common_type(symbol_c *first_type, symbol_c *second_type) { ccb@204: symbol_c *res = common_type__(first_type, second_type); ccb@204: if (NULL == res) ERROR; ccb@204: return res; ccb@204: } ccb@204: ccb@204: msousa@257: /* Return TRUE if the second (value) data type may be assigned to a variable of the first (variable) data type msousa@257: * such as: msousa@257: * var_type value_type msousa@257: * BOOL BYTE#7 -> returns false msousa@257: * INT INT#7 -> returns true msousa@257: * INT 7 -> returns true msousa@257: * REAL 7.89 -> returns true msousa@257: * REAL 7 -> returns true msousa@257: * INT 7.89 -> returns false msousa@257: * SAFEBOOL BOOL#1 -> returns false !!! msousa@257: * etc... msousa@257: * msousa@257: * NOTE: It is assumed that the var_type is the data type of an lvalue msousa@257: */ msousa@257: bool visit_expression_type_c::is_valid_assignment(symbol_c *var_type, symbol_c *value_type) { msousa@257: if (var_type == NULL) {/* STAGE3_ERROR(value_type, value_type, "Var_type == NULL"); */ ERROR;} msousa@257: if (value_type == NULL) {/* STAGE3_ERROR(var_type, var_type, "Value_type == NULL"); */ ERROR;} msousa@257: if (var_type == NULL || value_type == NULL) {ERROR;} msousa@257: msousa@257: symbol_c *common_type = common_type__(var_type, value_type); msousa@257: if (NULL == common_type) msousa@257: return false; msousa@257: return (typeid(*var_type) == typeid(*common_type)); msousa@257: } msousa@257: msousa@257: ccb@204: /* Return TRUE if there is a common data type, otherwise return FALSE msousa@257: * i.e., return TRUE if both data types may be used simultaneously in an expression msousa@257: * such as: msousa@257: * BOOL#0 AND BYTE#7 -> returns false msousa@257: * 0 AND BYTE#7 -> returns true msousa@257: * INT#10 AND INT#7 -> returns true msousa@257: * INT#10 AND 7 -> returns true msousa@257: * REAL#34.3 AND 7.89 -> returns true msousa@257: * REAL#34.3 AND 7 -> returns true msousa@257: * INT#10 AND 7.89 -> returns false msousa@257: * SAFEBOOL#0 AND BOOL#1 -> returns true !!! msousa@257: * etc... ccb@204: */ ccb@204: bool visit_expression_type_c::is_compatible_type(symbol_c *first_type, symbol_c *second_type) { ccb@204: if (first_type == NULL || second_type == NULL) {ERROR;} ccb@204: return (NULL != common_type__(first_type, second_type)); ccb@204: } ccb@204: ccb@204: ccb@204: msousa@257: #define is_num_type is_ANY_NUM_compatible msousa@257: #define is_integer_type is_ANY_INT_compatible msousa@257: #define is_real_type is_ANY_REAL_compatible msousa@257: #define is_binary_type is_ANY_BIT_compatible ccb@204: /* actually the ROR, ROL, SHL, and SHR function also accept boolean type! */ msousa@257: #define is_nbinary_type is_ANY_BIT_compatible ccb@204: #define compute_standard_function_default visit_expression_type_c::compute_standard_function_default ccb@204: #define compute_standard_function_il visit_expression_type_c::compute_standard_function_il ccb@204: #define search_expression_type_c visit_expression_type_c ccb@204: #define search(x) search_f(x) ccb@204: #define next() next_nf() ccb@204: // #define search_constant_type_c::constant_int_type_name search_expression_type_c::integer ccb@204: #define constant_int_type_name integer ccb@204: #define is_same_type is_compatible_type ccb@204: #include "../absyntax_utils/search_type_code.c" ccb@204: #undef is_same_type ccb@204: #undef constant_int_type_name ccb@204: // #undef search_constant_type_c::constant_int_type_name ccb@204: #undef next ccb@204: #undef search ccb@204: #undef compute_standard_function_default ccb@204: #undef compute_standard_function_il ccb@204: #undef search_expression_type_c ccb@204: #undef is_real_type ccb@204: #undef is_binary_type ccb@204: #undef is_nbinary_type ccb@204: #undef is_integer_type ccb@204: #undef is_num_type ccb@204: ccb@204: ccb@204: ccb@204: ccb@204: ccb@204: ccb@204: /* A helper function... */ msousa@257: /* ccb@204: symbol_c *visit_expression_type_c::compute_boolean_expression(symbol_c *left_type, symbol_c *right_type, ccb@204: is_data_type_t is_data_type) { msousa@257: */ msousa@257: symbol_c *visit_expression_type_c::compute_expression(symbol_c *left_type, symbol_c *right_type, msousa@257: is_data_type_t is_data_type) { ccb@204: bool error = false; ccb@204: ccb@204: if (!(this->*is_data_type)(left_type)) { msousa@257: STAGE3_ERROR(left_type, left_type, "Invalid data type of left operand."); ccb@204: error = true; ccb@204: } ccb@204: if (!(this->*is_data_type)(right_type)) { msousa@257: STAGE3_ERROR(right_type, right_type, "Invalid data type of right operand."); ccb@204: error = true; ccb@204: } ccb@204: if (!is_compatible_type(left_type, right_type)) { msousa@257: STAGE3_ERROR(left_type, right_type, "Type mismatch between operands."); ccb@204: error = true; ccb@204: } ccb@204: ccb@204: if (error) ccb@204: return NULL; ccb@204: else ccb@204: return common_type(left_type, right_type); ccb@204: } ccb@204: ccb@204: msousa@257: # if 0 ccb@204: /* A helper function... */ ccb@204: symbol_c *visit_expression_type_c::compute_numeric_expression(symbol_c *left_type, symbol_c *right_type, ccb@204: is_data_type_t is_data_type) { msousa@257: bool error = false; msousa@257: msousa@257: if (!(this->*is_data_type)(left_type)) { msousa@257: STAGE3_ERROR(left_type, right_type, "Invalid data type of left operand."); msousa@257: error = true; msousa@257: } msousa@257: if (!(this->*is_data_type)(right_type)) { msousa@257: STAGE3_ERROR(left_type, right_type, "Invalid data type of right operand."); msousa@257: error = true; msousa@257: } msousa@257: if (!is_compatible_type(left_type, right_type)) { msousa@257: STAGE3_ERROR(left_type, right_type, "Type mismatch between operands."); msousa@257: error = true; msousa@257: } msousa@257: msousa@257: /* ccb@204: if (is_literal_integer_type(left_type) || is_literal_real_type(left_type)) { ccb@204: return right_type; ccb@204: } else { ccb@204: return left_type; ccb@204: } msousa@257: */ msousa@257: msousa@257: if (error) msousa@257: return NULL; msousa@257: else msousa@257: return common_type(left_type, right_type); ccb@204: ccb@204: /* humour the compiler... */ msousa@257: /* msousa@257: return NULL; msousa@257: */ msousa@257: } msousa@257: #endif ccb@204: ccb@204: ccb@204: ccb@204: ccb@204: ccb@204: /* A helper function... */ ccb@204: /* check the semantics of a FB or Function non-formal call */ ccb@204: /* e.g. foo(1, 2, 3, 4); */ ccb@204: void visit_expression_type_c::check_nonformal_call(symbol_c *f_call, symbol_c *f_decl, bool use_il_defvar) { ccb@204: symbol_c *call_param_value, *call_param_type, *param_type; ccb@204: identifier_c *param_name; ccb@204: function_param_iterator_c fp_iterator(f_decl); ccb@204: function_call_param_iterator_c fcp_iterator(f_call); ccb@204: ccb@204: /* if use_il_defvar, then the first parameter for the call comes from the il_default_variable */ ccb@204: if (use_il_defvar) { ccb@204: /* The first parameter of the function corresponds to the il_default_variable_type of the function call */ ccb@204: do { ccb@204: param_name = fp_iterator.next(); ccb@204: if(param_name == NULL) break; ccb@204: /* The EN and ENO parameters are default parameters. ccb@204: * In the non-formal invocation of a function there can be no assignment of ccb@204: * values to these parameters. Therefore, we ignore the parameters declared ccb@204: * in the function. ccb@204: */ ccb@204: } while ((strcmp(param_name->value, "EN") == 0) || (strcmp(param_name->value, "ENO") == 0)); ccb@204: /* If the function does not have any parameters (param_name == NULL) ccb@204: * then we cannot compare its type with the il_default_variable_type. ccb@204: */ ccb@204: if(param_name != NULL) { ccb@204: param_type = fp_iterator.param_type(); msousa@257: if(!is_valid_assignment(param_type, il_default_variable_type)) ccb@204: STAGE3_ERROR(f_call, f_call, "In function/FB call, first parameter has invalid data type."); ccb@204: } ccb@204: } // if (use_il_defvar) ccb@204: ccb@204: /* Iterating through the non-formal parameters of the function call */ ccb@204: while((call_param_value = fcp_iterator.next_nf()) != NULL) { ccb@204: /* Obtaining the type of the current parameter in the function call */ ccb@204: call_param_type = base_type((symbol_c*)call_param_value->accept(*this)); ccb@204: if (call_param_type == NULL) STAGE3_ERROR(call_param_value, call_param_value, "Could not determine data type of value being passed in function/FB call.");; ccb@204: ccb@204: /* Iterate to the next parameter of the function being called. ccb@204: * Get the name of that parameter, and ignore if EN or ENO. ccb@204: */ ccb@204: do { ccb@204: param_name = fp_iterator.next(); ccb@204: /* If there is no parameter declared with that name */ ccb@204: if(param_name == NULL) {STAGE3_ERROR(f_call, f_call, "Too many parameters in function/FB call."); break;} ccb@204: } while ((strcmp(param_name->value, "EN") == 0) || (strcmp(param_name->value, "ENO") == 0)); ccb@204: ccb@204: if(param_name != NULL) { ccb@204: /* Get the parameter type */ ccb@204: param_type = fp_iterator.param_type(); ccb@204: /* If the declared parameter and the parameter from the function call do no have the same type */ msousa@257: if(!is_valid_assignment(param_type, call_param_type)) STAGE3_ERROR(call_param_value, call_param_value, "Type mismatch in function/FB call parameter."); ccb@204: } ccb@204: } ccb@204: } ccb@204: ccb@204: void visit_expression_type_c::compute_input_operatores(symbol_c *symbol, const char *input_operator){ ccb@204: symbol_c *call_param_type; ccb@204: symbol_c *fb_decl = il_operand_type; ccb@204: /* The following should never occur. The function block must be defined, ccb@204: * and the FB type being called MUST be in the symtable... ccb@204: * This was all already checked at stage 2! ccb@204: */ ccb@204: if (NULL == fb_decl){ ccb@204: STAGE3_ERROR(symbol, symbol, "Parameter operator needs an instance of a function block operand."); ccb@204: ERROR; ccb@204: } ccb@204: ccb@204: /* Iterating through the formal parameters of the function call */ ccb@204: identifier_c call_param_name(input_operator); ccb@204: ccb@204: /* Obtaining the type of the value being passed in the function call */ ccb@204: call_param_type = il_default_variable_type; ccb@204: if (call_param_type == NULL) { ccb@204: STAGE3_ERROR(&call_param_name, &call_param_name, "Could not determine data type of value being passed in function/FB call."); ccb@204: /* The data value being passed is possibly any enumerated type value. ccb@204: * We do not yet handle semantic verification of enumerated types. ccb@204: */ ccb@204: ERROR; ccb@204: } ccb@204: call_param_type = base_type(call_param_type); ccb@204: if (call_param_type == NULL) STAGE3_ERROR(&call_param_name, &call_param_name, "Could not determine data type of value being passed in function/FB call."); ccb@204: ccb@204: ccb@204: check_formal_parameter(&call_param_name, call_param_type, fb_decl); ccb@204: // return NULL; ccb@204: } ccb@204: ccb@204: void visit_expression_type_c::check_formal_parameter(symbol_c *call_param_name, symbol_c *call_param_type, symbol_c *f_decl) { ccb@204: symbol_c *param_type; ccb@204: identifier_c *param_name; ccb@204: function_param_iterator_c fp_iterator(f_decl); ccb@204: ccb@204: /* Find the corresponding parameter of the function being called */ ccb@204: param_name = fp_iterator.search(call_param_name); ccb@204: if(param_name == NULL) { ccb@204: STAGE3_ERROR(call_param_name, call_param_name, "Invalid parameter in function/FB call."); ccb@204: } else { ccb@204: /* Get the parameter type */ ccb@204: param_type = fp_iterator.param_type(); ccb@204: /* If the declared parameter and the parameter from the function call have the same type */ msousa@257: if(!is_valid_assignment(param_type, call_param_type)) STAGE3_ERROR(call_param_name, call_param_name, "Type mismatch function/FB call parameter."); ccb@204: } ccb@204: } ccb@204: ccb@204: ccb@204: /* A helper function... */ ccb@204: /* check the semantics of a FB or Function formal call */ ccb@204: /* e.g. foo(IN1 := 1, OUT1 =>x, EN := true); */ ccb@204: void visit_expression_type_c::check_formal_call(symbol_c *f_call, symbol_c *f_decl) { ccb@204: symbol_c *call_param_value, *call_param_type, *call_param_name, *param_type; ccb@204: symbol_c *verify_duplicate_param; ccb@204: identifier_c *param_name; ccb@204: function_param_iterator_c fp_iterator(f_decl); ccb@204: function_call_param_iterator_c fcp_iterator(f_call); ccb@204: ccb@204: /* Iterating through the formal parameters of the function call */ ccb@204: while((call_param_name = fcp_iterator.next_f()) != NULL) { ccb@204: ccb@204: /* Obtaining the value being passed in the function call */ ccb@204: call_param_value = fcp_iterator.get_current_value(); ccb@204: /* the following should never occur. If it does, then we have a bug in our code... */ ccb@204: if (NULL == call_param_value) ERROR; ccb@204: ccb@204: /* Checking if there are duplicated parameter values */ ccb@204: verify_duplicate_param = fcp_iterator.search_f(call_param_name); ccb@204: if(verify_duplicate_param != call_param_value){ ccb@204: STAGE3_ERROR(call_param_name, verify_duplicate_param, "Duplicated parameter values."); ccb@204: } ccb@204: ccb@204: /* Obtaining the type of the value being passed in the function call */ ccb@204: call_param_type = (symbol_c*)call_param_value->accept(*this); ccb@204: if (call_param_type == NULL) { ccb@204: STAGE3_ERROR(call_param_name, call_param_value, "Could not determine data type of value being passed in function/FB call."); ccb@204: /* The data value being passed is possibly any enumerated type value. ccb@204: * We do not yet handle semantic verification of enumerated types. ccb@204: */ ccb@204: ERROR; ccb@204: } ccb@204: call_param_type = base_type(call_param_type); ccb@204: if (call_param_type == NULL) STAGE3_ERROR(call_param_name, call_param_value, "Could not determine data type of value being passed in function/FB call."); ccb@204: ccb@204: /* Find the corresponding parameter of the function being called */ ccb@204: param_name = fp_iterator.search(call_param_name); ccb@204: if(param_name == NULL) { ccb@204: STAGE3_ERROR(call_param_name, call_param_name, "Invalid parameter in function/FB call."); ccb@204: } else { ccb@204: /* Get the parameter type */ ccb@204: param_type = fp_iterator.param_type(); ccb@204: /* If the declared parameter and the parameter from the function call have the same type */ msousa@257: if(!is_valid_assignment(param_type, call_param_type)) STAGE3_ERROR(call_param_name, call_param_value, "Type mismatch function/FB call parameter."); ccb@204: } ccb@204: } ccb@204: } ccb@204: ccb@204: ccb@204: ccb@204: ccb@204: /* a helper function... */ ccb@204: symbol_c *visit_expression_type_c::base_type(symbol_c *symbol) { ccb@204: return (symbol_c *)symbol->accept(search_base_type); ccb@204: } ccb@204: ccb@204: ccb@204: /* a helper function... */ ccb@204: void *visit_expression_type_c::verify_null(symbol_c *symbol){ ccb@204: if(il_default_variable_type == NULL){ ccb@204: STAGE3_ERROR(symbol, symbol, "Il default variable can't be NULL."); ccb@204: } ccb@204: if(il_operand_type == NULL){ ccb@204: STAGE3_ERROR(symbol, symbol, "function requires an operand."); ccb@204: } ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: ccb@204: ccb@204: ccb@204: /*********************/ ccb@204: /* B 1.4 - Variables */ ccb@204: /*********************/ ccb@204: ccb@204: void *visit_expression_type_c::visit(symbolic_variable_c *symbol) { ccb@204: return search_varfb_instance_type->get_type(symbol); ccb@204: } ccb@204: ccb@204: /********************************************/ ccb@204: /* B 1.4.1 - Directly Represented Variables */ ccb@204: /********************************************/ ccb@204: void *visit_expression_type_c::visit(direct_variable_c *symbol) { ccb@204: switch (symbol->value[2]) { ccb@204: case 'X': // bit - 1 bit ccb@204: return (void *)&bool_type_name; ccb@204: case 'B': // byte - 8 bits ccb@204: return (void *)&byte_type_name; ccb@204: case 'W': // word - 16 bits ccb@204: return (void *)&word_type_name; ccb@204: case 'D': // double word - 32 bits ccb@204: return (void *)&dword_type_name; ccb@204: case 'L': // long word - 64 bits ccb@204: return (void *)&lword_type_name; ccb@204: default: // if none of the above, then the empty string was used <=> boolean ccb@204: return (void *)&bool_type_name; ccb@204: } ccb@204: } ccb@204: ccb@204: /*************************************/ ccb@204: /* B 1.4.2 - Multi-element variables */ ccb@204: /*************************************/ ccb@204: void *visit_expression_type_c::visit(array_variable_c *symbol) { ccb@204: return search_varfb_instance_type->get_type(symbol); ccb@204: } ccb@204: ccb@204: void *visit_expression_type_c::visit(structured_variable_c *symbol) { ccb@204: return search_varfb_instance_type->get_type(symbol); ccb@204: } ccb@204: ccb@204: ccb@204: ccb@204: /****************************************/ ccb@204: /* B.2 - Language IL (Instruction List) */ ccb@204: /****************************************/ ccb@204: /***********************************/ ccb@204: /* B 2.1 Instructions and Operands */ ccb@204: /***********************************/ ccb@204: /*| instruction_list il_instruction */ ccb@204: /* The visitor of the base class search_visitor_c will handle calling each instruction in the list. ccb@204: * We do not need to do anything here... ccb@204: */ ccb@204: // void *visit_expression_type_c::visit(instruction_list_c *symbol) ccb@204: ccb@204: /* | label ':' [il_incomplete_instruction] eol_list */ ccb@204: //SYM_REF2(il_instruction_c, label, il_instruction) ccb@204: // void *visit_expression_type_c::visit(il_instruction_c *symbol); ccb@204: ccb@204: ccb@204: /* | il_simple_operator [il_operand] */ ccb@204: // SYM_REF2(il_simple_operation_c, il_simple_operator, il_operand) ccb@204: void *visit_expression_type_c::visit(il_simple_operation_c *symbol) { ccb@204: if (il_error) ccb@204: return NULL; ccb@204: ccb@204: /* determine the data type of the operand */ ccb@204: if (symbol->il_operand != NULL){ ccb@204: il_operand_type = base_type((symbol_c *)symbol->il_operand->accept(*this)); ccb@204: } else { ccb@204: il_operand_type = NULL; ccb@204: } ccb@204: /* recursive call to see whether data types are compatible */ ccb@204: symbol->il_simple_operator->accept(*this); ccb@204: ccb@204: il_operand_type = NULL; ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: // | function_name [il_operand_list] */ ccb@204: //SYM_REF2(il_function_call_c, function_name, il_operand_list) ccb@204: void *visit_expression_type_c::visit(il_function_call_c *symbol) { ccb@204: if (il_error) ccb@204: return NULL; ccb@204: ccb@204: /* First find the declaration of the function being called! */ ccb@204: function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name); ccb@204: ccb@204: symbol_c *return_data_type = NULL; ccb@204: ccb@204: if (f_decl == function_symtable.end_value()) { ccb@204: function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name); ccb@204: if (current_function_type == function_none) ERROR; ccb@204: /* This code is for the functions that the user did not declare and that are ccb@204: * part of the IL or ST languagem (built-in functions). ccb@204: * For now we won't do the semantics analysis for that kind of functions. ccb@204: */ ccb@204: /* ccb@204: return_data_type = (symbol_c *)search_expression_type->compute_standard_function_default(NULL, symbol); ccb@204: if (NULL == return_data_type) ERROR; ccb@204: ccb@204: function_call_param_iterator_c fcp_iterator(symbol); ccb@204: ccb@204: int nb_param = 0; ccb@204: if (symbol->il_param_list != NULL) ccb@204: nb_param += ((list_c *)symbol->il_param_list)->n; ccb@204: ccb@204: identifier_c en_param_name("EN");*/ ccb@204: /* Get the value from EN param */ ccb@204: /*symbol_c *EN_param_value = fcp_iterator.search(&en_param_name); ccb@204: if (EN_param_value == NULL) ccb@204: EN_param_value = (symbol_c*)(new boolean_literal_c((symbol_c*)(new bool_type_name_c()), new boolean_true_c())); ccb@204: else ccb@204: nb_param --; ccb@204: ADD_PARAM_LIST(EN_param_value, (symbol_c*)(new bool_type_name_c()), function_param_iterator_c::direction_in) ccb@204: ccb@204: identifier_c eno_param_name("EN0");*/ ccb@204: /* Get the value from ENO param */ ccb@204: /*symbol_c *ENO_param_value = fcp_iterator.search(&eno_param_name); ccb@204: if (ENO_param_value != NULL) ccb@204: nb_param --; ccb@204: ADD_PARAM_LIST(ENO_param_value, (symbol_c*)(new bool_type_name_c()), function_param_iterator_c::direction_out) ccb@204: ccb@204: #include "st_code_gen.c" ccb@204: */ ccb@204: } else { ccb@204: /* determine the base data type returned by the function being called... */ ccb@204: return_data_type = base_type(f_decl->type_name); ccb@204: /* If the following occurs, then we must have some big bug in the syntax parser (stage 2)... */ ccb@204: if (NULL == return_data_type) ERROR; ccb@204: ccb@204: /* check semantics of data passed in the function call... */ ccb@204: check_nonformal_call(symbol, f_decl, true); ccb@204: ccb@204: /* set the new ddata type of the default variable for the following verifications... */ ccb@204: il_default_variable_type = return_data_type; ccb@204: } ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: ccb@204: /* | il_expr_operator '(' [il_operand] eol_list [simple_instr_list] ')' */ ccb@204: // SYM_REF3(il_expression_c, il_expr_operator, il_operand, simple_instr_list); ccb@204: void *visit_expression_type_c::visit(il_expression_c *symbol) { ccb@204: if (il_error) ccb@204: return NULL; ccb@204: ccb@204: symbol_c *il_default_variable_type_back = il_default_variable_type; ccb@204: ccb@204: il_parenthesis_level++; ccb@204: ccb@204: if(symbol->il_operand != NULL) { ccb@204: il_default_variable_type = base_type((symbol_c *)symbol->il_operand->accept(*this)); ccb@204: } else { ccb@204: il_default_variable_type = NULL; ccb@204: } ccb@204: ccb@204: if(symbol->simple_instr_list != NULL) { ccb@204: symbol->simple_instr_list->accept(*this); ccb@204: } ccb@204: ccb@204: il_parenthesis_level--; ccb@204: if (il_parenthesis_level < 0) ERROR; ccb@204: ccb@204: il_operand_type = il_default_variable_type; ccb@204: il_default_variable_type = il_default_variable_type_back; ccb@204: ccb@204: /* Now check the if the data type semantics of operation are correct, ccb@204: * but only if no previous error has been found... ccb@204: */ ccb@204: if (il_error) ccb@204: return NULL; ccb@204: symbol->il_expr_operator->accept(*this); ccb@204: ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: ccb@204: #if 0 ccb@204: /* il_jump_operator label */ ccb@204: SYM_REF2(il_jump_operation_c, il_jump_operator, label) ccb@204: void *visit_expression_type_c::visit(il_jump_operation_c *symbol); ccb@204: #endif ccb@204: ccb@204: ccb@204: /* il_call_operator prev_declared_fb_name ccb@204: * | il_call_operator prev_declared_fb_name '(' ')' ccb@204: * | il_call_operator prev_declared_fb_name '(' eol_list ')' ccb@204: * | il_call_operator prev_declared_fb_name '(' il_operand_list ')' ccb@204: * | il_call_operator prev_declared_fb_name '(' eol_list il_param_list ')' ccb@204: */ ccb@204: /* SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list) */ ccb@204: void *visit_expression_type_c::visit(il_fb_call_c *symbol) { ccb@204: if (il_error) ccb@204: return NULL; ccb@204: ccb@204: /* first check whether the il_default_variable is of the correct type ccb@204: * for the CAL / CALC / CALCN operator being used... ccb@204: */ ccb@204: symbol->il_call_operator->accept(*this); ccb@204: ccb@204: /* Now check the FB call itself... */ ccb@204: ccb@204: /* First we find the declaration of the FB type of the FB instance being called... */ ccb@204: /* e.g. Function_block foo_fb_type ccb@204: * ... ccb@204: * End_Function_Block ccb@204: * ccb@204: * Program test ccb@204: * var fb1 : foo_fb_type; end_var ccb@204: * fb1(...) ccb@204: * End_Program ccb@204: * ccb@204: * search_varfb_instance_type->get_type( identifier_c("fb1") ) ccb@204: * in the scope of Program 'test' ccb@204: * will return the fb declaration of foo_fb_type !! ccb@204: */ ccb@204: #if 0 ccb@204: symbol_c *fb_decl_symbol = search_varfb_instance_type->get_type(symbol->fb_name); ccb@204: /* The following should never occur. The function block must be defined, ccb@204: * and the FB type being called MUST be in the symtable... ccb@204: * This was all already checked at stage 2! ccb@204: */ ccb@204: if (NULL == fb_decl_symbol) ERROR; ccb@204: ccb@204: function_block_declaration_c *fb_decl = dynamic_cast(fb_decl_symbol); ccb@204: /* should never occur. ... */ ccb@204: if (NULL == fb_decl) ERROR; ccb@204: #endif ccb@204: symbol_c *fb_decl = search_varfb_instance_type->get_type(symbol->fb_name); ccb@204: /* The following should never occur. The function block must be defined, ccb@204: * and the FB type being called MUST be in the symtable... ccb@204: * This was all already checked at stage 2! ccb@204: */ ccb@204: if (NULL == fb_decl) ERROR; ccb@204: ccb@204: /* now check the semantics of the fb call... */ ccb@204: /* If the syntax parser is working correctly, exactly one of the ccb@204: * following two symbols will be NULL, while the other is != NULL. ccb@204: */ ccb@204: if (NULL != symbol->il_operand_list) check_nonformal_call(symbol, fb_decl); ccb@204: if (NULL != symbol->il_param_list) check_formal_call (symbol, fb_decl); ccb@204: ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: ccb@204: ccb@204: /* | function_name '(' eol_list [il_param_list] ')' */ ccb@204: /* SYM_REF2(il_formal_funct_call_c, function_name, il_param_list) */ ccb@204: void *visit_expression_type_c::visit(il_formal_funct_call_c *symbol) { ccb@204: if (il_error) ccb@204: return NULL; ccb@204: ccb@204: function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name); ccb@204: ccb@204: symbol_c *return_data_type = NULL; ccb@204: ccb@204: if (f_decl == function_symtable.end_value()) { ccb@204: function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name); ccb@204: if (current_function_type == function_none) ERROR; ccb@204: ccb@204: /* This code is for the functions that the user did not declare and that are ccb@204: * part of the IL or ST languagem (built-in functions). ccb@204: * For now we won't do the semantics analysis for that kind of functions. ccb@204: */ ccb@204: #if 0 ccb@204: return_data_type = (symbol_c *)search_expression_type->compute_standard_function_default(NULL, symbol); ccb@204: if (NULL == return_data_type) ERROR; ccb@204: ccb@204: function_call_param_iterator_c fcp_iterator(symbol); ccb@204: ccb@204: int nb_param = 0; ccb@204: if (symbol->il_param_list != NULL) ccb@204: nb_param += ((list_c *)symbol->il_param_list)->n; ccb@204: ccb@204: identifier_c en_param_name("EN"); ccb@204: /* Get the value from EN param */ ccb@204: symbol_c *EN_param_value = fcp_iterator.search(&en_param_name); ccb@204: if (EN_param_value == NULL) ccb@204: EN_param_value = (symbol_c*)(new boolean_literal_c((symbol_c*)(new bool_type_name_c()), new boolean_true_c())); ccb@204: else ccb@204: nb_param --; ccb@204: ADD_PARAM_LIST(EN_param_value, (symbol_c*)(new bool_type_name_c()), function_param_iterator_c::direction_in) ccb@204: ccb@204: identifier_c eno_param_name("EN0"); ccb@204: /* Get the value from ENO param */ ccb@204: symbol_c *ENO_param_value = fcp_iterator.search(&eno_param_name); ccb@204: if (ENO_param_value != NULL) ccb@204: nb_param --; ccb@204: ADD_PARAM_LIST(ENO_param_value, (symbol_c*)(new bool_type_name_c()), function_param_iterator_c::direction_out) ccb@204: ccb@204: #include "st_code_gen.c" ccb@204: #endif ccb@204: } else { ccb@204: /* determine the base data type returned by the function being called... */ ccb@204: return_data_type = base_type(f_decl->type_name); ccb@204: /* the following should never occur. If it does, then we have a bug in the syntax parser (stage 2)... */ ccb@204: if (NULL == return_data_type) ERROR; ccb@204: ccb@204: /* check semantics of data passed in the function call... */ ccb@204: check_formal_call(symbol, f_decl); ccb@204: ccb@204: /* the data type of the data returned by the function, and stored in the il default variable... */ ccb@204: il_default_variable_type = return_data_type; ccb@204: } ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: ccb@204: #if 0 ccb@204: /* | il_operand_list ',' il_operand */ ccb@204: SYM_LIST(il_operand_list_c) ccb@204: void *visit_expression_type_c::visit(il_operand_list_c *symbol); ccb@204: ccb@204: /* | simple_instr_list il_simple_instruction */ ccb@204: SYM_LIST(simple_instr_list_c) ccb@204: void *visit_expression_type_c::visit(simple_instr_list_c *symbol); ccb@204: ccb@204: /* | il_initial_param_list il_param_instruction */ ccb@204: SYM_LIST(il_param_list_c) ccb@204: void *visit_expression_type_c::visit(il_param_list_c *symbol); ccb@204: ccb@204: /* il_assign_operator il_operand ccb@204: * | il_assign_operator '(' eol_list simple_instr_list ')' ccb@204: */ ccb@204: SYM_REF3(il_param_assignment_c, il_assign_operator, il_operand, simple_instr_list) ccb@204: void *visit_expression_type_c::visit(il_param_assignment_c *symbol); ccb@204: /* il_assign_out_operator variable */ ccb@204: SYM_REF2(il_param_out_assignment_c, il_assign_out_operator, variable) ccb@204: void *visit_expression_type_c::visit(il_param_out_assignment_c *symbol); ccb@204: ccb@204: #endif ccb@204: ccb@204: ccb@204: /*******************/ ccb@204: /* B 2.2 Operators */ ccb@204: /*******************/ ccb@204: ccb@204: //SYM_REF0(LD_operator_c) ccb@204: void *visit_expression_type_c::visit(LD_operator_c *symbol) { ccb@204: if (0 == il_parenthesis_level) ccb@204: il_error = false; ccb@204: ccb@204: if(il_operand_type == NULL) ccb@204: STAGE3_ERROR(symbol, symbol, "LD operator requires an operand."); ccb@204: il_default_variable_type = il_operand_type; ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: // SYM_REF0(LDN_operator_c) ccb@204: void *visit_expression_type_c::visit(LDN_operator_c *symbol) { ccb@204: if(il_operand_type == NULL) ccb@204: STAGE3_ERROR(symbol, symbol, "LDN operator requires an operand."); msousa@257: if(!is_ANY_BIT_compatible(il_operand_type)) ccb@204: STAGE3_ERROR(symbol, symbol, "invalid data type of LDN operand, should be of type ANY_BIT."); ccb@204: il_default_variable_type = il_operand_type; ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: // SYM_REF0(ST_operator_c) ccb@204: void *visit_expression_type_c::visit(ST_operator_c *symbol) { ccb@204: verify_null(symbol); msousa@257: msousa@257: if(!is_valid_assignment(il_operand_type, il_default_variable_type)) ccb@204: STAGE3_ERROR(symbol, symbol, "Type mismatch in ST operation."); ccb@204: /* TODO: check whether il_operand_type is an LVALUE !! */ ccb@204: /* data type of il_default_variable_type is unchanged... */ ccb@204: // il_default_variable_type = il_default_variable_type; ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: // SYM_REF0(STN_operator_c) ccb@204: void *visit_expression_type_c::visit(STN_operator_c *symbol) { ccb@204: verify_null(symbol); msousa@257: if(!is_valid_assignment(il_operand_type, il_default_variable_type)) ccb@204: STAGE3_ERROR(symbol, symbol, "Type mismatch in ST operation."); ccb@204: /* TODO: check whether il_operand_type is an LVALUE !! */ msousa@257: if(!is_ANY_BIT_compatible(il_default_variable_type)) ccb@204: STAGE3_ERROR(symbol, symbol, "invalid data type of il_default_variable for STN operand, should be of type ANY_BIT."); msousa@257: if(!is_ANY_BIT_compatible(il_operand_type)) ccb@204: STAGE3_ERROR(symbol, symbol, "invalid data type of STN operand, should be of type ANY_BIT."); ccb@204: /* data type of il_default_variable_type is unchanged... */ ccb@204: // il_default_variable_type = il_default_variable_type; ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: //SYM_REF0(NOT_operator_c) ccb@204: void *visit_expression_type_c::visit(NOT_operator_c *symbol) { ccb@204: if(il_operand_type != NULL){ ccb@204: STAGE3_ERROR(symbol, symbol, "NOT operator may not have an operand."); ccb@204: return NULL; ccb@204: } ccb@204: if(il_default_variable_type == NULL) { ccb@204: STAGE3_ERROR(symbol, symbol, "Il default variable should not be NULL."); ccb@204: return NULL; ccb@204: } msousa@257: if(!is_ANY_BIT_compatible(il_default_variable_type)) { ccb@204: STAGE3_ERROR(symbol, symbol, "Il default variable should be of type ANY_BIT."); ccb@204: return NULL; ccb@204: } ccb@204: /* data type of il_default_variable_type is unchanged... */ ccb@204: // il_default_variable_type = il_default_variable_type; ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: // SYM_REF0(S_operator_c) ccb@204: void *visit_expression_type_c::visit(S_operator_c *symbol) { ccb@204: verify_null(symbol); ccb@204: if (!is_BOOL_type(il_default_variable_type)) {STAGE3_ERROR(symbol, symbol, "IL default variable should be BOOL type.");} ccb@204: if (!is_BOOL_type(il_operand_type)) {STAGE3_ERROR(symbol, symbol, "operator S requires operand of type BOOL.");} ccb@204: /* TODO: check whether il_operand_type is an LVALUE !! */ ccb@204: /* data type of il_default_variable_type is unchanged... */ ccb@204: // il_default_variable_type = il_default_variable_type; ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: // SYM_REF0(R_operator_c) ccb@204: void *visit_expression_type_c::visit(R_operator_c *symbol) { ccb@204: verify_null(symbol); ccb@204: if (!is_BOOL_type(il_default_variable_type)) {STAGE3_ERROR(symbol, symbol, "IL default variable should be BOOL type.");} ccb@204: if (!is_BOOL_type(il_operand_type)) {STAGE3_ERROR(symbol, symbol, "operator R requires operand of type BOOL.");} ccb@204: /* TODO: check whether il_operand_type is an LVALUE !! */ ccb@204: /* data type of il_default_variable_type is unchanged... */ ccb@204: // il_default_variable_type = il_default_variable_type; ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: ccb@204: // SYM_REF0(S1_operator_c) ccb@204: void *visit_expression_type_c::visit(S1_operator_c *symbol){ ccb@204: compute_input_operatores(symbol, "S1"); ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: // SYM_REF0(R1_operator_c) ccb@204: void *visit_expression_type_c::visit(R1_operator_c *symbol) { ccb@204: compute_input_operatores(symbol, "R1"); ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: // SYM_REF0(CLK_operator_c) ccb@204: void *visit_expression_type_c::visit(CLK_operator_c *symbol) { ccb@204: compute_input_operatores(symbol, "CLK"); ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: // SYM_REF0(CU_operator_c) ccb@204: void *visit_expression_type_c::visit(CU_operator_c *symbol) { ccb@204: compute_input_operatores(symbol, "CU"); ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: // SYM_REF0(CD_operator_c) ccb@204: void *visit_expression_type_c::visit(CD_operator_c *symbol) { ccb@204: compute_input_operatores(symbol, "CD"); ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: // SYM_REF0(PV_operator_c) ccb@204: void *visit_expression_type_c::visit(PV_operator_c *symbol) { ccb@204: compute_input_operatores(symbol, "PV"); ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: // SYM_REF0(IN_operator_c) ccb@204: void *visit_expression_type_c::visit(IN_operator_c *symbol) { ccb@204: compute_input_operatores(symbol, "IN"); ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: // SYM_REF0(PT_operator_c) ccb@204: void *visit_expression_type_c::visit(PT_operator_c *symbol) { ccb@204: compute_input_operatores(symbol, "PT"); ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: //SYM_REF0(AND_operator_c) ccb@204: void *visit_expression_type_c::visit(AND_operator_c *symbol) { ccb@204: verify_null(symbol); msousa@257: il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_BIT_compatible); ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: //SYM_REF0(OR_operator_c) ccb@204: void *visit_expression_type_c::visit(OR_operator_c *symbol) { ccb@204: verify_null(symbol); msousa@257: il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_BIT_compatible); ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: //SYM_REF0(XOR_operator_c) ccb@204: void *visit_expression_type_c::visit(XOR_operator_c *symbol) { ccb@204: verify_null(symbol); msousa@257: il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_BIT_compatible); ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: // SYM_REF0(ANDN_operator_c) ccb@204: void *visit_expression_type_c::visit(ANDN_operator_c *symbol) { ccb@204: verify_null(symbol); msousa@257: il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_BIT_compatible); ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: // SYM_REF0(ORN_operator_c) ccb@204: void *visit_expression_type_c::visit(ORN_operator_c *symbol) { ccb@204: verify_null(symbol); msousa@257: il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_BIT_compatible); ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: // SYM_REF0(XORN_operator_c) ccb@204: void *visit_expression_type_c::visit(XORN_operator_c *symbol) { ccb@204: verify_null(symbol); msousa@257: il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_BIT_compatible); ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: // SYM_REF0(ADD_operator_c) ccb@204: void *visit_expression_type_c::visit(ADD_operator_c *symbol) { ccb@204: verify_null(symbol); ccb@204: symbol_c *left_type = il_default_variable_type; ccb@204: symbol_c *right_type = il_operand_type; msousa@257: msousa@257: /* The following is not required, it is already handled by compute_expression() ... */ msousa@257: /* msousa@257: if (is_type(left_type, time_type_name_c) && is_type(right_type, time_type_name_c)) ccb@204: il_default_variable_type = &time_type_name; msousa@257: */ msousa@257: msousa@257: if (is_type(left_type, tod_type_name_c) && is_type(right_type, time_type_name_c)) ccb@204: il_default_variable_type = &tod_type_name; msousa@257: else if (is_type(left_type, safetod_type_name_c) && is_type(right_type, time_type_name_c)) msousa@257: il_default_variable_type = &tod_type_name; msousa@257: else if (is_type(left_type, tod_type_name_c) && is_type(right_type, safetime_type_name_c)) msousa@257: il_default_variable_type = &tod_type_name; msousa@257: else if (is_type(left_type, safetod_type_name_c) && is_type(right_type, safetime_type_name_c)) msousa@257: il_default_variable_type = &safetod_type_name; msousa@257: msousa@257: else if (is_type(left_type, dt_type_name_c) && is_type(right_type, time_type_name_c)) ccb@204: il_default_variable_type = &dt_type_name; msousa@257: else if (is_type(left_type, safedt_type_name_c) && is_type(right_type, time_type_name_c)) msousa@257: il_default_variable_type = &dt_type_name; msousa@257: else if (is_type(left_type, dt_type_name_c) && is_type(right_type, safetime_type_name_c)) msousa@257: il_default_variable_type = &dt_type_name; msousa@257: else if (is_type(left_type, safedt_type_name_c) && is_type(right_type, safetime_type_name_c)) msousa@257: il_default_variable_type = &safedt_type_name; msousa@257: msousa@257: else il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_MAGNITUDE_compatible); ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: // SYM_REF0(SUB_operator_c) ccb@204: void *visit_expression_type_c::visit(SUB_operator_c *symbol) { ccb@204: verify_null(symbol); ccb@204: symbol_c *left_type = il_default_variable_type; ccb@204: symbol_c *right_type = il_operand_type;; msousa@257: msousa@257: /* The following is not required, it is already handled by compute_expression() ... */ msousa@257: /* ccb@204: if (typeid(*left_type) == typeid(time_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) ccb@204: il_default_variable_type = &time_type_name; msousa@257: */ msousa@257: msousa@257: if (is_type(left_type, tod_type_name_c) && is_type(right_type, time_type_name_c)) msousa@257: il_default_variable_type = &tod_type_name; msousa@257: else if (is_type(left_type, safetod_type_name_c) && is_type(right_type, time_type_name_c)) msousa@257: il_default_variable_type = &tod_type_name; msousa@257: else if (is_type(left_type, tod_type_name_c) && is_type(right_type, safetime_type_name_c)) msousa@257: il_default_variable_type = &tod_type_name; msousa@257: else if (is_type(left_type, safetod_type_name_c) && is_type(right_type, safetime_type_name_c)) msousa@257: il_default_variable_type = &safetod_type_name; msousa@257: msousa@257: else if (is_type(left_type, dt_type_name_c) && is_type(right_type, time_type_name_c)) msousa@257: il_default_variable_type = &dt_type_name; msousa@257: else if (is_type(left_type, safedt_type_name_c) && is_type(right_type, time_type_name_c)) msousa@257: il_default_variable_type = &dt_type_name; msousa@257: else if (is_type(left_type, dt_type_name_c) && is_type(right_type, safetime_type_name_c)) msousa@257: il_default_variable_type = &dt_type_name; msousa@257: else if (is_type(left_type, safedt_type_name_c) && is_type(right_type, safetime_type_name_c)) msousa@257: il_default_variable_type = &safedt_type_name; msousa@257: msousa@257: else if (is_type(left_type, date_type_name_c) && is_type(right_type, date_type_name_c)) ccb@204: il_default_variable_type = &time_type_name; msousa@257: else if (is_type(left_type, safedate_type_name_c) && is_type(right_type, date_type_name_c)) ccb@204: il_default_variable_type = &time_type_name; msousa@257: else if (is_type(left_type, date_type_name_c) && is_type(right_type, safedate_type_name_c)) ccb@204: il_default_variable_type = &time_type_name; msousa@257: else if (is_type(left_type, safedate_type_name_c) && is_type(right_type, safedate_type_name_c)) msousa@257: il_default_variable_type = &safetime_type_name; msousa@257: msousa@257: else if (is_type(left_type, tod_type_name_c) && is_type(right_type, tod_type_name_c)) msousa@257: il_default_variable_type = &time_type_name; msousa@257: else if (is_type(left_type, safetod_type_name_c) && is_type(right_type, tod_type_name_c)) msousa@257: il_default_variable_type = &time_type_name; msousa@257: else if (is_type(left_type, tod_type_name_c) && is_type(right_type, safetod_type_name_c)) msousa@257: il_default_variable_type = &time_type_name; msousa@257: else if (is_type(left_type, safetod_type_name_c) && is_type(right_type, safetod_type_name_c)) msousa@257: il_default_variable_type = &safetime_type_name; msousa@257: msousa@257: else if (is_type(left_type, dt_type_name_c) && is_type(right_type, dt_type_name_c)) msousa@257: il_default_variable_type = &time_type_name; msousa@257: else if (is_type(left_type, safedt_type_name_c) && is_type(right_type, dt_type_name_c)) msousa@257: il_default_variable_type = &time_type_name; msousa@257: else if (is_type(left_type, dt_type_name_c) && is_type(right_type, safedt_type_name_c)) msousa@257: il_default_variable_type = &time_type_name; msousa@257: else if (is_type(left_type, safedt_type_name_c) && is_type(right_type, safedt_type_name_c)) msousa@257: il_default_variable_type = &safetime_type_name; msousa@257: msousa@257: else il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_MAGNITUDE_compatible); ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: // SYM_REF0(MUL_operator_c) ccb@204: void *visit_expression_type_c::visit(MUL_operator_c *symbol) { ccb@204: verify_null(symbol); ccb@204: symbol_c *left_type = il_default_variable_type; ccb@204: symbol_c *right_type = il_operand_type; msousa@257: msousa@257: if (is_type(left_type, time_type_name_c) && is_ANY_NUM_compatible(right_type)) ccb@204: il_default_variable_type = &time_type_name; msousa@257: else if (is_type(left_type, safetime_type_name_c) && is_ANY_NUM_type(right_type)) msousa@257: il_default_variable_type = &time_type_name; msousa@257: else if (is_type(left_type, safetime_type_name_c) && is_ANY_SAFENUM_type(right_type)) msousa@257: il_default_variable_type = &safetime_type_name; msousa@257: /* Since we have already checked for ANY_NUM_type and ANY_SAFENUM_type in the previous lines, msousa@257: * this next line is really only to check for integers/reals of undefined type on 'right_type'... msousa@257: */ msousa@257: else if (is_type(left_type, safetime_type_name_c) && is_ANY_NUM_compatible(right_type)) msousa@257: il_default_variable_type = &safetime_type_name; msousa@257: msousa@257: else il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_NUM_compatible); ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: // SYM_REF0(DIV_operator_c) ccb@204: void *visit_expression_type_c::visit(DIV_operator_c *symbol) { ccb@204: verify_null(symbol); ccb@204: symbol_c *left_type = il_default_variable_type; ccb@204: symbol_c *right_type = il_operand_type; msousa@257: msousa@257: if (is_type(left_type, time_type_name_c) && is_ANY_NUM_compatible(right_type)) ccb@204: il_default_variable_type = &time_type_name; msousa@257: else if (is_type(left_type, safetime_type_name_c) && is_ANY_NUM_type(right_type)) msousa@257: il_default_variable_type = &time_type_name; msousa@257: else if (is_type(left_type, safetime_type_name_c) && is_ANY_SAFENUM_type(right_type)) msousa@257: il_default_variable_type = &safetime_type_name; msousa@257: /* Since we have already checked for ANY_NUM_type and ANY_SAFENUM_type in the previous lines, msousa@257: * this next line is really only to check for integers/reals of undefined type on 'right_type'... msousa@257: */ msousa@257: else if (is_type(left_type, safetime_type_name_c) && is_ANY_NUM_compatible(right_type)) msousa@257: il_default_variable_type = &safetime_type_name; msousa@257: msousa@257: else il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_NUM_compatible); ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: // SYM_REF0(MOD_operator_c) ccb@204: void *visit_expression_type_c::visit(MOD_operator_c *symbol) { ccb@204: verify_null(symbol); msousa@257: il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_INT_compatible); ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: // SYM_REF0(GT_operator_c) ccb@204: void *visit_expression_type_c::visit(GT_operator_c *symbol) { ccb@204: verify_null(symbol); msousa@257: compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible); ccb@204: il_default_variable_type = &search_expression_type_c::bool_type_name; ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: //SYM_REF0(GE_operator_c) ccb@204: void *visit_expression_type_c::visit(GE_operator_c *symbol) { ccb@204: verify_null(symbol); msousa@257: compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible); ccb@204: il_default_variable_type = &search_expression_type_c::bool_type_name; ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: //SYM_REF0(EQ_operator_c) ccb@204: void *visit_expression_type_c::visit(EQ_operator_c *symbol) { ccb@204: verify_null(symbol); msousa@257: compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible); ccb@204: il_default_variable_type = &search_expression_type_c::bool_type_name; ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: //SYM_REF0(LT_operator_c) ccb@204: void *visit_expression_type_c::visit(LT_operator_c *symbol) { ccb@204: verify_null(symbol); msousa@257: compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible); ccb@204: il_default_variable_type = &search_expression_type_c::bool_type_name; ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: //SYM_REF0(LE_operator_c) ccb@204: void *visit_expression_type_c::visit(LE_operator_c *symbol) { ccb@204: verify_null(symbol); msousa@257: compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible); ccb@204: il_default_variable_type = &search_expression_type_c::bool_type_name; ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: //SYM_REF0(NE_operator_c) ccb@204: void *visit_expression_type_c::visit(NE_operator_c *symbol) { ccb@204: verify_null(symbol); msousa@257: compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible); ccb@204: il_default_variable_type = &search_expression_type_c::bool_type_name; ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: // SYM_REF0(CAL_operator_c) ccb@204: void *visit_expression_type_c::visit(CAL_operator_c *symbol) { ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: // SYM_REF0(CALC_operator_c) ccb@204: void *visit_expression_type_c::visit(CALC_operator_c *symbol) { ccb@204: if(il_default_variable_type == NULL) ccb@204: STAGE3_ERROR(symbol, symbol, "CALC: il default variable should not be NULL."); ccb@204: if (!is_BOOL_type(il_default_variable_type)) ccb@204: STAGE3_ERROR(symbol, symbol, "CALC operator requires il_default_variable to be of type BOOL."); ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: // SYM_REF0(CALCN_operator_c) ccb@204: void *visit_expression_type_c::visit(CALCN_operator_c *symbol) { ccb@204: if(il_default_variable_type == NULL) ccb@204: STAGE3_ERROR(symbol, symbol, "CALCN: il_default_variable should not be NULL."); ccb@204: if (!is_BOOL_type(il_default_variable_type)) ccb@204: STAGE3_ERROR(symbol, symbol, "CALCN operator requires il_default_variable to be of type BOOL."); ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: // SYM_REF0(RET_operator_c) ccb@204: void *visit_expression_type_c::visit(RET_operator_c *symbol) { ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: // SYM_REF0(RETC_operator_c) ccb@204: void *visit_expression_type_c::visit(RETC_operator_c *symbol) { ccb@204: if(il_default_variable_type == NULL) ccb@204: STAGE3_ERROR(symbol, symbol, "RETC: il default variable should not be NULL."); ccb@204: if (!is_BOOL_type(il_default_variable_type)) ccb@204: STAGE3_ERROR(symbol, symbol, "RETC operator requires il_default_variable to be of type BOOL."); ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: // SYM_REF0(RETCN_operator_c) ccb@204: void *visit_expression_type_c::visit(RETCN_operator_c *symbol) { ccb@204: if(il_default_variable_type == NULL) ccb@204: STAGE3_ERROR(symbol, symbol, "RETCN: il_default_variable should not be NULL."); ccb@204: if (!is_BOOL_type(il_default_variable_type)) ccb@204: STAGE3_ERROR(symbol, symbol, "RETCN operator requires il_default_variable to be of type BOOL."); ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: // SYM_REF0(JMP_operator_c) ccb@204: void *visit_expression_type_c::visit(JMP_operator_c *symbol){ ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: // SYM_REF0(JMPC_operator_c) ccb@204: void *visit_expression_type_c::visit(JMPC_operator_c *symbol) { ccb@204: if(il_default_variable_type == NULL) ccb@204: STAGE3_ERROR(symbol, symbol, "JMPC: il default variable should not be NULL."); ccb@204: if (!is_BOOL_type(il_default_variable_type)) ccb@204: STAGE3_ERROR(symbol, symbol, "JMPC operator requires il_default_variable to be of type BOOL."); ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: // SYM_REF0(JMPCN_operator_c) ccb@204: void *visit_expression_type_c::visit(JMPCN_operator_c *symbol) { ccb@204: if(il_default_variable_type == NULL) ccb@204: STAGE3_ERROR(symbol, symbol, "JMPCN: il_default_variable should not be NULL."); ccb@204: if (!is_BOOL_type(il_default_variable_type)) ccb@204: STAGE3_ERROR(symbol, symbol, "JMPCN operator requires il_default_variable to be of type BOOL."); ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: /* Symbol class handled together with function call checks */ ccb@204: /* any_identifier ASSIGN */ ccb@204: // SYM_REF1(il_assign_operator_c, variable_name) ccb@204: // void *visit_expression_type_c::visit(il_assign_operator_c *symbol, variable_name); ccb@204: ccb@204: /* Symbol class handled together with function call checks */ ccb@204: /*| [NOT] any_identifier SENDTO */ ccb@204: // SYM_REF2(il_assign_out_operator_c, option, variable_name) ccb@204: // void *visit_expression_type_c::visit(il_assign_operator_c *symbol, option, variable_name); ccb@204: ccb@204: ccb@204: ccb@204: ccb@204: ccb@204: /***************************************/ ccb@204: /* B.3 - Language ST (Structured Text) */ ccb@204: /***************************************/ ccb@204: /***********************/ ccb@204: /* B 3.1 - Expressions */ ccb@204: /***********************/ ccb@204: ccb@204: void *visit_expression_type_c::visit(or_expression_c *symbol) { ccb@204: symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); ccb@204: symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); msousa@257: return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_BIT_compatible); ccb@204: } ccb@204: ccb@204: ccb@204: void *visit_expression_type_c::visit(xor_expression_c *symbol) { ccb@204: symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); ccb@204: symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); msousa@257: return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_BIT_compatible); ccb@204: } ccb@204: ccb@204: ccb@204: void *visit_expression_type_c::visit(and_expression_c *symbol) { ccb@204: symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); ccb@204: symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); msousa@257: return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_BIT_compatible); ccb@204: } ccb@204: ccb@204: ccb@204: void *visit_expression_type_c::visit(equ_expression_c *symbol) { ccb@204: symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); ccb@204: symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); msousa@257: compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible); ccb@204: return &search_expression_type_c::bool_type_name; ccb@204: } ccb@204: ccb@204: ccb@204: void *visit_expression_type_c::visit(notequ_expression_c *symbol) { ccb@204: symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); ccb@204: symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); msousa@257: compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible); ccb@204: return &search_expression_type_c::bool_type_name; ccb@204: } ccb@204: ccb@204: ccb@204: void *visit_expression_type_c::visit(lt_expression_c *symbol) { ccb@204: symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); ccb@204: symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); msousa@257: compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible); ccb@204: return &search_expression_type_c::bool_type_name; ccb@204: } ccb@204: ccb@204: ccb@204: void *visit_expression_type_c::visit(gt_expression_c *symbol) { ccb@204: symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); ccb@204: symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); msousa@257: compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible); ccb@204: return &search_expression_type_c::bool_type_name; ccb@204: } ccb@204: ccb@204: ccb@204: void *visit_expression_type_c::visit(le_expression_c *symbol) { ccb@204: symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); ccb@204: symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); msousa@257: compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible); ccb@204: return &search_expression_type_c::bool_type_name; ccb@204: } ccb@204: ccb@204: ccb@204: void *visit_expression_type_c::visit(ge_expression_c *symbol) { ccb@204: symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); ccb@204: symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); msousa@257: compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible); ccb@204: return &search_expression_type_c::bool_type_name; ccb@204: } ccb@204: ccb@204: ccb@204: void *visit_expression_type_c::visit(add_expression_c *symbol) { ccb@204: symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); ccb@204: symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); msousa@257: msousa@257: /* The following is already checked in compute_expression */ msousa@257: /* msousa@257: if (is_type(left_type, time_type_name_c) && is_type(right_type, time_type_name_c)) msousa@257: return (void *)&time_type_name; msousa@257: */ msousa@257: msousa@257: if (is_type(left_type, tod_type_name_c) && is_type(right_type, time_type_name_c)) msousa@257: return (void *)&tod_type_name; msousa@257: if (is_type(left_type, safetod_type_name_c) && is_type(right_type, time_type_name_c)) msousa@257: return (void *)&tod_type_name; msousa@257: if (is_type(left_type, tod_type_name_c) && is_type(right_type, safetime_type_name_c)) msousa@257: return (void *)&tod_type_name; msousa@257: if (is_type(left_type, safetod_type_name_c) && is_type(right_type, safetime_type_name_c)) msousa@257: return (void *)&safetod_type_name; msousa@257: msousa@257: if (is_type(left_type, dt_type_name_c) && is_type(right_type, time_type_name_c)) msousa@257: return (void *)&dt_type_name; msousa@257: if (is_type(left_type, safedt_type_name_c) && is_type(right_type, time_type_name_c)) msousa@257: return (void *)&dt_type_name; msousa@257: if (is_type(left_type, dt_type_name_c) && is_type(right_type, safetime_type_name_c)) msousa@257: return (void *)&dt_type_name; msousa@257: if (is_type(left_type, safedt_type_name_c) && is_type(right_type, safetime_type_name_c)) msousa@257: return (void *)&safedt_type_name; msousa@257: msousa@257: return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_MAGNITUDE_compatible); ccb@204: } ccb@204: ccb@204: ccb@204: void *visit_expression_type_c::visit(sub_expression_c *symbol) { ccb@204: symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); ccb@204: symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); msousa@257: msousa@257: /* The following is already checked in compute_expression */ msousa@257: /* msousa@257: if (is_type(left_type, time_type_name_c) && is_type(right_type, time_type_name_c)) msousa@257: return (void *)&time_type_name; msousa@257: */ msousa@257: msousa@257: if (is_type(left_type, tod_type_name_c) && is_type(right_type, time_type_name_c)) msousa@257: return (void *)&tod_type_name; msousa@257: if (is_type(left_type, safetod_type_name_c) && is_type(right_type, time_type_name_c)) msousa@257: return (void *)&tod_type_name; msousa@257: if (is_type(left_type, tod_type_name_c) && is_type(right_type, safetime_type_name_c)) msousa@257: return (void *)&tod_type_name; msousa@257: if (is_type(left_type, safetod_type_name_c) && is_type(right_type, safetime_type_name_c)) msousa@257: return (void *)&safetod_type_name; msousa@257: msousa@257: if (is_type(left_type, dt_type_name_c) && is_type(right_type, time_type_name_c)) msousa@257: return (void *)&dt_type_name; msousa@257: if (is_type(left_type, safedt_type_name_c) && is_type(right_type, time_type_name_c)) msousa@257: return (void *)&dt_type_name; msousa@257: if (is_type(left_type, dt_type_name_c) && is_type(right_type, safetime_type_name_c)) msousa@257: return (void *)&dt_type_name; msousa@257: if (is_type(left_type, safedt_type_name_c) && is_type(right_type, safetime_type_name_c)) msousa@257: return (void *)&safedt_type_name; msousa@257: msousa@257: if (is_type(left_type, tod_type_name_c) && is_type(right_type, tod_type_name_c)) msousa@257: return (void *)&time_type_name; msousa@257: if (is_type(left_type, safetod_type_name_c) && is_type(right_type, tod_type_name_c)) msousa@257: return (void *)&time_type_name; msousa@257: if (is_type(left_type, tod_type_name_c) && is_type(right_type, safetod_type_name_c)) msousa@257: return (void *)&time_type_name; msousa@257: if (is_type(left_type, safetod_type_name_c) && is_type(right_type, safetod_type_name_c)) msousa@257: return (void *)&safetime_type_name; msousa@257: msousa@257: if (is_type(left_type, date_type_name_c) && is_type(right_type, date_type_name_c)) msousa@257: return (void *)&time_type_name; msousa@257: if (is_type(left_type, safedate_type_name_c) && is_type(right_type, date_type_name_c)) msousa@257: return (void *)&time_type_name; msousa@257: if (is_type(left_type, date_type_name_c) && is_type(right_type, safedate_type_name_c)) msousa@257: return (void *)&time_type_name; msousa@257: if (is_type(left_type, safedate_type_name_c) && is_type(right_type, safedate_type_name_c)) msousa@257: return (void *)&safetime_type_name; msousa@257: msousa@257: if (is_type(left_type, dt_type_name_c) && is_type(right_type, dt_type_name_c)) msousa@257: return (void *)&time_type_name; msousa@257: if (is_type(left_type, safedt_type_name_c) && is_type(right_type, dt_type_name_c)) msousa@257: return (void *)&time_type_name; msousa@257: if (is_type(left_type, dt_type_name_c) && is_type(right_type, safedt_type_name_c)) msousa@257: return (void *)&time_type_name; msousa@257: if (is_type(left_type, safedt_type_name_c) && is_type(right_type, safedt_type_name_c)) msousa@257: return (void *)&safetime_type_name; msousa@257: msousa@257: return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_MAGNITUDE_compatible); ccb@204: } ccb@204: ccb@204: ccb@204: void *visit_expression_type_c::visit(mul_expression_c *symbol) { ccb@204: symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); ccb@204: symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); msousa@257: msousa@257: if (is_type(left_type, time_type_name_c) && is_ANY_NUM_compatible(right_type)) msousa@257: return (void *)&time_type_name; msousa@257: if (is_type(left_type, safetime_type_name_c) && is_ANY_NUM_type(right_type)) msousa@257: return (void *)&time_type_name; msousa@257: if (is_type(left_type, safetime_type_name_c) && is_ANY_SAFENUM_type(right_type)) msousa@257: return (void *)&safetime_type_name; msousa@257: /* Since we have already checked for ANY_NUM_type and ANY_SAFENUM_type in the previous lines, msousa@257: * this next line is really only to check for integers/reals of undefined type on 'right_type'... msousa@257: */ msousa@257: if (is_type(left_type, safetime_type_name_c) && is_ANY_NUM_compatible(right_type)) msousa@257: return (void *)&safetime_type_name; msousa@257: msousa@257: return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_NUM_compatible); ccb@204: } ccb@204: ccb@204: ccb@204: void *visit_expression_type_c::visit(div_expression_c *symbol) { ccb@204: symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); ccb@204: symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); msousa@257: msousa@257: if (is_type(left_type, time_type_name_c) && is_ANY_NUM_compatible(right_type)) msousa@257: return (void *)&time_type_name; msousa@257: if (is_type(left_type, safetime_type_name_c) && is_ANY_NUM_type(right_type)) msousa@257: return (void *)&time_type_name; msousa@257: if (is_type(left_type, safetime_type_name_c) && is_ANY_SAFENUM_type(right_type)) msousa@257: return (void *)&safetime_type_name; msousa@257: /* Since we have already checked for ANY_NUM_type and ANY_SAFENUM_type in the previous lines, msousa@257: * this next line is really only to check for integers/reals of undefined type on 'right_type'... msousa@257: */ msousa@257: if (is_type(left_type, safetime_type_name_c) && is_ANY_NUM_compatible(right_type)) msousa@257: return (void *)&safetime_type_name; msousa@257: msousa@257: return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_NUM_compatible); ccb@204: } ccb@204: ccb@204: ccb@204: void *visit_expression_type_c::visit(mod_expression_c *symbol) { ccb@204: symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); ccb@204: symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); msousa@257: return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_INT_compatible); ccb@204: } ccb@204: ccb@204: ccb@204: void *visit_expression_type_c::visit(power_expression_c *symbol) { ccb@204: symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); ccb@204: symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); msousa@257: if (!is_ANY_REAL_compatible(left_type)) ccb@204: STAGE3_ERROR(symbol->l_exp, symbol->l_exp, "first operand of ** operator has invalid data type, should be of type ANY_REAL."); msousa@257: if (!is_ANY_NUM_compatible(right_type)) ccb@204: STAGE3_ERROR(symbol->r_exp, symbol->r_exp, "second operand of ** operator has invalid data type, should be of type ANY_NUM."); ccb@204: ccb@204: return (void *)left_type; ccb@204: } ccb@204: ccb@204: ccb@204: void *visit_expression_type_c::visit(neg_expression_c *symbol) { ccb@204: symbol_c *exp_type = base_type((symbol_c *)symbol->exp->accept(*this)); msousa@257: if (!is_ANY_MAGNITUDE_compatible(exp_type)) ccb@204: STAGE3_ERROR(symbol, symbol, "operand of negate expression '-' has invalid data type, should be of type ANY_MAGNITUDE."); ccb@204: ccb@204: return exp_type; ccb@204: } ccb@204: ccb@204: ccb@204: void *visit_expression_type_c::visit(not_expression_c *symbol) { ccb@204: symbol_c *type = base_type((symbol_c *)symbol->exp->accept(*this)); msousa@257: return compute_expression(type, type, &visit_expression_type_c::is_ANY_BIT_compatible); ccb@204: } ccb@204: ccb@204: ccb@204: void *visit_expression_type_c::visit(function_invocation_c *symbol) { ccb@204: function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name); ccb@204: if (f_decl == function_symtable.end_value()) { ccb@204: /* TODO: the following code is for standard library functions. We do not yet support this... */ ccb@204: void *res = compute_standard_function_default(symbol); ccb@204: if (res != NULL) return res; ccb@204: ERROR; ccb@204: } ccb@204: ccb@204: /* now check the semantics of the function call... */ ccb@204: /* If the syntax parser is working correctly, exactly one of the ccb@204: * following two symbols will be NULL, while the other is != NULL. ccb@204: */ ccb@204: if (symbol-> formal_param_list != NULL) check_formal_call (symbol, f_decl); ccb@204: if (symbol->nonformal_param_list != NULL) check_nonformal_call(symbol, f_decl); ccb@204: ccb@204: return base_type(f_decl->type_name); ccb@204: } ccb@204: ccb@204: /********************/ ccb@204: /* B 3.2 Statements */ ccb@204: /********************/ ccb@204: // SYM_LIST(statement_list_c) ccb@204: /* The visitor of the base class search_visitor_c will handle calling each instruction in the list. ccb@204: * We do not need to do anything here... ccb@204: */ ccb@204: // void *visit_expression_type_c::visit(statement_list_c *symbol) ccb@204: ccb@204: ccb@204: /*********************************/ ccb@204: /* B 3.2.1 Assignment Statements */ ccb@204: /*********************************/ ccb@204: ccb@204: void *visit_expression_type_c::visit(assignment_statement_c *symbol) { ccb@204: symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); ccb@204: symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); ccb@204: msousa@257: if (!is_valid_assignment(left_type, right_type)) { ccb@204: STAGE3_ERROR(symbol, symbol, "data type mismatch in assignment statement!\n"); ccb@204: } ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: ccb@204: ccb@204: /*****************************************/ ccb@204: /* B 3.2.2 Subprogram Control Statements */ ccb@204: /*****************************************/ ccb@204: ccb@204: /* RETURN */ ccb@204: // SYM_REF0(return_statement_c) ccb@204: ccb@204: ccb@204: /* fb_name '(' [param_assignment_list] ')' */ ccb@204: /* param_assignment_list -> may be NULL ! */ ccb@204: // SYM_REF3(fb_invocation_c, fb_name, formal_param_list, nonformal_param_list) ccb@204: void *visit_expression_type_c::visit(fb_invocation_c *symbol) { ccb@204: symbol_c *fb_decl = search_varfb_instance_type->get_type(symbol->fb_name); ccb@204: /* The following should never occur. The function block must be defined, ccb@204: * and the FB type being called MUST be in the symtable... ccb@204: * This was all already checked at stage 2! ccb@204: */ ccb@204: if (NULL == fb_decl) ERROR; ccb@204: ccb@204: /* now check the semantics of the fb call... */ ccb@204: /* If the syntax parser is working correctly, exactly one of the ccb@204: * following two symbols will be NULL, while the other is != NULL. ccb@204: */ ccb@204: if (symbol-> formal_param_list != NULL) check_formal_call (symbol, fb_decl); ccb@204: if (symbol->nonformal_param_list != NULL) check_nonformal_call(symbol, fb_decl); ccb@204: ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: ccb@204: #if 0 ccb@204: /* helper symbol for fb_invocation */ ccb@204: /* param_assignment_list ',' param_assignment */ ccb@204: SYM_LIST(param_assignment_list_c) ccb@204: ccb@204: /* variable_name ASSIGN expression */ ccb@204: SYM_REF2(input_variable_param_assignment_c, variable_name, expression) ccb@204: ccb@204: /* [NOT] variable_name '=>' variable */ ccb@204: SYM_REF3(output_variable_param_assignment_c, not_param, variable_name, variable) ccb@204: ccb@204: /* helper CLASS for output_variable_param_assignment */ ccb@204: SYM_REF0(not_paramassign_c) ccb@204: #endif ccb@204: ccb@204: /********************************/ ccb@204: /* B 3.2.3 Selection Statements */ ccb@204: /********************************/ ccb@204: ccb@204: /* IF expression THEN statement_list elseif_statement_list ELSE statement_list END_IF */ ccb@204: // SYM_REF4(if_statement_c, expression, statement_list, elseif_statement_list, else_statement_list) ccb@204: void *visit_expression_type_c::visit(if_statement_c *symbol) { ccb@204: symbol_c *expr_type = base_type((symbol_c*)symbol->expression->accept(*this)); ccb@204: if (!is_BOOL_type(expr_type)) STAGE3_ERROR(symbol,symbol,"IF conditional expression is not of boolean type."); ccb@204: if (NULL != symbol->statement_list) ccb@204: symbol->statement_list->accept(*this); ccb@204: if (NULL != symbol->elseif_statement_list) ccb@204: symbol->elseif_statement_list->accept(*this); ccb@204: if (NULL != symbol->else_statement_list) ccb@204: symbol->else_statement_list->accept(*this); ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: /* helper symbol for if_statement */ ccb@204: // SYM_LIST(elseif_statement_list_c) ccb@204: // void *visit_expression_type_c::visit(elseif_statement_list_c *symbol) { } ccb@204: ccb@204: /* helper symbol for elseif_statement_list */ ccb@204: /* ELSIF expression THEN statement_list */ ccb@204: // SYM_REF2(elseif_statement_c, expression, statement_list) ccb@204: void *visit_expression_type_c::visit(elseif_statement_c *symbol) { ccb@204: symbol_c *elseif_expr_type = base_type((symbol_c*)symbol->expression->accept(*this)); ccb@204: if(!is_BOOL_type(elseif_expr_type)) STAGE3_ERROR(symbol,symbol,"ELSIF conditional expression is not of boolean type."); ccb@204: if (NULL != symbol->statement_list) ccb@204: symbol->statement_list->accept(*this); ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: ccb@204: /* CASE expression OF case_element_list ELSE statement_list END_CASE */ ccb@204: // SYM_REF3(case_statement_c, expression, case_element_list, statement_list) ccb@204: void *visit_expression_type_c::visit(case_statement_c *symbol) { ccb@204: case_expression_type = base_type((symbol_c*)symbol->expression->accept(*this)); ccb@204: if (NULL != case_expression_type) { ccb@204: if (NULL != symbol->case_element_list) ccb@204: symbol->case_element_list->accept(*this); ccb@204: } ccb@204: if (NULL != symbol->statement_list) ccb@204: symbol->statement_list->accept(*this); ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: #if 0 ccb@204: /* helper symbol for case_statement */ ccb@204: // SYM_LIST(case_element_list_c) ccb@204: // void *visit_expression_type_c::visit(case_element_list_c *symbol); ccb@204: ccb@204: /* case_list ':' statement_list */ ccb@204: // SYM_REF2(case_element_c, case_list, statement_list) ccb@204: void *visit_expression_type_c::visit(case_element_c *symbol); ccb@204: #endif ccb@204: ccb@204: // SYM_LIST(case_list_c) ccb@204: void *visit_expression_type_c::visit(case_list_c *symbol) { ccb@204: symbol_c *element_type; ccb@204: for(int i = 0; i < symbol->n; i++) { ccb@204: element_type = (symbol_c *)symbol->elements[i]->accept(*this); ccb@204: if (NULL == element_type) { ccb@204: STAGE3_ERROR(symbol->elements[i], symbol->elements[i], "Case list element has undefined data type."); ccb@204: } else { ccb@204: element_type = base_type(element_type); ccb@204: if (NULL != element_type){ msousa@257: /* The CASE value is only used for comparison (and not assingment), so we only check for compatibility! */ ccb@204: if (!is_compatible_type(case_expression_type, element_type)) ccb@204: STAGE3_ERROR(symbol->elements[i], symbol->elements[i], "Invalid data type of case list element."); ccb@204: } ccb@204: } ccb@204: } ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: /********************************/ ccb@204: /* B 3.2.4 Iteration Statements */ ccb@204: /********************************/ ccb@204: ccb@204: /* FOR control_variable ASSIGN expression TO expression [BY expression] DO statement_list END_FOR */ ccb@204: // SYM_REF5(for_statement_c, control_variable, beg_expression, end_expression, by_expression, statement_list) ccb@204: void *visit_expression_type_c::visit(for_statement_c *symbol) { ccb@204: symbol_c *var_type = (symbol_c*)symbol->control_variable->accept(*this); ccb@204: if (NULL == var_type) ERROR; ccb@204: var_type = base_type(var_type); ccb@204: if (NULL == var_type) ERROR; ccb@204: // ASSIGN ccb@204: symbol_c *beg_expr_type = base_type((symbol_c*)symbol->beg_expression->accept(*this)); msousa@257: if (NULL != beg_expr_type) { msousa@257: /* The BEG value is assigned to the variable, so we check for assignment validity! */ msousa@257: if(!is_valid_assignment(var_type, beg_expr_type)) ccb@204: STAGE3_ERROR(symbol, symbol, "Data type mismatch between control variable and initial value."); ccb@204: } ccb@204: // TO ccb@204: symbol_c *end_expr_type = base_type((symbol_c*)symbol->end_expression->accept(*this)); ccb@204: if (NULL != end_expr_type) { msousa@257: /* The TO value is only used for comparison, so we only check for compatibility! */ msousa@257: if(!is_compatible_type(var_type, end_expr_type)) ccb@204: STAGE3_ERROR(symbol, symbol, "Data type mismatch between control variable and final value."); ccb@204: } ccb@204: // BY ccb@204: if(symbol->by_expression != NULL) { ccb@204: symbol_c *by_expr_type = base_type((symbol_c*)symbol->by_expression->accept(*this)); ccb@204: if (NULL != end_expr_type) { msousa@257: /* The BY value is used in an expression (add, sub, ...), so we only check for compatibility! */ msousa@257: if(!is_compatible_type(var_type, by_expr_type)) ccb@204: STAGE3_ERROR(symbol, symbol, "Data type mismatch between control variable and BY value."); ccb@204: } ccb@204: } ccb@204: // DO ccb@204: if (NULL != symbol->statement_list) ccb@204: symbol->statement_list->accept(*this); ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: ccb@204: /* WHILE expression DO statement_list END_WHILE */ ccb@204: // SYM_REF2(while_statement_c, expression, statement_list) ccb@204: void *visit_expression_type_c::visit(while_statement_c *symbol) { ccb@204: symbol_c *expr_type = base_type((symbol_c*)symbol->expression->accept(*this)); ccb@204: if (NULL != expr_type) { ccb@204: if(!is_BOOL_type(expr_type)) ccb@204: STAGE3_ERROR(symbol,symbol,"WHILE conditional expression is not of boolean type."); ccb@204: } ccb@204: ccb@204: if (NULL != symbol->statement_list) ccb@204: symbol->statement_list->accept(*this); ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: /* REPEAT statement_list UNTIL expression END_REPEAT */ ccb@204: // SYM_REF2(repeat_statement_c, statement_list, expression) ccb@204: void *visit_expression_type_c::visit(repeat_statement_c *symbol) { ccb@204: if (NULL != symbol->statement_list) ccb@204: symbol->statement_list->accept(*this); ccb@204: ccb@204: symbol_c *expr_type = base_type((symbol_c*)symbol->expression->accept(*this)); ccb@204: if (NULL != expr_type) { ccb@204: if(!is_BOOL_type(expr_type)) ccb@204: STAGE3_ERROR(symbol,symbol,"REPEAT conditional expression is not of boolean type."); ccb@204: } ccb@204: return NULL; ccb@204: } ccb@204: ccb@204: /* EXIT */ ccb@204: // SYM_REF0(exit_statement_c) ccb@204: ccb@204: ccb@204: