stage4/generate_c/generate_c_st.cc
changeset 145 72ae82e65dbc
parent 130 a8263f33123f
child 146 eef5e62048c7
equal deleted inserted replaced
144:0ee0055a8ffe 145:72ae82e65dbc
    70 
    70 
    71     search_base_type_c search_base_type;
    71     search_base_type_c search_base_type;
    72 
    72 
    73     symbol_c* current_array_type;
    73     symbol_c* current_array_type;
    74 
    74 
       
    75     bool current_param_is_pointer;
       
    76 
    75   public:
    77   public:
    76     generate_c_st_c(stage4out_c *s4o_ptr, symbol_c *scope, const char *variable_prefix = NULL)
    78     generate_c_st_c(stage4out_c *s4o_ptr, symbol_c *scope, const char *variable_prefix = NULL)
    77     : generate_c_typedecl_c(s4o_ptr) {
    79     : generate_c_typedecl_c(s4o_ptr) {
    78       search_fb_instance_decl = new search_fb_instance_decl_c(scope);
    80       search_fb_instance_decl = new search_fb_instance_decl_c(scope);
    79       search_expression_type = new search_expression_type_c(scope);
    81       search_expression_type = new search_expression_type_c(scope);
    80       search_varfb_instance_type = new search_varfb_instance_type_c(scope);
    82       search_varfb_instance_type = new search_varfb_instance_type_c(scope);
    81       this->set_variable_prefix(variable_prefix);
    83       this->set_variable_prefix(variable_prefix);
    82       current_array_type = NULL;
    84       current_array_type = NULL;
       
    85       current_param_is_pointer = false;
    83     }
    86     }
    84 
    87 
    85     virtual ~generate_c_st_c(void) {
    88     virtual ~generate_c_st_c(void) {
    86       delete search_fb_instance_decl;
    89       delete search_fb_instance_decl;
    87       delete search_expression_type;
    90       delete search_expression_type;
   134 /*********************/
   137 /*********************/
   135 /* B 1.4 - Variables */
   138 /* B 1.4 - Variables */
   136 /*********************/
   139 /*********************/
   137 void *visit(symbolic_variable_c *symbol) {
   140 void *visit(symbolic_variable_c *symbol) {
   138   unsigned int vartype = search_varfb_instance_type->get_vartype(symbol);
   141   unsigned int vartype = search_varfb_instance_type->get_vartype(symbol);
   139   if (vartype == search_var_instance_decl_c::external_vt || vartype == search_var_instance_decl_c::located_vt) {
   142   if (!current_param_is_pointer && (vartype == search_var_instance_decl_c::external_vt || vartype == search_var_instance_decl_c::located_vt)) {
   140     s4o.print("*(");
   143     s4o.print("*(");
       
   144     generate_c_base_c::visit(symbol);
       
   145     s4o.print(")");
       
   146   }
       
   147   else if (current_param_is_pointer && vartype != search_var_instance_decl_c::external_vt && vartype != search_var_instance_decl_c::located_vt) {
       
   148     s4o.print("&(");
   141     generate_c_base_c::visit(symbol);
   149     generate_c_base_c::visit(symbol);
   142     s4o.print(")");
   150     s4o.print(")");
   143   }
   151   }
   144   else {
   152   else {
   145     generate_c_base_c::visit(symbol);
   153     generate_c_base_c::visit(symbol);
   153 // direct_variable: direct_variable_token   {$$ = new direct_variable_c($1);};
   161 // direct_variable: direct_variable_token   {$$ = new direct_variable_c($1);};
   154 void *visit(direct_variable_c *symbol) {
   162 void *visit(direct_variable_c *symbol) {
   155   TRACE("direct_variable_c");
   163   TRACE("direct_variable_c");
   156   /* Do not use print_token() as it will change everything into uppercase */
   164   /* Do not use print_token() as it will change everything into uppercase */
   157   if (strlen(symbol->value) == 0) ERROR;
   165   if (strlen(symbol->value) == 0) ERROR;
   158   s4o.print("*(");
   166   if (!current_param_is_pointer) {
       
   167     s4o.print("*(");
       
   168   }
   159   this->print_variable_prefix();
   169   this->print_variable_prefix();
   160   s4o.printlocation(symbol->value + 1);
   170   s4o.printlocation(symbol->value + 1);
   161   s4o.print(")");
   171   if (!current_param_is_pointer) {
       
   172     s4o.print(")");
       
   173   }
   162   return NULL;
   174   return NULL;
   163 }
   175 }
   164 
   176 
   165 /*************************************/
   177 /*************************************/
   166 /* B.1.4.2   Multi-element Variables */
   178 /* B.1.4.2   Multi-element Variables */
   593           if (search_base_type.type_is_subrange(param_type))
   605           if (search_base_type.type_is_subrange(param_type))
   594             s4o.print(")");
   606             s4o.print(")");
   595           break;
   607           break;
   596         case function_param_iterator_c::direction_out:
   608         case function_param_iterator_c::direction_out:
   597         case function_param_iterator_c::direction_inout:
   609         case function_param_iterator_c::direction_inout:
       
   610           current_param_is_pointer = true;
   598           if (param_value == NULL) {
   611           if (param_value == NULL) {
   599             /* no parameter value given, so we pass a previously declared temporary variable. */
   612             /* no parameter value given, so we pass a previously declared temporary variable. */
   600             std::string *temp_var_name = temp_var_name_factory.new_name();
   613             std::string *temp_var_name = temp_var_name_factory.new_name();
   601             s4o.print(*temp_var_name);
   614             s4o.print(*temp_var_name);
   602             delete temp_var_name;
   615             delete temp_var_name;
   603           } else {
   616           } else {
   604             param_value->accept(*this);
   617             param_value->accept(*this);
   605           }
   618           }
       
   619           current_param_is_pointer = false;
   606           break;
   620           break;
   607         case function_param_iterator_c::direction_extref:
   621         case function_param_iterator_c::direction_extref:
   608           /* TODO! */
   622           /* TODO! */
   609           ERROR;
   623           ERROR;
   610           break;
   624           break;