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 parameter iterator. mario@181: * Iterate through the in/out parameters of a function declaration. mario@181: * Function blocks are also suported. 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_declaration_c, iterate through each mario@181: * function in/out/inout parameter, returning the name mario@181: * of each parameter...function_param_iterator_c mario@181: */ mario@181: mario@181: mario@181: mario@181: #include "function_param_iterator.hh" mario@181: #include "spec_init_separator.hh" mario@181: #include 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: 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: void* function_param_iterator_c::handle_param_list(list_c *list) { mario@181: if (next_param <= param_count + list->n) mario@181: return list->elements[next_param - param_count - 1]; mario@181: mario@181: /* the desired param is not on this list... */ mario@181: param_count += list->n; mario@181: return NULL; mario@181: } mario@181: mario@181: void* function_param_iterator_c::handle_single_param(symbol_c *var_name) { mario@181: param_count++; mario@181: if (next_param == param_count) mario@181: return var_name; mario@181: mario@181: /* not yet the desired param... */ mario@181: return NULL; mario@181: } mario@181: mario@181: void* function_param_iterator_c::iterate_list(list_c *list) { mario@181: void *res; mario@181: for (int i = 0; i < list->n; i++) { mario@181: res = list->elements[i]->accept(*this); mario@181: if (res != NULL) mario@181: return res; mario@181: } mario@181: return NULL; mario@181: } mario@181: mario@181: /* start off at the first parameter once again... */ mario@181: void function_param_iterator_c::reset(void) { mario@181: next_param = param_count = 0; mario@181: current_param_name = NULL; mario@181: current_param_type = current_param_default_value = NULL; mario@181: en_declared = false; mario@181: eno_declared = false; 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_param_iterator_c::function_param_iterator_c(function_declaration_c *f_decl) { mario@181: this->f_decl = f_decl; mario@181: reset(); mario@181: } mario@181: mario@181: /* initialise the iterator object. mario@181: * We must be given a reference to the function block declaration mario@181: * that will be analysed... mario@181: */ mario@181: function_param_iterator_c::function_param_iterator_c(function_block_declaration_c *fb_decl) { mario@181: this->f_decl = fb_decl; mario@181: reset(); mario@181: } mario@181: mario@181: /* initialise the iterator object. mario@181: * We must be given a reference to the program declaration mario@181: * that will be analysed... mario@181: */ mario@181: function_param_iterator_c::function_param_iterator_c(program_declaration_c *p_decl) { mario@181: this->f_decl = p_decl; mario@181: reset(); mario@181: } mario@181: mario@181: /* Skip to the next parameter. After object creation, mario@181: * the object references on parameter _before_ the first, so mario@181: * this function must be called once to get the object to mario@181: * reference the first parameter... mario@181: * mario@181: * Returns the parameter's name! mario@181: */ mario@181: identifier_c *function_param_iterator_c::next(void) { mario@181: void *res; mario@181: identifier_c *identifier; mario@181: param_count = 0; mario@181: next_param++; mario@181: res = f_decl->accept(*this); mario@181: if (res != NULL) { mario@181: symbol_c *sym = (symbol_c *)res; mario@181: identifier = dynamic_cast(sym); mario@181: if (identifier == NULL) mario@181: ERROR; mario@181: } mario@181: else if (!en_declared) { mario@181: current_param_direction = direction_in; mario@181: identifier = declare_en_param(); mario@181: } mario@181: else if (!eno_declared) { mario@181: current_param_direction = direction_out; mario@181: identifier = declare_eno_param(); mario@181: } mario@181: else mario@181: return NULL; mario@181: mario@181: current_param_name = identifier; mario@181: return current_param_name; mario@181: } mario@181: mario@181: identifier_c *function_param_iterator_c::declare_en_param(void) { mario@181: en_declared = true; mario@181: identifier_c *identifier = new identifier_c("EN"); mario@181: current_param_type = (symbol_c*)(new bool_type_name_c()); mario@181: current_param_default_value = (symbol_c*)(new boolean_literal_c(current_param_type, new boolean_true_c())); mario@181: return identifier; mario@181: } mario@181: mario@181: identifier_c *function_param_iterator_c::declare_eno_param(void) { mario@181: eno_declared = true; mario@181: identifier_c *identifier = new identifier_c("ENO"); mario@181: current_param_type = (symbol_c*)(new bool_type_name_c()); mario@181: current_param_default_value = NULL; mario@181: return identifier; mario@181: } mario@181: mario@181: /* Returns the currently referenced parameter's default value, mario@181: * or NULL if none is specified in the function declrataion itself. mario@181: */ mario@181: symbol_c *function_param_iterator_c::default_value(void) { mario@181: return current_param_default_value; mario@181: } mario@181: mario@181: /* Returns the currently referenced parameter's type name. */ mario@181: symbol_c *function_param_iterator_c::param_type(void) { mario@181: return current_param_type; mario@181: } mario@181: mario@181: /* Returns the currently referenced parameter's data passing direction. mario@181: * i.e. VAR_INPUT, VAR_OUTPUT or VAR_INOUT mario@181: */ mario@181: function_param_iterator_c::param_direction_t function_param_iterator_c::param_direction(void) { mario@181: return current_param_direction; mario@181: } mario@181: mario@181: /****************************************/ mario@181: /* 1.4.3 - Declaration & Initialisation */ mario@181: /****************************************/ mario@181: void *function_param_iterator_c::visit(input_declarations_c *symbol) { mario@181: TRACE("input_declarations_c"); mario@181: current_param_direction = direction_in; mario@181: return symbol->input_declaration_list->accept(*this); mario@181: } mario@181: mario@181: void *function_param_iterator_c::visit(input_declaration_list_c *symbol) {TRACE("input_declaration_list_c"); return iterate_list(symbol);} mario@181: mario@181: void *function_param_iterator_c::visit(edge_declaration_c *symbol) {TRACE("edge_declaration_c"); return symbol->var1_list->accept(*this);} mario@181: mario@181: void *function_param_iterator_c::visit(en_param_declaration_c *symbol) { mario@181: TRACE("en_param_declaration_c"); mario@181: if (en_declared) ERROR; mario@181: return (void *)declare_en_param(); mario@181: } mario@181: mario@181: /* var1_list ':' array_spec_init */ mario@181: //SYM_REF2(array_var_init_decl_c, var1_list, array_spec_init) mario@181: void *function_param_iterator_c::visit(array_var_init_decl_c *symbol) {TRACE("array_var_init_decl_c"); return symbol->var1_list->accept(*this);} mario@181: mario@181: /* var1_list ':' initialized_structure */ mario@181: //SYM_REF2(structured_var_init_decl_c, var1_list, initialized_structure) mario@181: void *function_param_iterator_c::visit(structured_var_init_decl_c *symbol) {TRACE("structured_var_init_decl_c"); return symbol->var1_list->accept(*this);} mario@181: mario@181: void *function_param_iterator_c::visit(output_declarations_c *symbol) { mario@181: TRACE("output_declarations_c"); mario@181: current_param_direction = direction_out; mario@181: return symbol->var_init_decl_list->accept(*this); mario@181: } mario@181: void *function_param_iterator_c::visit(eno_param_declaration_c *symbol) { mario@181: TRACE("eno_param_declaration_c"); mario@181: if (eno_declared) ERROR; mario@181: return (void *)declare_eno_param(); mario@181: } mario@181: void *function_param_iterator_c::visit(input_output_declarations_c *symbol) { mario@181: TRACE("input_output_declarations_c"); mario@181: current_param_direction = direction_inout; mario@181: return symbol->var_declaration_list->accept(*this); mario@181: } mario@181: void *function_param_iterator_c::visit(var_declaration_list_c *symbol) {TRACE("var_declaration_list_c"); return iterate_list(symbol);} mario@181: mario@181: /* var1_list ':' array_specification */ mario@181: //SYM_REF2(array_var_declaration_c, var1_list, array_specification) mario@181: void *function_param_iterator_c::visit(array_var_declaration_c *symbol) {TRACE("array_var_declaration_c"); return symbol->var1_list->accept(*this);} mario@181: mario@181: /* var1_list ':' structure_type_name */ mario@181: //SYM_REF2(structured_var_declaration_c, var1_list, structure_type_name) mario@181: void *function_param_iterator_c::visit(structured_var_declaration_c *symbol) {TRACE("structured_var_declaration_c"); return symbol->var1_list->accept(*this);} mario@181: mario@181: /* VAR [CONSTANT] var_init_decl_list END_VAR */ mario@181: void *function_param_iterator_c::visit(var_declarations_c *symbol) {TRACE("var_declarations_c"); return NULL;} mario@181: mario@181: /*| VAR_EXTERNAL [CONSTANT] external_declaration_list END_VAR */ mario@181: /* option -> may be NULL ! */ mario@181: // SYM_REF2(external_var_declarations_c, option, external_declaration_list) mario@181: void *function_param_iterator_c::visit(external_var_declarations_c *symbol) { mario@181: TRACE("external_var_declarations_c"); mario@181: current_param_direction = direction_extref; mario@181: return symbol->external_declaration_list->accept(*this); mario@181: } mario@181: mario@181: /* helper symbol for external_var_declarations */ mario@181: /*| external_declaration_list external_declaration';' */ mario@181: // SYM_LIST(external_declaration_list_c) mario@181: void *function_param_iterator_c::visit(external_declaration_list_c *symbol) {TRACE("external_declaration_list_c"); return iterate_list(symbol);} mario@181: mario@181: /* global_var_name ':' (simple_specification|subrange_specification|enumerated_specification|array_specification|prev_declared_structure_type_name|function_block_type_name */ mario@181: //SYM_REF2(external_declaration_c, global_var_name, specification) mario@181: void *function_param_iterator_c::visit(external_declaration_c *symbol) { mario@181: TRACE("external_declaration_c"); mario@181: /* It is OK to store these values in the current_param_XXX mario@181: * variables, because if the desired parameter is not in the mario@181: * variable list we will be analysing, the current_param_XXXX mario@181: * variables will get overwritten when we visit the next mario@181: * var1_init_decl_c list! mario@181: */ mario@181: current_param_default_value = spec_init_sperator_c::get_init(symbol->specification); mario@181: current_param_type = spec_init_sperator_c::get_spec(symbol->specification); mario@181: mario@181: return handle_single_param(symbol->global_var_name); mario@181: } mario@181: mario@181: mario@181: #if 0 mario@181: /*| VAR_GLOBAL [CONSTANT|RETAIN] global_var_decl_list END_VAR */ mario@181: /* option -> may be NULL ! */ mario@181: SYM_REF2(global_var_declarations_c, option, global_var_decl_list) mario@181: mario@181: /* helper symbol for global_var_declarations */ mario@181: /*| global_var_decl_list global_var_decl ';' */ mario@181: SYM_LIST(global_var_decl_list_c) mario@181: mario@181: /*| global_var_spec ':' [located_var_spec_init|function_block_type_name] */ mario@181: /* type_specification ->may be NULL ! */ mario@181: SYM_REF2(global_var_decl_c, global_var_spec, type_specification) mario@181: mario@181: /*| global_var_name location */ mario@181: SYM_REF2(global_var_spec_c, global_var_name, location) mario@181: mario@181: /* AT direct_variable */ mario@181: SYM_REF2(location_c, direct_variable, unused) mario@181: mario@181: /*| global_var_list ',' global_var_name */ mario@181: SYM_LIST(global_var_list_c) mario@181: mario@181: /* var1_list ':' single_byte_string_spec */ mario@181: SYM_REF2(single_byte_string_var_declaration_c, var1_list, single_byte_string_spec) mario@181: mario@181: /* STRING ['[' integer ']'] [ASSIGN single_byte_character_string] */ mario@181: /* integer ->may be NULL ! */ mario@181: /* single_byte_character_string ->may be NULL ! */ mario@181: SYM_REF2(single_byte_string_spec_c, integer, single_byte_character_string) mario@181: mario@181: /* var1_list ':' double_byte_string_spec */ mario@181: SYM_REF2(double_byte_string_var_declaration_c, var1_list, double_byte_string_spec) mario@181: mario@181: /* WSTRING ['[' integer ']'] [ASSIGN double_byte_character_string] */ mario@181: /* integer ->may be NULL ! */ mario@181: /* double_byte_character_string ->may be NULL ! */ mario@181: SYM_REF2(double_byte_string_spec_c, integer, double_byte_character_string) mario@181: mario@181: /*| VAR [RETAIN|NON_RETAIN] incompl_located_var_decl_list END_VAR */ mario@181: /* option ->may be NULL ! */ mario@181: SYM_REF2(incompl_located_var_declarations_c, option, incompl_located_var_decl_list) mario@181: mario@181: /* helper symbol for incompl_located_var_declarations */ mario@181: /*| incompl_located_var_decl_list incompl_located_var_decl ';' */ mario@181: SYM_LIST(incompl_located_var_decl_list_c) mario@181: mario@181: /* variable_name incompl_location ':' var_spec */ mario@181: SYM_REF4(incompl_located_var_decl_c, variable_name, incompl_location, var_spec, unused) mario@181: mario@181: /* AT incompl_location_token */ mario@181: SYM_TOKEN(incompl_location_c) mario@181: #endif mario@181: mario@181: mario@181: void *function_param_iterator_c::visit(var1_init_decl_c *symbol) { mario@181: TRACE("var1_init_decl_c"); mario@181: /* It is OK to store these values in the current_param_XXX mario@181: * variables, because if the desired parameter is not in the mario@181: * variable list we will be analysing, the current_param_XXXX mario@181: * variables will get overwritten when we visit the next mario@181: * var1_init_decl_c list! mario@181: */ mario@181: current_param_default_value = spec_init_sperator_c::get_init(symbol->spec_init); mario@181: current_param_type = spec_init_sperator_c::get_spec(symbol->spec_init); mario@181: mario@181: return symbol->var1_list->accept(*this); mario@181: } mario@181: mario@181: mario@181: mario@181: void *function_param_iterator_c::visit(var1_list_c *symbol) { mario@181: TRACE("var1_list_c"); mario@181: return handle_param_list(symbol); mario@181: } mario@181: mario@181: void *function_param_iterator_c::visit(var_init_decl_list_c *symbol) {TRACE("var_init_decl_list_c"); return iterate_list(symbol);} mario@181: mario@181: mario@181: /***********************/ mario@181: /* B 1.5.1 - Functions */ mario@181: /***********************/ mario@181: void *function_param_iterator_c::visit(function_declaration_c *symbol) {TRACE("function_declaration_c"); return symbol->var_declarations_list->accept(*this);} mario@181: /* intermediate helper symbol for function_declaration */ mario@181: void *function_param_iterator_c::visit(var_declarations_list_c *symbol) {TRACE("var_declarations_list_c"); return iterate_list(symbol);} mario@181: void *function_param_iterator_c::visit(function_var_decls_c *symbol) {TRACE("function_var_decls_c"); /* ignore */ return NULL;} mario@181: mario@181: mario@181: /*****************************/ mario@181: /* B 1.5.2 - Function Blocks */ mario@181: /*****************************/ mario@181: /* FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */ mario@181: void *function_param_iterator_c::visit(function_block_declaration_c *symbol) {TRACE("function_block_declaration_c"); return symbol->var_declarations->accept(*this);} mario@181: mario@181: /* intermediate helper symbol for function_declaration */ mario@181: /* { io_var_declarations | other_var_declarations } */ mario@181: /* mario@181: * NOTE: we re-use the var_declarations_list_c mario@181: */ mario@181: mario@181: /* VAR_TEMP temp_var_decl_list END_VAR */ mario@181: void *function_param_iterator_c::visit(temp_var_decls_c *symbol) {TRACE("temp_var_decls_c"); /* ignore */ return NULL;} mario@181: void *function_param_iterator_c::visit(temp_var_decls_list_c *symbol) {TRACE("temp_var_decls_list_c"); /* ignore */ return NULL;} mario@181: mario@181: /* VAR NON_RETAIN var_init_decl_list END_VAR */ mario@181: void *function_param_iterator_c::visit(non_retentive_var_decls_c *symbol) {TRACE("non_retentive_var_decls_c"); /* ignore */ return NULL;} mario@181: mario@181: mario@181: /**********************/ mario@181: /* B 1.5.3 - Programs */ mario@181: /**********************/ mario@181: /* PROGRAM program_type_name program_var_declarations_list function_block_body END_PROGRAM */ mario@181: // SYM_REF4(program_declaration_c, program_type_name, var_declarations, function_block_body, unused) mario@181: void *function_param_iterator_c::visit(program_declaration_c *symbol) {TRACE("program_declaration_c"); return symbol->var_declarations->accept(*this);} mario@181: mario@181: /* intermediate helper symbol for program_declaration_c */ mario@181: /* { io_var_declarations | other_var_declarations } */ mario@181: /* mario@181: * NOTE: we re-use the var_declarations_list_c mario@181: */ mario@181: mario@181: mario@181: mario@181: mario@181: mario@181: