stage3/declaration_check.cc
author Edouard Tisserant
Fri, 05 Apr 2013 09:37:52 +0900
changeset 815 4cfca12bf850
parent 812 6679b6b21214
child 865 7365c3e5c9ae
permissions -rw-r--r--
Merged Mario's changes
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
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    73
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
    74
  current_display_error_level = 0;
f537c3315f83 Minor changes needed to build with pedantic flag.
Manuele Conti <conti.ma@alice.it>
parents: 660
diff changeset
    75
  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
    76
  current_resource_decl = NULL;
656
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    77
  error_count = 0;
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    78
}
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    79
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    80
declaration_check_c::~declaration_check_c(void) {
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    81
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    82
}
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    83
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    84
int declaration_check_c::get_error_count() {
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    85
  return error_count;
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    86
}
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    87
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    88
void declaration_check_c::check_global_decl(symbol_c *p_decl) {
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
    89
  if (NULL == current_pou_decl) ERROR;
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
    90
  
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
    91
  search_var_instance_decl_c search_var_instance_pou_glo_decl(current_pou_decl);
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
    92
  search_var_instance_decl_c search_var_instance_res_glo_decl(current_resource_decl);
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
    93
  search_var_instance_decl_c search_var_instance_ext_decl(p_decl);
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
    94
  function_param_iterator_c  fpi(p_decl);
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
    95
  
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
    96
  symbol_c *var_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
    97
  while((var_name = fpi.next()) != 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
    98
    if (fpi.param_direction() == function_param_iterator_c::direction_extref) {
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
    99
      /* found an external reference parameter. */
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
   100
      symbol_c *ext_decl = search_var_instance_ext_decl.get_decl(var_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
   101
      
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
   102
      // NOTE: Must check the POU first, and RESOURCE second!
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
   103
      symbol_c                                       *glo_decl =  search_var_instance_res_glo_decl.get_decl(var_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
   104
      search_var_instance_decl_c *search_var_instance_glo_decl = &search_var_instance_res_glo_decl;
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
   105
      if (NULL == glo_decl) {
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
   106
        glo_decl                     =  search_var_instance_pou_glo_decl.get_decl(var_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
   107
        search_var_instance_glo_decl = &search_var_instance_pou_glo_decl;
656
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   108
      }
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
   109
      
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
   110
      if (NULL == glo_decl) {
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
   111
        STAGE3_ERROR(0, ext_decl, ext_decl, "Declaration error. The external variable does not match with any global variable.");
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
   112
        continue;
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
   113
      }
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
   114
      
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
   115
      if (search_var_instance_glo_decl->get_option(var_name) != search_var_instance_ext_decl.get_option(var_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
   116
        STAGE3_ERROR(0, glo_decl, glo_decl, "Declaration error.  The external variable options do not match with thos of the global variable.");
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
   117
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
   118
      /* TODO: Check redefinition data type.
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
   119
       *       We need a new class (like search_base_type class) to get type id by variable 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
   120
       *  symbol_c *glo_type = ????;
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
   121
       *  symbol_c *ext_type = fpi.param_type();
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
   122
       */
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
   123
      /* For the moment, we will just use search_base_type_c instead... */
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
   124
      symbol_c *glo_type = search_base_type_c::get_basetype_decl(glo_decl);
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
   125
      symbol_c *ext_type = search_base_type_c::get_basetype_decl(ext_decl);
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
   126
      if (! get_datatype_info_c::is_type_equal(glo_type, ext_type))
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
   127
        STAGE3_ERROR(0, ext_decl, ext_decl, "Declaration error.  Data type mismatch between external and global variable 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
   128
    }
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
   129
  }
656
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   130
}
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   131
660
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   132
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   133
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   134
/*****************************/
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   135
/* 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
   136
/*****************************/
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   137
/*  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
   138
// SYM_REF3(function_block_declaration_c, fblock_name, var_declarations, fblock_body)
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   139
void *declaration_check_c::visit(function_block_declaration_c *symbol) {
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
   140
  /* The following two lines of code are only valid for v3 of IEC 61131-3, that allows VAR_GLOBAL declarations inside FBs!
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
   141
   * current_pou_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
   142
   * 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
   143
   */
660
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   144
  /* 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
   145
  if (NULL != symbol->var_declarations)
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   146
    symbol->var_declarations->accept(*this);
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   147
  return NULL;
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   148
}
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   149
656
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   150
/******************************************/
658
fe5e1f01a49e store POU reference to POU declaration instead of global var declarations.
Mario de Sousa <msousa@fe.up.pt>
parents: 656
diff changeset
   151
/* B 1.5.3 - Declaration & Initialisation */
656
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   152
/******************************************/
660
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   153
/*  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
   154
// SYM_REF3(program_declaration_c, program_type_name, var_declarations, function_block_body)
658
fe5e1f01a49e store POU reference to POU declaration instead of global var declarations.
Mario de Sousa <msousa@fe.up.pt>
parents: 656
diff changeset
   155
void *declaration_check_c::visit(program_declaration_c *symbol) {
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
   156
  /* The following two lines of code are only valid for v3 of IEC 61131-3, that allows VAR_GLOBAL declarations inside PROGRAMs!
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
   157
   * current_pou_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
   158
   * 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
   159
   */
660
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   160
  /* 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
   161
  if (NULL != symbol->var_declarations)
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   162
    symbol->var_declarations->accept(*this);
656
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   163
  return NULL;
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   164
}
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   165
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   166
/********************************/
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   167
/* B 1.7 Configuration elements */
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   168
/********************************/
660
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   169
/*
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   170
 * 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
   171
 *    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
   172
 *    (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
   173
 *    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
   174
 *    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
   175
 * END_CONFIGURATION
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   176
 */
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   177
//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
   178
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
   179
  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
   180
  /* 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
   181
  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
   182
    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
   183
  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
   184
  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
   185
}
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
   186
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
   187
/*
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
   188
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
   189
   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
   190
   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
   191
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
   192
*/
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
   193
/* 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
   194
// 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
   195
void *declaration_check_c::visit(resource_declaration_c *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
   196
  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
   197
  /* check if any FB declared as a VAR has any incompatible VAR_EXTERNAL 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
   198
  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
   199
  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
   200
  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
   201
}
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
   202
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
   203
/*  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
   204
void *declaration_check_c::visit(program_configuration_c *symbol) {
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   205
  symbol_c *p_decl = program_type_symtable.find_value(symbol->program_type_name);
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   206
  if (p_decl == program_type_symtable.end_value())
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   207
    p_decl = function_block_type_symtable.find_value(symbol->program_type_name);
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   208
  /* stage1_2 guarantees that we are sure to find a declaration in FB or Program symtable. */
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   209
  if (p_decl == function_block_type_symtable.end_value())
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   210
    ERROR;
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   211
  check_global_decl(p_decl);
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   212
  return NULL;
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   213
}
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   214