Cleaning up: Getting some sense into the code handling structures.
authorMario de Sousa <msousa@fe.up.pt>
Wed, 31 Aug 2011 14:10:53 +0100
changeset 371 926490780952
parent 370 f3cec8694f65
child 372 25332e048742
Cleaning up: Getting some sense into the code handling structures.
absyntax_utils/search_varfb_instance_type.cc
absyntax_utils/search_varfb_instance_type.hh
configure
--- a/absyntax_utils/search_varfb_instance_type.cc	Thu Aug 25 17:22:44 2011 +0200
+++ b/absyntax_utils/search_varfb_instance_type.cc	Wed Aug 31 14:10:53 2011 +0100
@@ -94,12 +94,12 @@
 search_varfb_instance_type_c::search_varfb_instance_type_c(symbol_c *search_scope): search_var_instance_decl(search_scope) {
   this->decompose_var_instance_name = NULL;
   this->current_structelement_name = NULL;
-  this->current_rawtype = NULL;
+  this->current_typeid = NULL;
 }
 
 symbol_c *search_varfb_instance_type_c::get_basetype_decl(symbol_c *variable_name) {
   this->current_structelement_name = NULL;
-  this->current_rawtype = NULL;
+  this->current_typeid = NULL;
   this->decompose_var_instance_name = new decompose_var_instance_name_c(variable_name);
   if (NULL == decompose_var_instance_name) ERROR;
 
@@ -146,7 +146,7 @@
 
 unsigned int search_varfb_instance_type_c::get_vartype(symbol_c *variable_name) {
   this->current_structelement_name = NULL;
-  this->current_rawtype = NULL;
+  this->current_typeid = NULL;
   this->is_complex = false;
   this->decompose_var_instance_name = new decompose_var_instance_name_c(variable_name);
   if (NULL == decompose_var_instance_name) ERROR;
@@ -182,12 +182,12 @@
 }
 
 symbol_c *search_varfb_instance_type_c::get_type_id(symbol_c *variable_name) {
-  this->current_rawtype = NULL;
-  symbol_c *rawtype = this->get_basetype_decl(variable_name);
-  if (this->current_rawtype != NULL)
-    return this->current_rawtype;
+  this->current_typeid = NULL;
+  symbol_c *vartype = this->get_basetype_decl(variable_name);
+  if (this->current_typeid != NULL)
+    return this->current_typeid;
   else
-	return rawtype;
+    return vartype;
 }
 
 bool search_varfb_instance_type_c::type_is_complex(void) {
@@ -219,23 +219,18 @@
  * of a function block type...
  */
 void *search_varfb_instance_type_c::visit(identifier_c *type_name) {
+  this->current_typeid = type_name;
+
   /* look up the type declaration... */
   symbol_c *fb_decl = function_block_type_symtable.find_value(type_name);
   if (fb_decl != function_block_type_symtable.end_value())
     /* Type declaration found!! */
     return fb_decl->accept(*this);
 
-  this->current_rawtype = type_name;
-
   /* No. It is not a function block, so we let
    * the base class take care of it...
    */
-  if (NULL == decompose_var_instance_name->next_part(false)) {
-    return base_type(type_name);
-  }
-  else {
-	return search_base_type_c::visit(type_name);
-  }
+  return search_base_type_c::visit(type_name);
 }
 
 /********************************/
@@ -262,8 +257,13 @@
 }
 
 /*  structure_type_name ':' structure_specification */
+/* NOTE: this is only used inside a TYPE ... END_TYPE declaration. 
+ * It is never used directly when declaring a new variable! 
+ */
 void *search_varfb_instance_type_c::visit(structure_type_declaration_c *symbol) {
   this->is_complex = true;
+
+  if (NULL == current_structelement_name) ERROR;
   return symbol->structure_specification->accept(*this);
   /* NOTE: structure_specification will point to either a
    *       initialized_structure_c
@@ -275,52 +275,12 @@
 /* structure_type_name ASSIGN structure_initialization */
 /* structure_initialization may be NULL ! */
 // SYM_REF2(initialized_structure_c, structure_type_name, structure_initialization)
+/* NOTE: only the initialized structure is ever used when declaring a new variable instance */
 void *search_varfb_instance_type_c::visit(initialized_structure_c *symbol)	{
   this->is_complex = true;
-  /* recursively find out the data type of var_name... */
-  return symbol->structure_type_name->accept(*this);
-}
-
-/* helper symbol for structure_declaration */
-/* structure_declaration:  STRUCT structure_element_declaration_list END_STRUCT */
-/* structure_element_declaration_list structure_element_declaration ';' */
-void *search_varfb_instance_type_c::visit(structure_element_declaration_list_c *symbol)	{
-  /* make sure that we have decomposed all structure elements of the variable name */
-  current_structelement_name = decompose_var_instance_name->next_part();
-  /* now search the structure declaration */
-  return visit_list(symbol);
-}
-
-/*  structure_element_name ':' spec_init */
-void *search_varfb_instance_type_c::visit(structure_element_declaration_c *symbol) {
-  if (NULL == current_structelement_name) ERROR;
-
-  if (compare_identifiers(symbol->structure_element_name, current_structelement_name) == 0)
-    return symbol->spec_init->accept(*this);
-
-  return NULL;
-}
-
-/* helper symbol for structure_initialization */
-/* structure_initialization: '(' structure_element_initialization_list ')' */
-/* structure_element_initialization_list ',' structure_element_initialization */
-void *search_varfb_instance_type_c::visit(structure_element_initialization_list_c *symbol) {ERROR; return NULL;} /* should never get called... */
-/*  structure_element_name ASSIGN value */
-void *search_varfb_instance_type_c::visit(structure_element_initialization_c *symbol) {ERROR; return NULL;} /* should never get called... */
-
-
-
-/**************************************/
-/* B.1.5 - Program organization units */
-/**************************************/
-/*****************************/
-/* B 1.5.2 - Function Blocks */
-/*****************************/
-/*  FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */
-// SYM_REF4(function_block_declaration_c, fblock_name, var_declarations, fblock_body, unused)
-void *search_varfb_instance_type_c::visit(function_block_declaration_c *symbol) {
+  if (NULL != current_structelement_name) ERROR;
+  
   /* make sure that we have decomposed all strcuture elements of the variable name */
-
   symbol_c *var_name = decompose_var_instance_name->next_part();
   if (NULL == var_name) {
     /* this is it... !
@@ -333,6 +293,68 @@
     return (void *)symbol;
    }
 
+  /* look for the var_name in the structure declaration */
+  current_structelement_name = var_name;
+
+  /* recursively find out the data type of current_structelement_name... */
+  return symbol->structure_type_name->accept(*this);
+}
+
+/* helper symbol for structure_declaration */
+/* structure_declaration:  STRUCT structure_element_declaration_list END_STRUCT */
+/* structure_element_declaration_list structure_element_declaration ';' */
+void *search_varfb_instance_type_c::visit(structure_element_declaration_list_c *symbol)	{
+  if (NULL == current_structelement_name) ERROR;
+  /* now search the structure declaration */
+  return visit_list(symbol);
+}
+
+/*  structure_element_name ':' spec_init */
+void *search_varfb_instance_type_c::visit(structure_element_declaration_c *symbol) {
+  if (NULL == current_structelement_name) ERROR;
+
+  if (compare_identifiers(symbol->structure_element_name, current_structelement_name) == 0) {
+    current_structelement_name = NULL;
+    /* found the type of the element we were looking for! */
+    return symbol->spec_init->accept(*this);
+  }  
+
+  /* Did not find the type of the element we were looking for! */
+  /* Will keep looking... */
+  return NULL;
+}
+
+/* helper symbol for structure_initialization */
+/* structure_initialization: '(' structure_element_initialization_list ')' */
+/* structure_element_initialization_list ',' structure_element_initialization */
+void *search_varfb_instance_type_c::visit(structure_element_initialization_list_c *symbol) {ERROR; return NULL;} /* should never get called... */
+/*  structure_element_name ASSIGN value */
+void *search_varfb_instance_type_c::visit(structure_element_initialization_c *symbol) {ERROR; return NULL;} /* should never get called... */
+
+
+
+/**************************************/
+/* B.1.5 - Program organization units */
+/**************************************/
+/*****************************/
+/* B 1.5.2 - Function Blocks */
+/*****************************/
+/*  FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */
+// SYM_REF4(function_block_declaration_c, fblock_name, var_declarations, fblock_body, unused)
+void *search_varfb_instance_type_c::visit(function_block_declaration_c *symbol) {
+  /* make sure that we have decomposed all strcuture elements of the variable name */
+  symbol_c *var_name = decompose_var_instance_name->next_part();
+  if (NULL == var_name) {
+    /* this is it... !
+     * No need to look any further...
+     * Note also that, unlike for the struct types, a function block may
+     * not be defined based on another (i.e. no inheritance is allowed),
+     * so this function block is already the most base type.
+     * We simply return it.
+     */
+    return (void *)symbol;
+   }
+
    /* now search the function block declaration for the variable... */
    search_var_instance_decl_c search_decl(symbol);
    symbol_c *var_decl = search_decl.get_decl(var_name);
@@ -340,7 +362,7 @@
      /* variable instance declaration not found! */
      return NULL;
    }
-
+#if 0
    /* We have found the declaration.
     * Should we look any further?
     */
@@ -353,4 +375,7 @@
   current_structelement_name = var_name;
   /* recursively find out the data type of var_name... */
   return symbol->var_declarations->accept(*this);
-}
+#endif  
+  /* carry on recursively, in case the variable has more elements to be decomposed... */
+  return var_decl->accept(*this);
+}
--- a/absyntax_utils/search_varfb_instance_type.hh	Thu Aug 25 17:22:44 2011 +0200
+++ b/absyntax_utils/search_varfb_instance_type.hh	Wed Aug 31 14:10:53 2011 +0100
@@ -91,7 +91,7 @@
     search_var_instance_decl_c search_var_instance_decl;
     decompose_var_instance_name_c *decompose_var_instance_name;
     symbol_c *current_structelement_name;
-    symbol_c *current_rawtype;
+    symbol_c *current_typeid;
     bool is_complex;
 
   public:
--- a/configure	Thu Aug 25 17:22:44 2011 +0200
+++ b/configure	Wed Aug 31 14:10:53 2011 +0100
@@ -1825,11 +1825,11 @@
        cat confdefs.h - <<_ACEOF >conftest.$ac_ext
 /* end confdefs.h.  */
 $ac_includes_default
-	     enum { N = $2 / 2 - 1 };
 int
 main ()
 {
-static int test_array [1 - 2 * !(0 < ($ac_type) ((((($ac_type) 1 << N) << N) - 1) * 2 + 1))];
+static int test_array [1 - 2 * !(enum { N = $2 / 2 - 1 };
+	     0 < ($ac_type) ((((($ac_type) 1 << N) << N) - 1) * 2 + 1))];
 test_array [0] = 0
 
   ;
@@ -1840,11 +1840,11 @@
   cat confdefs.h - <<_ACEOF >conftest.$ac_ext
 /* end confdefs.h.  */
 $ac_includes_default
-	        enum { N = $2 / 2 - 1 };
 int
 main ()
 {
-static int test_array [1 - 2 * !(($ac_type) ((((($ac_type) 1 << N) << N) - 1) * 2 + 1)
+static int test_array [1 - 2 * !(enum { N = $2 / 2 - 1 };
+		($ac_type) ((((($ac_type) 1 << N) << N) - 1) * 2 + 1)
 		 < ($ac_type) ((((($ac_type) 1 << N) << N) - 1) * 2 + 2))];
 test_array [0] = 0
 
@@ -6242,9 +6242,9 @@
 
 cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
 # Files that config.status was made for.
-config_files="$ac_config_files"
-config_headers="$ac_config_headers"
-config_commands="$ac_config_commands"
+config_files="`echo $ac_config_files`"
+config_headers="`echo $ac_config_headers`"
+config_commands="`echo $ac_config_commands`"
 
 _ACEOF