msousa@417: /* msousa@417: * matiec - a compiler for the programming languages defined in IEC 61131-3 msousa@417: * msousa@417: * Copyright (C) 2009-2012 Mario de Sousa (msousa@fe.up.pt) msousa@417: * Copyright (C) 2012 Manuele Conti (manuele.conti@sirius-es.it) msousa@417: * Copyright (C) 2012 Matteo Facchinetti (matteo.facchinetti@sirius-es.it) msousa@417: * msousa@417: * This program is free software: you can redistribute it and/or modify msousa@417: * it under the terms of the GNU General Public License as published by msousa@417: * the Free Software Foundation, either version 3 of the License, or msousa@417: * (at your option) any later version. msousa@417: * msousa@417: * This program is distributed in the hope that it will be useful, msousa@417: * but WITHOUT ANY WARRANTY; without even the implied warranty of msousa@417: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the msousa@417: * GNU General Public License for more details. msousa@417: * msousa@417: * You should have received a copy of the GNU General Public License msousa@417: * along with this program. If not, see . msousa@417: * msousa@417: * msousa@417: * This code is made available on the understanding that it will not be msousa@417: * used in safety-critical situations without a full and competent review. msousa@417: */ msousa@417: msousa@417: /* msousa@417: * An IEC 61131-3 compiler. msousa@417: * msousa@417: * Based on the msousa@417: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) msousa@417: * msousa@417: */ msousa@417: msousa@417: msousa@541: /* TODO - things yet not checked by this data type checker... msousa@541: * msousa@541: * - check variable declarations msousa@541: * - check data type declarations msousa@541: * - check inside configurations (variable declarations) msousa@541: * - check SFC code msousa@541: * - must fix S and R IL functions (includes potientialy fixing stage4 code!) msousa@541: */ msousa@541: msousa@541: msousa@552: /* NOTE: The algorithm implemented here assumes that flow control analysis has already been completed! msousa@552: * BEFORE running this visitor, be sure to CALL the flow_control_analysis_c visitor! msousa@552: */ msousa@552: msousa@552: msousa@417: /* msousa@552: * Fill the candidate datatype list for all symbols that may legally 'have' a data type (e.g. variables, literals, function calls, expressions, etc.) msousa@552: * msousa@552: * The candidate datatype list will be filled with a list of all the data types that expression may legally take. msousa@552: * For example, the very simple literal '0' (as in foo := 0), may represent a: msousa@552: * BOOL, BYTE, WORD, DWORD, LWORD, USINT, SINT, UINT, INT, UDINT, DINT, ULINT, LINT (as well as the SAFE versions of these data tyes too!) msousa@417: */ msousa@417: msousa@604: #include <../main.hh> /* required for UINT64_MAX, INT64_MAX, INT64_MIN, ... */ msousa@417: #include "fill_candidate_datatypes.hh" msousa@417: #include "datatype_functions.hh" msousa@417: #include msousa@417: #include msousa@417: #include msousa@417: #include msousa@417: #include msousa@417: msousa@612: #define GET_CVALUE(dtype, symbol) ((symbol)->const_value._##dtype.value) msousa@612: #define VALID_CVALUE(dtype, symbol) (symbol_c::cs_const_value == (symbol)->const_value._##dtype.status) msousa@612: #define IS_OVERFLOW(dtype, symbol) (symbol_c::cs_overflow == (symbol)->const_value._##dtype.status) conti@603: msousa@417: /* set to 1 to see debug info during execution */ msousa@454: static int debug = 0; msousa@417: msousa@417: fill_candidate_datatypes_c::fill_candidate_datatypes_c(symbol_c *ignore) { msousa@417: } msousa@417: msousa@417: fill_candidate_datatypes_c::~fill_candidate_datatypes_c(void) { msousa@417: } msousa@417: msousa@478: symbol_c *fill_candidate_datatypes_c::widening_conversion(symbol_c *left_type, symbol_c *right_type, const struct widen_entry widen_table[]) { msousa@417: int k; msousa@417: /* find a widening table entry compatible */ msousa@417: for (k = 0; NULL != widen_table[k].left; k++) msousa@478: if ((typeid(*left_type) == typeid(*widen_table[k].left)) && (typeid(*right_type) == typeid(*widen_table[k].right))) conti@476: return widen_table[k].result; msousa@417: return NULL; msousa@417: } msousa@417: msousa@421: msousa@465: /* add a data type to a candidate data type list, while guaranteeing no duplicate entries! */ msousa@465: /* Returns true if it really did add the datatype to the list, or false if it was already present in the list! */ msousa@465: bool fill_candidate_datatypes_c::add_datatype_to_candidate_list(symbol_c *symbol, symbol_c *datatype) { msousa@478: /* If it is an invalid data type, do not insert! msousa@478: * NOTE: it reduces overall code size to do this test here, instead of doing every time before calling the add_datatype_to_candidate_list() function. msousa@478: */ msousa@478: if (!is_type_valid(datatype)) /* checks for NULL and invalid_type_name_c */ msousa@478: return false; msousa@478: msousa@465: if (search_in_candidate_datatype_list(datatype, symbol->candidate_datatypes) >= 0) msousa@465: /* already in the list, Just return! */ msousa@465: return false; msousa@465: msousa@465: /* not yet in the candidate data type list, so we insert it now! */ msousa@465: symbol->candidate_datatypes.push_back(datatype); msousa@465: return true; msousa@465: } msousa@465: msousa@465: msousa@472: bool fill_candidate_datatypes_c::add_2datatypes_to_candidate_list(symbol_c *symbol, symbol_c *datatype1, symbol_c *datatype2) { msousa@472: add_datatype_to_candidate_list(symbol, datatype1); msousa@472: add_datatype_to_candidate_list(symbol, datatype2); msousa@472: return true; msousa@472: } conti@603: msousa@607: msousa@607: conti@603: void fill_candidate_datatypes_c::remove_incompatible_datatypes(symbol_c *symbol) { msousa@607: #ifdef __REMOVE__ msousa@607: #error __REMOVE__ macro already exists. Choose another name! msousa@607: #endif msousa@607: #define __REMOVE__(datatype)\ msousa@607: remove_from_candidate_datatype_list(&search_constant_type_c::datatype, symbol->candidate_datatypes);\ msousa@607: remove_from_candidate_datatype_list(&search_constant_type_c::safe##datatype, symbol->candidate_datatypes); msousa@607: msousa@607: {/* Remove unsigned data types */ msousa@607: uint64_t value = 0; msousa@607: if (VALID_CVALUE( uint64, symbol)) value = GET_CVALUE(uint64, symbol); msousa@607: if (IS_OVERFLOW ( uint64, symbol)) value = (uint64_t)UINT32_MAX + (uint64_t)1; msousa@607: msousa@607: if (value > 1 ) {__REMOVE__(bool_type_name);} msousa@607: if (value > UINT8_MAX ) {__REMOVE__(usint_type_name); __REMOVE__( byte_type_name);} msousa@607: if (value > UINT16_MAX ) {__REMOVE__( uint_type_name); __REMOVE__( word_type_name);} msousa@607: if (value > UINT32_MAX ) {__REMOVE__(udint_type_name); __REMOVE__(dword_type_name);} msousa@607: if (IS_OVERFLOW( uint64, symbol)) {__REMOVE__(ulint_type_name); __REMOVE__(lword_type_name);} conti@603: } msousa@607: msousa@607: {/* Remove signed data types */ msousa@607: int64_t value = 0; msousa@607: if (VALID_CVALUE( int64, symbol)) value = GET_CVALUE(int64, symbol); msousa@607: if (IS_OVERFLOW ( int64, symbol)) value = (int64_t)INT32_MAX + (int64_t)1; msousa@607: msousa@609: if ((value < INT8_MIN) || (value > INT8_MAX)) {__REMOVE__(sint_type_name);} msousa@609: if ((value < INT16_MIN) || (value > INT16_MAX)) {__REMOVE__( int_type_name);} msousa@609: if ((value < INT32_MIN) || (value > INT32_MAX)) {__REMOVE__(dint_type_name);} msousa@609: if (IS_OVERFLOW( int64, symbol)) {__REMOVE__(lint_type_name);} conti@603: } msousa@607: msousa@607: {/* Remove floating point data types */ msousa@607: real64_t value = 0; msousa@607: if (VALID_CVALUE( real64, symbol)) value = GET_CVALUE(real64, symbol); msousa@610: if (IS_OVERFLOW ( real64, symbol)) value = (real64_t)REAL32_MAX + (real64_t)1; msousa@607: if (value > REAL32_MAX ) {__REMOVE__( real_type_name);} msousa@607: if (value < -REAL32_MAX ) {__REMOVE__( real_type_name);} msousa@607: if (IS_OVERFLOW( real64, symbol)) {__REMOVE__(lreal_type_name);} conti@603: } msousa@607: #undef __REMOVE__ conti@603: } msousa@465: msousa@421: msousa@420: /* returns true if compatible function/FB invocation, otherwise returns false */ msousa@424: /* Assumes that the candidate_datatype lists of all the parameters being passed haved already been filled in */ msousa@443: /* msousa@443: * All parameters being passed to the called function MUST be in the parameter list to which f_call points to! msousa@443: * This means that, for non formal function calls in IL, de current (default value) must be artificially added to the msousa@443: * beginning of the parameter list BEFORE calling handle_function_call(). msousa@443: */ msousa@420: bool fill_candidate_datatypes_c::match_nonformal_call(symbol_c *f_call, symbol_c *f_decl) { msousa@449: symbol_c *call_param_value, *param_datatype; msousa@417: identifier_c *param_name; msousa@417: function_param_iterator_c fp_iterator(f_decl); msousa@417: function_call_param_iterator_c fcp_iterator(f_call); msousa@417: int extensible_parameter_highest_index = -1; msousa@417: unsigned int i; msousa@417: msousa@417: /* Iterating through the non-formal parameters of the function call */ msousa@417: while((call_param_value = fcp_iterator.next_nf()) != NULL) { msousa@417: /* Iterate to the next parameter of the function being called. msousa@417: * Get the name of that parameter, and ignore if EN or ENO. msousa@417: */ msousa@417: do { msousa@417: param_name = fp_iterator.next(); msousa@417: /* If there is no other parameter declared, then we are passing too many parameters... */ msousa@420: if(param_name == NULL) return false; msousa@417: } while ((strcmp(param_name->value, "EN") == 0) || (strcmp(param_name->value, "ENO") == 0)); msousa@417: msousa@455: /* TODO: verify if it is lvalue when INOUT or OUTPUT parameters! */ msousa@417: /* Get the parameter type */ msousa@449: param_datatype = base_type(fp_iterator.param_type()); msousa@420: msousa@420: /* check whether one of the candidate_data_types of the value being passed is the same as the param_type */ msousa@449: if (search_in_candidate_datatype_list(param_datatype, call_param_value->candidate_datatypes) < 0) msousa@442: return false; /* return false if param_type not in the list! */ msousa@420: } msousa@420: /* call is compatible! */ msousa@420: return true; msousa@420: } msousa@420: msousa@421: msousa@421: msousa@420: /* returns true if compatible function/FB invocation, otherwise returns false */ msousa@424: /* Assumes that the candidate_datatype lists of all the parameters being passed haved already been filled in */ msousa@455: bool fill_candidate_datatypes_c::match_formal_call(symbol_c *f_call, symbol_c *f_decl, symbol_c **first_param_datatype) { msousa@449: symbol_c *call_param_value, *call_param_name, *param_datatype; msousa@417: symbol_c *verify_duplicate_param; msousa@417: identifier_c *param_name; msousa@417: function_param_iterator_c fp_iterator(f_decl); msousa@417: function_call_param_iterator_c fcp_iterator(f_call); msousa@417: int extensible_parameter_highest_index = -1; msousa@417: identifier_c *extensible_parameter_name; msousa@417: unsigned int i; msousa@455: bool is_first_param = true; msousa@417: msousa@417: /* Iterating through the formal parameters of the function call */ msousa@417: while((call_param_name = fcp_iterator.next_f()) != NULL) { msousa@417: /* Obtaining the value being passed in the function call */ msousa@417: call_param_value = fcp_iterator.get_current_value(); msousa@417: /* the following should never occur. If it does, then we have a bug in our code... */ msousa@417: if (NULL == call_param_value) ERROR; msousa@417: msousa@449: /* Obtaining the assignment direction: := (assign_in) or => (assign_out) */ msousa@449: function_call_param_iterator_c::assign_direction_t call_param_dir = fcp_iterator.get_assign_direction(); msousa@449: msousa@417: /* Checking if there are duplicated parameter values */ msousa@417: verify_duplicate_param = fcp_iterator.search_f(call_param_name); msousa@417: if(verify_duplicate_param != call_param_value) msousa@420: return false; msousa@417: msousa@417: /* Obtaining the type of the value being passed in the function call */ msousa@417: std::vector &call_param_types = call_param_value->candidate_datatypes; msousa@417: msousa@417: /* Find the corresponding parameter in function declaration */ msousa@417: param_name = fp_iterator.search(call_param_name); msousa@421: if(param_name == NULL) return false; msousa@449: /* Get the parameter data type */ msousa@449: param_datatype = base_type(fp_iterator.param_type()); msousa@449: /* Get the parameter direction: IN, OUT, IN_OUT */ msousa@449: function_param_iterator_c::param_direction_t param_dir = fp_iterator.param_direction(); msousa@449: msousa@449: /* check whether direction (IN, OUT, IN_OUT) and assignment types (:= , =>) are compatible !!! */ msousa@449: if (function_call_param_iterator_c::assign_in == call_param_dir) { msousa@449: if ((function_param_iterator_c::direction_in != param_dir) && msousa@449: (function_param_iterator_c::direction_inout != param_dir)) msousa@449: return false; msousa@449: } else if (function_call_param_iterator_c::assign_out == call_param_dir) { msousa@449: if ((function_param_iterator_c::direction_out != param_dir)) msousa@449: return false; msousa@449: } else ERROR; msousa@449: msousa@421: /* check whether one of the candidate_data_types of the value being passed is the same as the param_type */ msousa@449: if (search_in_candidate_datatype_list(param_datatype, call_param_types) < 0) msousa@442: return false; /* return false if param_type not in the list! */ msousa@455: msousa@455: /* If this is the first parameter, then copy the datatype to *first_param_datatype */ msousa@455: if (is_first_param) msousa@455: if (NULL != first_param_datatype) msousa@455: *first_param_datatype = param_datatype; msousa@455: is_first_param = false; msousa@421: } msousa@421: /* call is compatible! */ msousa@420: return true; msousa@417: } msousa@417: msousa@421: msousa@421: msousa@421: msousa@438: /* Handle a generic function call! msousa@438: * Assumes that the parameter_list containing the values being passed in this function invocation msousa@438: * has already had all the candidate_datatype lists filled in! msousa@438: * msousa@438: * All parameters being passed to the called function MUST be in the parameter list to which f_call points to! msousa@438: * This means that, for non formal function calls in IL, de current (default value) must be artificially added to the msousa@438: * beginning of the parameter list BEFORE calling handle_function_call(). msousa@438: */ msousa@438: /* msousa@438: typedef struct { msousa@438: symbol_c *function_name, msousa@438: symbol_c *nonformal_operand_list, msousa@438: symbol_c * formal_operand_list, msousa@438: msousa@438: std::vector &candidate_functions, msousa@438: symbol_c &*called_function_declaration, msousa@438: int &extensible_param_count msousa@438: } generic_function_call_t; msousa@438: */ msousa@438: /* msousa@438: void narrow_candidate_datatypes_c::narrow_function_invocation(symbol_c *fcall, generic_function_call_t fcall_data) { msousa@438: void *fill_candidate_datatypes_c::handle_function_call(symbol_c *f_call, symbol_c *function_name, invocation_type_t invocation_type, msousa@438: std::vector *candidate_datatypes, msousa@438: std::vector *candidate_functions) { msousa@438: */ msousa@438: void fill_candidate_datatypes_c::handle_function_call(symbol_c *fcall, generic_function_call_t fcall_data) { msousa@438: function_declaration_c *f_decl; msousa@438: list_c *parameter_list; msousa@438: list_c *parameter_candidate_datatypes; msousa@438: symbol_c *returned_parameter_type; msousa@438: msousa@438: if (debug) std::cout << "function()\n"; msousa@438: msousa@438: function_symtable_t::iterator lower = function_symtable.lower_bound(fcall_data.function_name); msousa@438: function_symtable_t::iterator upper = function_symtable.upper_bound(fcall_data.function_name); msousa@438: /* If the name of the function being called is not found in the function symbol table, then this is an invalid call */ msousa@438: /* Since the lexical parser already checks for this, then if this occurs then we have an internal compiler error. */ msousa@438: if (lower == function_symtable.end()) ERROR; msousa@438: msousa@438: /* Look for all compatible function declarations, and add their return datatypes msousa@438: * to the candidate_datatype list of this function invocation. msousa@438: * msousa@438: * If only one function exists, we add its return datatype to the candidate_datatype list, msousa@438: * even if the parameters passed to it are invalid. msousa@438: * This guarantees that the remainder of the expression in which the function call is inserted msousa@438: * is treated as if the function call returns correctly, and therefore does not generate msousa@438: * spurious error messages. msousa@438: * Even if the parameters to the function call are invalid, doing this is still safe, as the msousa@438: * expressions inside the function call will themselves have erros and will guarantee that msousa@438: * compilation is aborted in stage3 (in print_datatypes_error_c). msousa@438: */ msousa@438: if (function_symtable.multiplicity(fcall_data.function_name) == 1) { msousa@438: f_decl = function_symtable.get_value(lower); msousa@438: returned_parameter_type = base_type(f_decl->type_name); msousa@465: if (add_datatype_to_candidate_list(fcall, returned_parameter_type)) msousa@465: /* we only add it to the function declaration list if this entry was not already present in the candidate datatype list! */ msousa@465: fcall_data.candidate_functions.push_back(f_decl); msousa@465: msousa@438: } msousa@438: for(; lower != upper; lower++) { msousa@438: bool compatible = false; msousa@438: msousa@438: f_decl = function_symtable.get_value(lower); msousa@438: /* Check if function declaration in symbol_table is compatible with parameters */ msousa@438: if (NULL != fcall_data.nonformal_operand_list) compatible=match_nonformal_call(fcall, f_decl); msousa@438: if (NULL != fcall_data. formal_operand_list) compatible= match_formal_call(fcall, f_decl); msousa@438: if (compatible) { msousa@438: /* Add the data type returned by the called functions. msousa@438: * However, only do this if this data type is not already present in the candidate_datatypes list_c msousa@438: */ msousa@465: returned_parameter_type = base_type(f_decl->type_name); msousa@465: if (add_datatype_to_candidate_list(fcall, returned_parameter_type)) msousa@465: /* we only add it to the function declaration list if this entry was not already present in the candidate datatype list! */ msousa@438: fcall_data.candidate_functions.push_back(f_decl); msousa@438: } msousa@438: } msousa@438: if (debug) std::cout << "end_function() [" << fcall->candidate_datatypes.size() << "] result.\n"; msousa@438: return; msousa@438: } msousa@438: msousa@438: msousa@447: /* handle implicit FB call in IL. msousa@448: * e.g. CLK ton_var msousa@447: * CU counter_var msousa@447: */ msousa@489: void *fill_candidate_datatypes_c::handle_implicit_il_fb_call(symbol_c *il_instruction, const char *param_name, symbol_c *&called_fb_declaration) { msousa@447: symbol_c *fb_type_id = search_varfb_instance_type->get_basetype_id(il_operand); msousa@455: /* Although a call to a non-declared FB is a semantic error, this is currently caught by stage 2! */ msousa@455: if (NULL == fb_type_id) ERROR; msousa@447: msousa@447: function_block_declaration_c *fb_decl = function_block_type_symtable.find_value(fb_type_id); msousa@447: if (function_block_type_symtable.end_value() == fb_decl) msousa@447: /* The il_operand is not the name of a FB instance. Most probably it is the name of a variable of some other type. msousa@455: * this is a semantic error. msousa@447: */ msousa@455: fb_decl = NULL; msousa@455: msousa@455: /* The narrow_candidate_datatypes_c does not rely on this called_fb_declaration pointer being == NULL to conclude that msousa@455: * we have a datatype incompatibility error, so we set it to fb_decl to allow the print_datatype_error_c to print out msousa@455: * more informative error messages! msousa@450: */ msousa@455: called_fb_declaration = fb_decl; msousa@455: msousa@455: /* This implicit FB call does not change the value stored in the current/default IL variable */ msousa@456: /* It does, however, require that the datatype be compatible with the input parameter of the FB being called. msousa@456: * If we were to follow the filling & narrowing algorithm correctly (implemented in fill_candidate_datatypes_c msousa@456: * & narrow_candidate_datatypes_c respectively), we should be restricting the candidate_datatpes to the datatypes msousa@456: * that are compatible to the FB call. msousa@456: * However, doing the above will often result in some very confusing error messages for the user, especially in the case msousa@456: * in which the FB call is wrong, so the resulting cadidate datatypes is an empty list. In this case, the user would see msousa@456: * many error messages related to the IL instructions that follow the FB call, even though those IL instructions may be perfectly msousa@456: * correct. msousa@456: * For now, we will simply let the narrow_candidate_datatypes_c verify if the datatypes are compatible (something that should be done msousa@456: * here). msousa@456: */ msousa@455: if (NULL != prev_il_instruction) msousa@467: il_instruction->candidate_datatypes = prev_il_instruction->candidate_datatypes; msousa@455: msousa@455: if (debug) std::cout << "handle_implicit_il_fb_call() [" << prev_il_instruction->candidate_datatypes.size() << "] ==> " << il_instruction->candidate_datatypes.size() << " result.\n"; msousa@489: return NULL; msousa@447: } msousa@438: msousa@438: msousa@479: msousa@479: msousa@479: /* handle a binary IL operator, like ADD, SUB, etc... */ msousa@479: void *fill_candidate_datatypes_c::handle_binary_operator(const struct widen_entry widen_table[], symbol_c *symbol, symbol_c *l_expr, symbol_c *r_expr) { msousa@479: if (NULL == l_expr) /* if no prev_il_instruction */ msousa@479: return NULL; msousa@479: msousa@479: for(unsigned int i = 0; i < l_expr->candidate_datatypes.size(); i++) msousa@479: for(unsigned int j = 0; j < r_expr->candidate_datatypes.size(); j++) msousa@479: /* NOTE: add_datatype_to_candidate_list() will only really add the datatype if it is != NULL !!! */ msousa@479: add_datatype_to_candidate_list(symbol, widening_conversion(l_expr->candidate_datatypes[i], r_expr->candidate_datatypes[j], widen_table)); conti@603: remove_incompatible_datatypes(symbol); msousa@479: if (debug) std::cout << "[" << l_expr->candidate_datatypes.size() << "," << r_expr->candidate_datatypes.size() << "] ==> " << symbol->candidate_datatypes.size() << " result.\n"; msousa@479: return NULL; msousa@479: } msousa@479: msousa@479: msousa@652: msousa@479: /* handle a binary ST expression, like '+', '-', etc... */ msousa@479: void *fill_candidate_datatypes_c::handle_binary_expression(const struct widen_entry widen_table[], symbol_c *symbol, symbol_c *l_expr, symbol_c *r_expr) { msousa@479: l_expr->accept(*this); msousa@479: r_expr->accept(*this); msousa@479: return handle_binary_operator(widen_table, symbol, l_expr, r_expr); msousa@479: } msousa@479: msousa@479: msousa@479: msousa@652: /* handle the two equality comparison operations, i.e. = (euqal) and != (not equal) */ msousa@652: /* This function is special, as it will also allow enumeration data types to be compared, with the result being a BOOL data type! msousa@652: * This possibility os not expressed in the 'widening' tables, so we need to hard code it here msousa@652: */ msousa@652: void *fill_candidate_datatypes_c::handle_equality_comparison(const struct widen_entry widen_table[], symbol_c *symbol, symbol_c *l_expr, symbol_c *r_expr) { msousa@652: search_base_type_c search_base_type; msousa@652: handle_binary_expression(widen_table, symbol, l_expr, r_expr); msousa@652: for(unsigned int i = 0; i < l_expr->candidate_datatypes.size(); i++) msousa@652: for(unsigned int j = 0; j < r_expr->candidate_datatypes.size(); j++) { msousa@652: if ((l_expr->candidate_datatypes[i] == r_expr->candidate_datatypes[j]) && search_base_type.type_is_enumerated(l_expr->candidate_datatypes[i])) msousa@652: add_datatype_to_candidate_list(symbol, &search_constant_type_c::bool_type_name); msousa@652: } msousa@652: return NULL; msousa@652: } msousa@652: msousa@652: msousa@479: msousa@417: /* a helper function... */ msousa@417: symbol_c *fill_candidate_datatypes_c::base_type(symbol_c *symbol) { msousa@417: /* NOTE: symbol == NULL is valid. It will occur when, for e.g., an undefined/undeclared symbolic_variable is used msousa@417: * in the code. msousa@417: */ msousa@417: if (symbol == NULL) return NULL; msousa@417: return (symbol_c *)symbol->accept(search_base_type); msousa@417: } msousa@417: msousa@417: /*********************/ msousa@417: /* B 1.2 - Constants */ msousa@417: /*********************/ msousa@417: /******************************/ msousa@417: /* B 1.2.1 - Numeric Literals */ msousa@417: /******************************/ msousa@472: #define sizeoftype(symbol) get_sizeof_datatype_c::getsize(symbol) msousa@472: msousa@472: void *fill_candidate_datatypes_c::handle_any_integer(symbol_c *symbol) { conti@603: add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::bool_type_name, &search_constant_type_c::safebool_type_name); conti@603: add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::byte_type_name, &search_constant_type_c::safebyte_type_name); conti@603: add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::word_type_name, &search_constant_type_c::safeword_type_name); conti@603: add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::dword_type_name, &search_constant_type_c::safedword_type_name); conti@603: add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::lword_type_name, &search_constant_type_c::safelword_type_name); conti@603: add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::sint_type_name, &search_constant_type_c::safesint_type_name); conti@603: add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::int_type_name, &search_constant_type_c::safeint_type_name); conti@603: add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::dint_type_name, &search_constant_type_c::safedint_type_name); conti@603: add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::lint_type_name, &search_constant_type_c::safelint_type_name); conti@603: add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::usint_type_name, &search_constant_type_c::safeusint_type_name); conti@603: add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::uint_type_name, &search_constant_type_c::safeuint_type_name); conti@603: add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::udint_type_name, &search_constant_type_c::safeudint_type_name); conti@603: add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::ulint_type_name, &search_constant_type_c::safeulint_type_name); conti@603: remove_incompatible_datatypes(symbol); msousa@479: if (debug) std::cout << "ANY_INT [" << symbol->candidate_datatypes.size()<< "]" << std::endl; msousa@472: return NULL; msousa@472: } msousa@472: msousa@472: msousa@472: msousa@488: void *fill_candidate_datatypes_c::handle_any_real(symbol_c *symbol) { conti@603: add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::real_type_name, &search_constant_type_c::safereal_type_name); conti@603: add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::lreal_type_name, &search_constant_type_c::safelreal_type_name); conti@603: remove_incompatible_datatypes(symbol); msousa@417: if (debug) std::cout << "ANY_REAL [" << symbol->candidate_datatypes.size() << "]" << std::endl; msousa@417: return NULL; msousa@417: } msousa@417: msousa@472: msousa@488: msousa@488: void *fill_candidate_datatypes_c::handle_any_literal(symbol_c *symbol, symbol_c *symbol_value, symbol_c *symbol_type) { msousa@488: symbol_value->accept(*this); msousa@488: if (search_in_candidate_datatype_list(symbol_type, symbol_value->candidate_datatypes) >= 0) msousa@488: add_datatype_to_candidate_list(symbol, symbol_type); conti@603: remove_incompatible_datatypes(symbol); msousa@643: if (debug) std::cout << "ANY_LITERAL [" << symbol->candidate_datatypes.size() << "]\n"; msousa@488: return NULL; msousa@488: } msousa@488: msousa@488: msousa@488: msousa@488: void *fill_candidate_datatypes_c::visit( real_c *symbol) {return handle_any_real(symbol);} msousa@488: void *fill_candidate_datatypes_c::visit(neg_real_c *symbol) {return handle_any_real(symbol);} msousa@488: msousa@417: msousa@472: msousa@417: void *fill_candidate_datatypes_c::visit(neg_integer_c *symbol) { msousa@650: /* Please read the comment in neg_expression_c method, as it also applies here */ conti@603: add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::int_type_name, &search_constant_type_c::safeint_type_name); conti@603: add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::sint_type_name, &search_constant_type_c::safesint_type_name); conti@603: add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::dint_type_name, &search_constant_type_c::safedint_type_name); conti@603: add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::lint_type_name, &search_constant_type_c::safelint_type_name); conti@603: remove_incompatible_datatypes(symbol); msousa@417: if (debug) std::cout << "neg ANY_INT [" << symbol->candidate_datatypes.size() << "]" << std::endl; msousa@417: return NULL; msousa@417: } msousa@417: msousa@472: msousa@488: msousa@479: void *fill_candidate_datatypes_c::visit(integer_c *symbol) {return handle_any_integer(symbol);} msousa@479: void *fill_candidate_datatypes_c::visit(binary_integer_c *symbol) {return handle_any_integer(symbol);} msousa@479: void *fill_candidate_datatypes_c::visit(octal_integer_c *symbol) {return handle_any_integer(symbol);} msousa@479: void *fill_candidate_datatypes_c::visit(hex_integer_c *symbol) {return handle_any_integer(symbol);} msousa@417: msousa@472: msousa@488: msousa@427: // SYM_REF2(integer_literal_c, type, value) msousa@472: /* msousa@472: * integer_literal: msousa@472: * integer_type_name '#' signed_integer msousa@472: * | integer_type_name '#' binary_integer msousa@472: * | integer_type_name '#' octal_integer msousa@472: * | integer_type_name '#' hex_integer msousa@472: */ msousa@488: void *fill_candidate_datatypes_c::visit( integer_literal_c *symbol) {return handle_any_literal(symbol, symbol->value, symbol->type);} msousa@488: void *fill_candidate_datatypes_c::visit( real_literal_c *symbol) {return handle_any_literal(symbol, symbol->value, symbol->type);} msousa@488: void *fill_candidate_datatypes_c::visit(bit_string_literal_c *symbol) {return handle_any_literal(symbol, symbol->value, symbol->type);} msousa@488: msousa@488: void *fill_candidate_datatypes_c::visit( boolean_literal_c *symbol) { msousa@488: if (NULL != symbol->type) return handle_any_literal(symbol, symbol->value, symbol->type); msousa@488: msousa@427: symbol->value->accept(*this); msousa@488: symbol->candidate_datatypes = symbol->value->candidate_datatypes; msousa@488: return NULL; msousa@488: } msousa@488: msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(boolean_true_c *symbol) { msousa@472: add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::bool_type_name, &search_constant_type_c::safebool_type_name); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(boolean_false_c *symbol) { msousa@472: add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::bool_type_name, &search_constant_type_c::safebool_type_name); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: /*******************************/ msousa@417: /* B.1.2.2 Character Strings */ msousa@417: /*******************************/ msousa@417: void *fill_candidate_datatypes_c::visit(double_byte_character_string_c *symbol) { msousa@472: add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::wstring_type_name, &search_constant_type_c::safewstring_type_name); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(single_byte_character_string_c *symbol) { msousa@472: add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::string_type_name, &search_constant_type_c::safestring_type_name); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: /***************************/ msousa@417: /* B 1.2.3 - Time Literals */ msousa@417: /***************************/ msousa@417: /************************/ msousa@417: /* B 1.2.3.1 - Duration */ msousa@417: /************************/ msousa@417: void *fill_candidate_datatypes_c::visit(duration_c *symbol) { msousa@437: /* TODO: check whether the literal follows the rules specified in section '2.2.3.1 Duration' of the standard! */ msousa@459: msousa@472: add_datatype_to_candidate_list(symbol, symbol->type_name); msousa@417: if (debug) std::cout << "TIME_LITERAL [" << symbol->candidate_datatypes.size() << "]\n"; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: /************************************/ msousa@417: /* B 1.2.3.2 - Time of day and Date */ msousa@417: /************************************/ msousa@479: void *fill_candidate_datatypes_c::visit(time_of_day_c *symbol) {add_datatype_to_candidate_list(symbol, symbol->type_name); return NULL;} msousa@479: void *fill_candidate_datatypes_c::visit(date_c *symbol) {add_datatype_to_candidate_list(symbol, symbol->type_name); return NULL;} msousa@479: void *fill_candidate_datatypes_c::visit(date_and_time_c *symbol) {add_datatype_to_candidate_list(symbol, symbol->type_name); return NULL;} msousa@417: msousa@417: /**********************/ msousa@417: /* B 1.3 - Data types */ msousa@417: /**********************/ msousa@417: /********************************/ msousa@417: /* B 1.3.3 - Derived data types */ msousa@417: /********************************/ msousa@502: msousa@502: /* simple_specification ASSIGN constant */ msousa@502: // SYM_REF2(simple_spec_init_c, simple_specification, constant) msousa@502: void *fill_candidate_datatypes_c::visit(simple_spec_init_c *symbol) { msousa@502: if (NULL != symbol->constant) symbol->constant->accept(*this); msousa@502: add_datatype_to_candidate_list(symbol->simple_specification, base_type(symbol->simple_specification)); msousa@502: symbol->candidate_datatypes = symbol->simple_specification->candidate_datatypes; msousa@502: /* NOTE: Even if the constant and the type are of incompatible data types, we let the msousa@502: * simple_spec_init_c object inherit the data type of the type declaration (simple_specification) msousa@502: * This will let us produce more informative error messages when checking data type compatibility msousa@502: * with located variables (AT %QW3.4 : WORD). msousa@502: */ msousa@502: // if (NULL != symbol->constant) intersect_candidate_datatype_list(symbol /*origin, dest.*/, symbol->constant /*with*/); msousa@502: return NULL; msousa@502: } msousa@502: msousa@417: /* signed_integer DOTDOT signed_integer */ msousa@417: // SYM_REF2(subrange_c, lower_limit, upper_limit) msousa@417: void *fill_candidate_datatypes_c::visit(subrange_c *symbol) { msousa@417: symbol->lower_limit->accept(*this); msousa@417: symbol->upper_limit->accept(*this); msousa@417: msousa@417: for (unsigned int u = 0; u < symbol->upper_limit->candidate_datatypes.size(); u++) { msousa@417: for(unsigned int l = 0; l < symbol->lower_limit->candidate_datatypes.size(); l++) { msousa@417: if (is_type_equal(symbol->upper_limit->candidate_datatypes[u], symbol->lower_limit->candidate_datatypes[l])) msousa@465: add_datatype_to_candidate_list(symbol, symbol->lower_limit->candidate_datatypes[l]); msousa@417: } msousa@417: } msousa@417: return NULL; msousa@417: } msousa@417: msousa@443: /* TYPE type_declaration_list END_TYPE */ msousa@443: // SYM_REF1(data_type_declaration_c, type_declaration_list) msousa@443: /* NOTE: Not required. already handled by iterator_visitor_c base class */ msousa@443: /* msousa@417: void *fill_candidate_datatypes_c::visit(data_type_declaration_c *symbol) { msousa@443: symbol->type_declaration_list->accept(*this); msousa@443: return NULL; msousa@443: } msousa@443: */ msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(enumerated_value_c *symbol) { msousa@417: symbol_c *enumerated_type; msousa@417: msousa@417: if (NULL != symbol->type) msousa@417: enumerated_type = symbol->type; msousa@417: else { msousa@417: enumerated_type = enumerated_value_symtable.find_value(symbol->value); msousa@417: if (enumerated_type == enumerated_value_symtable.end_value()) msousa@417: enumerated_type = NULL; msousa@417: } msousa@417: enumerated_type = base_type(enumerated_type); msousa@417: if (NULL != enumerated_type) msousa@465: add_datatype_to_candidate_list(symbol, enumerated_type); msousa@417: msousa@417: if (debug) std::cout << "ENUMERATE [" << symbol->candidate_datatypes.size() << "]\n"; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@417: /*********************/ msousa@417: /* B 1.4 - Variables */ msousa@417: /*********************/ msousa@417: void *fill_candidate_datatypes_c::visit(symbolic_variable_c *symbol) { msousa@479: add_datatype_to_candidate_list(symbol, search_varfb_instance_type->get_basetype_decl(symbol)); /* will only add if non NULL */ msousa@417: if (debug) std::cout << "VAR [" << symbol->candidate_datatypes.size() << "]\n"; msousa@417: return NULL; msousa@417: } msousa@417: msousa@502: msousa@417: /********************************************/ msousa@417: /* B 1.4.1 - Directly Represented Variables */ msousa@417: /********************************************/ msousa@417: void *fill_candidate_datatypes_c::visit(direct_variable_c *symbol) { msousa@417: /* Comment added by mario: msousa@417: * The following code is safe, actually, as the lexical parser guarantees the correct IEC61131-3 syntax was used. msousa@417: */ msousa@417: /* However, we should probably add an assertion in case we later change the lexical parser! */ msousa@417: /* if (symbol->value == NULL) ERROR; msousa@417: * if (symbol->value[0] == '\0') ERROR; msousa@417: * if (symbol->value[1] == '\0') ERROR; msousa@417: */ msousa@417: switch (symbol->value[2]) { msousa@502: case 'x': case 'X': /* bit - 1 bit */ add_datatype_to_candidate_list(symbol, &search_constant_type_c::bool_type_name); break; msousa@502: case 'b': case 'B': /* byte - 8 bits */ add_datatype_to_candidate_list(symbol, &search_constant_type_c::byte_type_name); break; msousa@502: case 'w': case 'W': /* word - 16 bits */ add_datatype_to_candidate_list(symbol, &search_constant_type_c::word_type_name); break; msousa@502: case 'd': case 'D': /* dword - 32 bits */ add_datatype_to_candidate_list(symbol, &search_constant_type_c::dword_type_name); break; msousa@502: case 'l': case 'L': /* lword - 64 bits */ add_datatype_to_candidate_list(symbol, &search_constant_type_c::lword_type_name); break; msousa@479: /* if none of the above, then the empty string was used <=> boolean */ msousa@479: default: add_datatype_to_candidate_list(symbol, &search_constant_type_c::bool_type_name); break; msousa@417: } msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: /*************************************/ msousa@417: /* B 1.4.2 - Multi-element variables */ msousa@417: /*************************************/ msousa@417: /* subscripted_variable '[' subscript_list ']' */ msousa@417: // SYM_REF2(array_variable_c, subscripted_variable, subscript_list) msousa@417: void *fill_candidate_datatypes_c::visit(array_variable_c *symbol) { msousa@417: /* get the declaration of the data type __stored__ in the array... */ msousa@417: /* if we were to want the data type of the array itself, then we should call_param_name msousa@417: * search_varfb_instance_type->get_basetype_decl(symbol->subscripted_variable) msousa@417: */ msousa@417: symbol_c *result = search_varfb_instance_type->get_basetype_decl(symbol); msousa@465: if (NULL != result) add_datatype_to_candidate_list(symbol, result); msousa@417: msousa@417: /* recursively call the subscript list, so we can check the data types of the expressions used for the subscripts */ msousa@417: symbol->subscript_list->accept(*this); msousa@417: msousa@417: if (debug) std::cout << "ARRAY_VAR [" << symbol->candidate_datatypes.size() << "]\n"; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@417: /* subscript_list ',' subscript */ msousa@417: // SYM_LIST(subscript_list_c) msousa@417: /* NOTE: we inherit from iterator visitor, so we do not need to implement this method... */ msousa@455: // void *fill_candidate_datatypes_c::visit(subscript_list_c *symbol) msousa@417: msousa@417: msousa@417: /* record_variable '.' field_selector */ msousa@417: /* WARNING: input and/or output variables of function blocks msousa@417: * may be accessed as fields of a structured variable! msousa@417: * Code handling a structured_variable_c must take msousa@417: * this into account! msousa@417: */ msousa@417: // SYM_REF2(structured_variable_c, record_variable, field_selector) msousa@417: /* NOTE: We do not need to recursively determine the data types of each field_selector, as the search_varfb_instance_type msousa@417: * will do that for us. So we determine the candidate datatypes only for the full structured_variable. msousa@417: */ msousa@417: void *fill_candidate_datatypes_c::visit(structured_variable_c *symbol) { msousa@479: add_datatype_to_candidate_list(symbol, search_varfb_instance_type->get_basetype_decl(symbol)); /* will only add if non NULL */ msousa@417: return NULL; msousa@417: } msousa@417: msousa@502: msousa@502: msousa@502: /******************************************/ msousa@502: /* B 1.4.3 - Declaration & Initialisation */ msousa@502: /******************************************/ msousa@502: msousa@502: void *fill_candidate_datatypes_c::visit(var1_list_c *symbol) { msousa@502: #if 0 /* We don't really need to set the datatype of each variable. We just check the declaration itself! */ msousa@502: for(int i = 0; i < symbol->n; i++) { msousa@502: add_datatype_to_candidate_list(symbol->elements[i], search_varfb_instance_type->get_basetype_decl(symbol->elements[i])); /* will only add if non NULL */ msousa@502: } msousa@502: #endif msousa@502: return NULL; msousa@502: } msousa@502: msousa@502: msousa@502: /* AT direct_variable */ msousa@502: // SYM_REF1(location_c, direct_variable) msousa@502: void *fill_candidate_datatypes_c::visit(location_c *symbol) { msousa@558: /* This is a special situation. msousa@558: * msousa@558: * The reason is that a located variable may be declared to be of any data type, as long as the size msousa@558: * matches the location (lines 1 3 and 4 of table 17). For example: msousa@558: * var1 AT %MB42.0 : BYTE; msousa@558: * var1 AT %MB42.1 : SINT; msousa@558: * var1 AT %MB42.2 : USINT; msousa@558: * var1 AT %MW64 : INT; msousa@558: * var1 AT %MD56 : DINT; msousa@558: * var1 AT %MD57 : REAL; msousa@558: * are all valid!! msousa@558: * msousa@558: * However, when used inside an expression, the direct variable (uses the same syntax as the location msousa@558: * of a located variable) is limited to the following (ANY_BIT) data types: msousa@558: * %MX --> BOOL msousa@558: * %MB --> BYTE msousa@558: * %MW --> WORD msousa@558: * %MD --> DWORD msousa@558: * %ML --> LWORD msousa@558: * msousa@558: * So, in order to be able to analyse expressions with direct variables msousa@558: * e.g: var1 := 66 OR %MW34 msousa@558: * where the direct variable may only take the ANY_BIT data types, the fill_candidate_datatypes_c msousa@558: * considers that only the ANY_BIT data types are allowed for a direct variable. msousa@558: * However, it appears from the examples in the standard (lines 1 3 and 4 of table 17) msousa@558: * a location may have any data type (presumably as long as the size in bits match). msousa@558: * For this reason, a location_c may have more allowable data types than a direct_variable_c msousa@558: */ msousa@558: msousa@502: symbol->direct_variable->accept(*this); msousa@558: for (unsigned int i = 0; i < symbol->direct_variable->candidate_datatypes.size(); i++) { msousa@558: switch (get_sizeof_datatype_c::getsize(symbol->direct_variable->candidate_datatypes[i])) { msousa@558: case 1: /* bit - 1 bit */ msousa@558: add_datatype_to_candidate_list(symbol, &search_constant_type_c::bool_type_name); msousa@558: add_datatype_to_candidate_list(symbol, &search_constant_type_c::safebool_type_name); msousa@558: break; msousa@558: case 8: /* byte - 8 bits */ msousa@558: add_datatype_to_candidate_list(symbol, &search_constant_type_c::byte_type_name); msousa@558: add_datatype_to_candidate_list(symbol, &search_constant_type_c::safebyte_type_name); msousa@558: add_datatype_to_candidate_list(symbol, &search_constant_type_c::sint_type_name); msousa@558: add_datatype_to_candidate_list(symbol, &search_constant_type_c::safesint_type_name); msousa@558: add_datatype_to_candidate_list(symbol, &search_constant_type_c::usint_type_name); msousa@558: add_datatype_to_candidate_list(symbol, &search_constant_type_c::safeusint_type_name); msousa@558: break; msousa@558: case 16: /* word - 16 bits */ msousa@558: add_datatype_to_candidate_list(symbol, &search_constant_type_c::word_type_name); msousa@558: add_datatype_to_candidate_list(symbol, &search_constant_type_c::safeword_type_name); msousa@558: add_datatype_to_candidate_list(symbol, &search_constant_type_c::int_type_name); msousa@558: add_datatype_to_candidate_list(symbol, &search_constant_type_c::safeint_type_name); msousa@558: add_datatype_to_candidate_list(symbol, &search_constant_type_c::uint_type_name); msousa@558: add_datatype_to_candidate_list(symbol, &search_constant_type_c::safeuint_type_name); msousa@558: break; msousa@558: case 32: /* dword - 32 bits */ msousa@558: add_datatype_to_candidate_list(symbol, &search_constant_type_c::dword_type_name); msousa@558: add_datatype_to_candidate_list(symbol, &search_constant_type_c::safedword_type_name); msousa@558: add_datatype_to_candidate_list(symbol, &search_constant_type_c::dint_type_name); msousa@558: add_datatype_to_candidate_list(symbol, &search_constant_type_c::safedint_type_name); msousa@558: add_datatype_to_candidate_list(symbol, &search_constant_type_c::udint_type_name); msousa@558: add_datatype_to_candidate_list(symbol, &search_constant_type_c::safeudint_type_name); msousa@558: add_datatype_to_candidate_list(symbol, &search_constant_type_c::real_type_name); msousa@558: add_datatype_to_candidate_list(symbol, &search_constant_type_c::safereal_type_name); msousa@558: break; msousa@558: case 64: /* lword - 64 bits */ msousa@558: add_datatype_to_candidate_list(symbol, &search_constant_type_c::lword_type_name); msousa@558: add_datatype_to_candidate_list(symbol, &search_constant_type_c::safelword_type_name); msousa@558: add_datatype_to_candidate_list(symbol, &search_constant_type_c::lint_type_name); msousa@558: add_datatype_to_candidate_list(symbol, &search_constant_type_c::safelint_type_name); msousa@558: add_datatype_to_candidate_list(symbol, &search_constant_type_c::ulint_type_name); msousa@558: add_datatype_to_candidate_list(symbol, &search_constant_type_c::safeulint_type_name); msousa@558: add_datatype_to_candidate_list(symbol, &search_constant_type_c::lreal_type_name); msousa@558: add_datatype_to_candidate_list(symbol, &search_constant_type_c::safelreal_type_name); msousa@558: break; msousa@558: default: /* if none of the above, then no valid datatype allowed... */ msousa@558: break; msousa@558: } /* switch() */ msousa@558: } /* for */ msousa@558: msousa@502: return NULL; msousa@502: } msousa@502: msousa@502: msousa@502: /* [variable_name] location ':' located_var_spec_init */ msousa@502: /* variable_name -> may be NULL ! */ msousa@502: // SYM_REF3(located_var_decl_c, variable_name, location, located_var_spec_init) msousa@502: void *fill_candidate_datatypes_c::visit(located_var_decl_c *symbol) { msousa@502: symbol->located_var_spec_init->accept(*this); msousa@502: symbol->location->accept(*this); msousa@523: if (NULL != symbol->variable_name) { msousa@523: symbol->variable_name->candidate_datatypes = symbol->location->candidate_datatypes; msousa@523: intersect_candidate_datatype_list(symbol->variable_name /*origin, dest.*/, symbol->located_var_spec_init /*with*/); msousa@523: } msousa@502: return NULL; msousa@502: } msousa@502: msousa@502: msousa@502: msousa@502: msousa@502: msousa@417: /************************************/ msousa@417: /* B 1.5 Program organization units */ msousa@417: /************************************/ msousa@417: /*********************/ msousa@417: /* B 1.5.1 Functions */ msousa@417: /*********************/ msousa@417: void *fill_candidate_datatypes_c::visit(function_declaration_c *symbol) { msousa@479: if (debug) printf("Filling candidate data types list of function %s\n", ((token_c *)(symbol->derived_function_name))->value); msousa@417: search_varfb_instance_type = new search_varfb_instance_type_c(symbol); msousa@417: symbol->var_declarations_list->accept(*this); msousa@417: symbol->function_body->accept(*this); msousa@417: delete search_varfb_instance_type; msousa@417: search_varfb_instance_type = NULL; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: /***************************/ msousa@417: /* B 1.5.2 Function blocks */ msousa@417: /***************************/ msousa@417: void *fill_candidate_datatypes_c::visit(function_block_declaration_c *symbol) { msousa@479: if (debug) printf("Filling candidate data types list of FB %s\n", ((token_c *)(symbol->fblock_name))->value); msousa@417: search_varfb_instance_type = new search_varfb_instance_type_c(symbol); msousa@417: symbol->var_declarations->accept(*this); msousa@417: symbol->fblock_body->accept(*this); msousa@417: delete search_varfb_instance_type; msousa@417: search_varfb_instance_type = NULL; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: /**********************/ msousa@417: /* B 1.5.3 - Programs */ msousa@417: /**********************/ msousa@417: void *fill_candidate_datatypes_c::visit(program_declaration_c *symbol) { msousa@479: if (debug) printf("Filling candidate data types list in program %s\n", ((token_c *)(symbol->program_type_name))->value); msousa@417: search_varfb_instance_type = new search_varfb_instance_type_c(symbol); msousa@417: symbol->var_declarations->accept(*this); msousa@417: symbol->function_block_body->accept(*this); msousa@417: delete search_varfb_instance_type; msousa@417: search_varfb_instance_type = NULL; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@417: msousa@417: /********************************/ msousa@417: /* B 1.7 Configuration elements */ msousa@417: /********************************/ msousa@417: void *fill_candidate_datatypes_c::visit(configuration_declaration_c *symbol) { msousa@417: // TODO !!! msousa@417: /* for the moment we must return NULL so semantic analysis of remaining code is not interrupted! */ msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: /****************************************/ msousa@417: /* B.2 - Language IL (Instruction List) */ msousa@417: /****************************************/ msousa@417: /***********************************/ msousa@417: /* B 2.1 Instructions and Operands */ msousa@417: /***********************************/ msousa@443: msousa@443: /*| instruction_list il_instruction */ msousa@443: // SYM_LIST(instruction_list_c) msousa@464: void *fill_candidate_datatypes_c::visit(instruction_list_c *symbol) { msousa@465: /* In order to fill the data type candidates correctly msousa@465: * in IL instruction lists containing JMPs to labels that come before the JMP instruction msousa@464: * itself, we need to run the fill candidate datatypes algorithm twice on the Instruction List. msousa@464: * e.g.: ... msousa@464: * ld 23 msousa@464: * label1:st byte_var msousa@464: * ld 34 msousa@464: * JMP label1 msousa@464: * msousa@464: * Note that the second time we run the algorithm, most of the candidate datatypes are already filled msousa@464: * in, so it will be able to produce tha correct candidate datatypes for the IL instruction referenced msousa@464: * by the label, as in the 2nd pass we already know the candidate datatypes of the JMP instruction! msousa@464: */ msousa@464: for(int j = 0; j < 2; j++) { msousa@464: for(int i = 0; i < symbol->n; i++) { msousa@464: symbol->elements[i]->accept(*this); msousa@464: } msousa@464: } msousa@464: return NULL; msousa@464: } msousa@443: msousa@443: msousa@459: msousa@443: /* | label ':' [il_incomplete_instruction] eol_list */ msousa@443: // SYM_REF2(il_instruction_c, label, il_instruction) msousa@443: // void *visit(instruction_list_c *symbol); msousa@443: void *fill_candidate_datatypes_c::visit(il_instruction_c *symbol) { msousa@448: if (NULL == symbol->il_instruction) { msousa@450: /* This empty/null il_instruction does not change the value of the current/default IL variable. msousa@450: * So it inherits the candidate_datatypes from it's previous IL instructions! msousa@450: */ msousa@459: intersect_prev_candidate_datatype_lists(symbol); msousa@448: } else { msousa@459: il_instruction_c fake_prev_il_instruction = *symbol; msousa@459: intersect_prev_candidate_datatype_lists(&fake_prev_il_instruction); msousa@459: msousa@457: if (symbol->prev_il_instruction.size() == 0) prev_il_instruction = NULL; msousa@459: else prev_il_instruction = &fake_prev_il_instruction; msousa@448: symbol->il_instruction->accept(*this); msousa@448: prev_il_instruction = NULL; msousa@448: msousa@448: /* This object has (inherits) the same candidate datatypes as the il_instruction */ msousa@467: symbol->candidate_datatypes = symbol->il_instruction->candidate_datatypes; msousa@448: } msousa@443: msousa@443: return NULL; msousa@443: } msousa@443: msousa@443: msousa@443: msousa@417: void *fill_candidate_datatypes_c::visit(il_simple_operation_c *symbol) { msousa@417: /* determine the data type of the operand */ msousa@417: if (NULL != symbol->il_operand) { msousa@417: symbol->il_operand->accept(*this); msousa@417: } msousa@417: /* recursive call to fill the candidate data types list */ msousa@417: il_operand = symbol->il_operand; msousa@417: symbol->il_simple_operator->accept(*this); msousa@417: il_operand = NULL; msousa@443: /* This object has (inherits) the same candidate datatypes as the il_simple_operator */ msousa@467: symbol->candidate_datatypes = symbol->il_simple_operator->candidate_datatypes; msousa@417: return NULL; msousa@417: } msousa@417: msousa@438: msousa@438: /* | function_name [il_operand_list] */ msousa@438: /* NOTE: The parameters 'called_function_declaration' and 'extensible_param_count' are used to pass data between the stage 3 and stage 4. */ msousa@438: // SYM_REF2(il_function_call_c, function_name, il_operand_list, symbol_c *called_function_declaration; int extensible_param_count;) msousa@417: void *fill_candidate_datatypes_c::visit(il_function_call_c *symbol) { msousa@438: /* The first parameter of a non formal function call in IL will be the 'current value' (i.e. the prev_il_instruction) msousa@438: * In order to be able to handle this without coding special cases, we will simply prepend that symbol msousa@451: * to the il_operand_list, and remove it after calling handle_function_call(). msousa@438: * msousa@438: * However, if no further paramters are given, then il_operand_list will be NULL, and we will msousa@438: * need to create a new object to hold the pointer to prev_il_instruction. msousa@438: */ msousa@438: if (NULL == symbol->il_operand_list) symbol->il_operand_list = new il_operand_list_c; msousa@438: if (NULL == symbol->il_operand_list) ERROR; msousa@438: msousa@438: symbol->il_operand_list->accept(*this); msousa@438: msousa@451: if (NULL != prev_il_instruction) { msousa@451: ((list_c *)symbol->il_operand_list)->insert_element(prev_il_instruction, 0); msousa@451: msousa@451: generic_function_call_t fcall_param = { msousa@451: /* fcall_param.function_name = */ symbol->function_name, msousa@451: /* fcall_param.nonformal_operand_list = */ symbol->il_operand_list, msousa@451: /* fcall_param.formal_operand_list = */ NULL, msousa@451: /* enum {POU_FB, POU_function} POU_type = */ generic_function_call_t::POU_function, msousa@451: /* fcall_param.candidate_functions = */ symbol->candidate_functions, msousa@451: /* fcall_param.called_function_declaration = */ symbol->called_function_declaration, msousa@451: /* fcall_param.extensible_param_count = */ symbol->extensible_param_count msousa@451: }; msousa@451: handle_function_call(symbol, fcall_param); msousa@451: msousa@451: /* Undo the changes to the abstract syntax tree we made above... */ msousa@451: ((list_c *)symbol->il_operand_list)->remove_element(0); msousa@451: } msousa@451: msousa@451: /* Undo the changes to the abstract syntax tree we made above... */ msousa@451: if (((list_c *)symbol->il_operand_list)->n == 0) { msousa@451: /* if the list becomes empty, then that means that it did not exist before we made these changes, so we delete it! */ msousa@451: delete symbol->il_operand_list; msousa@451: symbol->il_operand_list = NULL; msousa@451: } msousa@451: msousa@438: if (debug) std::cout << "il_function_call_c [" << symbol->candidate_datatypes.size() << "] result.\n"; msousa@438: return NULL; msousa@438: } msousa@438: msousa@438: msousa@417: /* | il_expr_operator '(' [il_operand] eol_list [simple_instr_list] ')' */ msousa@417: // SYM_REF3(il_expression_c, il_expr_operator, il_operand, simple_instr_list); msousa@417: void *fill_candidate_datatypes_c::visit(il_expression_c *symbol) { msousa@453: symbol_c *prev_il_instruction_backup = prev_il_instruction; msousa@453: msousa@417: if (NULL != symbol->il_operand) msousa@417: symbol->il_operand->accept(*this); msousa@453: msousa@453: if(symbol->simple_instr_list != NULL) msousa@417: symbol->simple_instr_list->accept(*this); msousa@417: msousa@417: /* Now check the if the data type semantics of operation are correct, */ msousa@452: il_operand = symbol->simple_instr_list; msousa@417: prev_il_instruction = prev_il_instruction_backup; msousa@417: symbol->il_expr_operator->accept(*this); msousa@417: il_operand = NULL; msousa@454: msousa@454: /* This object has the same candidate datatypes as the il_expr_operator. */ msousa@467: symbol->candidate_datatypes = symbol->il_expr_operator->candidate_datatypes; msousa@417: return NULL; msousa@417: } msousa@417: msousa@462: msousa@417: void *fill_candidate_datatypes_c::visit(il_jump_operation_c *symbol) { msousa@417: /* recursive call to fill the candidate data types list */ msousa@417: il_operand = NULL; msousa@417: symbol->il_jump_operator->accept(*this); msousa@417: il_operand = NULL; msousa@462: /* This object has the same candidate datatypes as the il_jump_operator. */ msousa@467: symbol->candidate_datatypes = symbol->il_jump_operator->candidate_datatypes; msousa@417: return NULL; msousa@417: } msousa@417: msousa@439: msousa@439: /* il_call_operator prev_declared_fb_name msousa@439: * | il_call_operator prev_declared_fb_name '(' ')' msousa@439: * | il_call_operator prev_declared_fb_name '(' eol_list ')' msousa@439: * | il_call_operator prev_declared_fb_name '(' il_operand_list ')' msousa@439: * | il_call_operator prev_declared_fb_name '(' eol_list il_param_list ')' msousa@439: */ msousa@439: /* NOTE: The parameter 'called_fb_declaration'is used to pass data between stage 3 and stage4 (although currently it is not used in stage 4 */ msousa@439: // SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list, symbol_c *called_fb_declaration) msousa@417: void *fill_candidate_datatypes_c::visit(il_fb_call_c *symbol) { msousa@455: /* We do not call msousa@455: * fb_decl = search_varfb_instance_type->get_basetype_decl(symbol->fb_name); msousa@455: * because we want to make sure it is a FB instance, and not some other data type... msousa@455: */ msousa@455: symbol_c *fb_type_id = search_varfb_instance_type->get_basetype_id(symbol->fb_name); msousa@455: /* Although a call to a non-declared FB is a semantic error, this is currently caught by stage 2! */ msousa@455: if (NULL == fb_type_id) ERROR; msousa@455: msousa@455: function_block_declaration_c *fb_decl = function_block_type_symtable.find_value(fb_type_id); msousa@455: if (function_block_type_symtable.end_value() == fb_decl) msousa@455: /* The fb_name not the name of a FB instance. Most probably it is the name of a variable of some other type. */ msousa@455: fb_decl = NULL; msousa@455: msousa@439: /* Although a call to a non-declared FB is a semantic error, this is currently caught by stage 2! */ msousa@439: if (NULL == fb_decl) ERROR; msousa@439: msousa@455: if (symbol-> il_param_list != NULL) symbol->il_param_list->accept(*this); msousa@455: if (symbol->il_operand_list != NULL) symbol->il_operand_list->accept(*this); msousa@439: msousa@439: /* The print_datatypes_error_c does not rely on this called_fb_declaration pointer being != NULL to conclude that msousa@439: * we have a datat type incompatibility error, so setting it to the correct fb_decl is actually safe, msousa@439: * as the compiler will never reach the compilation stage! msousa@439: */ msousa@439: symbol->called_fb_declaration = fb_decl; msousa@439: msousa@455: /* Let the il_call_operator (CAL, CALC, or CALCN) determine the candidate datatypes of the il_fb_call_c... */ msousa@455: /* NOTE: We ignore whether the call is 'compatible' or not when filling in the candidate datatypes list. msousa@455: * Even if it is not compatible, we fill in the candidate datatypes list correctly so that the following msousa@455: * IL instructions may be handled correctly and debuged. msousa@455: * Doing this is actually safe, as the parameter_list will still contain errors that will be found by msousa@455: * print_datatypes_error_c, so the code will never reach stage 4! msousa@455: */ msousa@455: symbol->il_call_operator->accept(*this); msousa@467: symbol->candidate_datatypes = symbol->il_call_operator->candidate_datatypes; msousa@450: msousa@439: if (debug) std::cout << "FB [] ==> " << symbol->candidate_datatypes.size() << " result.\n"; msousa@439: return NULL; msousa@439: } msousa@439: msousa@417: msousa@438: /* | function_name '(' eol_list [il_param_list] ')' */ msousa@438: /* NOTE: The parameter 'called_function_declaration' is used to pass data between the stage 3 and stage 4. */ msousa@438: // SYM_REF2(il_formal_funct_call_c, function_name, il_param_list, symbol_c *called_function_declaration; int extensible_param_count;) msousa@417: void *fill_candidate_datatypes_c::visit(il_formal_funct_call_c *symbol) { msousa@438: symbol->il_param_list->accept(*this); msousa@438: msousa@438: generic_function_call_t fcall_param = { msousa@441: /* fcall_param.function_name = */ symbol->function_name, msousa@441: /* fcall_param.nonformal_operand_list = */ NULL, msousa@441: /* fcall_param.formal_operand_list = */ symbol->il_param_list, msousa@441: /* enum {POU_FB, POU_function} POU_type = */ generic_function_call_t::POU_function, msousa@441: /* fcall_param.candidate_functions = */ symbol->candidate_functions, msousa@441: /* fcall_param.called_function_declaration = */ symbol->called_function_declaration, msousa@441: /* fcall_param.extensible_param_count = */ symbol->extensible_param_count msousa@438: }; msousa@438: handle_function_call(symbol, fcall_param); msousa@438: msousa@438: if (debug) std::cout << "il_formal_funct_call_c [" << symbol->candidate_datatypes.size() << "] result.\n"; msousa@438: return NULL; msousa@417: } msousa@417: msousa@452: msousa@452: // void *visit(il_operand_list_c *symbol); msousa@452: msousa@452: msousa@452: /* | simple_instr_list il_simple_instruction */ msousa@452: /* This object is referenced by il_expression_c objects */ msousa@452: void *fill_candidate_datatypes_c::visit(simple_instr_list_c *symbol) { msousa@453: if (symbol->n <= 0) msousa@453: return NULL; /* List is empty! Nothing to do. */ msousa@453: msousa@453: for(int i = 0; i < symbol->n; i++) msousa@452: symbol->elements[i]->accept(*this); msousa@453: msousa@453: /* This object has (inherits) the same candidate datatypes as the last il_instruction */ msousa@467: symbol->candidate_datatypes = symbol->elements[symbol->n-1]->candidate_datatypes; msousa@452: msousa@452: if (debug) std::cout << "simple_instr_list_c [" << symbol->candidate_datatypes.size() << "] result.\n"; msousa@452: return NULL; msousa@452: } msousa@452: msousa@457: msousa@457: msousa@457: msousa@453: // SYM_REF1(il_simple_instruction_c, il_simple_instruction, symbol_c *prev_il_instruction;) msousa@453: void *fill_candidate_datatypes_c::visit(il_simple_instruction_c *symbol) { msousa@459: if (symbol->prev_il_instruction.size() > 1) ERROR; /* There should be no labeled insructions inside an IL expression! */ msousa@457: if (symbol->prev_il_instruction.size() == 0) prev_il_instruction = NULL; msousa@457: else prev_il_instruction = symbol->prev_il_instruction[0]; msousa@453: symbol->il_simple_instruction->accept(*this); msousa@453: prev_il_instruction = NULL; msousa@453: msousa@453: /* This object has (inherits) the same candidate datatypes as the il_simple_instruction it points to */ msousa@467: symbol->candidate_datatypes = symbol->il_simple_instruction->candidate_datatypes; msousa@453: return NULL; msousa@453: } msousa@453: msousa@453: msousa@417: /* msousa@417: void *visit(il_param_list_c *symbol); msousa@417: void *visit(il_param_assignment_c *symbol); msousa@417: void *visit(il_param_out_assignment_c *symbol); msousa@417: */ msousa@417: msousa@417: /*******************/ msousa@417: /* B 2.2 Operators */ msousa@417: /*******************/ msousa@417: void *fill_candidate_datatypes_c::visit(LD_operator_c *symbol) { msousa@417: for(unsigned int i = 0; i < il_operand->candidate_datatypes.size(); i++) { msousa@465: add_datatype_to_candidate_list(symbol, il_operand->candidate_datatypes[i]); msousa@417: } msousa@417: if (debug) std::cout << "LD [" << il_operand->candidate_datatypes.size() << "] ==> " << symbol->candidate_datatypes.size() << " result.\n"; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(LDN_operator_c *symbol) { msousa@417: for(unsigned int i = 0; i < il_operand->candidate_datatypes.size(); i++) { msousa@417: if (is_ANY_BIT_compatible(il_operand->candidate_datatypes[i])) msousa@465: add_datatype_to_candidate_list(symbol, il_operand->candidate_datatypes[i]); msousa@417: } msousa@417: if (debug) std::cout << "LDN [" << il_operand->candidate_datatypes.size() << "] ==> " << symbol->candidate_datatypes.size() << " result.\n"; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(ST_operator_c *symbol) { msousa@417: symbol_c *prev_instruction_type, *operand_type; msousa@417: msousa@417: if (NULL == prev_il_instruction) return NULL; msousa@417: for (unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) { msousa@417: for(unsigned int j = 0; j < il_operand->candidate_datatypes.size(); j++) { msousa@417: prev_instruction_type = prev_il_instruction->candidate_datatypes[i]; msousa@417: operand_type = il_operand->candidate_datatypes[j]; conti@440: if (is_type_equal(prev_instruction_type, operand_type)) msousa@465: add_datatype_to_candidate_list(symbol, prev_instruction_type); msousa@417: } msousa@417: } msousa@417: if (debug) std::cout << "ST [" << prev_il_instruction->candidate_datatypes.size() << "," << il_operand->candidate_datatypes.size() << "] ==> " << symbol->candidate_datatypes.size() << " result.\n"; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(STN_operator_c *symbol) { msousa@417: symbol_c *prev_instruction_type, *operand_type; msousa@417: msousa@417: if (NULL == prev_il_instruction) return NULL; msousa@417: for (unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) { msousa@417: for(unsigned int j = 0; j < il_operand->candidate_datatypes.size(); j++) { msousa@417: prev_instruction_type = prev_il_instruction->candidate_datatypes[i]; msousa@417: operand_type = il_operand->candidate_datatypes[j]; conti@440: if (is_type_equal(prev_instruction_type,operand_type) && is_ANY_BIT_compatible(operand_type)) msousa@465: add_datatype_to_candidate_list(symbol, prev_instruction_type); msousa@417: } msousa@417: } msousa@417: if (debug) std::cout << "STN [" << prev_il_instruction->candidate_datatypes.size() << "," << il_operand->candidate_datatypes.size() << "] ==> " << symbol->candidate_datatypes.size() << " result.\n"; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(NOT_operator_c *symbol) { msousa@470: /* NOTE: the standard allows syntax in which the NOT operator is followed by an optional msousa@470: * NOT [] msousa@470: * However, it does not define the semantic of the NOT operation when the is specified. msousa@470: * We therefore consider it an error if an il_operand is specified! msousa@612: * We do not need to generate an error message. This error will be caught somewhere else! msousa@470: */ msousa@470: if (NULL == prev_il_instruction) return NULL; msousa@470: for (unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) { msousa@470: if (is_ANY_BIT_compatible(prev_il_instruction->candidate_datatypes[i])) msousa@470: add_datatype_to_candidate_list(symbol, prev_il_instruction->candidate_datatypes[i]); msousa@470: } msousa@470: if (debug) std::cout << "NOT_operator [" << prev_il_instruction->candidate_datatypes.size() << "] ==> " << symbol->candidate_datatypes.size() << " result.\n"; msousa@417: return NULL; msousa@417: } msousa@417: msousa@447: msousa@417: void *fill_candidate_datatypes_c::visit(S_operator_c *symbol) { msousa@447: /* TODO: what if this is a FB call ?? */ msousa@417: symbol_c *prev_instruction_type, *operand_type; msousa@417: msousa@417: if (NULL == prev_il_instruction) return NULL; msousa@417: for (unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) { msousa@417: for(unsigned int j = 0; j < il_operand->candidate_datatypes.size(); j++) { msousa@417: prev_instruction_type = prev_il_instruction->candidate_datatypes[i]; msousa@417: operand_type = il_operand->candidate_datatypes[j]; msousa@473: /* TODO: I believe the following is wrong! The data types of prev_instruction_type and operand_type DO NOT have to be equal. msousa@473: * the prev_instruction_type MUST be BOOL compatible. msousa@473: * I am not too sure about operand_type, does it have to be BOOL compatible, or can it be ANY_BIT compatible? Must check! msousa@473: */ conti@440: if (is_type_equal(prev_instruction_type,operand_type) && is_ANY_BOOL_compatible(operand_type)) msousa@465: add_datatype_to_candidate_list(symbol, prev_instruction_type); msousa@417: } msousa@417: } msousa@417: if (debug) std::cout << "S [" << prev_il_instruction->candidate_datatypes.size() << "," << il_operand->candidate_datatypes.size() << "] ==> " << symbol->candidate_datatypes.size() << " result.\n"; msousa@417: return NULL; msousa@417: } msousa@417: msousa@447: msousa@417: void *fill_candidate_datatypes_c::visit(R_operator_c *symbol) { msousa@447: /* TODO: what if this is a FB call ?? */ msousa@417: symbol_c *prev_instruction_type, *operand_type; msousa@417: msousa@417: if (NULL == prev_il_instruction) return NULL; msousa@417: for (unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) { msousa@417: for(unsigned int j = 0; j < il_operand->candidate_datatypes.size(); j++) { msousa@417: prev_instruction_type = prev_il_instruction->candidate_datatypes[i]; msousa@417: operand_type = il_operand->candidate_datatypes[j]; msousa@473: /* TODO: I believe the following is wrong! The data types of prev_instruction_type and operand_type DO NOT have to be equal. msousa@473: * the prev_instruction_type MUST be BOOL compatible. msousa@473: * I am not too sure about operand_type, does it have to be BOOL compatible, or can it be ANY_BIT compatible? Must check! msousa@473: */ conti@440: if (is_type_equal(prev_instruction_type,operand_type) && is_ANY_BOOL_compatible(operand_type)) msousa@465: add_datatype_to_candidate_list(symbol, prev_instruction_type); msousa@417: } msousa@417: } msousa@417: if (debug) std::cout << "R [" << prev_il_instruction->candidate_datatypes.size() << "," << il_operand->candidate_datatypes.size() << "] ==> " << symbol->candidate_datatypes.size() << " result.\n"; msousa@417: return NULL; msousa@417: } msousa@417: msousa@447: msousa@489: void *fill_candidate_datatypes_c::visit( S1_operator_c *symbol) {return handle_implicit_il_fb_call(symbol, "S1", symbol->called_fb_declaration);} msousa@489: void *fill_candidate_datatypes_c::visit( R1_operator_c *symbol) {return handle_implicit_il_fb_call(symbol, "R1", symbol->called_fb_declaration);} msousa@489: void *fill_candidate_datatypes_c::visit( CLK_operator_c *symbol) {return handle_implicit_il_fb_call(symbol, "CLK", symbol->called_fb_declaration);} msousa@489: void *fill_candidate_datatypes_c::visit( CU_operator_c *symbol) {return handle_implicit_il_fb_call(symbol, "CU", symbol->called_fb_declaration);} msousa@489: void *fill_candidate_datatypes_c::visit( CD_operator_c *symbol) {return handle_implicit_il_fb_call(symbol, "CD", symbol->called_fb_declaration);} msousa@489: void *fill_candidate_datatypes_c::visit( PV_operator_c *symbol) {return handle_implicit_il_fb_call(symbol, "PV", symbol->called_fb_declaration);} msousa@489: void *fill_candidate_datatypes_c::visit( IN_operator_c *symbol) {return handle_implicit_il_fb_call(symbol, "IN", symbol->called_fb_declaration);} msousa@489: void *fill_candidate_datatypes_c::visit( PT_operator_c *symbol) {return handle_implicit_il_fb_call(symbol, "PT", symbol->called_fb_declaration);} msousa@447: msousa@483: void *fill_candidate_datatypes_c::visit( AND_operator_c *symbol) {return handle_binary_operator(widen_AND_table, symbol, prev_il_instruction, il_operand);} msousa@489: void *fill_candidate_datatypes_c::visit( OR_operator_c *symbol) {return handle_binary_operator( widen_OR_table, symbol, prev_il_instruction, il_operand);} msousa@483: void *fill_candidate_datatypes_c::visit( XOR_operator_c *symbol) {return handle_binary_operator(widen_XOR_table, symbol, prev_il_instruction, il_operand);} msousa@483: void *fill_candidate_datatypes_c::visit(ANDN_operator_c *symbol) {return handle_binary_operator(widen_AND_table, symbol, prev_il_instruction, il_operand);} msousa@483: void *fill_candidate_datatypes_c::visit( ORN_operator_c *symbol) {return handle_binary_operator( widen_OR_table, symbol, prev_il_instruction, il_operand);} msousa@483: void *fill_candidate_datatypes_c::visit(XORN_operator_c *symbol) {return handle_binary_operator(widen_XOR_table, symbol, prev_il_instruction, il_operand);} msousa@417: msousa@489: void *fill_candidate_datatypes_c::visit( ADD_operator_c *symbol) {return handle_binary_operator(widen_ADD_table, symbol, prev_il_instruction, il_operand);} msousa@489: void *fill_candidate_datatypes_c::visit( SUB_operator_c *symbol) {return handle_binary_operator(widen_SUB_table, symbol, prev_il_instruction, il_operand);} msousa@489: void *fill_candidate_datatypes_c::visit( MUL_operator_c *symbol) {return handle_binary_operator(widen_MUL_table, symbol, prev_il_instruction, il_operand);} msousa@489: void *fill_candidate_datatypes_c::visit( DIV_operator_c *symbol) {return handle_binary_operator(widen_DIV_table, symbol, prev_il_instruction, il_operand);} msousa@489: void *fill_candidate_datatypes_c::visit( MOD_operator_c *symbol) {return handle_binary_operator(widen_MOD_table, symbol, prev_il_instruction, il_operand);} msousa@489: msousa@489: void *fill_candidate_datatypes_c::visit( GT_operator_c *symbol) {return handle_binary_operator(widen_CMP_table, symbol, prev_il_instruction, il_operand);} msousa@489: void *fill_candidate_datatypes_c::visit( GE_operator_c *symbol) {return handle_binary_operator(widen_CMP_table, symbol, prev_il_instruction, il_operand);} msousa@489: void *fill_candidate_datatypes_c::visit( EQ_operator_c *symbol) {return handle_binary_operator(widen_CMP_table, symbol, prev_il_instruction, il_operand);} msousa@489: void *fill_candidate_datatypes_c::visit( LT_operator_c *symbol) {return handle_binary_operator(widen_CMP_table, symbol, prev_il_instruction, il_operand);} msousa@489: void *fill_candidate_datatypes_c::visit( LE_operator_c *symbol) {return handle_binary_operator(widen_CMP_table, symbol, prev_il_instruction, il_operand);} msousa@489: void *fill_candidate_datatypes_c::visit( NE_operator_c *symbol) {return handle_binary_operator(widen_CMP_table, symbol, prev_il_instruction, il_operand);} msousa@484: msousa@484: msousa@417: msousa@487: void *fill_candidate_datatypes_c::handle_conditional_il_flow_control_operator(symbol_c *symbol) { msousa@417: if (NULL == prev_il_instruction) return NULL; msousa@417: for (unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) { msousa@472: if (is_ANY_BOOL_compatible(prev_il_instruction->candidate_datatypes[i])) msousa@465: add_datatype_to_candidate_list(symbol, prev_il_instruction->candidate_datatypes[i]); msousa@417: } msousa@487: return NULL; msousa@487: } msousa@487: msousa@489: void *fill_candidate_datatypes_c::visit( CAL_operator_c *symbol) {if (NULL != prev_il_instruction) symbol->candidate_datatypes = prev_il_instruction->candidate_datatypes; return NULL;} msousa@489: void *fill_candidate_datatypes_c::visit( RET_operator_c *symbol) {if (NULL != prev_il_instruction) symbol->candidate_datatypes = prev_il_instruction->candidate_datatypes; return NULL;} msousa@489: void *fill_candidate_datatypes_c::visit( JMP_operator_c *symbol) {if (NULL != prev_il_instruction) symbol->candidate_datatypes = prev_il_instruction->candidate_datatypes; return NULL;} msousa@487: void *fill_candidate_datatypes_c::visit( CALC_operator_c *symbol) {return handle_conditional_il_flow_control_operator(symbol);} msousa@487: void *fill_candidate_datatypes_c::visit(CALCN_operator_c *symbol) {return handle_conditional_il_flow_control_operator(symbol);} msousa@487: void *fill_candidate_datatypes_c::visit( RETC_operator_c *symbol) {return handle_conditional_il_flow_control_operator(symbol);} msousa@487: void *fill_candidate_datatypes_c::visit(RETCN_operator_c *symbol) {return handle_conditional_il_flow_control_operator(symbol);} msousa@487: void *fill_candidate_datatypes_c::visit( JMPC_operator_c *symbol) {return handle_conditional_il_flow_control_operator(symbol);} msousa@487: void *fill_candidate_datatypes_c::visit(JMPCN_operator_c *symbol) {return handle_conditional_il_flow_control_operator(symbol);} msousa@487: msousa@487: msousa@487: msousa@487: msousa@417: /* Symbol class handled together with function call checks */ msousa@417: // void *visit(il_assign_operator_c *symbol, variable_name); msousa@417: /* Symbol class handled together with function call checks */ msousa@417: // void *visit(il_assign_operator_c *symbol, option, variable_name); msousa@417: msousa@417: /***************************************/ msousa@417: /* B.3 - Language ST (Structured Text) */ msousa@417: /***************************************/ msousa@417: /***********************/ msousa@417: /* B 3.1 - Expressions */ msousa@417: /***********************/ msousa@652: void *fill_candidate_datatypes_c::visit( or_expression_c *symbol) {return handle_binary_expression (widen_OR_table, symbol, symbol->l_exp, symbol->r_exp);} msousa@652: void *fill_candidate_datatypes_c::visit( xor_expression_c *symbol) {return handle_binary_expression (widen_XOR_table, symbol, symbol->l_exp, symbol->r_exp);} msousa@652: void *fill_candidate_datatypes_c::visit( and_expression_c *symbol) {return handle_binary_expression (widen_AND_table, symbol, symbol->l_exp, symbol->r_exp);} msousa@652: msousa@652: void *fill_candidate_datatypes_c::visit( equ_expression_c *symbol) {return handle_equality_comparison(widen_CMP_table, symbol, symbol->l_exp, symbol->r_exp);} msousa@652: void *fill_candidate_datatypes_c::visit(notequ_expression_c *symbol) {return handle_equality_comparison(widen_CMP_table, symbol, symbol->l_exp, symbol->r_exp);} msousa@652: void *fill_candidate_datatypes_c::visit( lt_expression_c *symbol) {return handle_binary_expression (widen_CMP_table, symbol, symbol->l_exp, symbol->r_exp);} msousa@652: void *fill_candidate_datatypes_c::visit( gt_expression_c *symbol) {return handle_binary_expression (widen_CMP_table, symbol, symbol->l_exp, symbol->r_exp);} msousa@652: void *fill_candidate_datatypes_c::visit( le_expression_c *symbol) {return handle_binary_expression (widen_CMP_table, symbol, symbol->l_exp, symbol->r_exp);} msousa@652: void *fill_candidate_datatypes_c::visit( ge_expression_c *symbol) {return handle_binary_expression (widen_CMP_table, symbol, symbol->l_exp, symbol->r_exp);} msousa@652: msousa@652: void *fill_candidate_datatypes_c::visit( add_expression_c *symbol) {return handle_binary_expression (widen_ADD_table, symbol, symbol->l_exp, symbol->r_exp);} msousa@652: void *fill_candidate_datatypes_c::visit( sub_expression_c *symbol) {return handle_binary_expression (widen_SUB_table, symbol, symbol->l_exp, symbol->r_exp);} msousa@652: void *fill_candidate_datatypes_c::visit( mul_expression_c *symbol) {return handle_binary_expression (widen_MUL_table, symbol, symbol->l_exp, symbol->r_exp);} msousa@652: void *fill_candidate_datatypes_c::visit( div_expression_c *symbol) {return handle_binary_expression (widen_DIV_table, symbol, symbol->l_exp, symbol->r_exp);} msousa@652: void *fill_candidate_datatypes_c::visit( mod_expression_c *symbol) {return handle_binary_expression (widen_MOD_table, symbol, symbol->l_exp, symbol->r_exp);} msousa@652: void *fill_candidate_datatypes_c::visit( power_expression_c *symbol) {return handle_binary_expression (widen_EXPT_table, symbol, symbol->l_exp, symbol->r_exp);} msousa@417: msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(neg_expression_c *symbol) { msousa@435: /* NOTE: The standard defines the syntax for this 'negation' operation, but msousa@435: * does not define the its semantics. msousa@435: * msousa@435: * We could be tempted to consider that the semantics of the msousa@435: * 'negation' operation are similar/identical to the semantics of the msousa@435: * SUB expression/operation. This would include assuming that the msousa@435: * possible datatypes for the 'negation' operation is also msousa@435: * the same as those for the SUB expression/operation, namely ANY_MAGNITUDE. msousa@435: * msousa@435: * However, this would then mean that the following ST code would be msousa@435: * syntactically and semantically correct: msousa@650: * VAR uint_var : UINT END_VAR; msousa@435: * uint_var := - (uint_var); msousa@435: * msousa@650: * Assuming uint_var is not 0, the standard states that the above code should result in a msousa@650: * runtime error since the operation will result in an overflow. Since the above operation msousa@650: * is only valid when uint_var=0, it would probably make more sense for the programmer to msousa@650: * use if (uint_var=0) ..., so we will simply assume that the above statement simply msousa@650: * does not make sense in any situation (whether or not uint_var is 0), and therefore msousa@650: * we will not allow it. msousa@650: * (Notice that doing so does not ago against the standard, as the standard does not msousa@650: * explicitly define the semantics of the NEG operator, nor the data types it may accept msousa@650: * as input. We are simply assuming that the NEG operator may not be applied to unsigned msousa@650: * ANY_NUM data types!). msousa@435: * msousa@435: * It is much easier for the compiler to detect this at compile time, msousa@435: * and it is probably safer to the resulting code too. msousa@435: * msousa@435: * To detect these tyes of errors at compile time, the easisest solution msousa@435: * is to only allow ANY_NUM datatytpes that are signed. msousa@435: * So, that is what we do here! msousa@650: * msousa@650: * NOTE: The above argument also applies to the neg_integer_c method! msousa@435: */ msousa@417: symbol->exp->accept(*this); msousa@417: for (unsigned int i = 0; i < symbol->exp->candidate_datatypes.size(); i++) { msousa@435: if (is_ANY_signed_MAGNITUDE_compatible(symbol->exp->candidate_datatypes[i])) msousa@465: add_datatype_to_candidate_list(symbol, symbol->exp->candidate_datatypes[i]); msousa@417: } msousa@417: if (debug) std::cout << "neg [" << symbol->exp->candidate_datatypes.size() << "] ==> " << symbol->candidate_datatypes.size() << " result.\n"; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(not_expression_c *symbol) { msousa@417: symbol->exp->accept(*this); msousa@417: for (unsigned int i = 0; i < symbol->exp->candidate_datatypes.size(); i++) { msousa@417: if (is_ANY_BIT_compatible(symbol->exp->candidate_datatypes[i])) msousa@465: add_datatype_to_candidate_list(symbol, symbol->exp->candidate_datatypes[i]); msousa@417: } msousa@417: if (debug) std::cout << "not [" << symbol->exp->candidate_datatypes.size() << "] ==> " << symbol->candidate_datatypes.size() << " result.\n"; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(function_invocation_c *symbol) { msousa@438: if (NULL != symbol->formal_param_list) symbol-> formal_param_list->accept(*this); msousa@438: else if (NULL != symbol->nonformal_param_list) symbol->nonformal_param_list->accept(*this); msousa@417: else ERROR; msousa@438: msousa@438: generic_function_call_t fcall_param = { msousa@441: /* fcall_param.function_name = */ symbol->function_name, msousa@441: /* fcall_param.nonformal_operand_list = */ symbol->nonformal_param_list, msousa@441: /* fcall_param.formal_operand_list = */ symbol->formal_param_list, msousa@441: /* enum {POU_FB, POU_function} POU_type = */ generic_function_call_t::POU_function, msousa@441: /* fcall_param.candidate_functions = */ symbol->candidate_functions, msousa@441: /* fcall_param.called_function_declaration = */ symbol->called_function_declaration, msousa@441: /* fcall_param.extensible_param_count = */ symbol->extensible_param_count msousa@438: }; msousa@438: handle_function_call(symbol, fcall_param); msousa@438: msousa@438: if (debug) std::cout << "function_invocation_c [" << symbol->candidate_datatypes.size() << "] result.\n"; msousa@417: return NULL; msousa@417: } msousa@417: msousa@421: msousa@421: msousa@417: /********************/ msousa@417: /* B 3.2 Statements */ msousa@417: /********************/ msousa@417: // SYM_LIST(statement_list_c) msousa@417: /* The visitor of the base class search_visitor_c will handle calling each instruction in the list. msousa@417: * We do not need to do anything here... msousa@417: */ msousa@417: // void *fill_candidate_datatypes_c::visit(statement_list_c *symbol) msousa@417: msousa@417: msousa@417: /*********************************/ msousa@417: /* B 3.2.1 Assignment Statements */ msousa@417: /*********************************/ msousa@417: void *fill_candidate_datatypes_c::visit(assignment_statement_c *symbol) { msousa@417: symbol_c *left_type, *right_type; msousa@417: msousa@417: symbol->l_exp->accept(*this); msousa@417: symbol->r_exp->accept(*this); msousa@417: for (unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) { msousa@417: for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) { msousa@417: left_type = symbol->l_exp->candidate_datatypes[i]; msousa@417: right_type = symbol->r_exp->candidate_datatypes[j]; msousa@417: if (is_type_equal(left_type, right_type)) msousa@465: add_datatype_to_candidate_list(symbol, left_type); msousa@417: } msousa@417: } msousa@417: if (debug) std::cout << ":= [" << symbol->l_exp->candidate_datatypes.size() << "," << symbol->r_exp->candidate_datatypes.size() << "] ==> " << symbol->candidate_datatypes.size() << " result.\n"; msousa@417: return NULL; msousa@417: } msousa@417: conti@418: /*****************************************/ conti@418: /* B 3.2.2 Subprogram Control Statements */ conti@418: /*****************************************/ conti@418: void *fill_candidate_datatypes_c::visit(fb_invocation_c *symbol) { msousa@455: symbol_c *fb_type_id = search_varfb_instance_type->get_basetype_id(symbol->fb_name); msousa@455: /* Although a call to a non-declared FB is a semantic error, this is currently caught by stage 2! */ msousa@455: if (NULL == fb_type_id) ERROR; msousa@455: msousa@455: function_block_declaration_c *fb_decl = function_block_type_symtable.find_value(fb_type_id); msousa@455: if (function_block_type_symtable.end_value() == fb_decl) msousa@455: /* The fb_name not the name of a FB instance. Most probably it is the name of a variable of some other type. */ msousa@455: fb_decl = NULL; msousa@455: msousa@424: /* Although a call to a non-declared FB is a semantic error, this is currently caught by stage 2! */ conti@418: if (NULL == fb_decl) ERROR; msousa@455: msousa@455: if (symbol-> formal_param_list != NULL) symbol->formal_param_list->accept(*this); msousa@455: if (symbol->nonformal_param_list != NULL) symbol->nonformal_param_list->accept(*this); msousa@424: msousa@431: /* The print_datatypes_error_c does not rely on this called_fb_declaration pointer being != NULL to conclude that msousa@431: * we have a datat type incompatibility error, so setting it to the correct fb_decl is actually safe, msousa@431: * as the compiler will never reach the compilation stage! msousa@431: */ msousa@431: symbol->called_fb_declaration = fb_decl; msousa@424: conti@418: if (debug) std::cout << "FB [] ==> " << symbol->candidate_datatypes.size() << " result.\n"; conti@418: return NULL; conti@418: } conti@418: msousa@417: msousa@417: msousa@417: /********************************/ msousa@417: /* B 3.2.3 Selection Statements */ msousa@417: /********************************/ msousa@417: void *fill_candidate_datatypes_c::visit(if_statement_c *symbol) { msousa@417: symbol->expression->accept(*this); msousa@417: if (NULL != symbol->statement_list) msousa@417: symbol->statement_list->accept(*this); msousa@417: if (NULL != symbol->elseif_statement_list) msousa@417: symbol->elseif_statement_list->accept(*this); msousa@417: if (NULL != symbol->else_statement_list) msousa@417: symbol->else_statement_list->accept(*this); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(elseif_statement_c *symbol) { msousa@417: symbol->expression->accept(*this); msousa@417: if (NULL != symbol->statement_list) msousa@417: symbol->statement_list->accept(*this); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: /* CASE expression OF case_element_list ELSE statement_list END_CASE */ msousa@417: // SYM_REF3(case_statement_c, expression, case_element_list, statement_list) msousa@417: void *fill_candidate_datatypes_c::visit(case_statement_c *symbol) { msousa@417: symbol->expression->accept(*this); msousa@417: if (NULL != symbol->case_element_list) msousa@417: symbol->case_element_list->accept(*this); msousa@417: if (NULL != symbol->statement_list) msousa@417: symbol->statement_list->accept(*this); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@417: /* helper symbol for case_statement */ msousa@417: // SYM_LIST(case_element_list_c) msousa@417: /* NOTE: visitor method for case_element_list_c is not required since we inherit from iterator_visitor_c */ msousa@417: msousa@417: /* case_list ':' statement_list */ msousa@417: // SYM_REF2(case_element_c, case_list, statement_list) msousa@417: /* NOTE: visitor method for case_element_c is not required since we inherit from iterator_visitor_c */ msousa@417: msousa@417: // SYM_LIST(case_list_c) msousa@417: /* NOTE: visitor method for case_list_c is not required since we inherit from iterator_visitor_c */ msousa@417: msousa@417: /********************************/ msousa@417: /* B 3.2.4 Iteration Statements */ msousa@417: /********************************/ msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(for_statement_c *symbol) { msousa@417: symbol->control_variable->accept(*this); msousa@417: symbol->beg_expression->accept(*this); msousa@417: symbol->end_expression->accept(*this); msousa@417: if (NULL != symbol->by_expression) msousa@417: symbol->by_expression->accept(*this); msousa@417: if (NULL != symbol->statement_list) msousa@417: symbol->statement_list->accept(*this); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(while_statement_c *symbol) { msousa@417: symbol->expression->accept(*this); msousa@417: if (NULL != symbol->statement_list) msousa@417: symbol->statement_list->accept(*this); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(repeat_statement_c *symbol) { msousa@417: symbol->expression->accept(*this); msousa@417: if (NULL != symbol->statement_list) msousa@417: symbol->statement_list->accept(*this); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@417: msousa@417: msousa@417: msousa@417: