Add checks for undefined structure elements used IEC 61131-3 source code being compiled.
authorMario de Sousa <msousa@fe.up.pt>
Fri, 26 Aug 2011 10:48:09 +0100
changeset 367 6d94128ba5ad
parent 366 1aeb29ee9381
child 368 63cee5103e8f
Add checks for undefined structure elements used IEC 61131-3 source code being compiled.
absyntax_utils/search_varfb_instance_type.cc
stage3/visit_expression_type.cc
--- a/absyntax_utils/search_varfb_instance_type.cc	Thu Aug 25 17:55:48 2011 +0100
+++ b/absyntax_utils/search_varfb_instance_type.cc	Fri Aug 26 10:48:09 2011 +0100
@@ -124,11 +124,22 @@
    * decompose_var_instance_name->get_next() when and if required...
    */
   symbol_c *res = (symbol_c *)var_decl->accept(*this);
-  if (NULL == res) ERROR;
+  /* NOTE: A Null result is not really an internal compiler error, but rather an error in 
+   * the IEC 61131-3 source code being compiled. This means we cannot just abort the compiler with ERROR.
+   * //   if (NULL == res) ERROR;
+   */
+  if (NULL == res) return NULL;
 
   /* make sure that we have decomposed all structure elements of the variable name */
   symbol_c *var_name = decompose_var_instance_name->next_part();
-  if (NULL != var_name) ERROR;
+  /* NOTE: A non-NULL result is not really an internal compiler error, but rather an error in 
+   * the IEC 61131-3 source code being compiled. 
+   *  (for example, 'int_var.struct_elem' in the source code, when 'int_var' is a simple integer,
+   *   and not a structure, will result in this result being non-NULL!)
+   * This means we cannot just abort the compiler with ERROR.
+   * //   if (NULL != var_name) ERROR;
+   */
+  if (NULL != var_name) return NULL;
 
   return res;
 }
--- a/stage3/visit_expression_type.cc	Thu Aug 25 17:55:48 2011 +0100
+++ b/stage3/visit_expression_type.cc	Fri Aug 26 10:48:09 2011 +0100
@@ -2074,10 +2074,14 @@
     if (hi2 != NULL) printf("%s", hi2->value);
     printf("\n");
   } // if (debug)
-  
-  if (!is_valid_assignment(left_type, right_type))  {
-     STAGE3_ERROR(symbol, symbol, "data type mismatch in assignment statement!\n");
-  }
+
+  if        (NULL == left_type) {
+    STAGE3_ERROR(symbol->l_exp, symbol->l_exp, "Could not determine data type of expression (undefined variable or strcuture element?).\n");
+  } else if (NULL == right_type) {
+    STAGE3_ERROR(symbol->r_exp, symbol->r_exp, "Could not determine data type of expression (undefined variable or strcuture element?).\n");
+  } else if (!is_valid_assignment(left_type, right_type))
+    STAGE3_ERROR(symbol, symbol, "data type mismatch in assignment statement!\n");
+
   return NULL;
 }