stage4/generate_cc/search_varfb_instance_type.cc
changeset 0 fb772792efd1
child 25 e6a841e365b7
equal deleted inserted replaced
-1:000000000000 0:fb772792efd1
       
     1 /*
       
     2  * (c) 2003 Mario de Sousa
       
     3  *
       
     4  * Offered to the public under the terms of the GNU General Public License
       
     5  * as published by the Free Software Foundation; either version 2 of the
       
     6  * License, or (at your option) any later version.
       
     7  *
       
     8  * This program is distributed in the hope that it will be useful, but
       
     9  * WITHOUT ANY WARRANTY; without even the implied warranty of
       
    10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
       
    11  * Public License for more details.
       
    12  *
       
    13  * This code is made available on the understanding that it will not be
       
    14  * used in safety-critical situations without a full and competent review.
       
    15  */
       
    16 
       
    17 /*
       
    18  * An IEC 61131-3 IL and ST compiler.
       
    19  *
       
    20  * Based on the
       
    21  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
       
    22  *
       
    23  */
       
    24 
       
    25 
       
    26 
       
    27 /* Determine the data type of a variable.
       
    28  * The variable may be a simple variable, a function block instance, a
       
    29  * struture element within a data structured type (a struct or a fb), or
       
    30  * an array element.
       
    31  * A mixture of array element of a structure element of a structure element
       
    32  * of a .... is also suported!
       
    33  *
       
    34  * A reference to the relevant base type __definition__ is returned.
       
    35  * This means that if we find that the variable is of type MY_INT,
       
    36  * which was previously declared to be
       
    37  * TYPE MY_INT: INT := 9;
       
    38  * this class wil return INT, and __not__ MY_INT !!
       
    39  *
       
    40  *
       
    41  *  example:
       
    42  *    window.points[1].coordinate.x
       
    43  *    window.points[1].colour
       
    44  *    etc... ARE ALLOWED!
       
    45  *
       
    46  * This class must be passed the scope within which the
       
    47  * variable was declared, and the variable name...
       
    48  */
       
    49 class search_varfb_instance_type_c: public search_base_type_c {
       
    50   private:
       
    51     search_var_instance_decl_c search_var_instance_decl;
       
    52     decompose_var_instance_name_c *decompose_var_instance_name;
       
    53     symbol_c *current_structelement_name;
       
    54 
       
    55   public:
       
    56     search_varfb_instance_type_c(symbol_c *search_scope): search_var_instance_decl(search_scope) {
       
    57       this->decompose_var_instance_name = NULL;
       
    58       this->current_structelement_name = NULL;
       
    59     }
       
    60 
       
    61 
       
    62     symbol_c *get_type(symbol_c *variable_name) {
       
    63       this->current_structelement_name = NULL;
       
    64       this->decompose_var_instance_name = new decompose_var_instance_name_c(variable_name);
       
    65       if (NULL == decompose_var_instance_name) ERROR;
       
    66 
       
    67       /* find the part of the variable name that will appear in the
       
    68        * variable declaration, for e.g., in window.point.x, this would be
       
    69        * window!
       
    70        */
       
    71       symbol_c *var_name_part = decompose_var_instance_name->next_part();
       
    72       if (NULL == var_name_part) ERROR;
       
    73 
       
    74       /* Now we try to find the variable instance declaration, to determine its type... */
       
    75       symbol_c *var_decl = search_var_instance_decl.get_decl(var_name_part);
       
    76       if (NULL == var_decl) {
       
    77         /* variable instance declaration not found! */
       
    78  	return NULL;
       
    79       }
       
    80 
       
    81       /* if it is a struct or function block, we must search the type
       
    82        * of the struct or function block member.
       
    83        * This is done by this class visiting the var_decl.
       
    84        * This class, while visiting, will recursively call
       
    85        * decompose_var_instance_name->get_next() when and if required...
       
    86        */
       
    87       symbol_c *res = (symbol_c *)var_decl->accept(*this);
       
    88       if (NULL == res) ERROR;
       
    89 
       
    90       /* make sure that we have decomposed all strcuture elements of the variable name */
       
    91       symbol_c *var_name = decompose_var_instance_name->next_part();
       
    92       if (NULL != var_name) ERROR;
       
    93 
       
    94       return res;
       
    95     }
       
    96 
       
    97   private:
       
    98     /* a helper function... */
       
    99     void *visit_list(list_c *list)	{
       
   100       if (NULL == current_structelement_name) ERROR;
       
   101 
       
   102       for(int i = 0; i < list->n; i++) {
       
   103         void *res = list->elements[i]->accept(*this);
       
   104         if (res != NULL)
       
   105           return res;
       
   106       }
       
   107       /* not found! */
       
   108       return NULL;
       
   109     }
       
   110 
       
   111     /* a helper function... */
       
   112     void *base_type(symbol_c *symbol)	{
       
   113         search_base_type_c search_base_type;
       
   114 	return symbol->accept(search_base_type);
       
   115     }
       
   116 
       
   117 
       
   118   private:
       
   119     /* We override the base class' visitor to identifier_c.
       
   120      * This is so because the base class does not consider a function block
       
   121      * to be a type, unlike this class that allows a variable instance
       
   122      * of a function block type...
       
   123      */
       
   124     void *visit(identifier_c *type_name) {
       
   125       /* look up the type declaration... */
       
   126       symbol_c *fb_decl = function_block_type_symtable.find_value(type_name);
       
   127       if (fb_decl != function_block_type_symtable.end_value())
       
   128         /* Type declaration found!! */
       
   129 	return fb_decl->accept(*this);
       
   130 
       
   131       /* No. It is not a function block, so we let
       
   132        * the base class take care of it...
       
   133        */
       
   134       return search_base_type_c::visit(type_name);
       
   135     }
       
   136 
       
   137 /********************************/
       
   138 /* B 1.3.3 - Derived data types */
       
   139 /********************************/
       
   140 /*  structure_type_name ':' structure_specification */
       
   141     void *visit(structure_type_declaration_c *symbol) {
       
   142       return symbol->structure_specification->accept(*this);
       
   143       /* NOTE: structure_specification will point to either a
       
   144        *       initialized_structure_c
       
   145        *       OR A
       
   146        *       structure_element_declaration_list_c
       
   147        */
       
   148     }
       
   149 
       
   150 /* structure_type_name ASSIGN structure_initialization */
       
   151 /* structure_initialization may be NULL ! */
       
   152 // SYM_REF2(initialized_structure_c, structure_type_name, structure_initialization)
       
   153     void *visit(initialized_structure_c *symbol)	{
       
   154       /* make sure that we have decomposed all strcuture elements of the variable name */
       
   155       symbol_c *var_name = decompose_var_instance_name->next_part();
       
   156       if (NULL == var_name) {
       
   157         /* this is it... !
       
   158 	  * No need to look any further...
       
   159 	 */
       
   160 	/* NOTE: we could simply do a
       
   161 	 *   return (void *)symbol;
       
   162 	 *       nevertheless, note that this search_varfb_instance_type_c
       
   163 	 *       class inherits from the search_base_type_c class,
       
   164 	 *       which means that it will usually return the base type,
       
   165 	 *       and not the derived type (*). If we are to be consistent,
       
   166 	 *       we should guarantee that we always return the base type.
       
   167 	 *       To do this we could use
       
   168 	 *   return (void *)symbol->accept(*this);
       
   169 	 *       since this class inherits from the search_base_type_c.
       
   170 	 *       However, in this case we don't want it to follow
       
   171 	 *       the structs as this search_varfb_instance_type_c does.
       
   172 	 *       We therefore have to create a new search_base_type_c
       
   173 	 *       instance to search through this type without going
       
   174 	 *       through the structs...
       
   175 	 */
       
   176         return base_type(symbol->structure_type_name);
       
   177       }
       
   178 
       
   179       /* now search the structure declaration */
       
   180       current_structelement_name = var_name;
       
   181       /* recursively find out the data type of var_name... */
       
   182       return symbol->structure_type_name->accept(*this);
       
   183     }
       
   184 
       
   185 /* helper symbol for structure_declaration */
       
   186 /* structure_declaration:  STRUCT structure_element_declaration_list END_STRUCT */
       
   187 /* structure_element_declaration_list structure_element_declaration ';' */
       
   188     void *visit(structure_element_declaration_list_c *symbol)	{return visit_list(symbol);}
       
   189 
       
   190 /*  structure_element_name ':' spec_init */
       
   191     void *visit(structure_element_declaration_c *symbol) {
       
   192       if (NULL == current_structelement_name) ERROR;
       
   193 
       
   194       if (compare_identifiers(symbol->structure_element_name, current_structelement_name) == 0)
       
   195         return symbol->spec_init->accept(*this);
       
   196 
       
   197       return NULL;
       
   198     }
       
   199 
       
   200 /* helper symbol for structure_initialization */
       
   201 /* structure_initialization: '(' structure_element_initialization_list ')' */
       
   202 /* structure_element_initialization_list ',' structure_element_initialization */
       
   203     void *visit(structure_element_initialization_list_c *symbol) {ERROR; return NULL;} /* should never get called... */
       
   204 /*  structure_element_name ASSIGN value */
       
   205     void *visit(structure_element_initialization_c *symbol) {ERROR; return NULL;} /* should never get called... */
       
   206 
       
   207 
       
   208 
       
   209 /**************************************/
       
   210 /* B.1.5 - Program organization units */
       
   211 /**************************************/
       
   212 /*****************************/
       
   213 /* B 1.5.2 - Function Blocks */
       
   214 /*****************************/
       
   215 /*  FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */
       
   216 // SYM_REF4(function_block_declaration_c, fblock_name, var_declarations, fblock_body, unused)
       
   217     void *visit(function_block_declaration_c *symbol) {
       
   218       /* make sure that we have decomposed all strcuture elements of the variable name */
       
   219 
       
   220       symbol_c *var_name = decompose_var_instance_name->next_part();
       
   221       if (NULL == var_name) {
       
   222         /* this is it... !
       
   223 	 * No need to look any further...
       
   224 	 * Note also that, unlike for the struct types, a function block may
       
   225 	 * not be defined based on another (i.e. no inheritance is allowed),
       
   226 	 * so this function block is already the most base type.
       
   227 	 * We simply return it.
       
   228 	 */
       
   229 	return (void *)symbol;
       
   230       }
       
   231 
       
   232       /* now search the function block declaration for the variable... */
       
   233       search_var_instance_decl_c search_decl(symbol);
       
   234       symbol_c *var_decl = search_decl.get_decl(var_name);
       
   235       if (NULL == var_decl) {
       
   236         /* variable instance declaration not found! */
       
   237  	return NULL;
       
   238       }
       
   239 
       
   240       /* We have found the declaration.
       
   241        * Should we look any further?
       
   242        */
       
   243       var_name = decompose_var_instance_name->next_part();
       
   244       if (NULL == var_name) {
       
   245         /* this is it... ! */
       
   246 	return base_type(var_decl);
       
   247       }
       
   248 
       
   249       current_structelement_name = var_name;
       
   250       /* recursively find out the data type of var_name... */
       
   251       return symbol->var_declarations->accept(*this);
       
   252     }
       
   253 
       
   254 };
       
   255 
       
   256 
       
   257 
       
   258 
       
   259