stage4/generate_c/generate_c_il.cc
changeset 350 2c3c4dc34979
parent 336 229eb3e29216
child 355 30db860bd3bd
equal deleted inserted replaced
341:ba80c3ceb6fb 350:2c3c4dc34979
   787   symbol_c *param_data_type = default_variable_name.current_type;
   787   symbol_c *param_data_type = default_variable_name.current_type;
   788   symbol_c *return_data_type = NULL;
   788   symbol_c *return_data_type = NULL;
   789   
   789   
   790   function_call_param_iterator_c function_call_param_iterator(symbol);
   790   function_call_param_iterator_c function_call_param_iterator(symbol);
   791 
   791 
   792   function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name);
   792   function_declaration_c *f_decl = (function_declaration_c *)symbol->called_function_declaration;
   793   if (f_decl == function_symtable.end_value()) {
   793   if (f_decl == NULL) ERROR;
   794     function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name);
   794 
   795     if (current_function_type == function_none) ERROR;
   795   /* determine the base data type returned by the function being called... */
       
   796   search_base_type_c search_base_type;
       
   797   return_data_type = (symbol_c *)f_decl->type_name->accept(search_base_type);
       
   798   if (NULL == return_data_type) ERROR;
       
   799 
       
   800   function_name = symbol->function_name;
       
   801   
       
   802   /* loop through each function parameter, find the value we should pass
       
   803    * to it, and then output the c equivalent...
       
   804    */
       
   805   function_param_iterator_c fp_iterator(f_decl);
       
   806   identifier_c *param_name;
       
   807     /* flag to remember whether we have already used the value stored in the default variable to pass to the first parameter */
       
   808   bool used_defvar = false; 
       
   809     /* flag to cirreclty handle calls to extensible standard functions (i.e. functions with variable number of input parameters) */
       
   810   bool found_first_extensible_parameter = false;  
       
   811   for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) {
       
   812     if (fp_iterator.is_extensible_param() && (!found_first_extensible_parameter)) {
       
   813       /* We are calling an extensible function. Before passing the extensible
       
   814        * parameters, we must add a dummy paramater value to tell the called
       
   815        * function how many extensible parameters we will be passing.
       
   816        *
       
   817        * Note that stage 3 has already determined the number of extensible
       
   818        * paramters, and stored that info in the abstract syntax tree. We simply
       
   819        * re-use that value.
       
   820        */
       
   821       /* NOTE: we are not freeing the malloc'd memory. This is not really a bug.
       
   822        *       Since we are writing a compiler, which runs to termination quickly,
       
   823        *       we can consider this as just memory required for the compilation process
       
   824        *       that will be free'd when the program terminates.
       
   825        */
       
   826       char *tmp = (char *)malloc(32); /* enough space for a call with 10^31 (larger than 2^64) input parameters! */
       
   827       if (tmp == NULL) ERROR;
       
   828       int res = snprintf(tmp, 32, "%d", symbol->extensible_param_count);
       
   829       if ((res >= 32) || (res < 0)) ERROR;
       
   830       identifier_c *param_value = new identifier_c(tmp);
       
   831       uint_type_name_c *param_type  = new uint_type_name_c();
       
   832       identifier_c *param_name = new identifier_c("");
       
   833       ADD_PARAM_LIST(param_name, param_value, param_type, function_param_iterator_c::direction_in)
       
   834       found_first_extensible_parameter = true;
       
   835     }
   796     
   836     
   797     return_data_type = (symbol_c *)search_expression_type->compute_standard_function_il(symbol, param_data_type);
   837     symbol_c *param_type = fp_iterator.param_type();
   798     if (NULL == return_data_type) ERROR;
   838     if (param_type == NULL) ERROR;
   799     
   839     
   800     symbol_c *en_param_name = (symbol_c *)(new identifier_c("EN"));
   840     function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
   801     /* Add the value from EN param */
       
   802     ADD_PARAM_LIST(en_param_name,
       
   803                    (symbol_c*)(new boolean_literal_c((symbol_c*)(new bool_type_name_c()), new boolean_true_c())),
       
   804                    (symbol_c*)(new bool_type_name_c()), 
       
   805                    function_param_iterator_c::direction_in)
       
   806     
   841     
   807     symbol_c *eno_param_name = (symbol_c *)(new identifier_c("ENO"));
   842     symbol_c *param_value = NULL;
   808     /* Add the value from ENO param */
   843 
   809     ADD_PARAM_LIST(eno_param_name, NULL, (symbol_c*)(new bool_type_name_c()), function_param_iterator_c::direction_out)
   844     /* Get the value from a foo(<param_name> = <param_value>) style call */
       
   845     /* NOTE: Since the class il_function_call_c only references a non.formal function call,
       
   846      * the following line of code is not required in this case. However, it doesn't
       
   847      * harm to leave it in, as in the case of a non-formal syntax function call,
       
   848      * it will always return NULL.
       
   849      * We leave it in in case we later decide to merge this part of the code together
       
   850      * with the function calling code in generate_c_st_c, which does require
       
   851      * the following line...
       
   852      */
       
   853     if (param_value == NULL)
       
   854       param_value = function_call_param_iterator.search_f(param_name);
       
   855 
       
   856     /* if it is the first parameter in a non-formal function call (which is the
       
   857      * case being handled!), semantics specifies that we should
       
   858      * get the value off the IL default variable!
       
   859      *
       
   860      * However, if the parameter is an implicitly defined EN or ENO parameter, we should not
       
   861      * use the default variable as a source of data to pass to those parameters!
       
   862      */
       
   863     if ((param_value == NULL) &&  (!used_defvar) && !fp_iterator.is_en_eno_param_implicit()) {
       
   864       param_value = &this->default_variable_name;
       
   865       used_defvar = true;
       
   866     }
       
   867 
       
   868     /* Get the value from a foo(<param_value>) style call */
       
   869     if ((param_value == NULL) && !fp_iterator.is_en_eno_param_implicit()) {
       
   870       param_value = function_call_param_iterator.next_nf();
       
   871     }
   810     
   872     
   811     int nb_param = 1;
   873     /* if no more parameter values in function call, and the current parameter
   812     if (symbol->il_operand_list != NULL)
   874      * of the function declaration is an extensible parameter, we
   813       nb_param += ((list_c *)symbol->il_operand_list)->n;
   875      * have reached the end, and should simply jump out of the for loop.
   814 
   876      */
   815     #include "il_code_gen.c"
   877     if ((param_value == NULL) && (fp_iterator.is_extensible_param())) {
   816 
   878       break;
   817   }
   879     }
   818   else {
       
   819     /* determine the base data type returned by the function being called... */
       
   820     search_base_type_c search_base_type;
       
   821     return_data_type = (symbol_c *)f_decl->type_name->accept(search_base_type);
       
   822     if (NULL == return_data_type) ERROR;
       
   823   
       
   824     function_name = symbol->function_name;
       
   825     
   880     
   826     /* loop through each function parameter, find the value we should pass
   881     if ((param_value == NULL) && (param_direction == function_param_iterator_c::direction_in)) {
   827      * to it, and then output the c equivalent...
   882       /* No value given for parameter, so we must use the default... */
   828      */
   883       /* First check whether default value specified in function declaration...*/
   829   
   884       param_value = fp_iterator.default_value();
   830     function_param_iterator_c fp_iterator(f_decl);
   885     }
   831     identifier_c *param_name;
   886     
   832     for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) {
   887     ADD_PARAM_LIST(param_name, param_value, param_type, fp_iterator.param_direction())
   833       symbol_c *param_type = fp_iterator.param_type();
   888   } /* for(...) */
   834       if (param_type == NULL) ERROR;
   889 
   835       
       
   836       function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
       
   837       
       
   838       symbol_c *param_value = NULL;
       
   839   
       
   840       /* if it is the first parameter, semantics specifies that we should
       
   841        * get the value off the IL default variable!
       
   842        */
       
   843      if (1 == i)
       
   844        param_value = &this->default_variable_name;
       
   845   
       
   846       /* Get the value from a foo(<param_name> = <param_value>) style call */
       
   847       /* NOTE: the following line of code is not required in this case, but it doesn't
       
   848        * harm to leave it in, as in the case of a non-formal syntax function call,
       
   849        * it will always return NULL.
       
   850        * We leave it in in case we later decide to merge this part of the code together
       
   851        * with the function calling code in generate_c_st_c, which does require
       
   852        * the following line...
       
   853        */
       
   854       if (param_value == NULL)
       
   855         param_value = function_call_param_iterator.search_f(param_name);
       
   856   
       
   857       /* Get the value from a foo(<param_value>) style call */
       
   858       if (param_value == NULL) {
       
   859         param_value = function_call_param_iterator.next_nf();
       
   860         if (param_value != NULL && fp_iterator.is_en_eno_param_implicit()) ERROR;
       
   861       }
       
   862       
       
   863       if (param_value == NULL && param_direction == function_param_iterator_c::direction_in) {
       
   864         /* No value given for parameter, so we must use the default... */
       
   865         /* First check whether default value specified in function declaration...*/
       
   866         param_value = fp_iterator.default_value();
       
   867       }
       
   868       
       
   869       ADD_PARAM_LIST(param_name, param_value, param_type, fp_iterator.param_direction())
       
   870     } /* for(...) */
       
   871   }
       
   872   
       
   873   if (function_call_param_iterator.next_nf() != NULL) ERROR;
   890   if (function_call_param_iterator.next_nf() != NULL) ERROR;
   874 
   891 
   875   bool has_output_params = false;
   892   bool has_output_params = false;
   876 
   893 
   877   if (!this->is_variable_prefix_null()) {
   894   if (!this->is_variable_prefix_null()) {
   878     PARAM_LIST_ITERATOR() {
   895     PARAM_LIST_ITERATOR() {
   879 	  if ((PARAM_DIRECTION == function_param_iterator_c::direction_out ||
   896       if ((PARAM_DIRECTION == function_param_iterator_c::direction_out ||
   880 		   PARAM_DIRECTION == function_param_iterator_c::direction_inout) &&
   897            PARAM_DIRECTION == function_param_iterator_c::direction_inout) &&
   881 		  PARAM_VALUE != NULL) {
   898            PARAM_VALUE != NULL) {
   882 	    if (!has_output_params) {
   899         has_output_params = true;
   883 		  has_output_params = true;
   900       }
   884 		}
   901     }
   885 	  }
   902   }
   886     }
   903 
   887   }
   904   /* Check whether we are calling an overloaded function! */
       
   905   /* (fdecl_mutiplicity==2)  => calling overloaded function */
       
   906   int fdecl_mutiplicity =  function_symtable.multiplicity(symbol->function_name);
       
   907   if (fdecl_mutiplicity == 0) ERROR;
   888 
   908 
   889   default_variable_name.current_type = return_data_type;
   909   default_variable_name.current_type = return_data_type;
   890   this->default_variable_name.accept(*this);
   910   this->default_variable_name.accept(*this);
   891   default_variable_name.current_type = param_data_type;
   911   default_variable_name.current_type = param_data_type;
   892   s4o.print(" = ");
   912   s4o.print(" = ");
   895     s4o.print("(");
   915     s4o.print("(");
   896     search_expression_type->default_literal_type(function_type_prefix)->accept(*this);
   916     search_expression_type->default_literal_type(function_type_prefix)->accept(*this);
   897     s4o.print(")");
   917     s4o.print(")");
   898   }
   918   }
   899   if (function_type_suffix != NULL) {
   919   if (function_type_suffix != NULL) {
   900   	function_type_suffix = search_expression_type->default_literal_type(function_type_prefix);
   920     function_type_suffix = search_expression_type->default_literal_type(function_type_prefix);
   901   }
   921   }
   902   if (has_output_params) {
   922   if (has_output_params) {
   903   	fcall_number++;
   923     fcall_number++;
   904   	s4o.print("__");
   924     s4o.print("__");
   905     fbname->accept(*this);
   925     fbname->accept(*this);
   906     s4o.print("_");
   926     s4o.print("_");
   907     function_name->accept(*this);
   927     function_name->accept(*this);
       
   928     if (fdecl_mutiplicity == 2) {
       
   929       /* function being called is overloaded! */
       
   930       s4o.print("__");
       
   931       print_function_parameter_data_types_c overloaded_func_suf(&s4o);
       
   932       f_decl->accept(overloaded_func_suf);
       
   933     }
   908     s4o.print_integer(fcall_number);
   934     s4o.print_integer(fcall_number);
   909   }
   935   }
   910   else {
   936   else {
   911     if (function_name != NULL)
   937     if (function_name != NULL) {
   912 	  function_name->accept(*this);
   938           function_name->accept(*this);
       
   939           if (fdecl_mutiplicity == 2) {
       
   940             /* function being called is overloaded! */
       
   941             s4o.print("__");
       
   942             print_function_parameter_data_types_c overloaded_func_suf(&s4o);
       
   943             f_decl->accept(overloaded_func_suf);
       
   944           }
       
   945     }	  
   913     if (function_type_suffix != NULL)
   946     if (function_type_suffix != NULL)
   914 	  function_type_suffix->accept(*this);
   947       function_type_suffix->accept(*this);
   915   }
   948   }
   916   s4o.print("(");
   949   s4o.print("(");
   917   s4o.indent_right();
   950   s4o.indent_right();
   918   
   951   
   919   int nb_param = 0;
   952   int nb_param = 0;
   921     symbol_c *param_value = PARAM_VALUE;
   954     symbol_c *param_value = PARAM_VALUE;
   922     current_param_type = PARAM_TYPE;
   955     current_param_type = PARAM_TYPE;
   923     
   956     
   924     switch (PARAM_DIRECTION) {
   957     switch (PARAM_DIRECTION) {
   925       case function_param_iterator_c::direction_in:
   958       case function_param_iterator_c::direction_in:
   926     	if (nb_param > 0)
   959         if (nb_param > 0)
   927     	  s4o.print(",\n"+s4o.indent_spaces);
   960           s4o.print(",\n"+s4o.indent_spaces);
   928         if (param_value == NULL) {
   961         if (param_value == NULL) {
   929           /* If not, get the default value of this variable's type */
   962           /* If not, get the default value of this variable's type */
   930           param_value = (symbol_c *)current_param_type->accept(*type_initial_value_c::instance());
   963           param_value = (symbol_c *)current_param_type->accept(*type_initial_value_c::instance());
   931         }
   964         }
   932         if (param_value == NULL) ERROR;
   965         if (param_value == NULL) ERROR;
   941         print_check_function(current_param_type, param_value);
   974         print_check_function(current_param_type, param_value);
   942         nb_param++;
   975         nb_param++;
   943         break;
   976         break;
   944       case function_param_iterator_c::direction_out:
   977       case function_param_iterator_c::direction_out:
   945       case function_param_iterator_c::direction_inout:
   978       case function_param_iterator_c::direction_inout:
   946     	if (!has_output_params) {
   979         if (!has_output_params) {
   947           if (nb_param > 0)
   980           if (nb_param > 0)
   948     		s4o.print(",\n"+s4o.indent_spaces);
   981             s4o.print(",\n"+s4o.indent_spaces);
   949 		  if (param_value == NULL) {
   982             if (param_value == NULL) {
   950 		    s4o.print("NULL");
   983               s4o.print("NULL");
   951 		  } else {
   984             } else {
   952 		    wanted_variablegeneration = fparam_output_vg;
   985               wanted_variablegeneration = fparam_output_vg;
   953 		    param_value->accept(*this);
   986               param_value->accept(*this);
   954 		    wanted_variablegeneration = expression_vg;
   987               wanted_variablegeneration = expression_vg;
   955 		  }
   988             }
   956 		  nb_param++;
   989           nb_param++;
   957     	}
   990         }
   958         break;
   991         break;
   959       case function_param_iterator_c::direction_extref:
   992       case function_param_iterator_c::direction_extref:
   960         /* TODO! */
   993         /* TODO! */
   961         ERROR;
   994         ERROR;
   962         break;
   995         break;
   963     } /* switch */
   996     } /* switch */
   964   }
   997   }
   965   if (has_output_params) {
   998   if (has_output_params) {
   966     if (nb_param > 0)
   999     if (nb_param > 0)
   967 	  s4o.print(",\n"+s4o.indent_spaces);
  1000       s4o.print(",\n"+s4o.indent_spaces);
   968     s4o.print(FB_FUNCTION_PARAM);
  1001     s4o.print(FB_FUNCTION_PARAM);
   969   }
  1002   }
   970   
  1003   
   971   s4o.print(")");
  1004   s4o.print(")");
   972   /* the data type returned by the function, and stored in the il default variable... */
  1005   /* the data type returned by the function, and stored in the il default variable... */
  1070 
  1103 
  1071     /* Get the value from a foo(<param_name> = <param_value>) style call */
  1104     /* Get the value from a foo(<param_name> = <param_value>) style call */
  1072     symbol_c *param_value = function_call_param_iterator.search_f(param_name);
  1105     symbol_c *param_value = function_call_param_iterator.search_f(param_name);
  1073 
  1106 
  1074     /* Get the value from a foo(<param_value>) style call */
  1107     /* Get the value from a foo(<param_value>) style call */
  1075     if (param_value == NULL)
  1108     /* When using the informal invocation style, user can not pass values to EN or ENO parameters if these
       
  1109      * were implicitly defined!
       
  1110      */
       
  1111     if ((param_value == NULL) && !fp_iterator.is_en_eno_param_implicit())
  1076       param_value = function_call_param_iterator.next_nf();
  1112       param_value = function_call_param_iterator.next_nf();
  1077 
  1113 
  1078     symbol_c *param_type = fp_iterator.param_type();
  1114     symbol_c *param_type = fp_iterator.param_type();
  1079     if (param_type == NULL) ERROR;
  1115     if (param_type == NULL) ERROR;
  1080     
  1116     
  1114 
  1150 
  1115     /* Get the value from a foo(<param_name> = <param_value>) style call */
  1151     /* Get the value from a foo(<param_name> = <param_value>) style call */
  1116     symbol_c *param_value = function_call_param_iterator.search_f(param_name);
  1152     symbol_c *param_value = function_call_param_iterator.search_f(param_name);
  1117 
  1153 
  1118     /* Get the value from a foo(<param_value>) style call */
  1154     /* Get the value from a foo(<param_value>) style call */
  1119     if (param_value == NULL)
  1155     /* When using the informal invocation style, user can not pass values to EN or ENO parameters if these
       
  1156      * were implicitly defined!
       
  1157      */
       
  1158     if ((param_value == NULL) && !fp_iterator.is_en_eno_param_implicit())
  1120       param_value = function_call_param_iterator.next_nf();
  1159       param_value = function_call_param_iterator.next_nf();
  1121 
  1160 
  1122     /* now output the value assignment */
  1161     /* now output the value assignment */
  1123     if (param_value != NULL)
  1162     if (param_value != NULL)
  1124       if ((param_direction == function_param_iterator_c::direction_out) ||
  1163       if ((param_direction == function_param_iterator_c::direction_out) ||
  1156 
  1195 
  1157   symbol_c *return_data_type = NULL;
  1196   symbol_c *return_data_type = NULL;
  1158 
  1197 
  1159   function_call_param_iterator_c function_call_param_iterator(symbol);
  1198   function_call_param_iterator_c function_call_param_iterator(symbol);
  1160 
  1199 
  1161   function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name);
  1200   function_declaration_c *f_decl = (function_declaration_c *)symbol->called_function_declaration;
  1162   if (f_decl == function_symtable.end_value()) {
  1201   if (f_decl == NULL) ERROR;
  1163     function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name);
  1202         
  1164     if (current_function_type == function_none) ERROR;
  1203   /* determine the base data type returned by the function being called... */
       
  1204   search_base_type_c search_base_type;
       
  1205   return_data_type = (symbol_c *)f_decl->type_name->accept(search_base_type);
       
  1206   if (NULL == return_data_type) ERROR;
       
  1207   
       
  1208   function_name = symbol->function_name;
       
  1209 
       
  1210   /* loop through each function parameter, find the value we should pass
       
  1211    * to it, and then output the c equivalent...
       
  1212    */
       
  1213   function_param_iterator_c fp_iterator(f_decl);
       
  1214   identifier_c *param_name;
       
  1215 
       
  1216     /* flag to cirreclty handle calls to extensible standard functions (i.e. functions with variable number of input parameters) */
       
  1217   bool found_first_extensible_parameter = false;
       
  1218   for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) {
       
  1219     if (fp_iterator.is_extensible_param() && (!found_first_extensible_parameter)) {
       
  1220       /* We are calling an extensible function. Before passing the extensible
       
  1221        * parameters, we must add a dummy paramater value to tell the called
       
  1222        * function how many extensible parameters we will be passing.
       
  1223        *
       
  1224        * Note that stage 3 has already determined the number of extensible
       
  1225        * paramters, and stored that info in the abstract syntax tree. We simply
       
  1226        * re-use that value.
       
  1227        */
       
  1228       /* NOTE: we are not freeing the malloc'd memory. This is not really a bug.
       
  1229        *       Since we are writing a compiler, which runs to termination quickly,
       
  1230        *       we can consider this as just memory required for the compilation process
       
  1231        *       that will be free'd when the program terminates.
       
  1232        */
       
  1233       char *tmp = (char *)malloc(32); /* enough space for a call with 10^31 (larger than 2^64) input parameters! */
       
  1234       if (tmp == NULL) ERROR;
       
  1235       int res = snprintf(tmp, 32, "%d", symbol->extensible_param_count);
       
  1236       if ((res >= 32) || (res < 0)) ERROR;
       
  1237       identifier_c *param_value = new identifier_c(tmp);
       
  1238       uint_type_name_c *param_type  = new uint_type_name_c();
       
  1239       identifier_c *param_name = new identifier_c("");
       
  1240       ADD_PARAM_LIST(param_name, param_value, param_type, function_param_iterator_c::direction_in)
       
  1241       found_first_extensible_parameter = true;
       
  1242     }
  1165     
  1243     
  1166     return_data_type = (symbol_c *)search_expression_type->compute_standard_function_default(NULL, symbol);
  1244     if (fp_iterator.is_extensible_param()) {      
  1167     if (NULL == return_data_type) ERROR;
  1245       /* since we are handling an extensible parameter, we must add the index to the
       
  1246        * parameter name so we can go looking for the value passed to the correct
       
  1247        * extended parameter (e.g. IN1, IN2, IN3, IN4, ...)
       
  1248        */
       
  1249       char *tmp = (char *)malloc(32); /* enough space for a call with 10^31 (larger than 2^64) input parameters! */
       
  1250       int res = snprintf(tmp, 32, "%d", fp_iterator.extensible_param_index());
       
  1251       if ((res >= 32) || (res < 0)) ERROR;
       
  1252       param_name = new identifier_c(strdup2(param_name->value, tmp));
       
  1253       if (param_name->value == NULL) ERROR;
       
  1254     }
       
  1255 
       
  1256     symbol_c *param_type = fp_iterator.param_type();
       
  1257     if (param_type == NULL) ERROR;
       
  1258 
       
  1259     function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
       
  1260 
       
  1261     symbol_c *param_value = NULL;
       
  1262 
       
  1263     /* Get the value from a foo(<param_name> = <param_value>) style call */
       
  1264     if (param_value == NULL)
       
  1265       param_value = function_call_param_iterator.search_f(param_name);
       
  1266 
       
  1267     /* Get the value from a foo(<param_value>) style call */
       
  1268     /* NOTE: the following line of code is not required in this case, but it doesn't
       
  1269      * harm to leave it in, as in the case of a formal syntax function call,
       
  1270      * it will always return NULL.
       
  1271      * We leave it in in case we later decide to merge this part of the code together
       
  1272      * with the function calling code in generate_c_st_c, which does require
       
  1273      * the following line...
       
  1274      */
       
  1275     if ((param_value == NULL) && !fp_iterator.is_en_eno_param_implicit()) {
       
  1276       param_value = function_call_param_iterator.next_nf();
       
  1277     }
  1168     
  1278     
  1169     int nb_param = 0;
  1279     /* if no more parameter values in function call, and the current parameter
  1170     if (symbol->il_param_list != NULL)
  1280      * of the function declaration is an extensible parameter, we
  1171       nb_param += ((list_c *)symbol->il_param_list)->n;
  1281      * have reached the end, and should simply jump out of the for loop.
       
  1282      */
       
  1283     if ((param_value == NULL) && (fp_iterator.is_extensible_param())) {
       
  1284       break;
       
  1285     }
  1172     
  1286     
  1173     symbol_c *en_param_name = (symbol_c *)(new identifier_c("EN"));
  1287     if ((param_value == NULL) && (param_direction == function_param_iterator_c::direction_in)) {
  1174     /* Get the value from EN param */
  1288       /* No value given for parameter, so we must use the default... */
  1175     symbol_c *EN_param_value = function_call_param_iterator.search_f(en_param_name);
  1289       /* First check whether default value specified in function declaration...*/
  1176     if (EN_param_value == NULL)
  1290       param_value = fp_iterator.default_value();
  1177       EN_param_value = (symbol_c*)(new boolean_literal_c((symbol_c*)(new bool_type_name_c()), new boolean_true_c()));
  1291     }
  1178     else
       
  1179       nb_param --;
       
  1180     ADD_PARAM_LIST(en_param_name, EN_param_value, (symbol_c*)(new bool_type_name_c()), function_param_iterator_c::direction_in)
       
  1181     
  1292     
  1182     symbol_c *eno_param_name = (symbol_c *)(new identifier_c("ENO"));
  1293     ADD_PARAM_LIST(param_name, param_value, param_type, fp_iterator.param_direction())
  1183     /* Get the value from ENO param */
       
  1184     symbol_c *ENO_param_value = function_call_param_iterator.search_f(eno_param_name);
       
  1185     if (ENO_param_value != NULL)
       
  1186       nb_param --;
       
  1187     ADD_PARAM_LIST(eno_param_name, ENO_param_value, (symbol_c*)(new bool_type_name_c()), function_param_iterator_c::direction_out)
       
  1188     
       
  1189     #include "st_code_gen.c"
       
  1190     
       
  1191   }
       
  1192   else {
       
  1193     /* determine the base data type returned by the function being called... */
       
  1194     search_base_type_c search_base_type;
       
  1195     return_data_type = (symbol_c *)f_decl->type_name->accept(search_base_type);
       
  1196     if (NULL == return_data_type) ERROR;
       
  1197     
       
  1198     function_name = symbol->function_name;
       
  1199   
       
  1200     /* loop through each function parameter, find the value we should pass
       
  1201      * to it, and then output the c equivalent...
       
  1202      */
       
  1203   
       
  1204     function_param_iterator_c fp_iterator(f_decl);
       
  1205     identifier_c *param_name;
       
  1206     for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) {
       
  1207       symbol_c *param_type = fp_iterator.param_type();
       
  1208       if (param_type == NULL) ERROR;
       
  1209   
       
  1210       function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
       
  1211   
       
  1212   
       
  1213       symbol_c *param_value = NULL;
       
  1214   
       
  1215       /* Get the value from a foo(<param_name> = <param_value>) style call */
       
  1216       if (param_value == NULL)
       
  1217         param_value = function_call_param_iterator.search_f(param_name);
       
  1218   
       
  1219       /* Get the value from a foo(<param_value>) style call */
       
  1220       /* NOTE: the following line of code is not required in this case, but it doesn't
       
  1221        * harm to leave it in, as in the case of a formal syntax function call,
       
  1222        * it will always return NULL.
       
  1223        * We leave it in in case we later decide to merge this part of the code together
       
  1224        * with the function calling code in generate_c_st_c, which does require
       
  1225        * the following line...
       
  1226        */
       
  1227       if (param_value == NULL) {
       
  1228         param_value = function_call_param_iterator.next_nf();
       
  1229         if (param_value != NULL && fp_iterator.is_en_eno_param_implicit()) ERROR;
       
  1230       }
       
  1231       
       
  1232       if (param_value == NULL) {
       
  1233         /* No value given for parameter, so we must use the default... */
       
  1234         /* First check whether default value specified in function declaration...*/
       
  1235         param_value = fp_iterator.default_value();
       
  1236       }
       
  1237       
       
  1238       ADD_PARAM_LIST(param_name, param_value, param_type, fp_iterator.param_direction())
       
  1239     }
       
  1240   }
  1294   }
  1241   
  1295   
  1242   if (function_call_param_iterator.next_nf() != NULL) ERROR;
  1296   if (function_call_param_iterator.next_nf() != NULL) ERROR;
  1243 
  1297 
  1244   bool has_output_params = false;
  1298   bool has_output_params = false;
  1245 
  1299 
  1246   if (!this->is_variable_prefix_null()) {
  1300   if (!this->is_variable_prefix_null()) {
  1247     PARAM_LIST_ITERATOR() {
  1301     PARAM_LIST_ITERATOR() {
  1248 	  if ((PARAM_DIRECTION == function_param_iterator_c::direction_out ||
  1302       if ((PARAM_DIRECTION == function_param_iterator_c::direction_out ||
  1249 		   PARAM_DIRECTION == function_param_iterator_c::direction_inout) &&
  1303            PARAM_DIRECTION == function_param_iterator_c::direction_inout) &&
  1250 		  PARAM_VALUE != NULL) {
  1304            PARAM_VALUE != NULL) {
  1251 	    if (!has_output_params) {
  1305         has_output_params = true;
  1252 		  has_output_params = true;
  1306       }
  1253 		}
  1307     }
  1254 	  }
  1308   }
  1255     }
  1309 
  1256   }
  1310   /* Check whether we are calling an overloaded function! */
       
  1311   /* (fdecl_mutiplicity==2)  => calling overloaded function */
       
  1312   int fdecl_mutiplicity =  function_symtable.multiplicity(symbol->function_name);
       
  1313   if (fdecl_mutiplicity == 0) ERROR;
       
  1314   if (fdecl_mutiplicity == 1) 
       
  1315     /* function being called is NOT overloaded! */
       
  1316     f_decl = NULL; 
  1257 
  1317 
  1258   default_variable_name.current_type = return_data_type;
  1318   default_variable_name.current_type = return_data_type;
  1259   this->default_variable_name.accept(*this);
  1319   this->default_variable_name.accept(*this);
  1260   s4o.print(" = ");
  1320   s4o.print(" = ");
  1261   
  1321   
  1263     s4o.print("(");
  1323     s4o.print("(");
  1264     search_expression_type->default_literal_type(function_type_prefix)->accept(*this);
  1324     search_expression_type->default_literal_type(function_type_prefix)->accept(*this);
  1265     s4o.print(")");
  1325     s4o.print(")");
  1266   }
  1326   }
  1267   if (function_type_suffix != NULL) {
  1327   if (function_type_suffix != NULL) {
  1268   	function_type_suffix = search_expression_type->default_literal_type(function_type_prefix);
  1328     function_type_suffix = search_expression_type->default_literal_type(function_type_prefix);
  1269   }
  1329   }
  1270   if (has_output_params) {
  1330   if (has_output_params) {
  1271 	fcall_number++;
  1331     fcall_number++;
  1272 	s4o.print("__");
  1332     s4o.print("__");
  1273     fbname->accept(*this);
  1333     fbname->accept(*this);
  1274     s4o.print("_");
  1334     s4o.print("_");
  1275     function_name->accept(*this);
  1335     function_name->accept(*this);
       
  1336     if (fdecl_mutiplicity == 2) {
       
  1337       /* function being called is overloaded! */
       
  1338       s4o.print("__");
       
  1339       print_function_parameter_data_types_c overloaded_func_suf(&s4o);
       
  1340       f_decl->accept(overloaded_func_suf);
       
  1341     }
  1276     s4o.print_integer(fcall_number);
  1342     s4o.print_integer(fcall_number);
  1277   }
  1343   }
  1278   else {
  1344   else {
  1279     if (function_name != NULL)
  1345     if (function_name != NULL) {
  1280       function_name->accept(*this);
  1346       function_name->accept(*this);
       
  1347       if (fdecl_mutiplicity == 2) {
       
  1348         /* function being called is overloaded! */
       
  1349         s4o.print("__");
       
  1350         print_function_parameter_data_types_c overloaded_func_suf(&s4o);
       
  1351         f_decl->accept(overloaded_func_suf);
       
  1352       }
       
  1353     }  
  1281     if (function_type_suffix != NULL)
  1354     if (function_type_suffix != NULL)
  1282       function_type_suffix->accept(*this);
  1355       function_type_suffix->accept(*this);
  1283   }
  1356   }
  1284   s4o.print("(");
  1357   s4o.print("(");
  1285   s4o.indent_right();
  1358   s4o.indent_right();
  1286   
  1359   
  1287   int nb_param = 0;
  1360   int nb_param = 0;
  1288   PARAM_LIST_ITERATOR() {
  1361   PARAM_LIST_ITERATOR() {
  1289 	symbol_c *param_value = PARAM_VALUE;
  1362     symbol_c *param_value = PARAM_VALUE;
  1290 	current_param_type = PARAM_TYPE;
  1363     current_param_type = PARAM_TYPE;
  1291     
       
  1292     switch (PARAM_DIRECTION) {
  1364     switch (PARAM_DIRECTION) {
  1293       case function_param_iterator_c::direction_in:
  1365       case function_param_iterator_c::direction_in:
  1294     	if (nb_param > 0)
  1366         if (nb_param > 0)
  1295     	  s4o.print(",\n"+s4o.indent_spaces);
  1367           s4o.print(",\n"+s4o.indent_spaces);
  1296     	if (param_value == NULL) {
  1368         if (param_value == NULL) {
  1297           /* If not, get the default value of this variable's type */
  1369           /* If not, get the default value of this variable's type */
  1298           param_value = (symbol_c *)current_param_type->accept(*type_initial_value_c::instance());
  1370           param_value = (symbol_c *)current_param_type->accept(*type_initial_value_c::instance());
  1299         }
  1371         }
  1300         if (param_value == NULL) ERROR;
  1372         if (param_value == NULL) ERROR;
  1301         s4o.print("(");
  1373         s4o.print("(");
  1305           search_expression_type->lreal_type_name.accept(*this);
  1377           search_expression_type->lreal_type_name.accept(*this);
  1306         else
  1378         else
  1307           current_param_type->accept(*this);
  1379           current_param_type->accept(*this);
  1308         s4o.print(")");
  1380         s4o.print(")");
  1309         print_check_function(current_param_type, param_value);
  1381         print_check_function(current_param_type, param_value);
  1310 		nb_param++;
  1382         nb_param++;
  1311         break;
  1383         break;
  1312       case function_param_iterator_c::direction_out:
  1384       case function_param_iterator_c::direction_out:
  1313       case function_param_iterator_c::direction_inout:
  1385       case function_param_iterator_c::direction_inout:
  1314     	if (!has_output_params) {
  1386         if (!has_output_params) {
  1315           if (nb_param > 0)
  1387           if (nb_param > 0)
  1316         	s4o.print(",\n"+s4o.indent_spaces);
  1388             s4o.print(",\n"+s4o.indent_spaces);
  1317 		  if (param_value == NULL) {
  1389           if (param_value == NULL) {
  1318 		    s4o.print("NULL");
  1390             s4o.print("NULL");
  1319 		  } else {
  1391           } else {
  1320 		    wanted_variablegeneration = fparam_output_vg;
  1392             wanted_variablegeneration = fparam_output_vg;
  1321 		    param_value->accept(*this);
  1393             param_value->accept(*this);
  1322 		    wanted_variablegeneration = expression_vg;
  1394             wanted_variablegeneration = expression_vg;
  1323 		  }
  1395           }
  1324     	}
  1396         }
  1325 	    break;
  1397         break;
  1326       case function_param_iterator_c::direction_extref:
  1398       case function_param_iterator_c::direction_extref:
  1327         /* TODO! */
  1399         /* TODO! */
  1328         ERROR;
  1400         ERROR;
  1329 	      break;
  1401         break;
  1330     } /* switch */
  1402     } /* switch */
  1331   } /* for(...) */
  1403   } /* for(...) */
  1332   if (has_output_params) {
  1404   if (has_output_params) {
  1333     if (nb_param > 0)
  1405     if (nb_param > 0)
  1334       s4o.print(",\n"+s4o.indent_spaces);
  1406       s4o.print(",\n"+s4o.indent_spaces);
  1657 }
  1729 }
  1658 
  1730 
  1659 void *visit(ADD_operator_c *symbol)	{
  1731 void *visit(ADD_operator_c *symbol)	{
  1660   if (search_expression_type->is_time_type(this->default_variable_name.current_type) &&
  1732   if (search_expression_type->is_time_type(this->default_variable_name.current_type) &&
  1661       search_expression_type->is_time_type(this->current_operand_type)) {
  1733       search_expression_type->is_time_type(this->current_operand_type)) {
  1662     XXX_function("__TIME_ADD", &(this->default_variable_name), this->current_operand);
  1734     XXX_function("__time_add", &(this->default_variable_name), this->current_operand);
  1663     /* the data type resulting from this operation... */
  1735     /* the data type resulting from this operation... */
  1664     this->default_variable_name.current_type = this->current_operand_type;
  1736     this->default_variable_name.current_type = this->current_operand_type;
  1665     return NULL;
  1737     return NULL;
  1666   }
  1738   }
  1667   if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
  1739   if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
  1677 }
  1749 }
  1678 
  1750 
  1679 void *visit(SUB_operator_c *symbol)	{
  1751 void *visit(SUB_operator_c *symbol)	{
  1680   if (search_expression_type->is_time_type(this->default_variable_name.current_type) &&
  1752   if (search_expression_type->is_time_type(this->default_variable_name.current_type) &&
  1681       search_expression_type->is_time_type(this->current_operand_type)) {
  1753       search_expression_type->is_time_type(this->current_operand_type)) {
  1682     XXX_function("__TIME_SUB", &(this->default_variable_name), this->current_operand);
  1754     XXX_function("__time_sub", &(this->default_variable_name), this->current_operand);
  1683     /* the data type resulting from this operation... */
  1755     /* the data type resulting from this operation... */
  1684     this->default_variable_name.current_type = this->current_operand_type;
  1756     this->default_variable_name.current_type = this->current_operand_type;
  1685     return NULL;
  1757     return NULL;
  1686   }
  1758   }
  1687   if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
  1759   if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
  1697 }
  1769 }
  1698 
  1770 
  1699 void *visit(MUL_operator_c *symbol)	{
  1771 void *visit(MUL_operator_c *symbol)	{
  1700   if (search_expression_type->is_time_type(this->default_variable_name.current_type) &&
  1772   if (search_expression_type->is_time_type(this->default_variable_name.current_type) &&
  1701       search_expression_type->is_integer_type(this->current_operand_type)) {
  1773       search_expression_type->is_integer_type(this->current_operand_type)) {
  1702     XXX_function("__TIME_MUL", &(this->default_variable_name), this->current_operand);
  1774     XXX_function("__time_mul", &(this->default_variable_name), this->current_operand);
  1703     /* the data type resulting from this operation... */
  1775     /* the data type resulting from this operation... */
  1704     return NULL;
  1776     return NULL;
  1705   }
  1777   }
  1706   if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
  1778   if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
  1707       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
  1779       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
  1716 }
  1788 }
  1717 
  1789 
  1718 void *visit(DIV_operator_c *symbol)	{
  1790 void *visit(DIV_operator_c *symbol)	{
  1719   if (search_expression_type->is_time_type(this->default_variable_name.current_type) &&
  1791   if (search_expression_type->is_time_type(this->default_variable_name.current_type) &&
  1720       search_expression_type->is_integer_type(this->current_operand_type)) {
  1792       search_expression_type->is_integer_type(this->current_operand_type)) {
  1721     XXX_function("__TIME_DIV", &(this->default_variable_name), this->current_operand);
  1793     XXX_function("__time_div", &(this->default_variable_name), this->current_operand);
  1722     /* the data type resulting from this operation... */
  1794     /* the data type resulting from this operation... */
  1723     return NULL;
  1795     return NULL;
  1724   }
  1796   }
  1725   if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
  1797   if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
  1726       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
  1798       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
  1746 }
  1818 }
  1747 
  1819 
  1748 void *visit(GT_operator_c *symbol)	{
  1820 void *visit(GT_operator_c *symbol)	{
  1749   if (!search_base_type.type_is_enumerated(this->default_variable_name.current_type) &&
  1821   if (!search_base_type.type_is_enumerated(this->default_variable_name.current_type) &&
  1750       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type))
  1822       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type))
  1751     return CMP_operator(this->current_operand, "__gt_");
  1823     return CMP_operator(this->current_operand, "GT_");
  1752   ERROR;
  1824   ERROR;
  1753   return NULL;
  1825   return NULL;
  1754 }
  1826 }
  1755 
  1827 
  1756 void *visit(GE_operator_c *symbol)	{
  1828 void *visit(GE_operator_c *symbol)	{
  1757   if (!search_base_type.type_is_enumerated(this->default_variable_name.current_type) &&
  1829   if (!search_base_type.type_is_enumerated(this->default_variable_name.current_type) &&
  1758       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type))
  1830       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type))
  1759     return CMP_operator(this->current_operand, "__ge_");
  1831     return CMP_operator(this->current_operand, "GE_");
  1760   ERROR;
  1832   ERROR;
  1761   return NULL;
  1833   return NULL;
  1762 }
  1834 }
  1763 
  1835 
  1764 void *visit(EQ_operator_c *symbol)	{
  1836 void *visit(EQ_operator_c *symbol)	{
  1765   if (search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type))
  1837   if (search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type))
  1766     return CMP_operator(this->current_operand, "__eq_");
  1838     return CMP_operator(this->current_operand, "EQ_");
  1767   ERROR;
  1839   ERROR;
  1768   return NULL;
  1840   return NULL;
  1769 }
  1841 }
  1770 
  1842 
  1771 void *visit(LT_operator_c *symbol)	{
  1843 void *visit(LT_operator_c *symbol)	{
  1772   if (!search_base_type.type_is_enumerated(this->default_variable_name.current_type) &&
  1844   if (!search_base_type.type_is_enumerated(this->default_variable_name.current_type) &&
  1773       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type))
  1845       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type))
  1774     return CMP_operator(this->current_operand, "__lt_");
  1846     return CMP_operator(this->current_operand, "LT_");
  1775   ERROR;
  1847   ERROR;
  1776   return NULL;
  1848   return NULL;
  1777 }
  1849 }
  1778 
  1850 
  1779 void *visit(LE_operator_c *symbol)	{
  1851 void *visit(LE_operator_c *symbol)	{
  1780   if (!search_base_type.type_is_enumerated(this->default_variable_name.current_type) &&
  1852   if (!search_base_type.type_is_enumerated(this->default_variable_name.current_type) &&
  1781       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type))
  1853       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type))
  1782     return CMP_operator(this->current_operand, "__le_");
  1854     return CMP_operator(this->current_operand, "LE_");
  1783   ERROR;
  1855   ERROR;
  1784   return NULL;
  1856   return NULL;
  1785 }
  1857 }
  1786 
  1858 
  1787 void *visit(NE_operator_c *symbol)	{
  1859 void *visit(NE_operator_c *symbol)	{
  1788   if (search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type))
  1860   if (search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type))
  1789     return CMP_operator(this->current_operand, "__ne_");
  1861     return CMP_operator(this->current_operand, "NE_");
  1790   ERROR;
  1862   ERROR;
  1791   return NULL;
  1863   return NULL;
  1792 }
  1864 }
  1793 
  1865 
  1794 
  1866