|
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 * |
|
29 * This is part of the 4th stage that generates |
|
30 * a c++ source program equivalent to the IL and ST |
|
31 * code. |
|
32 */ |
|
33 |
|
34 /* given a function_body_c, iterate through each |
|
35 * function in/out/inout parameter, returning the name |
|
36 * of each parameter... |
|
37 */ |
|
38 |
|
39 |
|
40 #include "function_call_iterator.hh" |
|
41 |
|
42 |
|
43 //#define DEBUG |
|
44 #ifdef DEBUG |
|
45 #define TRACE(classname) printf("\n____%s____\n",classname); |
|
46 #else |
|
47 #define TRACE(classname) |
|
48 #endif |
|
49 |
|
50 #define ERROR error_exit(__FILE__,__LINE__) |
|
51 /* function defined in main.cc */ |
|
52 extern void error_exit(const char *file_name, int line_no); |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 /* initialise the iterator object. |
|
59 * We must be given a reference to the function declaration |
|
60 * that will be analysed... |
|
61 */ |
|
62 function_call_iterator_c::function_call_iterator_c(symbol_c *symbol) { |
|
63 this->start_symbol = symbol; |
|
64 next_fcall = fcall_count = 0; |
|
65 current_finvocation = NULL; |
|
66 current_fcall_name = NULL; |
|
67 } |
|
68 |
|
69 /* Skip to the next function call. After object creation, |
|
70 * the object references _before_ the first, so |
|
71 * this function must be called once to get the object to |
|
72 * reference the first function call... |
|
73 * |
|
74 * Returns the function_invocation_c! |
|
75 */ |
|
76 //function_invocation_c *next(void) {TRACE("function_call_iterator_c::next(): called "); |
|
77 symbol_c *function_call_iterator_c::next(void) {TRACE("function_call_iterator_c::next(): called "); |
|
78 fcall_count = 0; |
|
79 next_fcall++; |
|
80 current_finvocation = NULL; |
|
81 current_fcall_name = NULL; |
|
82 |
|
83 start_symbol->accept(*this); |
|
84 return current_finvocation; |
|
85 } |
|
86 |
|
87 /* Returns the name of the currently referenced function invocation */ |
|
88 identifier_c *function_call_iterator_c::fname(void) { |
|
89 identifier_c *identifier = dynamic_cast<identifier_c *>(current_fcall_name); |
|
90 if (identifier == NULL) ERROR; |
|
91 return identifier; |
|
92 } |
|
93 |
|
94 |
|
95 /***************************************/ |
|
96 /* B.3 - Language ST (Structured Text) */ |
|
97 /***************************************/ |
|
98 /***********************/ |
|
99 /* B 3.1 - Expressions */ |
|
100 /***********************/ |
|
101 void *function_call_iterator_c::visit(function_invocation_c *symbol) { |
|
102 fcall_count++; |
|
103 if (next_fcall == fcall_count) { |
|
104 current_finvocation = symbol; |
|
105 current_fcall_name = symbol->function_name; |
|
106 } |
|
107 return NULL; |
|
108 } |
|
109 |
|
110 |
|
111 |
|
112 /****************************************/ |
|
113 /* B.2 - Language IL (Instruction List) */ |
|
114 /****************************************/ |
|
115 /***********************************/ |
|
116 /* B 2.1 Instructions and Operands */ |
|
117 /***********************************/ |
|
118 |
|
119 /* | function_name [il_operand_list] */ |
|
120 // SYM_REF2(il_function_call_c, function_name, il_operand_list) |
|
121 void *function_call_iterator_c::visit(il_function_call_c *symbol) { |
|
122 fcall_count++; |
|
123 if (next_fcall == fcall_count) { |
|
124 current_finvocation = symbol; |
|
125 current_fcall_name = symbol->function_name; |
|
126 } |
|
127 return NULL; |
|
128 } |
|
129 |
|
130 |
|
131 |
|
132 /* | function_name '(' eol_list [il_param_list] ')' */ |
|
133 // SYM_REF2(il_formal_funct_call_c, function_name, il_param_list) |
|
134 void *function_call_iterator_c::visit(il_formal_funct_call_c *symbol) { |
|
135 fcall_count++; |
|
136 if (next_fcall == fcall_count) { |
|
137 current_finvocation = symbol; |
|
138 current_fcall_name = symbol->function_name; |
|
139 } |
|
140 return NULL; |
|
141 } |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |