stage3/declaration_check.cc
author mjsousa
Sun, 28 Dec 2014 12:05:39 +0000
changeset 979 f8f1d89ff7d5
parent 971 8aee27d46208
child 982 760b26477193
permissions -rw-r--r--
Add debug_c::print(const_value_c) for printing of const values.
656
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
     1
/*
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
     2
 *  matiec - a compiler for the programming languages defined in IEC 61131-3
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
     3
 *
658
fe5e1f01a49e store POU reference to POU declaration instead of global var declarations.
Mario de Sousa <msousa@fe.up.pt>
parents: 656
diff changeset
     4
 *  Copyright (C) 2003-2012  Mario de Sousa (msousa@fe.up.pt)
656
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
     5
 *  Copyright (C) 2012       Manuele Conti (conti.ma@alice.it)
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
     6
 *
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
     7
 *  This program is free software: you can redistribute it and/or modify
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
     8
 *  it under the terms of the GNU General Public License as published by
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
     9
 *  the Free Software Foundation, either version 3 of the License, or
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    10
 *  (at your option) any later version.
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    11
 *
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    12
 *  This program is distributed in the hope that it will be useful,
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    15
 *  GNU General Public License for more details.
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    16
 *
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    17
 *  You should have received a copy of the GNU General Public License
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    19
 *
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    20
 *
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    21
 * This code is made available on the understanding that it will not be
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    22
 * used in safety-critical situations without a full and competent review.
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    23
 */
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    24
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    25
/*
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    26
 * An IEC 61131-3 compiler.
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    27
 *
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    28
 * Based on the
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    29
 * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    30
 *
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    31
 */
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    32
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    33
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    34
/* Declaration sequence is a source code part needed to declare variables.
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    35
 * There are some checks we need to do before start with other analysis:
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    36
 *
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    37
 *   - Check external option redefinition.
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    38
 *   - Check external data type redefinition.
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    39
 *   - Check initial values consistently with the data types of the variables/data types being declared.
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    40
 *   - Check whether a function block uses a CONSTANT qualifier as described in 2.5.2.1.
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    41
 *
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    42
 */
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    43
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    44
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    45
#include "declaration_check.hh"
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    46
#include "datatype_functions.hh"
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    47
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    48
#define FIRST_(symbol1, symbol2) (((symbol1)->first_order < (symbol2)->first_order)   ? (symbol1) : (symbol2))
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    49
#define  LAST_(symbol1, symbol2) (((symbol1)->last_order  > (symbol2)->last_order)    ? (symbol1) : (symbol2))
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    50
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    51
#define STAGE3_ERROR(error_level, symbol1, symbol2, ...) {                                                                  \
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    52
  if (current_display_error_level >= error_level) {                                                                         \
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    53
    fprintf(stderr, "%s:%d-%d..%d-%d: error: ",                                                                             \
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    54
            FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column,\
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    55
                                                 LAST_(symbol1,symbol2) ->last_line,  LAST_(symbol1,symbol2) ->last_column);\
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    56
    fprintf(stderr, __VA_ARGS__);                                                                                           \
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    57
    fprintf(stderr, "\n");                                                                                                  \
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    58
    error_count++;                                                                                                     \
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    59
  }                                                                                                                         \
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    60
}
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    61
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    62
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    63
#define STAGE3_WARNING(symbol1, symbol2, ...) {                                                                             \
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    64
    fprintf(stderr, "%s:%d-%d..%d-%d: warning: ",                                                                           \
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    65
            FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column,\
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    66
                                                 LAST_(symbol1,symbol2) ->last_line,  LAST_(symbol1,symbol2) ->last_column);\
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    67
    fprintf(stderr, __VA_ARGS__);                                                                                           \
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    68
    fprintf(stderr, "\n");                                                                                                  \
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    69
    warning_found = true;                                                                                                   \
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    70
}
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    71
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    72
892
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
    73
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
    74
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
    75
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
    76
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
    77
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 892
diff changeset
    78
#include "constant_folding.hh"
892
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
    79
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
    80
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
    81
#include <set>
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
    82
class check_extern_c: public iterator_visitor_c {
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
    83
  public:
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
    84
  
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
    85
  private:
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
    86
    int current_display_error_level;
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
    87
    symbol_c *current_pou_decl;
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
    88
    symbol_c *current_resource_decl;
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
    89
    static std::set<symbol_c *> checked_decl; // A set with all the declarations that have already been checked, so we don't recheck it again!
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
    90
  
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
    91
  
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
    92
  public:
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
    93
    static int error_count;
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
    94
    
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
    95
    check_extern_c(symbol_c *current_pou, symbol_c *current_resource) {
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
    96
      current_display_error_level = 0;
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
    97
      current_pou_decl      = current_pou;
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
    98
      current_resource_decl = current_resource;
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
    99
    }
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   100
        
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   101
    ~check_extern_c(void) {}
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   102
    
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   103
    
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   104
    
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   105
    void check_global_decl(symbol_c *p_decl) {
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   106
      if (NULL == current_pou_decl) ERROR;
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   107
      
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   108
      if (checked_decl.find(p_decl) != checked_decl.end()) return; // has already been checked!
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   109
      checked_decl.insert(p_decl);
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   110
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   111
      search_var_instance_decl_c search_var_instance_pou_glo_decl(current_pou_decl);
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   112
      search_var_instance_decl_c search_var_instance_res_glo_decl(current_resource_decl);
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   113
      search_var_instance_decl_c search_var_instance_ext_decl(p_decl);
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   114
      function_param_iterator_c  fpi(p_decl);
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   115
      
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   116
      symbol_c *var_name;
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   117
      while((var_name = fpi.next()) != NULL) {            
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   118
        if (fpi.param_direction() == function_param_iterator_c::direction_extref) {
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   119
          /* found an external reference parameter. */
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   120
          symbol_c *ext_decl = search_var_instance_ext_decl.get_decl(var_name);
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   121
          
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   122
          // NOTE: Must check the POU first, and RESOURCE second!
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   123
          symbol_c                                       *glo_decl =  search_var_instance_res_glo_decl.get_decl(var_name);           
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   124
              search_var_instance_decl_c *search_var_instance_glo_decl = &search_var_instance_res_glo_decl;
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   125
          if (NULL == glo_decl) {
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   126
            glo_decl                     =  search_var_instance_pou_glo_decl.get_decl(var_name);
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   127
            search_var_instance_glo_decl = &search_var_instance_pou_glo_decl;
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   128
          }
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   129
          
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   130
          if (NULL == glo_decl) {
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   131
            STAGE3_ERROR(0, ext_decl, ext_decl, "Declaration error. The external variable does not match with any global variable.");
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   132
            continue;
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   133
          }
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   134
    
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   135
          /* Check whether variable's constness (CONSTANT) is compatible.
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   136
           *        VAR_GLOBAL is contant     => VAR_EXTERNAL must also be CONSTANT
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   137
           *        VAR_GLOBAL is not contant => VAR_EXTERNAL may be CONSTANT, or not!
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   138
           */
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   139
          search_var_instance_decl_c::opt_t ext_opt = search_var_instance_ext_decl. get_option(var_name);
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   140
          search_var_instance_decl_c::opt_t glo_opt = search_var_instance_glo_decl->get_option(var_name);
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   141
          if ((glo_opt == search_var_instance_decl_c::constant_opt) && (ext_opt != search_var_instance_decl_c::constant_opt))
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   142
            STAGE3_ERROR(0, glo_decl, glo_decl, "Declaration error. The external variable must be declared as constant, as it maps to a constant global variable.");
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   143
    
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 892
diff changeset
   144
          /* Copy the const_value from the global to the external variable.
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 892
diff changeset
   145
           *   This is actually part of the constant folding algorithm. It is placed here because it needs the extern<->global pairing
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 892
diff changeset
   146
           *   information to run correctly, which is all available right here!
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 892
diff changeset
   147
           */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 892
diff changeset
   148
          constant_folding_c constant_folding;
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 892
diff changeset
   149
          constant_folding.handle_var_extern_global_pair(var_name /* extern_var */, ext_decl /* extern_decl */, NULL /* global_var */, glo_decl /* global_decl */);
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 892
diff changeset
   150
892
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   151
          /* TODO: Check redefinition data type.
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   152
           *       We need a new class (like search_base_type class) to get type id by variable declaration.
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   153
           *  symbol_c *glo_type = ????;
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   154
           *  symbol_c *ext_type = fpi.param_type();
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   155
           */
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   156
          /* For the moment, we will just use search_base_type_c instead... */
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   157
          symbol_c *glo_type = search_base_type_c::get_basetype_decl(glo_decl);
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   158
          symbol_c *ext_type = search_base_type_c::get_basetype_decl(ext_decl);
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   159
          if (! get_datatype_info_c::is_type_equal(glo_type, ext_type))
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   160
            STAGE3_ERROR(0, ext_decl, ext_decl, "Declaration error.  Data type mismatch between external and global variable declarations.");
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   161
        }
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   162
      }
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   163
    }
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   164
    
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   165
    
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   166
    
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   167
    /********************************/
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   168
    /* B 1.3.3 - Derived data types */
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   169
    /********************************/
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   170
    /*  function_block_type_name ASSIGN structure_initialization */
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   171
    /* structure_initialization -> may be NULL ! */
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   172
    //SYM_REF2(fb_spec_init_c, function_block_type_name, structure_initialization)
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   173
    /* NOTE: This class is only used when declaring FB variables, as in:
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   174
     * name_list ':' function_block_type_name ASSIGN structure_initialization
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   175
     *               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   176
     *                      fb_spec_init_c                                   
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   177
     * Here, fb_spec_init_c is used whether it is an external, local, or global variable!
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   178
     * Note too that we must also check the datatypes of external and global variables!! 
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   179
     */
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   180
    void *visit(fb_spec_init_c *symbol) {
971
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 963
diff changeset
   181
      function_block_type_symtable_t::iterator iter = function_block_type_symtable.find(symbol->function_block_type_name);
892
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   182
      /* stage1_2 guarantees that we are sure to find a declaration in FB or Program symtable. */
971
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 963
diff changeset
   183
      if (iter == function_block_type_symtable.end())
892
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   184
        ERROR;
971
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 963
diff changeset
   185
      iter->second->accept(*this); // iter->second is a fb_decl
892
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   186
      return NULL;
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   187
    }
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   188
    
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   189
    /*****************************/
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   190
    /* B 1.5.2 - Function Blocks */
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   191
    /*****************************/
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   192
    /*  FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   193
    // SYM_REF3(function_block_declaration_c, fblock_name, var_declarations, fblock_body)
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   194
    void *visit(function_block_declaration_c *symbol) {
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   195
      check_global_decl(symbol);
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   196
      /* The following two lines of code are only valid for v3 of IEC 61131-3, that allows VAR_GLOBAL declarations inside FBs!
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   197
       * current_pou_decl = symbol;  
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   198
       * current_resource_decl = NULL;  
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   199
       */
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   200
      /* check if any FB declared as a VAR has any incompatible VAR_EXTERNAL declarations */
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   201
      if (NULL != symbol->var_declarations)
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   202
        symbol->var_declarations->accept(*this);  // This will eventually call the visit(fb_spec_init_c *) method, if any FB are declared here!
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   203
      return NULL;
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   204
    }
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   205
    
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   206
    /******************************************/
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   207
    /* B 1.5.3 - Declaration & Initialisation */
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   208
    /******************************************/
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   209
    /*  PROGRAM program_type_name program_var_declarations_list function_block_body END_PROGRAM */
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   210
    // SYM_REF3(program_declaration_c, program_type_name, var_declarations, function_block_body)
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   211
    void *visit(program_declaration_c *symbol) {
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   212
      check_global_decl(symbol);
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   213
      /* The following two lines of code are only valid for v3 of IEC 61131-3, that allows VAR_GLOBAL declarations inside PROGRAMs!
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   214
       * current_pou_decl = symbol;  
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   215
       * current_resource_decl = NULL;  
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   216
       */
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   217
      /* check if any FB declared as a VAR has any incompatible VAR_EXTERNAL declarations */
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   218
      if (NULL != symbol->var_declarations)
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   219
        symbol->var_declarations->accept(*this);  // This will eventually call the visit(fb_spec_init_c *) method, if any FB are declared here!
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   220
      return NULL;
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   221
    }
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   222
    
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   223
};
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   224
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   225
int check_extern_c::error_count = 0;
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   226
std::set<symbol_c *> check_extern_c::checked_decl;
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   227
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   228
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   229
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   230
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   231
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   232
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   233
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   234
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   235
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   236
656
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   237
declaration_check_c::declaration_check_c(symbol_c *ignore) {
661
f537c3315f83 Minor changes needed to build with pedantic flag.
Manuele Conti <conti.ma@alice.it>
parents: 660
diff changeset
   238
  current_display_error_level = 0;
f537c3315f83 Minor changes needed to build with pedantic flag.
Manuele Conti <conti.ma@alice.it>
parents: 660
diff changeset
   239
  current_pou_decl = NULL;
812
6679b6b21214 Partial fix to VAR_EXTERN vs VAR_GLOBAL check. Now considers globals declared in resources too. TODO: recursively check FB declared in the program.
Mario de Sousa <msousa@fe.up.pt>
parents: 718
diff changeset
   240
  current_resource_decl = NULL;
656
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   241
  error_count = 0;
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   242
}
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   243
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   244
declaration_check_c::~declaration_check_c(void) {
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   245
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   246
}
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   247
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   248
int declaration_check_c::get_error_count() {
892
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   249
  return check_extern_c::error_count;
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   250
}
660
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   251
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   252
/*****************************/
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   253
/* B 1.5.2 - Function Blocks */
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   254
/*****************************/
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   255
/*  FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   256
// SYM_REF3(function_block_declaration_c, fblock_name, var_declarations, fblock_body)
892
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   257
void *declaration_check_c::visit(function_block_declaration_c *symbol)
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   258
  {return NULL;} // We only check the declarations that are used to instantiate variables. This is done in the configuration!
660
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   259
656
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   260
/******************************************/
658
fe5e1f01a49e store POU reference to POU declaration instead of global var declarations.
Mario de Sousa <msousa@fe.up.pt>
parents: 656
diff changeset
   261
/* B 1.5.3 - Declaration & Initialisation */
656
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   262
/******************************************/
660
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   263
/*  PROGRAM program_type_name program_var_declarations_list function_block_body END_PROGRAM */
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   264
// SYM_REF3(program_declaration_c, program_type_name, var_declarations, function_block_body)
892
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   265
void *declaration_check_c::visit(program_declaration_c *symbol) 
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   266
  {return NULL;} // We only check the declarations that are used to instantiate variables. This is done in the configuration!
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   267
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   268
656
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   269
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   270
/********************************/
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   271
/* B 1.7 Configuration elements */
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   272
/********************************/
660
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   273
/*
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   274
 * CONFIGURATION configuration_name
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   275
 *    optional_global_var_declarations
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   276
 *    (resource_declaration_list | single_resource_declaration)
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   277
 *    optional_access_declarations
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   278
 *    optional_instance_specific_initializations
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   279
 * END_CONFIGURATION
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   280
 */
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   281
//SYM_REF5(configuration_declaration_c, configuration_name, global_var_declarations, resource_declarations, access_declarations, instance_specific_initializations)
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   282
void *declaration_check_c::visit(configuration_declaration_c *symbol) {
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   283
  current_pou_decl = symbol;
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   284
  /* check if any FB declared as a VAR has any incompatible VAR_EXTERNAL declarations */
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   285
  if (NULL != symbol->resource_declarations)
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   286
    symbol->resource_declarations->accept(*this);
812
6679b6b21214 Partial fix to VAR_EXTERN vs VAR_GLOBAL check. Now considers globals declared in resources too. TODO: recursively check FB declared in the program.
Mario de Sousa <msousa@fe.up.pt>
parents: 718
diff changeset
   287
  current_pou_decl = NULL;
6679b6b21214 Partial fix to VAR_EXTERN vs VAR_GLOBAL check. Now considers globals declared in resources too. TODO: recursively check FB declared in the program.
Mario de Sousa <msousa@fe.up.pt>
parents: 718
diff changeset
   288
  return NULL;
6679b6b21214 Partial fix to VAR_EXTERN vs VAR_GLOBAL check. Now considers globals declared in resources too. TODO: recursively check FB declared in the program.
Mario de Sousa <msousa@fe.up.pt>
parents: 718
diff changeset
   289
}
6679b6b21214 Partial fix to VAR_EXTERN vs VAR_GLOBAL check. Now considers globals declared in resources too. TODO: recursively check FB declared in the program.
Mario de Sousa <msousa@fe.up.pt>
parents: 718
diff changeset
   290
6679b6b21214 Partial fix to VAR_EXTERN vs VAR_GLOBAL check. Now considers globals declared in resources too. TODO: recursively check FB declared in the program.
Mario de Sousa <msousa@fe.up.pt>
parents: 718
diff changeset
   291
/*
6679b6b21214 Partial fix to VAR_EXTERN vs VAR_GLOBAL check. Now considers globals declared in resources too. TODO: recursively check FB declared in the program.
Mario de Sousa <msousa@fe.up.pt>
parents: 718
diff changeset
   292
RESOURCE resource_name ON resource_type_name
6679b6b21214 Partial fix to VAR_EXTERN vs VAR_GLOBAL check. Now considers globals declared in resources too. TODO: recursively check FB declared in the program.
Mario de Sousa <msousa@fe.up.pt>
parents: 718
diff changeset
   293
   optional_global_var_declarations
6679b6b21214 Partial fix to VAR_EXTERN vs VAR_GLOBAL check. Now considers globals declared in resources too. TODO: recursively check FB declared in the program.
Mario de Sousa <msousa@fe.up.pt>
parents: 718
diff changeset
   294
   single_resource_declaration
6679b6b21214 Partial fix to VAR_EXTERN vs VAR_GLOBAL check. Now considers globals declared in resources too. TODO: recursively check FB declared in the program.
Mario de Sousa <msousa@fe.up.pt>
parents: 718
diff changeset
   295
END_RESOURCE
6679b6b21214 Partial fix to VAR_EXTERN vs VAR_GLOBAL check. Now considers globals declared in resources too. TODO: recursively check FB declared in the program.
Mario de Sousa <msousa@fe.up.pt>
parents: 718
diff changeset
   296
*/
6679b6b21214 Partial fix to VAR_EXTERN vs VAR_GLOBAL check. Now considers globals declared in resources too. TODO: recursively check FB declared in the program.
Mario de Sousa <msousa@fe.up.pt>
parents: 718
diff changeset
   297
/* enumvalue_symtable is filled in by enum_declaration_check_c, during stage3 semantic verification, with a list of all enumerated constants declared inside this POU */
6679b6b21214 Partial fix to VAR_EXTERN vs VAR_GLOBAL check. Now considers globals declared in resources too. TODO: recursively check FB declared in the program.
Mario de Sousa <msousa@fe.up.pt>
parents: 718
diff changeset
   298
// SYM_REF4(resource_declaration_c, resource_name, resource_type_name, global_var_declarations, resource_declaration, enumvalue_symtable_t enumvalue_symtable;)
6679b6b21214 Partial fix to VAR_EXTERN vs VAR_GLOBAL check. Now considers globals declared in resources too. TODO: recursively check FB declared in the program.
Mario de Sousa <msousa@fe.up.pt>
parents: 718
diff changeset
   299
void *declaration_check_c::visit(resource_declaration_c *symbol) {
892
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   300
  // check if any FB instantiated inside this resource (in a VAR_GLOBAL) has any VAR_EXTERNAL declarations incompatible with the configuration's VAR_GLOBALs
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   301
  check_extern_c check_extern(current_pou_decl, current_resource_decl);
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   302
  symbol->global_var_declarations->accept(check_extern);   
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   303
  // Now check the Programs instantiated in this resource
812
6679b6b21214 Partial fix to VAR_EXTERN vs VAR_GLOBAL check. Now considers globals declared in resources too. TODO: recursively check FB declared in the program.
Mario de Sousa <msousa@fe.up.pt>
parents: 718
diff changeset
   304
  current_resource_decl = symbol;
6679b6b21214 Partial fix to VAR_EXTERN vs VAR_GLOBAL check. Now considers globals declared in resources too. TODO: recursively check FB declared in the program.
Mario de Sousa <msousa@fe.up.pt>
parents: 718
diff changeset
   305
  symbol->resource_declaration->accept(*this);
6679b6b21214 Partial fix to VAR_EXTERN vs VAR_GLOBAL check. Now considers globals declared in resources too. TODO: recursively check FB declared in the program.
Mario de Sousa <msousa@fe.up.pt>
parents: 718
diff changeset
   306
  current_resource_decl = NULL;  
6679b6b21214 Partial fix to VAR_EXTERN vs VAR_GLOBAL check. Now considers globals declared in resources too. TODO: recursively check FB declared in the program.
Mario de Sousa <msousa@fe.up.pt>
parents: 718
diff changeset
   307
  return NULL;
6679b6b21214 Partial fix to VAR_EXTERN vs VAR_GLOBAL check. Now considers globals declared in resources too. TODO: recursively check FB declared in the program.
Mario de Sousa <msousa@fe.up.pt>
parents: 718
diff changeset
   308
}
6679b6b21214 Partial fix to VAR_EXTERN vs VAR_GLOBAL check. Now considers globals declared in resources too. TODO: recursively check FB declared in the program.
Mario de Sousa <msousa@fe.up.pt>
parents: 718
diff changeset
   309
6679b6b21214 Partial fix to VAR_EXTERN vs VAR_GLOBAL check. Now considers globals declared in resources too. TODO: recursively check FB declared in the program.
Mario de Sousa <msousa@fe.up.pt>
parents: 718
diff changeset
   310
/*  PROGRAM [RETAIN | NON_RETAIN] program_name [WITH task_name] ':' program_type_name ['(' prog_conf_elements ')'] */
660
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   311
void *declaration_check_c::visit(program_configuration_c *symbol) {
971
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 963
diff changeset
   312
  symbol_c *p_decl = NULL;
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 963
diff changeset
   313
  program_type_symtable_t       ::iterator iter_p = program_type_symtable       .find(symbol->program_type_name);
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 963
diff changeset
   314
  function_block_type_symtable_t::iterator iter_f = function_block_type_symtable.find(symbol->program_type_name);
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 963
diff changeset
   315
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 963
diff changeset
   316
  if  (iter_p != program_type_symtable       .end())  p_decl = iter_p->second;
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 963
diff changeset
   317
  if  (iter_f != function_block_type_symtable.end())  p_decl = iter_f->second;
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 963
diff changeset
   318
  
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 963
diff changeset
   319
  if ((iter_f != function_block_type_symtable.end()) && (iter_p != program_type_symtable.end())) 
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 963
diff changeset
   320
    ERROR;  // Should never occur! stage1_2 guarantees that the same identifier cannot be re-used.
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 963
diff changeset
   321
  if ((iter_f == function_block_type_symtable.end()) && (iter_p == program_type_symtable.end())) 
892
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   322
    ERROR;  // Should never occur! stage1_2 guarantees that we are sure to find a declaration in FB or Program symtable.
971
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 963
diff changeset
   323
892
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   324
  check_extern_c check_extern(current_pou_decl, current_resource_decl);
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   325
  p_decl->accept(check_extern);
660
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   326
  return NULL;
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   327
}
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   328