stage3/narrow_candidate_datatypes.cc
changeset 424 43d73e28eca8
parent 423 f4a2d400ddbd
child 425 c8e6cf57324a
equal deleted inserted replaced
423:f4a2d400ddbd 424:43d73e28eca8
    82 		 * Get the name of that parameter, and ignore if EN or ENO.
    82 		 * Get the name of that parameter, and ignore if EN or ENO.
    83 		 */
    83 		 */
    84 		do {
    84 		do {
    85 			param_name = fp_iterator.next();
    85 			param_name = fp_iterator.next();
    86 			/* If there is no other parameter declared, then we are passing too many parameters... */
    86 			/* If there is no other parameter declared, then we are passing too many parameters... */
    87 			/* This error should have been caught in fill_candidate_datatypes_c, but may occur here again when we handle FB invocations! */
    87 			/* This error should have been caught in fill_candidate_datatypes_c, but may occur here again when we handle FB invocations! 
    88 			if(param_name == NULL) return;
    88 			 * In this case, we carry on analysing the code in order to be able to provide relevant error messages
       
    89 			 * for that code too!
       
    90 			 */
       
    91 			if(param_name == NULL) break;
    89 		} while ((strcmp(param_name->value, "EN") == 0) || (strcmp(param_name->value, "ENO") == 0));
    92 		} while ((strcmp(param_name->value, "EN") == 0) || (strcmp(param_name->value, "ENO") == 0));
    90 
    93 
    91 		/* Set the desired datatype for this parameter, and call it recursively. */
    94 		/* Set the desired datatype for this parameter, and call it recursively. */
       
    95 		/* Note that if the call has more parameters than those declared in the function/FB declaration,
       
    96 		 * we may be setting this to NULL!
       
    97 		 */
    92 		call_param_value->datatype = base_type(fp_iterator.param_type());
    98 		call_param_value->datatype = base_type(fp_iterator.param_type());
    93 		if (NULL == call_param_value->datatype) ERROR;
    99 		if ((NULL != param_name) && (NULL == call_param_value->datatype)) ERROR;
       
   100 		if ((NULL == param_name) && (NULL != call_param_value->datatype)) ERROR;
    94 		call_param_value->accept(*this);
   101 		call_param_value->accept(*this);
    95 
   102 
    96 		if (extensible_parameter_highest_index < fp_iterator.extensible_param_index())
   103 		if (NULL != param_name) 
    97 			extensible_parameter_highest_index = fp_iterator.extensible_param_index();
   104 			if (extensible_parameter_highest_index < fp_iterator.extensible_param_index())
    98 	}
   105 				extensible_parameter_highest_index = fp_iterator.extensible_param_index();
    99 	/* call is compatible! */
   106 	}
   100 
       
   101 	/* In the case of a call to an extensible function, we store the highest index 
   107 	/* In the case of a call to an extensible function, we store the highest index 
   102 	 * of the extensible parameters this particular call uses, in the symbol_c object
   108 	 * of the extensible parameters this particular call uses, in the symbol_c object
   103 	 * of the function call itself!
   109 	 * of the function call itself!
   104 	 * In calls to non-extensible functions, this value will be set to -1.
   110 	 * In calls to non-extensible functions, this value will be set to -1.
   105 	 * This information is later used in stage4 to correctly generate the
   111 	 * This information is later used in stage4 to correctly generate the
   133 
   139 
   134 		/* Find the corresponding parameter in function declaration */
   140 		/* Find the corresponding parameter in function declaration */
   135 		param_name = fp_iterator.search(call_param_name);
   141 		param_name = fp_iterator.search(call_param_name);
   136 
   142 
   137 		/* Set the desired datatype for this parameter, and call it recursively. */
   143 		/* Set the desired datatype for this parameter, and call it recursively. */
       
   144 		/* NOTE: When handling a FB call, this narrow_formal_call() may be called to analyse
       
   145 		 *       an invalid FB call (call with parameters that do not exist on the FB declaration).
       
   146 		 *       For this reason, the param_name may come out as NULL!
       
   147 		 */
   138 		call_param_value->datatype = base_type(fp_iterator.param_type());
   148 		call_param_value->datatype = base_type(fp_iterator.param_type());
   139 		if (NULL == call_param_value->datatype) ERROR;
   149 		if ((NULL != param_name) && (NULL == call_param_value->datatype)) ERROR;
       
   150 		if ((NULL == param_name) && (NULL != call_param_value->datatype)) ERROR;
       
   151 
   140 		call_param_value->accept(*this);
   152 		call_param_value->accept(*this);
   141 
   153 
   142 		if (extensible_parameter_highest_index < fp_iterator.extensible_param_index())
   154 		if (NULL != param_name) 
   143 			extensible_parameter_highest_index = fp_iterator.extensible_param_index();
   155 			if (extensible_parameter_highest_index < fp_iterator.extensible_param_index())
       
   156 				extensible_parameter_highest_index = fp_iterator.extensible_param_index();
   144 	}
   157 	}
   145 	/* call is compatible! */
   158 	/* call is compatible! */
   146 
   159 
   147 	/* In the case of a call to an extensible function, we store the highest index 
   160 	/* In the case of a call to an extensible function, we store the highest index 
   148 	 * of the extensible parameters this particular call uses, in the symbol_c object
   161 	 * of the extensible parameters this particular call uses, in the symbol_c object
   994 
  1007 
   995 
  1008 
   996 void *narrow_candidate_datatypes_c::visit(function_invocation_c *symbol) {
  1009 void *narrow_candidate_datatypes_c::visit(function_invocation_c *symbol) {
   997 	int  ext_parm_count;
  1010 	int  ext_parm_count;
   998 
  1011 
   999 	/* set the called_function_declaration taking into account the datatype that we need to return */
  1012 	/* set the called_function_declaration. */
  1000 	symbol->called_function_declaration = NULL;
  1013 	symbol->called_function_declaration = NULL;
  1001 	for(unsigned int i = 0; i < symbol->candidate_datatypes.size(); i++) {
  1014 	if (symbol->candidate_datatypes.size() == 1) {
  1002 		if (is_type_equal(symbol->candidate_datatypes[i], symbol->datatype)) {
  1015 		/* If only one possible called function, then that is the function to call!
  1003 			symbol->called_function_declaration = symbol->candidate_functions[i];
  1016 		 * In this case we ignore the symbol->datatype value (that may even be NULL).
  1004 			break;
  1017 		 * This helps in identifying potential errors in the expressions used inside this function call
  1005 		}
  1018 		 * even if there is a previous error, allowing us to make a more thorough analysis of the semantics
  1006 	}
  1019 		 * of the ST code, and providing as many relevant error messages as possible!
  1007 	if (NULL == symbol->called_function_declaration) ERROR;
  1020 		 * If symbol->datatype isn't NULL, then this chosen function should be returning the required datatype,
       
  1021 		 * otherwise we have a bug in our stage3 code!
       
  1022 		 */
       
  1023 		symbol->called_function_declaration = symbol->candidate_functions[0];
       
  1024 		if ((NULL != symbol->datatype) && (!is_type_equal(symbol->candidate_datatypes[0], symbol->datatype)))
       
  1025 			ERROR;
       
  1026 	}
       
  1027 	else {
       
  1028 		/* set the called_function_declaration taking into account the datatype that we need to return */
       
  1029 		symbol->called_function_declaration = NULL;
       
  1030 		for(unsigned int i = 0; i < symbol->candidate_datatypes.size(); i++) {
       
  1031 			if (is_type_equal(symbol->candidate_datatypes[i], symbol->datatype)) {
       
  1032 				symbol->called_function_declaration = symbol->candidate_functions[i];
       
  1033 				break;
       
  1034 			}
       
  1035 		}
       
  1036 	}
       
  1037 	/* NOTE: If we can't figure out the declaration of the function being called, this is not 
       
  1038 	 *       necessarily an internal compiler error. It could be because the symbol->datatype is NULL
       
  1039 	 *       (because the ST code being analysed has an error _before_ this function invocation).
       
  1040 	 *       However, we don't just give, up, we carry on recursivly analysing the code, so as to be
       
  1041 	 *       able to print out any error messages related to underlying code that could be partially correct.
       
  1042 	 */
       
  1043 	/* if (NULL == symbol->called_function_declaration) ERROR; */
  1008 	
  1044 	
  1009 	if (NULL != symbol->nonformal_param_list)  narrow_nonformal_call(symbol, symbol->called_function_declaration, &ext_parm_count);
  1045 	if (NULL != symbol->nonformal_param_list)  narrow_nonformal_call(symbol, symbol->called_function_declaration, &ext_parm_count);
  1010 	if (NULL != symbol->   formal_param_list)     narrow_formal_call(symbol, symbol->called_function_declaration, &ext_parm_count);
  1046 	if (NULL != symbol->   formal_param_list)     narrow_formal_call(symbol, symbol->called_function_declaration, &ext_parm_count);
  1011 	symbol->extensible_param_count = ext_parm_count;
  1047 	symbol->extensible_param_count = ext_parm_count;
  1012 
  1048 
  1037 /*****************************************/
  1073 /*****************************************/
  1038 /* B 3.2.2 Subprogram Control Statements */
  1074 /* B 3.2.2 Subprogram Control Statements */
  1039 /*****************************************/
  1075 /*****************************************/
  1040 
  1076 
  1041 void *narrow_candidate_datatypes_c::visit(fb_invocation_c *symbol) {
  1077 void *narrow_candidate_datatypes_c::visit(fb_invocation_c *symbol) {
       
  1078 	/* Note: We do not use the symbol->called_fb_declaration value (set in fill_candidate_datatypes_c)
       
  1079 	 *       because we try to identify any other datatype errors in the expressions used in the 
       
  1080 	 *       parameters to the FB call (e.g.  fb_var(var1 * 56 + func(var * 43)) )
       
  1081 	 *       even it the call to the FB is invalid. 
       
  1082 	 *       This makes sense because it may be errors in those expressions which are
       
  1083 	 *       making this an invalid call, so it makes sense to point them out to the user!
       
  1084 	 */
  1042 	symbol_c *fb_decl = search_varfb_instance_type->get_basetype_decl(symbol->fb_name);
  1085 	symbol_c *fb_decl = search_varfb_instance_type->get_basetype_decl(symbol->fb_name);
       
  1086 
       
  1087 	/* Although a call to a non-declared FB is a semantic error, this is currently caught by stage 2! */
  1043 	if (NULL == fb_decl) ERROR;
  1088 	if (NULL == fb_decl) ERROR;
  1044 	if (NULL != symbol->nonformal_param_list)  narrow_nonformal_call(symbol, fb_decl);
  1089 	if (NULL != symbol->nonformal_param_list)  narrow_nonformal_call(symbol, fb_decl);
  1045 	if (NULL != symbol->   formal_param_list)     narrow_formal_call(symbol, fb_decl);
  1090 	if (NULL != symbol->   formal_param_list)     narrow_formal_call(symbol, fb_decl);
  1046 
  1091 
  1047 	return NULL;
  1092 	return NULL;