stage3/declaration_check.cc
author Andrey Skvortsov <andrej.skvortzov@gmail.com>
Tue, 28 Jun 2016 13:34:29 +0000
branchfix_compilation_warnings
changeset 1009 d7d3a0db7cc2
parent 982 760b26477193
permissions -rw-r--r--
Created new branch fix_compilation_warnings
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
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
    78
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
#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
    81
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
    82
  public:
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
    83
  
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
    84
  private:
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
    85
    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
    86
    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
    87
    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
    88
    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
    89
  
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
  public:
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
    92
    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
    93
    
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
    94
    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
    95
      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
    96
      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
    97
      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
    98
    }
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
    ~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
   101
    
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
    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
   105
      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
   106
      
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   107
      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
   108
      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
   109
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   110
      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
   111
      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
   112
      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
   113
      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
   114
      
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   115
      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
   116
      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
   117
        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
   118
          /* 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
   119
          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
   120
          
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   121
          // 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
   122
          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
   123
              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
   124
          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
   125
            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
   126
            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
   127
          }
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
          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
   130
            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
   131
            continue;
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   132
          }
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
          /* 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
   135
           *        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
   136
           *        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
   137
           */
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   138
          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
   139
          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
   140
          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
   141
            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
   142
    
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   143
          /* 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
   144
           *       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
   145
           *  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
   146
           *  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
   147
           */
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   148
          /* 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
   149
          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
   150
          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
   151
          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
   152
            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
   153
        }
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   154
      }
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
    
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   157
    
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   158
    
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   159
    /********************************/
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   160
    /* 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
   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
    /*  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
   163
    /* 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
   164
    //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
   165
    /* 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
   166
     * 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
   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
     *                      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
   169
     * 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
   170
     * 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
   171
     */
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   172
    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
   173
      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
   174
      /* 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
   175
      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
   176
        ERROR;
971
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 963
diff changeset
   177
      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
   178
      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
   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
    
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   181
    /*****************************/
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   182
    /* 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
   183
    /*****************************/
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   184
    /*  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
   185
    // 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
   186
    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
   187
      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
   188
      /* 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
   189
       * 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
   190
       * 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
   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
      /* 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
   193
      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
   194
        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
   195
      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
   196
    }
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   197
    
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   198
    /******************************************/
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   199
    /* 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
   200
    /******************************************/
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   201
    /*  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
   202
    // 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
   203
    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
   204
      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
   205
      /* 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
   206
       * 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
   207
       * 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
   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
      /* 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
   210
      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
   211
        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
   212
      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
   213
    }
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   214
    
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   215
};
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
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
   218
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
   219
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   220
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
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   226
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
656
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   229
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
   230
  current_display_error_level = 0;
f537c3315f83 Minor changes needed to build with pedantic flag.
Manuele Conti <conti.ma@alice.it>
parents: 660
diff changeset
   231
  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
   232
  current_resource_decl = NULL;
656
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   233
  error_count = 0;
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   234
}
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   235
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   236
declaration_check_c::~declaration_check_c(void) {
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   237
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   238
}
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   239
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   240
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
   241
  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
   242
}
660
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   243
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   244
/*****************************/
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   245
/* 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
   246
/*****************************/
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   247
/*  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
   248
// 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
   249
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
   250
  {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
   251
656
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   252
/******************************************/
658
fe5e1f01a49e store POU reference to POU declaration instead of global var declarations.
Mario de Sousa <msousa@fe.up.pt>
parents: 656
diff changeset
   253
/* B 1.5.3 - Declaration & Initialisation */
656
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   254
/******************************************/
660
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   255
/*  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
   256
// 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
   257
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
   258
  {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
   259
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   260
656
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   261
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   262
/********************************/
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   263
/* B 1.7 Configuration elements */
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   264
/********************************/
660
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   265
/*
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   266
 * 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
   267
 *    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
   268
 *    (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
   269
 *    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
   270
 *    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
   271
 * END_CONFIGURATION
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   272
 */
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   273
//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
   274
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
   275
  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
   276
  /* 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
   277
  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
   278
    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
   279
  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
   280
  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
   281
}
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
   282
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
   283
/*
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
   284
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
   285
   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
   286
   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
   287
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
   288
*/
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
/* 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
   290
// 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
   291
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
   292
  // 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
   293
  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
   294
  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
   295
  // 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
   296
  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
   297
  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
   298
  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
   299
  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
   300
}
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
   301
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
   302
/*  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
   303
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
   304
  symbol_c *p_decl = NULL;
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 963
diff changeset
   305
  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
   306
  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
   307
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 963
diff changeset
   308
  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
   309
  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
   310
  
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 963
diff changeset
   311
  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
   312
    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
   313
  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
   314
    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
   315
892
54d5d185d6e2 Check VAR_EXTERN variables declared in FBs instantiated in a Program (and recursively too - FBs in FBs).
mjsousa
parents: 865
diff changeset
   316
  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
   317
  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
   318
  return NULL;
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   319
}
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   320