absyntax_utils/function_param_iterator.hh
changeset 181 38d6eb056260
child 202 da1a8186f86f
equal deleted inserted replaced
180:64334c5a00b1 181:38d6eb056260
       
     1 /*
       
     2  * (c) 2003 Mario de Sousa
       
     3  *
       
     4  * Offered to the public under the terms of the GNU General Public License
       
     5  * as published by the Free Software Foundation; either version 2 of the
       
     6  * License, or (at your option) any later version.
       
     7  *
       
     8  * This program is distributed in the hope that it will be useful, but
       
     9  * WITHOUT ANY WARRANTY; without even the implied warranty of
       
    10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
       
    11  * Public License for more details.
       
    12  *
       
    13  * This code is made available on the understanding that it will not be
       
    14  * used in safety-critical situations without a full and competent review.
       
    15  */
       
    16 
       
    17 /*
       
    18  * An IEC 61131-3 IL and ST compiler.
       
    19  *
       
    20  * Based on the
       
    21  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
       
    22  *
       
    23  */
       
    24 
       
    25 
       
    26 /*
       
    27  * Function parameter iterator.
       
    28  * Iterate through the in/out parameters of a function declaration.
       
    29  * Function blocks are also suported.
       
    30  *
       
    31  * This is part of the 4th stage that generates
       
    32  * a c++ source program equivalent to the IL and ST
       
    33  * code.
       
    34  */
       
    35 
       
    36 /* Given a function_declaration_c, iterate through each
       
    37  * function in/out/inout parameter, returning the name
       
    38  * of each parameter...function_param_iterator_c
       
    39  */
       
    40 
       
    41 
       
    42 #include "../absyntax/visitor.hh"
       
    43 
       
    44 
       
    45 class function_param_iterator_c : public null_visitor_c {
       
    46   public:
       
    47     /* A type to specify the type of parameter.
       
    48      * VAR_INPUT => direction_in
       
    49      * VAR_OUTPUT => direction_out
       
    50      * VAR_IN_OUT => direction_inout
       
    51      * VAR_EXTERNAL => direction_extref
       
    52      *
       
    53      * Note that VAR_EXTERNAL declares variables that are in reality references
       
    54      * to global variables. This is used only inside programs!
       
    55      * These references to external variables must be correctly initialised to refer
       
    56      * to the correct global variable. Note that the user may define which variable is to be
       
    57      * referenced in a CONFIGURATION, and that different instantiations of a program
       
    58      * may have the same external variable reference diffenrent global variables!
       
    59      * The references must therefore be correctly initialised when the program instance
       
    60      * is created. This may be done by the PROGRAM class constructor since the ST and IL
       
    61      * languages do not allow the VAR_EXTERNAL reference to change at runtime
       
    62      * for a specific instance.
       
    63      *
       
    64      * We therefore need to call a PROGRAM class constructor with the variables
       
    65      * that should be refernced by the VAR_EXTERNAL variables. The direction_extref will
       
    66      * be used to identify these parameters!
       
    67      */
       
    68     typedef enum {direction_in, direction_out, direction_inout, direction_extref} param_direction_t ;
       
    69 
       
    70 
       
    71   private:
       
    72       /* a pointer to the function_block_declaration_c
       
    73        * or function_declaration_c currently being analysed.
       
    74        */
       
    75     symbol_c *f_decl;
       
    76     int next_param, param_count;
       
    77     identifier_c *current_param_name;
       
    78     symbol_c *current_param_type;
       
    79     symbol_c *current_param_default_value;
       
    80     param_direction_t current_param_direction;
       
    81     bool en_declared;
       
    82     bool eno_declared;
       
    83 
       
    84   private:
       
    85     void* handle_param_list(list_c *list);
       
    86     void* handle_single_param(symbol_c *var_name);
       
    87 
       
    88     void* iterate_list(list_c *list);
       
    89 
       
    90   public:
       
    91     /* start off at the first parameter once again... */
       
    92     void reset(void);
       
    93 
       
    94     /* initialise the iterator object.
       
    95      * We must be given a reference to the function declaration
       
    96      * that will be analysed...
       
    97      */
       
    98     function_param_iterator_c(function_declaration_c *f_decl);
       
    99 
       
   100     /* initialise the iterator object.
       
   101      * We must be given a reference to the function block declaration
       
   102      * that will be analysed...
       
   103      */
       
   104     function_param_iterator_c(function_block_declaration_c *fb_decl);
       
   105 
       
   106     /* initialise the iterator object.
       
   107      * We must be given a reference to the program declaration
       
   108      * that will be analysed...
       
   109      */
       
   110     function_param_iterator_c(program_declaration_c *p_decl);
       
   111 
       
   112     /* Skip to the next parameter. After object creation,
       
   113      * the object references on parameter _before_ the first, so
       
   114      * this function must be called once to get the object to
       
   115      * reference the first parameter...
       
   116      *
       
   117      * Returns the parameter's name!
       
   118      */
       
   119     identifier_c *next(void);
       
   120 
       
   121     identifier_c *declare_en_param(void);
       
   122 
       
   123     identifier_c *declare_eno_param(void);
       
   124 
       
   125     /* Returns the currently referenced parameter's default value,
       
   126      * or NULL if none is specified in the function declrataion itself.
       
   127      */
       
   128     symbol_c *default_value(void);
       
   129 
       
   130     /* Returns the currently referenced parameter's type name. */
       
   131     symbol_c *param_type(void);
       
   132 
       
   133     /* Returns the currently referenced parameter's data passing direction.
       
   134      * i.e. VAR_INPUT, VAR_OUTPUT or VAR_INOUT
       
   135      */
       
   136     param_direction_t param_direction(void);
       
   137 
       
   138     private:
       
   139     /******************************************/
       
   140     /* B 1.4.3 - Declaration & Initialisation */
       
   141     /******************************************/
       
   142     void *visit(input_declarations_c *symbol);
       
   143 
       
   144     void *visit(input_declaration_list_c *symbol);
       
   145 
       
   146     void *visit(edge_declaration_c *symbol);
       
   147     void *visit(en_param_declaration_c *symbol);
       
   148     /* var1_list ':' array_spec_init */
       
   149     //SYM_REF2(array_var_init_decl_c, var1_list, array_spec_init)
       
   150     void *visit(array_var_init_decl_c *symbol);
       
   151     
       
   152     /*  var1_list ':' initialized_structure */
       
   153     //SYM_REF2(structured_var_init_decl_c, var1_list, initialized_structure)
       
   154     void *visit(structured_var_init_decl_c *symbol);
       
   155     
       
   156 #if 0
       
   157 /* name_list ':' function_block_type_name ASSIGN structure_initialization */
       
   158 /* structure_initialization -> may be NULL ! */
       
   159 SYM_REF4(fb_name_decl_c, fb_name_list, function_block_type_name, structure_initialization, unused)
       
   160 
       
   161 /* name_list ',' fb_name */
       
   162 SYM_LIST(fb_name_list_c)
       
   163 #endif
       
   164 
       
   165     void *visit(output_declarations_c *symbol);
       
   166     void *visit(eno_param_declaration_c *symbol);
       
   167     void *visit(input_output_declarations_c *symbol);
       
   168     void *visit(var_declaration_list_c *symbol);
       
   169 
       
   170 
       
   171     /*  var1_list ':' array_specification */
       
   172     //SYM_REF2(array_var_declaration_c, var1_list, array_specification)
       
   173     void *visit(array_var_declaration_c *symbol);
       
   174 
       
   175     /*  var1_list ':' structure_type_name */
       
   176     //SYM_REF2(structured_var_declaration_c, var1_list, structure_type_name)
       
   177     void *visit(structured_var_declaration_c *symbol);
       
   178 
       
   179     /* VAR [CONSTANT] var_init_decl_list END_VAR */
       
   180     void *visit(var_declarations_c *symbol);
       
   181 
       
   182 #if 0
       
   183 /*  VAR RETAIN var_init_decl_list END_VAR */
       
   184 SYM_REF2(retentive_var_declarations_c, var_init_decl_list, unused)
       
   185 
       
   186 /*  VAR [CONSTANT|RETAIN|NON_RETAIN] located_var_decl_list END_VAR */
       
   187 /* option -> may be NULL ! */
       
   188 SYM_REF2(located_var_declarations_c, option, located_var_decl_list)
       
   189 
       
   190 /* helper symbol for located_var_declarations */
       
   191 /* located_var_decl_list located_var_decl ';' */
       
   192 SYM_LIST(located_var_decl_list_c)
       
   193 
       
   194 /*  [variable_name] location ':' located_var_spec_init */
       
   195 /* variable_name -> may be NULL ! */
       
   196 SYM_REF4(located_var_decl_c, variable_name, location, located_var_spec_init, unused)
       
   197 #endif
       
   198 
       
   199     /*| VAR_EXTERNAL [CONSTANT] external_declaration_list END_VAR */
       
   200     /* option -> may be NULL ! */
       
   201     // SYM_REF2(external_var_declarations_c, option, external_declaration_list)
       
   202     void *visit(external_var_declarations_c *symbol);
       
   203 
       
   204     /* helper symbol for external_var_declarations */
       
   205     /*| external_declaration_list external_declaration';' */
       
   206     // SYM_LIST(external_declaration_list_c)
       
   207     void *visit(external_declaration_list_c *symbol);
       
   208 
       
   209     /*  global_var_name ':' (simple_specification|subrange_specification|enumerated_specification|array_specification|prev_declared_structure_type_name|function_block_type_name */
       
   210     //SYM_REF2(external_declaration_c, global_var_name, specification)
       
   211     void *visit(external_declaration_c *symbol);
       
   212 
       
   213 
       
   214 #if 0
       
   215 /*| VAR_GLOBAL [CONSTANT|RETAIN] global_var_decl_list END_VAR */
       
   216 /* option -> may be NULL ! */
       
   217 SYM_REF2(global_var_declarations_c, option, global_var_decl_list)
       
   218 
       
   219 /* helper symbol for global_var_declarations */
       
   220 /*| global_var_decl_list global_var_decl ';' */
       
   221 SYM_LIST(global_var_decl_list_c)
       
   222 
       
   223 /*| global_var_spec ':' [located_var_spec_init|function_block_type_name] */
       
   224 /* type_specification ->may be NULL ! */
       
   225 SYM_REF2(global_var_decl_c, global_var_spec, type_specification)
       
   226 
       
   227 /*| global_var_name location */
       
   228 SYM_REF2(global_var_spec_c, global_var_name, location)
       
   229 
       
   230 /*  AT direct_variable */
       
   231 SYM_REF2(location_c, direct_variable, unused)
       
   232 
       
   233 /*| global_var_list ',' global_var_name */
       
   234 SYM_LIST(global_var_list_c)
       
   235 
       
   236 /*  var1_list ':' single_byte_string_spec */
       
   237 SYM_REF2(single_byte_string_var_declaration_c, var1_list, single_byte_string_spec)
       
   238 
       
   239 /*  STRING ['[' integer ']'] [ASSIGN single_byte_character_string] */
       
   240 /* integer ->may be NULL ! */
       
   241 /* single_byte_character_string ->may be NULL ! */
       
   242 SYM_REF2(single_byte_string_spec_c, integer, single_byte_character_string)
       
   243 
       
   244 /*  var1_list ':' double_byte_string_spec */
       
   245 SYM_REF2(double_byte_string_var_declaration_c, var1_list, double_byte_string_spec)
       
   246 
       
   247 /*  WSTRING ['[' integer ']'] [ASSIGN double_byte_character_string] */
       
   248 /* integer ->may be NULL ! */
       
   249 /* double_byte_character_string ->may be NULL ! */
       
   250 SYM_REF2(double_byte_string_spec_c, integer, double_byte_character_string)
       
   251 
       
   252 /*| VAR [RETAIN|NON_RETAIN] incompl_located_var_decl_list END_VAR */
       
   253 /* option ->may be NULL ! */
       
   254 SYM_REF2(incompl_located_var_declarations_c, option, incompl_located_var_decl_list)
       
   255 
       
   256 /* helper symbol for incompl_located_var_declarations */
       
   257 /*| incompl_located_var_decl_list incompl_located_var_decl ';' */
       
   258 SYM_LIST(incompl_located_var_decl_list_c)
       
   259 
       
   260 /*  variable_name incompl_location ':' var_spec */
       
   261 SYM_REF4(incompl_located_var_decl_c, variable_name, incompl_location, var_spec, unused)
       
   262 
       
   263 /*  AT incompl_location_token */
       
   264 SYM_TOKEN(incompl_location_c)
       
   265 #endif
       
   266 
       
   267 
       
   268     void *visit(var1_init_decl_c *symbol);
       
   269     void *visit(var1_list_c *symbol);
       
   270     void *visit(var_init_decl_list_c *symbol);
       
   271 
       
   272 
       
   273     /***********************/
       
   274     /* B 1.5.1 - Functions */
       
   275     /***********************/
       
   276     void *visit(function_declaration_c *symbol);
       
   277     /* intermediate helper symbol for function_declaration */
       
   278     void *visit(var_declarations_list_c *symbol);
       
   279     void *visit(function_var_decls_c *symbol);
       
   280 
       
   281 
       
   282     /*****************************/
       
   283     /* B 1.5.2 - Function Blocks */
       
   284     /*****************************/
       
   285     /*  FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */
       
   286     void *visit(function_block_declaration_c *symbol);
       
   287 
       
   288     /* intermediate helper symbol for function_declaration */
       
   289     /*  { io_var_declarations | other_var_declarations }   */
       
   290     /*
       
   291      * NOTE: we re-use the var_declarations_list_c
       
   292      */
       
   293 
       
   294     /*  VAR_TEMP temp_var_decl_list END_VAR */
       
   295     void *visit(temp_var_decls_c *symbol);
       
   296     void *visit(temp_var_decls_list_c *symbol);
       
   297 
       
   298     /*  VAR NON_RETAIN var_init_decl_list END_VAR */
       
   299     void *visit(non_retentive_var_decls_c *symbol);
       
   300 
       
   301 
       
   302     /**********************/
       
   303     /* B 1.5.3 - Programs */
       
   304     /**********************/
       
   305     /*  PROGRAM program_type_name program_var_declarations_list function_block_body END_PROGRAM */
       
   306     // SYM_REF4(program_declaration_c, program_type_name, var_declarations, function_block_body, unused)
       
   307     void *visit(program_declaration_c *symbol);
       
   308 
       
   309     /* intermediate helper symbol for program_declaration_c */
       
   310     /*  { io_var_declarations | other_var_declarations }   */
       
   311     /*
       
   312      * NOTE: we re-use the var_declarations_list_c
       
   313      */
       
   314 
       
   315 }; // function_param_iterator_c
       
   316 
       
   317 
       
   318 
       
   319 
       
   320 
       
   321 
       
   322