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@417: /* msousa@417: * Fill candidate list of data types for all symbols msousa@417: */ msousa@417: 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@417: /* set to 1 to see debug info during execution */ msousa@417: 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@417: 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@417: if ((typeid(*left_type) == typeid(*widen_table[k].left)) && (typeid(*right_type) == typeid(*widen_table[k].right))) msousa@417: return widen_table[k].result; msousa@417: return NULL; msousa@417: } msousa@417: msousa@421: 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@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@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@420: bool fill_candidate_datatypes_c::match_formal_call(symbol_c *f_call, symbol_c *f_decl) { 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@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@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@438: fcall_data.candidate_functions.push_back(f_decl); msousa@438: fcall-> candidate_datatypes.push_back(returned_parameter_type); 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@438: /* TODO msousa@438: * call int search_in_datatype_list(symbol_c *datatype, std::vector candidate_datatypes); msousa@438: * instead of using for loop! msousa@438: */ msousa@438: unsigned int k; msousa@438: returned_parameter_type = base_type(f_decl->type_name); msousa@438: for(k = 0; k < fcall->candidate_datatypes.size(); k++) { msousa@438: if (is_type_equal(returned_parameter_type, fcall->candidate_datatypes[k])) msousa@438: break; msousa@438: } msousa@438: if (k >= fcall->candidate_datatypes.size()) { msousa@438: fcall-> candidate_datatypes.push_back(returned_parameter_type); msousa@438: fcall_data.candidate_functions.push_back(f_decl); msousa@438: } 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@447: * The algorithm will be to build a fake il_fb_call_c equivalent to the implicit IL FB call, and let msousa@447: * the visit(il_fb_call_c *) method handle it! msousa@447: */ msousa@447: void fill_candidate_datatypes_c::handle_implicit_il_fb_call(symbol_c *il_instruction, const char *param_name, symbol_c *&called_fb_declaration) { msousa@448: if (NULL == il_operand) msousa@448: /* No FB to call was specified. There is nothing we can do... */ msousa@448: return; msousa@448: msousa@447: symbol_c *fb_type_id = search_varfb_instance_type->get_basetype_id(il_operand); msousa@447: /* This is a call to a non-declared FB/Variable is a semantic error (which is currently caught by stage 2, so this should never occur) msousa@447: * or no operand was given (il_operand == NULL). In this case, we just give up! msousa@447: */ msousa@447: if (NULL == fb_type_id) msousa@447: return; 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@447: * this is a smeantic error, so there is no way we can evaluate the rest of the code. We simply give up, and leave msousa@447: * the candidate_datatype_list empty, and the called_fb_declaration pointing to NULL msousa@447: */ msousa@447: return; msousa@447: msousa@448: if (NULL == prev_il_instruction) { msousa@448: /* This IL implicit FB call (e.g. CLK ton_var) is not preceded by another IL instruction msousa@448: * (or list of instructions) that will set the IL current/default value. msousa@448: * We cannot proceed verifying type compatibility of something that does not ecist. msousa@448: */ msousa@448: return; msousa@448: } msousa@447: msousa@450: /* The value being passed to the 'param_name' parameter is actually the prev_il_instruction. msousa@450: * However, we do not place that object directly in the fake il_param_list_c that we will be msousa@450: * creating, since the visit(il_fb_call_c *) method will recursively call every object in that list. msousa@450: * The il_prev_intruction object has already been visited. We DO NOT want to visit it again. msousa@450: * The easiest way to work around this is to simply use a new object, and copy the relevant details to that object! msousa@450: */ msousa@450: symbol_c param_value; msousa@450: copy_candidate_datatype_list(prev_il_instruction/*from*/, ¶m_value/*to*/); msousa@450: msousa@447: identifier_c variable_name(param_name); msousa@447: // SYM_REF1(il_assign_operator_c, variable_name) msousa@447: il_assign_operator_c il_assign_operator(&variable_name); msousa@447: // SYM_REF3(il_param_assignment_c, il_assign_operator, il_operand, simple_instr_list) msousa@450: il_param_assignment_c il_param_assignment(&il_assign_operator, ¶m_value/*il_operand*/, NULL); msousa@447: il_param_list_c il_param_list; msousa@447: il_param_list.add_element(&il_param_assignment); msousa@447: // SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list, symbol_c *called_fb_declaration) msousa@447: il_fb_call_c il_fb_call(NULL, il_operand, NULL, &il_param_list); msousa@447: msousa@447: il_fb_call.accept(*this); msousa@447: copy_candidate_datatype_list(&il_fb_call/*from*/, il_instruction/*to*/); msousa@447: called_fb_declaration = il_fb_call.called_fb_declaration; msousa@447: } msousa@438: msousa@438: 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@417: void *fill_candidate_datatypes_c::visit(real_c *symbol) { msousa@417: int calc_size; msousa@417: msousa@417: calc_size = sizeoftype(symbol); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::real_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::real_type_name); msousa@429: if (calc_size <= sizeoftype(&search_constant_type_c::lreal_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::lreal_type_name); msousa@417: if (debug) std::cout << "ANY_REAL [" << symbol->candidate_datatypes.size() << "]" << std::endl; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(integer_c *symbol) { msousa@417: int calc_size; msousa@417: msousa@417: calc_size = sizeoftype(symbol); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::bool_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::bool_type_name); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::byte_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::byte_type_name); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::word_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::word_type_name); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::dword_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::dword_type_name); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::lword_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::lword_type_name); msousa@417: msousa@417: if (calc_size < sizeoftype(&search_constant_type_c::sint_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::sint_type_name); msousa@417: if (calc_size < sizeoftype(&search_constant_type_c::int_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::int_type_name); msousa@417: if (calc_size < sizeoftype(&search_constant_type_c::dint_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::dint_type_name); msousa@417: if (calc_size < sizeoftype(&search_constant_type_c::lint_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::lint_type_name); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::usint_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::usint_type_name); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::uint_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::uint_type_name); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::udint_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::udint_type_name); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::ulint_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::ulint_type_name); msousa@417: if (debug) std::cout << "ANY_INT [" << symbol->candidate_datatypes.size()<< "]" << std::endl; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(neg_real_c *symbol) { msousa@417: if (sizeoftype(symbol) <= sizeoftype(&search_constant_type_c::real_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::real_type_name); msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::lreal_type_name); msousa@417: if (debug) std::cout << "neg ANY_REAL [" << symbol->candidate_datatypes.size() << "]" << std::endl; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(neg_integer_c *symbol) { msousa@417: int calc_size; msousa@417: msousa@417: calc_size = sizeoftype(symbol); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::int_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::int_type_name); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::sint_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::sint_type_name); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::dint_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::dint_type_name); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::lint_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::lint_type_name); msousa@417: if (debug) std::cout << "neg ANY_INT [" << symbol->candidate_datatypes.size() << "]" << std::endl; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(binary_integer_c *symbol) { msousa@417: int calc_size; msousa@417: msousa@417: calc_size = sizeoftype(symbol); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::bool_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::bool_type_name); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::byte_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::byte_type_name); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::word_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::word_type_name); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::dword_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::dword_type_name); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::lword_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::lword_type_name); msousa@417: msousa@417: if (calc_size < sizeoftype(&search_constant_type_c::sint_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::sint_type_name); msousa@417: if (calc_size < sizeoftype(&search_constant_type_c::int_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::int_type_name); msousa@417: if (calc_size < sizeoftype(&search_constant_type_c::dint_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::dint_type_name); msousa@417: if (calc_size < sizeoftype(&search_constant_type_c::lint_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::lint_type_name); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::usint_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::usint_type_name); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::uint_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::uint_type_name); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::udint_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::udint_type_name); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::ulint_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::ulint_type_name); msousa@417: if (debug) std::cout << "ANY_INT [" << symbol->candidate_datatypes.size()<< "]" << std::endl; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(octal_integer_c *symbol) { msousa@417: int calc_size; msousa@417: msousa@417: calc_size = sizeoftype(symbol); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::bool_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::bool_type_name); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::byte_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::byte_type_name); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::word_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::word_type_name); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::dword_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::dword_type_name); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::lword_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::lword_type_name); msousa@417: msousa@417: if (calc_size < sizeoftype(&search_constant_type_c::sint_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::sint_type_name); msousa@417: if (calc_size < sizeoftype(&search_constant_type_c::int_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::int_type_name); msousa@417: if (calc_size < sizeoftype(&search_constant_type_c::dint_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::dint_type_name); msousa@417: if (calc_size < sizeoftype(&search_constant_type_c::lint_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::lint_type_name); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::usint_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::usint_type_name); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::uint_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::uint_type_name); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::udint_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::udint_type_name); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::ulint_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::ulint_type_name); msousa@417: if (debug) std::cout << "ANY_INT [" << symbol->candidate_datatypes.size()<< "]" << std::endl; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(hex_integer_c *symbol) { msousa@417: int calc_size; msousa@417: msousa@417: calc_size = sizeoftype(symbol); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::bool_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::bool_type_name); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::byte_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::byte_type_name); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::word_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::word_type_name); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::dword_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::dword_type_name); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::lword_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::lword_type_name); msousa@417: msousa@417: if (calc_size < sizeoftype(&search_constant_type_c::sint_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::sint_type_name); msousa@417: if (calc_size < sizeoftype(&search_constant_type_c::int_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::int_type_name); msousa@417: if (calc_size < sizeoftype(&search_constant_type_c::dint_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::dint_type_name); msousa@417: if (calc_size < sizeoftype(&search_constant_type_c::lint_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::lint_type_name); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::usint_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::usint_type_name); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::uint_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::uint_type_name); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::udint_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::udint_type_name); msousa@417: if (calc_size <= sizeoftype(&search_constant_type_c::ulint_type_name)) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::ulint_type_name); msousa@417: if (debug) std::cout << "ANY_INT [" << symbol->candidate_datatypes.size()<< "]" << std::endl; msousa@417: return NULL; msousa@417: } msousa@417: msousa@427: // SYM_REF2(integer_literal_c, type, value) msousa@417: void *fill_candidate_datatypes_c::visit(integer_literal_c *symbol) { msousa@427: symbol->value->accept(*this); conti@445: if (search_in_candidate_datatype_list(symbol->type, symbol->value->candidate_datatypes) >= 0) msousa@427: symbol->candidate_datatypes.push_back(symbol->type); msousa@417: if (debug) std::cout << "INT_LITERAL [" << symbol->candidate_datatypes.size() << "]\n"; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(real_literal_c *symbol) { msousa@427: symbol->value->accept(*this); conti@445: if (search_in_candidate_datatype_list(symbol->type, symbol->value->candidate_datatypes) >= 0) msousa@427: symbol->candidate_datatypes.push_back(symbol->type); msousa@417: if (debug) std::cout << "REAL_LITERAL [" << symbol->candidate_datatypes.size() << "]\n"; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(bit_string_literal_c *symbol) { msousa@427: symbol->value->accept(*this); conti@445: if (search_in_candidate_datatype_list(symbol->type, symbol->value->candidate_datatypes) >= 0) msousa@427: symbol->candidate_datatypes.push_back(symbol->type); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(boolean_literal_c *symbol) { msousa@427: symbol->value->accept(*this); conti@445: if (search_in_candidate_datatype_list(symbol->type, symbol->value->candidate_datatypes) >= 0) msousa@427: /* if an explicit datat type has been provided (e.g. SAFEBOOL#true), check whether msousa@427: * the possible datatypes of the value is consistent with the desired type. msousa@427: */ msousa@427: symbol->candidate_datatypes.push_back(symbol->type); msousa@427: else { msousa@427: /* Then only a literal TRUE or FALSE was given! */ msousa@427: /* In this case, symbol->type will be NULL! */ msousa@427: symbol->candidate_datatypes.push_back(&search_constant_type_c::bool_type_name); msousa@427: symbol->candidate_datatypes.push_back(&search_constant_type_c::safebool_type_name); msousa@427: } msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(boolean_true_c *symbol) { msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::bool_type_name); msousa@427: symbol->candidate_datatypes.push_back(&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@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::bool_type_name); msousa@427: symbol->candidate_datatypes.push_back(&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@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::wstring_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@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::string_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@417: symbol->candidate_datatypes.push_back(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@417: void *fill_candidate_datatypes_c::visit(time_of_day_c *symbol) { msousa@417: symbol->candidate_datatypes.push_back(symbol->type_name); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(date_c *symbol) { msousa@417: symbol->candidate_datatypes.push_back(symbol->type_name); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(date_and_time_c *symbol) { msousa@417: symbol->candidate_datatypes.push_back(symbol->type_name); msousa@417: return NULL; msousa@417: } 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@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@417: symbol->candidate_datatypes.push_back(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@417: symbol->candidate_datatypes.push_back(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@417: symbol_c *result = search_varfb_instance_type->get_basetype_decl(symbol); msousa@417: if (NULL != result) msousa@417: symbol->candidate_datatypes.push_back(result); msousa@417: if (debug) std::cout << "VAR [" << symbol->candidate_datatypes.size() << "]\n"; msousa@417: return NULL; msousa@417: } msousa@417: 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@417: case 'X': // bit - 1 bit msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::bool_type_name); msousa@417: break; msousa@417: msousa@417: case 'B': // byte - 8 bits msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::byte_type_name); msousa@417: break; msousa@417: msousa@417: case 'W': // word - 16 bits msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::word_type_name); msousa@417: break; msousa@417: msousa@417: case 'D': // double word - 32 bits msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::dword_type_name); msousa@417: break; msousa@417: msousa@417: case 'L': // long word - 64 bits msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::lword_type_name); msousa@417: break; msousa@417: msousa@417: default: // if none of the above, then the empty string was used <=> boolean msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::bool_type_name); msousa@417: 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@417: if (NULL != result) symbol->candidate_datatypes.push_back(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@417: #if 0 msousa@417: void *fill_candidate_datatypes_c::visit(subscript_list_c *symbol) { msousa@417: } msousa@417: #endif 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@417: symbol_c *result = search_varfb_instance_type->get_basetype_decl(symbol); msousa@417: if (NULL != result) symbol->candidate_datatypes.push_back(result); msousa@417: return NULL; msousa@417: } msousa@417: 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@417: search_varfb_instance_type = new search_varfb_instance_type_c(symbol); msousa@417: symbol->var_declarations_list->accept(*this); msousa@417: if (debug) printf("Filling candidate data types list in body of function %s\n", ((token_c *)(symbol->derived_function_name))->value); msousa@417: il_parenthesis_level = 0; 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@417: search_varfb_instance_type = new search_varfb_instance_type_c(symbol); msousa@417: symbol->var_declarations->accept(*this); msousa@417: if (debug) printf("Filling candidate data types list in body of FB %s\n", ((token_c *)(symbol->fblock_name))->value); msousa@417: il_parenthesis_level = 0; 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@417: search_varfb_instance_type = new search_varfb_instance_type_c(symbol); msousa@417: symbol->var_declarations->accept(*this); msousa@417: if (debug) printf("Filling candidate data types list in body of program %s\n", ((token_c *)(symbol->program_type_name))->value); msousa@417: il_parenthesis_level = 0; 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: #if 0 msousa@417: // TODO !!! msousa@417: /* for the moment we must return NULL so semantic analysis of remaining code is not interrupted! */ msousa@417: #endif 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@417: // void *visit(instruction_list_c *symbol); msousa@443: msousa@443: 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@450: if (NULL != symbol->prev_il_instruction) msousa@450: copy_candidate_datatype_list(symbol->prev_il_instruction /*from*/, symbol /*to*/); msousa@448: } else { msousa@448: prev_il_instruction = symbol->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@448: copy_candidate_datatype_list(symbol->il_instruction /*from*/, symbol /*to*/); 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@443: copy_candidate_datatype_list(symbol->il_simple_operator /*from*/, symbol /*to*/); 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@438: * to the il_operand_list, and remove it later (in the print_datatypes_error_c). 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: * This change will also be undone later in print_datatypes_error_c. 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@438: if (NULL == prev_il_instruction) return NULL; msousa@438: ((list_c *)symbol->il_operand_list)->insert_element(prev_il_instruction, 0); 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->il_operand_list, msousa@441: /* fcall_param.formal_operand_list = */ NULL, 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_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@417: if (NULL != symbol->il_operand) msousa@417: symbol->il_operand->accept(*this); msousa@417: msousa@417: il_parenthesis_level++; msousa@417: msousa@417: /* Note that prev_il_instruction will actually be used to get the current value store in the il_default_variable */ msousa@417: /* If a symbol->il_operand is provided, then that will be the result before executing the simple_instr_list. msousa@417: * If this symbol is NULL, then the current result is also NULL, which is correct for what we want to do! msousa@417: */ msousa@417: symbol_c *prev_il_instruction_backup = prev_il_instruction; msousa@417: prev_il_instruction = symbol->il_operand; msousa@417: msousa@417: if(symbol->simple_instr_list != NULL) { msousa@417: symbol->simple_instr_list->accept(*this); msousa@417: } msousa@417: msousa@417: il_parenthesis_level--; msousa@417: if (il_parenthesis_level < 0) ERROR; msousa@417: msousa@417: /* Now check the if the data type semantics of operation are correct, */ msousa@417: il_operand = prev_il_instruction; msousa@417: prev_il_instruction = prev_il_instruction_backup; msousa@417: symbol->il_expr_operator->accept(*this); msousa@417: il_operand = NULL; msousa@417: return NULL; msousa@417: } msousa@417: 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@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@439: bool compatible = false; msousa@439: symbol_c *fb_decl = search_varfb_instance_type->get_basetype_decl(symbol->fb_name); 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@439: if (symbol-> il_param_list != NULL) { msousa@439: symbol->il_param_list->accept(*this); msousa@439: compatible = match_formal_call(symbol, fb_decl); msousa@439: } msousa@439: if (symbol->il_operand_list != NULL) { msousa@439: symbol->il_operand_list->accept(*this); msousa@439: compatible = match_nonformal_call(symbol, fb_decl); msousa@439: } 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@450: /* This object has the same candidate datatypes as the prev_il_instruction, since it does not change the value stored in the current/default IL variable. */ msousa@450: copy_candidate_datatype_list(prev_il_instruction/*from*/, symbol/*to*/); 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@417: /* msousa@417: void *visit(il_operand_list_c *symbol); msousa@417: void *visit(simple_instr_list_c *symbol); 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@417: symbol->candidate_datatypes.push_back(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@417: symbol->candidate_datatypes.push_back(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@417: symbol->candidate_datatypes.push_back(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@417: symbol->candidate_datatypes.push_back(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@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]; conti@440: if (is_type_equal(prev_instruction_type,operand_type) && is_ANY_BOOL_compatible(operand_type)) msousa@417: symbol->candidate_datatypes.push_back(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]; conti@440: if (is_type_equal(prev_instruction_type,operand_type) && is_ANY_BOOL_compatible(operand_type)) msousa@417: symbol->candidate_datatypes.push_back(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@417: void *fill_candidate_datatypes_c::visit(S1_operator_c *symbol) { msousa@447: handle_implicit_il_fb_call(symbol, "S1", symbol->called_fb_declaration); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(R1_operator_c *symbol) { msousa@447: handle_implicit_il_fb_call(symbol, "R1", symbol->called_fb_declaration); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(CLK_operator_c *symbol) { msousa@447: handle_implicit_il_fb_call(symbol, "CLK", symbol->called_fb_declaration); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(CU_operator_c *symbol) { msousa@447: handle_implicit_il_fb_call(symbol, "CU", symbol->called_fb_declaration); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(CD_operator_c *symbol) { msousa@447: handle_implicit_il_fb_call(symbol, "CD", symbol->called_fb_declaration); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(PV_operator_c *symbol) { msousa@447: handle_implicit_il_fb_call(symbol, "PV", symbol->called_fb_declaration); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(IN_operator_c *symbol) { msousa@447: handle_implicit_il_fb_call(symbol, "IN", symbol->called_fb_declaration); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(PT_operator_c *symbol) { msousa@447: handle_implicit_il_fb_call(symbol, "PT", symbol->called_fb_declaration); msousa@447: return NULL; msousa@447: } msousa@447: msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(AND_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]; msousa@417: if (is_type_equal(prev_instruction_type, operand_type) && msousa@417: is_ANY_BIT_compatible(operand_type)) msousa@417: symbol->candidate_datatypes.push_back(prev_instruction_type); msousa@417: } msousa@417: } msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(OR_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]; msousa@417: if (is_type_equal(prev_instruction_type, operand_type) && msousa@417: is_ANY_BIT_compatible(operand_type)) msousa@417: symbol->candidate_datatypes.push_back(prev_instruction_type); msousa@417: } msousa@417: } msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(XOR_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]; msousa@417: if (is_type_equal(prev_instruction_type, operand_type) && msousa@417: is_ANY_BIT_compatible(operand_type)) msousa@417: symbol->candidate_datatypes.push_back(prev_instruction_type); msousa@417: } msousa@417: } msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(ANDN_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]; msousa@417: if (is_type_equal(prev_instruction_type, operand_type) && msousa@417: is_ANY_BIT_compatible(operand_type)) msousa@417: symbol->candidate_datatypes.push_back(prev_instruction_type); msousa@417: } msousa@417: } msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(ORN_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]; msousa@417: if (is_type_equal(prev_instruction_type, operand_type) && msousa@417: is_ANY_BIT_compatible(operand_type)) msousa@417: symbol->candidate_datatypes.push_back(prev_instruction_type); msousa@417: } msousa@417: } msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(XORN_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]; msousa@417: if (is_type_equal(prev_instruction_type, operand_type) && msousa@417: is_ANY_BIT_compatible(operand_type)) msousa@417: symbol->candidate_datatypes.push_back(prev_instruction_type); msousa@417: } msousa@417: } msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(ADD_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]; msousa@417: if (is_type_equal(prev_instruction_type, operand_type) && msousa@417: is_ANY_NUM_compatible(prev_instruction_type)) msousa@417: symbol->candidate_datatypes.push_back(prev_instruction_type); msousa@417: else { msousa@417: symbol_c *result = widening_conversion(prev_instruction_type, operand_type, widen_ADD_table); msousa@417: if (result) msousa@417: symbol->candidate_datatypes.push_back(result); msousa@417: msousa@417: } msousa@417: } msousa@417: } msousa@417: if (debug) std::cout << "ADD [" << 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(SUB_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]; msousa@417: if (is_type_equal(prev_instruction_type, operand_type) && msousa@417: is_ANY_NUM_compatible(prev_instruction_type)) msousa@417: symbol->candidate_datatypes.push_back(prev_instruction_type); msousa@417: else { msousa@417: symbol_c *result = widening_conversion(prev_instruction_type, operand_type, widen_SUB_table); msousa@417: if (result) msousa@417: symbol->candidate_datatypes.push_back(result); msousa@417: } msousa@417: } msousa@417: } msousa@417: if (debug) std::cout << "SUB [" << 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(MUL_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]; msousa@417: if (is_type_equal(prev_instruction_type, operand_type) && msousa@417: is_ANY_NUM_compatible(prev_instruction_type)) msousa@417: symbol->candidate_datatypes.push_back(prev_instruction_type); msousa@417: else { msousa@417: symbol_c *result = widening_conversion(prev_instruction_type, operand_type, widen_MUL_table); msousa@417: if (result) msousa@417: symbol->candidate_datatypes.push_back(result); msousa@417: } msousa@417: } msousa@417: } msousa@417: if (debug) std::cout << "MUL [" << 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(DIV_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]; msousa@417: if (is_type_equal(prev_instruction_type, operand_type) && msousa@417: is_ANY_NUM_compatible(prev_instruction_type)) msousa@417: symbol->candidate_datatypes.push_back(prev_instruction_type); msousa@417: else { msousa@417: symbol_c *result = widening_conversion(prev_instruction_type, operand_type, widen_DIV_table); msousa@417: if (result) msousa@417: symbol->candidate_datatypes.push_back(result); msousa@417: } msousa@417: } msousa@417: } msousa@417: if (debug) std::cout << "DIV [" << 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(MOD_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]; msousa@417: if (is_type_equal(prev_instruction_type, operand_type) && msousa@417: is_ANY_INT_compatible(prev_instruction_type)) msousa@417: symbol->candidate_datatypes.push_back(prev_instruction_type); msousa@417: } msousa@417: } msousa@417: if (debug) std::cout << "MOD [" << 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(GT_operator_c *symbol) { msousa@417: bool found = false; 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: if (is_type_equal(prev_il_instruction->candidate_datatypes[i], il_operand->candidate_datatypes[j]) msousa@417: && is_ANY_ELEMENTARY_compatible(prev_il_instruction->candidate_datatypes[i])) { msousa@417: found = true; msousa@417: break; msousa@417: } msousa@417: } msousa@417: } msousa@417: if (found) symbol->candidate_datatypes.push_back(&search_constant_type_c::bool_type_name); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(GE_operator_c *symbol) { msousa@417: bool found = false; 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: if (is_type_equal(prev_il_instruction->candidate_datatypes[i], il_operand->candidate_datatypes[j]) msousa@417: && is_ANY_ELEMENTARY_compatible(prev_il_instruction->candidate_datatypes[i])) { msousa@417: found = true; msousa@417: break; msousa@417: } msousa@417: } msousa@417: } msousa@417: if (found) symbol->candidate_datatypes.push_back(&search_constant_type_c::bool_type_name); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(EQ_operator_c *symbol) { msousa@417: bool found = false; 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: if (is_type_equal(prev_il_instruction->candidate_datatypes[i], il_operand->candidate_datatypes[j]) msousa@417: && is_ANY_ELEMENTARY_compatible(prev_il_instruction->candidate_datatypes[i])) { msousa@417: found = true; msousa@417: break; msousa@417: } msousa@417: } msousa@417: } msousa@417: if (found) symbol->candidate_datatypes.push_back(&search_constant_type_c::bool_type_name); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(LT_operator_c *symbol) { msousa@417: bool found = false; 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: if (is_type_equal(prev_il_instruction->candidate_datatypes[i], il_operand->candidate_datatypes[j]) msousa@417: && is_ANY_ELEMENTARY_compatible(prev_il_instruction->candidate_datatypes[i])) { msousa@417: found = true; msousa@417: break; msousa@417: } msousa@417: } msousa@417: } msousa@417: if (found) symbol->candidate_datatypes.push_back(&search_constant_type_c::bool_type_name); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(LE_operator_c *symbol) { msousa@417: bool found = false; 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: if (is_type_equal(prev_il_instruction->candidate_datatypes[i], il_operand->candidate_datatypes[j]) msousa@417: && is_ANY_ELEMENTARY_compatible(prev_il_instruction->candidate_datatypes[i])) { msousa@417: found = true; msousa@417: break; msousa@417: } msousa@417: } msousa@417: } msousa@417: if (found) symbol->candidate_datatypes.push_back(&search_constant_type_c::bool_type_name); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(NE_operator_c *symbol) { msousa@417: bool found = false; 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: if (is_type_equal(prev_il_instruction->candidate_datatypes[i], il_operand->candidate_datatypes[j]) msousa@417: && is_ANY_ELEMENTARY_compatible(prev_il_instruction->candidate_datatypes[i])) { msousa@417: found = true; msousa@417: break; msousa@417: } msousa@417: } msousa@417: } msousa@417: if (found) symbol->candidate_datatypes.push_back(&search_constant_type_c::bool_type_name); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(CAL_operator_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@417: /* does not need to be bool type !! */ msousa@417: symbol->candidate_datatypes.push_back(prev_il_instruction->candidate_datatypes[i]); msousa@417: } msousa@417: if (debug) std::cout << "CAL [" << prev_il_instruction->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(CALC_operator_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@417: if (is_type(prev_il_instruction->candidate_datatypes[i], bool_type_name_c)) msousa@417: symbol->candidate_datatypes.push_back(prev_il_instruction->candidate_datatypes[i]); msousa@417: } msousa@417: if (debug) std::cout << "CALC [" << prev_il_instruction->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(CALCN_operator_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@417: if (is_type(prev_il_instruction->candidate_datatypes[i], bool_type_name_c)) msousa@417: symbol->candidate_datatypes.push_back(prev_il_instruction->candidate_datatypes[i]); msousa@417: } msousa@417: if (debug) std::cout << "CALCN [" << prev_il_instruction->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(RET_operator_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@417: /* does not need to be bool type !! */ msousa@417: symbol->candidate_datatypes.push_back(prev_il_instruction->candidate_datatypes[i]); msousa@417: } msousa@417: if (debug) std::cout << "RET [" << prev_il_instruction->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(RETC_operator_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@417: if (is_type(prev_il_instruction->candidate_datatypes[i], bool_type_name_c)) msousa@417: symbol->candidate_datatypes.push_back(prev_il_instruction->candidate_datatypes[i]); msousa@417: } msousa@417: if (debug) std::cout << "RETC [" << prev_il_instruction->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(RETCN_operator_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@417: if (is_type(prev_il_instruction->candidate_datatypes[i], bool_type_name_c)) msousa@417: symbol->candidate_datatypes.push_back(prev_il_instruction->candidate_datatypes[i]); msousa@417: } msousa@417: if (debug) std::cout << "RETCN [" << prev_il_instruction->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(JMP_operator_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@417: /* does not need to be bool type !! */ msousa@417: symbol->candidate_datatypes.push_back(prev_il_instruction->candidate_datatypes[i]); msousa@417: } msousa@417: if (debug) std::cout << "JMP [" << prev_il_instruction->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(JMPC_operator_c *symbol) { msousa@417: for (unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) { msousa@417: if (is_type(prev_il_instruction->candidate_datatypes[i], bool_type_name_c)) msousa@417: symbol->candidate_datatypes.push_back(prev_il_instruction->candidate_datatypes[i]); msousa@417: } msousa@417: if (debug) std::cout << "JMPC [" << prev_il_instruction->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(JMPCN_operator_c *symbol) { msousa@417: for (unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) { msousa@417: if (is_type(prev_il_instruction->candidate_datatypes[i], bool_type_name_c)) msousa@417: symbol->candidate_datatypes.push_back(prev_il_instruction->candidate_datatypes[i]); msousa@417: } msousa@417: if (debug) std::cout << "JMPCN [" << prev_il_instruction->candidate_datatypes.size() << "] ==> " << symbol->candidate_datatypes.size() << " result.\n"; msousa@417: return NULL; msousa@417: } 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@417: msousa@417: void *fill_candidate_datatypes_c::visit(or_expression_c *symbol) { 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: if (is_type_equal(symbol->l_exp->candidate_datatypes[i], symbol->r_exp->candidate_datatypes[j]) msousa@417: && is_ANY_BIT_compatible(symbol->l_exp->candidate_datatypes[i])) msousa@417: symbol->candidate_datatypes.push_back(symbol->l_exp->candidate_datatypes[i]); msousa@417: } msousa@417: } msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(xor_expression_c *symbol) { msousa@417: symbol->l_exp->accept(*this); msousa@417: symbol->r_exp->accept(*this); msousa@417: 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: if (is_type_equal(symbol->l_exp->candidate_datatypes[i], symbol->r_exp->candidate_datatypes[j]) msousa@417: && is_ANY_BIT_compatible(symbol->l_exp->candidate_datatypes[i])) msousa@417: symbol->candidate_datatypes.push_back(symbol->l_exp->candidate_datatypes[i]); msousa@417: } msousa@417: } msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(and_expression_c *symbol) { msousa@417: symbol->l_exp->accept(*this); msousa@417: symbol->r_exp->accept(*this); msousa@417: 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: if (is_type_equal(symbol->l_exp->candidate_datatypes[i], symbol->r_exp->candidate_datatypes[j]) msousa@417: && is_ANY_BIT_compatible(symbol->l_exp->candidate_datatypes[i])) msousa@417: symbol->candidate_datatypes.push_back(symbol->l_exp->candidate_datatypes[i]); msousa@417: } msousa@417: } msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(equ_expression_c *symbol) { msousa@417: symbol->l_exp->accept(*this); msousa@417: symbol->r_exp->accept(*this); msousa@417: bool found = false; msousa@417: 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: if (is_type_equal(symbol->l_exp->candidate_datatypes[i], symbol->r_exp->candidate_datatypes[j]) msousa@417: && is_ANY_ELEMENTARY_compatible(symbol->l_exp->candidate_datatypes[i])) { msousa@417: found = true; msousa@417: break; msousa@417: } msousa@417: } msousa@417: } msousa@417: if (found) symbol->candidate_datatypes.push_back(&search_constant_type_c::bool_type_name); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(notequ_expression_c *symbol) { msousa@417: symbol->l_exp->accept(*this); msousa@417: symbol->r_exp->accept(*this); msousa@417: bool found = false; msousa@417: 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: if (is_type_equal(symbol->l_exp->candidate_datatypes[i], symbol->r_exp->candidate_datatypes[j]) msousa@417: && is_ANY_ELEMENTARY_compatible(symbol->l_exp->candidate_datatypes[i])) { msousa@417: found = true; msousa@417: break; msousa@417: } msousa@417: } msousa@417: } msousa@417: if (found) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::bool_type_name); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(lt_expression_c *symbol) { msousa@417: symbol->l_exp->accept(*this); msousa@417: symbol->r_exp->accept(*this); msousa@417: bool found = false; msousa@417: 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: if (is_type_equal(symbol->l_exp->candidate_datatypes[i], symbol->r_exp->candidate_datatypes[j]) msousa@417: && is_ANY_ELEMENTARY_compatible(symbol->l_exp->candidate_datatypes[i])) { msousa@417: found = true; msousa@417: break; msousa@417: } msousa@417: } msousa@417: } msousa@417: if (found) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::bool_type_name); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(gt_expression_c *symbol) { msousa@417: symbol->l_exp->accept(*this); msousa@417: symbol->r_exp->accept(*this); msousa@417: bool found = false; msousa@417: 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: if (is_type_equal(symbol->l_exp->candidate_datatypes[i], symbol->r_exp->candidate_datatypes[j]) msousa@417: && is_ANY_ELEMENTARY_compatible(symbol->l_exp->candidate_datatypes[i])) { msousa@417: found = true; msousa@417: break; msousa@417: } msousa@417: } msousa@417: } msousa@417: if (found) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::bool_type_name); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(le_expression_c *symbol) { msousa@417: symbol->l_exp->accept(*this); msousa@417: symbol->r_exp->accept(*this); msousa@417: bool found = false; msousa@417: 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: if (is_type_equal(symbol->l_exp->candidate_datatypes[i], symbol->r_exp->candidate_datatypes[j]) msousa@417: && is_ANY_ELEMENTARY_compatible(symbol->l_exp->candidate_datatypes[i])) { msousa@417: found = true; msousa@417: break; msousa@417: } msousa@417: } msousa@417: } msousa@417: if (found) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::bool_type_name); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(ge_expression_c *symbol) { msousa@417: symbol->l_exp->accept(*this); msousa@417: symbol->r_exp->accept(*this); msousa@417: bool found = false; msousa@417: 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: if (is_type_equal(symbol->l_exp->candidate_datatypes[i], symbol->r_exp->candidate_datatypes[j]) msousa@417: && is_ANY_ELEMENTARY_compatible(symbol->l_exp->candidate_datatypes[i])) { msousa@417: found = true; msousa@417: break; msousa@417: } msousa@417: } msousa@417: } msousa@417: if (found) msousa@417: symbol->candidate_datatypes.push_back(&search_constant_type_c::bool_type_name); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(add_expression_c *symbol) { msousa@417: /* The following code is correct when handling the addition of 2 symbolic_variables msousa@417: * In this case, adding two variables (e.g. USINT_var1 + USINT_var2) will always yield msousa@417: * the same data type, even if the result of the adition could not fit inside the same msousa@417: * data type (due to overflowing) msousa@417: * msousa@417: * However, when adding two literals (e.g. USINT#42 + USINT#3) msousa@417: * we should be able to detect overflows of the result, and therefore not consider msousa@417: * that the result may be of type USINT. msousa@417: * Currently we do not yet detect these overflows, and allow handling the sum of two USINTs msousa@417: * as always resulting in an USINT, even in the following expression msousa@417: * (USINT#65535 + USINT#2). msousa@417: * msousa@417: * In the future we can add some code to reduce msousa@417: * all the expressions that are based on literals into the resulting literal msousa@417: * value (maybe some visitor class that will run before or after data type msousa@417: * checking). Since this class will have to be very careful to make sure it implements the same mathematical msousa@417: * details (e.g. how to round and truncate numbers) as defined in IEC 61131-3, we will leave this to the future. msousa@417: * Also, the question will arise if we should also replace calls to standard msousa@417: * functions if the input parameters are all literals (e.g. ADD(42, 42)). This msousa@417: * means this class will be more difficult than it appears at first. msousa@417: */ 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) && is_ANY_NUM_compatible(left_type)) msousa@417: symbol->candidate_datatypes.push_back(left_type); msousa@417: else { msousa@417: symbol_c *result = widening_conversion(left_type, right_type, widen_ADD_table); msousa@417: if (result) msousa@417: symbol->candidate_datatypes.push_back(result); msousa@417: } 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: msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(sub_expression_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) && is_ANY_NUM_compatible(left_type)) msousa@417: symbol->candidate_datatypes.push_back(left_type); msousa@417: else { msousa@417: symbol_c *result = widening_conversion(left_type, right_type, widen_SUB_table); msousa@417: if (result) msousa@417: symbol->candidate_datatypes.push_back(result); msousa@417: } 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: msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(mul_expression_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) && is_ANY_NUM_compatible(left_type)) msousa@417: symbol->candidate_datatypes.push_back(left_type); msousa@417: else { msousa@417: symbol_c *result = widening_conversion(left_type, right_type, widen_MUL_table); msousa@417: if (result) msousa@417: symbol->candidate_datatypes.push_back(result); msousa@417: } msousa@417: 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: msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(div_expression_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) && is_ANY_NUM_type(left_type)) msousa@417: symbol->candidate_datatypes.push_back(left_type); msousa@417: else { msousa@417: symbol_c *result = widening_conversion(left_type, right_type, widen_DIV_table); msousa@417: if (result) msousa@417: symbol->candidate_datatypes.push_back(result); msousa@417: } msousa@417: 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: msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(mod_expression_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) && is_ANY_INT_compatible(left_type)) msousa@417: symbol->candidate_datatypes.push_back(left_type); msousa@417: } msousa@417: } msousa@417: if (debug) std::cout << "mod [" << 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: msousa@417: msousa@417: void *fill_candidate_datatypes_c::visit(power_expression_c *symbol) { msousa@417: symbol_c *left_type, *right_type; msousa@417: bool check_ok; msousa@417: msousa@417: symbol->l_exp->accept(*this); msousa@417: symbol->r_exp->accept(*this); msousa@417: check_ok = false; msousa@417: for (unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) { msousa@417: left_type = symbol->l_exp->candidate_datatypes[i]; msousa@417: if (is_ANY_REAL_compatible(left_type)) { msousa@417: check_ok = true; msousa@417: break; msousa@417: } msousa@417: } msousa@417: if (! check_ok) return NULL; msousa@417: check_ok = false; msousa@417: for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) { msousa@417: right_type = symbol->r_exp->candidate_datatypes[j]; msousa@417: if (is_ANY_NUM_compatible(right_type)) { msousa@417: check_ok = true; msousa@417: break; msousa@417: } msousa@417: } msousa@417: if (! check_ok) return NULL; msousa@417: for (unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) { msousa@417: symbol->candidate_datatypes.push_back(symbol->l_exp->candidate_datatypes[i]); 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: 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@435: * uint_var := - (uint_var); msousa@435: * msousa@435: * According to the standard, the above code should result in a msousa@435: * runtime error, when we try to apply a negative value to the msousa@435: * UINT typed variable 'uint_var'. 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@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@417: symbol->candidate_datatypes.push_back(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@417: symbol->candidate_datatypes.push_back(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@417: symbol->candidate_datatypes.push_back(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@420: bool compatible = false; conti@418: symbol_c *fb_decl = search_varfb_instance_type->get_basetype_decl(symbol->fb_name); 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@424: msousa@420: if (symbol-> formal_param_list != NULL) { msousa@420: symbol->formal_param_list->accept(*this); msousa@420: compatible = match_formal_call(symbol, fb_decl); msousa@420: } msousa@420: if (symbol->nonformal_param_list != NULL) { msousa@420: symbol->nonformal_param_list->accept(*this); msousa@420: compatible = match_nonformal_call(symbol, fb_decl); msousa@420: } 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: