absyntax_utils/search_var_instance_decl.cc
changeset 504 f8d422b98315
parent 417 d48f53715f77
child 507 30b31d8f6d0f
equal deleted inserted replaced
503:e1e7c7678c44 504:f8d422b98315
    28  * Based on the
    28  * Based on the
    29  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
    29  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
    30  *
    30  *
    31  */
    31  */
    32 
    32 
    33 /* Determine the data type of a specific variable instance, including
    33 /* Search in a VAR* END_VAR declaration for the delcration of the specified variable instance. 
    34  * function block instances.
    34  * Will return:
    35  * A reference to the relevant variable declaration is returned.
    35  *     - the declaration itself (get_decl() )
    36  * The variable instance may NOT be a member of a structure of a member
    36  *     - the type of declaration in which the variable was declared (get_vartype() )
       
    37  *
       
    38  * The variable instance may NOT be a member of a structure of a memeber
    37  * of a structure of an element of an array of ...
    39  * of a structure of an element of an array of ...
    38  *
    40  *
    39  * example:
    41  * For example, considering the following 'variables':
    40  *    window.points[1].coordinate.x
    42  *    window.points[1].coordinate.x
    41  *    window.points[1].colour
    43  *    window.points[1].colour
    42  *    etc... ARE NOT ALLOWED!
    44  *    offset[99]
       
    45  *
       
    46  *   passing a reference to 'points', 'points[1]', 'points[1].colour', 'colour'
       
    47  *    ARE NOT ALLOWED!
    43  *
    48  *
    44  * This class must only be passed the name of the variable that will appear
    49  * This class must only be passed the name of the variable that will appear
    45  * in the variable declaration. In the above examples, this would be
    50  * in the variable declaration. In the above examples, this would be
    46  *   'window' !!
    51  *   'window.points[1].coordinate.x'
    47  *
    52  *   'window.points[1].coordinate'
    48  *
    53  *   'window.points[1]'
    49  * If you need to pass a complete name of a variable instance (such as
    54  *   'window'
    50  * 'window.points[1].coordinate.x') use the search_varfb_instance_type_c instead!
    55  *   'window.points[1].colour'
    51  */
    56  *   'offset'
    52 
    57  *   'offset[99]'
    53 /* Note that current_type_decl that this class returns may reference the
    58  *
       
    59  *
       
    60  */
       
    61  
       
    62 /* Note: 
       
    63  * Determining the declaration type of a specific variable instance (including
       
    64  * function block instances) really means determining whether the variable was declared in a
       
    65  *  VAR_INPUT
       
    66  *  VAR_OUTPUT
       
    67  *  VAR_IN_OUT
       
    68  *  VAR
       
    69  *  VAR_TEMP
       
    70  *  VAR_EXTERNAL
       
    71  *  VAR_GLOBAL
       
    72  *  VAR <var_name> AT <location>   -> Located variable!
       
    73  * 
       
    74  */
       
    75 
       
    76 /* Note:
       
    77  *  The current_type_decl that this class returns may reference the
    54  * name of a type, or the type declaration itself!
    78  * name of a type, or the type declaration itself!
    55  * For an example of the first, consider a variable declared as ...
    79  * For an example of the first, consider a variable declared as ...
    56  * x : AAA;
    80  * x : AAA;
    57  * where the AAA type is previously declared as whatever.
    81  * where the AAA type is previously declared as whatever.
    58  * For an example of the second, consider a variable declared as ...
    82  * For an example of the second, consider a variable declared as ...
    59  * x : array of int [10];  ---->  is allowed
    83  * x : array of int [10];  ---->  is allowed
    60  *
    84  *
    61  * If it is the first, we will return a reference to the name, if the second
    85  * If it is the first, we will return a reference to the name, if the second
    62  * we return a reference to the declaration!!
    86  * we return a reference to the declaration!!
    63  */
    87  */
       
    88 
       
    89 
    64 #include "absyntax_utils.hh"
    90 #include "absyntax_utils.hh"
       
    91 
       
    92 
       
    93 
       
    94 
       
    95 /**********************************************/
       
    96 /*  A small helper visitor class, that will   */
       
    97 /*  return the name of a variable, as it will */
       
    98 /*  appear in the variable declaration.       */
       
    99 /**********************************************/
       
   100 /*  For ex.:
       
   101  *       VAR
       
   102  *          A : int;
       
   103  *          B : ARRAY [1..9] of int;
       
   104  *          C : some_struct_t;
       
   105  *       END_VAR
       
   106  *
       
   107  *          A    := 56;
       
   108  *          B[8] := 99;
       
   109  *          C.e  := 77;
       
   110  *
       
   111  *       Calling this visitor class with symbolic_variable_c instance referencing 'A' in
       
   112  *       the line 'A := 56', will return the string "A".
       
   113  *
       
   114  *       Calling this visitor class with array_variable_c instance referencing 'B[8]' in
       
   115  *       the line 'B[8] := 99', will return the string "B".
       
   116  *
       
   117  *       Calling this visitor class with array_variable_c instance referencing 'C.e' in
       
   118  *       the line 'C.e := 77', will return the string "C".
       
   119  */
       
   120 class get_var_name_c : public search_visitor_c {
       
   121   private:
       
   122     static get_var_name_c *singleton_instance_;
       
   123   public:
       
   124     get_var_name_c(void)  {};
       
   125     ~get_var_name_c(void) {};
       
   126     
       
   127     static token_c *get_name(symbol_c *symbol) {
       
   128       if (NULL == singleton_instance_) singleton_instance_ = new get_var_name_c(); 
       
   129       if (NULL == singleton_instance_) ERROR; 
       
   130       
       
   131       return (token_c *)(symbol->accept(*singleton_instance_));
       
   132     }
       
   133     
       
   134   private:  
       
   135     /*************************/
       
   136     /* B.1 - Common elements */
       
   137     /*************************/
       
   138     /*******************************************/
       
   139     /* B 1.1 - Letters, digits and identifiers */
       
   140     /*******************************************/
       
   141     // SYM_TOKEN(identifier_c)
       
   142     void *visit(identifier_c *symbol);
       
   143 
       
   144     /*********************/
       
   145     /* B 1.4 - Variables */
       
   146     /*********************/
       
   147     // SYM_REF2(symbolic_variable_c, var_name, unused)
       
   148     void *visit(symbolic_variable_c *symbol);
       
   149 
       
   150     /********************************************/
       
   151     /* B.1.4.1   Directly Represented Variables */
       
   152     /********************************************/
       
   153     // SYM_TOKEN(direct_variable_c)
       
   154     // void *visit(direct_variable_c *symbol);
       
   155 
       
   156     /*************************************/
       
   157     /* B.1.4.2   Multi-element Variables */
       
   158     /*************************************/
       
   159     /*  subscripted_variable '[' subscript_list ']' */
       
   160     // SYM_REF2(array_variable_c, subscripted_variable, subscript_list)
       
   161     void *visit(array_variable_c *symbol);
       
   162 
       
   163     /* subscript_list ',' subscript */
       
   164     // SYM_LIST(subscript_list_c)
       
   165     // void *visit(subscript_list_c *symbol);
       
   166 
       
   167     /*  record_variable '.' field_selector */
       
   168     /*  WARNING: input and/or output variables of function blocks
       
   169      *           may be accessed as fields of a tructured variable!
       
   170      *           Code handling a structured_variable_c must take
       
   171      *           this into account!
       
   172      */
       
   173     // SYM_REF2(structured_variable_c, record_variable, field_selector)
       
   174     void *visit(structured_variable_c *symbol);
       
   175 };
       
   176 
       
   177 get_var_name_c *get_var_name_c::singleton_instance_ = NULL;
       
   178 
       
   179 
       
   180 
       
   181 /*************************/
       
   182 /* B.1 - Common elements */
       
   183 /*************************/
       
   184 /*******************************************/
       
   185 /* B 1.1 - Letters, digits and identifiers */
       
   186 /*******************************************/
       
   187 // SYM_TOKEN(identifier_c)
       
   188 void *get_var_name_c::visit(identifier_c *symbol) {return (void *)symbol;}
       
   189 
       
   190 /*********************/
       
   191 /* B 1.4 - Variables */
       
   192 /*********************/
       
   193 // SYM_REF2(symbolic_variable_c, var_name, unused)
       
   194 void *get_var_name_c::visit(symbolic_variable_c *symbol) {return symbol->var_name->accept(*this);}
       
   195 
       
   196 /********************************************/
       
   197 /* B.1.4.1   Directly Represented Variables */
       
   198 /********************************************/
       
   199 // SYM_TOKEN(direct_variable_c)
       
   200 
       
   201 /*************************************/
       
   202 /* B.1.4.2   Multi-element Variables */
       
   203 /*************************************/
       
   204 /*  subscripted_variable '[' subscript_list ']' */
       
   205 // SYM_REF2(array_variable_c, subscripted_variable, subscript_list)
       
   206 void *get_var_name_c::visit(array_variable_c *symbol) {return symbol->subscripted_variable->accept(*this);}
       
   207 
       
   208 /* subscript_list ',' subscript */
       
   209 // SYM_LIST(subscript_list_c)
       
   210 
       
   211 /*  record_variable '.' field_selector */
       
   212 /*  WARNING: input and/or output variables of function blocks
       
   213  *           may be accessed as fields of a tructured variable!
       
   214  *           Code handling a structured_variable_c must take
       
   215  *           this into account!
       
   216  */
       
   217 // SYM_REF2(structured_variable_c, record_variable, field_selector)
       
   218 void *get_var_name_c::visit(structured_variable_c *symbol) {return symbol->record_variable->accept(*this);}
       
   219 
       
   220 
       
   221 
       
   222 
       
   223 
       
   224 
       
   225 
       
   226 
       
   227 
    65 
   228 
    66 
   229 
    67 search_var_instance_decl_c::search_var_instance_decl_c(symbol_c *search_scope) {
   230 search_var_instance_decl_c::search_var_instance_decl_c(symbol_c *search_scope) {
    68   this->current_vartype = none_vt;
   231   this->current_vartype = none_vt;
    69   this->search_scope = search_scope;
   232   this->search_scope = search_scope;
    70   this->search_name = NULL;
   233   this->search_name = NULL;
    71   this->current_type_decl = NULL;
   234   this->current_type_decl = NULL;
    72 }
   235 }
    73 
   236 
    74 symbol_c *search_var_instance_decl_c::get_decl(symbol_c *variable_instance_name) {
   237 symbol_c *search_var_instance_decl_c::get_decl(symbol_c *variable) {
    75   this->current_vartype = none_vt;
   238   this->current_vartype = none_vt;
    76   this->search_name = variable_instance_name;
   239   this->search_name = get_var_name_c::get_name(variable);
    77   return (symbol_c *)search_scope->accept(*this);
   240   return (symbol_c *)search_scope->accept(*this);
    78 }
   241 }
    79 
   242 
    80 unsigned int search_var_instance_decl_c::get_vartype(symbol_c *variable_instance_name) {
   243 unsigned int search_var_instance_decl_c::get_vartype(symbol_c *variable) {
    81   this->current_vartype = none_vt;
   244   this->current_vartype = none_vt;
    82   this->search_name = variable_instance_name;
   245   this->search_name = get_var_name_c::get_name(variable);
    83   search_scope->accept(*this);
   246   search_scope->accept(*this);
    84   return current_vartype;
   247   return current_vartype;
    85 }
   248 }
    86 
   249 
    87 
   250 
   234 /* name_list ',' fb_name */
   397 /* name_list ',' fb_name */
   235 void *search_var_instance_decl_c::visit(fb_name_list_c *symbol) {
   398 void *search_var_instance_decl_c::visit(fb_name_list_c *symbol) {
   236   list_c *list = symbol;
   399   list_c *list = symbol;
   237   for(int i = 0; i < list->n; i++) {
   400   for(int i = 0; i < list->n; i++) {
   238     if (compare_identifiers(list->elements[i], search_name) == 0)
   401     if (compare_identifiers(list->elements[i], search_name) == 0)
   239   /* by now, current_fb_declaration should be != NULL */
   402     /* by now, current_fb_declaration should be != NULL */
   240       return current_type_decl;
   403       return current_type_decl;
   241   }
   404   }
   242   return NULL;
   405   return NULL;
   243 }
   406 }
   244 
   407 
   471    * and there is no global variable declarations...!
   634    * and there is no global variable declarations...!
   472    */
   635    */
   473   return NULL;
   636   return NULL;
   474 }
   637 }
   475 
   638 
   476 #if 0
       
   477 /*********************/
       
   478 /* B 1.4 - Variables */
       
   479 /*********************/
       
   480 SYM_REF2(symbolic_variable_c, var_name, unused)
       
   481 
       
   482 /********************************************/
       
   483 /* B.1.4.1   Directly Represented Variables */
       
   484 /********************************************/
       
   485 SYM_TOKEN(direct_variable_c)
       
   486 
       
   487 /*************************************/
       
   488 /* B.1.4.2   Multi-element Variables */
       
   489 /*************************************/
       
   490 /*  subscripted_variable '[' subscript_list ']' */
       
   491 SYM_REF2(array_variable_c, subscripted_variable, subscript_list)
       
   492 
       
   493 /* subscript_list ',' subscript */
       
   494 SYM_LIST(subscript_list_c)
       
   495 
       
   496 /*  record_variable '.' field_selector */
       
   497 /*  WARNING: input and/or output variables of function blocks
       
   498  *           may be accessed as fields of a tructured variable!
       
   499  *           Code handling a structured_variable_c must take
       
   500  *           this into account!
       
   501  */
       
   502 SYM_REF2(structured_variable_c, record_variable, field_selector)
       
   503 
       
   504 
       
   505 
       
   506 };
       
   507 #endif