stage3/visit_expression_type.cc
changeset 399 55b074ea7255
parent 390 9cf96d45853d
child 406 6381589697ff
equal deleted inserted replaced
398:f1f3facc59e7 399:55b074ea7255
   564   return res;
   564   return res;
   565 */
   565 */
   566   return common_type__(first_type, second_type);
   566   return common_type__(first_type, second_type);
   567 }
   567 }
   568 
   568 
       
   569 symbol_c *visit_expression_type_c::common_literal(symbol_c *first_type, symbol_c *second_type) {
       
   570   printf("common_literal: %d == %d, %d == %d, %d == %d\n",
       
   571 		 (int)is_ANY_INT_compatible(first_type),
       
   572 		 (int)is_ANY_INT_compatible(second_type),
       
   573 		 (int)is_ANY_REAL_compatible(first_type),
       
   574 		 (int)is_ANY_REAL_compatible(second_type),
       
   575 		 (int)is_ANY_BIT_compatible(first_type),
       
   576 		 (int)is_ANY_BIT_compatible(second_type));
       
   577   if ((is_ANY_INT_compatible(first_type) && is_ANY_INT_compatible(second_type)) ||
       
   578 	  (is_ANY_BIT_compatible(first_type) && is_ANY_BIT_compatible(second_type)))
       
   579 	 return &search_constant_type_c::integer;
       
   580   else if (is_ANY_REAL_compatible(first_type) && is_ANY_REAL_compatible(second_type))
       
   581 	 return &search_constant_type_c::real;
       
   582   return NULL;
       
   583 }
       
   584 
       
   585 symbol_c *visit_expression_type_c::overloaded_return_type(symbol_c *type) {
       
   586   if (is_ANY_INT_compatible(type))
       
   587 	return &search_constant_type_c::ulint_type_name;
       
   588   else if (is_ANY_REAL_compatible(type))
       
   589 	return &search_constant_type_c::lreal_type_name;
       
   590   else if (is_ANY_BIT_compatible(type))
       
   591   	return &search_constant_type_c::lword_type_name;
       
   592   return NULL;
       
   593 }
   569 
   594 
   570 /* Return TRUE if the second (value) data type may be assigned to a variable of the first (variable) data type
   595 /* Return TRUE if the second (value) data type may be assigned to a variable of the first (variable) data type
   571  * such as: 
   596  * such as: 
   572  *     var_type     value_type
   597  *     var_type     value_type
   573  *    BOOL           BYTE#7     -> returns false
   598  *    BOOL           BYTE#7     -> returns false
  1040 void *visit_expression_type_c::visit(il_function_call_c *symbol) {
  1065 void *visit_expression_type_c::visit(il_function_call_c *symbol) {
  1041   if (il_error)
  1066   if (il_error)
  1042     return NULL;
  1067     return NULL;
  1043 
  1068 
  1044   symbol_c *return_data_type = NULL;
  1069   symbol_c *return_data_type = NULL;
       
  1070   symbol_c* fdecl_return_type;
       
  1071   symbol->called_function_declaration = NULL;
  1045 
  1072 
  1046   /* First find the declaration of the function being called! */
  1073   /* First find the declaration of the function being called! */
  1047   function_symtable_t::iterator lower = function_symtable.lower_bound(symbol->function_name);
  1074   function_symtable_t::iterator lower = function_symtable.lower_bound(symbol->function_name);
  1048   function_symtable_t::iterator upper = function_symtable.upper_bound(symbol->function_name);
  1075   function_symtable_t::iterator upper = function_symtable.upper_bound(symbol->function_name);
  1049   if (lower == function_symtable.end()) ERROR;
  1076   if (lower == function_symtable.end()) ERROR;
  1065     if (0 == error_count) {
  1092     if (0 == error_count) {
  1066       /* Either: 
  1093       /* Either: 
  1067        * (i) we have a call to a non-overloaded function (error_cnt_ptr is NULL!, so error_count won't change!)  
  1094        * (i) we have a call to a non-overloaded function (error_cnt_ptr is NULL!, so error_count won't change!)  
  1068        * (ii) we have a call to an overloaded function, with no errors!
  1095        * (ii) we have a call to an overloaded function, with no errors!
  1069        */
  1096        */
       
  1097 
       
  1098       fdecl_return_type = base_type(f_decl->type_name);
       
  1099 
       
  1100       if (symbol->called_function_declaration == NULL) {
       
  1101       	/* Store the pointer to the declaration of the function being called.
       
  1102       	 * This data will be used by stage 4 to call the correct function.
       
  1103       	 * Mostly needed to disambiguate overloaded functions...
       
  1104       	 * See comments in absyntax.def for more details
       
  1105          */
       
  1106         symbol->called_function_declaration = f_decl;
       
  1107   		symbol->overloaded_return_type = NULL;
       
  1108 
       
  1109   		/* determine the base data type returned by the function being called... */
       
  1110   	    return_data_type = fdecl_return_type;
       
  1111   	  }
       
  1112   	  else if (typeid(*return_data_type) != typeid(*fdecl_return_type)){
       
  1113   		if (symbol->overloaded_return_type == NULL)
       
  1114   		  symbol->overloaded_return_type = overloaded_return_type(return_data_type);
       
  1115   		return_data_type = common_literal(return_data_type, fdecl_return_type);
       
  1116   	  }
  1070       
  1117       
  1071       /* Store the pointer to the declaration of the function being called.
       
  1072        * This data will be used by stage 4 to call the correct function.
       
  1073        * Mostly needed to disambiguate overloaded functions...
       
  1074        * See comments in absyntax.def for more details
       
  1075        */
       
  1076       symbol->called_function_declaration = f_decl;
       
  1077       /* determine the base data type returned by the function being called... */
       
  1078       return_data_type = base_type(f_decl->type_name);
       
  1079       /* If the following occurs, then we must have some big bug in the syntax parser (stage 2)... */
       
  1080       if (NULL == return_data_type) ERROR;
  1118       if (NULL == return_data_type) ERROR;
  1081       /* set the new data type of the default variable for the following verifications... */
       
  1082       il_default_variable_type = return_data_type;
       
  1083       return NULL;
       
  1084     }
  1119     }
  1085   }
  1120   }
  1086 
  1121 
  1087   /* No compatible function was found for this function call */
  1122   if (NULL == return_data_type) {
  1088   STAGE3_ERROR(symbol, symbol, "Call to an overloaded function with invalid parameter type.");
  1123     /* No compatible function was found for this function call */
       
  1124     STAGE3_ERROR(symbol, symbol, "Call to an overloaded function with invalid parameter type.");
       
  1125   }
       
  1126   else {
       
  1127     /* set the new data type of the default variable for the following verifications... */
       
  1128     il_default_variable_type = return_data_type;
       
  1129   }
       
  1130 
  1089   return NULL;
  1131   return NULL;
  1090 }
  1132 }
  1091 
  1133 
  1092 
  1134 
  1093 /* | il_expr_operator '(' [il_operand] eol_list [simple_instr_list] ')' */
  1135 /* | il_expr_operator '(' [il_operand] eol_list [simple_instr_list] ')' */
  1204 void *visit_expression_type_c::visit(il_formal_funct_call_c *symbol) {
  1246 void *visit_expression_type_c::visit(il_formal_funct_call_c *symbol) {
  1205   if (il_error)
  1247   if (il_error)
  1206     return NULL;
  1248     return NULL;
  1207 
  1249 
  1208   symbol_c *return_data_type = NULL;
  1250   symbol_c *return_data_type = NULL;
       
  1251   symbol_c* fdecl_return_type;
       
  1252   symbol->called_function_declaration = NULL;
       
  1253 
  1209   function_symtable_t::iterator lower = function_symtable.lower_bound(symbol->function_name);
  1254   function_symtable_t::iterator lower = function_symtable.lower_bound(symbol->function_name);
  1210   function_symtable_t::iterator upper = function_symtable.upper_bound(symbol->function_name);
  1255   function_symtable_t::iterator upper = function_symtable.upper_bound(symbol->function_name);
  1211   
  1256   
  1212   if (lower == function_symtable.end()) {
  1257   if (lower == function_symtable.end()) {
  1213     function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name);
  1258     function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name);
  1233     if (0 == error_count) {
  1278     if (0 == error_count) {
  1234       /* Either: 
  1279       /* Either: 
  1235        * (i) we have a call to a non-overloaded function (error_cnt_ptr is NULL!, so error_count won't change!)  
  1280        * (i) we have a call to a non-overloaded function (error_cnt_ptr is NULL!, so error_count won't change!)  
  1236        * (ii) we have a call to an overloaded function, with no errors!
  1281        * (ii) we have a call to an overloaded function, with no errors!
  1237        */
  1282        */
  1238       
  1283 
  1239       /* Store the pointer to the declaration of the function being called.
  1284       fdecl_return_type = base_type(f_decl->type_name);
  1240        * This data will be used by stage 4 to call the correct function.
  1285 
  1241        * Mostly needed to disambiguate overloaded functions...
  1286       if (symbol->called_function_declaration == NULL) {
  1242        * See comments in absyntax.def for more details
  1287     	/* Store the pointer to the declaration of the function being called.
  1243        */
  1288     	 * This data will be used by stage 4 to call the correct function.
  1244       symbol->called_function_declaration = f_decl;
  1289     	 * Mostly needed to disambiguate overloaded functions...
  1245       /* determine the base data type returned by the function being called... */
  1290     	 * See comments in absyntax.def for more details
  1246       return_data_type = base_type(f_decl->type_name);
  1291          */
       
  1292         symbol->called_function_declaration = f_decl;
       
  1293 		symbol->overloaded_return_type = NULL;
       
  1294 
       
  1295 		/* determine the base data type returned by the function being called... */
       
  1296 		return_data_type = fdecl_return_type;
       
  1297 	  }
       
  1298 	  else if (typeid(*return_data_type) != typeid(*fdecl_return_type)){
       
  1299 		if (symbol->overloaded_return_type == NULL)
       
  1300 		  symbol->overloaded_return_type = overloaded_return_type(return_data_type);
       
  1301 		return_data_type = common_literal(return_data_type, fdecl_return_type);
       
  1302 	  }
       
  1303 
  1247       /* the following should never occur. If it does, then we have a bug in the syntax parser (stage 2)... */
  1304       /* the following should never occur. If it does, then we have a bug in the syntax parser (stage 2)... */
  1248       if (NULL == return_data_type) ERROR;
  1305       if (NULL == return_data_type) ERROR;
  1249       /* the data type of the data returned by the function, and stored in the il default variable... */
  1306 
  1250       il_default_variable_type = return_data_type;
  1307     }
  1251       return NULL;
       
  1252     }  
       
  1253   }
  1308   }
  1254   
  1309   
  1255   /* No compatible function was found for this function call */
  1310   if (NULL == return_data_type) {
  1256   STAGE3_ERROR(symbol, symbol, "Call to an overloaded function with invalid parameter type.");
  1311 	/* No compatible function was found for this function call */
       
  1312 	STAGE3_ERROR(symbol, symbol, "Call to an overloaded function with invalid parameter type.");
       
  1313   }
       
  1314   else {
       
  1315     /* the data type of the data returned by the function, and stored in the il default variable... */
       
  1316     il_default_variable_type = return_data_type;
       
  1317   }
       
  1318 
  1257   return NULL;
  1319   return NULL;
  1258 }
  1320 }
  1259 
  1321 
  1260 
  1322 
  1261 #if 0
  1323 #if 0
  2004 
  2066 
  2005 void *visit_expression_type_c::visit(function_invocation_c *symbol) {
  2067 void *visit_expression_type_c::visit(function_invocation_c *symbol) {
  2006   function_symtable_t::iterator lower = function_symtable.lower_bound(symbol->function_name);
  2068   function_symtable_t::iterator lower = function_symtable.lower_bound(symbol->function_name);
  2007   function_symtable_t::iterator upper = function_symtable.upper_bound(symbol->function_name);
  2069   function_symtable_t::iterator upper = function_symtable.upper_bound(symbol->function_name);
  2008   if (lower == function_symtable.end()) ERROR;
  2070   if (lower == function_symtable.end()) ERROR;
       
  2071 
       
  2072   symbol_c* return_data_type;
       
  2073   symbol_c* fdecl_return_type;
       
  2074   symbol->called_function_declaration = NULL;
  2009 
  2075 
  2010   function_symtable_t::iterator second = lower;
  2076   function_symtable_t::iterator second = lower;
  2011   second++;
  2077   second++;
  2012   if (second == upper) {
  2078   if (second == upper) {
  2013     /* call to a function that is not overloaded. */	  
  2079     /* call to a function that is not overloaded. */	  
  2022      * This data will be used by stage 4 to call the correct function.
  2088      * This data will be used by stage 4 to call the correct function.
  2023      * Mostly needed to disambiguate overloaded functions...
  2089      * Mostly needed to disambiguate overloaded functions...
  2024      * See comments in absyntax.def for more details
  2090      * See comments in absyntax.def for more details
  2025      */
  2091      */
  2026     symbol->called_function_declaration = f_decl;
  2092     symbol->called_function_declaration = f_decl;
       
  2093     symbol->overloaded_return_type = NULL;
  2027     return base_type(f_decl->type_name);
  2094     return base_type(f_decl->type_name);
  2028   }  
  2095   }  
  2029 
  2096 
  2030   /* This is a call to an overloaded function... */
  2097   /* This is a call to an overloaded function... */
  2031   if (debug) printf("visit_expression_type_c::visit(function_invocation_c *symbol): FOUND CALL TO OVERLOADED FUNCTION!!\n");
  2098   if (debug) printf("visit_expression_type_c::visit(function_invocation_c *symbol): FOUND CALL TO OVERLOADED FUNCTION!!\n");
  2034     int error_count = 0; 
  2101     int error_count = 0; 
  2035     function_declaration_c *f_decl = function_symtable.get_value(lower);
  2102     function_declaration_c *f_decl = function_symtable.get_value(lower);
  2036     if (symbol->   formal_param_list != NULL) check_formal_call   (symbol, f_decl, &error_count);
  2103     if (symbol->   formal_param_list != NULL) check_formal_call   (symbol, f_decl, &error_count);
  2037     if (symbol->nonformal_param_list != NULL) check_nonformal_call(symbol, f_decl, false, &error_count);
  2104     if (symbol->nonformal_param_list != NULL) check_nonformal_call(symbol, f_decl, false, &error_count);
  2038     if (0 == error_count) {
  2105     if (0 == error_count) {
  2039       /* Store the pointer to the declaration of the function being called.
  2106 
  2040        * This data will be used by stage 4 to call the correct function.
  2107       fdecl_return_type = base_type(f_decl->type_name);
  2041        * Mostly needed to disambiguate overloaded functions...
  2108 
  2042        * See comments in absyntax.def for more details
  2109       if (symbol->called_function_declaration == NULL) {
  2043        */
  2110         /* Store the pointer to the declaration of the function being called.
  2044       symbol->called_function_declaration = f_decl;
  2111          * This data will be used by stage 4 to call the correct function.
  2045       return base_type(f_decl->type_name);
  2112          * Mostly needed to disambiguate overloaded functions...
       
  2113          * See comments in absyntax.def for more details
       
  2114          */
       
  2115         symbol->called_function_declaration = f_decl;
       
  2116         symbol->overloaded_return_type = NULL;
       
  2117 
       
  2118         /* determine the base data type returned by the function being called... */
       
  2119         return_data_type = fdecl_return_type;
       
  2120       }
       
  2121       else if (typeid(*return_data_type) != typeid(*fdecl_return_type)){
       
  2122       	if (symbol->overloaded_return_type == NULL)
       
  2123       	  symbol->overloaded_return_type = overloaded_return_type(return_data_type);
       
  2124       	return_data_type = common_literal(return_data_type, fdecl_return_type);
       
  2125       }
       
  2126 
       
  2127       if (NULL == return_data_type) ERROR;
  2046     }
  2128     }
  2047   }
  2129   }
       
  2130 
       
  2131   if (return_data_type != NULL)
       
  2132 	return return_data_type;
  2048 
  2133 
  2049   /* No compatible function was found for this function call */
  2134   /* No compatible function was found for this function call */
  2050   STAGE3_ERROR(symbol, symbol, "Call to an overloaded function with invalid parameter type.");
  2135   STAGE3_ERROR(symbol, symbol, "Call to an overloaded function with invalid parameter type.");
  2051   return NULL;
  2136   return NULL;
  2052 }
  2137 }