stage3/fill_candidate_datatypes.cc
changeset 424 43d73e28eca8
parent 422 c957f712ef4d
child 426 78f31e12fc52
equal deleted inserted replaced
423:f4a2d400ddbd 424:43d73e28eca8
    65 
    65 
    66 
    66 
    67 
    67 
    68 
    68 
    69 /* returns true if compatible function/FB invocation, otherwise returns false */
    69 /* returns true if compatible function/FB invocation, otherwise returns false */
       
    70 /* Assumes that the candidate_datatype lists of all the parameters being passed haved already been filled in */
    70 bool fill_candidate_datatypes_c::match_nonformal_call(symbol_c *f_call, symbol_c *f_decl) {
    71 bool fill_candidate_datatypes_c::match_nonformal_call(symbol_c *f_call, symbol_c *f_decl) {
    71 	symbol_c *call_param_value,  *param_type;
    72 	symbol_c *call_param_value,  *param_type;
    72 	identifier_c *param_name;
    73 	identifier_c *param_name;
    73 	function_param_iterator_c       fp_iterator(f_decl);
    74 	function_param_iterator_c       fp_iterator(f_decl);
    74 	function_call_param_iterator_c fcp_iterator(f_call);
    75 	function_call_param_iterator_c fcp_iterator(f_call);
   102 }
   103 }
   103 
   104 
   104 
   105 
   105 
   106 
   106 /* returns true if compatible function/FB invocation, otherwise returns false */
   107 /* returns true if compatible function/FB invocation, otherwise returns false */
       
   108 /* Assumes that the candidate_datatype lists of all the parameters being passed haved already been filled in */
   107 bool fill_candidate_datatypes_c::match_formal_call(symbol_c *f_call, symbol_c *f_decl) {
   109 bool fill_candidate_datatypes_c::match_formal_call(symbol_c *f_call, symbol_c *f_decl) {
   108 	symbol_c *call_param_value, *call_param_name, *param_type;
   110 	symbol_c *call_param_value, *call_param_name, *param_type;
   109 	symbol_c *verify_duplicate_param;
   111 	symbol_c *verify_duplicate_param;
   110 	identifier_c *param_name;
   112 	identifier_c *param_name;
   111 	function_param_iterator_c       fp_iterator(f_decl);
   113 	function_param_iterator_c       fp_iterator(f_decl);
   127 		if(verify_duplicate_param != call_param_value)
   129 		if(verify_duplicate_param != call_param_value)
   128 			return false;
   130 			return false;
   129 
   131 
   130 		/* Obtaining the type of the value being passed in the function call */
   132 		/* Obtaining the type of the value being passed in the function call */
   131 		std::vector <symbol_c *>&call_param_types = call_param_value->candidate_datatypes;
   133 		std::vector <symbol_c *>&call_param_types = call_param_value->candidate_datatypes;
   132 
       
   133 
   134 
   134 		/* Find the corresponding parameter in function declaration */
   135 		/* Find the corresponding parameter in function declaration */
   135 		param_name = fp_iterator.search(call_param_name);
   136 		param_name = fp_iterator.search(call_param_name);
   136 		if(param_name == NULL) return false;
   137 		if(param_name == NULL) return false;
   137 		/* Get the parameter type */
   138 		/* Get the parameter type */
  1669 void *fill_candidate_datatypes_c::visit(function_invocation_c *symbol) {
  1670 void *fill_candidate_datatypes_c::visit(function_invocation_c *symbol) {
  1670 	function_declaration_c *f_decl;
  1671 	function_declaration_c *f_decl;
  1671 	list_c *parameter_list;
  1672 	list_c *parameter_list;
  1672 	list_c *parameter_candidate_datatypes;
  1673 	list_c *parameter_candidate_datatypes;
  1673 	symbol_c *returned_parameter_type;
  1674 	symbol_c *returned_parameter_type;
       
  1675 
       
  1676 	if (debug) std::cout << "function()\n";
       
  1677 
  1674 	function_symtable_t::iterator lower = function_symtable.lower_bound(symbol->function_name);
  1678 	function_symtable_t::iterator lower = function_symtable.lower_bound(symbol->function_name);
  1675 	function_symtable_t::iterator upper = function_symtable.upper_bound(symbol->function_name);
  1679 	function_symtable_t::iterator upper = function_symtable.upper_bound(symbol->function_name);
  1676 	/* If the name of the function being called is not found in the function symbol table, then this is an invalid call */
  1680 	/* If the name of the function being called is not found in the function symbol table, then this is an invalid call */
  1677 	/* Since the lexical parser already checks for this, then if this occurs then we have an internal compiler error. */
  1681 	/* Since the lexical parser already checks for this, then if this occurs then we have an internal compiler error. */
  1678 	if (lower == function_symtable.end()) ERROR;
  1682 	if (lower == function_symtable.end()) ERROR;
  1681 		parameter_list = (list_c *)symbol->formal_param_list;
  1685 		parameter_list = (list_c *)symbol->formal_param_list;
  1682 	else if (NULL != symbol->nonformal_param_list)
  1686 	else if (NULL != symbol->nonformal_param_list)
  1683 		parameter_list = (list_c *)symbol->nonformal_param_list;
  1687 		parameter_list = (list_c *)symbol->nonformal_param_list;
  1684 	else ERROR;
  1688 	else ERROR;
  1685 	
  1689 	
  1686 	if (debug) std::cout << "function()\n";
  1690 	/* Fill in the candidate_datatypes lists of all the expressions used in the function call parameters */
  1687 	parameter_list->accept(*this);
  1691 	parameter_list->accept(*this);
       
  1692 
       
  1693 	/* Look for all compatible function declarations, and add their return datatypes 
       
  1694 	 * to the candidate_datatype list of this function invocation. 
       
  1695 	 */
  1688 	for(; lower != upper; lower++) {
  1696 	for(; lower != upper; lower++) {
  1689 		bool compatible = false;
  1697 		bool compatible = false;
  1690 		
  1698 		
  1691 		f_decl = function_symtable.get_value(lower);
  1699 		f_decl = function_symtable.get_value(lower);
  1692 		/* Check if function declaration in symbol_table is compatible with parameters */
  1700 		/* Check if function declaration in symbol_table is compatible with parameters */
  1748 /* B 3.2.2 Subprogram Control Statements */
  1756 /* B 3.2.2 Subprogram Control Statements */
  1749 /*****************************************/
  1757 /*****************************************/
  1750 void *fill_candidate_datatypes_c::visit(fb_invocation_c *symbol) {
  1758 void *fill_candidate_datatypes_c::visit(fb_invocation_c *symbol) {
  1751 	bool compatible = false;
  1759 	bool compatible = false;
  1752 	symbol_c *fb_decl = search_varfb_instance_type->get_basetype_decl(symbol->fb_name);
  1760 	symbol_c *fb_decl = search_varfb_instance_type->get_basetype_decl(symbol->fb_name);
  1753 	
  1761 	/* Although a call to a non-declared FB is a semantic error, this is currently caught by stage 2! */
  1754 	if (NULL == fb_decl) ERROR;
  1762 	if (NULL == fb_decl) ERROR;
       
  1763 
  1755 	if (symbol->   formal_param_list != NULL) {
  1764 	if (symbol->   formal_param_list != NULL) {
  1756 		symbol->formal_param_list->accept(*this);
  1765 		symbol->formal_param_list->accept(*this);
  1757 		compatible = match_formal_call(symbol, fb_decl);
  1766 		compatible = match_formal_call(symbol, fb_decl);
  1758 	}
  1767 	}
  1759 	if (symbol->nonformal_param_list != NULL) {
  1768 	if (symbol->nonformal_param_list != NULL) {
  1760 		symbol->nonformal_param_list->accept(*this);
  1769 		symbol->nonformal_param_list->accept(*this);
  1761 		compatible = match_nonformal_call(symbol, fb_decl);
  1770 		compatible = match_nonformal_call(symbol, fb_decl);
  1762 	}
  1771 	}
       
  1772 
       
  1773 	if (compatible) 
       
  1774 		symbol->called_fb_declaration = fb_decl;
       
  1775 
  1763 	if (debug) std::cout << "FB [] ==> "  << symbol->candidate_datatypes.size() << " result.\n";
  1776 	if (debug) std::cout << "FB [] ==> "  << symbol->candidate_datatypes.size() << " result.\n";
  1764 	return NULL;
  1777 	return NULL;
  1765 }
  1778 }
  1766 
  1779 
  1767 
  1780