stage3/declaration_check.cc
author Mario de Sousa <msousa@fe.up.pt>
Wed, 19 Sep 2012 11:23:15 +0100
changeset 658 fe5e1f01a49e
parent 656 45a796bce487
child 660 9565d7d944ce
permissions -rw-r--r--
store POU reference to POU declaration instead of global var declarations.
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) {
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    74
  error_count = 0;
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    75
}
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    76
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    77
declaration_check_c::~declaration_check_c(void) {
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
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    81
int declaration_check_c::get_error_count() {
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    82
  return error_count;
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
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    85
void declaration_check_c::check_global_decl(symbol_c *p_decl) {
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    86
	symbol_c *var_name;
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    87
	search_base_type_c search_base_type;
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    88
658
fe5e1f01a49e store POU reference to POU declaration instead of global var declarations.
Mario de Sousa <msousa@fe.up.pt>
parents: 656
diff changeset
    89
	search_var_instance_decl_c search_var_instance_glo_decl(current_pou_decl);
656
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    90
	search_var_instance_decl_c search_var_instance_ext_decl(p_decl);
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    91
	function_param_iterator_c fpi(p_decl);
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    92
	while((var_name = fpi.next()) != NULL) {
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    93
      if (fpi.param_direction() == function_param_iterator_c::direction_extref) {
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    94
     	 /* found an external reference parameter. */
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    95
     	symbol_c *glo_decl = search_var_instance_glo_decl.get_decl(var_name);
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    96
        symbol_c *ext_decl = search_var_instance_ext_decl.get_decl(var_name);
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    97
    	if (search_var_instance_glo_decl.get_option(var_name) != search_var_instance_ext_decl.get_option(var_name))
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    98
          STAGE3_ERROR(0, glo_decl, glo_decl, "Declaration error an external redefinition option.");
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    99
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   100
        /* TODO: Check redefinition data type.
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   101
         *       We need a new class (like search_base_type class) to get type id by variable declaration.
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   102
         *  symbol_c *glo_type = ????;
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   103
         *  symbol_c *ext_type = fpi.param_type();
658
fe5e1f01a49e store POU reference to POU declaration instead of global var declarations.
Mario de Sousa <msousa@fe.up.pt>
parents: 656
diff changeset
   104
	 */
fe5e1f01a49e store POU reference to POU declaration instead of global var declarations.
Mario de Sousa <msousa@fe.up.pt>
parents: 656
diff changeset
   105
	/* For the moment, we will just use search_base_type_c instead... */
fe5e1f01a49e store POU reference to POU declaration instead of global var declarations.
Mario de Sousa <msousa@fe.up.pt>
parents: 656
diff changeset
   106
        symbol_c *glo_type = search_base_type.get_basetype_decl(glo_decl);
fe5e1f01a49e store POU reference to POU declaration instead of global var declarations.
Mario de Sousa <msousa@fe.up.pt>
parents: 656
diff changeset
   107
        symbol_c *ext_type = search_base_type.get_basetype_decl(ext_decl);
fe5e1f01a49e store POU reference to POU declaration instead of global var declarations.
Mario de Sousa <msousa@fe.up.pt>
parents: 656
diff changeset
   108
        if (! is_type_equal(glo_type, ext_type))
fe5e1f01a49e store POU reference to POU declaration instead of global var declarations.
Mario de Sousa <msousa@fe.up.pt>
parents: 656
diff changeset
   109
          STAGE3_ERROR(0, ext_decl, ext_decl, "Declaration error an external redefinition data type.");
656
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   110
      }
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   111
	}
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   112
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   113
}
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   114
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   115
/******************************************/
658
fe5e1f01a49e store POU reference to POU declaration instead of global var declarations.
Mario de Sousa <msousa@fe.up.pt>
parents: 656
diff changeset
   116
/* B 1.5.3 - Declaration & Initialisation */
656
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   117
/******************************************/
658
fe5e1f01a49e store POU reference to POU declaration instead of global var declarations.
Mario de Sousa <msousa@fe.up.pt>
parents: 656
diff changeset
   118
void *declaration_check_c::visit(program_declaration_c *symbol) {
fe5e1f01a49e store POU reference to POU declaration instead of global var declarations.
Mario de Sousa <msousa@fe.up.pt>
parents: 656
diff changeset
   119
  current_pou_decl = symbol;
656
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   120
  return NULL;
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   121
}
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   122
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   123
/********************************/
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   124
/* B 1.7 Configuration elements */
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   125
/********************************/
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   126
void *declaration_check_c::visit(program_configuration_c *symbol) {
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   127
	symbol_c *p_decl = program_type_symtable.find_value(symbol->program_type_name);
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   128
	if (p_decl == program_type_symtable.end_value())
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   129
	  p_decl = function_block_type_symtable.find_value(symbol->program_type_name);
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   130
	/* stage1_2 guarantees that we are sure to find a declaration in FB or Program symtable. */
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   131
	if (p_decl == function_block_type_symtable.end_value())
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   132
      ERROR;
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   133
	check_global_decl(p_decl);
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   134
	return NULL;
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   135
}
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   136