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: /* Returns the function block type declaration etisserant@0: * of a specific function block instance. etisserant@0: */ etisserant@0: etisserant@0: etisserant@0: etisserant@0: /* Returns the type name of a specific function block etisserant@0: * instance. This class will search the variable etisserant@0: * declarations inside the scope given to it etisserant@0: * searching for the declaration of the function etisserant@0: * block instance. etisserant@0: * etisserant@0: * The class constructor must be given the search scope etisserant@0: * (function, function block or program within which etisserant@0: * the function block instance was declared). etisserant@0: * etisserant@0: * This class will search the tree from the root given to the etisserant@0: * constructor. Another option would be to build a symbol table, etisserant@0: * and search that instead. Building the symbol table would be done etisserant@0: * while visiting the variable declaration objects in the parse etisserant@0: * tree. Unfortuantely, generate_cc_c does not visit these etisserant@0: * objects, delegating it to another class. This means that etisserant@0: * we would need another specialised class just to build the etisserant@0: * symbol table. We might just as well have a specialised class etisserant@0: * that searches the tree itself for the relevant info. This etisserant@0: * class is exactly that...! etisserant@0: */ etisserant@0: class search_fb_instance_decl_c: public search_visitor_c { etisserant@0: etisserant@0: private: etisserant@0: symbol_c *search_scope; etisserant@0: etisserant@0: symbol_c *search_name; etisserant@0: symbol_c *current_fb_type_name; etisserant@0: etisserant@0: public: etisserant@0: search_fb_instance_decl_c(symbol_c *search_scope) { etisserant@0: this->search_scope = search_scope; etisserant@0: this->current_fb_type_name = NULL; etisserant@0: } etisserant@0: etisserant@0: symbol_c *get_type_name(symbol_c *fb_instance_name) { etisserant@0: this->search_name = fb_instance_name; etisserant@0: return (symbol_c *)search_scope->accept(*this); etisserant@0: } etisserant@0: etisserant@0: public: etisserant@0: /***************************/ etisserant@0: /* B 0 - Programming Model */ etisserant@0: /***************************/ etisserant@0: void *visit(library_c *symbol) { etisserant@0: /* we do not want to search multiple declaration scopes, etisserant@0: * so we do not visit all the functions, fucntion blocks, etc... etisserant@0: */ etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: /******************************************/ etisserant@0: /* B 1.4.3 - Declaration & Initialisation */ etisserant@0: /******************************************/ etisserant@0: etisserant@0: /* name_list ':' function_block_type_name ASSIGN structure_initialization */ etisserant@0: /* structure_initialization -> may be NULL ! */ etisserant@0: void *visit(fb_name_decl_c *symbol) { etisserant@0: current_fb_type_name = symbol->function_block_type_name; etisserant@0: return symbol->fb_name_list->accept(*this); etisserant@0: } etisserant@0: etisserant@0: /* name_list ',' fb_name */ etisserant@0: void *visit(fb_name_list_c *symbol) { etisserant@0: list_c *list = symbol; etisserant@0: for(int i = 0; i < list->n; i++) { etisserant@0: if (compare_identifiers(list->elements[i], search_name) == 0) etisserant@0: /* by now, current_fb_declaration should be != NULL */ etisserant@0: return current_fb_type_name; etisserant@0: } etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: /**************************************/ etisserant@0: /* B.1.5 - Program organization units */ etisserant@0: /**************************************/ etisserant@0: /***********************/ etisserant@0: /* B 1.5.1 - Functions */ etisserant@0: /***********************/ etisserant@0: void *visit(function_declaration_c *symbol) { etisserant@0: /* no need to search through all the body, so we only etisserant@0: * visit the variable declarations...! etisserant@0: */ etisserant@0: return symbol->var_declarations_list->accept(*this); etisserant@0: } etisserant@0: etisserant@0: /*****************************/ etisserant@0: /* B 1.5.2 - Function Blocks */ etisserant@0: /*****************************/ etisserant@0: void *visit(function_block_declaration_c *symbol) { etisserant@0: /* no need to search through all the body, so we only etisserant@0: * visit the variable declarations...! etisserant@0: */ etisserant@0: return symbol->var_declarations->accept(*this); etisserant@0: } etisserant@0: etisserant@0: /**********************/ etisserant@0: /* B 1.5.3 - Programs */ etisserant@0: /**********************/ etisserant@0: void *visit(program_declaration_c *symbol) { etisserant@0: /* no need to search through all the body, so we only etisserant@0: * visit the variable declarations...! etisserant@0: */ etisserant@0: return symbol->var_declarations->accept(*this); etisserant@0: } etisserant@0: }; etisserant@0: etisserant@0: etisserant@0: etisserant@0: