stage3/array_range_check.cc
changeset 560 13b5b7faa3d7
parent 559 a3b8925e640c
child 581 1e158dc9f9c1
equal deleted inserted replaced
559:a3b8925e640c 560:13b5b7faa3d7
       
     1 /*
       
     2  *  matiec - a compiler for the programming languages defined in IEC 61131-3
       
     3  *
       
     4  *  Copyright (C) 2009-2012  Mario de Sousa (msousa@fe.up.pt)
       
     5  *  Copyright (C) 2012       Manuele Conti  (conti.ma@alice.it)
       
     6  *
       
     7  *
       
     8  *  This program is free software: you can redistribute it and/or modify
       
     9  *  it under the terms of the GNU General Public License as published by
       
    10  *  the Free Software Foundation, either version 3 of the License, or
       
    11  *  (at your option) any later version.
       
    12  *
       
    13  *  This program is distributed in the hope that it will be useful,
       
    14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    16  *  GNU General Public License for more details.
       
    17  *
       
    18  *  You should have received a copy of the GNU General Public License
       
    19  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
       
    20  *
       
    21  *
       
    22  * This code is made available on the understanding that it will not be
       
    23  * used in safety-critical situations without a full and competent review.
       
    24  */
       
    25 
       
    26 /*
       
    27  * An IEC 61131-3 compiler.
       
    28  *
       
    29  * Based on the
       
    30  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
       
    31  *
       
    32  */
       
    33 
       
    34 
       
    35 /*
       
    36  * TODO:
       
    37  *   - Check subscript values fall within allowed range.
       
    38  *     For the checking of subscript values to work correctly, we would need to have constant folding working too:
       
    39  *     array_var[8 + 99] can not be checked without constant folding.
       
    40  *     However, even without constant folding range check may be usefull,
       
    41  *     and later changing it to use the values coming out of constant folding should not be very difficult.
       
    42  *
       
    43  */
       
    44 
       
    45 
       
    46 #include "array_range_check.hh"
       
    47 
       
    48 #define FIRST_(symbol1, symbol2) (((symbol1)->first_order < (symbol2)->first_order)   ? (symbol1) : (symbol2))
       
    49 #define  LAST_(symbol1, symbol2) (((symbol1)->last_order  > (symbol2)->last_order)    ? (symbol1) : (symbol2))
       
    50 
       
    51 #define STAGE3_ERROR(error_level, symbol1, symbol2, ...) {                                                                  \
       
    52   if (current_display_error_level >= error_level) {                                                                         \
       
    53     fprintf(stderr, "%s:%d-%d..%d-%d: error: ",                                                                             \
       
    54             FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column,\
       
    55                                                  LAST_(symbol1,symbol2) ->last_line,  LAST_(symbol1,symbol2) ->last_column);\
       
    56     fprintf(stderr, __VA_ARGS__);                                                                                           \
       
    57     fprintf(stderr, "\n");                                                                                                  \
       
    58     error_count++;                                                                                                     \
       
    59   }                                                                                                                         \
       
    60 }
       
    61 
       
    62 
       
    63 #define STAGE3_WARNING(symbol1, symbol2, ...) {                                                                             \
       
    64     fprintf(stderr, "%s:%d-%d..%d-%d: warning: ",                                                                           \
       
    65             FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column,\
       
    66                                                  LAST_(symbol1,symbol2) ->last_line,  LAST_(symbol1,symbol2) ->last_column);\
       
    67     fprintf(stderr, __VA_ARGS__);                                                                                           \
       
    68     fprintf(stderr, "\n");                                                                                                  \
       
    69     warning_found = true;                                                                                                   \
       
    70 }
       
    71 
       
    72 
       
    73 array_range_check_c::array_range_check_c(symbol_c *ignore) {
       
    74 	error_count = 0;
       
    75 	current_display_error_level = 0;
       
    76 }
       
    77 
       
    78 array_range_check_c::~array_range_check_c(void) {
       
    79 }
       
    80 
       
    81 int array_range_check_c::get_error_count() {
       
    82 	return error_count;
       
    83 }
       
    84 
       
    85 void array_range_check_c::check_dimension_count(array_variable_c *symbol) {
       
    86 	int dimension_count;
       
    87 	symbol_c *var_decl;
       
    88 
       
    89 	var_decl = search_varfb_instance_type->get_basetype_decl(symbol->subscripted_variable);
       
    90 	array_dimension_iterator_c array_dimension_iterator(var_decl);
       
    91 	for (dimension_count = 0; NULL != array_dimension_iterator.next(); dimension_count++);
       
    92 	if (dimension_count != ((list_c *)symbol->subscript_list)->n)
       
    93 		STAGE3_ERROR(0, symbol, symbol, "Number of dimensions does not match, Array have %d dimension(s)", dimension_count);
       
    94 }
       
    95 
       
    96 /*********************/
       
    97 /* B 1.4 - Variables */
       
    98 /*********************/
       
    99 /*************************************/
       
   100 /* B 1.4.2 - Multi-element variables */
       
   101 /*************************************/
       
   102 void *array_range_check_c::visit(array_variable_c *symbol) {
       
   103 	check_dimension_count(symbol);
       
   104 	return NULL;
       
   105 }
       
   106 
       
   107 
       
   108 /**************************************/
       
   109 /* B 1.5 - Program organisation units */
       
   110 /**************************************/
       
   111 /***********************/
       
   112 /* B 1.5.1 - Functions */
       
   113 /***********************/
       
   114 void *array_range_check_c::visit(function_declaration_c *symbol) {
       
   115 	search_varfb_instance_type = new search_varfb_instance_type_c(symbol);
       
   116 	// search_var_instance_decl = new search_var_instance_decl_c(symbol);
       
   117 	symbol->function_body->accept(*this);
       
   118 	delete search_varfb_instance_type;
       
   119 	// delete search_var_instance_decl;
       
   120 	search_varfb_instance_type = NULL;
       
   121 	// search_var_instance_decl = NULL;
       
   122 	return NULL;
       
   123 }
       
   124 
       
   125 /*****************************/
       
   126 /* B 1.5.2 - Function blocks */
       
   127 /*****************************/
       
   128 void *array_range_check_c::visit(function_block_declaration_c *symbol) {
       
   129 	search_varfb_instance_type = new search_varfb_instance_type_c(symbol);
       
   130 	// search_var_instance_decl = new search_var_instance_decl_c(symbol);
       
   131 	symbol->fblock_body->accept(*this);
       
   132 	delete search_varfb_instance_type;
       
   133 	// delete search_var_instance_decl;
       
   134 	search_varfb_instance_type = NULL;
       
   135 	// search_var_instance_decl = NULL;
       
   136 	return NULL;
       
   137 }
       
   138 
       
   139 /**********************/
       
   140 /* B 1.5.3 - Programs */
       
   141 /**********************/
       
   142 void *array_range_check_c::visit(program_declaration_c *symbol) {
       
   143 	search_varfb_instance_type = new search_varfb_instance_type_c(symbol);
       
   144 	// search_var_instance_decl = new search_var_instance_decl_c(symbol);
       
   145 	symbol->function_block_body->accept(*this);
       
   146 	delete search_varfb_instance_type;
       
   147 	// delete search_var_instance_decl;
       
   148 	search_varfb_instance_type = NULL;
       
   149 	// search_var_instance_decl = NULL;
       
   150 	return NULL;
       
   151 }
       
   152 
       
   153 
       
   154