stage3/fill_candidate_datatypes.cc
changeset 476 38d85e9a0507
parent 473 933d6c204607
child 478 b13feab3b918
equal deleted inserted replaced
475:28816126cf8d 476:38d85e9a0507
    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[]) {
    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) {
    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 			return widen_table[k].result;
    61 		      deprecated_operation = (widen_table[k].status == widen_entry::deprecated);	
       
    62                       return widen_table[k].result;
       
    63                 }
    62 	return NULL;
    64 	return NULL;
    63 }
    65 }
    64 
    66 
    65 
    67 
    66 /* add a data type to a candidate data type list, while guaranteeing no duplicate entries! */
    68 /* add a data type to a candidate data type list, while guaranteeing no duplicate entries! */
  1267 	return NULL;
  1269 	return NULL;
  1268 }
  1270 }
  1269 
  1271 
  1270 void *fill_candidate_datatypes_c::visit(ADD_operator_c *symbol) {
  1272 void *fill_candidate_datatypes_c::visit(ADD_operator_c *symbol) {
  1271 	symbol_c *prev_instruction_type, *operand_type;
  1273 	symbol_c *prev_instruction_type, *operand_type;
  1272 
  1274         
       
  1275         symbol->deprecated_operation = false;
  1273 	if (NULL == prev_il_instruction) return NULL;
  1276 	if (NULL == prev_il_instruction) return NULL;
  1274 	for(unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) {
  1277 	for(unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) {
  1275 		for(unsigned int j = 0; j < il_operand->candidate_datatypes.size(); j++) {
  1278 		for(unsigned int j = 0; j < il_operand->candidate_datatypes.size(); j++) {
  1276 			prev_instruction_type = prev_il_instruction->candidate_datatypes[i];
  1279 			prev_instruction_type = prev_il_instruction->candidate_datatypes[i];
  1277 			operand_type = il_operand->candidate_datatypes[j];
  1280 			operand_type = il_operand->candidate_datatypes[j];
  1278 			if (is_type_equal(prev_instruction_type, operand_type) &&
  1281 			if (is_type_equal(prev_instruction_type, operand_type) &&
  1279 					is_ANY_NUM_compatible(prev_instruction_type))
  1282 					is_ANY_NUM_compatible(prev_instruction_type))
  1280 				add_datatype_to_candidate_list(symbol, prev_instruction_type);
  1283 				add_datatype_to_candidate_list(symbol, prev_instruction_type);
  1281 			else {
  1284 			else {
  1282 				symbol_c *result = widening_conversion(prev_instruction_type, operand_type, widen_ADD_table);
  1285 				symbol_c *result = widening_conversion(prev_instruction_type, operand_type, widen_ADD_table, symbol->deprecated_operation);
  1283 				if (result)
  1286 				if (result)
  1284 					add_datatype_to_candidate_list(symbol, result);
  1287 					add_datatype_to_candidate_list(symbol, result);
  1285 
  1288 
  1286 			}
  1289 			}
  1287 		}
  1290 		}
  1291 }
  1294 }
  1292 
  1295 
  1293 void *fill_candidate_datatypes_c::visit(SUB_operator_c *symbol) {
  1296 void *fill_candidate_datatypes_c::visit(SUB_operator_c *symbol) {
  1294 	symbol_c *prev_instruction_type, *operand_type;
  1297 	symbol_c *prev_instruction_type, *operand_type;
  1295 
  1298 
       
  1299         symbol->deprecated_operation = false;
  1296 	if (NULL == prev_il_instruction) return NULL;
  1300 	if (NULL == prev_il_instruction) return NULL;
  1297 	for(unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) {
  1301 	for(unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) {
  1298 		for(unsigned int j = 0; j < il_operand->candidate_datatypes.size(); j++) {
  1302 		for(unsigned int j = 0; j < il_operand->candidate_datatypes.size(); j++) {
  1299 			prev_instruction_type = prev_il_instruction->candidate_datatypes[i];
  1303 			prev_instruction_type = prev_il_instruction->candidate_datatypes[i];
  1300 			operand_type = il_operand->candidate_datatypes[j];
  1304 			operand_type = il_operand->candidate_datatypes[j];
  1301 			if (is_type_equal(prev_instruction_type, operand_type) &&
  1305 			if (is_type_equal(prev_instruction_type, operand_type) &&
  1302 					is_ANY_NUM_compatible(prev_instruction_type))
  1306 					is_ANY_NUM_compatible(prev_instruction_type))
  1303 				add_datatype_to_candidate_list(symbol, prev_instruction_type);
  1307 				add_datatype_to_candidate_list(symbol, prev_instruction_type);
  1304 			else {
  1308 			else {
  1305 				symbol_c *result = widening_conversion(prev_instruction_type, operand_type, widen_SUB_table);
  1309 				symbol_c *result = widening_conversion(prev_instruction_type, operand_type, widen_SUB_table, symbol->deprecated_operation);
  1306 				if (result)
  1310 				if (result)
  1307 					add_datatype_to_candidate_list(symbol, result);
  1311 					add_datatype_to_candidate_list(symbol, result);
  1308 			}
  1312 			}
  1309 		}
  1313 		}
  1310 	}
  1314 	}
  1313 }
  1317 }
  1314 
  1318 
  1315 void *fill_candidate_datatypes_c::visit(MUL_operator_c *symbol) {
  1319 void *fill_candidate_datatypes_c::visit(MUL_operator_c *symbol) {
  1316 	symbol_c *prev_instruction_type, *operand_type;
  1320 	symbol_c *prev_instruction_type, *operand_type;
  1317 
  1321 
       
  1322         symbol->deprecated_operation = false;
  1318 	if (NULL == prev_il_instruction) return NULL;
  1323 	if (NULL == prev_il_instruction) return NULL;
  1319 	for(unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) {
  1324 	for(unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) {
  1320 		for(unsigned int j = 0; j < il_operand->candidate_datatypes.size(); j++) {
  1325 		for(unsigned int j = 0; j < il_operand->candidate_datatypes.size(); j++) {
  1321 			prev_instruction_type = prev_il_instruction->candidate_datatypes[i];
  1326 			prev_instruction_type = prev_il_instruction->candidate_datatypes[i];
  1322 			operand_type = il_operand->candidate_datatypes[j];
  1327 			operand_type = il_operand->candidate_datatypes[j];
  1323 			if (is_type_equal(prev_instruction_type, operand_type) &&
  1328 			if (is_type_equal(prev_instruction_type, operand_type) &&
  1324 					is_ANY_NUM_compatible(prev_instruction_type))
  1329 					is_ANY_NUM_compatible(prev_instruction_type))
  1325 				add_datatype_to_candidate_list(symbol, prev_instruction_type);
  1330 				add_datatype_to_candidate_list(symbol, prev_instruction_type);
  1326 			else {
  1331 			else {
  1327 				symbol_c *result = widening_conversion(prev_instruction_type, operand_type, widen_MUL_table);
  1332 				symbol_c *result = widening_conversion(prev_instruction_type, operand_type, widen_MUL_table, symbol->deprecated_operation);
  1328 				if (result)
  1333 				if (result)
  1329 					add_datatype_to_candidate_list(symbol, result);
  1334 					add_datatype_to_candidate_list(symbol, result);
  1330 			}
  1335 			}
  1331 		}
  1336 		}
  1332 	}
  1337 	}
  1335 }
  1340 }
  1336 
  1341 
  1337 void *fill_candidate_datatypes_c::visit(DIV_operator_c *symbol) {
  1342 void *fill_candidate_datatypes_c::visit(DIV_operator_c *symbol) {
  1338 	symbol_c *prev_instruction_type, *operand_type;
  1343 	symbol_c *prev_instruction_type, *operand_type;
  1339 
  1344 
       
  1345         symbol->deprecated_operation = false;
  1340 	if (NULL == prev_il_instruction) return NULL;
  1346 	if (NULL == prev_il_instruction) return NULL;
  1341 	for(unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) {
  1347 	for(unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) {
  1342 		for(unsigned int j = 0; j < il_operand->candidate_datatypes.size(); j++) {
  1348 		for(unsigned int j = 0; j < il_operand->candidate_datatypes.size(); j++) {
  1343 			prev_instruction_type = prev_il_instruction->candidate_datatypes[i];
  1349 			prev_instruction_type = prev_il_instruction->candidate_datatypes[i];
  1344 			operand_type = il_operand->candidate_datatypes[j];
  1350 			operand_type = il_operand->candidate_datatypes[j];
  1345 			if (is_type_equal(prev_instruction_type, operand_type) &&
  1351 			if (is_type_equal(prev_instruction_type, operand_type) &&
  1346 					is_ANY_NUM_compatible(prev_instruction_type))
  1352 					is_ANY_NUM_compatible(prev_instruction_type))
  1347 				add_datatype_to_candidate_list(symbol, prev_instruction_type);
  1353 				add_datatype_to_candidate_list(symbol, prev_instruction_type);
  1348 			else {
  1354 			else {
  1349 				symbol_c *result = widening_conversion(prev_instruction_type, operand_type, widen_DIV_table);
  1355 				symbol_c *result = widening_conversion(prev_instruction_type, operand_type, widen_DIV_table, symbol->deprecated_operation);
  1350 				if (result)
  1356 				if (result)
  1351 					add_datatype_to_candidate_list(symbol, result);
  1357 					add_datatype_to_candidate_list(symbol, result);
  1352 			}
  1358 			}
  1353 		}
  1359 		}
  1354 	}
  1360 	}
  1756 	 * functions if the input parameters are all literals (e.g. ADD(42, 42)). This
  1762 	 * functions if the input parameters are all literals (e.g. ADD(42, 42)). This
  1757 	 * means this class will be more difficult than it appears at first.
  1763 	 * means this class will be more difficult than it appears at first.
  1758 	 */
  1764 	 */
  1759 	symbol_c *left_type, *right_type;
  1765 	symbol_c *left_type, *right_type;
  1760 
  1766 
       
  1767         symbol->deprecated_operation = false;
  1761 	symbol->l_exp->accept(*this);
  1768 	symbol->l_exp->accept(*this);
  1762 	symbol->r_exp->accept(*this);
  1769 	symbol->r_exp->accept(*this);
  1763 	for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
  1770 	for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
  1764 		for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
  1771 		for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
  1765 			left_type = symbol->l_exp->candidate_datatypes[i];
  1772 			left_type = symbol->l_exp->candidate_datatypes[i];
  1766 			right_type = symbol->r_exp->candidate_datatypes[j];
  1773 			right_type = symbol->r_exp->candidate_datatypes[j];
  1767 			if (is_type_equal(left_type, right_type) && is_ANY_NUM_compatible(left_type))
  1774 			if (is_type_equal(left_type, right_type) && is_ANY_NUM_compatible(left_type))
  1768 				add_datatype_to_candidate_list(symbol, left_type);
  1775 				add_datatype_to_candidate_list(symbol, left_type);
  1769 			else {
  1776 			else {
  1770 				symbol_c *result = widening_conversion(left_type, right_type, widen_ADD_table);
  1777 				symbol_c *result = widening_conversion(left_type, right_type, widen_ADD_table, symbol->deprecated_operation);
  1771 				if (result)
  1778 				if (result)
  1772 					add_datatype_to_candidate_list(symbol, result);
  1779 					add_datatype_to_candidate_list(symbol, result);
  1773 			}
  1780 			}
  1774 		}
  1781 		}
  1775 	}
  1782 	}
  1779 
  1786 
  1780 
  1787 
  1781 void *fill_candidate_datatypes_c::visit(sub_expression_c *symbol) {
  1788 void *fill_candidate_datatypes_c::visit(sub_expression_c *symbol) {
  1782 	symbol_c *left_type, *right_type;
  1789 	symbol_c *left_type, *right_type;
  1783 
  1790 
       
  1791         symbol->deprecated_operation = false;
  1784 	symbol->l_exp->accept(*this);
  1792 	symbol->l_exp->accept(*this);
  1785 	symbol->r_exp->accept(*this);
  1793 	symbol->r_exp->accept(*this);
  1786 	for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
  1794 	for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
  1787 		for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
  1795 		for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
  1788 			left_type = symbol->l_exp->candidate_datatypes[i];
  1796 			left_type = symbol->l_exp->candidate_datatypes[i];
  1789 			right_type = symbol->r_exp->candidate_datatypes[j];
  1797 			right_type = symbol->r_exp->candidate_datatypes[j];
  1790 			if (is_type_equal(left_type, right_type) && is_ANY_NUM_compatible(left_type))
  1798 			if (is_type_equal(left_type, right_type) && is_ANY_NUM_compatible(left_type))
  1791 				add_datatype_to_candidate_list(symbol, left_type);
  1799 				add_datatype_to_candidate_list(symbol, left_type);
  1792 			else {
  1800 			else {
  1793 				symbol_c *result = widening_conversion(left_type, right_type, widen_SUB_table);
  1801 				symbol_c *result = widening_conversion(left_type, right_type, widen_SUB_table, symbol->deprecated_operation);
  1794 				if (result)
  1802 				if (result)
  1795 					add_datatype_to_candidate_list(symbol, result);
  1803 					add_datatype_to_candidate_list(symbol, result);
  1796 			}
  1804 			}
  1797 		}
  1805 		}
  1798 	}
  1806 	}
  1802 
  1810 
  1803 
  1811 
  1804 void *fill_candidate_datatypes_c::visit(mul_expression_c *symbol) {
  1812 void *fill_candidate_datatypes_c::visit(mul_expression_c *symbol) {
  1805 	symbol_c *left_type, *right_type;
  1813 	symbol_c *left_type, *right_type;
  1806 
  1814 
       
  1815         symbol->deprecated_operation = false;
  1807 	symbol->l_exp->accept(*this);
  1816 	symbol->l_exp->accept(*this);
  1808 	symbol->r_exp->accept(*this);
  1817 	symbol->r_exp->accept(*this);
  1809 	for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
  1818 	for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
  1810 		for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
  1819 		for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
  1811 			left_type = symbol->l_exp->candidate_datatypes[i];
  1820 			left_type = symbol->l_exp->candidate_datatypes[i];
  1812 			right_type = symbol->r_exp->candidate_datatypes[j];
  1821 			right_type = symbol->r_exp->candidate_datatypes[j];
  1813 			if      (is_type_equal(left_type, right_type) && is_ANY_NUM_compatible(left_type))
  1822 			if      (is_type_equal(left_type, right_type) && is_ANY_NUM_compatible(left_type))
  1814 				add_datatype_to_candidate_list(symbol, left_type);
  1823 				add_datatype_to_candidate_list(symbol, left_type);
  1815 			else {
  1824 			else {
  1816 				symbol_c *result = widening_conversion(left_type, right_type, widen_MUL_table);
  1825 				symbol_c *result = widening_conversion(left_type, right_type, widen_MUL_table, symbol->deprecated_operation);
  1817 				if (result)
  1826 				if (result)
  1818 					add_datatype_to_candidate_list(symbol, result);
  1827 					add_datatype_to_candidate_list(symbol, result);
  1819 			}
  1828 			}
  1820 
  1829 
  1821 		}
  1830 		}
  1826 }
  1835 }
  1827 
  1836 
  1828 void *fill_candidate_datatypes_c::visit(div_expression_c *symbol) {
  1837 void *fill_candidate_datatypes_c::visit(div_expression_c *symbol) {
  1829 	symbol_c *left_type, *right_type;
  1838 	symbol_c *left_type, *right_type;
  1830 
  1839 
       
  1840         symbol->deprecated_operation = false;
  1831 	symbol->l_exp->accept(*this);
  1841 	symbol->l_exp->accept(*this);
  1832 	symbol->r_exp->accept(*this);
  1842 	symbol->r_exp->accept(*this);
  1833 	for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
  1843 	for(unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) {
  1834 		for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
  1844 		for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) {
  1835 			left_type = symbol->l_exp->candidate_datatypes[i];
  1845 			left_type = symbol->l_exp->candidate_datatypes[i];
  1836 			right_type = symbol->r_exp->candidate_datatypes[j];
  1846 			right_type = symbol->r_exp->candidate_datatypes[j];
  1837 			if      (is_type_equal(left_type, right_type) && is_ANY_NUM_type(left_type))
  1847 			if      (is_type_equal(left_type, right_type) && is_ANY_NUM_type(left_type))
  1838 				add_datatype_to_candidate_list(symbol, left_type);
  1848 				add_datatype_to_candidate_list(symbol, left_type);
  1839 			else {
  1849 			else {
  1840 				symbol_c *result = widening_conversion(left_type, right_type, widen_DIV_table);
  1850 				symbol_c *result = widening_conversion(left_type, right_type, widen_DIV_table, symbol->deprecated_operation);
  1841 				if (result)
  1851 				if (result)
  1842 					add_datatype_to_candidate_list(symbol, result);
  1852 					add_datatype_to_candidate_list(symbol, result);
  1843 			}
  1853 			}
  1844 
  1854 
  1845 		}
  1855 		}