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)
Edouard@279: * Copyright (C) 2007-2011 Laurent Bessard and Edouard Tisserant
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: #include "../absyntax/visitor.hh"
mario@181:
mario@181:
mario@181: class function_param_iterator_c : public null_visitor_c {
mario@181: public:
mario@181: /* A type to specify the type of parameter.
mario@181: * VAR_INPUT => direction_in
mario@181: * VAR_OUTPUT => direction_out
mario@181: * VAR_IN_OUT => direction_inout
mario@181: * VAR_EXTERNAL => direction_extref
mario@181: *
mario@181: * Note that VAR_EXTERNAL declares variables that are in reality references
mario@181: * to global variables. This is used only inside programs!
mario@181: * These references to external variables must be correctly initialised to refer
mario@181: * to the correct global variable. Note that the user may define which variable is to be
mario@181: * referenced in a CONFIGURATION, and that different instantiations of a program
mario@181: * may have the same external variable reference diffenrent global variables!
mario@181: * The references must therefore be correctly initialised when the program instance
mario@181: * is created. This may be done by the PROGRAM class constructor since the ST and IL
mario@181: * languages do not allow the VAR_EXTERNAL reference to change at runtime
mario@181: * for a specific instance.
mario@181: *
mario@181: * We therefore need to call a PROGRAM class constructor with the variables
mario@181: * that should be refernced by the VAR_EXTERNAL variables. The direction_extref will
mario@181: * be used to identify these parameters!
mario@181: */
mario@181: typedef enum {direction_in, direction_out, direction_inout, direction_extref} param_direction_t ;
mario@181:
mario@181:
mario@181: private:
ccb@202: /* a pointer to the function_block_declaration_c
ccb@202: * or function_declaration_c currently being analysed.
ccb@202: */
mario@181: symbol_c *f_decl;
mario@181: int next_param, param_count;
ccb@202: /* used when called to search() for a parameter by name */
ccb@202: identifier_c *search_param_name;
ccb@202: /* used when called to iterate() for a parameter */
mario@181: identifier_c *current_param_name;
mario@181: symbol_c *current_param_type;
mario@181: symbol_c *current_param_default_value;
mario@181: param_direction_t current_param_direction;
laurent@233: bool en_eno_param_implicit;
msousa@350: /* used when we reach an extensible parameter in the function declaration */
msousa@350: /* NOTE: this will handle syntax that is not in the standard.
msousa@350: * It is used to handle the extensible standard functions
msousa@350: * (e.g. AND(word#3, word#55, word#44); )
msousa@350: * See absyntax.def or iec.y for more details.
msousa@350: */
msousa@350: bool current_param_is_extensible;
msousa@350: int current_extensible_param_index;
msousa@350: int _first_extensible_param_index;
msousa@350:
ccb@202: /* Which operation of the class was called...
ccb@202: * Search a parameter, or iterate to the next parameter.
ccb@202: */
ccb@202: typedef enum {iterate_op, search_op} operation_t;
ccb@202: operation_t current_operation;
msousa@424:
msousa@424: /* the last parameter/value returned by search() or next() */
msousa@424: symbol_c *last_returned_parameter;
msousa@424:
mario@181: private:
msousa@350: int cmp_extparam_names(const char* s1, const char* s2);
mario@181: void* handle_param_list(list_c *list);
mario@181: void* handle_single_param(symbol_c *var_name);
mario@181:
mario@181: void* iterate_list(list_c *list);
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.
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(symbol_c *pou_decl);
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 *next(void);
mario@181:
ccb@202: /* Search for the parameter named ... */
ccb@202: /* The seach() function does not in any way affect the internal state related
ccb@202: * to the iterate() function.
ccb@202: * It will, however, affect the internal state necessary to correctly
msousa@350: * return the param_type(), default_value() and is_en_eno_param_implicit()
msousa@350: * of the found parameter.
ccb@202: */
ccb@202: identifier_c *search(symbol_c *param_name);
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 *default_value(void);
mario@181:
mario@181: /* Returns the currently referenced parameter's type name. */
mario@181: symbol_c *param_type(void);
mario@181:
laurent@233: /* Returns if currently referenced parameter is an implicit defined EN/ENO parameter. */
laurent@233: bool is_en_eno_param_implicit(void);
laurent@233:
msousa@350: /* Returns if currently referenced parameter is an extensible parameter. */
msousa@350: /* extensible paramters only occur in some standard functions, e.g. AND(word#34, word#44, word#65); */
msousa@350: bool is_extensible_param(void);
msousa@350: /* Returns the index of the current extensible parameter. */
msousa@350: /* If the current parameter is not an extensible paramter, returns -1 */
msousa@350: int extensible_param_index(void);
msousa@350: /* Returns the index of the first extensible parameter, or -1 if no extensible parameter found. */
msousa@350: /* WARNING: Will only return the correct value _after_ an extensible parameter has been found! */
msousa@350: int first_extensible_param_index(void);
msousa@350:
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: param_direction_t param_direction(void);
mario@181:
mario@181: private:
laurent@233: void *visit(implicit_definition_c *symbol);
mario@181: /******************************************/
mario@181: /* B 1.4.3 - Declaration & Initialisation */
mario@181: /******************************************/
mario@181: void *visit(input_declarations_c *symbol);
mario@181:
mario@181: void *visit(input_declaration_list_c *symbol);
mario@181:
mario@181: void *visit(edge_declaration_c *symbol);
mario@181: void *visit(en_param_declaration_c *symbol);
mario@181: /* var1_list ':' array_spec_init */
mario@181: //SYM_REF2(array_var_init_decl_c, var1_list, array_spec_init)
mario@181: void *visit(array_var_init_decl_c *symbol);
mario@181:
mario@181: /* var1_list ':' initialized_structure */
mario@181: //SYM_REF2(structured_var_init_decl_c, var1_list, initialized_structure)
mario@181: void *visit(structured_var_init_decl_c *symbol);
mario@181:
mario@181: #if 0
mario@181: /* name_list ':' function_block_type_name ASSIGN structure_initialization */
mario@181: /* structure_initialization -> may be NULL ! */
mario@181: SYM_REF4(fb_name_decl_c, fb_name_list, function_block_type_name, structure_initialization, unused)
mario@181:
mario@181: /* name_list ',' fb_name */
mario@181: SYM_LIST(fb_name_list_c)
mario@181: #endif
mario@181:
mario@181: void *visit(output_declarations_c *symbol);
mario@181: void *visit(eno_param_declaration_c *symbol);
mario@181: void *visit(input_output_declarations_c *symbol);
mario@181: void *visit(var_declaration_list_c *symbol);
mario@181:
mario@181:
mario@181: /* var1_list ':' array_specification */
mario@181: //SYM_REF2(array_var_declaration_c, var1_list, array_specification)
mario@181: void *visit(array_var_declaration_c *symbol);
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 *visit(structured_var_declaration_c *symbol);
mario@181:
mario@181: /* VAR [CONSTANT] var_init_decl_list END_VAR */
mario@181: void *visit(var_declarations_c *symbol);
mario@181:
mario@181: #if 0
mario@181: /* VAR RETAIN var_init_decl_list END_VAR */
mario@181: SYM_REF2(retentive_var_declarations_c, var_init_decl_list, unused)
mario@181:
mario@181: /* VAR [CONSTANT|RETAIN|NON_RETAIN] located_var_decl_list END_VAR */
mario@181: /* option -> may be NULL ! */
mario@181: SYM_REF2(located_var_declarations_c, option, located_var_decl_list)
mario@181:
mario@181: /* helper symbol for located_var_declarations */
mario@181: /* located_var_decl_list located_var_decl ';' */
mario@181: SYM_LIST(located_var_decl_list_c)
mario@181:
mario@181: /* [variable_name] location ':' located_var_spec_init */
mario@181: /* variable_name -> may be NULL ! */
mario@181: SYM_REF4(located_var_decl_c, variable_name, location, located_var_spec_init, unused)
mario@181: #endif
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 *visit(external_var_declarations_c *symbol);
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 *visit(external_declaration_list_c *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 *visit(external_declaration_c *symbol);
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 *visit(var1_init_decl_c *symbol);
mario@181: void *visit(var1_list_c *symbol);
mario@181: void *visit(var_init_decl_list_c *symbol);
mario@181:
mario@181:
mario@181: /***********************/
mario@181: /* B 1.5.1 - Functions */
mario@181: /***********************/
mario@181: void *visit(function_declaration_c *symbol);
mario@181: /* intermediate helper symbol for function_declaration */
mario@181: void *visit(var_declarations_list_c *symbol);
mario@181: void *visit(function_var_decls_c *symbol);
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 *visit(function_block_declaration_c *symbol);
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 *visit(temp_var_decls_c *symbol);
mario@181: void *visit(temp_var_decls_list_c *symbol);
mario@181:
mario@181: /* VAR NON_RETAIN var_init_decl_list END_VAR */
mario@181: void *visit(non_retentive_var_decls_c *symbol);
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 *visit(program_declaration_c *symbol);
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: }; // function_param_iterator_c
mario@181:
mario@181:
mario@181:
mario@181:
mario@181:
mario@181:
mario@181: