conti@656: /*
conti@656: * matiec - a compiler for the programming languages defined in IEC 61131-3
conti@656: *
conti@656: * Copyright (C) 2003-2011 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@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: search_base_type_c search_base_type;
conti@656:
conti@656: search_var_instance_decl_c search_var_instance_glo_decl(global_var_decls);
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@656: 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();
conti@656: * if (! is_type_equal(glo_type, ext_type))
conti@656: * STAGE3_ERROR(0, glo_decl, glo_decl, "Declaration error an external redefinition data type.");
conti@656: */
conti@656: }
conti@656: }
conti@656:
conti@656: }
conti@656:
conti@656: /******************************************/
conti@656: /* B 1.4.3 - Declaration & Initialisation */
conti@656: /******************************************/
conti@656: void *declaration_check_c::visit(global_var_declarations_c *symbol) {
conti@656: global_var_decls = dynamic_cast(symbol->global_var_decl_list);
conti@656: if (NULL == global_var_decls)
conti@656: ERROR;
conti@656: return NULL;
conti@656: }
conti@656:
conti@656: /********************************/
conti@656: /* B 1.7 Configuration elements */
conti@656: /********************************/
conti@656: void *declaration_check_c::visit(program_configuration_c *symbol) {
conti@656: symbol_c *p_decl = program_type_symtable.find_value(symbol->program_type_name);
conti@656: if (p_decl == program_type_symtable.end_value())
conti@656: p_decl = function_block_type_symtable.find_value(symbol->program_type_name);
conti@656: /* stage1_2 guarantees that we are sure to find a declaration in FB or Program symtable. */
conti@656: if (p_decl == function_block_type_symtable.end_value())
conti@656: ERROR;
conti@656: check_global_decl(p_decl);
conti@656: return NULL;
conti@656: }
conti@656: