Code cleanup: move datatype analysis to get_datatype_info_c
authorMario de Sousa <msousa@fe.up.pt>
Sun, 22 Dec 2013 09:50:02 +0000
changeset 854 13d0b67de111
parent 853 818c4ac5d64d
child 855 a3109442e166
child 863 06820d03a433
Code cleanup: move datatype analysis to get_datatype_info_c
absyntax_utils/search_base_type.cc
absyntax_utils/search_base_type.hh
absyntax_utils/search_var_instance_decl.cc
absyntax_utils/search_var_instance_decl.hh
stage3/fill_candidate_datatypes.cc
stage3/narrow_candidate_datatypes.cc
stage4/generate_c/generate_c.cc
stage4/generate_c/generate_c_base.cc
stage4/generate_c/generate_c_il.cc
stage4/generate_c/generate_c_inlinefcall.cc
stage4/generate_c/generate_c_st.cc
stage4/generate_c/generate_c_typedecl.cc
stage4/generate_c/generate_c_vardecl.cc
--- a/absyntax_utils/search_base_type.cc	Fri Dec 20 11:44:38 2013 +0000
+++ b/absyntax_utils/search_base_type.cc	Sun Dec 22 09:50:02 2013 +0000
@@ -82,32 +82,6 @@
 }
 
 
-/* Note by MJS: The following two functions definately do not belong in this class!! Maybe create a new utility class?
- * I will need to clean this up when the opportunity arises!
- */
-/* static method! */
-bool search_base_type_c::type_is_subrange(symbol_c* type_decl) {
-  create_singleton();
-  search_base_type_singleton->is_subrange = false;
-  type_decl->accept(*search_base_type_singleton);
-  return search_base_type_singleton->is_subrange;
-}
-
-
-/* static method! */
-bool search_base_type_c::type_is_enumerated(symbol_c* type_decl) {
-  create_singleton();
-  search_base_type_singleton->is_enumerated = false;
-  type_decl->accept(*search_base_type_singleton);
-  return search_base_type_singleton->is_enumerated;
-}
-
-bool search_base_type_c::type_is_fb(symbol_c* type_decl) {
-  create_singleton();
-  search_base_type_singleton->is_fb = false;
-  type_decl->accept(*search_base_type_singleton);
-  return search_base_type_singleton->is_fb;
-}
 
 /*************************/
 /* B.1 - Common elements */
@@ -241,7 +215,6 @@
 
 /* subrange_specification ASSIGN signed_integer */
 void *search_base_type_c::visit(subrange_spec_init_c *symbol) {
-  this->is_subrange = true;
   return symbol->subrange_specification->accept(*this);
 }
 
@@ -268,7 +241,6 @@
 
 /* enumerated_specification ASSIGN enumerated_value */
 void *search_base_type_c::visit(enumerated_spec_init_c *symbol) {
-  this->is_enumerated = true;
   // current_basetype may have been set in the previous enumerated_type_declaration_c visitor, in which case we do not want to overwrite the value!
   if (NULL == this->current_basetype)
     this->current_basetype  = symbol; 
@@ -282,7 +254,6 @@
 /* helper symbol for enumerated_specification->enumerated_spec_init */
 /* enumerated_value_list ',' enumerated_value */
 void *search_base_type_c::visit(enumerated_value_list_c *symbol) {
-  this->is_enumerated = true;
   // current_basetype may have been set in the previous enumerated_type_declaration_c or enumerated_spec_init_c visitors, in which case we do not want to overwrite the value!
   if (NULL == this->current_basetype) 
     this->current_basetype  = symbol; 
@@ -388,7 +359,6 @@
 /*  FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */
 // SYM_REF3(function_block_declaration_c, fblock_name, var_declarations, fblock_body)
 void *search_base_type_c::visit(function_block_declaration_c *symbol)                   {
-	this->is_fb = true;
 	return (void *)symbol;
 }
 
--- a/absyntax_utils/search_base_type.hh	Fri Dec 20 11:44:38 2013 +0000
+++ b/absyntax_utils/search_base_type.hh	Sun Dec 22 09:50:02 2013 +0000
@@ -53,10 +53,6 @@
   private:
     symbol_c *current_type_name;
     symbol_c *current_basetype;
-    bool is_array;
-    bool is_subrange;
-    bool is_enumerated;
-    bool is_fb;
     static search_base_type_c *search_base_type_singleton; // Make this a singleton class!
     
   private:  
@@ -66,9 +62,6 @@
     search_base_type_c(void);
     static symbol_c *get_basetype_decl (symbol_c *symbol);
     static symbol_c *get_basetype_id   (symbol_c *symbol);
-    static bool      type_is_subrange  (symbol_c *type_decl);
-    static bool      type_is_enumerated(symbol_c *type_decl);
-    static bool      type_is_fb        (symbol_c *type_decl);
 
   public:
   /*************************/
--- a/absyntax_utils/search_var_instance_decl.cc	Fri Dec 20 11:44:38 2013 +0000
+++ b/absyntax_utils/search_var_instance_decl.cc	Sun Dec 22 09:50:02 2013 +0000
@@ -129,33 +129,6 @@
 
 
 
-/* This is a temporary fix. Hopefully, once I clean up stage4 code, and I change the way
- * we generate C code, this function will no longer be needed!
- */
-#include <typeinfo>  /* required for typeid() */
-bool search_var_instance_decl_c::type_is_complex(symbol_c *symbol) {
-  symbol_c *decl;
-  
-  decl = symbol->datatype;
-  if (NULL == decl) ERROR;
-  return ((typeid( *(decl) ) == typeid( array_specification_c                )) ||
-//        (typeid( *(decl) ) == typeid( array_spec_init_c                    )) ||  /* does not seem to be necessary */
-          (typeid( *(decl) ) == typeid( structure_type_declaration_c         )) ||  
-          (typeid( *(decl) ) == typeid( structure_element_declaration_list_c )) ||
-//        (typeid( *(decl) ) == typeid( structure_type_declaration_c         )) ||  /* does not seem to be necessary */
-          (typeid( *(decl) ) == typeid( initialized_structure_c              )) ||
-          (search_base_type_c::type_is_fb(decl) && current_vartype == external_vt)
-         );
-}
-
-bool search_var_instance_decl_c::type_is_fb(symbol_c *symbol) {
-    symbol_c *decl;
-    search_base_type_c search_base_type;
-
-    decl = this->get_decl(symbol);
-    if (NULL == decl) ERROR;
-    return search_base_type.type_is_fb(decl);
-}
 
 /***************************/
 /* B 0 - Programming Model */
--- a/absyntax_utils/search_var_instance_decl.hh	Fri Dec 20 11:44:38 2013 +0000
+++ b/absyntax_utils/search_var_instance_decl.hh	Sun Dec 22 09:50:02 2013 +0000
@@ -134,14 +134,6 @@
     vt_t         get_vartype(symbol_c *variable_instance_name);
     opt_t        get_option (symbol_c *variable_instance_name);
 
-    /* NOTE: The following function will be completely deleted in the (hopefully near) future. */
-    /* Returns true if the variable is an ARRAY or a STRUCT, otherwise returns false.
-     * Note that for FB, also returns false!
-     */
-    bool type_is_complex(symbol_c *variable_name);
-
-    bool type_is_fb(symbol_c *symbol);
-    
   private:
     symbol_c *search_scope;
     symbol_c *search_name;
--- a/stage3/fill_candidate_datatypes.cc	Fri Dec 20 11:44:38 2013 +0000
+++ b/stage3/fill_candidate_datatypes.cc	Sun Dec 22 09:50:02 2013 +0000
@@ -668,7 +668,7 @@
 	handle_binary_expression(widen_table, symbol, l_expr, r_expr);
 	for(unsigned int i = 0; i < l_expr->candidate_datatypes.size(); i++)
 		for(unsigned int j = 0; j < r_expr->candidate_datatypes.size(); j++) {
-			if ((l_expr->candidate_datatypes[i] == r_expr->candidate_datatypes[j]) && search_base_type_c::type_is_enumerated(l_expr->candidate_datatypes[i]))
+			if ((l_expr->candidate_datatypes[i] == r_expr->candidate_datatypes[j]) && get_datatype_info_c::is_enumerated(l_expr->candidate_datatypes[i]))
 				add_datatype_to_candidate_list(symbol, &get_datatype_info_c::bool_type_name);
 		}
 	return NULL;
--- a/stage3/narrow_candidate_datatypes.cc	Fri Dec 20 11:44:38 2013 +0000
+++ b/stage3/narrow_candidate_datatypes.cc	Sun Dec 22 09:50:02 2013 +0000
@@ -1360,7 +1360,7 @@
 				l_expr->datatype = l_type;
 				r_expr->datatype = r_type;
 				count ++;
-			} else if ((l_type == r_type) && search_base_type_c::type_is_enumerated(l_type) && get_datatype_info_c::is_BOOL_compatible(symbol->datatype)) {
+			} else if ((l_type == r_type) && get_datatype_info_c::is_enumerated(l_type) && get_datatype_info_c::is_BOOL_compatible(symbol->datatype)) {
 				if (NULL != deprecated_operation)  *deprecated_operation = false;
 				l_expr->datatype = l_type;
 				r_expr->datatype = r_type;
@@ -1516,7 +1516,7 @@
 void *narrow_candidate_datatypes_c::visit(case_statement_c *symbol) {
 	for (unsigned int i = 0; i < symbol->expression->candidate_datatypes.size(); i++) {
 		if ((get_datatype_info_c::is_ANY_INT(symbol->expression->candidate_datatypes[i]))
-				 || (search_base_type_c::type_is_enumerated(symbol->expression->candidate_datatypes[i])))
+				 || (get_datatype_info_c::is_enumerated(symbol->expression->candidate_datatypes[i])))
 			symbol->expression->datatype = symbol->expression->candidate_datatypes[i];
 	}
 	symbol->expression->accept(*this);
--- a/stage4/generate_c/generate_c.cc	Fri Dec 20 11:44:38 2013 +0000
+++ b/stage4/generate_c/generate_c.cc	Sun Dec 22 09:50:02 2013 +0000
@@ -352,7 +352,7 @@
     
     static bool is_complex_type(symbol_c *symbol) {
       if (NULL == symbol)           ERROR;
-      if (NULL == symbol->datatype) ERROR;
+      if (!get_datatype_info_c::is_type_valid     (symbol->datatype)) ERROR;
       return (   get_datatype_info_c::is_structure(symbol->datatype) 
               || get_datatype_info_c::is_array    (symbol->datatype) 
              );
@@ -392,6 +392,7 @@
     void *visit(structured_variable_c *symbol) {
       symbol->record_variable->accept(*this);
       /* do not set the contains_complex_type_res to TRUE if this structured_variable_c is accessing a FB instance! */
+      if (!get_datatype_info_c::is_type_valid(symbol->datatype)) ERROR;
       contains_complex_type_res |= get_datatype_info_c::is_structure(symbol->datatype);
       return NULL;
     }
--- a/stage4/generate_c/generate_c_base.cc	Fri Dec 20 11:44:38 2013 +0000
+++ b/stage4/generate_c/generate_c_base.cc	Sun Dec 22 09:50:02 2013 +0000
@@ -227,7 +227,8 @@
           symbol_c *value,
           symbol_c *fb_name = NULL,
           bool temp = false) {
-      bool is_subrange = search_base_type_c::type_is_subrange(type);
+      if (!get_datatype_info_c::is_type_valid(type)) ERROR;
+      bool is_subrange = get_datatype_info_c::is_subrange(type);
       if (is_subrange) {
 		s4o.print("__CHECK_");
 		type->accept(*this);
--- a/stage4/generate_c/generate_c_il.cc	Fri Dec 20 11:44:38 2013 +0000
+++ b/stage4/generate_c/generate_c_il.cc	Sun Dec 22 09:50:02 2013 +0000
@@ -367,7 +367,8 @@
       unsigned int vartype = search_var_instance_decl->get_vartype(symbol);
       if (wanted_variablegeneration == fparam_output_vg) {
         if (vartype == search_var_instance_decl_c::external_vt) {
-          if (search_var_instance_decl->type_is_fb(symbol))
+          if (!get_datatype_info_c::is_type_valid    (symbol->datatype)) ERROR;
+          if ( get_datatype_info_c::is_function_block(symbol->datatype))
             s4o.print(GET_EXTERNAL_FB_BY_REF);
           else
             s4o.print(GET_EXTERNAL_BY_REF);
@@ -379,7 +380,8 @@
       }
       else {
         if (vartype == search_var_instance_decl_c::external_vt) {
-          if (search_var_instance_decl->type_is_fb(symbol))
+          if (!get_datatype_info_c::is_type_valid    (symbol->datatype)) ERROR;
+          if ( get_datatype_info_c::is_function_block(symbol->datatype))
             s4o.print(GET_EXTERNAL_FB);
           else
             s4o.print(GET_EXTERNAL);
@@ -414,7 +416,8 @@
         unsigned int vartype = search_var_instance_decl->get_vartype(symbol);
         type_is_complex = analyse_variable_c::contains_complex_type(symbol);
         if (vartype == search_var_instance_decl_c::external_vt) {
-          if (search_var_instance_decl->type_is_fb(symbol))
+          if (!get_datatype_info_c::is_type_valid    (symbol->datatype)) ERROR;
+          if ( get_datatype_info_c::is_function_block(symbol->datatype))
             s4o.print(SET_EXTERNAL_FB);
           else
             s4o.print(SET_EXTERNAL);
@@ -572,7 +575,7 @@
 // SYM_REF2(structured_variable_c, record_variable, field_selector)
 void *visit(structured_variable_c *symbol) {
   TRACE("structured_variable_c");
-  bool type_is_complex = search_var_instance_decl->type_is_complex(symbol->record_variable);
+  bool type_is_complex = analyse_variable_c::is_complex_type(symbol->record_variable);
   switch (wanted_variablegeneration) {
     case complextype_base_vg:
     case complextype_base_assignment_vg:
--- a/stage4/generate_c/generate_c_inlinefcall.cc	Fri Dec 20 11:44:38 2013 +0000
+++ b/stage4/generate_c/generate_c_inlinefcall.cc	Sun Dec 22 09:50:02 2013 +0000
@@ -243,7 +243,8 @@
     void *print_getter(symbol_c *symbol) {
       unsigned int vartype = search_var_instance_decl->get_vartype(symbol);
       if (vartype == search_var_instance_decl_c::external_vt) {
-        if (search_var_instance_decl->type_is_fb(symbol))
+        if (!get_datatype_info_c::is_type_valid    (symbol->datatype)) ERROR;
+        if ( get_datatype_info_c::is_function_block(symbol->datatype))
           s4o.print(GET_EXTERNAL_FB);
         else
           s4o.print(GET_EXTERNAL);
@@ -269,7 +270,8 @@
                        symbol_c* value) {
       unsigned int vartype = search_var_instance_decl->get_vartype(symbol);
       if (vartype == search_var_instance_decl_c::external_vt) {
-        if (search_var_instance_decl->type_is_fb(symbol))
+        if (!get_datatype_info_c::is_type_valid    (symbol->datatype)) ERROR;
+        if ( get_datatype_info_c::is_function_block(symbol->datatype))
           s4o.print(SET_EXTERNAL_FB);
          else
           s4o.print(SET_EXTERNAL);
@@ -336,7 +338,7 @@
     // SYM_REF2(structured_variable_c, record_variable, field_selector)
     void *visit(structured_variable_c *symbol) {
       TRACE("structured_variable_c");
-      bool type_is_complex = search_var_instance_decl->type_is_complex(symbol->record_variable);
+      bool type_is_complex = analyse_variable_c::is_complex_type(symbol->record_variable);
       if (generating_inlinefunction) {
         switch (wanted_variablegeneration) {
           case complextype_base_vg:
--- a/stage4/generate_c/generate_c_st.cc	Fri Dec 20 11:44:38 2013 +0000
+++ b/stage4/generate_c/generate_c_st.cc	Sun Dec 22 09:50:02 2013 +0000
@@ -131,7 +131,8 @@
   unsigned int vartype = search_var_instance_decl->get_vartype(symbol);
   if (wanted_variablegeneration == fparam_output_vg) {
     if (vartype == search_var_instance_decl_c::external_vt) {
-      if (search_var_instance_decl->type_is_fb(symbol))
+      if (!get_datatype_info_c::is_type_valid    (symbol->datatype)) ERROR;
+      if ( get_datatype_info_c::is_function_block(symbol->datatype))
         s4o.print(GET_EXTERNAL_FB_BY_REF);
       else
         s4o.print(GET_EXTERNAL_BY_REF);
@@ -143,7 +144,8 @@
   }
   else {
     if (vartype == search_var_instance_decl_c::external_vt) {
-      if (search_var_instance_decl->type_is_fb(symbol))
+      if (!get_datatype_info_c::is_type_valid    (symbol->datatype)) ERROR;
+      if ( get_datatype_info_c::is_function_block(symbol->datatype))
         s4o.print(GET_EXTERNAL_FB);
       else
         s4o.print(GET_EXTERNAL);
@@ -179,7 +181,8 @@
     unsigned int vartype = search_var_instance_decl->get_vartype(symbol);
     type_is_complex = analyse_variable_c::contains_complex_type(symbol);
     if (vartype == search_var_instance_decl_c::external_vt) {
-      if (search_var_instance_decl->type_is_fb(symbol))
+      if (!get_datatype_info_c::is_type_valid    (symbol->datatype)) ERROR;
+      if ( get_datatype_info_c::is_function_block(symbol->datatype))
         s4o.print(SET_EXTERNAL_FB);
       else
         s4o.print(SET_EXTERNAL);
@@ -329,7 +332,7 @@
 // SYM_REF2(structured_variable_c, record_variable, field_selector)
 void *visit(structured_variable_c *symbol) {
   TRACE("structured_variable_c");
-  bool type_is_complex = search_var_instance_decl->type_is_complex(symbol->record_variable);
+  bool type_is_complex = analyse_variable_c::is_complex_type(symbol->record_variable);
   switch (wanted_variablegeneration) {
     case complextype_base_vg:
     case complextype_base_assignment_vg:
--- a/stage4/generate_c/generate_c_typedecl.cc	Fri Dec 20 11:44:38 2013 +0000
+++ b/stage4/generate_c/generate_c_typedecl.cc	Sun Dec 22 09:50:02 2013 +0000
@@ -98,9 +98,6 @@
       return NULL;
     }
 
-    bool type_is_fb(symbol_c* type_decl) {
-      return search_base_type_c::type_is_fb(type_decl);
-    }
 
 /***************************/
 /* B 0 - Programming Model */
@@ -208,7 +205,7 @@
           s4o.print(" value) {\n");
           s4o.indent_right();
 
-          if (search_base_type_c::type_is_subrange(symbol->integer_type_name)) {
+          if (get_datatype_info_c::is_subrange(symbol->integer_type_name)) {
             s4o.print(s4o.indent_spaces + "value = __CHECK_");
             symbol->integer_type_name->accept(*this);
             s4o.print("(value);\n");
@@ -443,7 +440,7 @@
   symbol->simple_spec_init->accept(*this);
   s4o_incl.print(")\n");
 
-  if (search_base_type_c::type_is_subrange(symbol->simple_type_name)) {
+  if (get_datatype_info_c::is_subrange(symbol->simple_type_name)) {
     s4o.print("#define __CHECK_");
     current_type_name->accept(*this);
     s4o.print(" __CHECK_");
--- a/stage4/generate_c/generate_c_vardecl.cc	Fri Dec 20 11:44:38 2013 +0000
+++ b/stage4/generate_c/generate_c_vardecl.cc	Sun Dec 22 09:50:02 2013 +0000
@@ -1851,7 +1851,8 @@
    */
   this->current_var_type_symbol = symbol->specification;
   this->current_var_init_symbol = NULL;
-  bool is_fb = type_is_fb(this->current_var_type_symbol);
+  if(!get_datatype_info_c::is_type_valid(this->current_var_type_symbol)) ERROR;
+  bool is_fb = get_datatype_info_c::is_function_block(this->current_var_type_symbol);
 
   /* now to produce the c equivalent... */
   switch (wanted_varformat) {
@@ -2071,7 +2072,8 @@
 void *visit(global_var_list_c *symbol) {
   TRACE("global_var_list_c");
   list_c *list = dynamic_cast<list_c *>(symbol);
-  bool is_fb = type_is_fb(this->current_var_type_symbol);
+  if(!get_datatype_info_c::is_type_valid(this->current_var_type_symbol)) ERROR;
+  bool is_fb = get_datatype_info_c::is_function_block(this->current_var_type_symbol);
   /* should NEVER EVER occur!! */
   if (list == NULL) ERROR;