Adding support for setter and getter on complex datatype in ST
authorlaurent
Sat, 05 Dec 2009 18:38:53 +0100
changeset 226 29f8ffc203c1
parent 225 36d05588f91b
child 227 560c1231ad1f
Adding support for setter and getter on complex datatype in ST
absyntax_utils/search_var_instance_decl.cc
absyntax_utils/search_var_instance_decl.hh
absyntax_utils/search_varfb_instance_type.cc
absyntax_utils/search_varfb_instance_type.hh
stage4/generate_c/generate_c_il.cc
stage4/generate_c/generate_c_st.cc
stage4/generate_c/generate_c_vardecl.cc
--- a/absyntax_utils/search_var_instance_decl.cc	Sat Dec 05 18:35:38 2009 +0100
+++ b/absyntax_utils/search_var_instance_decl.cc	Sat Dec 05 18:38:53 2009 +0100
@@ -69,7 +69,7 @@
   return (symbol_c *)search_scope->accept(*this);
 }
 
-unsigned int search_var_instance_decl_c::get_vartype() {
+unsigned int search_var_instance_decl_c::get_vartype(void) {
   return current_vartype;
 }
 
--- a/absyntax_utils/search_var_instance_decl.hh	Sat Dec 05 18:35:38 2009 +0100
+++ b/absyntax_utils/search_var_instance_decl.hh	Sat Dec 05 18:38:53 2009 +0100
@@ -69,7 +69,7 @@
   public:
     search_var_instance_decl_c(symbol_c *search_scope);
     symbol_c *get_decl(symbol_c *variable_instance_name);
-    unsigned int get_vartype();
+    unsigned int get_vartype(void);
 
   public:
 
--- a/absyntax_utils/search_varfb_instance_type.cc	Sat Dec 05 18:35:38 2009 +0100
+++ b/absyntax_utils/search_varfb_instance_type.cc	Sat Dec 05 18:38:53 2009 +0100
@@ -56,13 +56,13 @@
 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->search_base_type = false;
-}
-
-symbol_c *search_varfb_instance_type_c::get_type(symbol_c *variable_name, bool base_type) {
+  this->current_rawtype = NULL;
+}
+
+symbol_c *search_varfb_instance_type_c::get_type(symbol_c *variable_name) {
   this->current_structelement_name = NULL;
+  this->current_rawtype = NULL;
   this->decompose_var_instance_name = new decompose_var_instance_name_c(variable_name);
-  this->search_base_type = base_type;
   if (NULL == decompose_var_instance_name) ERROR;
 
   /* find the part of the variable name that will appear in the
@@ -88,7 +88,7 @@
   symbol_c *res = (symbol_c *)var_decl->accept(*this);
   if (NULL == res) ERROR;
 
-  /* make sure that we have decomposed all strcuture elements of the variable name */
+  /* make sure that we have decomposed all structure elements of the variable name */
   symbol_c *var_name = decompose_var_instance_name->next_part();
   if (NULL != var_name) ERROR;
 
@@ -97,6 +97,8 @@
 
 unsigned int search_varfb_instance_type_c::get_vartype(symbol_c *variable_name) {
   this->current_structelement_name = NULL;
+  this->current_rawtype = 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;
 
@@ -120,15 +122,28 @@
    * This class, while visiting, will recursively call
    * decompose_var_instance_name->get_next() when and if required...
    */
+  var_decl->accept(*this);
   unsigned int res = search_var_instance_decl.get_vartype();
   
-  /* make sure that we have decomposed all strcuture elements of the variable name */
+  /* make sure that we have decomposed all structure elements of the variable name */
   symbol_c *var_name = decompose_var_instance_name->next_part();
   if (NULL != var_name) ERROR;
 
   return res;
 }
 
+symbol_c *search_varfb_instance_type_c::get_rawtype(symbol_c *variable_name) {
+  symbol_c *rawtype = this->get_type(variable_name);
+  if (this->current_rawtype != NULL)
+    return this->current_rawtype;
+  else
+	return rawtype;
+}
+
+bool search_varfb_instance_type_c::type_is_complex(void) {
+  return this->is_complex;
+}
+
 /* a helper function... */
 void *search_varfb_instance_type_c::visit_list(list_c *list)	{
   if (NULL == current_structelement_name) ERROR;
@@ -163,10 +178,11 @@
   /* No. It is not a function block, so we let
    * the base class take care of it...
    */
-  if (this->search_base_type)
+  this->current_rawtype = type_name;
+  if (current_structelement_name == NULL)
+	return base_type(type_name);
+  else
     return search_base_type_c::visit(type_name);
-  else
-    return type_name;
 }
 
 /********************************/
@@ -181,6 +197,7 @@
 /* array_specification [ASSIGN array_initialization} */
 /* array_initialization may be NULL ! */
 void *search_varfb_instance_type_c::visit(array_spec_init_c *symbol) {
+  this->is_complex = true;
   symbol_c *var_name = decompose_var_instance_name->next_part();
   if (NULL != var_name)
     current_structelement_name = var_name;
@@ -209,7 +226,8 @@
 /* structure_initialization may be NULL ! */
 // SYM_REF2(initialized_structure_c, structure_type_name, structure_initialization)
 void *search_varfb_instance_type_c::visit(initialized_structure_c *symbol)	{
-  /* make sure that we have decomposed all strcuture elements of the variable name */
+  this->is_complex = true;
+  /* make sure that we have decomposed all structure elements of the variable name */
   symbol_c *var_name = decompose_var_instance_name->next_part();
   if (NULL == var_name) {
     /* this is it... !
--- a/absyntax_utils/search_varfb_instance_type.hh	Sat Dec 05 18:35:38 2009 +0100
+++ b/absyntax_utils/search_varfb_instance_type.hh	Sat Dec 05 18:38:53 2009 +0100
@@ -53,13 +53,16 @@
     search_var_instance_decl_c search_var_instance_decl;
     decompose_var_instance_name_c *decompose_var_instance_name;
     symbol_c *current_structelement_name;
-    bool search_base_type;
+    symbol_c *current_rawtype;
+    bool is_complex;
 
   public:
     search_varfb_instance_type_c(symbol_c *search_scope);
-    symbol_c *get_type(symbol_c *variable_name, bool base_type = true);
+    symbol_c *get_type(symbol_c *variable_name);
+    symbol_c *get_rawtype(symbol_c *variable_name);
 
     unsigned int get_vartype(symbol_c *variable_name);
+    bool type_is_complex(void);
 
   private:
     /* a helper function... */
--- a/stage4/generate_c/generate_c_il.cc	Sat Dec 05 18:35:38 2009 +0100
+++ b/stage4/generate_c/generate_c_il.cc	Sat Dec 05 18:38:53 2009 +0100
@@ -1004,7 +1004,7 @@
     if (param_value != NULL)
       if ((param_direction == function_param_iterator_c::direction_out) ||
           (param_direction == function_param_iterator_c::direction_inout)) {
-        symbol_c *param_type = search_varfb_instance_type->get_type(param_value, false);
+        symbol_c *param_type = search_varfb_instance_type->get_rawtype(param_value);
         unsigned int vartype = search_varfb_instance_type->get_vartype(param_value);
 
         if (!this->is_variable_prefix_null()) {
@@ -1376,7 +1376,7 @@
 }
 
 void *visit(ST_operator_c *symbol)	{
-  symbol_c *operand_type = search_varfb_instance_type->get_type(this->current_operand, false);
+  symbol_c *operand_type = search_varfb_instance_type->get_rawtype(this->current_operand);
   
   if (!this->is_variable_prefix_null()) {
     unsigned int vartype = search_varfb_instance_type->get_vartype(this->current_operand);
@@ -1409,7 +1409,7 @@
 }
 
 void *visit(STN_operator_c *symbol)	{
-  symbol_c *operand_type = search_varfb_instance_type->get_type(this->current_operand, false);
+  symbol_c *operand_type = search_varfb_instance_type->get_rawtype(this->current_operand);
   
   if (!this->is_variable_prefix_null()) {
     unsigned int vartype = search_varfb_instance_type->get_vartype(this->current_operand);
--- a/stage4/generate_c/generate_c_st.cc	Sat Dec 05 18:35:38 2009 +0100
+++ b/stage4/generate_c/generate_c_st.cc	Sat Dec 05 18:38:53 2009 +0100
@@ -44,6 +44,8 @@
     typedef enum {
       expression_vg,
       assignment_vg,
+      complextype_base_vg,
+      complextype_suffix_vg,
       fparam_output_vg
     } variablegeneration_t;
 
@@ -111,14 +113,74 @@
 
   private:
 
+void *print_getter(symbol_c *symbol) {
+  unsigned int vartype = search_varfb_instance_type->get_vartype(symbol);
+  if (vartype == search_var_instance_decl_c::external_vt)
+	s4o.print(GET_EXTERNAL);
+  else if (vartype == search_var_instance_decl_c::located_vt)
+	s4o.print(GET_LOCATED);
+  else
+	s4o.print(GET_VAR);
+  s4o.print("(");
+
+  wanted_variablegeneration = complextype_base_vg;
+  symbol->accept(*this);
+  if (search_varfb_instance_type->type_is_complex())
+	s4o.print(",");
+  wanted_variablegeneration = complextype_suffix_vg;
+  symbol->accept(*this);
+  s4o.print(")");
+  wanted_variablegeneration = expression_vg;
+  return NULL;
+}
+
+void *print_setter(symbol_c* symbol,
+		symbol_c* type,
+		symbol_c* value,
+		symbol_c* fb_symbol = NULL,
+		symbol_c* fb_value = NULL) {
+  unsigned int vartype = search_varfb_instance_type->get_vartype(symbol);
+  if (vartype == search_var_instance_decl_c::external_vt)
+    s4o.print(SET_EXTERNAL);
+  else if (vartype == search_var_instance_decl_c::located_vt)
+    s4o.print(SET_LOCATED);
+  else
+    s4o.print(SET_VAR);
+  s4o.print("(");
+
+  if (fb_symbol != NULL) {
+    print_variable_prefix();
+    fb_symbol->accept(*this);
+    s4o.print(".");
+  }
+  else
+    wanted_variablegeneration = complextype_base_vg;
+  symbol->accept(*this);
+  s4o.print(",");
+  wanted_variablegeneration = expression_vg;
+  print_check_function(type, value, fb_value);
+  if (search_varfb_instance_type->type_is_complex()) {
+    s4o.print(",");
+    wanted_variablegeneration = complextype_suffix_vg;
+    symbol->accept(*this);
+  }
+  s4o.print(")");
+  wanted_variablegeneration = expression_vg;
+  return NULL;
+}
 
 /*********************/
 /* B 1.4 - Variables */
 /*********************/
 void *visit(symbolic_variable_c *symbol) {
-  unsigned int vartype = search_varfb_instance_type->get_vartype(symbol);
-  if (this->is_variable_prefix_null()) {
-    if (wanted_variablegeneration == fparam_output_vg) {
+  unsigned int vartype;
+  if (wanted_variablegeneration == complextype_base_vg)
+	generate_c_base_c::visit(symbol);
+  else if (wanted_variablegeneration == complextype_suffix_vg)
+	return NULL;
+  else if (this->is_variable_prefix_null()) {
+	vartype = search_varfb_instance_type->get_vartype(symbol);
+	if (wanted_variablegeneration == fparam_output_vg) {
       if (vartype == search_var_instance_decl_c::external_vt)
     	s4o.print(GET_EXTERNAL);
       else
@@ -141,7 +203,8 @@
   else {
     switch (wanted_variablegeneration) {
       case expression_vg:
-        if (vartype == search_var_instance_decl_c::external_vt)
+    	vartype = search_varfb_instance_type->get_vartype(symbol);
+    	if (vartype == search_var_instance_decl_c::external_vt)
     	  s4o.print(GET_EXTERNAL);
     	else if (vartype == search_var_instance_decl_c::located_vt)
 		  s4o.print(GET_LOCATED);
@@ -152,7 +215,8 @@
         s4o.print(")");
 		break;
       case fparam_output_vg:
-        if (vartype == search_var_instance_decl_c::external_vt)
+    	vartype = search_varfb_instance_type->get_vartype(symbol);
+    	if (vartype == search_var_instance_decl_c::external_vt)
           s4o.print(GET_EXTERNAL_BY_REF);
         else if (vartype == search_var_instance_decl_c::located_vt)
           s4o.print(GET_LOCATED_BY_REF);
@@ -211,60 +275,24 @@
 // SYM_REF2(structured_variable_c, record_variable, field_selector)
 void *visit(structured_variable_c *symbol) {
   TRACE("structured_variable_c");
-
-  unsigned int vartype = search_varfb_instance_type->get_vartype(symbol->record_variable);
-  if (this->is_variable_prefix_null()) {
-	  symbol->record_variable->accept(*this);
-	  s4o.print(".");
-	  symbol->field_selector->accept(*this);
-  }
-  else {
-	  variablegeneration_t old_wanted_variablegeneration = wanted_variablegeneration;
-	  switch (wanted_variablegeneration) {
-      case expression_vg:
-    	wanted_variablegeneration = assignment_vg;
-    	if (vartype == search_var_instance_decl_c::external_vt) {
-    	  s4o.print(GET_EXTERNAL);
-    	  s4o.print("(");
-    	  symbol->record_variable->accept(*this);
-    	  s4o.print(").");
-    	  symbol->field_selector->accept(*this);
-        }
-        else {
-          if (vartype == search_var_instance_decl_c::located_vt)
-            s4o.print(GET_LOCATED);
-          else
-            s4o.print(GET_VAR);
-          s4o.print("(");
-          symbol->record_variable->accept(*this);
-          s4o.print(".");
-          symbol->field_selector->accept(*this);
-          s4o.print(")");
-        }
-        wanted_variablegeneration = old_wanted_variablegeneration;
-		break;
-      case fparam_output_vg:
-    	wanted_variablegeneration = assignment_vg;
-    	s4o.print("&(");
-    	if (vartype == search_var_instance_decl_c::external_vt)
-          s4o.print(GET_EXTERNAL);
-        else if (vartype == search_var_instance_decl_c::located_vt)
-          s4o.print(GET_LOCATED);
-        else
-          s4o.print(GET_VAR);
-        s4o.print("(");
-    	symbol->record_variable->accept(*this);
-    	s4o.print(").");
-    	symbol->field_selector->accept(*this);
-    	s4o.print("))");
-        wanted_variablegeneration = old_wanted_variablegeneration;
-        break;
-      default:
+  switch (wanted_variablegeneration) {
+    case complextype_base_vg:
+      symbol->record_variable->accept(*this);
+      break;
+    case complextype_suffix_vg:
+      symbol->record_variable->accept(*this);
+      s4o.print(".");
+      symbol->field_selector->accept(*this);
+      break;
+    default:
+      if (this->is_variable_prefix_null()) {
     	symbol->record_variable->accept(*this);
     	s4o.print(".");
     	symbol->field_selector->accept(*this);
-        break;
-	}
+      }
+      else
+    	print_getter(symbol);
+      break;
   }
   return NULL;
 }
@@ -272,11 +300,30 @@
 /*  subscripted_variable '[' subscript_list ']' */
 //SYM_REF2(array_variable_c, subscripted_variable, subscript_list)
 void *visit(array_variable_c *symbol) {
-  current_array_type = search_varfb_instance_type->get_type(symbol->subscripted_variable, false);
-  symbol->subscripted_variable->accept(*this);
-  if (current_array_type != NULL) {
-    symbol->subscript_list->accept(*this);
-    current_array_type = NULL;
+  switch (wanted_variablegeneration) {
+    case complextype_base_vg:
+      symbol->subscripted_variable->accept(*this);
+      break;
+    case complextype_suffix_vg:
+      current_array_type = search_varfb_instance_type->get_rawtype(symbol->subscripted_variable);
+      symbol->subscripted_variable->accept(*this);
+      if (current_array_type != NULL) {
+        symbol->subscript_list->accept(*this);
+        current_array_type = NULL;
+      }
+      break;
+    default:
+      if (this->is_variable_prefix_null()) {
+    	current_array_type = search_varfb_instance_type->get_rawtype(symbol->subscripted_variable);
+    	symbol->subscripted_variable->accept(*this);
+    	if (current_array_type != NULL) {
+    	  symbol->subscript_list->accept(*this);
+    	  current_array_type = NULL;
+    	}
+      }
+      else
+    	print_getter(symbol);
+      break;
   }
   return NULL;
 }
@@ -708,32 +755,16 @@
 /* B 3.2.1 Assignment Statements */
 /*********************************/
 void *visit(assignment_statement_c *symbol) {
-  symbol_c *left_type = search_varfb_instance_type->get_type(symbol->l_exp, false);
+  symbol_c *left_type = search_varfb_instance_type->get_rawtype(symbol->l_exp);
   
-  if (!this->is_variable_prefix_null()) {
-    unsigned int vartype = search_varfb_instance_type->get_vartype(symbol->l_exp);
-    if (vartype == search_var_instance_decl_c::external_vt)
-	  s4o.print(SET_EXTERNAL);
-    else if (vartype == search_var_instance_decl_c::located_vt)
-	  s4o.print(SET_LOCATED);
-    else
-	  s4o.print(SET_VAR);
-    s4o.print("(");
-
-    wanted_variablegeneration = assignment_vg;
+  if (this->is_variable_prefix_null()) {
     symbol->l_exp->accept(*this);
-    wanted_variablegeneration = expression_vg;
-
-    s4o.print(",");
+    s4o.print(" = ");
+    print_check_function(left_type, symbol->r_exp);
   }
   else {
-	symbol->l_exp->accept(*this);
-
-    s4o.print(" = ");
-  }
-  print_check_function(left_type, symbol->r_exp);
-  if (!this->is_variable_prefix_null())
-    s4o.print(")");
+	print_setter(symbol->l_exp, left_type, symbol->r_exp);
+  }
   return NULL;
 }
 
@@ -781,21 +812,17 @@
     if (param_value != NULL)
       if ((param_direction == function_param_iterator_c::direction_in) ||
           (param_direction == function_param_iterator_c::direction_inout)) {
-        if (!this->is_variable_prefix_null()) {
-          s4o.print(SET_VAR);
-          s4o.print("(");
+        if (this->is_variable_prefix_null()) {
+          print_variable_prefix();
+          symbol->fb_name->accept(*this);
+          s4o.print(".");
+          param_name->accept(*this);
+          s4o.print(" = ");
+          print_check_function(param_type, param_value);
         }
-    	print_variable_prefix();
-        symbol->fb_name->accept(*this);
-        s4o.print(".");
-        param_name->accept(*this);
-        if (this->is_variable_prefix_null())
-          s4o.print(" = ");
-        else
-          s4o.print(",");
-        print_check_function(param_type, param_value);
-        if (!this->is_variable_prefix_null())
-          s4o.print(")");
+        else {
+        	print_setter(param_name, param_type, param_value, symbol->fb_name);
+        }
         s4o.print(";\n" + s4o.indent_spaces);
       }
   } /* for(...) */
@@ -827,31 +854,15 @@
     if (param_value != NULL)
       if ((param_direction == function_param_iterator_c::direction_out) ||
           (param_direction == function_param_iterator_c::direction_inout)) {
-        symbol_c *param_type = search_varfb_instance_type->get_type(param_value, false);
-
-        if (!this->is_variable_prefix_null()) {
-          unsigned int vartype = search_varfb_instance_type->get_vartype(param_value);
-          s4o.print(";\n"+ s4o.indent_spaces);
-          if (vartype == search_var_instance_decl_c::external_vt)
-            s4o.print(SET_EXTERNAL);
-          else if (vartype == search_var_instance_decl_c::located_vt)
-            s4o.print(SET_LOCATED);
-          else
-            s4o.print(SET_VAR);
-          s4o.print("(");
-
-          wanted_variablegeneration = assignment_vg;
+        symbol_c *param_type = search_varfb_instance_type->get_rawtype(param_value);
+        if (this->is_variable_prefix_null()) {
           param_value->accept(*this);
-          wanted_variablegeneration = expression_vg;
-          s4o.print(",");
+          s4o.print(" = ");
+          print_check_function(param_type, param_name, symbol->fb_name);
         }
         else {
-          param_value->accept(*this);
-          s4o.print(" = ");
+          print_setter(param_value, param_type, param_name, NULL, symbol->fb_name);
         }
-        print_check_function(param_type, param_name, symbol->fb_name);
-        if (!this->is_variable_prefix_null())
-          s4o.print(")");
       }
   } /* for(...) */
 
--- a/stage4/generate_c/generate_c_vardecl.cc	Sat Dec 05 18:35:38 2009 +0100
+++ b/stage4/generate_c/generate_c_vardecl.cc	Sat Dec 05 18:38:53 2009 +0100
@@ -588,9 +588,9 @@
       
       for (i = 0; i < symbol->n; i++) {
         s4o.print(s4o.indent_spaces);
-        print_variable_prefix();
         s4o.print(SET_VAR);
         s4o.print("(");
+        print_variable_prefix();
         symbol->elements[i]->accept(*this);
         s4o.print(",temp);\n");
       }
@@ -1506,7 +1506,13 @@
 #endif
 
 void *visit(array_initial_elements_list_c *symbol) {
-  s4o.print(";// array initialisation");
+  if (wanted_varformat == localinit_vf) {
+	generate_c_array_initialization_c *array_initialization = new generate_c_array_initialization_c(&s4o);
+	array_initialization->init_array_dimensions(this->current_var_type_symbol);
+	array_initialization->current_mode = generate_c_array_initialization_c::initializationvalue_am;
+	array_initialization->init_array_values(this->current_var_init_symbol);
+	delete array_initialization;
+  }
   return NULL;
 }
 
@@ -1542,6 +1548,16 @@
   return NULL;
 }
 
+void *visit(structure_element_initialization_list_c *symbol) {
+  if (wanted_varformat == localinit_vf) {
+    generate_c_structure_initialization_c *structure_initialization = new generate_c_structure_initialization_c(&s4o);
+    structure_initialization->init_structure_default(this->current_var_type_symbol);
+    structure_initialization->current_mode = generate_c_structure_initialization_c::initializationvalue_sm;
+    this->current_var_init_symbol->accept(*structure_initialization);
+	delete structure_initialization;
+  }
+  return NULL;
+}
 
 /* VAR [CONSTANT] var_init_decl_list END_VAR */
 /* option -> may be NULL ! */