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: mjsousa@892: mjsousa@892: mjsousa@892: mjsousa@892: mjsousa@892: mjsousa@892: mjsousa@892: mjsousa@892: #include mjsousa@892: class check_extern_c: public iterator_visitor_c { mjsousa@892: public: mjsousa@892: mjsousa@892: private: mjsousa@892: int current_display_error_level; mjsousa@892: symbol_c *current_pou_decl; mjsousa@892: symbol_c *current_resource_decl; mjsousa@892: static std::set checked_decl; // A set with all the declarations that have already been checked, so we don't recheck it again! mjsousa@892: mjsousa@892: mjsousa@892: public: mjsousa@892: static int error_count; mjsousa@892: mjsousa@892: check_extern_c(symbol_c *current_pou, symbol_c *current_resource) { mjsousa@892: current_display_error_level = 0; mjsousa@892: current_pou_decl = current_pou; mjsousa@892: current_resource_decl = current_resource; mjsousa@892: } mjsousa@892: mjsousa@892: ~check_extern_c(void) {} mjsousa@892: mjsousa@892: mjsousa@892: mjsousa@892: void check_global_decl(symbol_c *p_decl) { mjsousa@892: if (NULL == current_pou_decl) ERROR; mjsousa@892: mjsousa@892: if (checked_decl.find(p_decl) != checked_decl.end()) return; // has already been checked! mjsousa@892: checked_decl.insert(p_decl); mjsousa@892: mjsousa@892: search_var_instance_decl_c search_var_instance_pou_glo_decl(current_pou_decl); mjsousa@892: search_var_instance_decl_c search_var_instance_res_glo_decl(current_resource_decl); mjsousa@892: search_var_instance_decl_c search_var_instance_ext_decl(p_decl); mjsousa@892: function_param_iterator_c fpi(p_decl); mjsousa@892: mjsousa@892: symbol_c *var_name; mjsousa@892: while((var_name = fpi.next()) != NULL) { mjsousa@892: if (fpi.param_direction() == function_param_iterator_c::direction_extref) { mjsousa@892: /* found an external reference parameter. */ mjsousa@892: symbol_c *ext_decl = search_var_instance_ext_decl.get_decl(var_name); mjsousa@892: mjsousa@892: // NOTE: Must check the POU first, and RESOURCE second! mjsousa@892: symbol_c *glo_decl = search_var_instance_res_glo_decl.get_decl(var_name); mjsousa@892: search_var_instance_decl_c *search_var_instance_glo_decl = &search_var_instance_res_glo_decl; mjsousa@892: if (NULL == glo_decl) { mjsousa@892: glo_decl = search_var_instance_pou_glo_decl.get_decl(var_name); mjsousa@892: search_var_instance_glo_decl = &search_var_instance_pou_glo_decl; mjsousa@892: } mjsousa@892: mjsousa@892: if (NULL == glo_decl) { mjsousa@892: STAGE3_ERROR(0, ext_decl, ext_decl, "Declaration error. The external variable does not match with any global variable."); mjsousa@892: continue; mjsousa@892: } mjsousa@892: mjsousa@892: /* Check whether variable's constness (CONSTANT) is compatible. mjsousa@892: * VAR_GLOBAL is contant => VAR_EXTERNAL must also be CONSTANT mjsousa@892: * VAR_GLOBAL is not contant => VAR_EXTERNAL may be CONSTANT, or not! mjsousa@892: */ mjsousa@892: search_var_instance_decl_c::opt_t ext_opt = search_var_instance_ext_decl. get_option(var_name); mjsousa@892: search_var_instance_decl_c::opt_t glo_opt = search_var_instance_glo_decl->get_option(var_name); mjsousa@892: if ((glo_opt == search_var_instance_decl_c::constant_opt) && (ext_opt != search_var_instance_decl_c::constant_opt)) mjsousa@892: STAGE3_ERROR(0, glo_decl, glo_decl, "Declaration error. The external variable must be declared as constant, as it maps to a constant global variable."); mjsousa@892: mjsousa@892: /* TODO: Check redefinition data type. mjsousa@892: * We need a new class (like search_base_type class) to get type id by variable declaration. mjsousa@892: * symbol_c *glo_type = ????; mjsousa@892: * symbol_c *ext_type = fpi.param_type(); mjsousa@892: */ mjsousa@892: /* For the moment, we will just use search_base_type_c instead... */ mjsousa@892: symbol_c *glo_type = search_base_type_c::get_basetype_decl(glo_decl); mjsousa@892: symbol_c *ext_type = search_base_type_c::get_basetype_decl(ext_decl); mjsousa@892: if (! get_datatype_info_c::is_type_equal(glo_type, ext_type)) mjsousa@892: STAGE3_ERROR(0, ext_decl, ext_decl, "Declaration error. Data type mismatch between external and global variable declarations."); mjsousa@892: } mjsousa@892: } mjsousa@892: } mjsousa@892: mjsousa@892: mjsousa@892: mjsousa@892: /********************************/ mjsousa@892: /* B 1.3.3 - Derived data types */ mjsousa@892: /********************************/ mjsousa@892: /* function_block_type_name ASSIGN structure_initialization */ mjsousa@892: /* structure_initialization -> may be NULL ! */ mjsousa@892: //SYM_REF2(fb_spec_init_c, function_block_type_name, structure_initialization) mjsousa@892: /* NOTE: This class is only used when declaring FB variables, as in: mjsousa@892: * name_list ':' function_block_type_name ASSIGN structure_initialization mjsousa@892: * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ mjsousa@892: * fb_spec_init_c mjsousa@892: * Here, fb_spec_init_c is used whether it is an external, local, or global variable! mjsousa@892: * Note too that we must also check the datatypes of external and global variables!! mjsousa@892: */ mjsousa@892: void *visit(fb_spec_init_c *symbol) { mjsousa@971: function_block_type_symtable_t::iterator iter = function_block_type_symtable.find(symbol->function_block_type_name); mjsousa@892: /* stage1_2 guarantees that we are sure to find a declaration in FB or Program symtable. */ mjsousa@971: if (iter == function_block_type_symtable.end()) mjsousa@892: ERROR; mjsousa@971: iter->second->accept(*this); // iter->second is a fb_decl mjsousa@892: return NULL; mjsousa@892: } mjsousa@892: mjsousa@892: /*****************************/ mjsousa@892: /* B 1.5.2 - Function Blocks */ mjsousa@892: /*****************************/ mjsousa@892: /* FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */ mjsousa@892: // SYM_REF3(function_block_declaration_c, fblock_name, var_declarations, fblock_body) mjsousa@892: void *visit(function_block_declaration_c *symbol) { mjsousa@892: check_global_decl(symbol); mjsousa@892: /* The following two lines of code are only valid for v3 of IEC 61131-3, that allows VAR_GLOBAL declarations inside FBs! mjsousa@892: * current_pou_decl = symbol; mjsousa@892: * current_resource_decl = NULL; mjsousa@892: */ mjsousa@892: /* check if any FB declared as a VAR has any incompatible VAR_EXTERNAL declarations */ mjsousa@892: if (NULL != symbol->var_declarations) mjsousa@892: symbol->var_declarations->accept(*this); // This will eventually call the visit(fb_spec_init_c *) method, if any FB are declared here! mjsousa@892: return NULL; mjsousa@892: } mjsousa@892: mjsousa@892: /******************************************/ mjsousa@892: /* B 1.5.3 - Declaration & Initialisation */ mjsousa@892: /******************************************/ mjsousa@892: /* PROGRAM program_type_name program_var_declarations_list function_block_body END_PROGRAM */ mjsousa@892: // SYM_REF3(program_declaration_c, program_type_name, var_declarations, function_block_body) mjsousa@892: void *visit(program_declaration_c *symbol) { mjsousa@892: check_global_decl(symbol); mjsousa@892: /* The following two lines of code are only valid for v3 of IEC 61131-3, that allows VAR_GLOBAL declarations inside PROGRAMs! mjsousa@892: * current_pou_decl = symbol; mjsousa@892: * current_resource_decl = NULL; mjsousa@892: */ mjsousa@892: /* check if any FB declared as a VAR has any incompatible VAR_EXTERNAL declarations */ mjsousa@892: if (NULL != symbol->var_declarations) mjsousa@892: symbol->var_declarations->accept(*this); // This will eventually call the visit(fb_spec_init_c *) method, if any FB are declared here! mjsousa@892: return NULL; mjsousa@892: } mjsousa@892: mjsousa@892: }; mjsousa@892: mjsousa@892: int check_extern_c::error_count = 0; mjsousa@892: std::set check_extern_c::checked_decl; mjsousa@892: mjsousa@892: mjsousa@892: mjsousa@892: mjsousa@892: mjsousa@892: mjsousa@892: mjsousa@892: mjsousa@892: mjsousa@892: 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() { mjsousa@892: return check_extern_c::error_count; mjsousa@892: } 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) mjsousa@892: void *declaration_check_c::visit(function_block_declaration_c *symbol) mjsousa@892: {return NULL;} // We only check the declarations that are used to instantiate variables. This is done in the configuration! 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) mjsousa@892: void *declaration_check_c::visit(program_declaration_c *symbol) mjsousa@892: {return NULL;} // We only check the declarations that are used to instantiate variables. This is done in the configuration! mjsousa@892: mjsousa@892: 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) { mjsousa@892: // check if any FB instantiated inside this resource (in a VAR_GLOBAL) has any VAR_EXTERNAL declarations incompatible with the configuration's VAR_GLOBALs mjsousa@892: check_extern_c check_extern(current_pou_decl, current_resource_decl); mjsousa@892: symbol->global_var_declarations->accept(check_extern); mjsousa@892: // Now check the Programs instantiated in this resource msousa@812: current_resource_decl = symbol; 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) { mjsousa@971: symbol_c *p_decl = NULL; mjsousa@971: program_type_symtable_t ::iterator iter_p = program_type_symtable .find(symbol->program_type_name); mjsousa@971: function_block_type_symtable_t::iterator iter_f = function_block_type_symtable.find(symbol->program_type_name); mjsousa@971: mjsousa@971: if (iter_p != program_type_symtable .end()) p_decl = iter_p->second; mjsousa@971: if (iter_f != function_block_type_symtable.end()) p_decl = iter_f->second; mjsousa@971: mjsousa@971: if ((iter_f != function_block_type_symtable.end()) && (iter_p != program_type_symtable.end())) mjsousa@971: ERROR; // Should never occur! stage1_2 guarantees that the same identifier cannot be re-used. mjsousa@971: if ((iter_f == function_block_type_symtable.end()) && (iter_p == program_type_symtable.end())) mjsousa@892: ERROR; // Should never occur! stage1_2 guarantees that we are sure to find a declaration in FB or Program symtable. mjsousa@971: mjsousa@892: check_extern_c check_extern(current_pou_decl, current_resource_decl); mjsousa@892: p_decl->accept(check_extern); msousa@660: return NULL; msousa@660: } msousa@660: