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@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@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@417: call_param_value->datatype = base_type(fp_iterator.param_type()); msousa@424: if ((NULL != param_name) && (NULL == call_param_value->datatype)) ERROR; msousa@424: if ((NULL == param_name) && (NULL != call_param_value->datatype)) ERROR; msousa@417: 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: 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@423: call_param_value->datatype = base_type(fp_iterator.param_type()); msousa@424: if ((NULL != param_name) && (NULL == call_param_value->datatype)) ERROR; msousa@424: if ((NULL == param_name) && (NULL != call_param_value->datatype)) ERROR; msousa@424: msousa@423: 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@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@417: 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@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@417: if (debug) printf("Narrowing candidate data types list in body of function %s\n", ((token_c *)(symbol->derived_function_name))->value); msousa@417: prev_il_instruction = NULL; msousa@417: symbol->function_body->accept(*this); msousa@417: prev_il_instruction = NULL; 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@417: if (debug) printf("Narrowing candidate data types list in body of FB %s\n", ((token_c *)(symbol->fblock_name))->value); msousa@417: prev_il_instruction = NULL; msousa@417: symbol->fblock_body->accept(*this); msousa@417: prev_il_instruction = NULL; 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@417: if (debug) printf("Narrowing candidate data types list in body of program %s\n", ((token_c *)(symbol->program_type_name))->value); msousa@417: prev_il_instruction = NULL; msousa@417: symbol->function_block_body->accept(*this); msousa@417: prev_il_instruction = NULL; 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: #if 0 msousa@417: // TODO !!! msousa@417: /* for the moment we must return NULL so semantic analysis of remaining code is not interrupted! */ msousa@417: #endif msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@417: /****************************************/ msousa@417: /* B.2 - Language IL (Instruction List) */ msousa@417: /****************************************/ msousa@417: /***********************************/ msousa@417: /* B 2.1 Instructions and Operands */ msousa@417: /***********************************/ msousa@417: // void *visit(instruction_list_c *symbol); msousa@417: void *narrow_candidate_datatypes_c::visit(il_simple_operation_c *symbol) { msousa@417: il_operand = symbol->il_operand; msousa@417: if (NULL != symbol->il_operand) { msousa@417: symbol->il_operand->accept(*this); msousa@417: } msousa@417: /* recursive call to see whether data types are compatible */ msousa@417: symbol->il_simple_operator->accept(*this); msousa@417: il_operand = NULL; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(il_function_call_c *symbol) { msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: /* MJS: Manuele, could you please not delete the following 2 lines of comments. They help me understand where this class is used msousa@417: * and when it is created by bison - syntax parse, and how it can show up in the abstract syntax tree. msousa@417: * msousa@417: * Actually, it could be helpful if we could have all the similar comments already present in visit_expression_type_c msousa@417: * in the 3 new classes fill/narrow/print candidate datatype msousa@417: */ 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@417: /* MJS: TODO... */ msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(il_fb_call_c *symbol) { msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(il_formal_funct_call_c *symbol) { msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: msousa@417: /* msousa@417: void *visit(il_operand_list_c *symbol); msousa@417: void *visit(simple_instr_list_c *symbol); msousa@417: void *visit(il_param_list_c *symbol); msousa@417: void *visit(il_param_assignment_c *symbol); msousa@417: void *visit(il_param_out_assignment_c *symbol); msousa@417: */ msousa@417: msousa@417: /*******************/ msousa@417: /* B 2.2 Operators */ msousa@417: /*******************/ msousa@417: void *narrow_candidate_datatypes_c::visit(LD_operator_c *symbol) { msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(LDN_operator_c *symbol) { msousa@417: if (symbol->candidate_datatypes.size() != 1) msousa@417: return NULL; msousa@417: symbol->datatype = symbol->candidate_datatypes[0]; msousa@417: il_operand->datatype = symbol->datatype; msousa@417: il_operand->accept(*this); msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(ST_operator_c *symbol) { msousa@417: if (symbol->candidate_datatypes.size() != 1) msousa@417: return NULL; msousa@417: symbol->datatype = symbol->candidate_datatypes[0]; msousa@417: il_operand->datatype = symbol->datatype; msousa@417: il_operand->accept(*this); msousa@417: prev_il_instruction = symbol; 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@417: il_operand->datatype = symbol->datatype; msousa@417: il_operand->accept(*this); msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(NOT_operator_c *symbol) { msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(S_operator_c *symbol) { msousa@417: if (symbol->candidate_datatypes.size() != 1) msousa@417: return NULL; msousa@417: symbol->datatype = symbol->candidate_datatypes[0]; msousa@417: il_operand->datatype = symbol->datatype; msousa@417: il_operand->accept(*this); msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(R_operator_c *symbol) { msousa@417: if (symbol->candidate_datatypes.size() != 1) msousa@417: return NULL; msousa@417: symbol->datatype = symbol->candidate_datatypes[0]; msousa@417: il_operand->datatype = symbol->datatype; msousa@417: il_operand->accept(*this); msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(S1_operator_c *symbol) { msousa@417: if (symbol->candidate_datatypes.size() != 1) msousa@417: return NULL; msousa@417: symbol->datatype = symbol->candidate_datatypes[0]; msousa@417: il_operand->datatype = symbol->datatype; msousa@417: il_operand->accept(*this); msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(R1_operator_c *symbol) { msousa@417: if (symbol->candidate_datatypes.size() != 1) msousa@417: return NULL; msousa@417: symbol->datatype = symbol->candidate_datatypes[0]; msousa@417: il_operand->datatype = symbol->datatype; msousa@417: il_operand->accept(*this); msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(CLK_operator_c *symbol) { msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(CU_operator_c *symbol) { msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(CD_operator_c *symbol) { msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(PV_operator_c *symbol) { msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(IN_operator_c *symbol) { msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(PT_operator_c *symbol) { msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(AND_operator_c *symbol) { msousa@417: if (symbol->candidate_datatypes.size() != 1) msousa@417: return NULL; msousa@417: symbol->datatype = symbol->candidate_datatypes[0]; msousa@417: il_operand->datatype = symbol->datatype; msousa@417: il_operand->accept(*this); msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(OR_operator_c *symbol) { msousa@417: if (symbol->candidate_datatypes.size() != 1) msousa@417: return NULL; msousa@417: symbol->datatype = symbol->candidate_datatypes[0]; msousa@417: il_operand->datatype = symbol->datatype; msousa@417: il_operand->accept(*this); msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(XOR_operator_c *symbol) { msousa@417: if (symbol->candidate_datatypes.size() != 1) msousa@417: return NULL; msousa@417: symbol->datatype = symbol->candidate_datatypes[0]; msousa@417: il_operand->datatype = symbol->datatype; msousa@417: il_operand->accept(*this); msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(ANDN_operator_c *symbol) { msousa@417: if (symbol->candidate_datatypes.size() != 1) msousa@417: return NULL; msousa@417: symbol->datatype = symbol->candidate_datatypes[0]; msousa@417: il_operand->datatype = symbol->datatype; msousa@417: il_operand->accept(*this); msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(ORN_operator_c *symbol) { msousa@417: if (symbol->candidate_datatypes.size() != 1) msousa@417: return NULL; msousa@417: symbol->datatype = symbol->candidate_datatypes[0]; msousa@417: il_operand->datatype = symbol->datatype; msousa@417: il_operand->accept(*this); msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(XORN_operator_c *symbol) { msousa@417: if (symbol->candidate_datatypes.size() != 1) msousa@417: return NULL; msousa@417: symbol->datatype = symbol->candidate_datatypes[0]; msousa@417: il_operand->datatype = symbol->datatype; msousa@417: il_operand->accept(*this); msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(ADD_operator_c *symbol) { msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(SUB_operator_c *symbol) { msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(MUL_operator_c *symbol) { msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(DIV_operator_c *symbol) { msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(MOD_operator_c *symbol) { msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(GT_operator_c *symbol) { msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(GE_operator_c *symbol) { msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(EQ_operator_c *symbol) { msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(LT_operator_c *symbol) { msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(LE_operator_c *symbol) { msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(NE_operator_c *symbol) { msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(CAL_operator_c *symbol) { msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(CALC_operator_c *symbol) { msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(CALCN_operator_c *symbol) { msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(RET_operator_c *symbol) { msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(RETC_operator_c *symbol) { msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(RETCN_operator_c *symbol) { msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(JMP_operator_c *symbol) { msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(JMPC_operator_c *symbol) { msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; msousa@417: } msousa@417: msousa@417: void *narrow_candidate_datatypes_c::visit(JMPCN_operator_c *symbol) { msousa@417: prev_il_instruction = symbol; msousa@417: return NULL; 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->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(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@417: void *narrow_candidate_datatypes_c::visit(function_invocation_c *symbol) { msousa@421: int ext_parm_count; msousa@421: msousa@424: /* set the called_function_declaration. */ msousa@421: symbol->called_function_declaration = NULL; msousa@424: if (symbol->candidate_datatypes.size() == 1) { msousa@424: /* If only one possible called function, then that is the function to call! msousa@424: * In this case we ignore the symbol->datatype value (that may even be NULL). msousa@424: * This helps in identifying potential errors in the expressions used inside this function call msousa@424: * even if there is a previous error, allowing us to make a more thorough analysis of the semantics msousa@424: * of the ST code, and providing as many relevant error messages as possible! msousa@424: * If symbol->datatype isn't NULL, then this chosen function should be returning the required datatype, msousa@424: * otherwise we have a bug in our stage3 code! msousa@424: */ msousa@424: symbol->called_function_declaration = symbol->candidate_functions[0]; msousa@424: if ((NULL != symbol->datatype) && (!is_type_equal(symbol->candidate_datatypes[0], symbol->datatype))) msousa@424: ERROR; msousa@424: } msousa@424: else { msousa@424: /* set the called_function_declaration taking into account the datatype that we need to return */ msousa@424: symbol->called_function_declaration = NULL; msousa@424: for(unsigned int i = 0; i < symbol->candidate_datatypes.size(); i++) { msousa@424: if (is_type_equal(symbol->candidate_datatypes[i], symbol->datatype)) { msousa@424: symbol->called_function_declaration = symbol->candidate_functions[i]; msousa@424: break; msousa@424: } msousa@424: } msousa@424: } msousa@424: /* NOTE: If we can't figure out the declaration of the function being called, this is not msousa@424: * necessarily an internal compiler error. It could be because the symbol->datatype is NULL msousa@424: * (because the ST code being analysed has an error _before_ this function invocation). msousa@424: * However, we don't just give, up, we carry on recursivly analysing the code, so as to be msousa@424: * able to print out any error messages related to underlying code that could be partially correct. msousa@424: */ msousa@424: /* if (NULL == symbol->called_function_declaration) ERROR; */ msousa@421: msousa@421: if (NULL != symbol->nonformal_param_list) narrow_nonformal_call(symbol, symbol->called_function_declaration, &ext_parm_count); msousa@421: if (NULL != symbol-> formal_param_list) narrow_formal_call(symbol, symbol->called_function_declaration, &ext_parm_count); msousa@421: symbol->extensible_param_count = ext_parm_count; msousa@417: 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: