msousa@417: /* msousa@417: * matiec - a compiler for the programming languages defined in IEC 61131-3 msousa@417: * msousa@417: * Copyright (C) 2009-2012 Mario de Sousa (msousa@fe.up.pt) msousa@417: * Copyright (C) 2012 Manuele Conti (manuele.conti@sirius-es.it) msousa@417: * Copyright (C) 2012 Matteo Facchinetti (matteo.facchinetti@sirius-es.it) msousa@417: * msousa@417: * This program is free software: you can redistribute it and/or modify msousa@417: * it under the terms of the GNU General Public License as published by msousa@417: * the Free Software Foundation, either version 3 of the License, or msousa@417: * (at your option) any later version. msousa@417: * msousa@417: * This program is distributed in the hope that it will be useful, msousa@417: * but WITHOUT ANY WARRANTY; without even the implied warranty of msousa@417: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the msousa@417: * GNU General Public License for more details. msousa@417: * msousa@417: * You should have received a copy of the GNU General Public License msousa@417: * along with this program. If not, see . msousa@417: * msousa@417: * msousa@417: * This code is made available on the understanding that it will not be msousa@417: * used in safety-critical situations without a full and competent review. msousa@417: */ msousa@417: msousa@417: /* msousa@417: * An IEC 61131-3 compiler. msousa@417: * msousa@417: * Based on the msousa@417: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) msousa@417: * msousa@417: */ msousa@417: msousa@417: msousa@417: /* msousa@417: * Narrow class select and store a data type from candidate data types list for all symbols msousa@417: */ msousa@417: 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) { 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@425: symbol->datatype = NULL; conti@445: if (search_in_candidate_datatype_list(datatype, symbol->candidate_datatypes) >= 0) msousa@425: symbol->datatype = datatype; msousa@425: } msousa@425: msousa@425: msousa@425: msousa@425: msousa@417: bool narrow_candidate_datatypes_c::is_widening_compatible(symbol_c *left_type, symbol_c *right_type, symbol_c *result_type, const struct widen_entry widen_table[]) { 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@417: && (typeid(*result_type) == typeid(*widen_table[k].result))) { 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@438: if (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@448: 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) { msousa@455: /* only set it if it is in the candidate datatypes list! */ msousa@455: set_datatype(called_fb_declaration, il_operand); msousa@455: il_operand->accept(*this); msousa@455: } msousa@455: symbol_c *fb_decl = il_operand->datatype; msousa@455: msousa@448: if (NULL == prev_il_instruction) { msousa@448: /* This IL implicit FB call (e.g. CLK ton_var) is not preceded by another IL instruction msousa@448: * (or list of instructions) that will set the IL current/default value. msousa@451: * We cannot proceed verifying type compatibility of something that does not exist. msousa@448: */ msousa@448: return; msousa@448: } msousa@448: msousa@455: if (NULL == fb_decl) { msousa@455: /* the il_operand is a not FB instance */ msousa@455: /* so we simply pass on the required datatype to the prev_il_instruction */ msousa@455: /* The invalid FB invocation will be caught by the print_datatypes_error_c by analysing NULL value in il_operand->datatype! */ msousa@455: prev_il_instruction->datatype = il_instruction->datatype; msousa@455: return; 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@451: symbol_c param_value = *prev_il_instruction; msousa@455: 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: // copy_candidate_datatype_list(il_instruction/*from*/, &il_fb_call/*to*/); 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@451: * and we set it to NULL. 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@455: if ((NULL == il_instruction->datatype) || (is_type_equal(param_value.datatype, il_instruction->datatype))) { msousa@455: prev_il_instruction->datatype = param_value.datatype; msousa@455: } else { msousa@455: prev_il_instruction->datatype = NULL; msousa@455: } 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@417: /* NOTE: symbol == NULL is valid. It will occur when, for e.g., an undefined/undeclared symbolic_variable is used msousa@417: * in the code. msousa@417: */ msousa@423: if (symbol == NULL) return NULL; msousa@423: return (symbol_c *)symbol->accept(search_base_type); msousa@417: } msousa@417: msousa@417: /*********************/ msousa@417: /* B 1.2 - Constants */ msousa@417: /*********************/ msousa@417: msousa@417: /**********************/ msousa@417: /* B 1.3 - Data types */ msousa@417: /**********************/ msousa@417: /********************************/ msousa@417: /* B 1.3.3 - Derived data types */ msousa@417: /********************************/ msousa@417: /* signed_integer DOTDOT signed_integer */ msousa@417: // SYM_REF2(subrange_c, lower_limit, upper_limit) msousa@417: void *narrow_candidate_datatypes_c::visit(subrange_c *symbol) { msousa@417: symbol->lower_limit->datatype = symbol->datatype; msousa@417: symbol->lower_limit->accept(*this); msousa@417: symbol->upper_limit->datatype = symbol->datatype; msousa@417: symbol->upper_limit->accept(*this); msousa@417: return NULL; msousa@417: } msousa@417: msousa@443: /* simple_specification ASSIGN constant */ msousa@443: // SYM_REF2(simple_spec_init_c, simple_specification, constant) msousa@443: void *narrow_candidate_datatypes_c::visit(simple_spec_init_c *symbol) { msousa@443: symbol_c *datatype = base_type(symbol->simple_specification); msousa@443: if (NULL != symbol->constant) { conti@445: int typeoffset = search_in_candidate_datatype_list(datatype, symbol->constant->candidate_datatypes); msousa@443: if (typeoffset >= 0) msousa@443: symbol->constant->datatype = symbol->constant->candidate_datatypes[typeoffset]; msousa@443: } msousa@443: return NULL; msousa@443: } msousa@417: msousa@417: /*********************/ msousa@417: /* B 1.4 - Variables */ msousa@417: /*********************/ msousa@417: 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); 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@417: if (is_ANY_INT_type(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: 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@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; 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) { 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; 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) { 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; msousa@417: return NULL; msousa@417: } msousa@417: 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@443: for(int i = symbol->n-1; i >= 0; i--) { msousa@443: symbol->elements[i]->accept(*this); 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@448: if (NULL != symbol->prev_il_instruction) msousa@448: symbol->prev_il_instruction->datatype = symbol->datatype; msousa@448: } else { 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@448: symbol->il_instruction->datatype = symbol->datatype; msousa@450: prev_il_instruction = symbol->prev_il_instruction; msousa@448: symbol->il_instruction->accept(*this); msousa@450: prev_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@451: * This change will also be undone later in print_datatypes_error_c. msousa@451: */ msousa@451: symbol_c param_value; 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: if (NULL != prev_il_instruction) msousa@451: param_value = *prev_il_instruction; 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@451: if (NULL != prev_il_instruction) msousa@451: prev_il_instruction->datatype = param_value.datatype; 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@453: symbol_c *save_prev_il_instruction = prev_il_instruction; /*this is not really necessary, but lets play it safe */ msousa@452: symbol->simple_instr_list->accept(*this); msousa@452: prev_il_instruction = save_prev_il_instruction; msousa@417: return NULL; msousa@417: } msousa@417: msousa@439: /* il_call_operator prev_declared_fb_name msousa@439: * | il_call_operator prev_declared_fb_name '(' ')' msousa@439: * | il_call_operator prev_declared_fb_name '(' eol_list ')' msousa@439: * | il_call_operator prev_declared_fb_name '(' il_operand_list ')' msousa@439: * | il_call_operator prev_declared_fb_name '(' eol_list il_param_list ')' msousa@439: */ msousa@439: /* NOTE: The parameter 'called_fb_declaration'is used to pass data between stage 3 and stage4 (although currently it is not used in stage 4 */ msousa@439: // SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list, symbol_c *called_fb_declaration) msousa@417: void *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@453: prev_il_instruction = symbol->prev_il_instruction; msousa@453: symbol->il_simple_instruction->datatype = symbol->datatype; msousa@453: symbol->il_simple_instruction->accept(*this); msousa@453: 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: /*******************/ msousa@443: msousa@443: void *narrow_candidate_datatypes_c::handle_il_instruction(symbol_c *symbol) { msousa@443: if (NULL == symbol->datatype) msousa@443: /* next IL instructions were unable to determine the datatype this instruction should produce */ msousa@443: return NULL; msousa@454: /* NOTE 1: the il_operand __may__ be pointing to a parenthesized list of IL instructions. msousa@454: * e.g. LD 33 msousa@454: * AND ( 45 msousa@454: * OR 56 msousa@454: * ) msousa@454: * When we handle the first 'AND' IL_operator, the il_operand will point to an simple_instr_list_c. msousa@454: * In this case, when we call il_operand->accept(*this);, the prev_il_instruction pointer will be overwritten! msousa@454: * msousa@454: * We must therefore set the prev_il_instruction->datatype = symbol->datatype; msousa@454: * __before__ calling il_operand->accept(*this) !! msousa@454: * msousa@454: * NOTE 2: We do not need to call prev_il_instruction->accept(*this), as the object to which prev_il_instruction msousa@454: * is pointing to will be later narrowed by the call from the for() loop of the instruction_list_c msousa@454: * (or simple_instr_list_c), which iterates backwards. msousa@454: */ msousa@454: /* set the desired datatype of the previous il instruction */ msousa@454: prev_il_instruction->datatype = symbol->datatype; msousa@443: /* set the datatype for the operand */ msousa@443: il_operand->datatype = symbol->datatype; msousa@443: il_operand->accept(*this); msousa@443: return NULL; msousa@443: } msousa@443: msousa@443: void *narrow_candidate_datatypes_c::visit(LD_operator_c *symbol) { msousa@443: if (NULL == symbol->datatype) msousa@443: /* next IL instructions were unable to determine the datatype this instruction should produce */ msousa@443: return NULL; msousa@443: /* set the datatype for the operand */ msousa@443: il_operand->datatype = symbol->datatype; msousa@443: il_operand->accept(*this); msousa@443: return NULL; msousa@443: } msousa@443: msousa@443: msousa@443: void *narrow_candidate_datatypes_c::visit(LDN_operator_c *symbol) { msousa@443: if (NULL == symbol->datatype) msousa@443: /* next IL instructions were unable to determine the datatype this instruction should produce */ msousa@443: return NULL; msousa@443: /* set the datatype for the operand */ msousa@443: il_operand->datatype = symbol->datatype; msousa@443: il_operand->accept(*this); msousa@443: return NULL; msousa@443: } msousa@443: msousa@443: void *narrow_candidate_datatypes_c::visit(ST_operator_c *symbol) { msousa@443: if (debug) printf("narrow_candidate_datatypes_c::visit(ST_operator_c *symbol) called.\n"); msousa@417: if (symbol->candidate_datatypes.size() != 1) msousa@417: return NULL; msousa@417: symbol->datatype = symbol->candidate_datatypes[0]; msousa@443: /* set the datatype for the operand */ msousa@417: il_operand->datatype = symbol->datatype; msousa@417: il_operand->accept(*this); msousa@443: /* set the desired datatype of the previous il instruction */ msousa@443: prev_il_instruction->datatype = symbol->datatype; msousa@443: if (debug) printf("narrow_candidate_datatypes_c::visit(ST_operator_c *symbol) returning. previous_il_instruction->datatype = %p\n", prev_il_instruction->datatype); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(STN_operator_c *symbol) { msousa@417: if (symbol->candidate_datatypes.size() != 1) msousa@417: return NULL; msousa@417: symbol->datatype = symbol->candidate_datatypes[0]; msousa@443: /* set the datatype for the operand */ msousa@417: il_operand->datatype = symbol->datatype; msousa@417: il_operand->accept(*this); msousa@443: /* set the desired datatype of the previous il instruction */ msousa@443: prev_il_instruction->datatype = symbol->datatype; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(NOT_operator_c *symbol) { msousa@417: return NULL; msousa@417: } msousa@417: msousa@448: void *narrow_candidate_datatypes_c::visit(S_operator_c *symbol) { msousa@448: /* TODO: what if this is a FB call? */ msousa@448: return handle_il_instruction(symbol); msousa@448: } msousa@448: void *narrow_candidate_datatypes_c::visit(R_operator_c *symbol) { msousa@448: /* TODO: what if this is a FB call? */ msousa@448: return handle_il_instruction(symbol); msousa@448: } msousa@448: msousa@448: msousa@448: void *narrow_candidate_datatypes_c::visit(S1_operator_c *symbol) { msousa@448: narrow_implicit_il_fb_call(symbol, "S1", symbol->called_fb_declaration); msousa@448: return NULL; msousa@448: } msousa@448: msousa@448: void *narrow_candidate_datatypes_c::visit(R1_operator_c *symbol) { msousa@448: narrow_implicit_il_fb_call(symbol, "R1", symbol->called_fb_declaration); msousa@448: return NULL; msousa@448: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(CLK_operator_c *symbol) { msousa@448: narrow_implicit_il_fb_call(symbol, "CLK", symbol->called_fb_declaration); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(CU_operator_c *symbol) { msousa@448: narrow_implicit_il_fb_call(symbol, "CU", symbol->called_fb_declaration); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(CD_operator_c *symbol) { msousa@448: narrow_implicit_il_fb_call(symbol, "CD", symbol->called_fb_declaration); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(PV_operator_c *symbol) { msousa@448: narrow_implicit_il_fb_call(symbol, "PV", symbol->called_fb_declaration); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(IN_operator_c *symbol) { msousa@448: narrow_implicit_il_fb_call(symbol, "IN", symbol->called_fb_declaration); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(PT_operator_c *symbol) { msousa@448: narrow_implicit_il_fb_call(symbol, "PT", symbol->called_fb_declaration); msousa@448: return NULL; msousa@448: } msousa@448: msousa@448: msousa@448: msousa@448: void *narrow_candidate_datatypes_c::visit(AND_operator_c *symbol) {return handle_il_instruction(symbol);} msousa@448: void *narrow_candidate_datatypes_c::visit(OR_operator_c *symbol) {return handle_il_instruction(symbol);} msousa@448: void *narrow_candidate_datatypes_c::visit(XOR_operator_c *symbol) {return handle_il_instruction(symbol);} msousa@443: void *narrow_candidate_datatypes_c::visit(ANDN_operator_c *symbol) {return handle_il_instruction(symbol);} msousa@448: void *narrow_candidate_datatypes_c::visit(ORN_operator_c *symbol) {return handle_il_instruction(symbol);} msousa@443: void *narrow_candidate_datatypes_c::visit(XORN_operator_c *symbol) {return handle_il_instruction(symbol);} msousa@448: void *narrow_candidate_datatypes_c::visit(ADD_operator_c *symbol) {return handle_il_instruction(symbol);} msousa@448: void *narrow_candidate_datatypes_c::visit(SUB_operator_c *symbol) {return handle_il_instruction(symbol);} msousa@448: void *narrow_candidate_datatypes_c::visit(MUL_operator_c *symbol) {return handle_il_instruction(symbol);} msousa@448: void *narrow_candidate_datatypes_c::visit(DIV_operator_c *symbol) {return handle_il_instruction(symbol);} msousa@448: void *narrow_candidate_datatypes_c::visit(MOD_operator_c *symbol) {return handle_il_instruction(symbol);} msousa@448: void *narrow_candidate_datatypes_c::visit(GT_operator_c *symbol) {return handle_il_instruction(symbol);} msousa@448: void *narrow_candidate_datatypes_c::visit(GE_operator_c *symbol) {return handle_il_instruction(symbol);} msousa@448: void *narrow_candidate_datatypes_c::visit(EQ_operator_c *symbol) {return handle_il_instruction(symbol);} msousa@448: void *narrow_candidate_datatypes_c::visit(LT_operator_c *symbol) {return handle_il_instruction(symbol);} msousa@448: void *narrow_candidate_datatypes_c::visit(LE_operator_c *symbol) {return handle_il_instruction(symbol);} msousa@448: void *narrow_candidate_datatypes_c::visit(NE_operator_c *symbol) {return handle_il_instruction(symbol);} msousa@448: msousa@417: msousa@455: // SYM_REF0(CAL_operator_c) msousa@455: /* called from il_fb_call_c (symbol->il_call_operator->accpet(*this) ) */ msousa@417: void *narrow_candidate_datatypes_c::visit(CAL_operator_c *symbol) { msousa@455: /* set the desired datatype of the previous il instruction */ msousa@455: /* This FB call does not change the value in the current/default IL variable, so we pass the required datatype to the previous IL instruction */ msousa@455: if (NULL != prev_il_instruction) msousa@455: prev_il_instruction->datatype = symbol->datatype; msousa@455: return NULL; msousa@455: } msousa@455: msousa@455: msousa@455: void *narrow_candidate_datatypes_c::narrow_conditional_flow_control_IL_instruction(symbol_c *symbol) { msousa@455: /* if the next IL instructions needs us to provide a datatype other than a bool, msousa@455: * then we have an internal compiler error - most likely in fill_candidate_datatypes_c msousa@455: */ msousa@455: if ((NULL != symbol->datatype) && (!is_type(symbol->datatype, bool_type_name_c))) ERROR; msousa@455: if (symbol->candidate_datatypes.size() > 1) ERROR; msousa@455: msousa@455: /* NOTE: If there is not IL instruction following this CALC, CALCN, JMPC, JMPC, ..., instruction, msousa@455: * we must still provide a bool_type_name_c datatype (if possible, i.e. if it exists in the candidate datatype list). msousa@455: * If it is not possible, we set it to NULL msousa@455: */ msousa@455: if (symbol->candidate_datatypes.size() == 0) symbol->datatype = NULL; msousa@455: else symbol->datatype = symbol->candidate_datatypes[0]; /* i.e. a bool_type_name_c! */ msousa@455: if ((NULL != symbol->datatype) && (!is_type(symbol->datatype, bool_type_name_c))) ERROR; msousa@455: msousa@455: /* set the required datatype of the previous IL instruction, i.e. a bool_type_name_c! */ msousa@455: if (NULL != prev_il_instruction) prev_il_instruction->datatype = symbol->datatype; msousa@455: return NULL; msousa@455: } msousa@455: msousa@455: msousa@455: // SYM_REF0(CALC_operator_c) msousa@455: /* called from il_fb_call_c (symbol->il_call_operator->accpet(*this) ) */ msousa@417: void *narrow_candidate_datatypes_c::visit(CALC_operator_c *symbol) { msousa@455: return narrow_conditional_flow_control_IL_instruction(symbol); msousa@455: } msousa@455: msousa@455: msousa@455: // SYM_REF0(CALCN_operator_c) msousa@455: /* called from il_fb_call_c (symbol->il_call_operator->accpet(*this) ) */ msousa@417: void *narrow_candidate_datatypes_c::visit(CALCN_operator_c *symbol) { msousa@455: return narrow_conditional_flow_control_IL_instruction(symbol); msousa@455: } msousa@455: msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(RET_operator_c *symbol) { msousa@455: /* set the desired datatype of the previous il instruction */ msousa@455: /* This RET instruction does not change the value in the current/default IL variable, so we pass the required datatype to the previous IL instruction. msousa@455: * Actually this should always be NULL, otherwise we have a bug in the flow_control_analysis_c msousa@455: * However, since that class has not yet been completely finished, we do not yet check this assertion! msousa@455: */ msousa@455: // if (NULL != symbol->datatype) ERROR; msousa@455: if (NULL != prev_il_instruction) msousa@455: prev_il_instruction->datatype = symbol->datatype; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(RETC_operator_c *symbol) { msousa@455: return narrow_conditional_flow_control_IL_instruction(symbol); msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(RETCN_operator_c *symbol) { msousa@455: return narrow_conditional_flow_control_IL_instruction(symbol); msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(JMP_operator_c *symbol) { msousa@455: /* set the desired datatype of the previous il instruction */ msousa@455: /* This JMP instruction does not change the value in the current/default IL variable, so we pass the required datatype to the previous IL instruction. msousa@455: * Actually this should always be NULL, otherwise we have a bug in the flow_control_analysis_c msousa@455: * However, since that class has not yet been completely finished, we do not yet check this assertion! msousa@455: */ msousa@455: // if (NULL != symbol->datatype) ERROR; msousa@455: if (NULL != prev_il_instruction) msousa@455: prev_il_instruction->datatype = symbol->datatype; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(JMPC_operator_c *symbol) { msousa@455: return narrow_conditional_flow_control_IL_instruction(symbol); msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(JMPCN_operator_c *symbol) { msousa@455: return narrow_conditional_flow_control_IL_instruction(symbol); msousa@417: } 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: /***********************/ msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(or_expression_c *symbol) { msousa@417: symbol_c * selected_type = NULL; msousa@417: for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) { msousa@417: for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) { msousa@417: if (is_type_equal(symbol->l_exp->candidate_datatypes[i], symbol->r_exp->candidate_datatypes[j])) { msousa@417: selected_type = symbol->l_exp->candidate_datatypes[i]; msousa@417: break; msousa@417: } msousa@417: } msousa@417: } msousa@417: msousa@417: if (NULL != selected_type) { msousa@417: symbol->l_exp->datatype = selected_type; msousa@417: symbol->l_exp->accept(*this); msousa@417: symbol->r_exp->datatype = selected_type; msousa@417: symbol->r_exp->accept(*this); msousa@417: } msousa@417: else msousa@417: ERROR; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(xor_expression_c *symbol) { msousa@417: symbol_c * selected_type = NULL; msousa@417: for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) { msousa@417: for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) { msousa@417: if (is_type_equal(symbol->l_exp->candidate_datatypes[i], symbol->r_exp->candidate_datatypes[j])) { msousa@417: selected_type = symbol->l_exp->candidate_datatypes[i]; msousa@417: break; msousa@417: } msousa@417: } msousa@417: } msousa@417: msousa@417: if (NULL != selected_type) { msousa@417: symbol->l_exp->datatype = selected_type; msousa@417: symbol->l_exp->accept(*this); msousa@417: symbol->r_exp->datatype = selected_type; msousa@417: symbol->r_exp->accept(*this); msousa@417: } msousa@417: else msousa@417: ERROR; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(and_expression_c *symbol) { msousa@417: symbol_c * selected_type = NULL; msousa@417: for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) { msousa@417: for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) { msousa@417: if (typeid(*symbol->l_exp->candidate_datatypes[i]) == typeid(*symbol->r_exp->candidate_datatypes[j])) { msousa@417: selected_type = symbol->l_exp->candidate_datatypes[i]; msousa@417: break; msousa@417: } msousa@417: } msousa@417: } msousa@417: msousa@417: if (NULL != selected_type) { msousa@417: symbol->l_exp->datatype = selected_type; msousa@417: symbol->l_exp->accept(*this); msousa@417: symbol->r_exp->datatype = selected_type; msousa@417: symbol->r_exp->accept(*this); msousa@417: } msousa@417: else msousa@417: ERROR; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(equ_expression_c *symbol) { msousa@417: /* Here symbol->datatype has already assigned to BOOL msousa@417: * In conditional symbols like =, <>, =<, <, >, >= we have to set msousa@417: * l_exp and r_exp expression matched with compatible type. msousa@417: * Example: msousa@417: * INT#14 = INT#81 msousa@417: * equ_expression_c symbol->datatype = BOOL from top visit msousa@417: * symbol->l_exp->datatype => INT msousa@417: * symbol->r_exp->datatype => INT msousa@417: */ msousa@417: symbol_c * selected_type = NULL; msousa@417: for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) { msousa@417: for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) { msousa@417: if (typeid(*symbol->l_exp->candidate_datatypes[i]) == typeid(*symbol->r_exp->candidate_datatypes[j])) { msousa@417: /* msousa@417: * We do not need to check whether the type is an ANY_ELEMENTARY here. msousa@417: * That was already done in fill_candidate_datatypes_c. msousa@417: */ msousa@417: selected_type = symbol->l_exp->candidate_datatypes[i]; msousa@417: break; msousa@417: } msousa@417: } msousa@417: } msousa@417: msousa@417: if (NULL != selected_type) { msousa@417: symbol->l_exp->datatype = selected_type; msousa@417: symbol->r_exp->datatype = selected_type; msousa@417: } msousa@417: else msousa@417: ERROR; msousa@427: msousa@427: symbol->l_exp->accept(*this); msousa@427: symbol->r_exp->accept(*this); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(notequ_expression_c *symbol) { msousa@417: symbol_c * selected_type = NULL; msousa@417: for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) { msousa@417: for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) { msousa@417: if (typeid(*symbol->l_exp->candidate_datatypes[i]) == typeid(*symbol->r_exp->candidate_datatypes[j])) { msousa@417: selected_type = symbol->l_exp->candidate_datatypes[i]; msousa@417: break; msousa@417: } msousa@417: } msousa@417: } msousa@417: msousa@417: if (NULL != selected_type) { msousa@417: symbol->l_exp->datatype = selected_type; msousa@417: symbol->l_exp->accept(*this); msousa@417: symbol->r_exp->datatype = selected_type; msousa@417: symbol->r_exp->accept(*this); msousa@417: } msousa@417: else msousa@417: ERROR; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(lt_expression_c *symbol) { msousa@417: symbol_c * selected_type = NULL; msousa@417: for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) { msousa@417: for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) { msousa@417: if (typeid(*symbol->l_exp->candidate_datatypes[i]) == typeid(*symbol->r_exp->candidate_datatypes[j]) msousa@417: && is_ANY_ELEMENTARY_type(symbol->l_exp->candidate_datatypes[i])) { msousa@417: selected_type = symbol->l_exp->candidate_datatypes[i]; msousa@417: break; msousa@417: } msousa@417: } msousa@417: } msousa@417: msousa@417: if (NULL != selected_type) { msousa@417: symbol->l_exp->datatype = selected_type; msousa@417: symbol->l_exp->accept(*this); msousa@417: symbol->r_exp->datatype = selected_type; msousa@417: symbol->r_exp->accept(*this); msousa@417: } msousa@417: else msousa@417: ERROR; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(gt_expression_c *symbol) { msousa@417: symbol_c * selected_type = NULL; msousa@417: for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) { msousa@417: for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) { msousa@417: if (typeid(*symbol->l_exp->candidate_datatypes[i]) == typeid(*symbol->r_exp->candidate_datatypes[j]) msousa@417: && is_ANY_ELEMENTARY_type(symbol->l_exp->candidate_datatypes[i])) { msousa@417: selected_type = symbol->l_exp->candidate_datatypes[i]; msousa@417: break; msousa@417: } msousa@417: } msousa@417: } msousa@417: msousa@417: if (NULL != selected_type) { msousa@417: symbol->l_exp->datatype = selected_type; msousa@417: symbol->l_exp->accept(*this); msousa@417: symbol->r_exp->datatype = selected_type; msousa@417: symbol->r_exp->accept(*this); msousa@417: } msousa@417: else msousa@417: ERROR; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(le_expression_c *symbol) { msousa@417: symbol_c * selected_type = NULL; msousa@417: for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) { msousa@417: for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) { msousa@417: if (typeid(*symbol->l_exp->candidate_datatypes[i]) == typeid(*symbol->r_exp->candidate_datatypes[j]) msousa@417: && is_ANY_ELEMENTARY_type(symbol->l_exp->candidate_datatypes[i])) { msousa@417: selected_type = symbol->l_exp->candidate_datatypes[i]; msousa@417: break; msousa@417: } msousa@417: } msousa@417: } msousa@417: msousa@417: if (NULL != selected_type) { msousa@417: symbol->l_exp->datatype = selected_type; msousa@417: symbol->l_exp->accept(*this); msousa@417: symbol->r_exp->datatype = selected_type; msousa@417: symbol->r_exp->accept(*this); msousa@417: } msousa@417: else msousa@417: ERROR; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(ge_expression_c *symbol) { msousa@417: symbol_c * selected_type = NULL; msousa@417: for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) { msousa@417: for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) { msousa@417: if (typeid(*symbol->l_exp->candidate_datatypes[i]) == typeid(*symbol->r_exp->candidate_datatypes[j]) msousa@417: && is_ANY_ELEMENTARY_type(symbol->l_exp->candidate_datatypes[i])) { msousa@417: selected_type = symbol->l_exp->candidate_datatypes[i]; msousa@417: break; msousa@417: } msousa@417: } msousa@417: } msousa@417: msousa@417: if (NULL != selected_type) { msousa@417: symbol->l_exp->datatype = selected_type; msousa@417: symbol->l_exp->accept(*this); msousa@417: symbol->r_exp->datatype = selected_type; msousa@417: symbol->r_exp->accept(*this); msousa@417: } msousa@417: else msousa@417: ERROR; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(add_expression_c *symbol) { msousa@417: int count = 0; msousa@417: msousa@417: if (is_ANY_NUM_compatible(symbol->datatype)) { msousa@417: symbol->l_exp->datatype = symbol->datatype; msousa@417: symbol->r_exp->datatype = symbol->datatype; msousa@417: count++; msousa@417: } else { msousa@417: /* TIME data type */ msousa@417: for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) { msousa@417: for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) { msousa@417: /* test widening compatibility */ msousa@417: if (is_widening_compatible(symbol->l_exp->candidate_datatypes[i], msousa@417: symbol->r_exp->candidate_datatypes[j], msousa@417: symbol->datatype, widen_ADD_table)) { msousa@417: symbol->l_exp->datatype = symbol->l_exp->candidate_datatypes[i]; msousa@417: symbol->r_exp->datatype = symbol->r_exp->candidate_datatypes[j]; msousa@417: count ++; msousa@417: } msousa@417: } msousa@417: } msousa@417: } msousa@417: if (count > 1) msousa@417: ERROR; msousa@417: symbol->l_exp->accept(*this); msousa@417: symbol->r_exp->accept(*this); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(sub_expression_c *symbol) { msousa@417: int count = 0; msousa@417: msousa@417: if (is_ANY_NUM_compatible(symbol->datatype)) { msousa@417: symbol->l_exp->datatype = symbol->datatype; msousa@417: symbol->r_exp->datatype = symbol->datatype; msousa@417: count++; msousa@417: } else { msousa@417: /* TIME data type */ msousa@417: for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) { msousa@417: for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) { msousa@417: /* test widening compatibility */ msousa@417: if (is_widening_compatible(symbol->l_exp->candidate_datatypes[i], msousa@417: symbol->r_exp->candidate_datatypes[j], msousa@417: symbol->datatype, widen_SUB_table)) { msousa@417: symbol->l_exp->datatype = symbol->l_exp->candidate_datatypes[i]; msousa@417: symbol->r_exp->datatype = symbol->r_exp->candidate_datatypes[j]; msousa@417: count ++; msousa@417: } msousa@417: } msousa@417: } msousa@417: } msousa@417: if (count > 1) msousa@417: ERROR; msousa@417: symbol->l_exp->accept(*this); msousa@417: symbol->r_exp->accept(*this); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(mul_expression_c *symbol) { msousa@417: int count = 0; msousa@417: msousa@417: if (is_ANY_NUM_compatible(symbol->datatype)) { msousa@417: symbol->l_exp->datatype = symbol->datatype; msousa@417: symbol->r_exp->datatype = symbol->datatype; msousa@417: count++; msousa@417: } else { msousa@417: /* TIME data type */ msousa@417: for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) { msousa@417: for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) { msousa@417: /* test widening compatibility */ msousa@417: if (is_widening_compatible(symbol->l_exp->candidate_datatypes[i], msousa@417: symbol->r_exp->candidate_datatypes[j], msousa@417: symbol->datatype, widen_MUL_table)) { msousa@417: symbol->l_exp->datatype = symbol->l_exp->candidate_datatypes[i]; msousa@417: symbol->r_exp->datatype = symbol->r_exp->candidate_datatypes[j]; msousa@417: count ++; msousa@417: } msousa@417: } msousa@417: } msousa@417: } msousa@417: if (count > 1) msousa@417: ERROR; msousa@417: symbol->l_exp->accept(*this); msousa@417: symbol->r_exp->accept(*this); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(div_expression_c *symbol) { msousa@417: int count = 0; msousa@417: msousa@417: if (is_ANY_NUM_compatible(symbol->datatype)) { msousa@417: symbol->l_exp->datatype = symbol->datatype; msousa@417: symbol->r_exp->datatype = symbol->datatype; msousa@417: count++; msousa@417: } else { msousa@417: /* TIME data type */ msousa@417: for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) { msousa@417: for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) { msousa@417: /* test widening compatibility */ msousa@417: if (is_widening_compatible(symbol->l_exp->candidate_datatypes[i], msousa@417: symbol->r_exp->candidate_datatypes[j], msousa@417: symbol->datatype, widen_DIV_table)) { msousa@417: symbol->l_exp->datatype = symbol->l_exp->candidate_datatypes[i]; msousa@417: symbol->r_exp->datatype = symbol->r_exp->candidate_datatypes[j]; msousa@417: count ++; msousa@417: } msousa@417: } msousa@417: } msousa@417: } msousa@417: if (count > 1) msousa@417: ERROR; msousa@417: symbol->l_exp->accept(*this); msousa@417: symbol->r_exp->accept(*this); msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(mod_expression_c *symbol) { 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: void *narrow_candidate_datatypes_c::visit(power_expression_c *symbol) { msousa@417: symbol->l_exp->datatype = symbol->datatype; msousa@417: symbol->l_exp->accept(*this); msousa@417: if (! symbol->r_exp->candidate_datatypes.size()){ msousa@417: symbol->r_exp->datatype = symbol->r_exp->candidate_datatypes[0]; msousa@417: symbol->r_exp->accept(*this); msousa@417: } msousa@417: return NULL; msousa@417: } 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@417: if (is_type(symbol->expression->candidate_datatypes[i], bool_type_name_c)) 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@417: if (is_type(symbol->expression->candidate_datatypes[i], bool_type_name_c)) 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@417: if ((is_ANY_INT_type(symbol->expression->candidate_datatypes[i])) msousa@417: || (search_base_type.type_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@417: if (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@417: if (is_ANY_INT_type(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@417: if (is_type_equal(symbol->control_variable->datatype,symbol->beg_expression->candidate_datatypes[i]) && msousa@417: is_ANY_INT_type(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@417: if (is_type_equal(symbol->control_variable->datatype,symbol->end_expression->candidate_datatypes[i]) && msousa@417: is_ANY_INT_type(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@417: if (is_type_equal(symbol->control_variable->datatype,symbol->by_expression->candidate_datatypes[i]) && msousa@417: is_ANY_INT_type(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@417: if(is_BOOL_type(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@417: if(is_BOOL_type(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: