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: * Function call parameter iterator. etisserant@0: * etisserant@0: * This is part of the 4th stage that generates etisserant@0: * a c++ source program equivalent to the IL and ST etisserant@0: * code. etisserant@0: */ etisserant@0: etisserant@0: etisserant@0: //#include /* required for NULL */ etisserant@0: //#include etisserant@0: //#include etisserant@0: etisserant@0: //#include "../../util/symtable.hh" etisserant@0: etisserant@0: //#include "generate_cc.hh" etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: /* given a function_body_c, iterate through each etisserant@0: * function in/out/inout parameter, returning the name etisserant@0: * of each parameter... etisserant@0: */ etisserant@0: class function_call_iterator_c : public iterator_visitor_c { etisserant@0: private: etisserant@0: symbol_c *start_symbol; etisserant@0: int next_fcall, fcall_count; etisserant@0: //identifier_c *current_fcall_name; etisserant@0: symbol_c *current_fcall_name; etisserant@0: //function_invocation_c *current_finvocation; etisserant@0: symbol_c *current_finvocation; etisserant@0: etisserant@0: public: etisserant@0: /* initialise the iterator object. etisserant@0: * We must be given a reference to the function declaration etisserant@0: * that will be analysed... etisserant@0: */ etisserant@0: function_call_iterator_c(symbol_c *symbol) { etisserant@0: this->start_symbol = symbol; etisserant@0: next_fcall = fcall_count = 0; etisserant@0: current_finvocation = NULL; etisserant@0: current_fcall_name = NULL; etisserant@0: } etisserant@0: etisserant@0: /* Skip to the next function call. After object creation, etisserant@0: * the object references _before_ the first, so etisserant@0: * this function must be called once to get the object to etisserant@0: * reference the first function call... etisserant@0: * etisserant@0: * Returns the function_invocation_c! etisserant@0: */ etisserant@0: //function_invocation_c *next(void) {TRACE("function_call_iterator_c::next(): called "); etisserant@0: symbol_c *next(void) {TRACE("function_call_iterator_c::next(): called "); etisserant@0: fcall_count = 0; etisserant@0: next_fcall++; etisserant@0: current_finvocation = NULL; etisserant@0: current_fcall_name = NULL; etisserant@0: etisserant@0: start_symbol->accept(*this); etisserant@0: return current_finvocation; etisserant@0: } etisserant@0: etisserant@0: /* Returns the name of the currently referenced function invocation */ etisserant@0: identifier_c *fname(void) { etisserant@0: identifier_c *identifier = dynamic_cast(current_fcall_name); etisserant@0: if (identifier == NULL) ERROR; etisserant@0: return identifier; etisserant@0: } etisserant@0: etisserant@0: etisserant@0: /***************************************/ etisserant@0: /* B.3 - Language ST (Structured Text) */ etisserant@0: /***************************************/ etisserant@0: /***********************/ etisserant@0: /* B 3.1 - Expressions */ etisserant@0: /***********************/ etisserant@0: void *visit(function_invocation_c *symbol) { etisserant@0: fcall_count++; etisserant@0: if (next_fcall == fcall_count) { etisserant@0: current_finvocation = symbol; etisserant@0: current_fcall_name = symbol->function_name; etisserant@0: } etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: etisserant@0: etisserant@0: /****************************************/ etisserant@0: /* B.2 - Language IL (Instruction List) */ etisserant@0: /****************************************/ etisserant@0: /***********************************/ etisserant@0: /* B 2.1 Instructions and Operands */ etisserant@0: /***********************************/ etisserant@0: etisserant@0: /* | function_name [il_operand_list] */ etisserant@0: // SYM_REF2(il_function_call_c, function_name, il_operand_list) etisserant@0: void *visit(il_function_call_c *symbol) { etisserant@0: fcall_count++; etisserant@0: if (next_fcall == fcall_count) { etisserant@0: current_finvocation = symbol; etisserant@0: current_fcall_name = symbol->function_name; etisserant@0: } etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: etisserant@0: etisserant@0: /* | function_name '(' eol_list [il_param_list] ')' */ etisserant@0: // SYM_REF2(il_formal_funct_call_c, function_name, il_param_list) etisserant@0: void *visit(il_formal_funct_call_c *symbol) { etisserant@0: fcall_count++; etisserant@0: if (next_fcall == fcall_count) { etisserant@0: current_finvocation = symbol; etisserant@0: current_fcall_name = symbol->function_name; etisserant@0: } etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: etisserant@0: }; etisserant@0: etisserant@0: etisserant@0: etisserant@0: