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