stage3/declaration_check.cc
author mjsousa
Sun, 16 Feb 2014 14:27:06 +0000
changeset 868 23f271534a7c
parent 865 7365c3e5c9ae
child 892 54d5d185d6e2
permissions -rw-r--r--
Fix the state machine that became broken 2 commits ago (when adding support for nested comments)
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
      
865
7365c3e5c9ae Fix bug: when checking compatibility between GLOBAL and EXTERNAL variables, must only enforce GLOBAL CONSTANT => EXTERNAL CONSTANT.
mjsousa
parents: 812
diff changeset
   115
      /* Check whether variable's constness (CONSTANT) is compatible.
7365c3e5c9ae Fix bug: when checking compatibility between GLOBAL and EXTERNAL variables, must only enforce GLOBAL CONSTANT => EXTERNAL CONSTANT.
mjsousa
parents: 812
diff changeset
   116
       *        VAR_GLOBAL is contant     => VAR_EXTERNAL must also be CONSTANT
7365c3e5c9ae Fix bug: when checking compatibility between GLOBAL and EXTERNAL variables, must only enforce GLOBAL CONSTANT => EXTERNAL CONSTANT.
mjsousa
parents: 812
diff changeset
   117
       *        VAR_GLOBAL is not contant => VAR_EXTERNAL may be CONSTANT, or not!
7365c3e5c9ae Fix bug: when checking compatibility between GLOBAL and EXTERNAL variables, must only enforce GLOBAL CONSTANT => EXTERNAL CONSTANT.
mjsousa
parents: 812
diff changeset
   118
       */
7365c3e5c9ae Fix bug: when checking compatibility between GLOBAL and EXTERNAL variables, must only enforce GLOBAL CONSTANT => EXTERNAL CONSTANT.
mjsousa
parents: 812
diff changeset
   119
      search_var_instance_decl_c::opt_t ext_opt = search_var_instance_ext_decl. get_option(var_name);
7365c3e5c9ae Fix bug: when checking compatibility between GLOBAL and EXTERNAL variables, must only enforce GLOBAL CONSTANT => EXTERNAL CONSTANT.
mjsousa
parents: 812
diff changeset
   120
      search_var_instance_decl_c::opt_t glo_opt = search_var_instance_glo_decl->get_option(var_name);
7365c3e5c9ae Fix bug: when checking compatibility between GLOBAL and EXTERNAL variables, must only enforce GLOBAL CONSTANT => EXTERNAL CONSTANT.
mjsousa
parents: 812
diff changeset
   121
      if ((glo_opt == search_var_instance_decl_c::constant_opt) && (ext_opt != search_var_instance_decl_c::constant_opt))
7365c3e5c9ae Fix bug: when checking compatibility between GLOBAL and EXTERNAL variables, must only enforce GLOBAL CONSTANT => EXTERNAL CONSTANT.
mjsousa
parents: 812
diff changeset
   122
        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.");
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
   123
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
      /* 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
   125
       *       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
   126
       *  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
   127
       *  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
   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
      /* 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
   130
      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
   131
      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
   132
      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
   133
        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
   134
    }
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
   135
  }
656
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   136
}
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   137
660
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   138
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   139
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   140
/*****************************/
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   141
/* 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
   142
/*****************************/
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   143
/*  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
   144
// 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
   145
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
   146
  /* 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
   147
   * 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
   148
   * 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
   149
   */
660
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   150
  /* 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
   151
  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
   152
    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
   153
  return NULL;
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   154
}
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   155
656
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   156
/******************************************/
658
fe5e1f01a49e store POU reference to POU declaration instead of global var declarations.
Mario de Sousa <msousa@fe.up.pt>
parents: 656
diff changeset
   157
/* B 1.5.3 - Declaration & Initialisation */
656
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   158
/******************************************/
660
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   159
/*  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
   160
// 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
   161
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
   162
  /* 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
   163
   * 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
   164
   * 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
   165
   */
660
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   166
  /* 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
   167
  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
   168
    symbol->var_declarations->accept(*this);
656
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   169
  return NULL;
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   170
}
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   171
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   172
/********************************/
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   173
/* B 1.7 Configuration elements */
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   174
/********************************/
660
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   175
/*
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   176
 * 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
   177
 *    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
   178
 *    (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
   179
 *    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
   180
 *    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
   181
 * END_CONFIGURATION
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   182
 */
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   183
//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
   184
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
   185
  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
   186
  /* 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
   187
  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
   188
    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
   189
  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
   190
  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
   191
}
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
/*
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
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
   195
   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
   196
   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
   197
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
   198
*/
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
/* 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
   200
// 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
   201
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
   202
  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
   203
  /* 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
   204
  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
   205
  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
   206
  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
   207
}
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
   208
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
   209
/*  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
   210
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
   211
  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
   212
  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
   213
    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
   214
  /* 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
   215
  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
   216
    ERROR;
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   217
  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
   218
  return NULL;
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   219
}
9565d7d944ce Also check external/global variable consistency in configurations and FBs.
Mario de Sousa <msousa@fe.up.pt>
parents: 658
diff changeset
   220