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