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