mario@181: /* msousa@265: * matiec - a compiler for the programming languages defined in IEC 61131-3 mario@181: * msousa@265: * Copyright (C) 2003-2011 Mario de Sousa (msousa@fe.up.pt) Edouard@279: * Copyright (C) 2007-2011 Laurent Bessard and Edouard Tisserant mario@181: * msousa@265: * This program is free software: you can redistribute it and/or modify msousa@265: * it under the terms of the GNU General Public License as published by msousa@265: * the Free Software Foundation, either version 3 of the License, or msousa@265: * (at your option) any later version. msousa@265: * msousa@265: * This program is distributed in the hope that it will be useful, msousa@265: * but WITHOUT ANY WARRANTY; without even the implied warranty of msousa@265: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the msousa@265: * GNU General Public License for more details. msousa@265: * msousa@265: * You should have received a copy of the GNU General Public License msousa@265: * along with this program. If not, see . msousa@265: * mario@181: * mario@181: * This code is made available on the understanding that it will not be mario@181: * used in safety-critical situations without a full and competent review. mario@181: */ mario@181: mario@181: /* msousa@265: * An IEC 61131-3 compiler. mario@181: * mario@181: * Based on the mario@181: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) mario@181: * mario@181: */ mario@181: mario@181: #include "absyntax_utils.hh" mario@181: mario@181: mario@181: /* Returns the function block type declaration mario@181: * of a specific function block instance. mario@181: */ mario@181: mario@181: mario@181: mario@181: /* Returns the type name of a specific function block mario@181: * instance. This class will search the variable mario@181: * declarations inside the scope given to it mario@181: * searching for the declaration of the function mario@181: * block instance. mario@181: * mario@181: * The class constructor must be given the search scope mario@181: * (function, function block or program within which mario@181: * the function block instance was declared). mario@181: * mario@181: * This class will search the tree from the root given to the mario@181: * constructor. Another option would be to build a symbol table, mario@181: * and search that instead. Building the symbol table would be done mario@181: * while visiting the variable declaration objects in the parse mario@181: * tree. Unfortuantely, generate_c_c does not visit these mario@181: * objects, delegating it to another class. This means that mario@181: * we would need another specialised class just to build the mario@181: * symbol table. We might just as well have a specialised class mario@181: * that searches the tree itself for the relevant info. This mario@181: * class is exactly that...! mario@181: */ mario@181: search_fb_instance_decl_c::search_fb_instance_decl_c(symbol_c *search_scope) { mario@181: this->search_scope = search_scope; mario@181: this->current_fb_type_name = NULL; mario@181: } mario@181: mario@181: symbol_c *search_fb_instance_decl_c::get_type_name(symbol_c *fb_instance_name) { mario@181: this->search_name = fb_instance_name; mario@181: return (symbol_c *)search_scope->accept(*this); mario@181: } mario@181: mario@181: /***************************/ mario@181: /* B 0 - Programming Model */ mario@181: /***************************/ mario@181: void *search_fb_instance_decl_c::visit(library_c *symbol) { mario@181: /* we do not want to search multiple declaration scopes, mario@181: * so we do not visit all the functions, fucntion blocks, etc... mario@181: */ mario@181: return NULL; mario@181: } mario@181: mario@181: /******************************************/ mario@181: /* B 1.4.3 - Declaration & Initialisation */ mario@181: /******************************************/ mario@181: mario@181: /* name_list ':' function_block_type_name ASSIGN structure_initialization */ mario@181: /* structure_initialization -> may be NULL ! */ mario@181: void *search_fb_instance_decl_c::visit(fb_name_decl_c *symbol) { mario@181: current_fb_type_name = symbol->function_block_type_name; mario@181: return symbol->fb_name_list->accept(*this); mario@181: } mario@181: mario@181: /* name_list ',' fb_name */ mario@181: void *search_fb_instance_decl_c::visit(fb_name_list_c *symbol) { mario@181: list_c *list = symbol; mario@181: for(int i = 0; i < list->n; i++) { mario@181: if (compare_identifiers(list->elements[i], search_name) == 0) mario@181: /* by now, current_fb_declaration should be != NULL */ mario@181: return current_fb_type_name; mario@181: } mario@181: return NULL; mario@181: } mario@181: mario@181: /**************************************/ mario@181: /* B.1.5 - Program organization units */ mario@181: /**************************************/ mario@181: /***********************/ mario@181: /* B 1.5.1 - Functions */ mario@181: /***********************/ mario@181: void *search_fb_instance_decl_c::visit(function_declaration_c *symbol) { mario@181: /* no need to search through all the body, so we only mario@181: * visit the variable declarations...! mario@181: */ mario@181: return symbol->var_declarations_list->accept(*this); mario@181: } mario@181: mario@181: /*****************************/ mario@181: /* B 1.5.2 - Function Blocks */ mario@181: /*****************************/ mario@181: void *search_fb_instance_decl_c::visit(function_block_declaration_c *symbol) { mario@181: /* no need to search through all the body, so we only mario@181: * visit the variable declarations...! mario@181: */ mario@181: return symbol->var_declarations->accept(*this); mario@181: } mario@181: mario@181: /**********************/ mario@181: /* B 1.5.3 - Programs */ mario@181: /**********************/ mario@181: void *search_fb_instance_decl_c::visit(program_declaration_c *symbol) { mario@181: /* no need to search through all the body, so we only mario@181: * visit the variable declarations...! mario@181: */ mario@181: return symbol->var_declarations->accept(*this); mario@181: }