stage3/visit_expression_type.cc
changeset 406 6381589697ff
parent 399 55b074ea7255
child 407 2d77f0f77773
equal deleted inserted replaced
405:7b5d67d1aeef 406:6381589697ff
   582   return NULL;
   582   return NULL;
   583 }
   583 }
   584 
   584 
   585 symbol_c *visit_expression_type_c::overloaded_return_type(symbol_c *type) {
   585 symbol_c *visit_expression_type_c::overloaded_return_type(symbol_c *type) {
   586   if (is_ANY_INT_compatible(type))
   586   if (is_ANY_INT_compatible(type))
   587 	return &search_constant_type_c::ulint_type_name;
   587     return &search_constant_type_c::ulint_type_name;
   588   else if (is_ANY_REAL_compatible(type))
   588   else if (is_ANY_REAL_compatible(type))
   589 	return &search_constant_type_c::lreal_type_name;
   589     return &search_constant_type_c::lreal_type_name;
   590   else if (is_ANY_BIT_compatible(type))
   590   else if (is_ANY_BIT_compatible(type))
   591   	return &search_constant_type_c::lword_type_name;
   591       return &search_constant_type_c::lword_type_name;
   592   return NULL;
   592   return NULL;
   593 }
   593 }
   594 
   594 
   595 /* 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
   596  * such as: 
   596  * such as: 
  1066   if (il_error)
  1066   if (il_error)
  1067     return NULL;
  1067     return NULL;
  1068 
  1068 
  1069   symbol_c *return_data_type = NULL;
  1069   symbol_c *return_data_type = NULL;
  1070   symbol_c* fdecl_return_type;
  1070   symbol_c* fdecl_return_type;
       
  1071   symbol_c* overloaded_data_type = NULL;
  1071   symbol->called_function_declaration = NULL;
  1072   symbol->called_function_declaration = NULL;
  1072 
  1073 
  1073   /* First find the declaration of the function being called! */
  1074   /* First find the declaration of the function being called! */
  1074   function_symtable_t::iterator lower = function_symtable.lower_bound(symbol->function_name);
  1075   function_symtable_t::iterator lower = function_symtable.lower_bound(symbol->function_name);
  1075   function_symtable_t::iterator upper = function_symtable.upper_bound(symbol->function_name);
  1076   function_symtable_t::iterator upper = function_symtable.upper_bound(symbol->function_name);
       
  1077   function_symtable_t::iterator current;
  1076   if (lower == function_symtable.end()) ERROR;
  1078   if (lower == function_symtable.end()) ERROR;
  1077 
  1079 
  1078   int error_count = 0; 
  1080   int error_count = 0; 
  1079   int *error_count_ptr = NULL;
  1081   int *error_count_ptr = NULL;
  1080 
  1082 
  1082   second++;
  1084   second++;
  1083   if (second != upper) 
  1085   if (second != upper) 
  1084     /* This is a call to an overloaded function... */  
  1086     /* This is a call to an overloaded function... */  
  1085     error_count_ptr = &error_count;
  1087     error_count_ptr = &error_count;
  1086 
  1088 
  1087   for(; lower != upper; lower++) {
  1089   for(current = lower; current != upper; current++) {
  1088     function_declaration_c *f_decl = function_symtable.get_value(lower);
  1090     function_declaration_c *f_decl = function_symtable.get_value(current);
  1089     
  1091     
  1090     check_nonformal_call(symbol, f_decl, true, error_count_ptr);
  1092     check_nonformal_call(symbol, f_decl, true, error_count_ptr);
  1091     
  1093     
  1092     if (0 == error_count) {
  1094     if (0 == error_count) {
  1093       /* Either: 
  1095       /* Either: 
  1096        */
  1098        */
  1097 
  1099 
  1098       fdecl_return_type = base_type(f_decl->type_name);
  1100       fdecl_return_type = base_type(f_decl->type_name);
  1099 
  1101 
  1100       if (symbol->called_function_declaration == NULL) {
  1102       if (symbol->called_function_declaration == NULL) {
  1101       	/* Store the pointer to the declaration of the function being called.
  1103           /* 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.
  1104            * This data will be used by stage 4 to call the correct function.
  1103       	 * Mostly needed to disambiguate overloaded functions...
  1105            * Mostly needed to disambiguate overloaded functions...
  1104       	 * See comments in absyntax.def for more details
  1106            * See comments in absyntax.def for more details
  1105          */
  1107          */
  1106         symbol->called_function_declaration = f_decl;
  1108         symbol->called_function_declaration = f_decl;
  1107   		symbol->overloaded_return_type = NULL;
  1109 
  1108 
  1110           /* determine the base data type returned by the function being called... */
  1109   		/* determine the base data type returned by the function being called... */
  1111           return_data_type = fdecl_return_type;
  1110   	    return_data_type = fdecl_return_type;
  1112         }
  1111   	  }
  1113         else if (typeid(*return_data_type) != typeid(*fdecl_return_type)){
  1112   	  else if (typeid(*return_data_type) != typeid(*fdecl_return_type)){
  1114           return_data_type = common_literal(return_data_type, fdecl_return_type);
  1113   		if (symbol->overloaded_return_type == NULL)
  1115           overloaded_data_type = overloaded_return_type(return_data_type);
  1114   		  symbol->overloaded_return_type = overloaded_return_type(return_data_type);
  1116         }
  1115   		return_data_type = common_literal(return_data_type, fdecl_return_type);
       
  1116   	  }
       
  1117       
  1117       
  1118       if (NULL == return_data_type) ERROR;
  1118       if (NULL == return_data_type) ERROR;
       
  1119     }
       
  1120   }
       
  1121 
       
  1122   if (overloaded_data_type != NULL) {
       
  1123     for(current = lower; current != upper; current++) {
       
  1124       function_declaration_c *f_decl = function_symtable.get_value(current);
       
  1125 
       
  1126       /* check semantics of data passed in the function call... */
       
  1127       check_nonformal_call(symbol, f_decl, true, error_count_ptr);
       
  1128 
       
  1129       if (0 == error_count) {
       
  1130 
       
  1131         fdecl_return_type = base_type(f_decl->type_name);
       
  1132 
       
  1133         if (typeid(*overloaded_data_type) == typeid(*fdecl_return_type)){
       
  1134           /* Store the pointer to the declaration of the function being called.
       
  1135            * This data will be used by stage 4 to call the correct function.
       
  1136            * Mostly needed to disambiguate overloaded functions...
       
  1137            * See comments in absyntax.def for more details
       
  1138            */
       
  1139           symbol->called_function_declaration = f_decl;
       
  1140         }
       
  1141       }
  1119     }
  1142     }
  1120   }
  1143   }
  1121 
  1144 
  1122   if (NULL == return_data_type) {
  1145   if (NULL == return_data_type) {
  1123     /* No compatible function was found for this function call */
  1146     /* No compatible function was found for this function call */
  1247   if (il_error)
  1270   if (il_error)
  1248     return NULL;
  1271     return NULL;
  1249 
  1272 
  1250   symbol_c *return_data_type = NULL;
  1273   symbol_c *return_data_type = NULL;
  1251   symbol_c* fdecl_return_type;
  1274   symbol_c* fdecl_return_type;
       
  1275   symbol_c *overloaded_data_type = NULL;
  1252   symbol->called_function_declaration = NULL;
  1276   symbol->called_function_declaration = NULL;
  1253 
  1277 
  1254   function_symtable_t::iterator lower = function_symtable.lower_bound(symbol->function_name);
  1278   function_symtable_t::iterator lower = function_symtable.lower_bound(symbol->function_name);
  1255   function_symtable_t::iterator upper = function_symtable.upper_bound(symbol->function_name);
  1279   function_symtable_t::iterator upper = function_symtable.upper_bound(symbol->function_name);
       
  1280   function_symtable_t::iterator current;
  1256   
  1281   
  1257   if (lower == function_symtable.end()) {
  1282   if (lower == function_symtable.end()) {
  1258     function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name);
  1283     function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name);
  1259     if (current_function_type == function_none) ERROR;
  1284     if (current_function_type == function_none) ERROR;
  1260     return NULL;
  1285     return NULL;
  1267   second++;
  1292   second++;
  1268   if (second != upper) 
  1293   if (second != upper) 
  1269     /* This is a call to an overloaded function... */  
  1294     /* This is a call to an overloaded function... */  
  1270     error_count_ptr = &error_count;
  1295     error_count_ptr = &error_count;
  1271 
  1296 
  1272   for(; lower != upper; lower++) {
  1297   for(current = lower; current != upper; current++) {
  1273     function_declaration_c *f_decl = function_symtable.get_value(lower);
  1298     function_declaration_c *f_decl = function_symtable.get_value(current);
  1274   
  1299   
  1275     /* check semantics of data passed in the function call... */
  1300     /* check semantics of data passed in the function call... */
  1276     check_formal_call(symbol, f_decl, error_count_ptr);
  1301     check_formal_call(symbol, f_decl, error_count_ptr);
  1277 
  1302 
  1278     if (0 == error_count) {
  1303     if (0 == error_count) {
  1282        */
  1307        */
  1283 
  1308 
  1284       fdecl_return_type = base_type(f_decl->type_name);
  1309       fdecl_return_type = base_type(f_decl->type_name);
  1285 
  1310 
  1286       if (symbol->called_function_declaration == NULL) {
  1311       if (symbol->called_function_declaration == NULL) {
  1287     	/* Store the pointer to the declaration of the function being called.
  1312         /* Store the pointer to the declaration of the function being called.
  1288     	 * This data will be used by stage 4 to call the correct function.
  1313          * This data will be used by stage 4 to call the correct function.
  1289     	 * Mostly needed to disambiguate overloaded functions...
  1314          * Mostly needed to disambiguate overloaded functions...
  1290     	 * See comments in absyntax.def for more details
  1315          * See comments in absyntax.def for more details
  1291          */
  1316          */
  1292         symbol->called_function_declaration = f_decl;
  1317         symbol->called_function_declaration = f_decl;
  1293 		symbol->overloaded_return_type = NULL;
  1318 
  1294 
  1319         /* determine the base data type returned by the function being called... */
  1295 		/* determine the base data type returned by the function being called... */
  1320         return_data_type = fdecl_return_type;
  1296 		return_data_type = fdecl_return_type;
  1321       }
  1297 	  }
  1322       else if (typeid(*return_data_type) != typeid(*fdecl_return_type)){
  1298 	  else if (typeid(*return_data_type) != typeid(*fdecl_return_type)){
  1323         return_data_type = common_literal(return_data_type, fdecl_return_type);
  1299 		if (symbol->overloaded_return_type == NULL)
  1324         overloaded_data_type = overloaded_return_type(return_data_type);
  1300 		  symbol->overloaded_return_type = overloaded_return_type(return_data_type);
  1325       }
  1301 		return_data_type = common_literal(return_data_type, fdecl_return_type);
       
  1302 	  }
       
  1303 
  1326 
  1304       /* the following should never occur. If it does, then we have a bug in the syntax parser (stage 2)... */
  1327       /* the following should never occur. If it does, then we have a bug in the syntax parser (stage 2)... */
  1305       if (NULL == return_data_type) ERROR;
  1328       if (NULL == return_data_type) ERROR;
  1306 
  1329 
  1307     }
  1330     }
  1308   }
  1331   }
  1309   
  1332   
       
  1333   if (overloaded_data_type != NULL) {
       
  1334     for(current = lower; current != upper; current++) {
       
  1335       function_declaration_c *f_decl = function_symtable.get_value(current);
       
  1336 
       
  1337       /* check semantics of data passed in the function call... */
       
  1338       check_formal_call(symbol, f_decl, error_count_ptr);
       
  1339 
       
  1340       if (0 == error_count) {
       
  1341 
       
  1342         fdecl_return_type = base_type(f_decl->type_name);
       
  1343 
       
  1344         if (typeid(*overloaded_data_type) == typeid(*fdecl_return_type)){
       
  1345           /* Store the pointer to the declaration of the function being called.
       
  1346            * This data will be used by stage 4 to call the correct function.
       
  1347            * Mostly needed to disambiguate overloaded functions...
       
  1348            * See comments in absyntax.def for more details
       
  1349            */
       
  1350           symbol->called_function_declaration = f_decl;
       
  1351         }
       
  1352       }
       
  1353     }
       
  1354   }
       
  1355 
  1310   if (NULL == return_data_type) {
  1356   if (NULL == return_data_type) {
  1311 	/* No compatible function was found for this function call */
  1357     /* No compatible function was found for this function call */
  1312 	STAGE3_ERROR(symbol, symbol, "Call to an overloaded function with invalid parameter type.");
  1358     STAGE3_ERROR(symbol, symbol, "Call to an overloaded function with invalid parameter type.");
  1313   }
  1359   }
  1314   else {
  1360   else {
  1315     /* the data type of the data returned by the function, and stored in the il default variable... */
  1361     /* 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;
  1362     il_default_variable_type = return_data_type;
  1317   }
  1363   }
  2065 
  2111 
  2066 
  2112 
  2067 void *visit_expression_type_c::visit(function_invocation_c *symbol) {
  2113 void *visit_expression_type_c::visit(function_invocation_c *symbol) {
  2068   function_symtable_t::iterator lower = function_symtable.lower_bound(symbol->function_name);
  2114   function_symtable_t::iterator lower = function_symtable.lower_bound(symbol->function_name);
  2069   function_symtable_t::iterator upper = function_symtable.upper_bound(symbol->function_name);
  2115   function_symtable_t::iterator upper = function_symtable.upper_bound(symbol->function_name);
       
  2116   function_symtable_t::iterator current;
  2070   if (lower == function_symtable.end()) ERROR;
  2117   if (lower == function_symtable.end()) ERROR;
  2071 
  2118 
  2072   symbol_c* return_data_type;
  2119   symbol_c* return_data_type;
  2073   symbol_c* fdecl_return_type;
  2120   symbol_c* fdecl_return_type;
       
  2121   symbol_c* overloaded_data_type = NULL;
  2074   symbol->called_function_declaration = NULL;
  2122   symbol->called_function_declaration = NULL;
  2075 
  2123 
  2076   function_symtable_t::iterator second = lower;
  2124   function_symtable_t::iterator second = lower;
  2077   second++;
  2125   second++;
  2078   if (second == upper) {
  2126   if (second == upper) {
  2079     /* call to a function that is not overloaded. */	  
  2127     /* call to a function that is not overloaded. */
  2080     /* now check the semantics of the function call... */
  2128     /* now check the semantics of the function call... */
  2081     /* If the syntax parser is working correctly, exactly one of the 
  2129     /* If the syntax parser is working correctly, exactly one of the 
  2082      * following two symbols will be NULL, while the other is != NULL.
  2130      * following two symbols will be NULL, while the other is != NULL.
  2083      */
  2131      */
  2084     function_declaration_c *f_decl = function_symtable.get_value(lower);
  2132     function_declaration_c *f_decl = function_symtable.get_value(lower);
  2088      * This data will be used by stage 4 to call the correct function.
  2136      * This data will be used by stage 4 to call the correct function.
  2089      * Mostly needed to disambiguate overloaded functions...
  2137      * Mostly needed to disambiguate overloaded functions...
  2090      * See comments in absyntax.def for more details
  2138      * See comments in absyntax.def for more details
  2091      */
  2139      */
  2092     symbol->called_function_declaration = f_decl;
  2140     symbol->called_function_declaration = f_decl;
  2093     symbol->overloaded_return_type = NULL;
       
  2094     return base_type(f_decl->type_name);
  2141     return base_type(f_decl->type_name);
  2095   }  
  2142   }  
  2096 
  2143 
  2097   /* This is a call to an overloaded function... */
  2144   /* This is a call to an overloaded function... */
  2098   if (debug) printf("visit_expression_type_c::visit(function_invocation_c *symbol): FOUND CALL TO OVERLOADED FUNCTION!!\n");
  2145   if (debug) printf("visit_expression_type_c::visit(function_invocation_c *symbol): FOUND CALL TO OVERLOADED FUNCTION!!\n");
  2099   for(; lower != upper; lower++) {
  2146   for(current = lower; current != upper; current++) {
  2100     if (debug) printf("visit_expression_type_c::visit(function_invocation_c *symbol): FOUND CALL TO OVERLOADED FUNCTION!! iterating...\n");
  2147     if (debug) printf("visit_expression_type_c::visit(function_invocation_c *symbol): FOUND CALL TO OVERLOADED FUNCTION!! iterating...\n");
  2101     int error_count = 0; 
  2148     int error_count = 0; 
  2102     function_declaration_c *f_decl = function_symtable.get_value(lower);
  2149     function_declaration_c *f_decl = function_symtable.get_value(current);
  2103     if (symbol->   formal_param_list != NULL) check_formal_call   (symbol, f_decl, &error_count);
  2150     if (symbol->   formal_param_list != NULL) check_formal_call   (symbol, f_decl, &error_count);
  2104     if (symbol->nonformal_param_list != NULL) check_nonformal_call(symbol, f_decl, false, &error_count);
  2151     if (symbol->nonformal_param_list != NULL) check_nonformal_call(symbol, f_decl, false, &error_count);
  2105     if (0 == error_count) {
  2152     if (0 == error_count) {
  2106 
  2153 
  2107       fdecl_return_type = base_type(f_decl->type_name);
  2154       fdecl_return_type = base_type(f_decl->type_name);
  2111          * This data will be used by stage 4 to call the correct function.
  2158          * This data will be used by stage 4 to call the correct function.
  2112          * Mostly needed to disambiguate overloaded functions...
  2159          * Mostly needed to disambiguate overloaded functions...
  2113          * See comments in absyntax.def for more details
  2160          * See comments in absyntax.def for more details
  2114          */
  2161          */
  2115         symbol->called_function_declaration = f_decl;
  2162         symbol->called_function_declaration = f_decl;
  2116         symbol->overloaded_return_type = NULL;
       
  2117 
  2163 
  2118         /* determine the base data type returned by the function being called... */
  2164         /* determine the base data type returned by the function being called... */
  2119         return_data_type = fdecl_return_type;
  2165         return_data_type = fdecl_return_type;
  2120       }
  2166       }
  2121       else if (typeid(*return_data_type) != typeid(*fdecl_return_type)){
  2167       else if (typeid(*return_data_type) != typeid(*fdecl_return_type)){
  2122       	if (symbol->overloaded_return_type == NULL)
  2168           return_data_type = common_literal(return_data_type, fdecl_return_type);
  2123       	  symbol->overloaded_return_type = overloaded_return_type(return_data_type);
  2169           overloaded_data_type = overloaded_return_type(return_data_type);
  2124       	return_data_type = common_literal(return_data_type, fdecl_return_type);
       
  2125       }
  2170       }
  2126 
  2171 
  2127       if (NULL == return_data_type) ERROR;
  2172       if (NULL == return_data_type) ERROR;
  2128     }
  2173     }
  2129   }
  2174   }
  2130 
  2175 
       
  2176   if (overloaded_data_type != NULL) {
       
  2177     for(current = lower; current != upper; current++) {
       
  2178       function_declaration_c *f_decl = function_symtable.get_value(current);
       
  2179       int error_count = 0;
       
  2180       if (symbol->   formal_param_list != NULL) check_formal_call   (symbol, f_decl, &error_count);
       
  2181       if (symbol->nonformal_param_list != NULL) check_nonformal_call(symbol, f_decl, false, &error_count);
       
  2182       if (0 == error_count) {
       
  2183 
       
  2184         fdecl_return_type = base_type(f_decl->type_name);
       
  2185 
       
  2186         if (typeid(*overloaded_data_type) == typeid(*fdecl_return_type)){
       
  2187           /* Store the pointer to the declaration of the function being called.
       
  2188            * This data will be used by stage 4 to call the correct function.
       
  2189            * Mostly needed to disambiguate overloaded functions...
       
  2190            * See comments in absyntax.def for more details
       
  2191            */
       
  2192           symbol->called_function_declaration = f_decl;
       
  2193         }
       
  2194       }
       
  2195     }
       
  2196   }
       
  2197 
  2131   if (return_data_type != NULL)
  2198   if (return_data_type != NULL)
  2132 	return return_data_type;
  2199     return return_data_type;
  2133 
  2200 
  2134   /* No compatible function was found for this function call */
  2201   /* No compatible function was found for this function call */
  2135   STAGE3_ERROR(symbol, symbol, "Call to an overloaded function with invalid parameter type.");
  2202   STAGE3_ERROR(symbol, symbol, "Call to an overloaded function with invalid parameter type.");
  2136   return NULL;
  2203   return NULL;
  2137 }
  2204 }