conti@656: /* conti@656: * matiec - a compiler for the programming languages defined in IEC 61131-3 conti@656: * msousa@658: * Copyright (C) 2003-2012 Mario de Sousa (msousa@fe.up.pt) conti@656: * Copyright (C) 2012 Manuele Conti (conti.ma@alice.it) conti@656: * conti@656: * This program is free software: you can redistribute it and/or modify conti@656: * it under the terms of the GNU General Public License as published by conti@656: * the Free Software Foundation, either version 3 of the License, or conti@656: * (at your option) any later version. conti@656: * conti@656: * This program is distributed in the hope that it will be useful, conti@656: * but WITHOUT ANY WARRANTY; without even the implied warranty of conti@656: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the conti@656: * GNU General Public License for more details. conti@656: * conti@656: * You should have received a copy of the GNU General Public License conti@656: * along with this program. If not, see . conti@656: * conti@656: * conti@656: * This code is made available on the understanding that it will not be conti@656: * used in safety-critical situations without a full and competent review. conti@656: */ conti@656: conti@656: /* conti@656: * An IEC 61131-3 compiler. conti@656: * conti@656: * Based on the conti@656: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) conti@656: * conti@656: */ conti@656: conti@656: conti@656: /* Declaration sequence is a source code part needed to declare variables. conti@656: * There are some checks we need to do before start with other analysis: conti@656: * conti@656: * - Check external option redefinition. conti@656: * - Check external data type redefinition. conti@656: * - Check initial values consistently with the data types of the variables/data types being declared. conti@656: * - Check whether a function block uses a CONSTANT qualifier as described in 2.5.2.1. conti@656: * conti@656: */ conti@656: conti@656: conti@656: #include "declaration_check.hh" conti@656: #include "datatype_functions.hh" conti@656: conti@656: #define FIRST_(symbol1, symbol2) (((symbol1)->first_order < (symbol2)->first_order) ? (symbol1) : (symbol2)) conti@656: #define LAST_(symbol1, symbol2) (((symbol1)->last_order > (symbol2)->last_order) ? (symbol1) : (symbol2)) conti@656: conti@656: #define STAGE3_ERROR(error_level, symbol1, symbol2, ...) { \ conti@656: if (current_display_error_level >= error_level) { \ conti@656: fprintf(stderr, "%s:%d-%d..%d-%d: error: ", \ conti@656: FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column,\ conti@656: LAST_(symbol1,symbol2) ->last_line, LAST_(symbol1,symbol2) ->last_column);\ conti@656: fprintf(stderr, __VA_ARGS__); \ conti@656: fprintf(stderr, "\n"); \ conti@656: error_count++; \ conti@656: } \ conti@656: } conti@656: conti@656: conti@656: #define STAGE3_WARNING(symbol1, symbol2, ...) { \ conti@656: fprintf(stderr, "%s:%d-%d..%d-%d: warning: ", \ conti@656: FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column,\ conti@656: LAST_(symbol1,symbol2) ->last_line, LAST_(symbol1,symbol2) ->last_column);\ conti@656: fprintf(stderr, __VA_ARGS__); \ conti@656: fprintf(stderr, "\n"); \ conti@656: warning_found = true; \ conti@656: } conti@656: conti@656: conti@656: declaration_check_c::declaration_check_c(symbol_c *ignore) { conti@661: current_display_error_level = 0; conti@661: current_pou_decl = NULL; msousa@812: current_resource_decl = NULL; conti@656: error_count = 0; conti@656: } conti@656: conti@656: declaration_check_c::~declaration_check_c(void) { conti@656: conti@656: } conti@656: conti@656: int declaration_check_c::get_error_count() { conti@656: return error_count; conti@656: } conti@656: conti@656: void declaration_check_c::check_global_decl(symbol_c *p_decl) { msousa@812: if (NULL == current_pou_decl) ERROR; msousa@812: msousa@812: search_var_instance_decl_c search_var_instance_pou_glo_decl(current_pou_decl); msousa@812: search_var_instance_decl_c search_var_instance_res_glo_decl(current_resource_decl); msousa@812: search_var_instance_decl_c search_var_instance_ext_decl(p_decl); msousa@812: function_param_iterator_c fpi(p_decl); msousa@812: msousa@812: symbol_c *var_name; msousa@812: while((var_name = fpi.next()) != NULL) { msousa@812: if (fpi.param_direction() == function_param_iterator_c::direction_extref) { msousa@812: /* found an external reference parameter. */ msousa@812: symbol_c *ext_decl = search_var_instance_ext_decl.get_decl(var_name); msousa@812: msousa@812: // NOTE: Must check the POU first, and RESOURCE second! msousa@812: symbol_c *glo_decl = search_var_instance_res_glo_decl.get_decl(var_name); msousa@812: search_var_instance_decl_c *search_var_instance_glo_decl = &search_var_instance_res_glo_decl; msousa@812: if (NULL == glo_decl) { msousa@812: glo_decl = search_var_instance_pou_glo_decl.get_decl(var_name); msousa@812: search_var_instance_glo_decl = &search_var_instance_pou_glo_decl; conti@656: } msousa@812: msousa@812: if (NULL == glo_decl) { msousa@812: STAGE3_ERROR(0, ext_decl, ext_decl, "Declaration error. The external variable does not match with any global variable."); msousa@812: continue; msousa@812: } msousa@812: msousa@812: if (search_var_instance_glo_decl->get_option(var_name) != search_var_instance_ext_decl.get_option(var_name)) msousa@812: STAGE3_ERROR(0, glo_decl, glo_decl, "Declaration error. The external variable options do not match with thos of the global variable."); msousa@812: msousa@812: /* TODO: Check redefinition data type. msousa@812: * We need a new class (like search_base_type class) to get type id by variable declaration. msousa@812: * symbol_c *glo_type = ????; msousa@812: * symbol_c *ext_type = fpi.param_type(); msousa@812: */ msousa@812: /* For the moment, we will just use search_base_type_c instead... */ msousa@812: symbol_c *glo_type = search_base_type_c::get_basetype_decl(glo_decl); msousa@812: symbol_c *ext_type = search_base_type_c::get_basetype_decl(ext_decl); msousa@812: if (! get_datatype_info_c::is_type_equal(glo_type, ext_type)) msousa@812: STAGE3_ERROR(0, ext_decl, ext_decl, "Declaration error. Data type mismatch between external and global variable declarations."); msousa@812: } msousa@812: } conti@656: } conti@656: msousa@660: msousa@660: msousa@660: /*****************************/ msousa@660: /* B 1.5.2 - Function Blocks */ msousa@660: /*****************************/ msousa@660: /* FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */ msousa@660: // SYM_REF3(function_block_declaration_c, fblock_name, var_declarations, fblock_body) msousa@660: void *declaration_check_c::visit(function_block_declaration_c *symbol) { msousa@812: /* The following two lines of code are only valid for v3 of IEC 61131-3, that allows VAR_GLOBAL declarations inside FBs! msousa@812: * current_pou_decl = symbol; msousa@812: * current_resource_decl = NULL; msousa@812: */ msousa@660: /* check if any FB declared as a VAR has any incompatible VAR_EXTERNAL declarations */ msousa@660: if (NULL != symbol->var_declarations) msousa@660: symbol->var_declarations->accept(*this); msousa@660: return NULL; msousa@660: } msousa@660: conti@656: /******************************************/ msousa@658: /* B 1.5.3 - Declaration & Initialisation */ conti@656: /******************************************/ msousa@660: /* PROGRAM program_type_name program_var_declarations_list function_block_body END_PROGRAM */ msousa@660: // SYM_REF3(program_declaration_c, program_type_name, var_declarations, function_block_body) msousa@658: void *declaration_check_c::visit(program_declaration_c *symbol) { msousa@812: /* The following two lines of code are only valid for v3 of IEC 61131-3, that allows VAR_GLOBAL declarations inside PROGRAMs! msousa@812: * current_pou_decl = symbol; msousa@812: * current_resource_decl = NULL; msousa@812: */ msousa@660: /* check if any FB declared as a VAR has any incompatible VAR_EXTERNAL declarations */ msousa@660: if (NULL != symbol->var_declarations) msousa@660: symbol->var_declarations->accept(*this); conti@656: return NULL; conti@656: } conti@656: conti@656: /********************************/ conti@656: /* B 1.7 Configuration elements */ conti@656: /********************************/ msousa@660: /* msousa@660: * CONFIGURATION configuration_name msousa@660: * optional_global_var_declarations msousa@660: * (resource_declaration_list | single_resource_declaration) msousa@660: * optional_access_declarations msousa@660: * optional_instance_specific_initializations msousa@660: * END_CONFIGURATION msousa@660: */ msousa@660: //SYM_REF5(configuration_declaration_c, configuration_name, global_var_declarations, resource_declarations, access_declarations, instance_specific_initializations) msousa@660: void *declaration_check_c::visit(configuration_declaration_c *symbol) { msousa@660: current_pou_decl = symbol; msousa@660: /* check if any FB declared as a VAR has any incompatible VAR_EXTERNAL declarations */ msousa@660: if (NULL != symbol->resource_declarations) msousa@660: symbol->resource_declarations->accept(*this); msousa@812: current_pou_decl = NULL; msousa@812: return NULL; msousa@812: } msousa@812: msousa@812: /* msousa@812: RESOURCE resource_name ON resource_type_name msousa@812: optional_global_var_declarations msousa@812: single_resource_declaration msousa@812: END_RESOURCE msousa@812: */ msousa@812: /* enumvalue_symtable is filled in by enum_declaration_check_c, during stage3 semantic verification, with a list of all enumerated constants declared inside this POU */ msousa@812: // SYM_REF4(resource_declaration_c, resource_name, resource_type_name, global_var_declarations, resource_declaration, enumvalue_symtable_t enumvalue_symtable;) msousa@812: void *declaration_check_c::visit(resource_declaration_c *symbol) { msousa@812: current_resource_decl = symbol; msousa@812: /* check if any FB declared as a VAR has any incompatible VAR_EXTERNAL declarations */ msousa@812: symbol->resource_declaration->accept(*this); msousa@812: current_resource_decl = NULL; msousa@812: return NULL; msousa@812: } msousa@812: msousa@812: /* PROGRAM [RETAIN | NON_RETAIN] program_name [WITH task_name] ':' program_type_name ['(' prog_conf_elements ')'] */ msousa@660: void *declaration_check_c::visit(program_configuration_c *symbol) { msousa@660: symbol_c *p_decl = program_type_symtable.find_value(symbol->program_type_name); msousa@660: if (p_decl == program_type_symtable.end_value()) msousa@660: p_decl = function_block_type_symtable.find_value(symbol->program_type_name); msousa@660: /* stage1_2 guarantees that we are sure to find a declaration in FB or Program symtable. */ msousa@660: if (p_decl == function_block_type_symtable.end_value()) msousa@660: ERROR; msousa@660: check_global_decl(p_decl); msousa@660: return NULL; msousa@660: } msousa@660: