stage3/fill_candidate_datatypes.cc
changeset 478 b13feab3b918
parent 476 38d85e9a0507
child 479 a174d61f2087
equal deleted inserted replaced
477:c0c6ff7357b7 478:b13feab3b918
    51 }
    51 }
    52 
    52 
    53 fill_candidate_datatypes_c::~fill_candidate_datatypes_c(void) {
    53 fill_candidate_datatypes_c::~fill_candidate_datatypes_c(void) {
    54 }
    54 }
    55 
    55 
    56 symbol_c *fill_candidate_datatypes_c::widening_conversion(symbol_c *left_type, symbol_c *right_type, const struct widen_entry widen_table[], bool &deprecated_operation) {
    56 symbol_c *fill_candidate_datatypes_c::widening_conversion(symbol_c *left_type, symbol_c *right_type, const struct widen_entry widen_table[]) {
    57 	int k;
    57 	int k;
    58 	/* find a widening table entry compatible */
    58 	/* find a widening table entry compatible */
    59 	for (k = 0; NULL != widen_table[k].left;  k++)
    59 	for (k = 0; NULL != widen_table[k].left;  k++)
    60 		if ((typeid(*left_type) == typeid(*widen_table[k].left)) && (typeid(*right_type) == typeid(*widen_table[k].right))) {
    60 		if ((typeid(*left_type) == typeid(*widen_table[k].left)) && (typeid(*right_type) == typeid(*widen_table[k].right)))
    61 		      deprecated_operation = (widen_table[k].status == widen_entry::deprecated);	
       
    62                       return widen_table[k].result;
    61                       return widen_table[k].result;
    63                 }
       
    64 	return NULL;
    62 	return NULL;
    65 }
    63 }
    66 
    64 
    67 
    65 
    68 /* add a data type to a candidate data type list, while guaranteeing no duplicate entries! */
    66 /* add a data type to a candidate data type list, while guaranteeing no duplicate entries! */
    69 /* Returns true if it really did add the datatype to the list, or false if it was already present in the list! */
    67 /* Returns true if it really did add the datatype to the list, or false if it was already present in the list! */
    70 bool fill_candidate_datatypes_c::add_datatype_to_candidate_list(symbol_c *symbol, symbol_c *datatype) {
    68 bool fill_candidate_datatypes_c::add_datatype_to_candidate_list(symbol_c *symbol, symbol_c *datatype) {
       
    69   /* If it is an invalid data type, do not insert!
       
    70    * NOTE: it reduces overall code size to do this test here, instead of doing every time before calling the add_datatype_to_candidate_list() function. 
       
    71    */
       
    72   if (!is_type_valid(datatype)) /* checks for NULL and invalid_type_name_c */
       
    73     return false;
       
    74 
    71   if (search_in_candidate_datatype_list(datatype, symbol->candidate_datatypes) >= 0) 
    75   if (search_in_candidate_datatype_list(datatype, symbol->candidate_datatypes) >= 0) 
    72     /* already in the list, Just return! */
    76     /* already in the list, Just return! */
    73     return false;
    77     return false;
    74   
    78   
    75   /* not yet in the candidate data type list, so we insert it now! */
    79   /* not yet in the candidate data type list, so we insert it now! */
  1276 	if (NULL == prev_il_instruction) return NULL;
  1280 	if (NULL == prev_il_instruction) return NULL;
  1277 	for(unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) {
  1281 	for(unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) {
  1278 		for(unsigned int j = 0; j < il_operand->candidate_datatypes.size(); j++) {
  1282 		for(unsigned int j = 0; j < il_operand->candidate_datatypes.size(); j++) {
  1279 			prev_instruction_type = prev_il_instruction->candidate_datatypes[i];
  1283 			prev_instruction_type = prev_il_instruction->candidate_datatypes[i];
  1280 			operand_type = il_operand->candidate_datatypes[j];
  1284 			operand_type = il_operand->candidate_datatypes[j];
  1281 			if (is_type_equal(prev_instruction_type, operand_type) &&
  1285 			/* NOTE: add_datatype_to_candidate_list() will only really add the datatype if it is != NULL !!! */
  1282 					is_ANY_NUM_compatible(prev_instruction_type))
  1286 			add_datatype_to_candidate_list(symbol, widening_conversion(prev_instruction_type, operand_type, widen_ADD_table));
  1283 				add_datatype_to_candidate_list(symbol, prev_instruction_type);
       
  1284 			else {
       
  1285 				symbol_c *result = widening_conversion(prev_instruction_type, operand_type, widen_ADD_table, symbol->deprecated_operation);
       
  1286 				if (result)
       
  1287 					add_datatype_to_candidate_list(symbol, result);
       
  1288 
       
  1289 			}
       
  1290 		}
  1287 		}
  1291 	}
  1288 	}
  1292 	if (debug) std::cout <<  "ADD [" << prev_il_instruction->candidate_datatypes.size() << "," << il_operand->candidate_datatypes.size() << "] ==> "  << symbol->candidate_datatypes.size() << " result.\n";
  1289 	if (debug) std::cout <<  "ADD [" << prev_il_instruction->candidate_datatypes.size() << "," << il_operand->candidate_datatypes.size() << "] ==> "  << symbol->candidate_datatypes.size() << " result.\n";
  1293 	return NULL;
  1290 	return NULL;
  1294 }
  1291 }
  1300 	if (NULL == prev_il_instruction) return NULL;
  1297 	if (NULL == prev_il_instruction) return NULL;
  1301 	for(unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) {
  1298 	for(unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) {
  1302 		for(unsigned int j = 0; j < il_operand->candidate_datatypes.size(); j++) {
  1299 		for(unsigned int j = 0; j < il_operand->candidate_datatypes.size(); j++) {
  1303 			prev_instruction_type = prev_il_instruction->candidate_datatypes[i];
  1300 			prev_instruction_type = prev_il_instruction->candidate_datatypes[i];
  1304 			operand_type = il_operand->candidate_datatypes[j];
  1301 			operand_type = il_operand->candidate_datatypes[j];
  1305 			if (is_type_equal(prev_instruction_type, operand_type) &&
  1302 			/* NOTE: add_datatype_to_candidate_list() will only really add the datatype if it is != NULL !!! */
  1306 					is_ANY_NUM_compatible(prev_instruction_type))
  1303 			add_datatype_to_candidate_list(symbol, widening_conversion(prev_instruction_type, operand_type, widen_SUB_table));
  1307 				add_datatype_to_candidate_list(symbol, prev_instruction_type);
       
  1308 			else {
       
  1309 				symbol_c *result = widening_conversion(prev_instruction_type, operand_type, widen_SUB_table, symbol->deprecated_operation);
       
  1310 				if (result)
       
  1311 					add_datatype_to_candidate_list(symbol, result);
       
  1312 			}
       
  1313 		}
  1304 		}
  1314 	}
  1305 	}
  1315 	if (debug) std::cout <<  "SUB [" << prev_il_instruction->candidate_datatypes.size() << "," << il_operand->candidate_datatypes.size() << "] ==> "  << symbol->candidate_datatypes.size() << " result.\n";
  1306 	if (debug) std::cout <<  "SUB [" << prev_il_instruction->candidate_datatypes.size() << "," << il_operand->candidate_datatypes.size() << "] ==> "  << symbol->candidate_datatypes.size() << " result.\n";
  1316 	return NULL;
  1307 	return NULL;
  1317 }
  1308 }
  1323 	if (NULL == prev_il_instruction) return NULL;
  1314 	if (NULL == prev_il_instruction) return NULL;
  1324 	for(unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) {
  1315 	for(unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) {
  1325 		for(unsigned int j = 0; j < il_operand->candidate_datatypes.size(); j++) {
  1316 		for(unsigned int j = 0; j < il_operand->candidate_datatypes.size(); j++) {
  1326 			prev_instruction_type = prev_il_instruction->candidate_datatypes[i];
  1317 			prev_instruction_type = prev_il_instruction->candidate_datatypes[i];
  1327 			operand_type = il_operand->candidate_datatypes[j];
  1318 			operand_type = il_operand->candidate_datatypes[j];
  1328 			if (is_type_equal(prev_instruction_type, operand_type) &&
  1319 			/* NOTE: add_datatype_to_candidate_list() will only really add the datatype if it is != NULL !!! */
  1329 					is_ANY_NUM_compatible(prev_instruction_type))
  1320 			add_datatype_to_candidate_list(symbol, widening_conversion(prev_instruction_type, operand_type, widen_MUL_table));
  1330 				add_datatype_to_candidate_list(symbol, prev_instruction_type);
       
  1331 			else {
       
  1332 				symbol_c *result = widening_conversion(prev_instruction_type, operand_type, widen_MUL_table, symbol->deprecated_operation);
       
  1333 				if (result)
       
  1334 					add_datatype_to_candidate_list(symbol, result);
       
  1335 			}
       
  1336 		}
  1321 		}
  1337 	}
  1322 	}
  1338 	if (debug) std::cout <<  "MUL [" << prev_il_instruction->candidate_datatypes.size() << "," << il_operand->candidate_datatypes.size() << "] ==> "  << symbol->candidate_datatypes.size() << " result.\n";
  1323 	if (debug) std::cout <<  "MUL [" << prev_il_instruction->candidate_datatypes.size() << "," << il_operand->candidate_datatypes.size() << "] ==> "  << symbol->candidate_datatypes.size() << " result.\n";
  1339 	return NULL;
  1324 	return NULL;
  1340 }
  1325 }
  1346 	if (NULL == prev_il_instruction) return NULL;
  1331 	if (NULL == prev_il_instruction) return NULL;
  1347 	for(unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) {
  1332 	for(unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) {
  1348 		for(unsigned int j = 0; j < il_operand->candidate_datatypes.size(); j++) {
  1333 		for(unsigned int j = 0; j < il_operand->candidate_datatypes.size(); j++) {
  1349 			prev_instruction_type = prev_il_instruction->candidate_datatypes[i];
  1334 			prev_instruction_type = prev_il_instruction->candidate_datatypes[i];
  1350 			operand_type = il_operand->candidate_datatypes[j];
  1335 			operand_type = il_operand->candidate_datatypes[j];
  1351 			if (is_type_equal(prev_instruction_type, operand_type) &&
  1336 			/* NOTE: add_datatype_to_candidate_list() will only really add the datatype if it is != NULL !!! */
  1352 					is_ANY_NUM_compatible(prev_instruction_type))
  1337 			add_datatype_to_candidate_list(symbol, widening_conversion(prev_instruction_type, operand_type, widen_DIV_table));
  1353 				add_datatype_to_candidate_list(symbol, prev_instruction_type);
       
  1354 			else {
       
  1355 				symbol_c *result = widening_conversion(prev_instruction_type, operand_type, widen_DIV_table, symbol->deprecated_operation);
       
  1356 				if (result)
       
  1357 					add_datatype_to_candidate_list(symbol, result);
       
  1358 			}
       
  1359 		}
  1338 		}
  1360 	}
  1339 	}
  1361 	if (debug) std::cout <<  "DIV [" << prev_il_instruction->candidate_datatypes.size() << "," << il_operand->candidate_datatypes.size() << "] ==> "  << symbol->candidate_datatypes.size() << " result.\n";
  1340 	if (debug) std::cout <<  "DIV [" << prev_il_instruction->candidate_datatypes.size() << "," << il_operand->candidate_datatypes.size() << "] ==> "  << symbol->candidate_datatypes.size() << " result.\n";
  1362 	return NULL;
  1341 	return NULL;
  1363 }
  1342 }
  1769 	symbol->r_exp->accept(*this);
  1748 	symbol->r_exp->accept(*this);
  1770 	for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
  1749 	for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
  1771 		for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
  1750 		for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
  1772 			left_type = symbol->l_exp->candidate_datatypes[i];
  1751 			left_type = symbol->l_exp->candidate_datatypes[i];
  1773 			right_type = symbol->r_exp->candidate_datatypes[j];
  1752 			right_type = symbol->r_exp->candidate_datatypes[j];
  1774 			if (is_type_equal(left_type, right_type) && is_ANY_NUM_compatible(left_type))
  1753 			/* NOTE: add_datatype_to_candidate_list() will only really add the datatype if it is != NULL !!! */
  1775 				add_datatype_to_candidate_list(symbol, left_type);
  1754 			add_datatype_to_candidate_list(symbol, widening_conversion(left_type, right_type, widen_ADD_table));
  1776 			else {
       
  1777 				symbol_c *result = widening_conversion(left_type, right_type, widen_ADD_table, symbol->deprecated_operation);
       
  1778 				if (result)
       
  1779 					add_datatype_to_candidate_list(symbol, result);
       
  1780 			}
       
  1781 		}
  1755 		}
  1782 	}
  1756 	}
  1783 	if (debug) std::cout <<  "+ [" << symbol->l_exp->candidate_datatypes.size() << "," << symbol->r_exp->candidate_datatypes.size() << "] ==> "  << symbol->candidate_datatypes.size() << " result.\n";
  1757 	if (debug) std::cout <<  "+ [" << symbol->l_exp->candidate_datatypes.size() << "," << symbol->r_exp->candidate_datatypes.size() << "] ==> "  << symbol->candidate_datatypes.size() << " result.\n";
  1784 	return NULL;
  1758 	return NULL;
  1785 }
  1759 }
  1793 	symbol->r_exp->accept(*this);
  1767 	symbol->r_exp->accept(*this);
  1794 	for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
  1768 	for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
  1795 		for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
  1769 		for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
  1796 			left_type = symbol->l_exp->candidate_datatypes[i];
  1770 			left_type = symbol->l_exp->candidate_datatypes[i];
  1797 			right_type = symbol->r_exp->candidate_datatypes[j];
  1771 			right_type = symbol->r_exp->candidate_datatypes[j];
  1798 			if (is_type_equal(left_type, right_type) && is_ANY_NUM_compatible(left_type))
  1772 			/* NOTE: add_datatype_to_candidate_list() will only really add the datatype if it is != NULL !!! */
  1799 				add_datatype_to_candidate_list(symbol, left_type);
  1773 			add_datatype_to_candidate_list(symbol, widening_conversion(left_type, right_type, widen_SUB_table));
  1800 			else {
       
  1801 				symbol_c *result = widening_conversion(left_type, right_type, widen_SUB_table, symbol->deprecated_operation);
       
  1802 				if (result)
       
  1803 					add_datatype_to_candidate_list(symbol, result);
       
  1804 			}
       
  1805 		}
  1774 		}
  1806 	}
  1775 	}
  1807 	if (debug) std::cout <<  "- [" << symbol->l_exp->candidate_datatypes.size() << "," << symbol->r_exp->candidate_datatypes.size() << "] ==> "  << symbol->candidate_datatypes.size() << " result.\n";
  1776 	if (debug) std::cout <<  "- [" << symbol->l_exp->candidate_datatypes.size() << "," << symbol->r_exp->candidate_datatypes.size() << "] ==> "  << symbol->candidate_datatypes.size() << " result.\n";
  1808 	return NULL;
  1777 	return NULL;
  1809 }
  1778 }
  1817 	symbol->r_exp->accept(*this);
  1786 	symbol->r_exp->accept(*this);
  1818 	for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
  1787 	for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
  1819 		for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
  1788 		for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
  1820 			left_type = symbol->l_exp->candidate_datatypes[i];
  1789 			left_type = symbol->l_exp->candidate_datatypes[i];
  1821 			right_type = symbol->r_exp->candidate_datatypes[j];
  1790 			right_type = symbol->r_exp->candidate_datatypes[j];
  1822 			if      (is_type_equal(left_type, right_type) && is_ANY_NUM_compatible(left_type))
  1791 			/* NOTE: add_datatype_to_candidate_list() will only really add the datatype if it is != NULL !!! */
  1823 				add_datatype_to_candidate_list(symbol, left_type);
  1792 			add_datatype_to_candidate_list(symbol, widening_conversion(left_type, right_type, widen_MUL_table));
  1824 			else {
       
  1825 				symbol_c *result = widening_conversion(left_type, right_type, widen_MUL_table, symbol->deprecated_operation);
       
  1826 				if (result)
       
  1827 					add_datatype_to_candidate_list(symbol, result);
       
  1828 			}
       
  1829 
       
  1830 		}
  1793 		}
  1831 	}
  1794 	}
  1832 	if (debug) std::cout << "* [" << symbol->l_exp->candidate_datatypes.size() << "," << symbol->r_exp->candidate_datatypes.size() << "] ==> "  << symbol->candidate_datatypes.size() << " result.\n";
  1795 	if (debug) std::cout << "* [" << symbol->l_exp->candidate_datatypes.size() << "," << symbol->r_exp->candidate_datatypes.size() << "] ==> "  << symbol->candidate_datatypes.size() << " result.\n";
  1833 
  1796 
  1834 	return NULL;
  1797 	return NULL;
  1842 	symbol->r_exp->accept(*this);
  1805 	symbol->r_exp->accept(*this);
  1843 	for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
  1806 	for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
  1844 		for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
  1807 		for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
  1845 			left_type = symbol->l_exp->candidate_datatypes[i];
  1808 			left_type = symbol->l_exp->candidate_datatypes[i];
  1846 			right_type = symbol->r_exp->candidate_datatypes[j];
  1809 			right_type = symbol->r_exp->candidate_datatypes[j];
  1847 			if      (is_type_equal(left_type, right_type) && is_ANY_NUM_type(left_type))
  1810 			/* NOTE: add_datatype_to_candidate_list() will only really add the datatype if it is != NULL !!! */
  1848 				add_datatype_to_candidate_list(symbol, left_type);
  1811 			add_datatype_to_candidate_list(symbol, widening_conversion(left_type, right_type, widen_DIV_table));
  1849 			else {
       
  1850 				symbol_c *result = widening_conversion(left_type, right_type, widen_DIV_table, symbol->deprecated_operation);
       
  1851 				if (result)
       
  1852 					add_datatype_to_candidate_list(symbol, result);
       
  1853 			}
       
  1854 
       
  1855 		}
  1812 		}
  1856 	}
  1813 	}
  1857 	if (debug) std::cout << "/ [" << symbol->l_exp->candidate_datatypes.size() << "," << symbol->r_exp->candidate_datatypes.size() << "] ==> "  << symbol->candidate_datatypes.size() << " result.\n";
  1814 	if (debug) std::cout << "/ [" << symbol->l_exp->candidate_datatypes.size() << "," << symbol->r_exp->candidate_datatypes.size() << "] ==> "  << symbol->candidate_datatypes.size() << " result.\n";
  1858 	return NULL;
  1815 	return NULL;
  1859 }
  1816 }