Renamed class, remove unused code, delete allocated objects.
authormjsousa <msousa@fe.up.pt>
Tue, 15 May 2012 23:03:25 +0100
changeset 560 13b5b7faa3d7
parent 559 a3b8925e640c
child 561 30a327be60d4
Renamed class, remove unused code, delete allocated objects.
stage3/Makefile.am
stage3/Makefile.in
stage3/array_range_check.cc
stage3/array_range_check.hh
stage3/range_check.cc
stage3/range_check.hh
stage3/stage3.cc
--- a/stage3/Makefile.am	Tue May 15 18:59:32 2012 +0200
+++ b/stage3/Makefile.am	Tue May 15 23:03:25 2012 +0100
@@ -10,5 +10,5 @@
 	print_datatypes_error.cc \
 	datatype_functions.cc \
 	lvalue_check.cc \
-        range_check.cc
+	array_range_check.cc
 
--- a/stage3/Makefile.in	Tue May 15 18:59:32 2012 +0200
+++ b/stage3/Makefile.in	Tue May 15 23:03:25 2012 +0100
@@ -1,4 +1,4 @@
-# Makefile.in generated by automake 1.11 from Makefile.am.
+# Makefile.in generated by automake 1.11.1 from Makefile.am.
 # @configure_input@
 
 # Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
@@ -75,7 +75,7 @@
 	fill_candidate_datatypes.$(OBJEXT) \
 	narrow_candidate_datatypes.$(OBJEXT) \
 	print_datatypes_error.$(OBJEXT) datatype_functions.$(OBJEXT) \
-	lvalue_check.$(OBJEXT)
+	lvalue_check.$(OBJEXT) array_range_check.$(OBJEXT)
 libstage3_a_OBJECTS = $(am_libstage3_a_OBJECTS)
 DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/config
 depcomp = $(SHELL) $(top_srcdir)/config/depcomp
@@ -197,7 +197,8 @@
 	narrow_candidate_datatypes.cc \
 	print_datatypes_error.cc \
 	datatype_functions.cc \
-	lvalue_check.cc
+	lvalue_check.cc \
+	array_range_check.cc
 
 all: all-am
 
@@ -276,6 +277,7 @@
 distclean-compile:
 	-rm -f *.tab.c
 
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/array_range_check.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/datatype_functions.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/fill_candidate_datatypes.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/flow_control_analysis.Po@am__quote@
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stage3/array_range_check.cc	Tue May 15 23:03:25 2012 +0100
@@ -0,0 +1,154 @@
+/*
+ *  matiec - a compiler for the programming languages defined in IEC 61131-3
+ *
+ *  Copyright (C) 2009-2012  Mario de Sousa (msousa@fe.up.pt)
+ *  Copyright (C) 2012       Manuele Conti  (conti.ma@alice.it)
+ *
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ *
+ * This code is made available on the understanding that it will not be
+ * used in safety-critical situations without a full and competent review.
+ */
+
+/*
+ * An IEC 61131-3 compiler.
+ *
+ * Based on the
+ * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
+ *
+ */
+
+
+/*
+ * TODO:
+ *   - Check subscript values fall within allowed range.
+ *     For the checking of subscript values to work correctly, we would need to have constant folding working too:
+ *     array_var[8 + 99] can not be checked without constant folding.
+ *     However, even without constant folding range check may be usefull,
+ *     and later changing it to use the values coming out of constant folding should not be very difficult.
+ *
+ */
+
+
+#include "array_range_check.hh"
+
+#define FIRST_(symbol1, symbol2) (((symbol1)->first_order < (symbol2)->first_order)   ? (symbol1) : (symbol2))
+#define  LAST_(symbol1, symbol2) (((symbol1)->last_order  > (symbol2)->last_order)    ? (symbol1) : (symbol2))
+
+#define STAGE3_ERROR(error_level, symbol1, symbol2, ...) {                                                                  \
+  if (current_display_error_level >= error_level) {                                                                         \
+    fprintf(stderr, "%s:%d-%d..%d-%d: error: ",                                                                             \
+            FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column,\
+                                                 LAST_(symbol1,symbol2) ->last_line,  LAST_(symbol1,symbol2) ->last_column);\
+    fprintf(stderr, __VA_ARGS__);                                                                                           \
+    fprintf(stderr, "\n");                                                                                                  \
+    error_count++;                                                                                                     \
+  }                                                                                                                         \
+}
+
+
+#define STAGE3_WARNING(symbol1, symbol2, ...) {                                                                             \
+    fprintf(stderr, "%s:%d-%d..%d-%d: warning: ",                                                                           \
+            FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column,\
+                                                 LAST_(symbol1,symbol2) ->last_line,  LAST_(symbol1,symbol2) ->last_column);\
+    fprintf(stderr, __VA_ARGS__);                                                                                           \
+    fprintf(stderr, "\n");                                                                                                  \
+    warning_found = true;                                                                                                   \
+}
+
+
+array_range_check_c::array_range_check_c(symbol_c *ignore) {
+	error_count = 0;
+	current_display_error_level = 0;
+}
+
+array_range_check_c::~array_range_check_c(void) {
+}
+
+int array_range_check_c::get_error_count() {
+	return error_count;
+}
+
+void array_range_check_c::check_dimension_count(array_variable_c *symbol) {
+	int dimension_count;
+	symbol_c *var_decl;
+
+	var_decl = search_varfb_instance_type->get_basetype_decl(symbol->subscripted_variable);
+	array_dimension_iterator_c array_dimension_iterator(var_decl);
+	for (dimension_count = 0; NULL != array_dimension_iterator.next(); dimension_count++);
+	if (dimension_count != ((list_c *)symbol->subscript_list)->n)
+		STAGE3_ERROR(0, symbol, symbol, "Number of dimensions does not match, Array have %d dimension(s)", dimension_count);
+}
+
+/*********************/
+/* B 1.4 - Variables */
+/*********************/
+/*************************************/
+/* B 1.4.2 - Multi-element variables */
+/*************************************/
+void *array_range_check_c::visit(array_variable_c *symbol) {
+	check_dimension_count(symbol);
+	return NULL;
+}
+
+
+/**************************************/
+/* B 1.5 - Program organisation units */
+/**************************************/
+/***********************/
+/* B 1.5.1 - Functions */
+/***********************/
+void *array_range_check_c::visit(function_declaration_c *symbol) {
+	search_varfb_instance_type = new search_varfb_instance_type_c(symbol);
+	// search_var_instance_decl = new search_var_instance_decl_c(symbol);
+	symbol->function_body->accept(*this);
+	delete search_varfb_instance_type;
+	// delete search_var_instance_decl;
+	search_varfb_instance_type = NULL;
+	// search_var_instance_decl = NULL;
+	return NULL;
+}
+
+/*****************************/
+/* B 1.5.2 - Function blocks */
+/*****************************/
+void *array_range_check_c::visit(function_block_declaration_c *symbol) {
+	search_varfb_instance_type = new search_varfb_instance_type_c(symbol);
+	// search_var_instance_decl = new search_var_instance_decl_c(symbol);
+	symbol->fblock_body->accept(*this);
+	delete search_varfb_instance_type;
+	// delete search_var_instance_decl;
+	search_varfb_instance_type = NULL;
+	// search_var_instance_decl = NULL;
+	return NULL;
+}
+
+/**********************/
+/* B 1.5.3 - Programs */
+/**********************/
+void *array_range_check_c::visit(program_declaration_c *symbol) {
+	search_varfb_instance_type = new search_varfb_instance_type_c(symbol);
+	// search_var_instance_decl = new search_var_instance_decl_c(symbol);
+	symbol->function_block_body->accept(*this);
+	delete search_varfb_instance_type;
+	// delete search_var_instance_decl;
+	search_varfb_instance_type = NULL;
+	// search_var_instance_decl = NULL;
+	return NULL;
+}
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stage3/array_range_check.hh	Tue May 15 23:03:25 2012 +0100
@@ -0,0 +1,90 @@
+/*
+ *  matiec - a compiler for the programming languages defined in IEC 61131-3
+ *
+ *  Copyright (C) 2009-2012  Mario de Sousa (msousa@fe.up.pt)
+ *  Copyright (C) 2012       Manuele Conti  (conti.ma@alice.it)
+ *
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ *
+ * This code is made available on the understanding that it will not be
+ * used in safety-critical situations without a full and competent review.
+ */
+
+/*
+ * An IEC 61131-3 compiler.
+ *
+ * Based on the
+ * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
+ *
+ */
+
+// #include <vector>
+#include "../absyntax_utils/absyntax_utils.hh"
+// #include "datatype_functions.hh"
+
+
+
+
+class array_range_check_c: public iterator_visitor_c {
+
+  private:
+    search_varfb_instance_type_c *search_varfb_instance_type;
+    // search_var_instance_decl_c *search_var_instance_decl;
+    search_base_type_c search_base_type;
+    int error_count;
+    int current_display_error_level;
+
+    void check_dimension_count(array_variable_c *symbol);
+
+  public:
+    array_range_check_c(symbol_c *ignore);
+    virtual ~array_range_check_c(void);
+    int get_error_count();
+
+    /*********************/
+    /* B 1.4 - Variables */
+    /*********************/
+    /*************************************/
+    /* B 1.4.2 - Multi-element variables */
+    /*************************************/
+    void *visit(array_variable_c *symbol);
+
+    /**************************************/
+    /* B 1.5 - Program organisation units */
+    /**************************************/
+    /***********************/
+    /* B 1.5.1 - Functions */
+    /***********************/
+    void *visit(function_declaration_c *symbol);
+
+    /*****************************/
+    /* B 1.5.2 - Function blocks */
+    /*****************************/
+    void *visit(function_block_declaration_c *symbol);
+
+    /**********************/
+    /* B 1.5.3 - Programs */
+    /**********************/
+    void *visit(program_declaration_c *symbol);
+
+}; /* array_range_check_c */
+
+
+
+
+
+
+
--- a/stage3/range_check.cc	Tue May 15 18:59:32 2012 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,175 +0,0 @@
-/*
- *  matiec - a compiler for the programming languages defined in IEC 61131-3
- *
- *  Copyright (C) 2009-2012  Mario de Sousa (msousa@fe.up.pt)
- *  Copyright (C) 2012       Manuele Conti  (conti.ma@alice.it)
- *
- *
- *  This program is free software: you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation, either version 3 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
- *
- *
- * This code is made available on the understanding that it will not be
- * used in safety-critical situations without a full and competent review.
- */
-
-/*
- * An IEC 61131-3 compiler.
- *
- * Based on the
- * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
- *
- */
-
-
-/*
- * TODO:
- *   - Check subscript values fall within allowed range.
- *     For the checking of subscript values to work correctly, we would need to have constant folding working too:
- *     array_var[8 + 99] can not be checked without constant folding.
- *     However, even without constant folding range check may be usefull,
- *     and later changing it to use the values coming out of constant folding should not be very difficult.
- *
- */
-
-
-#include "range_check.hh"
-
-#define FIRST_(symbol1, symbol2) (((symbol1)->first_order < (symbol2)->first_order)   ? (symbol1) : (symbol2))
-#define  LAST_(symbol1, symbol2) (((symbol1)->last_order  > (symbol2)->last_order)    ? (symbol1) : (symbol2))
-
-#define STAGE3_ERROR(error_level, symbol1, symbol2, ...) {                                                                  \
-  if (current_display_error_level >= error_level) {                                                                         \
-    fprintf(stderr, "%s:%d-%d..%d-%d: error: ",                                                                             \
-            FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column,\
-                                                 LAST_(symbol1,symbol2) ->last_line,  LAST_(symbol1,symbol2) ->last_column);\
-    fprintf(stderr, __VA_ARGS__);                                                                                           \
-    fprintf(stderr, "\n");                                                                                                  \
-    error_count++;                                                                                                     \
-  }                                                                                                                         \
-}
-
-
-#define STAGE3_WARNING(symbol1, symbol2, ...) {                                                                             \
-    fprintf(stderr, "%s:%d-%d..%d-%d: warning: ",                                                                           \
-            FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column,\
-                                                 LAST_(symbol1,symbol2) ->last_line,  LAST_(symbol1,symbol2) ->last_column);\
-    fprintf(stderr, __VA_ARGS__);                                                                                           \
-    fprintf(stderr, "\n");                                                                                                  \
-    warning_found = true;                                                                                                   \
-}
-
-
-range_check_c::range_check_c(symbol_c *ignore) {
-	error_count = 0;
-	current_display_error_level = 0;
-}
-
-range_check_c::~range_check_c(void) {
-}
-
-int range_check_c::get_error_count() {
-	return error_count;
-}
-
-void range_check_c::check_range_array_check(array_variable_c *symbol) {
-	int dimension_count;
-	symbol_c* dimension;
-	var_declarations_c *var_decl;
-	array_dimension_iterator_c* array_dimension_iterator;
-
-	var_decl = (var_declarations_c *)search_varfb_instance_type->get_basetype_decl(symbol->subscripted_variable);
-	array_dimension_iterator = new array_dimension_iterator_c(var_decl);
-	for (dimension_count = 0; NULL != array_dimension_iterator->next(); dimension_count++);
-	if (dimension_count != ((list_c *)symbol->subscript_list)->n)
-		STAGE3_ERROR(0, symbol, symbol, "Number of dimensions does not match, Array have %d dimension(s)", dimension_count);
-}
-
-/*********************/
-/* B 1.4 - Variables */
-/*********************/
-/*************************************/
-/* B 1.4.2 - Multi-element variables */
-/*************************************/
-void *range_check_c::visit(array_variable_c *symbol) {
-	check_range_array_check(symbol);
-	return NULL;
-}
-
-
-/**************************************/
-/* B 1.5 - Program organisation units */
-/**************************************/
-/***********************/
-/* B 1.5.1 - Functions */
-/***********************/
-void *range_check_c::visit(function_declaration_c *symbol) {
-	search_varfb_instance_type = new search_varfb_instance_type_c(symbol);
-	search_var_instance_decl = new search_var_instance_decl_c(symbol);
-	symbol->function_body->accept(*this);
-	delete search_varfb_instance_type;
-	delete search_var_instance_decl;
-	search_varfb_instance_type = NULL;
-	search_var_instance_decl = NULL;
-	return NULL;
-}
-
-/*****************************/
-/* B 1.5.2 - Function blocks */
-/*****************************/
-void *range_check_c::visit(function_block_declaration_c *symbol) {
-	search_varfb_instance_type = new search_varfb_instance_type_c(symbol);
-	search_var_instance_decl = new search_var_instance_decl_c(symbol);
-	symbol->fblock_body->accept(*this);
-	delete search_varfb_instance_type;
-	delete search_var_instance_decl;
-	search_varfb_instance_type = NULL;
-	search_var_instance_decl = NULL;
-	return NULL;
-}
-
-/**********************/
-/* B 1.5.3 - Programs */
-/**********************/
-void *range_check_c::visit(program_declaration_c *symbol) {
-	search_varfb_instance_type = new search_varfb_instance_type_c(symbol);
-	search_var_instance_decl = new search_var_instance_decl_c(symbol);
-	symbol->function_block_body->accept(*this);
-	delete search_varfb_instance_type;
-	delete search_var_instance_decl;
-	search_varfb_instance_type = NULL;
-	search_var_instance_decl = NULL;
-	return NULL;
-}
-
-
-
-/***************************************/
-/* B.3 - Language ST (Structured Text) */
-/***************************************/
-/*********************************/
-/* B 3.2.1 Assignment Statements */
-/*********************************/
-void *range_check_c::visit(assignment_statement_c *symbol) {
-	symbol->l_exp->accept(*this);
-    symbol->r_exp->accept(*this);
-	return NULL;
-}
-
-
-
-
-
-
-
-
--- a/stage3/range_check.hh	Tue May 15 18:59:32 2012 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,103 +0,0 @@
-/*
- *  matiec - a compiler for the programming languages defined in IEC 61131-3
- *
- *  Copyright (C) 2009-2012  Mario de Sousa (msousa@fe.up.pt)
- *  Copyright (C) 2012       Manuele Conti  (conti.ma@alice.it)
- *
- *
- *  This program is free software: you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation, either version 3 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
- *
- *
- * This code is made available on the understanding that it will not be
- * used in safety-critical situations without a full and competent review.
- */
-
-/*
- * An IEC 61131-3 compiler.
- *
- * Based on the
- * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
- *
- */
-
-#include <vector>
-#include "../absyntax_utils/absyntax_utils.hh"
-#include "datatype_functions.hh"
-
-
-
-
-class range_check_c: public iterator_visitor_c {
-
-  private:
-    search_varfb_instance_type_c *search_varfb_instance_type;
-    search_var_instance_decl_c *search_var_instance_decl;
-    search_base_type_c search_base_type;
-    int error_count;
-    int current_display_error_level;
-
-    void check_range_array_check(array_variable_c *symbol);
-
-  public:
-    range_check_c(symbol_c *ignore);
-    virtual ~range_check_c(void);
-    int get_error_count();
-
-    /*********************/
-    /* B 1.4 - Variables */
-    /*********************/
-    /*************************************/
-    /* B 1.4.2 - Multi-element variables */
-    /*************************************/
-    void *visit(array_variable_c *symbol);
-
-    /**************************************/
-    /* B 1.5 - Program organisation units */
-    /**************************************/
-    /***********************/
-    /* B 1.5.1 - Functions */
-    /***********************/
-    void *visit(function_declaration_c *symbol);
-
-    /*****************************/
-    /* B 1.5.2 - Function blocks */
-    /*****************************/
-    void *visit(function_block_declaration_c *symbol);
-
-    /**********************/
-    /* B 1.5.3 - Programs */
-    /**********************/
-    void *visit(program_declaration_c *symbol);
-
-
-    /***************************************/
-    /* B.3 - Language ST (Structured Text) */
-    /***************************************/
-
-    /*********************************/
-    /* B 3.2.1 Assignment Statements */
-    /*********************************/
-    void *visit(assignment_statement_c *symbol);
-
-
-
-
-}; /* range_check_c */
-
-
-
-
-
-
-
--- a/stage3/stage3.cc	Tue May 15 18:59:32 2012 +0200
+++ b/stage3/stage3.cc	Tue May 15 23:03:25 2012 +0100
@@ -39,7 +39,7 @@
 #include "narrow_candidate_datatypes.hh"
 #include "print_datatypes_error.hh"
 #include "lvalue_check.hh"
-#include "range_check.hh"
+#include "array_range_check.hh"
 
 
 static int flow_control_analysis(symbol_c *tree_root){
@@ -73,9 +73,9 @@
 }
 
 static int range_check(symbol_c *tree_root){
-	range_check_c range_check(tree_root);
-	tree_root->accept(range_check);
-	return range_check.get_error_count();
+	array_range_check_c array_range_check(tree_root);
+	tree_root->accept(array_range_check);
+	return array_range_check.get_error_count();
 }