stage4/generate_c/function_call_param_iterator.cc
changeset 70 e1f0ebd2d9ec
child 146 eef5e62048c7
equal deleted inserted replaced
69:41cb5b80416e 70:e1f0ebd2d9ec
       
     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 call parameter iterator.
       
    28  * It will iterate through the formal parameters of a function call
       
    29  * (i.e. function calls using the foo(<param1>, <param2>, ...) syntax).
       
    30  * and/or search through the non-formal parameters of a function call
       
    31  * (i.e. function calls using the foo(<name1> = <param1>, <name2> = <param2>, ...) syntax).
       
    32  *
       
    33  * Calls to function blocks and programs are also supported.
       
    34  *
       
    35  * This is part of the 4th stage that generates
       
    36  * a c++ source program equivalent to the IL and ST
       
    37  * code.
       
    38  */
       
    39 
       
    40 
       
    41 
       
    42 
       
    43 
       
    44 
       
    45 //#include <stdio.h>  /* required for NULL */
       
    46 //#include <string>
       
    47 //#include <iostream>
       
    48 
       
    49 //#include "../../util/symtable.hh"
       
    50 
       
    51 //#include "generate_c.hh"
       
    52 
       
    53 
       
    54 #include "../../absyntax/visitor.hh"
       
    55 
       
    56 
       
    57 /*
       
    58  * Function call parameter iterator.
       
    59  * It will iterate through the formal parameters of a function call
       
    60  * (i.e. function calls using the foo(<param1>, <param2>, ...) syntax).
       
    61  * and/or search through the non-formal parameters of a function call
       
    62  * (i.e. function calls using the foo(<name1> = <param1>, <name2> = <param2>, ...) syntax).
       
    63  *
       
    64  * Calls to function blocks and programs are also supported.
       
    65  *
       
    66  * Note that calls to next() will only iterate through formal parameters,
       
    67  * and calls to search()  will only serach through non-formal parameters.
       
    68  */
       
    69 
       
    70 class function_call_param_iterator_c : public null_visitor_c {
       
    71   private:
       
    72       /* a pointer to the function call
       
    73        * (or function block or program call!)
       
    74        */
       
    75     symbol_c *f_call;
       
    76     int next_param, param_count;
       
    77     identifier_c *search_param_name;
       
    78 
       
    79     /* Which operation of the class was called...
       
    80      * Search a parameter, or iterate to the next parameter.
       
    81      */
       
    82     typedef enum {iterate_op, search_op} operation_t;
       
    83     operation_t current_operation;
       
    84 
       
    85   private:
       
    86     void *search_list(list_c *list) {
       
    87       switch (current_operation) {
       
    88         case iterate_op:
       
    89           for(int i = 0; i < list->n; i++) {
       
    90             void *res = list->elements[i]->accept(*this);
       
    91             if (NULL != res) {
       
    92               /* It went through the handle_parameter_assignment() function,
       
    93                * and is therefore a parameter assignment (<param> = <value>),
       
    94                * and not a simple expression (<value>).
       
    95                */
       
    96               /* we do nothing... */
       
    97             } else {
       
    98               param_count++;
       
    99               if (param_count == next_param) {
       
   100                 return list->elements[i];
       
   101               }
       
   102             }
       
   103           }
       
   104           return NULL;
       
   105           break;
       
   106 
       
   107         case search_op:
       
   108           for(int i = 0; i < list->n; i++) {
       
   109             void *res = list->elements[i]->accept(*this);
       
   110             if (res != NULL)
       
   111               return res;
       
   112           }
       
   113           return NULL;
       
   114           break;
       
   115       } /* switch */
       
   116       return NULL;
       
   117     }
       
   118 
       
   119 
       
   120 
       
   121     void *handle_parameter_assignment(symbol_c *variable_name, symbol_c *expression) {
       
   122       switch (current_operation) {
       
   123         case iterate_op:
       
   124 	        /* UGLY HACK -> this will be detected in the search_list() function */
       
   125           return (void *)this; /* anything, as long as it is not NULL!! */
       
   126           break;
       
   127 
       
   128         case search_op:
       
   129           identifier_c *variable_name2 = dynamic_cast<identifier_c *>(variable_name);
       
   130           if (variable_name2 == NULL) ERROR;
       
   131           if (strcasecmp(search_param_name->value, variable_name2->value) == 0)
       
   132             /* FOUND! This is the same parameter!! */
       
   133             return (void *)expression;
       
   134           return NULL;
       
   135           break;
       
   136       }
       
   137 
       
   138       ERROR;
       
   139       return NULL;
       
   140     }
       
   141 
       
   142 
       
   143 
       
   144   public:
       
   145     /* start off at the first parameter once again... */
       
   146     void reset(void) {
       
   147       next_param = param_count = 0;
       
   148     }
       
   149 
       
   150     /* initialise the iterator object.
       
   151      * We must be given a reference to the function/program/function block call
       
   152      * that will be analysed...
       
   153      */
       
   154     function_call_param_iterator_c(symbol_c *f_call) {
       
   155       /* It is expected that f_call will reference one of the following:
       
   156        *  program_configuration_c
       
   157        *  function_invocation_c
       
   158        *  fb_invocation_c
       
   159        *  il_function_call_c
       
   160        *  il_formal_funct_call_c
       
   161        *  ... (have I missed any?)
       
   162        */
       
   163       this->f_call = f_call;
       
   164       search_param_name = NULL;
       
   165       reset();
       
   166     }
       
   167 
       
   168     /* Skip to the next parameter. After object creation,
       
   169      * the object references on parameter _before_ the first, so
       
   170      * this function must be called once to get the object to
       
   171      * reference the first parameter...
       
   172      *
       
   173      * Returns whatever is being passed to the parameter!
       
   174      */
       
   175     symbol_c *next(void) {
       
   176       param_count = 0;
       
   177       next_param++;
       
   178       current_operation = function_call_param_iterator_c::iterate_op;
       
   179       void *res = f_call->accept(*this);
       
   180       return (symbol_c *)res;
       
   181     }
       
   182 
       
   183     /* Search for the value passed to the parameter named <param_name>...  */
       
   184     symbol_c *search(symbol_c *param_name) {
       
   185       if (NULL == param_name) ERROR;
       
   186       search_param_name = dynamic_cast<identifier_c *>(param_name);
       
   187       if (NULL == search_param_name) ERROR;
       
   188       current_operation = function_call_param_iterator_c::search_op;
       
   189       void *res = f_call->accept(*this);
       
   190       return (symbol_c *)res;
       
   191     }
       
   192 
       
   193 
       
   194 
       
   195 
       
   196   private:
       
   197 /********************************/
       
   198 /* B 1.7 Configuration elements */
       
   199 /********************************/
       
   200 
       
   201 /*
       
   202 CONFIGURATION configuration_name
       
   203    optional_global_var_declarations
       
   204    (resource_declaration_list | single_resource_declaration)
       
   205    optional_access_declarations
       
   206    optional_instance_specific_initializations
       
   207 END_CONFIGURATION
       
   208 */
       
   209 /*
       
   210 SYM_REF6(configuration_declaration_c, configuration_name, global_var_declarations, resource_declarations, access_declarations, instance_specific_initializations, unused)
       
   211 */
       
   212 
       
   213 /* helper symbol for configuration_declaration */
       
   214 /*
       
   215 SYM_LIST(resource_declaration_list_c)
       
   216 */
       
   217 
       
   218 /*
       
   219 RESOURCE resource_name ON resource_type_name
       
   220    optional_global_var_declarations
       
   221    single_resource_declaration
       
   222 END_RESOURCE
       
   223 */
       
   224 /*
       
   225 SYM_REF4(resource_declaration_c, resource_name, resource_type_name, global_var_declarations, resource_declaration)
       
   226 */
       
   227 
       
   228 /* task_configuration_list program_configuration_list */
       
   229 /*
       
   230 SYM_REF2(single_resource_declaration_c, task_configuration_list, program_configuration_list)
       
   231 */
       
   232 
       
   233 /* helper symbol for single_resource_declaration */
       
   234 /*
       
   235 SYM_LIST(task_configuration_list_c)
       
   236 */
       
   237 
       
   238 /* helper symbol for single_resource_declaration */
       
   239 /*
       
   240 SYM_LIST(program_configuration_list_c)
       
   241 */
       
   242 
       
   243 /* helper symbol for
       
   244  *  - access_path
       
   245  *  - instance_specific_init
       
   246  */
       
   247 /*
       
   248 SYM_LIST(any_fb_name_list_c)
       
   249 */
       
   250 
       
   251 /*  [resource_name '.'] global_var_name ['.' structure_element_name] */
       
   252 /*
       
   253 SYM_REF4(global_var_reference_c, resource_name, global_var_name, structure_element_name, unused)
       
   254 */
       
   255 
       
   256 /*  prev_declared_program_name '.' symbolic_variable */
       
   257 /*
       
   258 SYM_REF2(program_output_reference_c, program_name, symbolic_variable)
       
   259 */
       
   260 
       
   261 /*  TASK task_name task_initialization */
       
   262 /*
       
   263 SYM_REF2(task_configuration_c, task_name, task_initialization)
       
   264 */
       
   265 
       
   266 /*  '(' [SINGLE ASSIGN data_source ','] [INTERVAL ASSIGN data_source ','] PRIORITY ASSIGN integer ')' */
       
   267 /*
       
   268 SYM_REF4(task_initialization_c, single_data_source, interval_data_source, priority_data_source, unused)
       
   269 */
       
   270 
       
   271 /*  PROGRAM [RETAIN | NON_RETAIN] program_name [WITH task_name] ':' program_type_name ['(' prog_conf_elements ')'] */
       
   272 // SYM_REF6(program_configuration_c, retain_option, program_name, task_name, program_type_name, prog_conf_elements, unused)
       
   273     void *visit(program_configuration_c *symbol) {
       
   274       TRACE("program_configuration_c");
       
   275       return symbol->prog_conf_elements->accept(*this);
       
   276     }
       
   277 
       
   278 /* prog_conf_elements ',' prog_conf_element */
       
   279 // SYM_LIST(prog_conf_elements_c)
       
   280     void *visit(prog_conf_elements_c *symbol) {
       
   281       TRACE("prog_conf_elements_c");
       
   282       return search_list(symbol);
       
   283     }
       
   284 
       
   285 /*  fb_name WITH task_name */
       
   286 /*
       
   287 SYM_REF2(fb_task_c, fb_name, task_name)
       
   288 */
       
   289 
       
   290 /*  any_symbolic_variable ASSIGN prog_data_source */
       
   291 // SYM_REF2(prog_cnxn_assign_c, symbolic_variable, prog_data_source)
       
   292     void *visit(prog_cnxn_assign_c *symbol) {
       
   293       TRACE("prog_cnxn_assign_c");
       
   294 
       
   295       /* NOTE: symbolic_variable may be something other than a symbolic_variable_c, but I (Mario)
       
   296        *       do not understand the semantics that should be implmeneted if it is not a
       
   297        *        symbolic_variable, so for the moment we simply give up!
       
   298        */
       
   299       symbolic_variable_c *symb_var = dynamic_cast<symbolic_variable_c *>(symbol->symbolic_variable);
       
   300       if (NULL == symb_var)
       
   301         ERROR;
       
   302 
       
   303       return handle_parameter_assignment(symb_var->var_name, symbol->prog_data_source);
       
   304     }
       
   305 
       
   306 /* any_symbolic_variable SENDTO data_sink */
       
   307 // SYM_REF2(prog_cnxn_sendto_c, symbolic_variable, prog_data_source)
       
   308     void *visit(prog_cnxn_sendto_c *symbol) {
       
   309       TRACE("prog_cnxn_sendto_c");
       
   310 
       
   311       /* NOTE: symbolic_variable may be something other than a symbolic_variable_c, but I (Mario)
       
   312        *       do not understand the semantics that should be implmeneted if it is not a
       
   313        *        symbolic_variable, so for the moment we simply give up!
       
   314        */
       
   315       symbolic_variable_c *symb_var = dynamic_cast<symbolic_variable_c *>(symbol->symbolic_variable);
       
   316       if (NULL == symb_var)
       
   317         ERROR;
       
   318 
       
   319       return handle_parameter_assignment(symb_var->var_name, symbol->data_sink);
       
   320     }
       
   321 
       
   322 /* VAR_CONFIG instance_specific_init_list END_VAR */
       
   323 /*
       
   324 SYM_REF2(instance_specific_initializations_c, instance_specific_init_list, unused)
       
   325 */
       
   326 
       
   327 /* helper symbol for instance_specific_initializations */
       
   328 /*
       
   329 SYM_LIST(instance_specific_init_list_c)
       
   330 */
       
   331 
       
   332 /* resource_name '.' program_name '.' {fb_name '.'}
       
   333     ((variable_name [location] ':' located_var_spec_init) | (fb_name ':' fb_initialization))
       
   334 */
       
   335 /*
       
   336 SYM_REF6(instance_specific_init_c, resource_name, program_name, any_fb_name_list, variable_name, location, initialization)
       
   337 */
       
   338 
       
   339 /* helper symbol for instance_specific_init */
       
   340 /* function_block_type_name ':=' structure_initialization */
       
   341 /*
       
   342 SYM_REF2(fb_initialization_c, function_block_type_name, structure_initialization)
       
   343 */
       
   344 
       
   345 
       
   346 
       
   347 
       
   348 
       
   349 
       
   350 /****************************************/
       
   351 /* B.2 - Language IL (Instruction List) */
       
   352 /****************************************/
       
   353 /***********************************/
       
   354 /* B 2.1 Instructions and Operands */
       
   355 /***********************************/
       
   356 
       
   357 /* | function_name [il_operand_list] */
       
   358 // SYM_REF2(il_function_call_c, function_name, il_operand_list)
       
   359     void *visit(il_function_call_c *symbol) {
       
   360       TRACE("il_function_call_c");
       
   361       if (NULL != symbol->il_operand_list)
       
   362         return symbol->il_operand_list->accept(*this);
       
   363       return NULL;
       
   364     }
       
   365 
       
   366 
       
   367 /* | function_name '(' eol_list [il_param_list] ')' */
       
   368 // SYM_REF2(il_formal_funct_call_c, function_name, il_param_list)
       
   369     void *visit(il_formal_funct_call_c *symbol) {
       
   370       TRACE("il_formal_funct_call_c");
       
   371       if (NULL != symbol->il_param_list)
       
   372         return symbol->il_param_list->accept(*this);
       
   373       return NULL;
       
   374     }
       
   375 
       
   376 
       
   377 /*   il_call_operator prev_declared_fb_name
       
   378  * | il_call_operator prev_declared_fb_name '(' ')'
       
   379  * | il_call_operator prev_declared_fb_name '(' eol_list ')'
       
   380  * | il_call_operator prev_declared_fb_name '(' il_operand_list ')'
       
   381  * | il_call_operator prev_declared_fb_name '(' eol_list il_param_list ')'
       
   382  */
       
   383 // SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list)
       
   384     void *visit(il_fb_call_c *symbol) {
       
   385       TRACE("il_fb_call_c");
       
   386       /* the following should never occur. In reality the syntax parser
       
   387        * will guarantee that they never occur, but it makes it easier to
       
   388        * understand the remaining code :-)
       
   389        */
       
   390       //if ((NULL == symbol->il_operand_list) && (NULL == symbol->il_param_list)) ERROR;
       
   391       //if ((NULL != symbol->il_operand_list) && (NULL != symbol->il_param_list)) ERROR;
       
   392 
       
   393       if (NULL != symbol->il_operand_list)
       
   394         return symbol->il_operand_list->accept(*this);
       
   395       if (NULL != symbol->il_param_list)
       
   396         return symbol->il_param_list->accept(*this);
       
   397       return NULL;
       
   398     }
       
   399 
       
   400 
       
   401 
       
   402 /* | il_operand_list ',' il_operand */
       
   403 // SYM_LIST(il_operand_list_c)
       
   404     void *visit(il_operand_list_c *symbol) {
       
   405       TRACE("il_operand_list_c");
       
   406       return search_list(symbol);
       
   407     }
       
   408 
       
   409 
       
   410 /* | il_initial_param_list il_param_instruction */
       
   411 // SYM_LIST(il_param_list_c)
       
   412     void *visit(il_param_list_c *symbol) {
       
   413       TRACE("il_param_list_c");
       
   414       return search_list(symbol);
       
   415     }
       
   416 
       
   417 /*  il_assign_operator il_operand
       
   418  * | il_assign_operator '(' eol_list simple_instr_list ')'
       
   419  */
       
   420 // SYM_REF4(il_param_assignment_c, il_assign_operator, il_operand, simple_instr_list, unused)
       
   421     void *visit(il_param_assignment_c *symbol) {
       
   422       TRACE("il_param_assignment_c");
       
   423 
       
   424       // TODO : We do not yet handle a instruction list passed as parameter !!!
       
   425       // since we do not yet support it, it is best to simply stop than to fail silently...
       
   426       if (NULL != symbol->simple_instr_list) ERROR;
       
   427 
       
   428       return handle_parameter_assignment(symbol->il_assign_operator, symbol->il_operand);
       
   429     }
       
   430 
       
   431 /*  il_assign_out_operator variable */
       
   432 // SYM_REF2(il_param_out_assignment_c, il_assign_out_operator, variable);
       
   433     void *visit(il_param_out_assignment_c *symbol) {
       
   434       TRACE("il_param_out_assignment_c");
       
   435       return handle_parameter_assignment((symbol_c *)symbol->il_assign_out_operator->accept(*this), symbol->variable);
       
   436     }
       
   437 
       
   438 
       
   439 /*******************/
       
   440 /* B 2.2 Operators */
       
   441 /*******************/
       
   442 /*| [NOT] any_identifier SENDTO */
       
   443 // SYM_REF2(il_assign_out_operator_c, option, variable_name)
       
   444     void *visit(il_assign_out_operator_c *symbol) {
       
   445       TRACE("il_assign_out_operator_c");
       
   446 
       
   447       // TODO : Handle not_param !!!
       
   448       // we do not yet support it, so it is best to simply stop than to fail silently...
       
   449       if (NULL != symbol->option) ERROR;
       
   450 
       
   451       return (void *)symbol->variable_name;
       
   452     }
       
   453 
       
   454 
       
   455 
       
   456 
       
   457 /***************************************/
       
   458 /* B.3 - Language ST (Structured Text) */
       
   459 /***************************************/
       
   460 /***********************/
       
   461 /* B 3.1 - Expressions */
       
   462 /***********************/
       
   463 
       
   464 /*
       
   465 SYM_REF2(function_invocation_c, function_name, parameter_assignment_list)
       
   466 */
       
   467     void *visit(function_invocation_c *symbol) {
       
   468       TRACE("function_invocation_c");
       
   469       if ((symbol_c *)symbol == f_call && symbol->parameter_assignment_list != NULL)
       
   470         return symbol->parameter_assignment_list->accept(*this);
       
   471       else
       
   472         return NULL;
       
   473     }
       
   474 
       
   475 
       
   476 
       
   477 
       
   478 /********************/
       
   479 /* B 3.2 Statements */
       
   480 /********************/
       
   481 
       
   482 /*********************************/
       
   483 /* B 3.2.1 Assignment Statements */
       
   484 /*********************************/
       
   485 /*
       
   486 SYM_REF2(assignment_statement_c, l_exp, r_exp)
       
   487 */
       
   488 
       
   489 /*****************************************/
       
   490 /* B 3.2.2 Subprogram Control Statements */
       
   491 /*****************************************/
       
   492 /*  RETURN */
       
   493 // SYM_REF0(return_statement_c)
       
   494 
       
   495 
       
   496 /* fb_name '(' [param_assignment_list] ')' */
       
   497 /* param_assignment_list -> may be NULL ! */
       
   498 // SYM_REF2(fb_invocation_c, fb_name, param_assignment_list)
       
   499     void *visit(fb_invocation_c *symbol) {
       
   500       TRACE("fb_invocation_c");
       
   501       if (symbol->param_assignment_list != NULL)
       
   502         return symbol->param_assignment_list->accept(*this);
       
   503       else
       
   504         return NULL;
       
   505     }
       
   506 
       
   507 /* helper symbol for fb_invocation */
       
   508 /* param_assignment_list ',' param_assignment */
       
   509 // SYM_LIST(param_assignment_list_c)
       
   510     void *visit(param_assignment_list_c *symbol) {
       
   511       TRACE("param_assignment_list_c");
       
   512       return search_list(symbol);
       
   513     }
       
   514 
       
   515 /*  variable_name ASSIGN expression */
       
   516 // SYM_REF2(input_variable_param_assignment_c, variable_name, expression)
       
   517     void *visit(input_variable_param_assignment_c *symbol) {
       
   518       TRACE("input_variable_param_assignment_c");
       
   519       return handle_parameter_assignment(symbol->variable_name, symbol->expression);
       
   520     }
       
   521 
       
   522 /* [NOT] variable_name '=>' variable */
       
   523 // SYM_REF4(output_variable_param_assignment_c, not_param, variable_name, variable, unused)
       
   524     void *visit(output_variable_param_assignment_c *symbol) {
       
   525       TRACE("output_variable_param_assignment_c");
       
   526       // TODO : Handle not_param !!!
       
   527       if (NULL != symbol->not_param) ERROR;  // we do not yet support it, so it is best to simply stop than to fail silently...
       
   528 
       
   529       return handle_parameter_assignment(symbol->variable_name, symbol->variable);
       
   530     }
       
   531 
       
   532 /* helper CLASS for output_variable_param_assignment */
       
   533 // SYM_REF0(not_paramassign_c)
       
   534 // TODO... ???
       
   535 
       
   536 
       
   537 
       
   538 
       
   539 
       
   540 };
       
   541 
       
   542 
       
   543 
       
   544 
       
   545 
       
   546