mario@181: /* mario@181: * (c) 2003 Mario de Sousa mario@181: * mario@181: * Offered to the public under the terms of the GNU General Public License mario@181: * as published by the Free Software Foundation; either version 2 of the mario@181: * License, or (at your option) any later version. mario@181: * mario@181: * This program is distributed in the hope that it will be useful, but mario@181: * WITHOUT ANY WARRANTY; without even the implied warranty of mario@181: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General mario@181: * Public License for more details. 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: /* mario@181: * An IEC 61131-3 IL and ST 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: mario@181: /* mario@181: * Function call parameter iterator. mario@181: * mario@181: * This is part of the 4th stage that generates mario@181: * a c++ source program equivalent to the IL and ST mario@181: * code. mario@181: */ mario@181: mario@181: /* given a function_body_c, iterate through each mario@181: * function in/out/inout parameter, returning the name mario@181: * of each parameter... mario@181: */ mario@181: mario@181: mario@181: #include "function_call_iterator.hh" mario@181: mario@181: mario@181: //#define DEBUG mario@181: #ifdef DEBUG mario@181: #define TRACE(classname) printf("\n____%s____\n",classname); mario@181: #else mario@181: #define TRACE(classname) mario@181: #endif mario@181: mario@181: #define ERROR error_exit(__FILE__,__LINE__) mario@181: /* function defined in main.cc */ mario@181: extern void error_exit(const char *file_name, int line_no); mario@181: mario@181: mario@181: mario@181: mario@181: mario@181: /* initialise the iterator object. mario@181: * We must be given a reference to the function declaration mario@181: * that will be analysed... mario@181: */ mario@181: function_call_iterator_c::function_call_iterator_c(symbol_c *symbol) { mario@181: this->start_symbol = symbol; mario@181: next_fcall = fcall_count = 0; mario@181: current_finvocation = NULL; mario@181: current_fcall_name = NULL; mario@181: } mario@181: mario@181: /* Skip to the next function call. After object creation, mario@181: * the object references _before_ the first, so mario@181: * this function must be called once to get the object to mario@181: * reference the first function call... mario@181: * mario@181: * Returns the function_invocation_c! mario@181: */ mario@181: //function_invocation_c *next(void) {TRACE("function_call_iterator_c::next(): called "); mario@181: symbol_c *function_call_iterator_c::next(void) {TRACE("function_call_iterator_c::next(): called "); mario@181: fcall_count = 0; mario@181: next_fcall++; mario@181: current_finvocation = NULL; mario@181: current_fcall_name = NULL; mario@181: mario@181: start_symbol->accept(*this); mario@181: return current_finvocation; mario@181: } mario@181: mario@181: /* Returns the name of the currently referenced function invocation */ mario@181: identifier_c *function_call_iterator_c::fname(void) { mario@181: identifier_c *identifier = dynamic_cast(current_fcall_name); mario@181: if (identifier == NULL) ERROR; mario@181: return identifier; mario@181: } mario@181: mario@181: mario@181: /***************************************/ mario@181: /* B.3 - Language ST (Structured Text) */ mario@181: /***************************************/ mario@181: /***********************/ mario@181: /* B 3.1 - Expressions */ mario@181: /***********************/ mario@181: void *function_call_iterator_c::visit(function_invocation_c *symbol) { mario@181: fcall_count++; mario@181: if (next_fcall == fcall_count) { mario@181: current_finvocation = symbol; mario@181: current_fcall_name = symbol->function_name; mario@181: } mario@181: return NULL; mario@181: } mario@181: mario@181: mario@181: mario@181: /****************************************/ mario@181: /* B.2 - Language IL (Instruction List) */ mario@181: /****************************************/ mario@181: /***********************************/ mario@181: /* B 2.1 Instructions and Operands */ mario@181: /***********************************/ mario@181: mario@181: /* | function_name [il_operand_list] */ mario@181: // SYM_REF2(il_function_call_c, function_name, il_operand_list) mario@181: void *function_call_iterator_c::visit(il_function_call_c *symbol) { mario@181: fcall_count++; mario@181: if (next_fcall == fcall_count) { mario@181: current_finvocation = symbol; mario@181: current_fcall_name = symbol->function_name; mario@181: } mario@181: return NULL; mario@181: } mario@181: mario@181: mario@181: mario@181: /* | function_name '(' eol_list [il_param_list] ')' */ mario@181: // SYM_REF2(il_formal_funct_call_c, function_name, il_param_list) mario@181: void *function_call_iterator_c::visit(il_formal_funct_call_c *symbol) { mario@181: fcall_count++; mario@181: if (next_fcall == fcall_count) { mario@181: current_finvocation = symbol; mario@181: current_fcall_name = symbol->function_name; mario@181: } mario@181: return NULL; mario@181: } mario@181: mario@181: mario@181: mario@181: mario@181: