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