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: * Function call parameter iterator. mario@181: * It will iterate through the formal parameters of a function call mario@181: * (i.e. function calls using the foo(, , ...) syntax). mario@181: * and/or search through the non-formal parameters of a function call mario@181: * (i.e. function calls using the foo( = , = , ...) syntax). mario@181: * mario@181: * Calls to function blocks and programs are also supported. mario@181: * mario@181: * Note that calls to next() will only iterate through formal parameters, mario@181: * and calls to search() will only serach through non-formal parameters. mario@181: */ mario@181: mario@181: mario@181: mario@181: #include "function_call_param_iterator.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: #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: void *function_call_param_iterator_c::search_list(list_c *list) { mario@181: switch (current_operation) { mario@181: case iterate_op: mario@181: for(int i = 0; i < list->n; i++) { mario@181: void *res = list->elements[i]->accept(*this); mario@181: if (NULL != res) { mario@181: /* It went through the handle_parameter_assignment() function, mario@181: * and is therefore a parameter assignment ( = ), mario@181: * and not a simple expression (). mario@181: */ mario@181: /* we do nothing... */ mario@181: } else { mario@181: param_count++; mario@181: if (param_count == next_param) { mario@181: return list->elements[i]; mario@181: } mario@181: } mario@181: } mario@181: return NULL; mario@181: break; mario@181: mario@181: case search_op: mario@181: for(int i = 0; i < list->n; i++) { mario@181: void *res = list->elements[i]->accept(*this); mario@181: if (res != NULL) mario@181: return res; mario@181: } mario@181: return NULL; mario@181: break; mario@181: } /* switch */ mario@181: return NULL; mario@181: } mario@181: mario@181: mario@181: mario@181: void *function_call_param_iterator_c::handle_parameter_assignment(symbol_c *variable_name, symbol_c *expression) { mario@181: switch (current_operation) { mario@181: case iterate_op: mario@181: /* UGLY HACK -> this will be detected in the search_list() function */ mario@181: return (void *)this; /* anything, as long as it is not NULL!! */ mario@181: break; mario@181: mario@181: case search_op: mario@181: identifier_c *variable_name2 = dynamic_cast(variable_name); mario@181: mario@181: if (variable_name2 == NULL) { mario@181: en_param_c *en_param = dynamic_cast(variable_name); mario@181: if (en_param != NULL) mario@181: variable_name2 = new identifier_c("EN"); mario@181: } mario@181: mario@181: if (variable_name2 == NULL) { mario@181: eno_param_c *eno_param = dynamic_cast(variable_name); mario@181: if (eno_param != NULL) mario@181: variable_name2 = new identifier_c("ENO"); mario@181: } mario@181: mario@181: if (variable_name2 == NULL) ERROR; mario@181: mario@181: if (strcasecmp(search_param_name->value, variable_name2->value) == 0) mario@181: /* FOUND! This is the same parameter!! */ mario@181: return (void *)expression; mario@181: return NULL; mario@181: break; mario@181: } mario@181: mario@181: ERROR; mario@181: return NULL; mario@181: } mario@181: mario@181: mario@181: /* start off at the first parameter once again... */ mario@181: void function_call_param_iterator_c::reset(void) { mario@181: next_param = param_count = 0; mario@181: } mario@181: mario@181: /* initialise the iterator object. mario@181: * We must be given a reference to the function/program/function block call mario@181: * that will be analysed... mario@181: */ mario@181: function_call_param_iterator_c::function_call_param_iterator_c(symbol_c *f_call) { mario@181: /* It is expected that f_call will reference one of the following: mario@181: * program_configuration_c mario@181: * function_invocation_c mario@181: * fb_invocation_c mario@181: * il_function_call_c mario@181: * il_formal_funct_call_c mario@181: * ... (have I missed any?) mario@181: */ mario@181: this->f_call = f_call; mario@181: search_param_name = NULL; 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 whatever is being passed to the parameter! mario@181: */ mario@181: symbol_c *function_call_param_iterator_c::next(void) { mario@181: param_count = 0; mario@181: next_param++; mario@181: current_operation = function_call_param_iterator_c::iterate_op; mario@181: void *res = f_call->accept(*this); mario@181: return (symbol_c *)res; mario@181: } mario@181: mario@181: /* Search for the value passed to the parameter named ... */ mario@181: symbol_c *function_call_param_iterator_c::search(symbol_c *param_name) { mario@181: if (NULL == param_name) ERROR; mario@181: search_param_name = dynamic_cast(param_name); mario@181: if (NULL == search_param_name) ERROR; mario@181: current_operation = function_call_param_iterator_c::search_op; mario@181: void *res = f_call->accept(*this); mario@181: return (symbol_c *)res; mario@181: } mario@181: mario@181: mario@181: mario@181: /********************************/ mario@181: /* B 1.7 Configuration elements */ mario@181: /********************************/ mario@181: mario@181: /* mario@181: CONFIGURATION configuration_name mario@181: optional_global_var_declarations mario@181: (resource_declaration_list | single_resource_declaration) mario@181: optional_access_declarations mario@181: optional_instance_specific_initializations mario@181: END_CONFIGURATION mario@181: */ mario@181: /* mario@181: SYM_REF6(configuration_declaration_c, configuration_name, global_var_declarations, resource_declarations, access_declarations, instance_specific_initializations, unused) mario@181: */ mario@181: mario@181: /* helper symbol for configuration_declaration */ mario@181: /* mario@181: SYM_LIST(resource_declaration_list_c) mario@181: */ mario@181: mario@181: /* mario@181: RESOURCE resource_name ON resource_type_name mario@181: optional_global_var_declarations mario@181: single_resource_declaration mario@181: END_RESOURCE mario@181: */ mario@181: /* mario@181: SYM_REF4(resource_declaration_c, resource_name, resource_type_name, global_var_declarations, resource_declaration) mario@181: */ mario@181: mario@181: /* task_configuration_list program_configuration_list */ mario@181: /* mario@181: SYM_REF2(single_resource_declaration_c, task_configuration_list, program_configuration_list) mario@181: */ mario@181: mario@181: /* helper symbol for single_resource_declaration */ mario@181: /* mario@181: SYM_LIST(task_configuration_list_c) mario@181: */ mario@181: mario@181: /* helper symbol for single_resource_declaration */ mario@181: /* mario@181: SYM_LIST(program_configuration_list_c) mario@181: */ mario@181: mario@181: /* helper symbol for mario@181: * - access_path mario@181: * - instance_specific_init mario@181: */ mario@181: /* mario@181: SYM_LIST(any_fb_name_list_c) mario@181: */ mario@181: mario@181: /* [resource_name '.'] global_var_name ['.' structure_element_name] */ mario@181: /* mario@181: SYM_REF4(global_var_reference_c, resource_name, global_var_name, structure_element_name, unused) mario@181: */ mario@181: mario@181: /* prev_declared_program_name '.' symbolic_variable */ mario@181: /* mario@181: SYM_REF2(program_output_reference_c, program_name, symbolic_variable) mario@181: */ mario@181: mario@181: /* TASK task_name task_initialization */ mario@181: /* mario@181: SYM_REF2(task_configuration_c, task_name, task_initialization) mario@181: */ mario@181: mario@181: /* '(' [SINGLE ASSIGN data_source ','] [INTERVAL ASSIGN data_source ','] PRIORITY ASSIGN integer ')' */ mario@181: /* mario@181: SYM_REF4(task_initialization_c, single_data_source, interval_data_source, priority_data_source, unused) mario@181: */ mario@181: mario@181: /* PROGRAM [RETAIN | NON_RETAIN] program_name [WITH task_name] ':' program_type_name ['(' prog_conf_elements ')'] */ mario@181: // SYM_REF6(program_configuration_c, retain_option, program_name, task_name, program_type_name, prog_conf_elements, unused) mario@181: void *function_call_param_iterator_c::visit(program_configuration_c *symbol) { mario@181: TRACE("program_configuration_c"); mario@181: return symbol->prog_conf_elements->accept(*this); mario@181: } mario@181: mario@181: /* prog_conf_elements ',' prog_conf_element */ mario@181: // SYM_LIST(prog_conf_elements_c) mario@181: void *function_call_param_iterator_c::visit(prog_conf_elements_c *symbol) { mario@181: TRACE("prog_conf_elements_c"); mario@181: return search_list(symbol); mario@181: } mario@181: mario@181: /* fb_name WITH task_name */ mario@181: /* mario@181: SYM_REF2(fb_task_c, fb_name, task_name) mario@181: */ mario@181: mario@181: /* any_symbolic_variable ASSIGN prog_data_source */ mario@181: // SYM_REF2(prog_cnxn_assign_c, symbolic_variable, prog_data_source) mario@181: void *function_call_param_iterator_c::visit(prog_cnxn_assign_c *symbol) { mario@181: TRACE("prog_cnxn_assign_c"); mario@181: mario@181: /* NOTE: symbolic_variable may be something other than a symbolic_variable_c, but I (Mario) mario@181: * do not understand the semantics that should be implmeneted if it is not a mario@181: * symbolic_variable, so for the moment we simply give up! mario@181: */ mario@181: symbolic_variable_c *symb_var = dynamic_cast(symbol->symbolic_variable); mario@181: if (NULL == symb_var) mario@181: ERROR; mario@181: mario@181: return handle_parameter_assignment(symb_var->var_name, symbol->prog_data_source); mario@181: } mario@181: mario@181: /* any_symbolic_variable SENDTO data_sink */ mario@181: // SYM_REF2(prog_cnxn_sendto_c, symbolic_variable, prog_data_source) mario@181: void *function_call_param_iterator_c::visit(prog_cnxn_sendto_c *symbol) { mario@181: TRACE("prog_cnxn_sendto_c"); mario@181: mario@181: /* NOTE: symbolic_variable may be something other than a symbolic_variable_c, but I (Mario) mario@181: * do not understand the semantics that should be implmeneted if it is not a mario@181: * symbolic_variable, so for the moment we simply give up! mario@181: */ mario@181: symbolic_variable_c *symb_var = dynamic_cast(symbol->symbolic_variable); mario@181: if (NULL == symb_var) mario@181: ERROR; mario@181: mario@181: return handle_parameter_assignment(symb_var->var_name, symbol->data_sink); mario@181: } mario@181: mario@181: /* VAR_CONFIG instance_specific_init_list END_VAR */ mario@181: /* mario@181: SYM_REF2(instance_specific_initializations_c, instance_specific_init_list, unused) mario@181: */ mario@181: mario@181: /* helper symbol for instance_specific_initializations */ mario@181: /* mario@181: SYM_LIST(instance_specific_init_list_c) mario@181: */ mario@181: mario@181: /* resource_name '.' program_name '.' {fb_name '.'} mario@181: ((variable_name [location] ':' located_var_spec_init) | (fb_name ':' fb_initialization)) mario@181: */ mario@181: /* mario@181: SYM_REF6(instance_specific_init_c, resource_name, program_name, any_fb_name_list, variable_name, location, initialization) mario@181: */ mario@181: mario@181: /* helper symbol for instance_specific_init */ mario@181: /* function_block_type_name ':=' structure_initialization */ mario@181: /* mario@181: SYM_REF2(fb_initialization_c, function_block_type_name, structure_initialization) 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_param_iterator_c::visit(il_function_call_c *symbol) { mario@181: TRACE("il_function_call_c"); mario@181: if (NULL != symbol->il_operand_list) mario@181: return symbol->il_operand_list->accept(*this); mario@181: return NULL; 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_param_iterator_c::visit(il_formal_funct_call_c *symbol) { mario@181: TRACE("il_formal_funct_call_c"); mario@181: if (NULL != symbol->il_param_list) mario@181: return symbol->il_param_list->accept(*this); mario@181: return NULL; mario@181: } mario@181: mario@181: mario@181: /* il_call_operator prev_declared_fb_name mario@181: * | il_call_operator prev_declared_fb_name '(' ')' mario@181: * | il_call_operator prev_declared_fb_name '(' eol_list ')' mario@181: * | il_call_operator prev_declared_fb_name '(' il_operand_list ')' mario@181: * | il_call_operator prev_declared_fb_name '(' eol_list il_param_list ')' mario@181: */ mario@181: // SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list) mario@181: void *function_call_param_iterator_c::visit(il_fb_call_c *symbol) { mario@181: TRACE("il_fb_call_c"); mario@181: /* the following should never occur. In reality the syntax parser mario@181: * will guarantee that they never occur, but it makes it easier to mario@181: * understand the remaining code :-) mario@181: */ mario@181: //if ((NULL == symbol->il_operand_list) && (NULL == symbol->il_param_list)) ERROR; mario@181: //if ((NULL != symbol->il_operand_list) && (NULL != symbol->il_param_list)) ERROR; mario@181: mario@181: if (NULL != symbol->il_operand_list) mario@181: return symbol->il_operand_list->accept(*this); mario@181: if (NULL != symbol->il_param_list) mario@181: return symbol->il_param_list->accept(*this); mario@181: return NULL; mario@181: } mario@181: mario@181: /* | il_operand_list ',' il_operand */ mario@181: // SYM_LIST(il_operand_list_c) mario@181: void *function_call_param_iterator_c::visit(il_operand_list_c *symbol) { mario@181: TRACE("il_operand_list_c"); mario@181: return search_list(symbol); mario@181: } mario@181: mario@181: mario@181: /* | il_initial_param_list il_param_instruction */ mario@181: // SYM_LIST(il_param_list_c) mario@181: void *function_call_param_iterator_c::visit(il_param_list_c *symbol) { mario@181: TRACE("il_param_list_c"); mario@181: return search_list(symbol); mario@181: } mario@181: mario@181: /* il_assign_operator il_operand mario@181: * | il_assign_operator '(' eol_list simple_instr_list ')' mario@181: */ mario@181: // SYM_REF4(il_param_assignment_c, il_assign_operator, il_operand, simple_instr_list, unused) mario@181: void *function_call_param_iterator_c::visit(il_param_assignment_c *symbol) { mario@181: TRACE("il_param_assignment_c"); mario@181: mario@181: // TODO : We do not yet handle a instruction list passed as parameter !!! mario@181: // since we do not yet support it, it is best to simply stop than to fail silently... mario@181: if (NULL != symbol->simple_instr_list) ERROR; mario@181: mario@181: return handle_parameter_assignment(symbol->il_assign_operator, symbol->il_operand); mario@181: } mario@181: mario@181: /* il_assign_out_operator variable */ mario@181: // SYM_REF2(il_param_out_assignment_c, il_assign_out_operator, variable); mario@181: void *function_call_param_iterator_c::visit(il_param_out_assignment_c *symbol) { mario@181: TRACE("il_param_out_assignment_c"); mario@181: return handle_parameter_assignment((symbol_c *)symbol->il_assign_out_operator->accept(*this), symbol->variable); mario@181: } mario@181: mario@181: mario@181: /*******************/ mario@181: /* B 2.2 Operators */ mario@181: /*******************/ mario@181: /*| [NOT] any_identifier SENDTO */ mario@181: // SYM_REF2(il_assign_out_operator_c, option, variable_name) mario@181: void *function_call_param_iterator_c::visit(il_assign_out_operator_c *symbol) { mario@181: TRACE("il_assign_out_operator_c"); mario@181: mario@181: // TODO : Handle not_param !!! mario@181: // we do not yet support it, so it is best to simply stop than to fail silently... mario@181: if (NULL != symbol->option) ERROR; mario@181: mario@181: return (void *)symbol->variable_name; mario@181: } mario@181: 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: mario@181: /* mario@181: SYM_REF2(function_invocation_c, function_name, parameter_assignment_list) mario@181: */ mario@181: void *function_call_param_iterator_c::visit(function_invocation_c *symbol) { mario@181: TRACE("function_invocation_c"); mario@181: if ((symbol_c *)symbol == f_call && symbol->parameter_assignment_list != NULL) mario@181: return symbol->parameter_assignment_list->accept(*this); mario@181: else mario@181: return NULL; mario@181: } mario@181: mario@181: mario@181: /********************/ mario@181: /* B 3.2 Statements */ mario@181: /********************/ mario@181: mario@181: /*********************************/ mario@181: /* B 3.2.1 Assignment Statements */ mario@181: /*********************************/ mario@181: /* mario@181: SYM_REF2(assignment_statement_c, l_exp, r_exp) mario@181: */ mario@181: mario@181: /*****************************************/ mario@181: /* B 3.2.2 Subprogram Control Statements */ mario@181: /*****************************************/ mario@181: /* RETURN */ mario@181: // SYM_REF0(return_statement_c) mario@181: mario@181: mario@181: /* fb_name '(' [param_assignment_list] ')' */ mario@181: /* param_assignment_list -> may be NULL ! */ mario@181: // SYM_REF2(fb_invocation_c, fb_name, param_assignment_list) mario@181: void *function_call_param_iterator_c::visit(fb_invocation_c *symbol) { mario@181: TRACE("fb_invocation_c"); mario@181: if (symbol->param_assignment_list != NULL) mario@181: return symbol->param_assignment_list->accept(*this); mario@181: else mario@181: return NULL; mario@181: } mario@181: mario@181: /* helper symbol for fb_invocation */ mario@181: /* param_assignment_list ',' param_assignment */ mario@181: // SYM_LIST(param_assignment_list_c) mario@181: void *function_call_param_iterator_c::visit(param_assignment_list_c *symbol) { mario@181: TRACE("param_assignment_list_c"); mario@181: return search_list(symbol); mario@181: } mario@181: mario@181: /* variable_name ASSIGN expression */ mario@181: // SYM_REF2(input_variable_param_assignment_c, variable_name, expression) mario@181: void *function_call_param_iterator_c::visit(input_variable_param_assignment_c *symbol) { mario@181: TRACE("input_variable_param_assignment_c"); mario@181: return handle_parameter_assignment(symbol->variable_name, symbol->expression); mario@181: } mario@181: mario@181: /* [NOT] variable_name '=>' variable */ mario@181: // SYM_REF4(output_variable_param_assignment_c, not_param, variable_name, variable, unused) mario@181: void *function_call_param_iterator_c::visit(output_variable_param_assignment_c *symbol) { mario@181: TRACE("output_variable_param_assignment_c"); mario@181: // TODO : Handle not_param !!! mario@181: if (NULL != symbol->not_param) ERROR; // we do not yet support it, so it is best to simply stop than to fail silently... mario@181: mario@181: return handle_parameter_assignment(symbol->variable_name, symbol->variable); mario@181: } mario@181: mario@181: /* helper CLASS for output_variable_param_assignment */ mario@181: // SYM_REF0(not_paramassign_c) mario@181: // TODO... ??? mario@181: mario@181: