Adding support for checking direct variable declared before using
authorlbessard
Mon, 18 May 2009 13:19:30 +0200
changeset 175 dadda1b5401e
parent 174 f648065fb66e
child 176 bef3cc16c064
Adding support for checking direct variable declared before using
stage1_2/iec.flex
stage1_2/iec.y
stage1_2/stage1_2.cc
stage1_2/stage1_2_priv.hh
stage4/generate_c/search_varfb_instance_type.cc
--- a/stage1_2/iec.flex	Fri May 15 10:23:20 2009 +0200
+++ b/stage1_2/iec.flex	Mon May 18 13:19:30 2009 +0200
@@ -1408,7 +1408,7 @@
 	/********************************************/
 	/* B.1.4.1   Directly Represented Variables */
 	/********************************************/
-{direct_variable}	{yylval.ID=strdup(yytext); return direct_variable_token;}
+{direct_variable}   {yylval.ID=strdup(yytext); return get_direct_variable_token(yytext);}
 
 
 	/******************************************/
@@ -1563,6 +1563,7 @@
 
 
 int get_identifier_token(const char *identifier_str) {return 0;}
+int get_direct_variable_token(const char *direct_variable_str) {return 0;}
 
 
 
--- a/stage1_2/iec.y	Fri May 15 10:23:20 2009 +0200
+++ b/stage1_2/iec.y	Mon May 18 13:19:30 2009 +0200
@@ -235,8 +235,10 @@
 %type <leaf>	any_identifier
 
 %token <ID>	prev_declared_variable_name_token
+%token <ID>	prev_declared_direct_variable_token
 %token <ID>	prev_declared_fb_name_token
 %type <leaf>	prev_declared_variable_name
+%type <leaf>	prev_declared_direct_variable
 %type <leaf>	prev_declared_fb_name
 
 %token  <ID>	prev_declared_simple_type_name_token
@@ -621,7 +623,7 @@
  size_prefix
 */
 %token <ID>	direct_variable_token
-%type  <leaf>	direct_variable
+//%type  <leaf>	direct_variable
 
 
 /*************************************/
@@ -2967,7 +2969,7 @@
 /*********************/
 variable:
   symbolic_variable
-| direct_variable
+| prev_declared_direct_variable
 | eno_param
 ;
 
@@ -3017,7 +3019,7 @@
 /********************************************/
 /* B.1.4.1   Directly Represented Variables */
 /********************************************/
-direct_variable: direct_variable_token	{$$ = new direct_variable_c($1, locloc(@$));};
+prev_declared_direct_variable: prev_declared_direct_variable_token	{$$ = new direct_variable_c($1, locloc(@$));};
 
 
 
@@ -3856,8 +3858,10 @@
 
 
 location:
-  AT direct_variable
-	{$$ = new location_c($2, locloc(@$));}
+  AT direct_variable_token
+	{$$ = new location_c(new direct_variable_c($2, locloc(@$)), locloc(@$));
+	 direct_variable_symtable.insert($2, prev_declared_direct_variable_token);
+	}
 /* ERROR_CHECK_BEGIN */
 | AT error
 	{$$ = NULL;
@@ -4315,6 +4319,7 @@
   function_name_declaration ':' elementary_type_name io_OR_function_var_declarations_list function_body END_FUNCTION
 	{$$ = new function_declaration_c($1, $3, $4, $5, locloc(@$));
 	 variable_name_symtable.pop();
+	 direct_variable_symtable.pop();
 	 if (allow_function_overloading) {
 	   switch (library_element_symtable.find_value($1)) {
 	     case prev_declared_derived_function_name_token:
@@ -4336,6 +4341,7 @@
 | function_name_declaration ':' derived_type_name io_OR_function_var_declarations_list function_body END_FUNCTION
 	{$$ = new function_declaration_c($1, $3, $4, $5, locloc(@$));
 	 variable_name_symtable.pop();
+	 direct_variable_symtable.pop();
 	 if (allow_function_overloading) {
 	   switch (library_element_symtable.find_value($1)) {
 	     case prev_declared_derived_function_name_token: /* do nothing, already in map. */ break;
@@ -4551,6 +4557,7 @@
 	  * are no longer valid!
 	  */
 	 variable_name_symtable.pop();
+	 direct_variable_symtable.pop();
 	}
 /* ERROR_CHECK_BEGIN */
 | FUNCTION_BLOCK io_OR_other_var_declarations_list function_block_body END_FUNCTION_BLOCK
@@ -4702,6 +4709,7 @@
 	  * are no longer valid!
 	  */
 	 variable_name_symtable.pop();
+	 direct_variable_symtable.pop();
 	}
 /* ERROR_CHECK_BEGIN */
 | PROGRAM program_var_declarations_list function_block_body END_PROGRAM
@@ -5165,13 +5173,15 @@
   CONFIGURATION configuration_name
    optional_global_var_declarations
    single_resource_declaration
-   {variable_name_symtable.pop();}
+   {variable_name_symtable.pop();
+    direct_variable_symtable.pop();}
    optional_access_declarations
    optional_instance_specific_initializations
   END_CONFIGURATION
 	{$$ = new configuration_declaration_c($2, $3, $4, $6, $7, locloc(@$));
 	 library_element_symtable.insert($2, prev_declared_configuration_name_token);
 	 variable_name_symtable.pop();
+	 direct_variable_symtable.pop();
 	}
 | CONFIGURATION configuration_name
    optional_global_var_declarations
@@ -5182,12 +5192,14 @@
 	{$$ = new configuration_declaration_c($2, $3, $4, $5, $6, locloc(@$));
 	 library_element_symtable.insert($2, prev_declared_configuration_name_token);
 	 variable_name_symtable.pop();
+	 direct_variable_symtable.pop();
 }
 /* ERROR_CHECK_BEGIN */
 | CONFIGURATION 
    optional_global_var_declarations
    single_resource_declaration
-   {variable_name_symtable.pop();}
+   {variable_name_symtable.pop();
+    direct_variable_symtable.pop();}
    optional_access_declarations
    optional_instance_specific_initializations
   END_CONFIGURATION
@@ -5202,7 +5214,8 @@
 | CONFIGURATION error
    optional_global_var_declarations
    single_resource_declaration
-   {variable_name_symtable.pop();}
+   {variable_name_symtable.pop();
+    direct_variable_symtable.pop();}
    optional_access_declarations
    optional_instance_specific_initializations
   END_CONFIGURATION
@@ -5230,7 +5243,8 @@
 /*| CONFIGURATION configuration_name
    optional_global_var_declarations
    single_resource_declaration
-   {variable_name_symtable.pop();}
+   {variable_name_symtable.pop();
+    direct_variable_symtable.pop();}
    optional_access_declarations
    optional_instance_specific_initializations
   END_OF_INPUT
@@ -5286,21 +5300,22 @@
 
 
 resource_declaration:
-  RESOURCE {variable_name_symtable.push();} resource_name ON resource_type_name
+  RESOURCE {variable_name_symtable.push();direct_variable_symtable.push();} resource_name ON resource_type_name
    optional_global_var_declarations
    single_resource_declaration
   END_RESOURCE
 	{$$ = new resource_declaration_c($3, $5, $6, $7, locloc(@$));
 	 variable_name_symtable.pop();
+	 direct_variable_symtable.pop();
 	 variable_name_symtable.insert($3, prev_declared_resource_name_token);
 	}
 /* ERROR_CHECK_BEGIN */
-| RESOURCE {variable_name_symtable.push();} ON resource_type_name
+| RESOURCE {variable_name_symtable.push();direct_variable_symtable.push();} ON resource_type_name
    optional_global_var_declarations
    single_resource_declaration
   END_RESOURCE
   {$$ = NULL; print_err_msg(locl(@1), locf(@3), "no resource name defined in resource declaration."); yynerrs++;}
-/*|	RESOURCE {variable_name_symtable.push();} resource_name ON resource_type_name
+/*|	RESOURCE {variable_name_symtable.push();direct_variable_symtable.push();} resource_name ON resource_type_name
    optional_global_var_declarations
    single_resource_declaration
   END_OF_INPUT
@@ -5397,8 +5412,8 @@
 
 
 access_path:
-  direct_variable
-| prev_declared_resource_name '.' direct_variable
+  prev_declared_direct_variable
+| prev_declared_resource_name '.' prev_declared_direct_variable
 | any_fb_name_list symbolic_variable
 | prev_declared_resource_name '.' any_fb_name_list symbolic_variable
 | prev_declared_program_name '.'  any_fb_name_list symbolic_variable
@@ -5566,7 +5581,7 @@
   constant
 | global_var_reference
 | program_output_reference
-| direct_variable
+| prev_declared_direct_variable
 ;
 
 program_configuration:
@@ -5758,12 +5773,12 @@
   constant
 | enumerated_value
 | global_var_reference
-| direct_variable
+| prev_declared_direct_variable
 ;
 
 data_sink:
   global_var_reference
-| direct_variable
+| prev_declared_direct_variable
 ;
 
 instance_specific_initializations:
--- a/stage1_2/stage1_2.cc	Fri May 15 10:23:20 2009 +0200
+++ b/stage1_2/stage1_2.cc	Mon May 18 13:19:30 2009 +0200
@@ -134,6 +134,10 @@
  */
 /* static */ symtable_c<int, BOGUS_TOKEN_ID> variable_name_symtable;
 
+/* A symbol table to store the declared direct variables of
+ * the function currently being parsed...
+ */
+/* static */ symtable_c<int, BOGUS_TOKEN_ID> direct_variable_symtable;
 
 /* Function only called from within flex!
  *
@@ -153,6 +157,19 @@
   return token_id;
 }
 
+/* Function only called from within flex!
+ *
+ * search for a symbol in direct variables symbol table
+ * declared above, and return the token id of the first
+ * symbol found.
+ */
+int get_direct_variable_token(const char *direct_variable_str) {
+  int token_id;
+
+  if ((token_id = direct_variable_symtable.find_value(direct_variable_str)) == direct_variable_symtable.end_value())
+    return direct_variable_token;
+  return token_id;
+}
 
 /************************/
 /* Utility Functions... */
--- a/stage1_2/stage1_2_priv.hh	Fri May 15 10:23:20 2009 +0200
+++ b/stage1_2/stage1_2_priv.hh	Mon May 18 13:19:30 2009 +0200
@@ -165,6 +165,11 @@
  */
 extern symtable_c<int, BOGUS_TOKEN_ID> variable_name_symtable;
 
+/* A symbol table to store the declared direct variables of
+ * the function currently being parsed...
+ */
+extern symtable_c<int, BOGUS_TOKEN_ID> direct_variable_symtable;
+
 /* Function only called from within flex!
  *
  * search for a symbol in either of the two symbol tables
@@ -175,6 +180,13 @@
  */
 int get_identifier_token(const char *identifier_str);
 
+/* Function only called from within flex!
+ *
+ * search for a symbol in direct variables symbol table
+ * declared above, and return the token id of the first
+ * symbol found.
+ */
+int get_direct_variable_token(const char *direct_variable_str);
 
 
 /************************/
--- a/stage4/generate_c/search_varfb_instance_type.cc	Fri May 15 10:23:20 2009 +0200
+++ b/stage4/generate_c/search_varfb_instance_type.cc	Mon May 18 13:19:30 2009 +0200
@@ -77,7 +77,7 @@
       symbol_c *var_decl = search_var_instance_decl.get_decl(var_name_part);
       if (NULL == var_decl) {
         /* variable instance declaration not found! */
- 	      return NULL;
+ 	      ERROR;
       }
 
       /* if it is a struct or function block, we must search the type