mjsousa@959: /*
mjsousa@959: * matiec - a compiler for the programming languages defined in IEC 61131-3
mjsousa@959: *
mjsousa@959: * Copyright (C) 2014 Mario de Sousa (msousa@fe.up.pt)
mjsousa@959: *
mjsousa@959: * This program is free software: you can redistribute it and/or modify
mjsousa@959: * it under the terms of the GNU General Public License as published by
mjsousa@959: * the Free Software Foundation, either version 3 of the License, or
mjsousa@959: * (at your option) any later version.
mjsousa@959: *
mjsousa@959: * This program is distributed in the hope that it will be useful,
mjsousa@959: * but WITHOUT ANY WARRANTY; without even the implied warranty of
mjsousa@959: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
mjsousa@959: * GNU General Public License for more details.
mjsousa@959: *
mjsousa@959: * You should have received a copy of the GNU General Public License
mjsousa@959: * along with this program. If not, see .
mjsousa@959: *
mjsousa@959: *
mjsousa@959: * This code is made available on the understanding that it will not be
mjsousa@959: * used in safety-critical situations without a full and competent review.
mjsousa@959: */
mjsousa@959:
mjsousa@959: /*
mjsousa@959: * An IEC 61131-3 compiler.
mjsousa@959: *
mjsousa@959: * Based on the
mjsousa@959: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
mjsousa@959: *
mjsousa@959: */
mjsousa@959:
mjsousa@959:
mjsousa@959: /*
mjsousa@959: * Re-oder the POUs in te library so that no forward references occur.
mjsousa@959: *
mjsousa@959: * Since stage1_2 now suppport POUs that contain references to POUS that are only declared later,
mjsousa@959: * (e.g. a variable of FB1_t is declared, before the FB1_T function block is itself declared!)
mjsousa@959: * we may need to re-order all the POUs in the library so that these forward references do not occur.
mjsousa@959: *
mjsousa@959: * This utility class will do just that. However, it does not destroy the original abstract syntax
mjsousa@959: * tree (AST). It instead creates a new re-ordered AST, by instantiating a new library_c object.
mjsousa@959: * This new library_c object will however point to the *same* objects of the original AST, just in
mjsousa@959: * a new order.
mjsousa@959: * This means that the new and original AST share all the object instances, and only use a distinct
mjsousa@959: * library_c object!
mjsousa@959: */
mjsousa@959:
mjsousa@959: #include "../absyntax/absyntax.hh"
mjsousa@959: #include "../absyntax/visitor.hh"
mjsousa@959: #include "../util/symtable.hh"
mjsousa@959: #include
mjsousa@959:
mjsousa@959:
mjsousa@959: class find_forward_dependencies_c;
mjsousa@971: typedef symtable_c identifiers_symbtable_t;
mjsousa@959:
mjsousa@959:
mjsousa@959:
mjsousa@959:
mjsousa@959:
mjsousa@959:
mjsousa@959:
mjsousa@959: class remove_forward_dependencies_c: public search_visitor_c {
mjsousa@959:
mjsousa@959: private:
mjsousa@959: /* The level of detail that the user wants us to display error messages. */
mjsousa@959: #define error_level_default (1)
mjsousa@959: #define error_level_nagging (4)
mjsousa@959: unsigned int current_display_error_level;
mjsousa@959: int error_count;
mjsousa@959: bool warning_found;
mjsousa@959: library_c *new_tree;
mjsousa@959: int cycle_count; // main algorithm runs in a loop. The nuber of the current cycle...
mjsousa@959: // NOTE: we need two lists in order to correctly handle overloaded functions
mjsousa@959: identifiers_symbtable_t declared_identifiers; // list of identifiers already declared by the symbols in the new tree
mjsousa@959: std::set inserted_symbols; // list of symbols already inserted in the new tree
mjsousa@959: symbol_c *current_code_generation_pragma; // points to any currently 'active' enable_code_generation_pragma_c
mjsousa@959: find_forward_dependencies_c *find_forward_dependencies;
mjsousa@959:
mjsousa@959: public:
mjsousa@959: remove_forward_dependencies_c(void);
mjsousa@959: ~remove_forward_dependencies_c(void);
mjsousa@959: library_c *create_new_tree(symbol_c *old_tree); // create a new tree with POUs ordered so it does not contain forward dependencies...
mjsousa@959: int get_error_count(void);
mjsousa@959:
mjsousa@959: private:
mjsousa@959: void *handle_library_symbol(symbol_c *symbol, symbol_c *name, symbol_c *search1, symbol_c *search2 = NULL, symbol_c *search3 = NULL);
mjsousa@959: void print_circ_error(library_c *symbol);
mjsousa@959:
mjsousa@959: /***************************/
mjsousa@959: /* B 0 - Programming Model */
mjsousa@959: /***************************/
mjsousa@959: void *visit(library_c *symbol);
mjsousa@959: /**************************************/
mjsousa@959: /* B.1.5 - Program organization units */
mjsousa@959: /**************************************/
mjsousa@959: void *visit(function_declaration_c *symbol);
mjsousa@959: void *visit(function_block_declaration_c *symbol);
mjsousa@959: void *visit(program_declaration_c *symbol);
mjsousa@959: /********************************/
mjsousa@959: /* B 1.7 Configuration elements */
mjsousa@959: /********************************/
mjsousa@959: void *visit(configuration_declaration_c *symbol);
mjsousa@959: /********************/
mjsousa@959: /* 2.1.6 - Pragmas */
mjsousa@959: /********************/
mjsousa@959: void *visit(disable_code_generation_pragma_c *symbol);
mjsousa@959: void *visit(enable_code_generation_pragma_c *symbol);
mjsousa@959: void *visit(pragma_c *symbol);
mjsousa@959:
mjsousa@959: }; /* class remove_forward_dependencies_c */
mjsousa@959:
mjsousa@959:
mjsousa@959: