mario@181: /*
msousa@265: * matiec - a compiler for the programming languages defined in IEC 61131-3
msousa@265: *
msousa@265: * Copyright (C) 2003-2011 Mario de Sousa (msousa@fe.up.pt)
msousa@265: *
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:
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) {
ccb@202: switch (current_operation) {
ccb@202: case iterate_op:
ccb@202: if (next_param <= param_count + list->n)
ccb@202: return list->elements[next_param - param_count - 1];
ccb@202:
ccb@202: /* the desired param is not on this list... */
ccb@202: param_count += list->n;
ccb@202: break;
ccb@202:
ccb@202: case search_op:
ccb@202: for(int i = 0; i < list->n; i++) {
ccb@202: identifier_c *variable_name = dynamic_cast(list->elements[i]);
ccb@202: if (variable_name == NULL) ERROR;
ccb@202:
ccb@202: if (strcasecmp(search_param_name->value, variable_name->value) == 0)
ccb@202: /* FOUND! This is the same parameter!! */
ccb@202: return (void *)variable_name;
ccb@202: }
ccb@202: break;
ccb@202: } /* switch */
ccb@202:
ccb@202: /* Not found! */
ccb@202: return NULL;
mario@181: }
mario@181:
mario@181: void* function_param_iterator_c::handle_single_param(symbol_c *var_name) {
ccb@202: switch (current_operation) {
ccb@202: case iterate_op:
ccb@202: param_count++;
ccb@202: if (next_param == param_count)
ccb@202: return var_name;
ccb@202: break;
ccb@202:
ccb@202: case search_op:
ccb@202: identifier_c *variable_name = dynamic_cast(var_name);
ccb@202: if (variable_name == NULL) ERROR;
ccb@202:
ccb@202: if (strcasecmp(search_param_name->value, variable_name->value) == 0)
ccb@202: /* FOUND! This is the same parameter!! */
ccb@202: return (void *)variable_name;
ccb@202: break;
ccb@202: } /* switch */
ccb@202:
ccb@202: /* Not found! */
ccb@202: 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;
ccb@202: }
ccb@202:
mario@181:
mario@181: /* initialise the iterator object.
ccb@202: * We must be given a reference to one of the following
ccb@202: * - function_declaration_c
ccb@202: * - function_block_declaration_c
ccb@202: * - program_declaration_c
mario@181: * that will be analysed...
mario@181: */
ccb@202: function_param_iterator_c::function_param_iterator_c(symbol_c *pou_decl) {
ccb@202: /* do some consistency checks... */
ccb@202: function_declaration_c * f_decl = dynamic_cast(pou_decl);
ccb@202: function_block_declaration_c *fb_decl = dynamic_cast(pou_decl);
ccb@202: program_declaration_c * p_decl = dynamic_cast(pou_decl);
ccb@202:
ccb@202: if ((NULL == f_decl) && (NULL == fb_decl) && (NULL == p_decl)) ERROR;
ccb@202:
ccb@202: /* OK. Now initialise this object... */
ccb@202: this->f_decl = pou_decl;
mario@181: reset();
mario@181: }
mario@181:
ccb@202:
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;
ccb@202:
mario@181: param_count = 0;
laurent@233: en_eno_param_implicit = false;
mario@181: next_param++;
ccb@202: current_operation = function_param_iterator_c::iterate_op;
mario@181: res = f_decl->accept(*this);
ccb@202: if (res == NULL)
mario@181: return NULL;
ccb@202:
ccb@202: symbol_c *sym = (symbol_c *)res;
ccb@202: identifier = dynamic_cast(sym);
ccb@202: if (identifier == NULL)
ccb@202: ERROR;
mario@181: current_param_name = identifier;
mario@181: return current_param_name;
mario@181: }
mario@181:
ccb@202: /* Search for the value passed to the parameter named ... */
ccb@202: identifier_c *function_param_iterator_c::search(symbol_c *param_name) {
ccb@202: if (NULL == param_name) ERROR;
ccb@202: search_param_name = dynamic_cast(param_name);
ccb@202: if (NULL == search_param_name) ERROR;
ccb@202: current_operation = function_param_iterator_c::search_op;
ccb@202: void *res = f_decl->accept(*this);
ccb@202: identifier_c *res_param_name = dynamic_cast((symbol_c *)res);
ccb@202: return res_param_name;
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:
laurent@233: /* Returns if currently referenced parameter is an implicit defined EN/ENO parameter. */
laurent@233: bool function_param_iterator_c::is_en_eno_param_implicit(void) {
laurent@233: return en_eno_param_implicit;
laurent@233: }
laurent@233:
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:
laurent@233: void *function_param_iterator_c::visit(implicit_definition_c *symbol) {
laurent@233: en_eno_param_implicit = current_operation == function_param_iterator_c::iterate_op;
laurent@233: return NULL;
laurent@233: }
laurent@233:
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");
ccb@202: /* It is OK to store these values in the current_param_XXX
ccb@202: * variables, because if the desired parameter is not in the
ccb@202: * variable list we will be analysing, the current_param_XXXX
ccb@202: * variables will get overwritten when we visit the next
ccb@202: * var1_init_decl_c list!
ccb@202: */
laurent@233: symbol->method->accept(*this);
laurent@233:
ccb@202: current_param_default_value = symbol->value;
ccb@202: current_param_type = symbol->type;
ccb@202:
ccb@202: return handle_single_param(symbol->name);
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)
laurent@235: void *function_param_iterator_c::visit(array_var_init_decl_c *symbol) {
laurent@235: TRACE("array_var_init_decl_c");
laurent@235:
laurent@235: current_param_default_value = spec_init_sperator_c::get_init(symbol->array_spec_init);
laurent@235: current_param_type = spec_init_sperator_c::get_spec(symbol->array_spec_init);
laurent@235:
laurent@235: return symbol->var1_list->accept(*this);
laurent@235: }
mario@181:
mario@181: /* var1_list ':' initialized_structure */
mario@181: //SYM_REF2(structured_var_init_decl_c, var1_list, initialized_structure)
laurent@235: void *function_param_iterator_c::visit(structured_var_init_decl_c *symbol) {
laurent@235: TRACE("structured_var_init_decl_c");
laurent@235:
laurent@235: current_param_default_value = spec_init_sperator_c::get_init(symbol->initialized_structure);
laurent@235: current_param_type = spec_init_sperator_c::get_spec(symbol->initialized_structure);
laurent@235:
laurent@235: return symbol->var1_list->accept(*this);
laurent@235: }
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");
ccb@202: /* It is OK to store these values in the current_param_XXX
ccb@202: * variables, because if the desired parameter is not in the
ccb@202: * variable list we will be analysing, the current_param_XXXX
ccb@202: * variables will get overwritten when we visit the next
ccb@202: * var1_init_decl_c list!
ccb@202: */
laurent@233: symbol->method->accept(*this);
laurent@233:
ccb@202: current_param_default_value = NULL;
ccb@202: current_param_type = symbol->type;
ccb@202:
ccb@202: return handle_single_param(symbol->name);
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)
laurent@237: void *function_param_iterator_c::visit(array_var_declaration_c *symbol) {
laurent@237: TRACE("array_var_declaration_c");
laurent@237:
laurent@237: current_param_default_value = NULL;
laurent@237: current_param_type = symbol->array_specification;
laurent@237:
laurent@237: return symbol->var1_list->accept(*this);
laurent@237: }
mario@181:
mario@181: /* var1_list ':' structure_type_name */
mario@181: //SYM_REF2(structured_var_declaration_c, var1_list, structure_type_name)
laurent@235: void *function_param_iterator_c::visit(structured_var_declaration_c *symbol) {
laurent@235: TRACE("structured_var_declaration_c");
laurent@235:
laurent@235: current_param_default_value = NULL;
laurent@235: current_param_type = symbol->structure_type_name;
laurent@235:
laurent@235: return symbol->var1_list->accept(*this);
laurent@235: }
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: