stage3/remove_forward_dependencies.hh
changeset 959 8bfcc8e62bd6
child 971 8aee27d46208
equal deleted inserted replaced
958:7474d2cd1d6e 959:8bfcc8e62bd6
       
     1 /*
       
     2  *  matiec - a compiler for the programming languages defined in IEC 61131-3
       
     3  *
       
     4  *  Copyright (C) 2014  Mario de Sousa (msousa@fe.up.pt)
       
     5  *
       
     6  *  This program is free software: you can redistribute it and/or modify
       
     7  *  it under the terms of the GNU General Public License as published by
       
     8  *  the Free Software Foundation, either version 3 of the License, or
       
     9  *  (at your option) any later version.
       
    10  *
       
    11  *  This program is distributed in the hope that it will be useful,
       
    12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    14  *  GNU General Public License for more details.
       
    15  *
       
    16  *  You should have received a copy of the GNU General Public License
       
    17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
       
    18  *
       
    19  *
       
    20  * This code is made available on the understanding that it will not be
       
    21  * used in safety-critical situations without a full and competent review.
       
    22  */
       
    23 
       
    24 /*
       
    25  * An IEC 61131-3 compiler.
       
    26  *
       
    27  * Based on the
       
    28  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
       
    29  *
       
    30  */
       
    31 
       
    32 
       
    33 /*
       
    34  * Re-oder the POUs in te library so that no forward references occur.
       
    35  * 
       
    36  * Since stage1_2 now suppport POUs that contain references to POUS that are only declared later,
       
    37  * (e.g. a variable of FB1_t is declared, before the FB1_T function block is itself declared!)
       
    38  * we may need to re-order all the POUs in the library so that these forward references do not occur.
       
    39  * 
       
    40  * This utility class will do just that. However, it does not destroy the original abstract syntax 
       
    41  * tree (AST). It instead creates a new re-ordered AST, by instantiating a new library_c object.
       
    42  * This new library_c object will however point to the *same* objects of the original AST, just in 
       
    43  * a new order. 
       
    44  * This means that the new and original AST share all the object instances, and only use a distinct
       
    45  * library_c object!
       
    46  */
       
    47 
       
    48 #include "../absyntax/absyntax.hh"
       
    49 #include "../absyntax/visitor.hh"
       
    50 #include "../util/symtable.hh"
       
    51 #include <set>
       
    52 
       
    53 
       
    54 class   find_forward_dependencies_c;
       
    55 extern  symbol_c remove_forward_dependencies_c_null_symbol;
       
    56 typedef symtable_c<symbol_c *, &remove_forward_dependencies_c_null_symbol> identifiers_symbtable_t;
       
    57 
       
    58 
       
    59 
       
    60 
       
    61 
       
    62 
       
    63 
       
    64 class remove_forward_dependencies_c: public search_visitor_c {
       
    65 
       
    66   private:
       
    67     /* The level of detail that the user wants us to display error messages. */
       
    68     #define error_level_default (1)
       
    69     #define error_level_nagging (4)
       
    70     unsigned int    current_display_error_level;
       
    71     int             error_count;
       
    72     bool            warning_found;
       
    73     library_c      *new_tree;
       
    74     int             cycle_count; // main algorithm runs in a loop. The nuber of the current cycle...
       
    75     // NOTE: we need two lists in order to correctly handle overloaded functions
       
    76     identifiers_symbtable_t      declared_identifiers; // list of identifiers already declared by the symbols in the new tree
       
    77     std::set <symbol_c *>        inserted_symbols;     // list of symbols already inserted in the new tree 
       
    78     symbol_c       *current_code_generation_pragma;    // points to any currently 'active' enable_code_generation_pragma_c
       
    79     find_forward_dependencies_c    *find_forward_dependencies;  
       
    80 
       
    81   public:
       
    82      remove_forward_dependencies_c(void);
       
    83     ~remove_forward_dependencies_c(void);
       
    84     library_c *create_new_tree(symbol_c *old_tree);  // create a new tree with POUs ordered so it does not contain forward dependencies...
       
    85     int        get_error_count(void);
       
    86 
       
    87   private:
       
    88     void *handle_library_symbol(symbol_c *symbol, symbol_c *name, symbol_c *search1, symbol_c *search2 = NULL, symbol_c *search3 = NULL);
       
    89     void  print_circ_error(library_c *symbol);
       
    90 
       
    91     /***************************/
       
    92     /* B 0 - Programming Model */
       
    93     /***************************/
       
    94     void *visit(library_c *symbol);
       
    95     /**************************************/
       
    96     /* B.1.5 - Program organization units */
       
    97     /**************************************/
       
    98     void *visit(function_declaration_c *symbol);
       
    99     void *visit(function_block_declaration_c *symbol);
       
   100     void *visit(program_declaration_c *symbol);
       
   101     /********************************/
       
   102     /* B 1.7 Configuration elements */
       
   103     /********************************/
       
   104     void *visit(configuration_declaration_c *symbol);
       
   105     /********************/
       
   106     /* 2.1.6 - Pragmas  */
       
   107     /********************/
       
   108     void *visit(disable_code_generation_pragma_c *symbol);
       
   109     void *visit(enable_code_generation_pragma_c *symbol);
       
   110     void *visit(pragma_c *symbol);
       
   111 
       
   112 };   /* class remove_forward_dependencies_c */
       
   113 
       
   114 
       
   115