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@552: /* NOTE: The algorithm implemented here assumes that candidate datatype lists have already been filled! msousa@552: * BEFORE running this visitor, be sure to CALL the fill_candidate_datatype_c visitor! msousa@552: */ msousa@552: msousa@552: msousa@417: /* msousa@552: * Choose, from the list of all the possible datatypes each expression may take, the single datatype that it will in fact take. msousa@552: * The resulting (chosen) datatype, will be stored in the symbol_c.datatype variable, leaving the candidate datatype list untouched! msousa@552: * msousa@552: * For rvalue expressions, this decision will be based on the datatype of the lvalue expression. msousa@552: * For lvalue expressions, the candidate datatype list should have a single entry. msousa@552: * msousa@552: * For example, the very simple literal '0' in 'foo := 0', may represent a: msousa@552: * BOOL, BYTE, WORD, DWORD, LWORD, USINT, SINT, UINT, INT, UDINT, DINT, ULINT, LINT (as well as the SAFE versions of these data tyes too!) msousa@552: * msousa@552: * In this class, the datatype of '0' will be set to the same datatype as the 'foo' variable. msousa@552: * If the intersection of the candidate datatype lists of the left and right side expressions is empty, msousa@552: * then a datatype error has been found, and the datatype is either left at NULL, or set to a pointer of an invalid_type_name_c object! msousa@417: */ msousa@417: msousa@552: msousa@417: #include "narrow_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: msousa@417: /* set to 1 to see debug info during execution */ msousa@417: static int debug = 0; msousa@417: msousa@417: narrow_candidate_datatypes_c::narrow_candidate_datatypes_c(symbol_c *ignore) { conti@661: search_varfb_instance_type = NULL; conti@661: fake_prev_il_instruction = NULL; msousa@674: current_il_instruction = NULL; conti@661: il_operand = NULL; mjsousa@889: current_scope = NULL; msousa@417: } msousa@417: msousa@417: narrow_candidate_datatypes_c::~narrow_candidate_datatypes_c(void) { msousa@417: } msousa@417: msousa@425: msousa@425: /* Only set the symbol's desired datatype to 'datatype' if that datatype is in the candidate_datatype list */ msousa@425: static void set_datatype(symbol_c *datatype, symbol_c *symbol) { msousa@459: msousa@459: /* If we are trying to set to the undefined type, and the symbol's datatype has already been set to something else, msousa@459: * we abort the compoiler as I don't think this should ever occur. msousa@466: * NOTE: In order to handle JMPs to labels that come before the JMP itself, we run the narrow algorithm twice. msousa@466: * This means that this situation may legally occur, so we cannot abort the compiler here! msousa@466: */ msousa@466: // if ((NULL == datatype) && (NULL != symbol->datatype)) ERROR; msousa@466: if ((NULL == datatype) && (NULL != symbol->datatype)) return; msousa@459: if ((NULL == datatype) && (NULL == symbol->datatype)) return; msousa@459: msousa@459: if (search_in_candidate_datatype_list(datatype, symbol->candidate_datatypes) < 0) msousa@693: symbol->datatype = &(get_datatype_info_c::invalid_type_name); msousa@459: else { msousa@459: if (NULL == symbol->datatype) msousa@459: /* not yet set to anything, so we set it to the requested data type */ msousa@459: symbol->datatype = datatype; msousa@459: else { msousa@459: /* had already been set previously to some data type. Let's check if they are the same! */ msousa@676: if (!get_datatype_info_c::is_type_equal(symbol->datatype, datatype)) msousa@693: symbol->datatype = &(get_datatype_info_c::invalid_type_name); msousa@459: // else msousa@459: /* we leave it unchanged, as it is the same as the requested data type! */ msousa@459: } msousa@459: } msousa@425: } msousa@425: msousa@425: msousa@425: msousa@457: /* Only set the symbol's desired datatype to 'datatype' if that datatype is in the candidate_datatype list */ msousa@459: // static void set_datatype_in_prev_il_instructions(symbol_c *datatype, std::vector prev_il_instructions) { msousa@459: static void set_datatype_in_prev_il_instructions(symbol_c *datatype, il_instruction_c *symbol) { msousa@459: if (NULL == symbol) ERROR; msousa@457: for (unsigned int i = 0; i < symbol->prev_il_instruction.size(); i++) msousa@457: set_datatype(datatype, symbol->prev_il_instruction[i]); msousa@457: } msousa@457: msousa@457: msousa@425: msousa@480: bool narrow_candidate_datatypes_c::is_widening_compatible(const struct widen_entry widen_table[], symbol_c *left_type, symbol_c *right_type, symbol_c *result_type, bool *deprecated_status) { msousa@515: /* NOTE: According to our algorithm, left_type and right_type should never by NULL (if they are, we have an internal compiler error! msousa@515: * However, result_type may be NULL if the code has a data type semantic error! msousa@515: */ msousa@515: if ((NULL == left_type) || (NULL == right_type) || (NULL == result_type)) msousa@515: return false; msousa@515: msousa@417: for (int k = 0; NULL != widen_table[k].left; k++) { msousa@417: if ((typeid(*left_type) == typeid(*widen_table[k].left)) msousa@417: && (typeid(*right_type) == typeid(*widen_table[k].right)) msousa@478: && (typeid(*result_type) == typeid(*widen_table[k].result))) { msousa@480: if (NULL != deprecated_status) msousa@480: *deprecated_status = (widen_table[k].status == widen_entry::deprecated); msousa@417: return true; msousa@417: } msousa@417: } msousa@417: return false; msousa@417: } msousa@417: 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@421: void narrow_candidate_datatypes_c::narrow_nonformal_call(symbol_c *f_call, symbol_c *f_decl, int *ext_parm_count) { msousa@417: symbol_c *call_param_value, *param_type; 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@421: if (NULL != ext_parm_count) *ext_parm_count = -1; 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: /* Obtaining the type of the value being passed in the function call */ 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@424: /* This error should have been caught in fill_candidate_datatypes_c, but may occur here again when we handle FB invocations! msousa@424: * In this case, we carry on analysing the code in order to be able to provide relevant error messages msousa@424: * for that code too! msousa@424: */ msousa@424: if(param_name == NULL) break; msousa@417: } while ((strcmp(param_name->value, "EN") == 0) || (strcmp(param_name->value, "ENO") == 0)); msousa@417: msousa@421: /* Set the desired datatype for this parameter, and call it recursively. */ msousa@424: /* Note that if the call has more parameters than those declared in the function/FB declaration, msousa@424: * we may be setting this to NULL! msousa@424: */ msousa@425: symbol_c *desired_datatype = base_type(fp_iterator.param_type()); msousa@425: if ((NULL != param_name) && (NULL == desired_datatype)) ERROR; msousa@425: if ((NULL == param_name) && (NULL != desired_datatype)) ERROR; msousa@425: msousa@443: /* NOTE: When we are handling a nonformal function call made from IL, the first parameter is the 'default' or 'current' msousa@451: * il value. However, a pointer to a copy of the prev_il_instruction is pre-pended into the operand list, so msousa@443: * the call msousa@443: * call_param_value->accept(*this); msousa@451: * may actually be calling an object of the base symbol_c . msousa@443: */ msousa@425: set_datatype(desired_datatype, call_param_value); msousa@451: call_param_value->accept(*this); msousa@421: msousa@424: if (NULL != param_name) msousa@424: if (extensible_parameter_highest_index < fp_iterator.extensible_param_index()) msousa@424: extensible_parameter_highest_index = fp_iterator.extensible_param_index(); msousa@424: } msousa@421: /* In the case of a call to an extensible function, we store the highest index msousa@421: * of the extensible parameters this particular call uses, in the symbol_c object msousa@421: * of the function call itself! msousa@421: * In calls to non-extensible functions, this value will be set to -1. msousa@421: * This information is later used in stage4 to correctly generate the msousa@421: * output code. msousa@421: */ msousa@421: if ((NULL != ext_parm_count) && (extensible_parameter_highest_index >=0) /* if call to extensible function */) msousa@421: *ext_parm_count = 1 + extensible_parameter_highest_index - fp_iterator.first_extensible_param_index(); msousa@421: } msousa@421: msousa@421: msousa@421: msousa@421: void narrow_candidate_datatypes_c::narrow_formal_call(symbol_c *f_call, symbol_c *f_decl, int *ext_parm_count) { msousa@417: symbol_c *call_param_value, *call_param_name, *param_type; 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@421: if (NULL != ext_parm_count) *ext_parm_count = -1; msousa@417: /* Iterating through the formal parameters of the function call */ msousa@417: while((call_param_name = fcp_iterator.next_f()) != NULL) { msousa@417: 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@417: /* Find the corresponding parameter in function declaration */ msousa@417: param_name = fp_iterator.search(call_param_name); msousa@417: msousa@421: /* Set the desired datatype for this parameter, and call it recursively. */ msousa@424: /* NOTE: When handling a FB call, this narrow_formal_call() may be called to analyse msousa@424: * an invalid FB call (call with parameters that do not exist on the FB declaration). msousa@424: * For this reason, the param_name may come out as NULL! msousa@424: */ msousa@425: symbol_c *desired_datatype = base_type(fp_iterator.param_type()); msousa@425: if ((NULL != param_name) && (NULL == desired_datatype)) ERROR; msousa@425: if ((NULL == param_name) && (NULL != desired_datatype)) ERROR; msousa@425: msousa@450: /* set the desired data type for this parameter */ msousa@425: set_datatype(desired_datatype, call_param_value); msousa@450: /* And recursively call that parameter/expression, so it can propagate that info */ msousa@451: call_param_value->accept(*this); msousa@421: msousa@455: /* set the extensible_parameter_highest_index, which will be needed in stage 4 */ msousa@455: /* This value says how many extensible parameters are being passed to the standard function */ msousa@424: if (NULL != param_name) msousa@424: if (extensible_parameter_highest_index < fp_iterator.extensible_param_index()) msousa@424: extensible_parameter_highest_index = fp_iterator.extensible_param_index(); msousa@421: } msousa@421: /* call is compatible! */ msousa@421: msousa@421: /* In the case of a call to an extensible function, we store the highest index msousa@417: * of the extensible parameters this particular call uses, in the symbol_c object msousa@417: * of the function call itself! msousa@417: * In calls to non-extensible functions, this value will be set to -1. msousa@417: * This information is later used in stage4 to correctly generate the msousa@417: * output code. msousa@417: */ msousa@421: if ((NULL != ext_parm_count) && (extensible_parameter_highest_index >=0) /* if call to extensible function */) msousa@421: *ext_parm_count = 1 + extensible_parameter_highest_index - fp_iterator.first_extensible_param_index(); msousa@421: } msousa@421: msousa@421: 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: void narrow_candidate_datatypes_c::narrow_function_invocation(symbol_c *fcall, generic_function_call_t fcall_data) { msousa@438: /* set the called_function_declaration. */ msousa@438: fcall_data.called_function_declaration = NULL; msousa@438: msousa@438: /* set the called_function_declaration taking into account the datatype that we need to return */ msousa@438: for(unsigned int i = 0; i < fcall->candidate_datatypes.size(); i++) { msousa@676: if (get_datatype_info_c::is_type_equal(fcall->candidate_datatypes[i], fcall->datatype)) { msousa@438: fcall_data.called_function_declaration = fcall_data.candidate_functions[i]; msousa@438: break; msousa@438: } msousa@438: } msousa@438: msousa@438: /* NOTE: If we can't figure out the declaration of the function being called, this is not msousa@438: * necessarily an internal compiler error. It could be because the symbol->datatype is NULL msousa@438: * (because the ST code being analysed has an error _before_ this function invocation). msousa@438: * However, we don't just give, up, we carry on recursivly analysing the code, so as to be msousa@438: * able to print out any error messages related to the parameters being passed in this function msousa@438: * invocation. msousa@438: */ msousa@438: /* if (NULL == symbol->called_function_declaration) ERROR; */ msousa@438: if (fcall->candidate_datatypes.size() == 1) { msousa@438: /* If only one function declaration, then we use that (even if symbol->datatypes == NULL) msousa@438: * so we can check for errors in the expressions used to pass parameters in this msousa@438: * function invocation. msousa@438: */ msousa@438: fcall_data.called_function_declaration = fcall_data.candidate_functions[0]; msousa@438: } msousa@438: msousa@438: /* If an overloaded function is being invoked, and we cannot determine which version to use, msousa@438: * then we can not meaningfully verify the expressions used inside that function invocation. msousa@438: * We simply give up! msousa@438: */ msousa@438: if (NULL == fcall_data.called_function_declaration) msousa@438: return; msousa@438: msousa@438: if (NULL != fcall_data.nonformal_operand_list) narrow_nonformal_call(fcall, fcall_data.called_function_declaration, &(fcall_data.extensible_param_count)); msousa@438: if (NULL != fcall_data. formal_operand_list) narrow_formal_call(fcall, fcall_data.called_function_declaration, &(fcall_data.extensible_param_count)); msousa@438: msousa@438: return; msousa@438: } msousa@438: msousa@438: msousa@417: msousa@448: msousa@448: /* narrow implicit FB call in IL. msousa@448: * e.g. CLK ton_var msousa@448: * CU counter_var msousa@448: * msousa@448: * The algorithm will be to build a fake il_fb_call_c equivalent to the implicit IL FB call, and let msousa@448: * the visit(il_fb_call_c *) method handle it! msousa@448: */ msousa@456: void *narrow_candidate_datatypes_c::narrow_implicit_il_fb_call(symbol_c *il_instruction, const char *param_name, symbol_c *&called_fb_declaration) { msousa@455: msousa@455: /* set the datatype of the il_operand, this is, the FB being called! */ msousa@455: if (NULL != il_operand) { mjsousa@834: set_datatype(called_fb_declaration, il_operand); /* only set it if it is in the candidate datatypes list! */ msousa@455: il_operand->accept(*this); msousa@455: } msousa@455: msousa@459: if (0 == fake_prev_il_instruction->prev_il_instruction.size()) { 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@451: * We cannot proceed verifying type compatibility of something that does not exist. msousa@448: */ msousa@456: return NULL; msousa@448: } msousa@448: mjsousa@834: symbol_c *fb_decl = (NULL == il_operand)? NULL : il_operand->datatype; mjsousa@834: msousa@455: if (NULL == fb_decl) { mjsousa@834: /* the il_operand is a not FB instance, or it simply does not even exist, */ msousa@459: /* so we simply pass on the required datatype to the prev_il_instructions */ msousa@459: /* The invalid FB invocation will be caught in the print_datatypes_error_c by analysing NULL value in il_operand->datatype! */ msousa@459: set_datatype_in_prev_il_instructions(il_instruction->datatype, fake_prev_il_instruction); msousa@456: return NULL; msousa@455: } msousa@455: msousa@455: msousa@451: /* The value being passed to the 'param_name' parameter is actually the prev_il_instruction. msousa@451: * However, we do not place that object directly in the fake il_param_list_c that we will be msousa@451: * creating, since the visit(il_fb_call_c *) method will recursively call every object in that list. msousa@451: * The il_prev_intruction object will be visited once we have handled this implici IL FB call msousa@451: * (called from the instruction_list_c for() loop that works backwards). We DO NOT want to visit it twice. msousa@451: * (Anyway, if we let the visit(il_fb_call_c *) recursively visit the current prev_il_instruction, this pointer msousa@451: * would be changed to the IL instruction coming before the current prev_il_instruction! => things would get all messed up!) msousa@451: * The easiest way to work around this is to simply use a new object, and copy the relevant details to that object! msousa@451: */ msousa@459: symbol_c param_value = *fake_prev_il_instruction; /* copy the candidate_datatypes list ! */ msousa@461: msousa@448: identifier_c variable_name(param_name); msousa@448: // SYM_REF1(il_assign_operator_c, variable_name) msousa@448: il_assign_operator_c il_assign_operator(&variable_name); msousa@448: // SYM_REF3(il_param_assignment_c, il_assign_operator, il_operand, simple_instr_list) msousa@451: il_param_assignment_c il_param_assignment(&il_assign_operator, ¶m_value/*il_operand*/, NULL); msousa@448: il_param_list_c il_param_list; msousa@448: il_param_list.add_element(&il_param_assignment); msousa@448: // SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list, symbol_c *called_fb_declaration) msousa@455: CAL_operator_c CAL_operator; msousa@455: il_fb_call_c il_fb_call(&CAL_operator, il_operand, NULL, &il_param_list); msousa@455: msousa@448: /* A FB call does not return any datatype, but the IL instructions that come after this msousa@448: * FB call may require a specific datatype in the il current/default variable, msousa@448: * so we must pass this information up to the IL instruction before the FB call, since it will msousa@448: * be that IL instruction that will be required to produce the desired dtataype. msousa@448: * msousa@448: * The above will be done by the visit(il_fb_call_c *) method, so we must make sure to msousa@448: * correctly set up the il_fb_call.datatype variable! msousa@448: */ msousa@455: il_fb_call.called_fb_declaration = called_fb_declaration; msousa@448: il_fb_call.accept(*this); msousa@455: msousa@451: /* set the required datatype of the previous IL instruction! */ msousa@451: /* NOTE: msousa@451: * When handling these implicit IL calls, the parameter_value being passed to the FB parameter msousa@451: * is actually the prev_il_instruction. msousa@451: * msousa@451: * However, since the FB call does not change the value in the current/default IL variable, this value msousa@451: * must also be used ny the IL instruction coming after this FB call. msousa@451: * msousa@451: * This means that we have two consumers/users for the same value. msousa@451: * We must therefore check whether the datatype required by the IL instructions following this FB call msousa@451: * is the same as that required for the first parameter. If not, then we have a semantic error, msousa@459: * and we set it to invalid_type_name. msousa@451: * msousa@451: * However, we only do that if: msousa@451: * - The IL instruction that comes after this IL FB call actually asked this FB call for a specific msousa@451: * datatype in the current/default vairable, once this IL FB call returns. msousa@451: * However, sometimes, (for e.g., this FB call is the last in the IL list) the subsequent FB to not aks this msousa@451: * FB call for any datatype. In that case, then the datatype required to pass to the first parameter of the msousa@451: * FB call must be left unchanged! msousa@451: */ msousa@676: if ((NULL == il_instruction->datatype) || (get_datatype_info_c::is_type_equal(param_value.datatype, il_instruction->datatype))) { msousa@459: set_datatype_in_prev_il_instructions(param_value.datatype, fake_prev_il_instruction); msousa@455: } else { msousa@693: set_datatype_in_prev_il_instructions(&get_datatype_info_c::invalid_type_name, fake_prev_il_instruction); msousa@455: } msousa@456: return NULL; msousa@448: } msousa@448: msousa@448: msousa@417: /* a helper function... */ msousa@417: symbol_c *narrow_candidate_datatypes_c::base_type(symbol_c *symbol) { msousa@718: /* NOTE: symbol == NULL is valid. It will occur when, for e.g., an undefined/undeclared symbolic_variable is used in the code. */ msousa@423: if (symbol == NULL) return NULL; msousa@718: return search_base_type_c::get_basetype_decl(symbol); msousa@417: } msousa@417: msousa@726: msousa@726: msousa@417: msousa@417: /**********************/ msousa@417: /* B 1.3 - Data types */ msousa@417: /**********************/ msousa@797: /***********************************/ msousa@797: /* B 1.3.1 - Elementary Data Types */ msousa@797: /***********************************/ msousa@797: /* NOTE: elementary datatypes are their own basetype ! */ msousa@797: void *narrow_candidate_datatypes_c::visit( time_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit( bool_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit( sint_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit( int_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit( dint_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit( lint_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit( usint_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit( uint_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit( udint_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit( ulint_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit( real_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit( lreal_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit( date_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit( tod_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit( dt_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit( byte_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit( word_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit( dword_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit( lword_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit( string_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit( wstring_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: msousa@797: void *narrow_candidate_datatypes_c::visit(safetime_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit(safebool_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit(safesint_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit(safeint_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit(safedint_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit(safelint_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit(safeusint_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit(safeuint_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit(safeudint_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit(safeulint_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit(safereal_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit(safelreal_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit(safedate_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit(safetod_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit(safedt_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit(safebyte_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit(safeword_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit(safedword_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit(safelword_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit(safestring_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: void *narrow_candidate_datatypes_c::visit(safewstring_type_name_c *symbol) {symbol->datatype = search_base_type_c::get_basetype_decl(symbol); return NULL;} msousa@797: msousa@797: msousa@797: /********************************/ msousa@797: /* B.1.3.2 - Generic data types */ msousa@797: /********************************/ msousa@797: /* empty!! */ msousa@797: msousa@417: /********************************/ msousa@417: /* B 1.3.3 - Derived data types */ msousa@417: /********************************/ msousa@806: void *narrow_candidate_datatypes_c::narrow_spec_init(symbol_c *symbol, symbol_c *type_decl, symbol_c *init_value) { msousa@806: // If we are handling an anonymous datatype (i.e. a datatype implicitly declared inside a VAR ... END_VAR declaration) msousa@806: // then the symbol->datatype has not yet been set by the previous visit(type_decl) method, because it does not exist! msousa@806: // So we set the datatype ourselves! msousa@806: if ((NULL == symbol->datatype) && (symbol->candidate_datatypes.size() == 1)) msousa@806: symbol->datatype = symbol->candidate_datatypes[0]; msousa@806: msousa@806: set_datatype(symbol->datatype, type_decl); msousa@806: type_decl->accept(*this); msousa@806: msousa@806: if (NULL != init_value) { msousa@806: set_datatype(symbol->datatype, init_value); msousa@806: init_value->accept(*this); msousa@806: } msousa@806: return NULL; msousa@806: } msousa@806: msousa@806: msousa@806: void *narrow_candidate_datatypes_c::narrow_type_decl(symbol_c *symbol, symbol_c *type_name, symbol_c *spec_init) { msousa@806: if (symbol->candidate_datatypes.size() == 1) { msousa@806: symbol->datatype = symbol->candidate_datatypes[0]; msousa@806: msousa@806: set_datatype(symbol->datatype, type_name); msousa@806: set_datatype(symbol->datatype, spec_init); msousa@806: spec_init->accept(*this); msousa@806: } msousa@806: return NULL; msousa@806: } msousa@806: msousa@806: msousa@726: /* TYPE type_declaration_list END_TYPE */ msousa@726: // SYM_REF1(data_type_declaration_c, type_declaration_list) msousa@726: /* NOTE: Not required. already handled by iterator_visitor_c base class */ msousa@726: msousa@726: /* helper symbol for data_type_declaration */ msousa@726: // SYM_LIST(type_declaration_list_c) msousa@726: /* NOTE: Not required. already handled by iterator_visitor_c base class */ msousa@726: msousa@726: /* simple_type_name ':' simple_spec_init */ msousa@726: // SYM_REF2(simple_type_declaration_c, simple_type_name, simple_spec_init) msousa@806: void *narrow_candidate_datatypes_c::visit(simple_type_declaration_c *symbol) {return narrow_type_decl(symbol, symbol->simple_type_name, symbol->simple_spec_init);} msousa@726: msousa@502: /* simple_specification ASSIGN constant */ msousa@502: // SYM_REF2(simple_spec_init_c, simple_specification, constant) msousa@806: void *narrow_candidate_datatypes_c::visit(simple_spec_init_c *symbol) {return narrow_spec_init(symbol, symbol->simple_specification, symbol->constant);} msousa@502: msousa@726: /* subrange_type_name ':' subrange_spec_init */ msousa@726: // SYM_REF2(subrange_type_declaration_c, subrange_type_name, subrange_spec_init) msousa@806: void *narrow_candidate_datatypes_c::visit(subrange_type_declaration_c *symbol) {return narrow_type_decl(symbol, symbol->subrange_type_name, symbol->subrange_spec_init);} msousa@726: msousa@726: /* subrange_specification ASSIGN signed_integer */ msousa@726: // SYM_REF2(subrange_spec_init_c, subrange_specification, signed_integer) msousa@806: void *narrow_candidate_datatypes_c::visit(subrange_spec_init_c *symbol) {return narrow_spec_init(symbol, symbol->subrange_specification, symbol->signed_integer);} msousa@726: msousa@726: /* integer_type_name '(' subrange')' */ msousa@726: // SYM_REF2(subrange_specification_c, integer_type_name, subrange) msousa@806: void *narrow_candidate_datatypes_c::visit(subrange_specification_c *symbol) { msousa@806: set_datatype(symbol->datatype, symbol->integer_type_name); msousa@806: symbol->integer_type_name->accept(*this); msousa@806: set_datatype(symbol->datatype, symbol->integer_type_name); msousa@806: symbol->integer_type_name->accept(*this); msousa@806: return NULL; msousa@806: } msousa@502: msousa@417: /* signed_integer DOTDOT signed_integer */ msousa@726: /* dimension will be filled in during stage 3 (array_range_check_c) with the number of elements in this subrange */ msousa@726: // SYM_REF2(subrange_c, lower_limit, upper_limit, unsigned long long int dimension;) msousa@417: void *narrow_candidate_datatypes_c::visit(subrange_c *symbol) { msousa@806: set_datatype(symbol->datatype, symbol->lower_limit); msousa@417: symbol->lower_limit->accept(*this); msousa@806: set_datatype(symbol->datatype, symbol->upper_limit); msousa@417: symbol->upper_limit->accept(*this); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@726: /* enumerated_type_name ':' enumerated_spec_init */ msousa@726: // SYM_REF2(enumerated_type_declaration_c, enumerated_type_name, enumerated_spec_init) msousa@806: void *narrow_candidate_datatypes_c::visit(enumerated_type_declaration_c *symbol) {return narrow_type_decl(symbol, symbol->enumerated_type_name, symbol->enumerated_spec_init);} msousa@726: msousa@726: msousa@726: /* enumerated_specification ASSIGN enumerated_value */ msousa@726: // SYM_REF2(enumerated_spec_init_c, enumerated_specification, enumerated_value) msousa@806: void *narrow_candidate_datatypes_c::visit(enumerated_spec_init_c *symbol) {return narrow_spec_init(symbol, symbol->enumerated_specification, symbol->enumerated_value);} msousa@726: msousa@726: /* helper symbol for enumerated_specification->enumerated_spec_init */ msousa@726: /* enumerated_value_list ',' enumerated_value */ msousa@726: // SYM_LIST(enumerated_value_list_c) msousa@726: void *narrow_candidate_datatypes_c::visit(enumerated_value_list_c *symbol) { msousa@806: //if (NULL == symbol->datatype) ERROR; // Comented out-> Reserve this check for the print_datatypes_error_c ??? msousa@806: for(int i = 0; i < symbol->n; i++) set_datatype(symbol->datatype, symbol->elements[i]); msousa@806: //for(int i = 0; i < symbol->n; i++) if (NULL == symbol->elements[i]->datatype) ERROR; // Comented out-> Reserve this check for the print_datatypes_error_c ??? msousa@726: return NULL; msousa@726: } msousa@726: msousa@726: msousa@726: /* enumerated_type_name '#' identifier */ msousa@726: // SYM_REF2(enumerated_value_c, type, value) msousa@726: // void *narrow_candidate_datatypes_c::visit(enumerated_value_c *symbol) {/* do nothing! */ return NULL;} msousa@726: msousa@726: msousa@726: /* identifier ':' array_spec_init */ msousa@726: // SYM_REF2(array_type_declaration_c, identifier, array_spec_init) msousa@806: void *narrow_candidate_datatypes_c::visit(array_type_declaration_c *symbol) {return narrow_type_decl(symbol, symbol->identifier, symbol->array_spec_init);} msousa@726: msousa@726: /* array_specification [ASSIGN array_initialization} */ msousa@726: /* array_initialization may be NULL ! */ msousa@726: // SYM_REF2(array_spec_init_c, array_specification, array_initialization) msousa@806: void *narrow_candidate_datatypes_c::visit(array_spec_init_c *symbol) {return narrow_spec_init(symbol, symbol->array_specification, symbol->array_initialization);} msousa@726: msousa@726: /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */ msousa@726: // SYM_REF2(array_specification_c, array_subrange_list, non_generic_type_name) msousa@806: // Not needed!! msousa@726: msousa@726: /* helper symbol for array_specification */ msousa@726: /* array_subrange_list ',' subrange */ msousa@726: // SYM_LIST(array_subrange_list_c) msousa@806: // Not needed ?? msousa@726: msousa@726: /* array_initialization: '[' array_initial_elements_list ']' */ msousa@726: /* helper symbol for array_initialization */ msousa@726: /* array_initial_elements_list ',' array_initial_elements */ msousa@726: // SYM_LIST(array_initial_elements_list_c) msousa@806: // Not needed ??? msousa@726: msousa@726: /* integer '(' [array_initial_element] ')' */ msousa@726: /* array_initial_element may be NULL ! */ msousa@726: // SYM_REF2(array_initial_elements_c, integer, array_initial_element) msousa@806: // Not needed ??? msousa@726: msousa@726: /* structure_type_name ':' structure_specification */ msousa@726: // SYM_REF2(structure_type_declaration_c, structure_type_name, structure_specification) msousa@806: void *narrow_candidate_datatypes_c::visit(structure_type_declaration_c *symbol) {return narrow_type_decl(symbol, symbol->structure_type_name, symbol->structure_specification);} msousa@726: msousa@726: /* structure_type_name ASSIGN structure_initialization */ msousa@726: /* structure_initialization may be NULL ! */ msousa@726: // SYM_REF2(initialized_structure_c, structure_type_name, structure_initialization) msousa@806: void *narrow_candidate_datatypes_c::visit(initialized_structure_c *symbol) {return narrow_spec_init(symbol, symbol->structure_type_name, symbol->structure_initialization);} msousa@726: msousa@726: /* helper symbol for structure_declaration */ msousa@726: /* structure_declaration: STRUCT structure_element_declaration_list END_STRUCT */ msousa@726: /* structure_element_declaration_list structure_element_declaration ';' */ msousa@726: // SYM_LIST(structure_element_declaration_list_c) msousa@806: // Not needed ??? msousa@726: msousa@726: /* structure_element_name ':' *_spec_init */ msousa@726: // SYM_REF2(structure_element_declaration_c, structure_element_name, spec_init) msousa@806: // Not needed ??? msousa@726: msousa@726: /* helper symbol for structure_initialization */ msousa@726: /* structure_initialization: '(' structure_element_initialization_list ')' */ msousa@726: /* structure_element_initialization_list ',' structure_element_initialization */ msousa@726: // SYM_LIST(structure_element_initialization_list_c) msousa@806: // Not needed ??? msousa@726: msousa@726: /* structure_element_name ASSIGN value */ msousa@726: // SYM_REF2(structure_element_initialization_c, structure_element_name, value) msousa@806: // Not needed ??? msousa@726: msousa@726: /* string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */ msousa@726: // SYM_REF4(string_type_declaration_c, string_type_name, elementary_string_type_name, string_type_declaration_size, string_type_declaration_init/* may be == NULL! */) msousa@726: msousa@726: msousa@810: /* structure_type_name ASSIGN structure_initialization */ msousa@810: /* structure_initialization may be NULL ! */ msousa@810: // SYM_REF2(initialized_structure_c, structure_type_name, structure_initialization) msousa@810: void *narrow_candidate_datatypes_c::visit(fb_spec_init_c *symbol) {return narrow_spec_init(symbol, symbol->function_block_type_name, symbol->structure_initialization);} msousa@726: msousa@726: mjsousa@909: /* REF_TO (non_generic_type_name | function_block_type_name) */ mjsousa@909: // SYM_REF1(ref_spec_c, type_name) mjsousa@909: void *narrow_candidate_datatypes_c::visit(ref_spec_c *symbol) { mjsousa@909: /* First handle the datatype being referenced (pointed to) */ mjsousa@909: if (symbol->type_name->candidate_datatypes.size() == 1) { mjsousa@909: symbol->type_name->datatype = symbol->type_name->candidate_datatypes[0]; mjsousa@909: symbol->type_name->accept(*this); mjsousa@909: } mjsousa@909: mjsousa@909: /* Now handle the reference datatype itself (i.e. the pointer) */ mjsousa@909: // If we are handling an anonymous datatype (i.e. a datatype implicitly declared inside a VAR ... END_VAR declaration) mjsousa@909: // then the symbol->datatype has not yet been set by the previous visit(type_decl) method, because it does not exist! mjsousa@909: // So we set the datatype ourselves! mjsousa@909: if ((NULL == symbol->datatype) && (symbol->candidate_datatypes.size() == 1)) mjsousa@909: symbol->datatype = symbol->candidate_datatypes[0]; mjsousa@909: mjsousa@909: return NULL; mjsousa@909: } mjsousa@909: mjsousa@909: /* For the moment, we do not support initialising reference data types */ mjsousa@909: /* ref_spec [ ASSIGN ref_initialization ] */ mjsousa@909: /* NOTE: ref_initialization may be NULL!! */ mjsousa@909: // SYM_REF2(ref_spec_init_c, ref_spec, ref_initialization) mjsousa@909: void *narrow_candidate_datatypes_c::visit(ref_spec_init_c *symbol) {return narrow_spec_init(symbol, symbol->ref_spec, symbol->ref_initialization);} mjsousa@909: mjsousa@909: /* identifier ':' ref_spec_init */ mjsousa@909: // SYM_REF2(ref_type_decl_c, ref_type_name, ref_spec_init) mjsousa@909: void *narrow_candidate_datatypes_c::visit(ref_type_decl_c *symbol) {return narrow_type_decl(symbol, symbol->ref_type_name, symbol->ref_spec_init);} mjsousa@909: mjsousa@909: mjsousa@909: msousa@417: /*********************/ msousa@417: /* B 1.4 - Variables */ msousa@417: /*********************/ mjsousa@843: // SYM_REF1(symbolic_variable_c, var_name) mjsousa@843: void *narrow_candidate_datatypes_c::visit(symbolic_variable_c *symbol) { mjsousa@889: symbol->scope = current_scope; // the scope in which this variable was declared! mjsousa@843: symbol->var_name->datatype = symbol->datatype; mjsousa@843: return NULL; mjsousa@843: } msousa@417: /********************************************/ msousa@417: /* B 1.4.1 - Directly Represented Variables */ 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 *narrow_candidate_datatypes_c::visit(array_variable_c *symbol) { msousa@417: /* we need to check the data types of the expressions used for the subscripts... */ msousa@417: symbol->subscript_list->accept(*this); mjsousa@827: mjsousa@827: /* Set the datatype of the subscripted variable and visit it recursively. For the reason why we do this, */ mjsousa@827: /* Please read the comments in the array_variable_c and structured_variable_c visitors in the fill_candidate_datatypes.cc file! */ mjsousa@827: symbol->subscripted_variable->accept(*this); // visit recursively mjsousa@827: mjsousa@827: if (symbol->subscripted_variable->candidate_datatypes.size() == 1) mjsousa@827: symbol->subscripted_variable->datatype = symbol->subscripted_variable->candidate_datatypes[0]; // set the datatype mjsousa@827: mjsousa@889: // the scope in which this variable was declared! It will be the same as the subscripted variable (a symbolic_variable_ !) mjsousa@889: symbol->scope = symbol->subscripted_variable->scope; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@417: /* subscript_list ',' subscript */ msousa@417: // SYM_LIST(subscript_list_c) msousa@417: void *narrow_candidate_datatypes_c::visit(subscript_list_c *symbol) { msousa@417: for (int i = 0; i < symbol->n; i++) { msousa@417: for (unsigned int k = 0; k < symbol->elements[i]->candidate_datatypes.size(); k++) { msousa@666: if (get_datatype_info_c::is_ANY_INT(symbol->elements[i]->candidate_datatypes[k])) msousa@417: symbol->elements[i]->datatype = symbol->elements[i]->candidate_datatypes[k]; msousa@417: } msousa@417: symbol->elements[i]->accept(*this); msousa@417: } msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@417: mjsousa@827: /* record_variable '.' field_selector */ mjsousa@827: /* WARNING: input and/or output variables of function blocks mjsousa@827: * may be accessed as fields of a structured variable! mjsousa@827: * Code handling a structured_variable_c must take mjsousa@827: * this into account! mjsousa@827: */ mjsousa@827: // SYM_REF2(structured_variable_c, record_variable, field_selector) mjsousa@827: void *narrow_candidate_datatypes_c::visit(structured_variable_c *symbol) { mjsousa@827: /* Set the datatype of the record_variable and visit it recursively. For the reason why we do this, */ mjsousa@827: /* Please read the comments in the array_variable_c and structured_variable_c visitors in the fill_candidate_datatypes.cc file! */ mjsousa@827: symbol->record_variable->accept(*this); // visit recursively mjsousa@827: mjsousa@827: if (symbol->record_variable->candidate_datatypes.size() == 1) mjsousa@827: symbol->record_variable->datatype = symbol->record_variable->candidate_datatypes[0]; // set the datatype mjsousa@827: mjsousa@889: symbol->scope = symbol->record_variable->datatype; // the scope in which this variable was declared! mjsousa@827: return NULL; mjsousa@827: } mjsousa@827: msousa@502: msousa@502: /******************************************/ msousa@502: /* B 1.4.3 - Declaration & Initialisation */ msousa@502: /******************************************/ msousa@502: msousa@502: void *narrow_candidate_datatypes_c::visit(var1_list_c *symbol) { msousa@502: #if 0 /* We don't really need to set the datatype of each variable. We just check the declaration itself! */ msousa@502: for(int i = 0; i < symbol->n; i++) { msousa@502: if (symbol->elements[i]->candidate_datatypes.size() == 1) msousa@502: symbol->elements[i]->datatype = symbol->elements[i]->candidate_datatypes[0]; msousa@502: } msousa@502: #endif msousa@502: return NULL; msousa@502: } msousa@502: msousa@502: msousa@502: /* AT direct_variable */ msousa@502: // SYM_REF1(location_c, direct_variable) msousa@502: void *narrow_candidate_datatypes_c::visit(location_c *symbol) { msousa@502: set_datatype(symbol->datatype, symbol->direct_variable); msousa@502: symbol->direct_variable->accept(*this); /* currently does nothing! */ msousa@502: return NULL; msousa@502: } msousa@502: msousa@502: msousa@502: /* [variable_name] location ':' located_var_spec_init */ msousa@502: /* variable_name -> may be NULL ! */ msousa@502: // SYM_REF3(located_var_decl_c, variable_name, location, located_var_spec_init) msousa@502: void *narrow_candidate_datatypes_c::visit(located_var_decl_c *symbol) { msousa@502: /* let the var_spec_init set its own symbol->datatype value */ msousa@502: symbol->located_var_spec_init->accept(*this); msousa@502: msousa@502: if (NULL != symbol->variable_name) msousa@502: set_datatype(symbol->located_var_spec_init->datatype, symbol->variable_name); msousa@502: msousa@502: set_datatype(symbol->located_var_spec_init->datatype, symbol->location); msousa@502: symbol->location->accept(*this); msousa@502: return NULL; msousa@502: } msousa@502: msousa@502: msousa@808: 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 *narrow_candidate_datatypes_c::visit(function_declaration_c *symbol) { msousa@798: /* set the function_declaration_c->datatype to the datatype returned by the function! */ msousa@798: symbol->type_name->datatype = search_base_type_c::get_basetype_decl(symbol->type_name); msousa@798: symbol->datatype = symbol->type_name->datatype; msousa@798: mjsousa@889: current_scope = symbol; msousa@417: search_varfb_instance_type = new search_varfb_instance_type_c(symbol); msousa@443: symbol->var_declarations_list->accept(*this); msousa@417: if (debug) printf("Narrowing candidate data types list in body of function %s\n", ((token_c *)(symbol->derived_function_name))->value); msousa@417: symbol->function_body->accept(*this); msousa@417: delete search_varfb_instance_type; msousa@417: search_varfb_instance_type = NULL; mjsousa@889: current_scope = NULL; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: /***************************/ msousa@417: /* B 1.5.2 Function blocks */ msousa@417: /***************************/ msousa@417: void *narrow_candidate_datatypes_c::visit(function_block_declaration_c *symbol) { mjsousa@889: current_scope = symbol; msousa@417: search_varfb_instance_type = new search_varfb_instance_type_c(symbol); msousa@443: symbol->var_declarations->accept(*this); msousa@417: if (debug) printf("Narrowing candidate data types list in body of FB %s\n", ((token_c *)(symbol->fblock_name))->value); msousa@417: symbol->fblock_body->accept(*this); msousa@417: delete search_varfb_instance_type; msousa@417: search_varfb_instance_type = NULL; mjsousa@889: current_scope = NULL; mjsousa@889: msousa@807: // A FB declaration can also be used as a Datatype! We now do the narrow algorithm considering it as such! msousa@807: if (symbol->candidate_datatypes.size() == 1) msousa@807: symbol->datatype = symbol->candidate_datatypes[0]; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: /********************/ msousa@417: /* B 1.5.3 Programs */ msousa@417: /********************/ msousa@417: void *narrow_candidate_datatypes_c::visit(program_declaration_c *symbol) { mjsousa@889: current_scope = symbol; msousa@417: search_varfb_instance_type = new search_varfb_instance_type_c(symbol); msousa@443: symbol->var_declarations->accept(*this); msousa@417: if (debug) printf("Narrowing candidate data types list in body of program %s\n", ((token_c *)(symbol->program_type_name))->value); msousa@417: symbol->function_block_body->accept(*this); msousa@417: delete search_varfb_instance_type; msousa@417: search_varfb_instance_type = NULL; mjsousa@889: current_scope = NULL; msousa@417: return NULL; msousa@417: } msousa@417: Laurent@802: /********************************************/ Laurent@802: /* B 1.6 Sequential function chart elements */ Laurent@802: /********************************************/ Laurent@802: void *narrow_candidate_datatypes_c::visit(transition_condition_c *symbol) { mjsousa@895: // We can safely ask for a BOOL type, as even if the result is a SAFEBOOL, in that case it will aslo include BOOL as a possible datatype. mjsousa@895: set_datatype(&get_datatype_info_c::bool_type_name /* datatype*/, symbol /* symbol */); mjsousa@895: Laurent@802: if (symbol->transition_condition_il != NULL) { mjsousa@895: set_datatype(symbol->datatype, symbol->transition_condition_il); Laurent@802: symbol->transition_condition_il->accept(*this); Laurent@802: } Laurent@802: if (symbol->transition_condition_st != NULL) { mjsousa@895: set_datatype(symbol->datatype, symbol->transition_condition_st); Laurent@802: symbol->transition_condition_st->accept(*this); Laurent@802: } Laurent@802: return NULL; Laurent@802: } msousa@417: msousa@417: /********************************/ msousa@417: /* B 1.7 Configuration elements */ msousa@417: /********************************/ msousa@417: void *narrow_candidate_datatypes_c::visit(configuration_declaration_c *symbol) { msousa@417: // TODO !!! msousa@417: /* for the moment we must return NULL so semantic analysis of remaining code is not interrupted! */ msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@417: /****************************************/ 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@443: void *narrow_candidate_datatypes_c::visit(instruction_list_c *symbol) { msousa@450: /* In order to execute the narrow algoritm correctly, we need to go through the instructions backwards, msousa@450: * so we can not use the base class' visitor msousa@450: */ msousa@466: /* In order to execute the narrow algoritm correctly msousa@466: * in IL instruction lists containing JMPs to labels that come before the JMP instruction msousa@466: * itself, we need to run the narrow algorithm twice on the Instruction List. msousa@466: * e.g.: ... msousa@466: * ld 23 msousa@466: * label1:st byte_var msousa@466: * ld 34 msousa@466: * JMP label1 msousa@466: * msousa@466: * Note that the second time we run the narrow, most of the datatypes are already filled msousa@466: * in, so it will be able to produce tha correct datatypes for the IL instruction referenced msousa@466: * by the label, as in the 2nd pass we already know the datatypes of the JMP instruction! msousa@466: */ msousa@466: for(int j = 0; j < 2; j++) { msousa@466: for(int i = symbol->n-1; i >= 0; i--) { msousa@466: symbol->elements[i]->accept(*this); msousa@466: } msousa@443: } msousa@443: return NULL; 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 *narrow_candidate_datatypes_c::visit(il_instruction_c *symbol) { msousa@448: if (NULL == symbol->il_instruction) { msousa@448: /* this empty/null il_instruction cannot generate the desired datatype. We pass on the request to the previous il instruction. */ msousa@459: set_datatype_in_prev_il_instructions(symbol->datatype, symbol); msousa@448: } else { msousa@459: il_instruction_c tmp_prev_il_instruction(NULL, NULL); msousa@459: /* the narrow algorithm will need access to the intersected candidate_datatype lists of all prev_il_instructions, as well as the msousa@459: * list of the prev_il_instructions. msousa@459: * Instead of creating two 'global' (within the class) variables, we create a single il_instruction_c variable (fake_prev_il_instruction), msousa@459: * and shove that data into this single variable. msousa@459: */ msousa@459: tmp_prev_il_instruction.prev_il_instruction = symbol->prev_il_instruction; msousa@459: intersect_prev_candidate_datatype_lists(&tmp_prev_il_instruction); msousa@448: /* Tell the il_instruction the datatype that it must generate - this was chosen by the next il_instruction (remember: we are iterating backwards!) */ msousa@459: fake_prev_il_instruction = &tmp_prev_il_instruction; msousa@674: current_il_instruction = symbol; msousa@459: symbol->il_instruction->datatype = symbol->datatype; msousa@448: symbol->il_instruction->accept(*this); msousa@459: fake_prev_il_instruction = NULL; msousa@674: current_il_instruction = NULL; msousa@450: } msousa@450: return NULL; msousa@450: } msousa@444: msousa@444: msousa@444: msousa@444: msousa@417: // void *visit(instruction_list_c *symbol); msousa@417: void *narrow_candidate_datatypes_c::visit(il_simple_operation_c *symbol) { msousa@443: /* Tell the il_simple_operator the datatype that it must generate - this was chosen by the next il_instruction (we iterate backwards!) */ msousa@443: symbol->il_simple_operator->datatype = symbol->datatype; msousa@443: /* recursive call to see whether data types are compatible */ msousa@417: il_operand = symbol->il_operand; msousa@417: symbol->il_simple_operator->accept(*this); msousa@417: il_operand = NULL; msousa@417: return NULL; msousa@417: } msousa@417: 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 *narrow_candidate_datatypes_c::visit(il_function_call_c *symbol) { msousa@451: /* The first parameter of a non formal function call in IL will be the 'current value' (i.e. the prev_il_instruction) msousa@451: * In order to be able to handle this without coding special cases, we will simply prepend that symbol msousa@451: * to the il_operand_list, and remove it after calling handle_function_call(). msousa@451: * However, since handle_function_call() will be recursively calling all parameter, and we don't want msousa@451: * to do that for the prev_il_instruction (since it has already been visited by the fill_candidate_datatypes_c) msousa@451: * we create a new ____ symbol_c ____ object, and copy the relevant info to/from that object before/after msousa@451: * the call to handle_function_call(). msousa@451: * msousa@451: * However, if no further paramters are given, then il_operand_list will be NULL, and we will msousa@451: * need to create a new object to hold the pointer to prev_il_instruction. msousa@534: * This change will also be undone at the end of this method. msousa@451: */ msousa@459: symbol_c param_value = *fake_prev_il_instruction; /* copy the candidate_datatypes list */ msousa@451: if (NULL == symbol->il_operand_list) symbol->il_operand_list = new il_operand_list_c; msousa@451: if (NULL == symbol->il_operand_list) ERROR; msousa@451: msousa@451: ((list_c *)symbol->il_operand_list)->insert_element(¶m_value, 0); msousa@451: 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: msousa@438: narrow_function_invocation(symbol, fcall_param); msousa@459: set_datatype_in_prev_il_instructions(param_value.datatype, fake_prev_il_instruction); msousa@451: msousa@451: /* Undo the changes to the abstract syntax tree we made above... */ msousa@451: ((list_c *)symbol->il_operand_list)->remove_element(0); msousa@451: if (((list_c *)symbol->il_operand_list)->n == 0) { msousa@451: /* if the list becomes empty, then that means that it did not exist before we made these changes, so we delete it! */ msousa@451: delete symbol->il_operand_list; msousa@451: symbol->il_operand_list = NULL; msousa@451: } msousa@451: msousa@417: return NULL; msousa@417: } msousa@417: msousa@452: 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 *narrow_candidate_datatypes_c::visit(il_expression_c *symbol) { msousa@453: /* first handle the operation (il_expr_operator) that will use the result coming from the parenthesised IL list (i.e. simple_instr_list) */ msousa@453: symbol->il_expr_operator->datatype = symbol->datatype; msousa@454: il_operand = symbol->simple_instr_list; /* This is not a bug! The parenthesised expression will be used as the operator! */ msousa@453: symbol->il_expr_operator->accept(*this); msousa@453: msousa@453: /* now give the parenthesised IL list a chance to narrow the datatypes */ msousa@454: /* The datatype that is must return was set by the call symbol->il_expr_operator->accept(*this) */ msousa@459: il_instruction_c *save_fake_prev_il_instruction = fake_prev_il_instruction; /*this is not really necessary, but lets play it safe */ msousa@452: symbol->simple_instr_list->accept(*this); msousa@459: fake_prev_il_instruction = save_fake_prev_il_instruction; msousa@690: msousa@690: /* Since stage2 will insert an artificial (and equivalent) LD to the simple_instr_list when an 'il_operand' exists, we know msousa@690: * that if (symbol->il_operand != NULL), then the first IL instruction in the simple_instr_list will be the equivalent and artificial msousa@690: * 'LD ' IL instruction. msousa@690: * Just to be consistent, we will copy the datatype info back into the il_operand, even though this should not be necessary! msousa@690: */ msousa@690: if ((NULL != symbol->il_operand) && ((NULL == symbol->simple_instr_list) || (0 == ((list_c *)symbol->simple_instr_list)->n))) ERROR; // stage2 is not behaving as we expect it to! msousa@690: if (NULL != symbol->il_operand) msousa@690: symbol->il_operand->datatype = ((list_c *)symbol->simple_instr_list)->elements[0]->datatype; msousa@690: msousa@459: return NULL; msousa@417: } msousa@417: msousa@456: msousa@466: msousa@466: msousa@466: /* il_jump_operator label */ msousa@466: void *narrow_candidate_datatypes_c::visit(il_jump_operation_c *symbol) { msousa@466: /* recursive call to fill the datatype */ msousa@466: symbol->il_jump_operator->datatype = symbol->datatype; msousa@466: symbol->il_jump_operator->accept(*this); msousa@466: return NULL; msousa@466: } msousa@466: msousa@466: msousa@466: msousa@466: msousa@466: msousa@466: msousa@466: 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: */ mjsousa@834: /* NOTE: The parameter 'called_fb_declaration'is used to pass data between stage 3 and stage4 */ 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 *narrow_candidate_datatypes_c::visit(il_fb_call_c *symbol) { msousa@455: symbol_c *fb_decl = symbol->called_fb_declaration; msousa@455: msousa@439: /* Although a call to a non-declared FB is a semantic error, this is currently caught by stage 2! */ msousa@439: if (NULL == fb_decl) ERROR; msousa@439: if (NULL != symbol->il_operand_list) narrow_nonformal_call(symbol, fb_decl); msousa@439: if (NULL != symbol-> il_param_list) narrow_formal_call(symbol, fb_decl); msousa@439: msousa@455: /* Let the il_call_operator (CAL, CALC, or CALCN) set the datatype of prev_il_instruction... */ msousa@455: symbol->il_call_operator->datatype = symbol->datatype; msousa@455: symbol->il_call_operator->accept(*this); 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 *narrow_candidate_datatypes_c::visit(il_formal_funct_call_c *symbol) { 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: msousa@438: narrow_function_invocation(symbol, fcall_param); msousa@450: /* The desired datatype of the previous il instruction was already set by narrow_function_invocation() */ msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@452: // void *visit(il_operand_list_c *symbol); msousa@453: msousa@453: msousa@453: /* | simple_instr_list il_simple_instruction */ msousa@453: /* This object is referenced by il_expression_c objects */ msousa@452: void *narrow_candidate_datatypes_c::visit(simple_instr_list_c *symbol) { msousa@452: if (symbol->n > 0) msousa@452: symbol->elements[symbol->n - 1]->datatype = symbol->datatype; msousa@452: msousa@452: for(int i = symbol->n-1; i >= 0; i--) { msousa@452: symbol->elements[i]->accept(*this); msousa@452: } msousa@452: return NULL; msousa@452: } msousa@452: msousa@453: msousa@453: // SYM_REF1(il_simple_instruction_c, il_simple_instruction, symbol_c *prev_il_instruction;) msousa@453: void *narrow_candidate_datatypes_c::visit(il_simple_instruction_c *symbol) { msousa@459: if (symbol->prev_il_instruction.size() > 1) ERROR; /* There should be no labeled insructions inside an IL expression! */ msousa@459: msousa@459: il_instruction_c tmp_prev_il_instruction(NULL, NULL); msousa@459: /* the narrow algorithm will need access to the intersected candidate_datatype lists of all prev_il_instructions, as well as the msousa@459: * list of the prev_il_instructions. msousa@459: * Instead of creating two 'global' (within the class) variables, we create a single il_instruction_c variable (fake_prev_il_instruction), msousa@459: * and shove that data into this single variable. msousa@459: */ msousa@459: if (symbol->prev_il_instruction.size() > 0) msousa@459: tmp_prev_il_instruction.candidate_datatypes = symbol->prev_il_instruction[0]->candidate_datatypes; msousa@459: tmp_prev_il_instruction.prev_il_instruction = symbol->prev_il_instruction; msousa@459: msousa@459: /* copy the candidate_datatypes list */ msousa@459: fake_prev_il_instruction = &tmp_prev_il_instruction; msousa@453: symbol->il_simple_instruction->datatype = symbol->datatype; msousa@453: symbol->il_simple_instruction->accept(*this); msousa@459: fake_prev_il_instruction = NULL; msousa@453: return NULL; msousa@453: } msousa@453: msousa@452: // void *visit(il_param_list_c *symbol); msousa@452: // void *visit(il_param_assignment_c *symbol); msousa@452: // void *visit(il_param_out_assignment_c *symbol); msousa@452: msousa@417: msousa@417: /*******************/ msousa@417: /* B 2.2 Operators */ msousa@417: /*******************/ mjsousa@836: /* Sets the datatype of the il_operand, and calls it recursively! mjsousa@836: * mjsousa@836: * NOTE 1: the il_operand __may__ be pointing to a parenthesized list of IL instructions. mjsousa@836: * e.g. LD 33 mjsousa@836: * AND ( 45 mjsousa@836: * OR 56 mjsousa@836: * ) mjsousa@836: * When we handle the first 'AND' IL_operator, the il_operand will point to an simple_instr_list_c. mjsousa@836: * In this case, when we call il_operand->accept(*this);, the prev_il_instruction pointer will be overwritten! mjsousa@836: * mjsousa@836: * So, if yoy wish to set the prev_il_instruction->datatype = symbol->datatype; mjsousa@836: * do it __before__ calling set_il_operand_datatype() (which in turn calls il_operand->accept(*this)) !! mjsousa@836: */ mjsousa@836: void *narrow_candidate_datatypes_c::set_il_operand_datatype(symbol_c *il_operand, symbol_c *datatype) { mjsousa@836: if (NULL == il_operand) return NULL; /* if no IL operand => error in the source code!! */ mjsousa@836: mjsousa@836: /* If il_operand already has a non-NULL datatype (remember, narrow algorithm runs twice over IL lists!), mjsousa@836: * but narrow algorithm has not yet been able to determine what datatype it should take? This is strange, mjsousa@836: * and most probably an error! mjsousa@836: */ mjsousa@838: if ((NULL != il_operand->datatype) && (NULL == datatype)) ERROR; mjsousa@836: mjsousa@836: /* If the il_operand's datatype has already been set previously, and mjsousa@836: * the narrow algorithm has already determined the datatype the il_operand should take! mjsousa@836: * ...we just make sure that the new datatype is the same as the current il_operand's datatype mjsousa@836: */ mjsousa@836: if ((NULL != il_operand->datatype) && (NULL != datatype)) { mjsousa@838: /* Both datatypes are an invalid_type_name_c. This implies they are the same!! */ mjsousa@838: if ((!get_datatype_info_c::is_type_valid(datatype)) && ((!get_datatype_info_c::is_type_valid(il_operand->datatype)))) mjsousa@838: return NULL;; mjsousa@836: /* OK, so both the datatypes are valid, but are they equal? */ mjsousa@838: if ( !get_datatype_info_c::is_type_equal(il_operand->datatype, datatype)) mjsousa@838: ERROR; mjsousa@836: /* The datatypes are the same. We have nothing to do, so we simply return! */ mjsousa@836: return NULL; mjsousa@836: } mjsousa@836: mjsousa@836: /* Set the il_operand's datatype. Note that the new 'datatype' may even be NULL!!! */ mjsousa@836: il_operand->datatype = datatype; mjsousa@836: /* Even if we are not able to determine the il_operand's datatype ('datatype' is NULL), we still visit it recursively, mjsousa@836: * to give a chance of any complex expressions embedded in the il_operand (e.g. expressions inside array subscripts!) mjsousa@836: * to be narrowed too. mjsousa@836: */ mjsousa@836: il_operand->accept(*this); mjsousa@836: return NULL; mjsousa@836: } mjsousa@836: mjsousa@836: mjsousa@836: mjsousa@836: msousa@480: void *narrow_candidate_datatypes_c::narrow_binary_operator(const struct widen_entry widen_table[], symbol_c *symbol, bool *deprecated_operation) { msousa@478: symbol_c *prev_instruction_type, *operand_type; msousa@478: int count = 0; msousa@478: msousa@480: if (NULL != deprecated_operation) msousa@480: *deprecated_operation = false; msousa@480: mjsousa@834: if (NULL == il_operand) return NULL; /* if no IL operand => error in the source code!! */ mjsousa@834: mjsousa@834: /* NOTE 1: the il_operand __may__ be pointing to a parenthesized list of IL instructions. msousa@478: * e.g. LD 33 msousa@478: * AND ( 45 msousa@478: * OR 56 msousa@478: * ) msousa@478: * When we handle the first 'AND' IL_operator, the il_operand will point to an simple_instr_list_c. msousa@478: * In this case, when we call il_operand->accept(*this);, the prev_il_instruction pointer will be overwritten! msousa@478: * msousa@478: * We must therefore set the prev_il_instruction->datatype = symbol->datatype; msousa@478: * __before__ calling il_operand->accept(*this) !! msousa@478: * msousa@478: * NOTE 2: We do not need to call prev_il_instruction->accept(*this), as the object to which prev_il_instruction msousa@478: * is pointing to will be later narrowed by the call from the for() loop of the instruction_list_c msousa@478: * (or simple_instr_list_c), which iterates backwards. msousa@478: */ mjsousa@836: if (NULL != symbol->datatype) { // next IL instructions were able to determine the datatype this instruction should produce mjsousa@836: for(unsigned int i = 0; i < fake_prev_il_instruction->candidate_datatypes.size(); i++) { mjsousa@836: for(unsigned int j = 0; j < il_operand->candidate_datatypes.size(); j++) { mjsousa@836: prev_instruction_type = fake_prev_il_instruction->candidate_datatypes[i]; mjsousa@836: operand_type = il_operand->candidate_datatypes[j]; mjsousa@836: if (is_widening_compatible(widen_table, prev_instruction_type, operand_type, symbol->datatype, deprecated_operation)) { mjsousa@836: /* set the desired datatype of the previous il instruction */ mjsousa@836: set_datatype_in_prev_il_instructions(prev_instruction_type, fake_prev_il_instruction); mjsousa@836: /* set the datatype for the operand */ mjsousa@836: set_il_operand_datatype(il_operand, operand_type); mjsousa@836: mjsousa@836: /* NOTE: DO NOT search any further! Return immediately! mjsousa@836: * Since we support SAFE*** datatypes, multiple entries in the widen_table may be compatible. mjsousa@836: * If we try to set more than one distinct datatype on the same symbol, then the datatype will be set to mjsousa@836: * an invalid_datatype, which is NOT what we want! mjsousa@836: */ mjsousa@836: return NULL; mjsousa@836: } msousa@478: } msousa@478: } msousa@478: } mjsousa@836: /* We were not able to determine the required datatype, but we still give the il_operand a chance to be narrowed! */ mjsousa@836: set_il_operand_datatype(il_operand, NULL); msousa@478: return NULL; msousa@478: } msousa@478: msousa@478: msousa@443: msousa@480: mjsousa@838: /* Narrow IL operators whose execution is conditional on the boolean value in the accumulator. mjsousa@838: * Basically, narrow the JMPC, JMPCN, RETC, RETCN, CALC, and CALCN operators! mjsousa@838: * Also does part of the S and R operator narrowing!!! mjsousa@838: */ mjsousa@838: void *narrow_candidate_datatypes_c::narrow_conditional_operator(symbol_c *symbol) { mjsousa@838: /* if the next IL instructions needs us to provide a datatype other than a BOOL or a SAFEBOOL, mjsousa@838: * then we have an internal compiler error - most likely in fill_candidate_datatypes_c mjsousa@838: */ mjsousa@838: // I (mario) am confident the fill/narrow algorithms are working correctly, so for now we can disable the assertions! mjsousa@838: //if ((NULL != symbol->datatype) && (!get_datatype_info_c::is_BOOL_compatible(symbol->datatype))) ERROR; mjsousa@838: //if (symbol->candidate_datatypes.size() > 2) ERROR; /* may contain, at most, a BOOL and a SAFEBOOL */ mjsousa@838: mjsousa@838: /* NOTE: If there is no IL instruction following this S, R, CALC, CALCN, JMPC, JMPCN, RETC, or RETCN instruction, mjsousa@838: * we must still provide a bool_type_name_c datatype (if possible, i.e. if it exists in the candidate datatype list). mjsousa@838: * If it is not possible, we set it to NULL mjsousa@838: * mjsousa@838: * NOTE: Note that this algorithm we are implementing is slightly wrong. mjsousa@838: * (a) It ignores that a SAFEBOOL may be needed instead of a BOOL datatype. mjsousa@838: * (b) It also ignores that this method gets to be called twice on the same mjsousa@838: * object (the narrow algorithm runs through the IL list twice in order to mjsousa@838: * handle forward JMPs), so the assumption that we must immediately set our mjsousa@838: * own datatype if we get called with a NULL symbol->datatype is incorrect mjsousa@838: * (it may be that the second time it is called it will be with the correct datatype!). mjsousa@838: * mjsousa@838: * These two issues (a) and (b) together means that we should only really be setting our own mjsousa@838: * datatype if we are certain that the following IL instructions will never set it for us mjsousa@838: * - basically if the following IL instruction is a LD, or a JMP to a LD, or a JMP to a JMP to a LD, mjsousa@838: * etc..., or a conditional JMP whose both branches go to LD, etc...!!! mjsousa@838: * mjsousa@838: * At the moment, it seems to me that we would need to write a visitor class to do this for us! mjsousa@838: * I currently have other things on my mind at the moment, so I will leave this for later... mjsousa@838: * For the moment we just set it to BOOL, and ignore the support of SAFEBOOL! mjsousa@838: */ mjsousa@838: if (NULL == symbol->datatype) set_datatype(&get_datatype_info_c::bool_type_name /* datatype*/, symbol /* symbol */); mjsousa@838: if (NULL == symbol->datatype) ERROR; // the BOOL is not on the candidate_datatypes! Strange... Probably a bug in fill_candidate_datatype_c mjsousa@838: mjsousa@838: /* set the required datatype of the previous IL instruction, i.e. a bool_type_name_c! */ mjsousa@838: set_datatype_in_prev_il_instructions(symbol->datatype, fake_prev_il_instruction); mjsousa@838: return NULL; mjsousa@838: } mjsousa@838: mjsousa@838: mjsousa@838: mjsousa@838: void *narrow_candidate_datatypes_c::narrow_S_and_R_operator(symbol_c *symbol, const char *param_name, symbol_c *called_fb_declaration) { mjsousa@838: if (NULL != called_fb_declaration) mjsousa@838: /* FB call semantics */ mjsousa@838: return narrow_implicit_il_fb_call(symbol, param_name, called_fb_declaration); mjsousa@838: mjsousa@838: /* Set/Reset semantics */ mjsousa@838: narrow_conditional_operator(symbol); mjsousa@838: /* set the datatype for the il_operand */ mjsousa@838: if ((NULL != il_operand) && (il_operand->candidate_datatypes.size() > 0)) mjsousa@838: set_il_operand_datatype(il_operand, il_operand->candidate_datatypes[0]); mjsousa@836: return NULL; mjsousa@836: } mjsousa@836: mjsousa@836: mjsousa@836: mjsousa@839: void *narrow_candidate_datatypes_c::narrow_store_operator(symbol_c *symbol) { mjsousa@836: if (symbol->candidate_datatypes.size() == 1) { mjsousa@836: symbol->datatype = symbol->candidate_datatypes[0]; mjsousa@836: /* set the desired datatype of the previous il instruction */ mjsousa@836: set_datatype_in_prev_il_instructions(symbol->datatype, fake_prev_il_instruction); mjsousa@836: /* In the case of the ST operator, we must set the datatype of the il_instruction_c object that points to this ST_operator_c ourselves, mjsousa@836: * since the following il_instruction_c objects have not done it, as is normal/standard for other instructions! mjsousa@836: */ mjsousa@836: current_il_instruction->datatype = symbol->datatype; mjsousa@836: } mjsousa@834: mjsousa@834: /* set the datatype for the operand */ mjsousa@836: set_il_operand_datatype(il_operand, symbol->datatype); msousa@417: return NULL; msousa@417: } msousa@417: mjsousa@839: mjsousa@839: mjsousa@839: void *narrow_candidate_datatypes_c::visit( LD_operator_c *symbol) {return set_il_operand_datatype(il_operand, symbol->datatype);} mjsousa@839: void *narrow_candidate_datatypes_c::visit( LDN_operator_c *symbol) {return set_il_operand_datatype(il_operand, symbol->datatype);} mjsousa@839: mjsousa@839: void *narrow_candidate_datatypes_c::visit( ST_operator_c *symbol) {return narrow_store_operator(symbol);} mjsousa@839: void *narrow_candidate_datatypes_c::visit( STN_operator_c *symbol) {return narrow_store_operator(symbol);} mjsousa@838: mjsousa@838: mjsousa@838: /* NOTE: the standard allows syntax in which the NOT operator is followed by an optional mjsousa@838: * NOT [] mjsousa@838: * However, it does not define the semantic of the NOT operation when the is specified. mjsousa@838: * We therefore consider it an error if an il_operand is specified! mjsousa@838: * This error will be detected in print_datatypes_error_c!! mjsousa@838: */ mjsousa@838: /* This operator does not change the data type, it simply inverts the bits in the ANT_BIT data types! */ mjsousa@838: /* So, we merely set the desired datatype of the previous il instruction */ mjsousa@838: void *narrow_candidate_datatypes_c::visit( NOT_operator_c *symbol) {set_datatype_in_prev_il_instructions(symbol->datatype, fake_prev_il_instruction);return NULL;} mjsousa@838: mjsousa@838: void *narrow_candidate_datatypes_c::visit( S_operator_c *symbol) {return narrow_S_and_R_operator (symbol, "S", symbol->called_fb_declaration);} mjsousa@838: void *narrow_candidate_datatypes_c::visit( R_operator_c *symbol) {return narrow_S_and_R_operator (symbol, "R", symbol->called_fb_declaration);} msousa@448: msousa@456: void *narrow_candidate_datatypes_c::visit( S1_operator_c *symbol) {return narrow_implicit_il_fb_call(symbol, "S1", symbol->called_fb_declaration);} msousa@456: void *narrow_candidate_datatypes_c::visit( R1_operator_c *symbol) {return narrow_implicit_il_fb_call(symbol, "R1", symbol->called_fb_declaration);} msousa@456: void *narrow_candidate_datatypes_c::visit( CLK_operator_c *symbol) {return narrow_implicit_il_fb_call(symbol, "CLK", symbol->called_fb_declaration);} msousa@456: void *narrow_candidate_datatypes_c::visit( CU_operator_c *symbol) {return narrow_implicit_il_fb_call(symbol, "CU", symbol->called_fb_declaration);} msousa@456: void *narrow_candidate_datatypes_c::visit( CD_operator_c *symbol) {return narrow_implicit_il_fb_call(symbol, "CD", symbol->called_fb_declaration);} msousa@456: void *narrow_candidate_datatypes_c::visit( PV_operator_c *symbol) {return narrow_implicit_il_fb_call(symbol, "PV", symbol->called_fb_declaration);} msousa@456: void *narrow_candidate_datatypes_c::visit( IN_operator_c *symbol) {return narrow_implicit_il_fb_call(symbol, "IN", symbol->called_fb_declaration);} msousa@456: void *narrow_candidate_datatypes_c::visit( PT_operator_c *symbol) {return narrow_implicit_il_fb_call(symbol, "PT", symbol->called_fb_declaration);} msousa@456: msousa@481: void *narrow_candidate_datatypes_c::visit( AND_operator_c *symbol) {return narrow_binary_operator(widen_AND_table, symbol);} msousa@481: void *narrow_candidate_datatypes_c::visit( OR_operator_c *symbol) {return narrow_binary_operator( widen_OR_table, symbol);} msousa@481: void *narrow_candidate_datatypes_c::visit( XOR_operator_c *symbol) {return narrow_binary_operator(widen_XOR_table, symbol);} msousa@483: void *narrow_candidate_datatypes_c::visit(ANDN_operator_c *symbol) {return narrow_binary_operator(widen_AND_table, symbol);} msousa@483: void *narrow_candidate_datatypes_c::visit( ORN_operator_c *symbol) {return narrow_binary_operator( widen_OR_table, symbol);} msousa@483: void *narrow_candidate_datatypes_c::visit(XORN_operator_c *symbol) {return narrow_binary_operator(widen_XOR_table, symbol);} msousa@480: void *narrow_candidate_datatypes_c::visit( ADD_operator_c *symbol) {return narrow_binary_operator(widen_ADD_table, symbol, &(symbol->deprecated_operation));} msousa@480: void *narrow_candidate_datatypes_c::visit( SUB_operator_c *symbol) {return narrow_binary_operator(widen_SUB_table, symbol, &(symbol->deprecated_operation));} msousa@480: void *narrow_candidate_datatypes_c::visit( MUL_operator_c *symbol) {return narrow_binary_operator(widen_MUL_table, symbol, &(symbol->deprecated_operation));} msousa@480: void *narrow_candidate_datatypes_c::visit( DIV_operator_c *symbol) {return narrow_binary_operator(widen_DIV_table, symbol, &(symbol->deprecated_operation));} msousa@480: void *narrow_candidate_datatypes_c::visit( MOD_operator_c *symbol) {return narrow_binary_operator(widen_MOD_table, symbol);} msousa@484: void *narrow_candidate_datatypes_c::visit( GT_operator_c *symbol) {return narrow_binary_operator(widen_CMP_table, symbol);} msousa@484: void *narrow_candidate_datatypes_c::visit( GE_operator_c *symbol) {return narrow_binary_operator(widen_CMP_table, symbol);} msousa@484: void *narrow_candidate_datatypes_c::visit( EQ_operator_c *symbol) {return narrow_binary_operator(widen_CMP_table, symbol);} msousa@484: void *narrow_candidate_datatypes_c::visit( LT_operator_c *symbol) {return narrow_binary_operator(widen_CMP_table, symbol);} msousa@484: void *narrow_candidate_datatypes_c::visit( LE_operator_c *symbol) {return narrow_binary_operator(widen_CMP_table, symbol);} msousa@484: void *narrow_candidate_datatypes_c::visit( NE_operator_c *symbol) {return narrow_binary_operator(widen_CMP_table, symbol);} msousa@448: msousa@417: mjsousa@838: /* visitors to CAL_operator_c, CALC_operator_c and CALCN_operator_c are called from visit(il_fb_call_c *) {symbol->il_call_operator->accept(*this)} */ msousa@487: /* NOTE: The CAL, JMP and RET instructions simply set the desired datatype of the previous il instruction since they do not change the value in the current/default IL variable */ msousa@487: void *narrow_candidate_datatypes_c::visit( CAL_operator_c *symbol) {set_datatype_in_prev_il_instructions(symbol->datatype, fake_prev_il_instruction); return NULL;} msousa@487: void *narrow_candidate_datatypes_c::visit( RET_operator_c *symbol) {set_datatype_in_prev_il_instructions(symbol->datatype, fake_prev_il_instruction); return NULL;} msousa@487: void *narrow_candidate_datatypes_c::visit( JMP_operator_c *symbol) {set_datatype_in_prev_il_instructions(symbol->datatype, fake_prev_il_instruction); return NULL;} mjsousa@838: void *narrow_candidate_datatypes_c::visit( CALC_operator_c *symbol) {return narrow_conditional_operator(symbol);} mjsousa@838: void *narrow_candidate_datatypes_c::visit(CALCN_operator_c *symbol) {return narrow_conditional_operator(symbol);} mjsousa@838: void *narrow_candidate_datatypes_c::visit( RETC_operator_c *symbol) {return narrow_conditional_operator(symbol);} mjsousa@838: void *narrow_candidate_datatypes_c::visit(RETCN_operator_c *symbol) {return narrow_conditional_operator(symbol);} mjsousa@838: void *narrow_candidate_datatypes_c::visit( JMPC_operator_c *symbol) {return narrow_conditional_operator(symbol);} mjsousa@838: void *narrow_candidate_datatypes_c::visit(JMPCN_operator_c *symbol) {return narrow_conditional_operator(symbol);} 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: /***************************************/ msousa@417: /* B.3 - Language ST (Structured Text) */ msousa@417: /***************************************/ msousa@417: /***********************/ msousa@417: /* B 3.1 - Expressions */ msousa@417: /***********************/ mjsousa@873: /* SYM_REF1(ref_expression_c, exp) --> an extension to the IEC 61131-3 standard - based on the IEC 61131-3 v3 standard. Returns address of the varible! */ mjsousa@873: void *narrow_candidate_datatypes_c::visit( ref_expression_c *symbol) { mjsousa@873: if (symbol->exp->candidate_datatypes.size() > 0) { mjsousa@873: symbol->exp->datatype = symbol->exp->candidate_datatypes[0]; /* just use the first possible datatype */ mjsousa@873: symbol->exp->accept(*this); mjsousa@873: } mjsousa@873: return NULL; mjsousa@873: } mjsousa@873: mjsousa@873: mjsousa@873: msousa@652: /* allow_enums is FALSE by default!! msousa@652: * deprecated_operation is NULL by default!! msousa@652: * if (allow_enums) then consider that we are ectually processing an equ_expression or notequ_expression, where two enums of the same data type may also be legally compared msousa@652: * e.g. symbol := l_expr == r_expr msousa@652: * symbol := l_expr != r_expr msousa@652: * In the above situation it is a legal operation when (l_expr.datatype == r_expr.datatype) && is_enumerated(r/l_expr.datatype) && is_bool(symbol.datatype) msousa@652: */ msousa@652: void *narrow_candidate_datatypes_c::narrow_binary_expression(const struct widen_entry widen_table[], symbol_c *symbol, symbol_c *l_expr, symbol_c *r_expr, bool *deprecated_operation, bool allow_enums) { msousa@480: symbol_c *l_type, *r_type; msousa@480: int count = 0; msousa@652: msousa@652: if (NULL != deprecated_operation) msousa@480: *deprecated_operation = false; msousa@480: msousa@480: for(unsigned int i = 0; i < l_expr->candidate_datatypes.size(); i++) { msousa@480: for(unsigned int j = 0; j < r_expr->candidate_datatypes.size(); j++) { msousa@480: /* test widening compatibility */ msousa@480: l_type = l_expr->candidate_datatypes[i]; msousa@480: r_type = r_expr->candidate_datatypes[j]; msousa@652: if (is_widening_compatible(widen_table, l_type, r_type, symbol->datatype, deprecated_operation)) { msousa@652: l_expr->datatype = l_type; msousa@652: r_expr->datatype = r_type; msousa@652: count ++; msousa@854: } else if ((l_type == r_type) && get_datatype_info_c::is_enumerated(l_type) && get_datatype_info_c::is_BOOL_compatible(symbol->datatype)) { msousa@652: if (NULL != deprecated_operation) *deprecated_operation = false; msousa@480: l_expr->datatype = l_type; msousa@480: r_expr->datatype = r_type; msousa@480: count ++; msousa@480: } msousa@652: msousa@480: } msousa@480: } msousa@480: // if (count > 1) ERROR; /* Since we also support SAFE data types, this assertion is not necessarily always tru! */ msousa@676: if (get_datatype_info_c::is_type_valid(symbol->datatype) && (count <= 0)) ERROR; msousa@480: msousa@480: l_expr->accept(*this); msousa@480: r_expr->accept(*this); msousa@480: return NULL; msousa@480: } msousa@480: msousa@417: msousa@652: void *narrow_candidate_datatypes_c::narrow_equality_comparison(const struct widen_entry widen_table[], symbol_c *symbol, symbol_c *l_expr, symbol_c *r_expr, bool *deprecated_operation) { msousa@652: return narrow_binary_expression(widen_table, symbol, l_expr, r_expr, deprecated_operation, true); msousa@652: } msousa@652: msousa@652: msousa@652: void *narrow_candidate_datatypes_c::visit( or_expression_c *symbol) {return narrow_binary_expression ( widen_OR_table, symbol, symbol->l_exp, symbol->r_exp);} msousa@652: void *narrow_candidate_datatypes_c::visit( xor_expression_c *symbol) {return narrow_binary_expression (widen_XOR_table, symbol, symbol->l_exp, symbol->r_exp);} msousa@652: void *narrow_candidate_datatypes_c::visit( and_expression_c *symbol) {return narrow_binary_expression (widen_AND_table, symbol, symbol->l_exp, symbol->r_exp);} msousa@652: msousa@652: void *narrow_candidate_datatypes_c::visit( equ_expression_c *symbol) {return narrow_equality_comparison(widen_CMP_table, symbol, symbol->l_exp, symbol->r_exp);} msousa@652: void *narrow_candidate_datatypes_c::visit(notequ_expression_c *symbol) {return narrow_equality_comparison(widen_CMP_table, symbol, symbol->l_exp, symbol->r_exp);} msousa@652: void *narrow_candidate_datatypes_c::visit( lt_expression_c *symbol) {return narrow_binary_expression (widen_CMP_table, symbol, symbol->l_exp, symbol->r_exp);} msousa@652: void *narrow_candidate_datatypes_c::visit( gt_expression_c *symbol) {return narrow_binary_expression (widen_CMP_table, symbol, symbol->l_exp, symbol->r_exp);} msousa@652: void *narrow_candidate_datatypes_c::visit( le_expression_c *symbol) {return narrow_binary_expression (widen_CMP_table, symbol, symbol->l_exp, symbol->r_exp);} msousa@652: void *narrow_candidate_datatypes_c::visit( ge_expression_c *symbol) {return narrow_binary_expression (widen_CMP_table, symbol, symbol->l_exp, symbol->r_exp);} msousa@652: msousa@652: void *narrow_candidate_datatypes_c::visit( add_expression_c *symbol) {return narrow_binary_expression (widen_ADD_table, symbol, symbol->l_exp, symbol->r_exp, &symbol->deprecated_operation);} msousa@652: void *narrow_candidate_datatypes_c::visit( sub_expression_c *symbol) {return narrow_binary_expression (widen_SUB_table, symbol, symbol->l_exp, symbol->r_exp, &symbol->deprecated_operation);} msousa@652: void *narrow_candidate_datatypes_c::visit( mul_expression_c *symbol) {return narrow_binary_expression (widen_MUL_table, symbol, symbol->l_exp, symbol->r_exp, &symbol->deprecated_operation);} msousa@652: void *narrow_candidate_datatypes_c::visit( div_expression_c *symbol) {return narrow_binary_expression (widen_DIV_table, symbol, symbol->l_exp, symbol->r_exp, &symbol->deprecated_operation);} msousa@652: void *narrow_candidate_datatypes_c::visit( mod_expression_c *symbol) {return narrow_binary_expression (widen_MOD_table, symbol, symbol->l_exp, symbol->r_exp);} msousa@652: void *narrow_candidate_datatypes_c::visit( power_expression_c *symbol) {return narrow_binary_expression (widen_EXPT_table,symbol, symbol->l_exp, symbol->r_exp);} msousa@417: msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(neg_expression_c *symbol) { msousa@417: symbol->exp->datatype = symbol->datatype; msousa@417: symbol->exp->accept(*this); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(not_expression_c *symbol) { msousa@417: symbol->exp->datatype = symbol->datatype; msousa@417: symbol->exp->accept(*this); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@438: msousa@438: /* NOTE: The parameter 'called_function_declaration', 'extensible_param_count' and 'candidate_functions' are used to pass data between the stage 3 and stage 4. */ msousa@438: /* formal_param_list -> may be NULL ! */ msousa@438: /* nonformal_param_list -> may be NULL ! */ msousa@438: // SYM_REF3(function_invocation_c, function_name, formal_param_list, nonformal_param_list, symbol_c *called_function_declaration; int extensible_param_count; std::vector candidate_functions;) msousa@417: void *narrow_candidate_datatypes_c::visit(function_invocation_c *symbol) { 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: msousa@438: narrow_function_invocation(symbol, fcall_param); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: /********************/ msousa@417: /* B 3.2 Statements */ msousa@417: /********************/ msousa@417: msousa@417: msousa@417: /*********************************/ msousa@417: /* B 3.2.1 Assignment Statements */ msousa@417: /*********************************/ msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(assignment_statement_c *symbol) { msousa@417: if (symbol->candidate_datatypes.size() != 1) msousa@417: return NULL; msousa@417: symbol->datatype = symbol->candidate_datatypes[0]; msousa@417: symbol->l_exp->datatype = symbol->datatype; msousa@417: symbol->l_exp->accept(*this); msousa@417: symbol->r_exp->datatype = symbol->datatype; msousa@417: symbol->r_exp->accept(*this); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@417: /*****************************************/ msousa@417: /* B 3.2.2 Subprogram Control Statements */ msousa@417: /*****************************************/ msousa@417: msousa@423: void *narrow_candidate_datatypes_c::visit(fb_invocation_c *symbol) { msousa@424: /* Note: We do not use the symbol->called_fb_declaration value (set in fill_candidate_datatypes_c) msousa@424: * because we try to identify any other datatype errors in the expressions used in the msousa@424: * parameters to the FB call (e.g. fb_var(var1 * 56 + func(var * 43)) ) msousa@424: * even it the call to the FB is invalid. msousa@424: * This makes sense because it may be errors in those expressions which are msousa@424: * making this an invalid call, so it makes sense to point them out to the user! msousa@424: */ msousa@423: symbol_c *fb_decl = search_varfb_instance_type->get_basetype_decl(symbol->fb_name); msousa@424: msousa@424: /* Although a call to a non-declared FB is a semantic error, this is currently caught by stage 2! */ msousa@423: if (NULL == fb_decl) ERROR; msousa@423: if (NULL != symbol->nonformal_param_list) narrow_nonformal_call(symbol, fb_decl); msousa@423: if (NULL != symbol-> formal_param_list) narrow_formal_call(symbol, fb_decl); msousa@423: msousa@423: return NULL; msousa@423: } msousa@423: msousa@423: msousa@417: /********************************/ msousa@417: /* B 3.2.3 Selection Statements */ msousa@417: /********************************/ msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(if_statement_c *symbol) { msousa@417: for(unsigned int i = 0; i < symbol->expression->candidate_datatypes.size(); i++) { msousa@666: if (get_datatype_info_c::is_BOOL_compatible(symbol->expression->candidate_datatypes[i])) msousa@417: symbol->expression->datatype = symbol->expression->candidate_datatypes[i]; msousa@417: } 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 *narrow_candidate_datatypes_c::visit(elseif_statement_c *symbol) { msousa@417: for (unsigned int i = 0; i < symbol->expression->candidate_datatypes.size(); i++) { msousa@666: if (get_datatype_info_c::is_BOOL_compatible(symbol->expression->candidate_datatypes[i])) msousa@417: symbol->expression->datatype = symbol->expression->candidate_datatypes[i]; msousa@417: } 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 *narrow_candidate_datatypes_c::visit(case_statement_c *symbol) { msousa@417: for (unsigned int i = 0; i < symbol->expression->candidate_datatypes.size(); i++) { msousa@666: if ((get_datatype_info_c::is_ANY_INT(symbol->expression->candidate_datatypes[i])) msousa@854: || (get_datatype_info_c::is_enumerated(symbol->expression->candidate_datatypes[i]))) msousa@417: symbol->expression->datatype = symbol->expression->candidate_datatypes[i]; msousa@417: } 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->case_element_list) { msousa@417: symbol->case_element_list->datatype = symbol->expression->datatype; msousa@417: symbol->case_element_list->accept(*this); msousa@417: } msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: /* helper symbol for case_statement */ msousa@417: // SYM_LIST(case_element_list_c) msousa@417: void *narrow_candidate_datatypes_c::visit(case_element_list_c *symbol) { msousa@417: for (int i = 0; i < symbol->n; i++) { msousa@417: symbol->elements[i]->datatype = symbol->datatype; msousa@417: symbol->elements[i]->accept(*this); msousa@417: } msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: /* case_list ':' statement_list */ msousa@417: // SYM_REF2(case_element_c, case_list, statement_list) msousa@417: void *narrow_candidate_datatypes_c::visit(case_element_c *symbol) { msousa@417: symbol->case_list->datatype = symbol->datatype; msousa@417: symbol->case_list->accept(*this); msousa@417: symbol->statement_list->accept(*this); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: // SYM_LIST(case_list_c) msousa@417: void *narrow_candidate_datatypes_c::visit(case_list_c *symbol) { msousa@417: for (int i = 0; i < symbol->n; i++) { msousa@417: for (unsigned int k = 0; k < symbol->elements[i]->candidate_datatypes.size(); k++) { msousa@676: if (get_datatype_info_c::is_type_equal(symbol->datatype, symbol->elements[i]->candidate_datatypes[k])) msousa@417: symbol->elements[i]->datatype = symbol->elements[i]->candidate_datatypes[k]; msousa@417: } msousa@417: /* NOTE: this may be an integer, a subrange_c, or a enumerated value! */ msousa@417: symbol->elements[i]->accept(*this); msousa@417: } msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@417: /********************************/ msousa@417: /* B 3.2.4 Iteration Statements */ msousa@417: /********************************/ msousa@417: void *narrow_candidate_datatypes_c::visit(for_statement_c *symbol) { msousa@417: /* Control variable */ msousa@417: for(unsigned int i = 0; i < symbol->control_variable->candidate_datatypes.size(); i++) { msousa@666: if (get_datatype_info_c::is_ANY_INT(symbol->control_variable->candidate_datatypes[i])) { msousa@417: symbol->control_variable->datatype = symbol->control_variable->candidate_datatypes[i]; msousa@417: } msousa@417: } msousa@417: symbol->control_variable->accept(*this); msousa@417: /* BEG expression */ msousa@417: for(unsigned int i = 0; i < symbol->beg_expression->candidate_datatypes.size(); i++) { msousa@676: if (get_datatype_info_c::is_type_equal(symbol->control_variable->datatype,symbol->beg_expression->candidate_datatypes[i]) && msousa@666: get_datatype_info_c::is_ANY_INT(symbol->beg_expression->candidate_datatypes[i])) { msousa@417: symbol->beg_expression->datatype = symbol->beg_expression->candidate_datatypes[i]; msousa@417: } msousa@417: } msousa@417: symbol->beg_expression->accept(*this); msousa@417: /* END expression */ msousa@417: for(unsigned int i = 0; i < symbol->end_expression->candidate_datatypes.size(); i++) { msousa@676: if (get_datatype_info_c::is_type_equal(symbol->control_variable->datatype,symbol->end_expression->candidate_datatypes[i]) && msousa@666: get_datatype_info_c::is_ANY_INT(symbol->end_expression->candidate_datatypes[i])) { msousa@417: symbol->end_expression->datatype = symbol->end_expression->candidate_datatypes[i]; msousa@417: } msousa@417: } msousa@417: symbol->end_expression->accept(*this); msousa@417: /* BY expression */ msousa@417: if (NULL != symbol->by_expression) { msousa@417: for(unsigned int i = 0; i < symbol->by_expression->candidate_datatypes.size(); i++) { msousa@676: if (get_datatype_info_c::is_type_equal(symbol->control_variable->datatype,symbol->by_expression->candidate_datatypes[i]) && msousa@666: get_datatype_info_c::is_ANY_INT(symbol->by_expression->candidate_datatypes[i])) { msousa@417: symbol->by_expression->datatype = symbol->by_expression->candidate_datatypes[i]; msousa@417: } msousa@417: } msousa@417: symbol->by_expression->accept(*this); msousa@417: } msousa@417: if (NULL != symbol->statement_list) msousa@417: symbol->statement_list->accept(*this); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(while_statement_c *symbol) { msousa@417: for (unsigned int i = 0; i < symbol->expression->candidate_datatypes.size(); i++) { msousa@666: if(get_datatype_info_c::is_BOOL(symbol->expression->candidate_datatypes[i])) msousa@417: symbol->expression->datatype = symbol->expression->candidate_datatypes[i]; msousa@417: } 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: void *narrow_candidate_datatypes_c::visit(repeat_statement_c *symbol) { msousa@417: for (unsigned int i = 0; i < symbol->expression->candidate_datatypes.size(); i++) { msousa@666: if(get_datatype_info_c::is_BOOL(symbol->expression->candidate_datatypes[i])) msousa@417: symbol->expression->datatype = symbol->expression->candidate_datatypes[i]; msousa@417: } 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: