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; 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) { conti@656: symbol_c *var_name; conti@656: msousa@658: search_var_instance_decl_c search_var_instance_glo_decl(current_pou_decl); conti@656: search_var_instance_decl_c search_var_instance_ext_decl(p_decl); conti@656: function_param_iterator_c fpi(p_decl); conti@656: while((var_name = fpi.next()) != NULL) { conti@656: if (fpi.param_direction() == function_param_iterator_c::direction_extref) { conti@656: /* found an external reference parameter. */ conti@656: symbol_c *glo_decl = search_var_instance_glo_decl.get_decl(var_name); conti@656: symbol_c *ext_decl = search_var_instance_ext_decl.get_decl(var_name); conti@663: if (glo_decl == NULL) { conti@663: STAGE3_ERROR(0, ext_decl, ext_decl, "Declaration error an external doesn't mach with any global var."); conti@663: continue; conti@663: } conti@663: if (search_var_instance_glo_decl.get_option(var_name) != search_var_instance_ext_decl.get_option(var_name)) conti@656: STAGE3_ERROR(0, glo_decl, glo_decl, "Declaration error an external redefinition option."); conti@656: conti@656: /* TODO: Check redefinition data type. conti@656: * We need a new class (like search_base_type class) to get type id by variable declaration. conti@656: * symbol_c *glo_type = ????; conti@656: * symbol_c *ext_type = fpi.param_type(); msousa@658: */ msousa@658: /* For the moment, we will just use search_base_type_c instead... */ msousa@718: symbol_c *glo_type = search_base_type_c::get_basetype_decl(glo_decl); msousa@718: symbol_c *ext_type = search_base_type_c::get_basetype_decl(ext_decl); msousa@676: if (! get_datatype_info_c::is_type_equal(glo_type, ext_type)) msousa@658: STAGE3_ERROR(0, ext_decl, ext_decl, "Declaration error an external redefinition data type."); conti@656: } conti@656: } conti@656: 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@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->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@658: 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->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@660: return NULL; conti@656: } conti@656: 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: