lbessard@70: /* lbessard@70: * (c) 2003 Mario de Sousa lbessard@70: * lbessard@70: * Offered to the public under the terms of the GNU General Public License lbessard@70: * as published by the Free Software Foundation; either version 2 of the lbessard@70: * License, or (at your option) any later version. lbessard@70: * lbessard@70: * This program is distributed in the hope that it will be useful, but lbessard@70: * WITHOUT ANY WARRANTY; without even the implied warranty of lbessard@70: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General lbessard@70: * Public License for more details. lbessard@70: * lbessard@70: * This code is made available on the understanding that it will not be lbessard@70: * used in safety-critical situations without a full and competent review. lbessard@70: */ lbessard@70: lbessard@70: /* lbessard@70: * An IEC 61131-3 IL and ST compiler. lbessard@70: * lbessard@70: * Based on the lbessard@70: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) lbessard@70: * lbessard@70: */ lbessard@70: lbessard@70: lbessard@70: lbessard@70: /* Determine the data type of a variable. lbessard@70: * The variable may be a simple variable, a function block instance, a lbessard@70: * struture element within a data structured type (a struct or a fb), or lbessard@70: * an array element. lbessard@70: * A mixture of array element of a structure element of a structure element lbessard@70: * of a .... is also suported! lbessard@70: * lbessard@70: * A reference to the relevant base type __definition__ is returned. lbessard@70: * This means that if we find that the variable is of type MY_INT, lbessard@70: * which was previously declared to be lbessard@70: * TYPE MY_INT: INT := 9; lbessard@70: * this class wil return INT, and __not__ MY_INT !! lbessard@70: * lbessard@70: * lbessard@70: * example: lbessard@70: * window.points[1].coordinate.x lbessard@70: * window.points[1].colour lbessard@70: * etc... ARE ALLOWED! lbessard@70: * lbessard@70: * This class must be passed the scope within which the lbessard@70: * variable was declared, and the variable name... lbessard@70: */ lbessard@70: class search_varfb_instance_type_c: public search_base_type_c { lbessard@70: private: lbessard@70: search_var_instance_decl_c search_var_instance_decl; lbessard@70: decompose_var_instance_name_c *decompose_var_instance_name; lbessard@70: symbol_c *current_structelement_name; lbessard@70: lbessard@70: public: lbessard@70: search_varfb_instance_type_c(symbol_c *search_scope): search_var_instance_decl(search_scope) { lbessard@70: this->decompose_var_instance_name = NULL; lbessard@70: this->current_structelement_name = NULL; lbessard@70: } lbessard@70: lbessard@70: lbessard@70: symbol_c *get_type(symbol_c *variable_name) { lbessard@70: this->current_structelement_name = NULL; lbessard@70: this->decompose_var_instance_name = new decompose_var_instance_name_c(variable_name); lbessard@70: if (NULL == decompose_var_instance_name) ERROR; lbessard@70: lbessard@70: /* find the part of the variable name that will appear in the lbessard@70: * variable declaration, for e.g., in window.point.x, this would be lbessard@70: * window! lbessard@70: */ lbessard@70: symbol_c *var_name_part = decompose_var_instance_name->next_part(); lbessard@70: if (NULL == var_name_part) ERROR; lbessard@70: lbessard@70: /* Now we try to find the variable instance declaration, to determine its type... */ lbessard@70: symbol_c *var_decl = search_var_instance_decl.get_decl(var_name_part); lbessard@70: if (NULL == var_decl) { lbessard@70: /* variable instance declaration not found! */ lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: /* if it is a struct or function block, we must search the type lbessard@70: * of the struct or function block member. lbessard@70: * This is done by this class visiting the var_decl. lbessard@70: * This class, while visiting, will recursively call lbessard@70: * decompose_var_instance_name->get_next() when and if required... lbessard@70: */ lbessard@70: symbol_c *res = (symbol_c *)var_decl->accept(*this); lbessard@70: if (NULL == res) ERROR; lbessard@70: lbessard@70: /* make sure that we have decomposed all strcuture elements of the variable name */ lbessard@70: symbol_c *var_name = decompose_var_instance_name->next_part(); lbessard@70: if (NULL != var_name) ERROR; lbessard@70: lbessard@70: return res; lbessard@70: } lbessard@70: lbessard@70: unsigned int get_vartype(symbol_c *variable_name) { lbessard@70: this->current_structelement_name = NULL; lbessard@70: this->decompose_var_instance_name = new decompose_var_instance_name_c(variable_name); lbessard@70: if (NULL == decompose_var_instance_name) ERROR; lbessard@70: lbessard@70: /* find the part of the variable name that will appear in the lbessard@70: * variable declaration, for e.g., in window.point.x, this would be lbessard@70: * window! lbessard@70: */ lbessard@70: symbol_c *var_name_part = decompose_var_instance_name->next_part(); lbessard@70: if (NULL == var_name_part) ERROR; lbessard@70: lbessard@70: /* Now we try to find the variable instance declaration, to determine its type... */ lbessard@70: symbol_c *var_decl = search_var_instance_decl.get_decl(var_name_part); lbessard@70: if (NULL == var_decl) { lbessard@70: /* variable instance declaration not found! */ lbessard@70: return 0; lbessard@70: } lbessard@70: lbessard@70: /* if it is a struct or function block, we must search the type lbessard@70: * of the struct or function block member. lbessard@70: * This is done by this class visiting the var_decl. lbessard@70: * This class, while visiting, will recursively call lbessard@70: * decompose_var_instance_name->get_next() when and if required... lbessard@70: */ lbessard@70: unsigned int res = search_var_instance_decl.get_vartype(); lbessard@70: lbessard@70: /* make sure that we have decomposed all strcuture elements of the variable name */ lbessard@70: symbol_c *var_name = decompose_var_instance_name->next_part(); lbessard@70: if (NULL != var_name) ERROR; lbessard@70: lbessard@70: return res; lbessard@70: } lbessard@70: lbessard@70: private: lbessard@70: /* a helper function... */ lbessard@70: void *visit_list(list_c *list) { lbessard@70: if (NULL == current_structelement_name) ERROR; lbessard@70: lbessard@70: for(int i = 0; i < list->n; i++) { lbessard@70: void *res = list->elements[i]->accept(*this); lbessard@70: if (res != NULL) lbessard@70: return res; lbessard@70: } lbessard@70: /* not found! */ lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: /* a helper function... */ lbessard@70: void *base_type(symbol_c *symbol) { lbessard@70: search_base_type_c search_base_type; lbessard@70: return symbol->accept(search_base_type); lbessard@70: } lbessard@70: lbessard@70: lbessard@70: private: lbessard@70: /* We override the base class' visitor to identifier_c. lbessard@70: * This is so because the base class does not consider a function block lbessard@70: * to be a type, unlike this class that allows a variable instance lbessard@70: * of a function block type... lbessard@70: */ lbessard@70: void *visit(identifier_c *type_name) { lbessard@70: /* look up the type declaration... */ lbessard@70: symbol_c *fb_decl = function_block_type_symtable.find_value(type_name); lbessard@70: if (fb_decl != function_block_type_symtable.end_value()) lbessard@70: /* Type declaration found!! */ lbessard@70: return fb_decl->accept(*this); lbessard@70: lbessard@70: /* No. It is not a function block, so we let lbessard@70: * the base class take care of it... lbessard@70: */ lbessard@70: return search_base_type_c::visit(type_name); lbessard@70: } lbessard@70: lbessard@70: /********************************/ lbessard@70: /* B 1.3.3 - Derived data types */ lbessard@70: /********************************/ lbessard@70: /* structure_type_name ':' structure_specification */ lbessard@70: void *visit(structure_type_declaration_c *symbol) { lbessard@70: return symbol->structure_specification->accept(*this); lbessard@70: /* NOTE: structure_specification will point to either a lbessard@70: * initialized_structure_c lbessard@70: * OR A lbessard@70: * structure_element_declaration_list_c lbessard@70: */ lbessard@70: } lbessard@70: lbessard@70: /* structure_type_name ASSIGN structure_initialization */ lbessard@70: /* structure_initialization may be NULL ! */ lbessard@70: // SYM_REF2(initialized_structure_c, structure_type_name, structure_initialization) lbessard@70: void *visit(initialized_structure_c *symbol) { lbessard@70: /* make sure that we have decomposed all strcuture elements of the variable name */ lbessard@70: symbol_c *var_name = decompose_var_instance_name->next_part(); lbessard@70: if (NULL == var_name) { lbessard@70: /* this is it... ! lbessard@70: * No need to look any further... lbessard@70: */ lbessard@70: /* NOTE: we could simply do a lbessard@70: * return (void *)symbol; lbessard@70: * nevertheless, note that this search_varfb_instance_type_c lbessard@70: * class inherits from the search_base_type_c class, lbessard@70: * which means that it will usually return the base type, lbessard@70: * and not the derived type (*). If we are to be consistent, lbessard@70: * we should guarantee that we always return the base type. lbessard@70: * To do this we could use lbessard@70: * return (void *)symbol->accept(*this); lbessard@70: * since this class inherits from the search_base_type_c. lbessard@70: * However, in this case we don't want it to follow lbessard@70: * the structs as this search_varfb_instance_type_c does. lbessard@70: * We therefore have to create a new search_base_type_c lbessard@70: * instance to search through this type without going lbessard@70: * through the structs... lbessard@70: */ lbessard@70: return base_type(symbol->structure_type_name); lbessard@70: } lbessard@70: lbessard@70: /* now search the structure declaration */ lbessard@70: current_structelement_name = var_name; lbessard@70: /* recursively find out the data type of var_name... */ lbessard@70: return symbol->structure_type_name->accept(*this); lbessard@70: } lbessard@70: lbessard@70: /* helper symbol for structure_declaration */ lbessard@70: /* structure_declaration: STRUCT structure_element_declaration_list END_STRUCT */ lbessard@70: /* structure_element_declaration_list structure_element_declaration ';' */ lbessard@70: void *visit(structure_element_declaration_list_c *symbol) {return visit_list(symbol);} lbessard@70: lbessard@70: /* structure_element_name ':' spec_init */ lbessard@70: void *visit(structure_element_declaration_c *symbol) { lbessard@70: if (NULL == current_structelement_name) ERROR; lbessard@70: lbessard@70: if (compare_identifiers(symbol->structure_element_name, current_structelement_name) == 0) lbessard@70: return symbol->spec_init->accept(*this); lbessard@70: lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: /* helper symbol for structure_initialization */ lbessard@70: /* structure_initialization: '(' structure_element_initialization_list ')' */ lbessard@70: /* structure_element_initialization_list ',' structure_element_initialization */ lbessard@70: void *visit(structure_element_initialization_list_c *symbol) {ERROR; return NULL;} /* should never get called... */ lbessard@70: /* structure_element_name ASSIGN value */ lbessard@70: void *visit(structure_element_initialization_c *symbol) {ERROR; return NULL;} /* should never get called... */ lbessard@70: lbessard@70: lbessard@70: lbessard@70: /**************************************/ lbessard@70: /* B.1.5 - Program organization units */ lbessard@70: /**************************************/ lbessard@70: /*****************************/ lbessard@70: /* B 1.5.2 - Function Blocks */ lbessard@70: /*****************************/ lbessard@70: /* FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */ lbessard@70: // SYM_REF4(function_block_declaration_c, fblock_name, var_declarations, fblock_body, unused) lbessard@70: void *visit(function_block_declaration_c *symbol) { lbessard@70: /* make sure that we have decomposed all strcuture elements of the variable name */ lbessard@70: lbessard@70: symbol_c *var_name = decompose_var_instance_name->next_part(); lbessard@70: if (NULL == var_name) { lbessard@70: /* this is it... ! lbessard@70: * No need to look any further... lbessard@70: * Note also that, unlike for the struct types, a function block may lbessard@70: * not be defined based on another (i.e. no inheritance is allowed), lbessard@70: * so this function block is already the most base type. lbessard@70: * We simply return it. lbessard@70: */ lbessard@70: return (void *)symbol; lbessard@70: } lbessard@70: lbessard@70: /* now search the function block declaration for the variable... */ lbessard@70: search_var_instance_decl_c search_decl(symbol); lbessard@70: symbol_c *var_decl = search_decl.get_decl(var_name); lbessard@70: if (NULL == var_decl) { lbessard@70: /* variable instance declaration not found! */ lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: /* We have found the declaration. lbessard@70: * Should we look any further? lbessard@70: */ lbessard@70: var_name = decompose_var_instance_name->next_part(); lbessard@70: if (NULL == var_name) { lbessard@70: /* this is it... ! */ lbessard@70: return base_type(var_decl); lbessard@70: } lbessard@70: lbessard@70: current_structelement_name = var_name; lbessard@70: /* recursively find out the data type of var_name... */ lbessard@70: return symbol->var_declarations->accept(*this); lbessard@70: } lbessard@70: lbessard@70: }; lbessard@70: lbessard@70: lbessard@70: lbessard@70: lbessard@70: