conti@508: /* conti@508: * matiec - a compiler for the programming languages defined in IEC 61131-3 conti@508: * conti@508: * Copyright (C) 2009-2012 Mario de Sousa (msousa@fe.up.pt) conti@508: * Copyright (C) 2012 Manuele Conti (conti.ma@alice.it) conti@508: * conti@508: * conti@508: * This program is free software: you can redistribute it and/or modify conti@508: * it under the terms of the GNU General Public License as published by conti@508: * the Free Software Foundation, either version 3 of the License, or conti@508: * (at your option) any later version. conti@508: * conti@508: * This program is distributed in the hope that it will be useful, conti@508: * but WITHOUT ANY WARRANTY; without even the implied warranty of conti@508: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the conti@508: * GNU General Public License for more details. conti@508: * conti@508: * You should have received a copy of the GNU General Public License conti@508: * along with this program. If not, see . conti@508: * conti@508: * conti@508: * This code is made available on the understanding that it will not be conti@508: * used in safety-critical situations without a full and competent review. conti@508: */ conti@508: conti@508: /* conti@508: * An IEC 61131-3 compiler. conti@508: * conti@508: * Based on the conti@508: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) conti@508: * conti@508: */ conti@508: conti@508: conti@508: #include "lvalue_check.hh" conti@508: conti@508: #define FIRST_(symbol1, symbol2) (((symbol1)->first_order < (symbol2)->first_order) ? (symbol1) : (symbol2)) conti@508: #define LAST_(symbol1, symbol2) (((symbol1)->last_order > (symbol2)->last_order) ? (symbol1) : (symbol2)) conti@508: conti@508: #define STAGE3_ERROR(error_level, symbol1, symbol2, ...) { \ conti@508: if (current_display_error_level >= error_level) { \ conti@508: fprintf(stderr, "%s:%d-%d..%d-%d: error: ", \ conti@508: FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column,\ conti@508: LAST_(symbol1,symbol2) ->last_line, LAST_(symbol1,symbol2) ->last_column);\ conti@508: fprintf(stderr, __VA_ARGS__); \ conti@508: fprintf(stderr, "\n"); \ conti@508: error_found = true; \ conti@508: } \ conti@508: } conti@508: conti@508: conti@508: #define STAGE3_WARNING(symbol1, symbol2, ...) { \ conti@508: fprintf(stderr, "%s:%d-%d..%d-%d: warning: ", \ conti@508: FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column,\ conti@508: LAST_(symbol1,symbol2) ->last_line, LAST_(symbol1,symbol2) ->last_column);\ conti@508: fprintf(stderr, __VA_ARGS__); \ conti@508: fprintf(stderr, "\n"); \ conti@508: warning_found = true; \ conti@508: } conti@508: conti@508: conti@508: lvalue_check_c::lvalue_check_c(symbol_c *ignore) { conti@508: error_found = false; conti@508: } conti@508: conti@508: lvalue_check_c::~lvalue_check_c(void) { conti@508: } conti@508: conti@508: int lvalue_check_c::get_error_found() { conti@508: return error_found; conti@508: } conti@508: msousa@509: msousa@509: #include conti@508: /* No writing to iterator variables (used in FOR loops) inside the loop itself */ msousa@509: void lvalue_check_c::check_assignment_to_controlvar(symbol_c *lvalue) { conti@508: for (unsigned int i = 0; i < control_variables.size(); i++) { conti@508: symbolic_variable_c *cvar = (symbolic_variable_c *)control_variables[i]; msousa@509: if (strcasecmp(((identifier_c *)((symbolic_variable_c *)lvalue)->var_name)->value, ((identifier_c *)((symbolic_variable_c *)cvar)->var_name)->value) == 0) { conti@508: STAGE3_ERROR(0, lvalue, lvalue, "Assignment to FOR control variable are not be allowed."); conti@508: break; conti@508: } conti@508: } conti@508: } conti@508: msousa@509: conti@508: /* fb_instance.var := ... is not valid if var is output (not input ??) variable */ msousa@509: void lvalue_check_c::check_assignment_to_output(symbol_c * lvalue) { msousa@509: symbol_c *type_id = search_varfb_instance_type->get_basetype_id(lvalue); conti@508: if (NULL != type_id) { conti@508: function_block_declaration_c *fb_decl = function_block_type_symtable.find_value(type_id); conti@508: if (function_block_type_symtable.end_value() != fb_decl) { conti@508: search_var_instance_decl_c search_var_instance_decl(fb_decl); conti@508: structured_variable_c * str_var = (structured_variable_c *)lvalue; conti@508: unsigned int vartype = search_var_instance_decl.get_vartype(str_var->field_selector); conti@508: if (vartype == search_var_instance_decl_c::output_vt) msousa@509: STAGE3_ERROR(0, lvalue, lvalue, "Assignment to FB output field variable is not be allowed."); conti@508: } conti@508: } conti@508: } conti@508: msousa@509: conti@508: /* No writing to CONSTANTs */ msousa@509: void lvalue_check_c::check_assignment_to_constant(symbol_c *lvalue) { msousa@509: unsigned int option = search_var_instance_decl->get_option(lvalue); conti@508: if (option == search_var_instance_decl_c::constant_opt) { msousa@509: STAGE3_ERROR(0, lvalue, lvalue, "Assignment to CONSTANT variables is not be allowed."); msousa@509: } msousa@509: } msousa@509: msousa@509: msousa@509: /* No assigning values to expressions. */ msousa@509: void lvalue_check_c::check_assignment_to_expression(symbol_c *lvalue) { msousa@509: /* TODO: check whether the lvalue is an expresion! */ msousa@509: /* This may occur in function invocations, when passing values (possibly an expression) to one msousa@509: * of the function's OUTPUT parameters. conti@508: */ msousa@509: } msousa@509: msousa@509: msousa@509: msousa@509: void lvalue_check_c::verify_is_lvalue(symbol_c *lvalue) { msousa@509: check_assignment_to_controlvar(lvalue); msousa@509: check_assignment_to_output(lvalue); msousa@509: check_assignment_to_constant(lvalue); msousa@509: check_assignment_to_expression(lvalue); msousa@509: } msousa@509: msousa@509: msousa@509: msousa@509: msousa@509: /* check whether all values passed to OUT or IN_OUT parameters are legal lvalues. */ msousa@509: void lvalue_check_c::check_nonformal_call(symbol_c *f_call, symbol_c *f_decl) { msousa@509: /* TODO */ msousa@509: } msousa@509: msousa@509: msousa@509: /* check whether all values passed to OUT or IN_OUT parameters are legal lvalues. */ msousa@509: void lvalue_check_c::check_formal_call(symbol_c *f_call, symbol_c *f_decl) { msousa@509: /* if data type semantic verification was unable to determine which function is being called, msousa@509: * then it does not make sense to go ahead and check for lvalues to unknown parameters. msousa@509: * We simply bug out! msousa@509: */ msousa@509: if (NULL == f_decl) return; msousa@509: msousa@509: symbol_c *call_param_name; msousa@509: function_param_iterator_c fp_iterator(f_decl); conti@508: function_call_param_iterator_c fcp_iterator(f_call); msousa@509: msousa@509: /* Iterating through the formal parameters of the function call */ msousa@509: while((call_param_name = fcp_iterator.next_f()) != NULL) { msousa@509: msousa@509: /* Obtaining the value being passed in the function call */ msousa@509: symbol_c *call_param_value = fcp_iterator.get_current_value(); msousa@509: if (NULL == call_param_value) ERROR; msousa@509: msousa@509: /* Find the corresponding parameter in function declaration, and it's direction (IN, OUT, IN_OUT) */ msousa@509: identifier_c *param_name = fp_iterator.search(call_param_name); msousa@509: function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); msousa@509: msousa@509: /* We only check if 'call_param_value' is a valid lvalue if the value is being passed msousa@509: * to a valid paramater of the function being called, and that parameter is either OUT or IN_OUT. msousa@509: */ msousa@509: if ((param_name != NULL) && msousa@509: ((function_param_iterator_c::direction_out == param_direction) || (function_param_iterator_c::direction_inout == param_direction))) { msousa@509: verify_is_lvalue(call_param_value); conti@508: } msousa@509: } msousa@509: } msousa@509: msousa@509: msousa@509: msousa@509: conti@508: conti@508: conti@508: conti@508: /**************************************/ conti@508: /* B 1.5 - Program organisation units */ conti@508: /**************************************/ conti@508: /***********************/ conti@508: /* B 1.5.1 - Functions */ conti@508: /***********************/ conti@508: void *lvalue_check_c::visit(function_declaration_c *symbol) { conti@508: search_varfb_instance_type = new search_varfb_instance_type_c(symbol); conti@508: search_var_instance_decl = new search_var_instance_decl_c(symbol); conti@508: symbol->function_body->accept(*this); conti@508: delete search_varfb_instance_type; conti@508: delete search_var_instance_decl; conti@508: search_varfb_instance_type = NULL; conti@508: search_var_instance_decl = NULL; conti@508: return NULL; conti@508: } conti@508: conti@508: /*****************************/ conti@508: /* B 1.5.2 - Function blocks */ conti@508: /*****************************/ conti@508: void *lvalue_check_c::visit(function_block_declaration_c *symbol) { conti@508: search_varfb_instance_type = new search_varfb_instance_type_c(symbol); conti@508: search_var_instance_decl = new search_var_instance_decl_c(symbol); conti@508: symbol->fblock_body->accept(*this); conti@508: delete search_varfb_instance_type; conti@508: delete search_var_instance_decl; conti@508: search_varfb_instance_type = NULL; conti@508: search_var_instance_decl = NULL; conti@508: return NULL; conti@508: } conti@508: conti@508: /**********************/ conti@508: /* B 1.5.3 - Programs */ conti@508: /**********************/ conti@508: void *lvalue_check_c::visit(program_declaration_c *symbol) { conti@508: search_varfb_instance_type = new search_varfb_instance_type_c(symbol); conti@508: search_var_instance_decl = new search_var_instance_decl_c(symbol); conti@508: symbol->function_block_body->accept(*this); conti@508: delete search_varfb_instance_type; conti@508: delete search_var_instance_decl; conti@508: search_varfb_instance_type = NULL; conti@508: search_var_instance_decl = NULL; conti@508: return NULL; conti@508: } conti@508: conti@508: /***************************************/ conti@508: /* B.3 - Language ST (Structured Text) */ conti@508: /***************************************/ conti@508: /***********************/ conti@508: /* B 3.1 - Expressions */ conti@508: /***********************/ msousa@509: // 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;) conti@508: void *lvalue_check_c::visit(function_invocation_c *symbol) { msousa@509: if (NULL != symbol->formal_param_list) msousa@509: check_formal_call(symbol, symbol->called_function_declaration); msousa@509: if (NULL != symbol->nonformal_param_list) msousa@509: check_nonformal_call(symbol, symbol->called_function_declaration); conti@508: return NULL; conti@508: } conti@508: conti@508: /*********************************/ conti@508: /* B 3.2.1 Assignment Statements */ conti@508: /*********************************/ conti@508: void *lvalue_check_c::visit(assignment_statement_c *symbol) { msousa@509: verify_is_lvalue(symbol->l_exp); conti@508: /* We call visit r_exp to check function_call */ conti@508: symbol->r_exp->accept(*this); conti@508: return NULL; conti@508: } conti@508: conti@508: /********************************/ conti@508: /* B 3.2.4 Iteration Statements */ conti@508: /********************************/ conti@508: void *lvalue_check_c::visit(for_statement_c *symbol) { conti@508: control_variables.push_back(symbol->control_variable); conti@508: symbol->statement_list->accept(*this); conti@508: control_variables.pop_back(); conti@508: return NULL; conti@508: } conti@508: conti@508: conti@508: conti@508: conti@508: conti@508: conti@508: