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: * Function call parameter iterator. ccb@202: * It will iterate through the non-formal parameters of a function call mario@181: * (i.e. function calls using the foo(, , ...) syntax). ccb@202: * and/or search through the 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: * ccb@202: * Note that calls to next_nf() will only iterate through non-formal parameters, ccb@202: * calls to next_f() will only iterate through formal parameters, ccb@202: * and calls to search_f() will only serach through formal parameters. mario@181: */ mario@181: mario@181: mario@181: #include "../absyntax/visitor.hh" mario@181: mario@181: mario@181: class function_call_param_iterator_c : public null_visitor_c { mario@181: mario@181: private: mario@181: /* a pointer to the function call mario@181: * (or function block or program call!) mario@181: */ mario@181: symbol_c *f_call; ccb@202: int iterate_f_next_param, iterate_nf_next_param, param_count; mario@181: identifier_c *search_param_name; ccb@202: symbol_c *current_value; ccb@202: ccb@202: /* Which operation of the class was called: ccb@202: * - iterate to the next non-formal parameter. ccb@202: * - iterate to the next formal parameter. ccb@202: * - search a formal parameter, mario@181: */ ccb@202: typedef enum {iterate_nf_op, iterate_f_op, search_f_op} operation_t; mario@181: operation_t current_operation; mario@181: mario@181: private: mario@181: void *search_list(list_c *list); mario@181: void *handle_parameter_assignment(symbol_c *variable_name, symbol_c *expression) ; mario@181: mario@181: mario@181: public: mario@181: /* start off at the first parameter once again... */ mario@181: void reset(void); 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(symbol_c *f_call); mario@181: ccb@202: /* Skip to the next formal parameter. After object creation, ccb@202: * the object references on parameter _before_ the first, so ccb@202: * this function must be called once to get the object to ccb@202: * reference the first parameter... ccb@202: * ccb@202: * Returns the paramater name to which a value is being passed! ccb@202: * You can determine the value being passed by calling ccb@202: * function_call_param_iterator_c::search_f() ccb@202: */ ccb@202: symbol_c *next_f(void); ccb@202: ccb@202: /* Skip to the next non-formal 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: */ ccb@202: symbol_c *next_nf(void); mario@181: mario@181: /* Search for the value passed to the parameter named ... */ ccb@202: symbol_c *search_f(symbol_c *param_name); ccb@202: ccb@202: /* Returns the value being passed to the current parameter. */ ccb@202: symbol_c *get_current_value(void); mario@181: mario@181: mario@181: private: 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 *visit(program_configuration_c *symbol); mario@181: mario@181: /* prog_conf_elements ',' prog_conf_element */ mario@181: // SYM_LIST(prog_conf_elements_c) mario@181: void *visit(prog_conf_elements_c *symbol); 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 *visit(prog_cnxn_assign_c *symbol); 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 *visit(prog_cnxn_sendto_c *symbol); 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: /* 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 *visit(il_function_call_c *symbol); 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 *visit(il_formal_funct_call_c *symbol); 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 *visit(il_fb_call_c *symbol); mario@181: mario@181: mario@181: mario@181: /* | il_operand_list ',' il_operand */ mario@181: // SYM_LIST(il_operand_list_c) mario@181: void *visit(il_operand_list_c *symbol); mario@181: mario@181: mario@181: /* | il_initial_param_list il_param_instruction */ mario@181: // SYM_LIST(il_param_list_c) mario@181: void *visit(il_param_list_c *symbol); 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 *visit(il_param_assignment_c *symbol); 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 *visit(il_param_out_assignment_c *symbol); mario@181: mario@181: mario@181: /*******************/ mario@181: /* B 2.2 Operators */ mario@181: /*******************/ ccb@202: /* any_identifier ASSIGN */ ccb@202: // SYM_REF1(il_assign_operator_c, variable_name) ccb@202: void *visit(il_assign_operator_c *symbol); ccb@202: mario@181: /*| [NOT] any_identifier SENDTO */ mario@181: // SYM_REF2(il_assign_out_operator_c, option, variable_name) mario@181: void *visit(il_assign_out_operator_c *symbol); 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 *visit(function_invocation_c *symbol); 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 *visit(fb_invocation_c *symbol); 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 *visit(param_assignment_list_c *symbol); mario@181: mario@181: /* variable_name ASSIGN expression */ mario@181: // SYM_REF2(input_variable_param_assignment_c, variable_name, expression) mario@181: void *visit(input_variable_param_assignment_c *symbol); 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 *visit(output_variable_param_assignment_c *symbol); 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: }; // function_call_param_iterator_c mario@181: mario@181: mario@181: mario@181: