msousa@752: /* msousa@752: * matiec - a compiler for the programming languages defined in IEC 61131-3 msousa@752: * msousa@752: * Copyright (C) 2012 Mario de Sousa (msousa@fe.up.pt) msousa@752: * msousa@752: * This program is free software: you can redistribute it and/or modify msousa@752: * it under the terms of the GNU General Public License as published by msousa@752: * the Free Software Foundation, either version 3 of the License, or msousa@752: * (at your option) any later version. msousa@752: * msousa@752: * This program is distributed in the hope that it will be useful, msousa@752: * but WITHOUT ANY WARRANTY; without even the implied warranty of msousa@752: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the msousa@752: * GNU General Public License for more details. msousa@752: * msousa@752: * You should have received a copy of the GNU General Public License msousa@752: * along with this program. If not, see . msousa@752: * msousa@752: * msousa@752: * This code is made available on the understanding that it will not be msousa@752: * used in safety-critical situations without a full and competent review. msousa@752: */ msousa@752: msousa@752: /* msousa@752: * An IEC 61131-3 compiler. msousa@752: * msousa@752: * Based on the msousa@752: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) msousa@752: * msousa@752: */ msousa@752: msousa@752: msousa@752: /* Declaration sequence is a source code part needed to declare variables. msousa@752: * There are some checks we need to do before start with other analysis: msousa@752: * msousa@752: * - Check external option redefinition. msousa@752: * - Check external data type redefinition. msousa@752: * - Check initial values consistently with the data types of the variables/data types being declared. msousa@752: * - Check whether a function block uses a CONSTANT qualifier as described in 2.5.2.1. msousa@752: * msousa@752: */ msousa@752: msousa@752: msousa@752: #include "enum_declaration_check.hh" msousa@752: msousa@752: msousa@752: msousa@752: #define FIRST_(symbol1, symbol2) (((symbol1)->first_order < (symbol2)->first_order) ? (symbol1) : (symbol2)) msousa@752: #define LAST_(symbol1, symbol2) (((symbol1)->last_order > (symbol2)->last_order) ? (symbol1) : (symbol2)) msousa@752: msousa@752: #define STAGE3_ERROR(error_level, symbol1, symbol2, ...) { \ msousa@752: if (current_display_error_level >= error_level) { \ msousa@752: fprintf(stderr, "%s:%d-%d..%d-%d: error: ", \ msousa@752: FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column,\ msousa@752: LAST_(symbol1,symbol2) ->last_line, LAST_(symbol1,symbol2) ->last_column);\ msousa@752: fprintf(stderr, __VA_ARGS__); \ msousa@752: fprintf(stderr, "\n"); \ msousa@752: error_count++; \ msousa@752: } \ msousa@752: } msousa@752: msousa@752: msousa@752: #define STAGE3_WARNING(symbol1, symbol2, ...) { \ msousa@752: fprintf(stderr, "%s:%d-%d..%d-%d: warning: ", \ msousa@752: FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column,\ msousa@752: LAST_(symbol1,symbol2) ->last_line, LAST_(symbol1,symbol2) ->last_column);\ msousa@752: fprintf(stderr, __VA_ARGS__); \ msousa@752: fprintf(stderr, "\n"); \ msousa@752: warning_found = true; \ msousa@752: } msousa@752: msousa@752: msousa@752: msousa@752: msousa@752: msousa@752: msousa@752: msousa@752: msousa@752: /*****************************************************/ msousa@752: /* */ msousa@752: /* A small helper class... */ msousa@752: /* */ msousa@752: /*****************************************************/ msousa@752: msousa@752: /* Add to the local_enumerated_value_symtable the local enum value constants */ msousa@752: msousa@752: msousa@752: class populate_enumvalue_symtable_c: public iterator_visitor_c { msousa@752: private: msousa@752: symbol_c *current_enumerated_type; msousa@752: symbol_c::enumvalue_symtable_t *enumvalue_symtable; msousa@752: msousa@752: private: msousa@752: int &error_count; msousa@752: int ¤t_display_error_level; msousa@752: msousa@752: public: msousa@752: populate_enumvalue_symtable_c(int &error_count_, int ¤t_display_error_level_) msousa@752: : error_count(error_count_), current_display_error_level(current_display_error_level_) { msousa@752: current_enumerated_type = NULL; msousa@752: enumvalue_symtable = NULL; msousa@752: }; msousa@752: ~populate_enumvalue_symtable_c(void) {} msousa@752: msousa@752: void populate(symbol_c::enumvalue_symtable_t *symtable, symbol_c *symbol) { msousa@752: enumvalue_symtable = symtable; msousa@752: symbol->accept(*this); msousa@752: enumvalue_symtable = NULL; msousa@752: } msousa@752: msousa@752: protected: msousa@752: /*************************/ msousa@752: /* B.1 - Common elements */ msousa@752: /*************************/ msousa@752: /**********************/ msousa@752: /* B.1.3 - Data types */ msousa@752: /**********************/ msousa@752: /********************************/ msousa@752: /* B 1.3.3 - Derived data types */ msousa@752: /********************************/ msousa@752: /* enumerated_type_name ':' enumerated_spec_init */ msousa@752: void *visit(enumerated_type_declaration_c *symbol) { msousa@752: current_enumerated_type = symbol; msousa@752: symbol->enumerated_spec_init->accept(*this); msousa@752: current_enumerated_type = NULL; msousa@752: return NULL; msousa@752: } msousa@752: msousa@752: /* enumerated_specification ASSIGN enumerated_value */ msousa@752: void *visit(enumerated_spec_init_c *symbol) { msousa@752: if (NULL == current_enumerated_type) msousa@752: current_enumerated_type = symbol; msousa@752: symbol->enumerated_specification->accept(*this); msousa@752: /* DO NOT visit the symbol->enumerated_value !!! */ msousa@752: current_enumerated_type = NULL; msousa@752: return NULL; msousa@752: } msousa@752: msousa@752: /* [enumerated_type_name '#'] identifier */ msousa@752: void *visit(enumerated_value_c *symbol) { msousa@752: token_c *value = dynamic_cast (symbol->value); msousa@752: if (NULL == value) ERROR; msousa@752: const char *value_str = value->value; msousa@752: msousa@752: if (current_enumerated_type == NULL) ERROR; msousa@752: /* this is really an ERROR! The initial value may use the syntax NUM_TYPE#enum_value, but in that case we should not have reached this visit method !! */ msousa@752: if (symbol->type != NULL) ERROR; msousa@752: msousa@752: symbol_c::enumvalue_symtable_t::iterator lower = enumvalue_symtable->lower_bound(value_str); msousa@752: symbol_c::enumvalue_symtable_t::iterator upper = enumvalue_symtable->upper_bound(value_str); msousa@752: for (; lower != upper; lower++) msousa@752: if (lower->second == current_enumerated_type) { msousa@752: /* The same identifier is used more than once as an enumerated value/constant inside the same enumerated datat type! */ msousa@752: STAGE3_ERROR(0, symbol, symbol, "Duplicate identifier in enumerated data type."); msousa@752: return NULL; /* No need to insert it! It is already in the table! */ msousa@752: } msousa@752: msousa@752: /* add it to the local symbol table. */ msousa@752: enumvalue_symtable->insert(std::pair(value_str, current_enumerated_type)); msousa@752: return NULL; msousa@752: } msousa@752: }; // class populate_enumvalue_symtable_c msousa@752: msousa@752: msousa@752: msousa@752: msousa@752: msousa@752: msousa@752: msousa@752: msousa@752: msousa@752: msousa@752: msousa@752: enum_declaration_check_c::enum_declaration_check_c(symbol_c *ignore) { msousa@752: error_count = 0; msousa@752: current_display_error_level = 0; msousa@752: global_enumvalue_symtable = NULL; msousa@752: populate_enumvalue_symtable = new populate_enumvalue_symtable_c(error_count, current_display_error_level); msousa@752: } msousa@752: msousa@752: enum_declaration_check_c::~enum_declaration_check_c(void) {} msousa@752: msousa@752: int enum_declaration_check_c::get_error_count() {return error_count;} msousa@752: msousa@752: msousa@752: /***************************/ msousa@752: /* B 0 - Programming Model */ msousa@752: /***************************/ msousa@752: void *enum_declaration_check_c::visit(library_c *symbol) { msousa@752: global_enumvalue_symtable = &(symbol->enumvalue_symtable); msousa@752: iterator_visitor_c::visit(symbol); // fall back to base class msousa@752: return NULL; msousa@752: } msousa@752: msousa@752: msousa@752: msousa@752: /**********************/ msousa@752: /* B.1.3 - Data types */ msousa@752: /**********************/ msousa@752: void *enum_declaration_check_c::visit(data_type_declaration_c *symbol) { msousa@752: if (NULL == global_enumvalue_symtable) ERROR; msousa@752: populate_enumvalue_symtable->populate(global_enumvalue_symtable, symbol); msousa@752: return NULL; msousa@752: } msousa@752: msousa@752: /*********************/ msousa@752: /* B 1.5.1 Functions */ msousa@752: /*********************/ msousa@752: void *enum_declaration_check_c::visit(function_declaration_c *symbol) { msousa@752: populate_enumvalue_symtable->populate(&(symbol->enumvalue_symtable), symbol->var_declarations_list); msousa@752: return NULL; msousa@752: } msousa@752: msousa@752: /***************************/ msousa@752: /* B 1.5.2 Function blocks */ msousa@752: /***************************/ msousa@752: void *enum_declaration_check_c::visit(function_block_declaration_c *symbol) { msousa@752: populate_enumvalue_symtable->populate(&(symbol->enumvalue_symtable), symbol->var_declarations); msousa@752: return NULL; msousa@752: } msousa@752: msousa@752: /**********************/ msousa@752: /* B 1.5.3 - Programs */ msousa@752: /**********************/ msousa@752: void *enum_declaration_check_c::visit(program_declaration_c *symbol) { msousa@752: populate_enumvalue_symtable->populate(&(symbol->enumvalue_symtable), symbol->var_declarations); msousa@752: return NULL; msousa@752: } msousa@752: msousa@752: /********************************/ msousa@752: /* B 1.7 Configuration elements */ msousa@752: /********************************/ msousa@752: void *enum_declaration_check_c::visit(configuration_declaration_c *symbol) { msousa@752: if (NULL != symbol->global_var_declarations) msousa@752: populate_enumvalue_symtable->populate(&(symbol->enumvalue_symtable), symbol->global_var_declarations); msousa@752: if (NULL != symbol->resource_declarations) msousa@752: /* May reference either a list of resource_declaration_c, or a single_resource_declaration_c */ msousa@752: populate_enumvalue_symtable->populate(&(symbol->enumvalue_symtable), symbol->resource_declarations); msousa@752: msousa@752: return NULL; msousa@752: } msousa@752: msousa@752: msousa@752: void *enum_declaration_check_c::visit(resource_declaration_c *symbol) { msousa@752: if (NULL != symbol->global_var_declarations) msousa@752: populate_enumvalue_symtable->populate(&(symbol->enumvalue_symtable), symbol->global_var_declarations); msousa@752: return NULL; msousa@752: } msousa@752: msousa@752: msousa@752: void *enum_declaration_check_c::visit(single_resource_declaration_c *symbol) { msousa@752: /* do NOT visit! */ msousa@752: return NULL; msousa@752: }