Continue checking data type compatibility inside expressions used to pass paramters to invalid function/FB calls
authorMario de Sousa <msousa@fe.up.pt>
Fri, 03 Feb 2012 14:43:14 +0000
changeset 424 43d73e28eca8
parent 423 f4a2d400ddbd
child 425 c8e6cf57324a
Continue checking data type compatibility inside expressions used to pass paramters to invalid function/FB calls
absyntax/absyntax.def
absyntax_utils/function_param_iterator.cc
absyntax_utils/function_param_iterator.hh
stage3/fill_candidate_datatypes.cc
stage3/narrow_candidate_datatypes.cc
--- a/absyntax/absyntax.def	Fri Feb 03 10:54:35 2012 +0000
+++ b/absyntax/absyntax.def	Fri Feb 03 14:43:14 2012 +0000
@@ -1099,7 +1099,15 @@
 /* fb_name '(' [param_assignment_list] ')' */
 /*    formal_param_list -> may be NULL ! */
 /* nonformal_param_list -> may be NULL ! */
-SYM_REF3(fb_invocation_c, fb_name, formal_param_list, nonformal_param_list)
+/* NOTES:
+ *    The parameter 'called_fb_declaration'... 
+ *       ...is used to pass data between two passes of stage 3.
+ *       (actually set in fill_candidate_datatypes_c, and used in narrow_candidate_datatypes_c and print_datatypes_error_c).
+ *       This allows fill_candidate_datatypes_c to figure out whether it is a valid FB call,
+ *       and let the other classes handle it aproproately.
+ *       It could also be used in stage 4, if required.
+ */
+SYM_REF3(fb_invocation_c, fb_name, formal_param_list, nonformal_param_list, symbol_c *called_fb_declaration;)
 
 /* helper symbol for fb_invocation */
 /* param_assignment_list ',' param_assignment */
--- a/absyntax_utils/function_param_iterator.cc	Fri Feb 03 10:54:35 2012 +0000
+++ b/absyntax_utils/function_param_iterator.cc	Fri Feb 03 14:43:14 2012 +0000
@@ -206,7 +206,9 @@
   _first_extensible_param_index = -1;
   current_param_is_extensible = false;
   current_param_name = NULL;
-  current_param_type = current_param_default_value = NULL;
+  current_param_type = NULL;
+  current_param_default_value = NULL;
+  last_returned_parameter = NULL; /* the last parameter returned by search() or next() */
 }
 
 
@@ -248,6 +250,7 @@
     return current_param_name;
   }
   
+  last_returned_parameter = NULL; 
   param_count = 0;
   en_eno_param_implicit = false;
   next_param++;
@@ -268,6 +271,7 @@
   if (identifier == NULL)
     ERROR;
   current_param_name = identifier;
+  last_returned_parameter = current_param_name; 
   return current_param_name;
 }
 
@@ -281,6 +285,7 @@
   current_operation = function_param_iterator_c::search_op;
   void *res = f_decl->accept(*this);
   identifier_c *res_param_name = dynamic_cast<identifier_c *>((symbol_c *)res);
+  last_returned_parameter = res_param_name; 
   return res_param_name;
 }
 
@@ -288,28 +293,38 @@
  * or NULL if none is specified in the function declrataion itself.
  */
 symbol_c *function_param_iterator_c::default_value(void) {
+  if (NULL == last_returned_parameter) 
+    return NULL;
   return current_param_default_value;
 }
 
 /* Returns the currently referenced parameter's type name. */
 symbol_c *function_param_iterator_c::param_type(void) {
+  if (NULL == last_returned_parameter) 
+    return NULL;
   return current_param_type;
 }
 
 /* Returns if currently referenced parameter is an implicit defined EN/ENO parameter. */
 bool function_param_iterator_c::is_en_eno_param_implicit(void) {
+  if (NULL == last_returned_parameter) 
+    ERROR;
   return en_eno_param_implicit;
 }
 
 /* Returns if currently referenced parameter is an extensible parameter. */
 /* extensible paramters only occur in some standard functions, e.g. AND(word#34, word#44, word#65); */
 bool function_param_iterator_c::is_extensible_param(void) {
+  if (NULL == last_returned_parameter) 
+    ERROR;
   return current_param_is_extensible;
 }
 
 /* Returns the index of the current extensible parameter. */             
 /* If the current parameter is not an extensible paramter, returns -1 */
 int function_param_iterator_c::extensible_param_index(void) {
+  if (NULL == last_returned_parameter) 
+    ERROR;
   return (current_param_is_extensible? current_extensible_param_index : -1);
 }
 
@@ -323,6 +338,8 @@
  * i.e. VAR_INPUT, VAR_OUTPUT or VAR_INOUT
  */
 function_param_iterator_c::param_direction_t function_param_iterator_c::param_direction(void) {
+  if (NULL == last_returned_parameter) 
+    ERROR;
   return current_param_direction;
 }
 
--- a/absyntax_utils/function_param_iterator.hh	Fri Feb 03 10:54:35 2012 +0000
+++ b/absyntax_utils/function_param_iterator.hh	Fri Feb 03 14:43:14 2012 +0000
@@ -105,7 +105,10 @@
      */
     typedef enum {iterate_op, search_op} operation_t;
     operation_t current_operation;
-
+    
+    /* the last parameter/value returned by search() or next() */
+    symbol_c *last_returned_parameter; 
+    
   private:
     int   cmp_extparam_names(const char* s1, const char* s2);
     void* handle_param_list(list_c *list);
--- a/stage3/fill_candidate_datatypes.cc	Fri Feb 03 10:54:35 2012 +0000
+++ b/stage3/fill_candidate_datatypes.cc	Fri Feb 03 14:43:14 2012 +0000
@@ -67,6 +67,7 @@
 
 
 /* returns true if compatible function/FB invocation, otherwise returns false */
+/* Assumes that the candidate_datatype lists of all the parameters being passed haved already been filled in */
 bool fill_candidate_datatypes_c::match_nonformal_call(symbol_c *f_call, symbol_c *f_decl) {
 	symbol_c *call_param_value,  *param_type;
 	identifier_c *param_name;
@@ -104,6 +105,7 @@
 
 
 /* returns true if compatible function/FB invocation, otherwise returns false */
+/* Assumes that the candidate_datatype lists of all the parameters being passed haved already been filled in */
 bool fill_candidate_datatypes_c::match_formal_call(symbol_c *f_call, symbol_c *f_decl) {
 	symbol_c *call_param_value, *call_param_name, *param_type;
 	symbol_c *verify_duplicate_param;
@@ -130,7 +132,6 @@
 		/* Obtaining the type of the value being passed in the function call */
 		std::vector <symbol_c *>&call_param_types = call_param_value->candidate_datatypes;
 
-
 		/* Find the corresponding parameter in function declaration */
 		param_name = fp_iterator.search(call_param_name);
 		if(param_name == NULL) return false;
@@ -1671,6 +1672,9 @@
 	list_c *parameter_list;
 	list_c *parameter_candidate_datatypes;
 	symbol_c *returned_parameter_type;
+
+	if (debug) std::cout << "function()\n";
+
 	function_symtable_t::iterator lower = function_symtable.lower_bound(symbol->function_name);
 	function_symtable_t::iterator upper = function_symtable.upper_bound(symbol->function_name);
 	/* If the name of the function being called is not found in the function symbol table, then this is an invalid call */
@@ -1683,8 +1687,12 @@
 		parameter_list = (list_c *)symbol->nonformal_param_list;
 	else ERROR;
 	
-	if (debug) std::cout << "function()\n";
+	/* Fill in the candidate_datatypes lists of all the expressions used in the function call parameters */
 	parameter_list->accept(*this);
+
+	/* Look for all compatible function declarations, and add their return datatypes 
+	 * to the candidate_datatype list of this function invocation. 
+	 */
 	for(; lower != upper; lower++) {
 		bool compatible = false;
 		
@@ -1750,8 +1758,9 @@
 void *fill_candidate_datatypes_c::visit(fb_invocation_c *symbol) {
 	bool compatible = false;
 	symbol_c *fb_decl = search_varfb_instance_type->get_basetype_decl(symbol->fb_name);
-	
+	/* Although a call to a non-declared FB is a semantic error, this is currently caught by stage 2! */
 	if (NULL == fb_decl) ERROR;
+
 	if (symbol->   formal_param_list != NULL) {
 		symbol->formal_param_list->accept(*this);
 		compatible = match_formal_call(symbol, fb_decl);
@@ -1760,6 +1769,10 @@
 		symbol->nonformal_param_list->accept(*this);
 		compatible = match_nonformal_call(symbol, fb_decl);
 	}
+
+	if (compatible) 
+		symbol->called_fb_declaration = fb_decl;
+
 	if (debug) std::cout << "FB [] ==> "  << symbol->candidate_datatypes.size() << " result.\n";
 	return NULL;
 }
--- a/stage3/narrow_candidate_datatypes.cc	Fri Feb 03 10:54:35 2012 +0000
+++ b/stage3/narrow_candidate_datatypes.cc	Fri Feb 03 14:43:14 2012 +0000
@@ -84,20 +84,26 @@
 		do {
 			param_name = fp_iterator.next();
 			/* If there is no other parameter declared, then we are passing too many parameters... */
-			/* This error should have been caught in fill_candidate_datatypes_c, but may occur here again when we handle FB invocations! */
-			if(param_name == NULL) return;
+			/* This error should have been caught in fill_candidate_datatypes_c, but may occur here again when we handle FB invocations! 
+			 * In this case, we carry on analysing the code in order to be able to provide relevant error messages
+			 * for that code too!
+			 */
+			if(param_name == NULL) break;
 		} while ((strcmp(param_name->value, "EN") == 0) || (strcmp(param_name->value, "ENO") == 0));
 
 		/* Set the desired datatype for this parameter, and call it recursively. */
+		/* Note that if the call has more parameters than those declared in the function/FB declaration,
+		 * we may be setting this to NULL!
+		 */
 		call_param_value->datatype = base_type(fp_iterator.param_type());
-		if (NULL == call_param_value->datatype) ERROR;
+		if ((NULL != param_name) && (NULL == call_param_value->datatype)) ERROR;
+		if ((NULL == param_name) && (NULL != call_param_value->datatype)) ERROR;
 		call_param_value->accept(*this);
 
-		if (extensible_parameter_highest_index < fp_iterator.extensible_param_index())
-			extensible_parameter_highest_index = fp_iterator.extensible_param_index();
-	}
-	/* call is compatible! */
-
+		if (NULL != param_name) 
+			if (extensible_parameter_highest_index < fp_iterator.extensible_param_index())
+				extensible_parameter_highest_index = fp_iterator.extensible_param_index();
+	}
 	/* In the case of a call to an extensible function, we store the highest index 
 	 * of the extensible parameters this particular call uses, in the symbol_c object
 	 * of the function call itself!
@@ -135,12 +141,19 @@
 		param_name = fp_iterator.search(call_param_name);
 
 		/* Set the desired datatype for this parameter, and call it recursively. */
+		/* NOTE: When handling a FB call, this narrow_formal_call() may be called to analyse
+		 *       an invalid FB call (call with parameters that do not exist on the FB declaration).
+		 *       For this reason, the param_name may come out as NULL!
+		 */
 		call_param_value->datatype = base_type(fp_iterator.param_type());
-		if (NULL == call_param_value->datatype) ERROR;
+		if ((NULL != param_name) && (NULL == call_param_value->datatype)) ERROR;
+		if ((NULL == param_name) && (NULL != call_param_value->datatype)) ERROR;
+
 		call_param_value->accept(*this);
 
-		if (extensible_parameter_highest_index < fp_iterator.extensible_param_index())
-			extensible_parameter_highest_index = fp_iterator.extensible_param_index();
+		if (NULL != param_name) 
+			if (extensible_parameter_highest_index < fp_iterator.extensible_param_index())
+				extensible_parameter_highest_index = fp_iterator.extensible_param_index();
 	}
 	/* call is compatible! */
 
@@ -996,15 +1009,38 @@
 void *narrow_candidate_datatypes_c::visit(function_invocation_c *symbol) {
 	int  ext_parm_count;
 
-	/* set the called_function_declaration taking into account the datatype that we need to return */
+	/* set the called_function_declaration. */
 	symbol->called_function_declaration = NULL;
-	for(unsigned int i = 0; i < symbol->candidate_datatypes.size(); i++) {
-		if (is_type_equal(symbol->candidate_datatypes[i], symbol->datatype)) {
-			symbol->called_function_declaration = symbol->candidate_functions[i];
-			break;
-		}
-	}
-	if (NULL == symbol->called_function_declaration) ERROR;
+	if (symbol->candidate_datatypes.size() == 1) {
+		/* If only one possible called function, then that is the function to call!
+		 * In this case we ignore the symbol->datatype value (that may even be NULL).
+		 * This helps in identifying potential errors in the expressions used inside this function call
+		 * even if there is a previous error, allowing us to make a more thorough analysis of the semantics
+		 * of the ST code, and providing as many relevant error messages as possible!
+		 * If symbol->datatype isn't NULL, then this chosen function should be returning the required datatype,
+		 * otherwise we have a bug in our stage3 code!
+		 */
+		symbol->called_function_declaration = symbol->candidate_functions[0];
+		if ((NULL != symbol->datatype) && (!is_type_equal(symbol->candidate_datatypes[0], symbol->datatype)))
+			ERROR;
+	}
+	else {
+		/* set the called_function_declaration taking into account the datatype that we need to return */
+		symbol->called_function_declaration = NULL;
+		for(unsigned int i = 0; i < symbol->candidate_datatypes.size(); i++) {
+			if (is_type_equal(symbol->candidate_datatypes[i], symbol->datatype)) {
+				symbol->called_function_declaration = symbol->candidate_functions[i];
+				break;
+			}
+		}
+	}
+	/* NOTE: If we can't figure out the declaration of the function being called, this is not 
+	 *       necessarily an internal compiler error. It could be because the symbol->datatype is NULL
+	 *       (because the ST code being analysed has an error _before_ this function invocation).
+	 *       However, we don't just give, up, we carry on recursivly analysing the code, so as to be
+	 *       able to print out any error messages related to underlying code that could be partially correct.
+	 */
+	/* if (NULL == symbol->called_function_declaration) ERROR; */
 	
 	if (NULL != symbol->nonformal_param_list)  narrow_nonformal_call(symbol, symbol->called_function_declaration, &ext_parm_count);
 	if (NULL != symbol->   formal_param_list)     narrow_formal_call(symbol, symbol->called_function_declaration, &ext_parm_count);
@@ -1039,7 +1075,16 @@
 /*****************************************/
 
 void *narrow_candidate_datatypes_c::visit(fb_invocation_c *symbol) {
+	/* Note: We do not use the symbol->called_fb_declaration value (set in fill_candidate_datatypes_c)
+	 *       because we try to identify any other datatype errors in the expressions used in the 
+	 *       parameters to the FB call (e.g.  fb_var(var1 * 56 + func(var * 43)) )
+	 *       even it the call to the FB is invalid. 
+	 *       This makes sense because it may be errors in those expressions which are
+	 *       making this an invalid call, so it makes sense to point them out to the user!
+	 */
 	symbol_c *fb_decl = search_varfb_instance_type->get_basetype_decl(symbol->fb_name);
+
+	/* Although a call to a non-declared FB is a semantic error, this is currently caught by stage 2! */
 	if (NULL == fb_decl) ERROR;
 	if (NULL != symbol->nonformal_param_list)  narrow_nonformal_call(symbol, fb_decl);
 	if (NULL != symbol->   formal_param_list)     narrow_formal_call(symbol, fb_decl);