|
1 /* |
|
2 * matiec - a compiler for the programming languages defined in IEC 61131-3 |
|
3 * |
|
4 * Copyright (C) 2009-2012 Mario de Sousa (msousa@fe.up.pt) |
|
5 * Copyright (C) 2012 Manuele Conti (manuele.conti@sirius-es.it) |
|
6 * Copyright (C) 2012 Matteo Facchinetti (matteo.facchinetti@sirius-es.it) |
|
7 * |
|
8 * This program is free software: you can redistribute it and/or modify |
|
9 * it under the terms of the GNU General Public License as published by |
|
10 * the Free Software Foundation, either version 3 of the License, or |
|
11 * (at your option) any later version. |
|
12 * |
|
13 * This program is distributed in the hope that it will be useful, |
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16 * GNU General Public License for more details. |
|
17 * |
|
18 * You should have received a copy of the GNU General Public License |
|
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
20 * |
|
21 * |
|
22 * This code is made available on the understanding that it will not be |
|
23 * used in safety-critical situations without a full and competent review. |
|
24 */ |
|
25 |
|
26 /* |
|
27 * An IEC 61131-3 compiler. |
|
28 * |
|
29 * Based on the |
|
30 * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) |
|
31 * |
|
32 */ |
|
33 |
|
34 |
|
35 /* NOTE: The algorithm implemented here assumes that candidate datatype lists have already been filled! |
|
36 * BEFORE running this visitor, be sure to CALL the fill_candidate_datatype_c visitor! |
|
37 */ |
|
38 |
|
39 |
|
40 /* |
|
41 * Choose, from the list of all the possible datatypes each expression may take, the single datatype that it will in fact take. |
|
42 * The resulting (chosen) datatype, will be stored in the symbol_c.datatype variable, leaving the candidate datatype list untouched! |
|
43 * |
|
44 * For rvalue expressions, this decision will be based on the datatype of the lvalue expression. |
|
45 * For lvalue expressions, the candidate datatype list should have a single entry. |
|
46 * |
|
47 * For example, the very simple literal '0' in 'foo := 0', may represent a: |
|
48 * BOOL, BYTE, WORD, DWORD, LWORD, USINT, SINT, UINT, INT, UDINT, DINT, ULINT, LINT (as well as the SAFE versions of these data tyes too!) |
|
49 * |
|
50 * In this class, the datatype of '0' will be set to the same datatype as the 'foo' variable. |
|
51 * If the intersection of the candidate datatype lists of the left and right side expressions is empty, |
|
52 * then a datatype error has been found, and the datatype is either left at NULL, or set to a pointer of an invalid_type_name_c object! |
|
53 */ |
|
54 |
|
55 |
|
56 #include "narrow_candidate_datatypes.hh" |
|
57 #include "datatype_functions.hh" |
|
58 #include <typeinfo> |
|
59 #include <list> |
|
60 #include <string> |
|
61 #include <string.h> |
|
62 #include <strings.h> |
|
63 |
|
64 |
|
65 /* set to 1 to see debug info during execution */ |
|
66 static int debug = 0; |
|
67 |
|
68 narrow_candidate_datatypes_c::narrow_candidate_datatypes_c(symbol_c *ignore) { |
|
69 } |
|
70 |
|
71 narrow_candidate_datatypes_c::~narrow_candidate_datatypes_c(void) { |
|
72 } |
|
73 |
|
74 |
|
75 /* Only set the symbol's desired datatype to 'datatype' if that datatype is in the candidate_datatype list */ |
|
76 static void set_datatype(symbol_c *datatype, symbol_c *symbol) { |
|
77 |
|
78 /* If we are trying to set to the undefined type, and the symbol's datatype has already been set to something else, |
|
79 * we abort the compoiler as I don't think this should ever occur. |
|
80 * NOTE: In order to handle JMPs to labels that come before the JMP itself, we run the narrow algorithm twice. |
|
81 * This means that this situation may legally occur, so we cannot abort the compiler here! |
|
82 */ |
|
83 // if ((NULL == datatype) && (NULL != symbol->datatype)) ERROR; |
|
84 if ((NULL == datatype) && (NULL != symbol->datatype)) return; |
|
85 if ((NULL == datatype) && (NULL == symbol->datatype)) return; |
|
86 |
|
87 if (search_in_candidate_datatype_list(datatype, symbol->candidate_datatypes) < 0) |
|
88 symbol->datatype = &(search_constant_type_c::invalid_type_name); |
|
89 else { |
|
90 if (NULL == symbol->datatype) |
|
91 /* not yet set to anything, so we set it to the requested data type */ |
|
92 symbol->datatype = datatype; |
|
93 else { |
|
94 /* had already been set previously to some data type. Let's check if they are the same! */ |
|
95 if (!is_type_equal(symbol->datatype, datatype)) |
|
96 symbol->datatype = &(search_constant_type_c::invalid_type_name); |
|
97 // else |
|
98 /* we leave it unchanged, as it is the same as the requested data type! */ |
|
99 } |
|
100 } |
|
101 } |
|
102 |
|
103 |
|
104 |
|
105 /* Only set the symbol's desired datatype to 'datatype' if that datatype is in the candidate_datatype list */ |
|
106 // static void set_datatype_in_prev_il_instructions(symbol_c *datatype, std::vector <symbol_c *> prev_il_instructions) { |
|
107 static void set_datatype_in_prev_il_instructions(symbol_c *datatype, il_instruction_c *symbol) { |
|
108 if (NULL == symbol) ERROR; |
|
109 for (unsigned int i = 0; i < symbol->prev_il_instruction.size(); i++) |
|
110 set_datatype(datatype, symbol->prev_il_instruction[i]); |
|
111 } |
|
112 |
|
113 |
|
114 |
|
115 bool narrow_candidate_datatypes_c::is_widening_compatible(const struct widen_entry widen_table[], symbol_c *left_type, symbol_c *right_type, symbol_c *result_type, bool *deprecated_status) { |
|
116 /* NOTE: According to our algorithm, left_type and right_type should never by NULL (if they are, we have an internal compiler error! |
|
117 * However, result_type may be NULL if the code has a data type semantic error! |
|
118 */ |
|
119 if ((NULL == left_type) || (NULL == right_type) || (NULL == result_type)) |
|
120 return false; |
|
121 |
|
122 for (int k = 0; NULL != widen_table[k].left; k++) { |
|
123 if ((typeid(*left_type) == typeid(*widen_table[k].left)) |
|
124 && (typeid(*right_type) == typeid(*widen_table[k].right)) |
|
125 && (typeid(*result_type) == typeid(*widen_table[k].result))) { |
|
126 if (NULL != deprecated_status) |
|
127 *deprecated_status = (widen_table[k].status == widen_entry::deprecated); |
|
128 return true; |
|
129 } |
|
130 } |
|
131 return false; |
|
132 } |
|
133 |
|
134 /* |
|
135 * All parameters being passed to the called function MUST be in the parameter list to which f_call points to! |
|
136 * This means that, for non formal function calls in IL, de current (default value) must be artificially added to the |
|
137 * beginning of the parameter list BEFORE calling handle_function_call(). |
|
138 */ |
|
139 void narrow_candidate_datatypes_c::narrow_nonformal_call(symbol_c *f_call, symbol_c *f_decl, int *ext_parm_count) { |
|
140 symbol_c *call_param_value, *param_type; |
|
141 identifier_c *param_name; |
|
142 function_param_iterator_c fp_iterator(f_decl); |
|
143 function_call_param_iterator_c fcp_iterator(f_call); |
|
144 int extensible_parameter_highest_index = -1; |
|
145 unsigned int i; |
|
146 |
|
147 if (NULL != ext_parm_count) *ext_parm_count = -1; |
|
148 |
|
149 /* Iterating through the non-formal parameters of the function call */ |
|
150 while((call_param_value = fcp_iterator.next_nf()) != NULL) { |
|
151 /* Obtaining the type of the value being passed in the function call */ |
|
152 /* Iterate to the next parameter of the function being called. |
|
153 * Get the name of that parameter, and ignore if EN or ENO. |
|
154 */ |
|
155 do { |
|
156 param_name = fp_iterator.next(); |
|
157 /* If there is no other parameter declared, then we are passing too many parameters... */ |
|
158 /* This error should have been caught in fill_candidate_datatypes_c, but may occur here again when we handle FB invocations! |
|
159 * In this case, we carry on analysing the code in order to be able to provide relevant error messages |
|
160 * for that code too! |
|
161 */ |
|
162 if(param_name == NULL) break; |
|
163 } while ((strcmp(param_name->value, "EN") == 0) || (strcmp(param_name->value, "ENO") == 0)); |
|
164 |
|
165 /* Set the desired datatype for this parameter, and call it recursively. */ |
|
166 /* Note that if the call has more parameters than those declared in the function/FB declaration, |
|
167 * we may be setting this to NULL! |
|
168 */ |
|
169 symbol_c *desired_datatype = base_type(fp_iterator.param_type()); |
|
170 if ((NULL != param_name) && (NULL == desired_datatype)) ERROR; |
|
171 if ((NULL == param_name) && (NULL != desired_datatype)) ERROR; |
|
172 |
|
173 /* NOTE: When we are handling a nonformal function call made from IL, the first parameter is the 'default' or 'current' |
|
174 * il value. However, a pointer to a copy of the prev_il_instruction is pre-pended into the operand list, so |
|
175 * the call |
|
176 * call_param_value->accept(*this); |
|
177 * may actually be calling an object of the base symbol_c . |
|
178 */ |
|
179 set_datatype(desired_datatype, call_param_value); |
|
180 call_param_value->accept(*this); |
|
181 |
|
182 if (NULL != param_name) |
|
183 if (extensible_parameter_highest_index < fp_iterator.extensible_param_index()) |
|
184 extensible_parameter_highest_index = fp_iterator.extensible_param_index(); |
|
185 } |
|
186 /* In the case of a call to an extensible function, we store the highest index |
|
187 * of the extensible parameters this particular call uses, in the symbol_c object |
|
188 * of the function call itself! |
|
189 * In calls to non-extensible functions, this value will be set to -1. |
|
190 * This information is later used in stage4 to correctly generate the |
|
191 * output code. |
|
192 */ |
|
193 if ((NULL != ext_parm_count) && (extensible_parameter_highest_index >=0) /* if call to extensible function */) |
|
194 *ext_parm_count = 1 + extensible_parameter_highest_index - fp_iterator.first_extensible_param_index(); |
|
195 } |
|
196 |
|
197 |
|
198 |
|
199 void narrow_candidate_datatypes_c::narrow_formal_call(symbol_c *f_call, symbol_c *f_decl, int *ext_parm_count) { |
|
200 symbol_c *call_param_value, *call_param_name, *param_type; |
|
201 symbol_c *verify_duplicate_param; |
|
202 identifier_c *param_name; |
|
203 function_param_iterator_c fp_iterator(f_decl); |
|
204 function_call_param_iterator_c fcp_iterator(f_call); |
|
205 int extensible_parameter_highest_index = -1; |
|
206 identifier_c *extensible_parameter_name; |
|
207 unsigned int i; |
|
208 |
|
209 if (NULL != ext_parm_count) *ext_parm_count = -1; |
|
210 /* Iterating through the formal parameters of the function call */ |
|
211 while((call_param_name = fcp_iterator.next_f()) != NULL) { |
|
212 |
|
213 /* Obtaining the value being passed in the function call */ |
|
214 call_param_value = fcp_iterator.get_current_value(); |
|
215 /* the following should never occur. If it does, then we have a bug in our code... */ |
|
216 if (NULL == call_param_value) ERROR; |
|
217 |
|
218 /* Find the corresponding parameter in function declaration */ |
|
219 param_name = fp_iterator.search(call_param_name); |
|
220 |
|
221 /* Set the desired datatype for this parameter, and call it recursively. */ |
|
222 /* NOTE: When handling a FB call, this narrow_formal_call() may be called to analyse |
|
223 * an invalid FB call (call with parameters that do not exist on the FB declaration). |
|
224 * For this reason, the param_name may come out as NULL! |
|
225 */ |
|
226 symbol_c *desired_datatype = base_type(fp_iterator.param_type()); |
|
227 if ((NULL != param_name) && (NULL == desired_datatype)) ERROR; |
|
228 if ((NULL == param_name) && (NULL != desired_datatype)) ERROR; |
|
229 |
|
230 /* set the desired data type for this parameter */ |
|
231 set_datatype(desired_datatype, call_param_value); |
|
232 /* And recursively call that parameter/expression, so it can propagate that info */ |
|
233 call_param_value->accept(*this); |
|
234 |
|
235 /* set the extensible_parameter_highest_index, which will be needed in stage 4 */ |
|
236 /* This value says how many extensible parameters are being passed to the standard function */ |
|
237 if (NULL != param_name) |
|
238 if (extensible_parameter_highest_index < fp_iterator.extensible_param_index()) |
|
239 extensible_parameter_highest_index = fp_iterator.extensible_param_index(); |
|
240 } |
|
241 /* call is compatible! */ |
|
242 |
|
243 /* In the case of a call to an extensible function, we store the highest index |
|
244 * of the extensible parameters this particular call uses, in the symbol_c object |
|
245 * of the function call itself! |
|
246 * In calls to non-extensible functions, this value will be set to -1. |
|
247 * This information is later used in stage4 to correctly generate the |
|
248 * output code. |
|
249 */ |
|
250 if ((NULL != ext_parm_count) && (extensible_parameter_highest_index >=0) /* if call to extensible function */) |
|
251 *ext_parm_count = 1 + extensible_parameter_highest_index - fp_iterator.first_extensible_param_index(); |
|
252 } |
|
253 |
|
254 |
|
255 /* |
|
256 typedef struct { |
|
257 symbol_c *function_name, |
|
258 symbol_c *nonformal_operand_list, |
|
259 symbol_c * formal_operand_list, |
|
260 |
|
261 std::vector <symbol_c *> &candidate_functions, |
|
262 symbol_c &*called_function_declaration, |
|
263 int &extensible_param_count |
|
264 } generic_function_call_t; |
|
265 */ |
|
266 void narrow_candidate_datatypes_c::narrow_function_invocation(symbol_c *fcall, generic_function_call_t fcall_data) { |
|
267 /* set the called_function_declaration. */ |
|
268 fcall_data.called_function_declaration = NULL; |
|
269 |
|
270 /* set the called_function_declaration taking into account the datatype that we need to return */ |
|
271 for(unsigned int i = 0; i < fcall->candidate_datatypes.size(); i++) { |
|
272 if (is_type_equal(fcall->candidate_datatypes[i], fcall->datatype)) { |
|
273 fcall_data.called_function_declaration = fcall_data.candidate_functions[i]; |
|
274 break; |
|
275 } |
|
276 } |
|
277 |
|
278 /* NOTE: If we can't figure out the declaration of the function being called, this is not |
|
279 * necessarily an internal compiler error. It could be because the symbol->datatype is NULL |
|
280 * (because the ST code being analysed has an error _before_ this function invocation). |
|
281 * However, we don't just give, up, we carry on recursivly analysing the code, so as to be |
|
282 * able to print out any error messages related to the parameters being passed in this function |
|
283 * invocation. |
|
284 */ |
|
285 /* if (NULL == symbol->called_function_declaration) ERROR; */ |
|
286 if (fcall->candidate_datatypes.size() == 1) { |
|
287 /* If only one function declaration, then we use that (even if symbol->datatypes == NULL) |
|
288 * so we can check for errors in the expressions used to pass parameters in this |
|
289 * function invocation. |
|
290 */ |
|
291 fcall_data.called_function_declaration = fcall_data.candidate_functions[0]; |
|
292 } |
|
293 |
|
294 /* If an overloaded function is being invoked, and we cannot determine which version to use, |
|
295 * then we can not meaningfully verify the expressions used inside that function invocation. |
|
296 * We simply give up! |
|
297 */ |
|
298 if (NULL == fcall_data.called_function_declaration) |
|
299 return; |
|
300 |
|
301 if (NULL != fcall_data.nonformal_operand_list) narrow_nonformal_call(fcall, fcall_data.called_function_declaration, &(fcall_data.extensible_param_count)); |
|
302 if (NULL != fcall_data. formal_operand_list) narrow_formal_call(fcall, fcall_data.called_function_declaration, &(fcall_data.extensible_param_count)); |
|
303 |
|
304 return; |
|
305 } |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 /* narrow implicit FB call in IL. |
|
311 * e.g. CLK ton_var |
|
312 * CU counter_var |
|
313 * |
|
314 * The algorithm will be to build a fake il_fb_call_c equivalent to the implicit IL FB call, and let |
|
315 * the visit(il_fb_call_c *) method handle it! |
|
316 */ |
|
317 void *narrow_candidate_datatypes_c::narrow_implicit_il_fb_call(symbol_c *il_instruction, const char *param_name, symbol_c *&called_fb_declaration) { |
|
318 |
|
319 /* set the datatype of the il_operand, this is, the FB being called! */ |
|
320 if (NULL != il_operand) { |
|
321 /* only set it if it is in the candidate datatypes list! */ |
|
322 set_datatype(called_fb_declaration, il_operand); |
|
323 il_operand->accept(*this); |
|
324 } |
|
325 symbol_c *fb_decl = il_operand->datatype; |
|
326 |
|
327 if (0 == fake_prev_il_instruction->prev_il_instruction.size()) { |
|
328 /* This IL implicit FB call (e.g. CLK ton_var) is not preceded by another IL instruction |
|
329 * (or list of instructions) that will set the IL current/default value. |
|
330 * We cannot proceed verifying type compatibility of something that does not exist. |
|
331 */ |
|
332 return NULL; |
|
333 } |
|
334 |
|
335 if (NULL == fb_decl) { |
|
336 /* the il_operand is a not FB instance */ |
|
337 /* so we simply pass on the required datatype to the prev_il_instructions */ |
|
338 /* The invalid FB invocation will be caught in the print_datatypes_error_c by analysing NULL value in il_operand->datatype! */ |
|
339 set_datatype_in_prev_il_instructions(il_instruction->datatype, fake_prev_il_instruction); |
|
340 return NULL; |
|
341 } |
|
342 |
|
343 |
|
344 /* The value being passed to the 'param_name' parameter is actually the prev_il_instruction. |
|
345 * However, we do not place that object directly in the fake il_param_list_c that we will be |
|
346 * creating, since the visit(il_fb_call_c *) method will recursively call every object in that list. |
|
347 * The il_prev_intruction object will be visited once we have handled this implici IL FB call |
|
348 * (called from the instruction_list_c for() loop that works backwards). We DO NOT want to visit it twice. |
|
349 * (Anyway, if we let the visit(il_fb_call_c *) recursively visit the current prev_il_instruction, this pointer |
|
350 * would be changed to the IL instruction coming before the current prev_il_instruction! => things would get all messed up!) |
|
351 * The easiest way to work around this is to simply use a new object, and copy the relevant details to that object! |
|
352 */ |
|
353 symbol_c param_value = *fake_prev_il_instruction; /* copy the candidate_datatypes list ! */ |
|
354 |
|
355 identifier_c variable_name(param_name); |
|
356 // SYM_REF1(il_assign_operator_c, variable_name) |
|
357 il_assign_operator_c il_assign_operator(&variable_name); |
|
358 // SYM_REF3(il_param_assignment_c, il_assign_operator, il_operand, simple_instr_list) |
|
359 il_param_assignment_c il_param_assignment(&il_assign_operator, ¶m_value/*il_operand*/, NULL); |
|
360 il_param_list_c il_param_list; |
|
361 il_param_list.add_element(&il_param_assignment); |
|
362 // SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list, symbol_c *called_fb_declaration) |
|
363 CAL_operator_c CAL_operator; |
|
364 il_fb_call_c il_fb_call(&CAL_operator, il_operand, NULL, &il_param_list); |
|
365 |
|
366 /* A FB call does not return any datatype, but the IL instructions that come after this |
|
367 * FB call may require a specific datatype in the il current/default variable, |
|
368 * so we must pass this information up to the IL instruction before the FB call, since it will |
|
369 * be that IL instruction that will be required to produce the desired dtataype. |
|
370 * |
|
371 * The above will be done by the visit(il_fb_call_c *) method, so we must make sure to |
|
372 * correctly set up the il_fb_call.datatype variable! |
|
373 */ |
|
374 il_fb_call.called_fb_declaration = called_fb_declaration; |
|
375 il_fb_call.accept(*this); |
|
376 |
|
377 /* set the required datatype of the previous IL instruction! */ |
|
378 /* NOTE: |
|
379 * When handling these implicit IL calls, the parameter_value being passed to the FB parameter |
|
380 * is actually the prev_il_instruction. |
|
381 * |
|
382 * However, since the FB call does not change the value in the current/default IL variable, this value |
|
383 * must also be used ny the IL instruction coming after this FB call. |
|
384 * |
|
385 * This means that we have two consumers/users for the same value. |
|
386 * We must therefore check whether the datatype required by the IL instructions following this FB call |
|
387 * is the same as that required for the first parameter. If not, then we have a semantic error, |
|
388 * and we set it to invalid_type_name. |
|
389 * |
|
390 * However, we only do that if: |
|
391 * - The IL instruction that comes after this IL FB call actually asked this FB call for a specific |
|
392 * datatype in the current/default vairable, once this IL FB call returns. |
|
393 * However, sometimes, (for e.g., this FB call is the last in the IL list) the subsequent FB to not aks this |
|
394 * FB call for any datatype. In that case, then the datatype required to pass to the first parameter of the |
|
395 * FB call must be left unchanged! |
|
396 */ |
|
397 if ((NULL == il_instruction->datatype) || (is_type_equal(param_value.datatype, il_instruction->datatype))) { |
|
398 set_datatype_in_prev_il_instructions(param_value.datatype, fake_prev_il_instruction); |
|
399 } else { |
|
400 set_datatype_in_prev_il_instructions(&search_constant_type_c::invalid_type_name, fake_prev_il_instruction); |
|
401 } |
|
402 return NULL; |
|
403 } |
|
404 |
|
405 |
|
406 /* a helper function... */ |
|
407 symbol_c *narrow_candidate_datatypes_c::base_type(symbol_c *symbol) { |
|
408 /* NOTE: symbol == NULL is valid. It will occur when, for e.g., an undefined/undeclared symbolic_variable is used |
|
409 * in the code. |
|
410 */ |
|
411 if (symbol == NULL) return NULL; |
|
412 return (symbol_c *)symbol->accept(search_base_type); |
|
413 } |
|
414 |
|
415 /*********************/ |
|
416 /* B 1.2 - Constants */ |
|
417 /*********************/ |
|
418 |
|
419 /**********************/ |
|
420 /* B 1.3 - Data types */ |
|
421 /**********************/ |
|
422 /********************************/ |
|
423 /* B 1.3.3 - Derived data types */ |
|
424 /********************************/ |
|
425 /* simple_specification ASSIGN constant */ |
|
426 // SYM_REF2(simple_spec_init_c, simple_specification, constant) |
|
427 void *narrow_candidate_datatypes_c::visit(simple_spec_init_c *symbol) { |
|
428 if (symbol->candidate_datatypes.size() == 1) |
|
429 symbol->datatype = symbol->candidate_datatypes[0]; |
|
430 |
|
431 if (symbol->simple_specification->candidate_datatypes.size() == 1) |
|
432 symbol->simple_specification->datatype = symbol->simple_specification->candidate_datatypes[0]; |
|
433 |
|
434 if (NULL != symbol->constant) { |
|
435 set_datatype(symbol->datatype, symbol->constant); |
|
436 symbol->constant->accept(*this); |
|
437 } |
|
438 return NULL; |
|
439 } |
|
440 |
|
441 |
|
442 |
|
443 /* signed_integer DOTDOT signed_integer */ |
|
444 // SYM_REF2(subrange_c, lower_limit, upper_limit) |
|
445 void *narrow_candidate_datatypes_c::visit(subrange_c *symbol) { |
|
446 symbol->lower_limit->datatype = symbol->datatype; |
|
447 symbol->lower_limit->accept(*this); |
|
448 symbol->upper_limit->datatype = symbol->datatype; |
|
449 symbol->upper_limit->accept(*this); |
|
450 return NULL; |
|
451 } |
|
452 |
|
453 |
|
454 /*********************/ |
|
455 /* B 1.4 - Variables */ |
|
456 /*********************/ |
|
457 |
|
458 /********************************************/ |
|
459 /* B 1.4.1 - Directly Represented Variables */ |
|
460 /********************************************/ |
|
461 |
|
462 /*************************************/ |
|
463 /* B 1.4.2 - Multi-element variables */ |
|
464 /*************************************/ |
|
465 /* subscripted_variable '[' subscript_list ']' */ |
|
466 // SYM_REF2(array_variable_c, subscripted_variable, subscript_list) |
|
467 void *narrow_candidate_datatypes_c::visit(array_variable_c *symbol) { |
|
468 /* we need to check the data types of the expressions used for the subscripts... */ |
|
469 symbol->subscript_list->accept(*this); |
|
470 return NULL; |
|
471 } |
|
472 |
|
473 |
|
474 /* subscript_list ',' subscript */ |
|
475 // SYM_LIST(subscript_list_c) |
|
476 void *narrow_candidate_datatypes_c::visit(subscript_list_c *symbol) { |
|
477 for (int i = 0; i < symbol->n; i++) { |
|
478 for (unsigned int k = 0; k < symbol->elements[i]->candidate_datatypes.size(); k++) { |
|
479 if (is_ANY_INT_type(symbol->elements[i]->candidate_datatypes[k])) |
|
480 symbol->elements[i]->datatype = symbol->elements[i]->candidate_datatypes[k]; |
|
481 } |
|
482 symbol->elements[i]->accept(*this); |
|
483 } |
|
484 return NULL; |
|
485 } |
|
486 |
|
487 |
|
488 |
|
489 |
|
490 /******************************************/ |
|
491 /* B 1.4.3 - Declaration & Initialisation */ |
|
492 /******************************************/ |
|
493 |
|
494 void *narrow_candidate_datatypes_c::visit(var1_list_c *symbol) { |
|
495 #if 0 /* We don't really need to set the datatype of each variable. We just check the declaration itself! */ |
|
496 for(int i = 0; i < symbol->n; i++) { |
|
497 if (symbol->elements[i]->candidate_datatypes.size() == 1) |
|
498 symbol->elements[i]->datatype = symbol->elements[i]->candidate_datatypes[0]; |
|
499 } |
|
500 #endif |
|
501 return NULL; |
|
502 } |
|
503 |
|
504 |
|
505 /* AT direct_variable */ |
|
506 // SYM_REF1(location_c, direct_variable) |
|
507 void *narrow_candidate_datatypes_c::visit(location_c *symbol) { |
|
508 set_datatype(symbol->datatype, symbol->direct_variable); |
|
509 symbol->direct_variable->accept(*this); /* currently does nothing! */ |
|
510 return NULL; |
|
511 } |
|
512 |
|
513 |
|
514 /* [variable_name] location ':' located_var_spec_init */ |
|
515 /* variable_name -> may be NULL ! */ |
|
516 // SYM_REF3(located_var_decl_c, variable_name, location, located_var_spec_init) |
|
517 void *narrow_candidate_datatypes_c::visit(located_var_decl_c *symbol) { |
|
518 /* let the var_spec_init set its own symbol->datatype value */ |
|
519 symbol->located_var_spec_init->accept(*this); |
|
520 |
|
521 if (NULL != symbol->variable_name) |
|
522 set_datatype(symbol->located_var_spec_init->datatype, symbol->variable_name); |
|
523 |
|
524 set_datatype(symbol->located_var_spec_init->datatype, symbol->location); |
|
525 symbol->location->accept(*this); |
|
526 return NULL; |
|
527 } |
|
528 |
|
529 |
|
530 /************************************/ |
|
531 /* B 1.5 Program organization units */ |
|
532 /************************************/ |
|
533 /*********************/ |
|
534 /* B 1.5.1 Functions */ |
|
535 /*********************/ |
|
536 void *narrow_candidate_datatypes_c::visit(function_declaration_c *symbol) { |
|
537 search_varfb_instance_type = new search_varfb_instance_type_c(symbol); |
|
538 symbol->var_declarations_list->accept(*this); |
|
539 if (debug) printf("Narrowing candidate data types list in body of function %s\n", ((token_c *)(symbol->derived_function_name))->value); |
|
540 symbol->function_body->accept(*this); |
|
541 delete search_varfb_instance_type; |
|
542 search_varfb_instance_type = NULL; |
|
543 return NULL; |
|
544 } |
|
545 |
|
546 /***************************/ |
|
547 /* B 1.5.2 Function blocks */ |
|
548 /***************************/ |
|
549 void *narrow_candidate_datatypes_c::visit(function_block_declaration_c *symbol) { |
|
550 search_varfb_instance_type = new search_varfb_instance_type_c(symbol); |
|
551 symbol->var_declarations->accept(*this); |
|
552 if (debug) printf("Narrowing candidate data types list in body of FB %s\n", ((token_c *)(symbol->fblock_name))->value); |
|
553 symbol->fblock_body->accept(*this); |
|
554 delete search_varfb_instance_type; |
|
555 search_varfb_instance_type = NULL; |
|
556 return NULL; |
|
557 } |
|
558 |
|
559 /********************/ |
|
560 /* B 1.5.3 Programs */ |
|
561 /********************/ |
|
562 void *narrow_candidate_datatypes_c::visit(program_declaration_c *symbol) { |
|
563 search_varfb_instance_type = new search_varfb_instance_type_c(symbol); |
|
564 symbol->var_declarations->accept(*this); |
|
565 if (debug) printf("Narrowing candidate data types list in body of program %s\n", ((token_c *)(symbol->program_type_name))->value); |
|
566 symbol->function_block_body->accept(*this); |
|
567 delete search_varfb_instance_type; |
|
568 search_varfb_instance_type = NULL; |
|
569 return NULL; |
|
570 } |
|
571 |
|
572 |
|
573 /********************************/ |
|
574 /* B 1.7 Configuration elements */ |
|
575 /********************************/ |
|
576 void *narrow_candidate_datatypes_c::visit(configuration_declaration_c *symbol) { |
|
577 // TODO !!! |
|
578 /* for the moment we must return NULL so semantic analysis of remaining code is not interrupted! */ |
|
579 return NULL; |
|
580 } |
|
581 |
|
582 |
|
583 /****************************************/ |
|
584 /* B.2 - Language IL (Instruction List) */ |
|
585 /****************************************/ |
|
586 /***********************************/ |
|
587 /* B 2.1 Instructions and Operands */ |
|
588 /***********************************/ |
|
589 |
|
590 /*| instruction_list il_instruction */ |
|
591 // SYM_LIST(instruction_list_c) |
|
592 void *narrow_candidate_datatypes_c::visit(instruction_list_c *symbol) { |
|
593 /* In order to execute the narrow algoritm correctly, we need to go through the instructions backwards, |
|
594 * so we can not use the base class' visitor |
|
595 */ |
|
596 /* In order to execute the narrow algoritm correctly |
|
597 * in IL instruction lists containing JMPs to labels that come before the JMP instruction |
|
598 * itself, we need to run the narrow algorithm twice on the Instruction List. |
|
599 * e.g.: ... |
|
600 * ld 23 |
|
601 * label1:st byte_var |
|
602 * ld 34 |
|
603 * JMP label1 |
|
604 * |
|
605 * Note that the second time we run the narrow, most of the datatypes are already filled |
|
606 * in, so it will be able to produce tha correct datatypes for the IL instruction referenced |
|
607 * by the label, as in the 2nd pass we already know the datatypes of the JMP instruction! |
|
608 */ |
|
609 for(int j = 0; j < 2; j++) { |
|
610 for(int i = symbol->n-1; i >= 0; i--) { |
|
611 symbol->elements[i]->accept(*this); |
|
612 } |
|
613 } |
|
614 return NULL; |
|
615 } |
|
616 |
|
617 /* | label ':' [il_incomplete_instruction] eol_list */ |
|
618 // SYM_REF2(il_instruction_c, label, il_instruction) |
|
619 // void *visit(instruction_list_c *symbol); |
|
620 void *narrow_candidate_datatypes_c::visit(il_instruction_c *symbol) { |
|
621 if (NULL == symbol->il_instruction) { |
|
622 /* this empty/null il_instruction cannot generate the desired datatype. We pass on the request to the previous il instruction. */ |
|
623 set_datatype_in_prev_il_instructions(symbol->datatype, symbol); |
|
624 } else { |
|
625 il_instruction_c tmp_prev_il_instruction(NULL, NULL); |
|
626 /* the narrow algorithm will need access to the intersected candidate_datatype lists of all prev_il_instructions, as well as the |
|
627 * list of the prev_il_instructions. |
|
628 * Instead of creating two 'global' (within the class) variables, we create a single il_instruction_c variable (fake_prev_il_instruction), |
|
629 * and shove that data into this single variable. |
|
630 */ |
|
631 tmp_prev_il_instruction.prev_il_instruction = symbol->prev_il_instruction; |
|
632 intersect_prev_candidate_datatype_lists(&tmp_prev_il_instruction); |
|
633 /* Tell the il_instruction the datatype that it must generate - this was chosen by the next il_instruction (remember: we are iterating backwards!) */ |
|
634 fake_prev_il_instruction = &tmp_prev_il_instruction; |
|
635 symbol->il_instruction->datatype = symbol->datatype; |
|
636 symbol->il_instruction->accept(*this); |
|
637 fake_prev_il_instruction = NULL; |
|
638 } |
|
639 return NULL; |
|
640 } |
|
641 |
|
642 |
|
643 |
|
644 |
|
645 // void *visit(instruction_list_c *symbol); |
|
646 void *narrow_candidate_datatypes_c::visit(il_simple_operation_c *symbol) { |
|
647 /* Tell the il_simple_operator the datatype that it must generate - this was chosen by the next il_instruction (we iterate backwards!) */ |
|
648 symbol->il_simple_operator->datatype = symbol->datatype; |
|
649 /* recursive call to see whether data types are compatible */ |
|
650 il_operand = symbol->il_operand; |
|
651 symbol->il_simple_operator->accept(*this); |
|
652 il_operand = NULL; |
|
653 return NULL; |
|
654 } |
|
655 |
|
656 /* | function_name [il_operand_list] */ |
|
657 /* NOTE: The parameters 'called_function_declaration' and 'extensible_param_count' are used to pass data between the stage 3 and stage 4. */ |
|
658 // SYM_REF2(il_function_call_c, function_name, il_operand_list, symbol_c *called_function_declaration; int extensible_param_count;) |
|
659 void *narrow_candidate_datatypes_c::visit(il_function_call_c *symbol) { |
|
660 /* The first parameter of a non formal function call in IL will be the 'current value' (i.e. the prev_il_instruction) |
|
661 * In order to be able to handle this without coding special cases, we will simply prepend that symbol |
|
662 * to the il_operand_list, and remove it after calling handle_function_call(). |
|
663 * However, since handle_function_call() will be recursively calling all parameter, and we don't want |
|
664 * to do that for the prev_il_instruction (since it has already been visited by the fill_candidate_datatypes_c) |
|
665 * we create a new ____ symbol_c ____ object, and copy the relevant info to/from that object before/after |
|
666 * the call to handle_function_call(). |
|
667 * |
|
668 * However, if no further paramters are given, then il_operand_list will be NULL, and we will |
|
669 * need to create a new object to hold the pointer to prev_il_instruction. |
|
670 * This change will also be undone at the end of this method. |
|
671 */ |
|
672 symbol_c param_value = *fake_prev_il_instruction; /* copy the candidate_datatypes list */ |
|
673 if (NULL == symbol->il_operand_list) symbol->il_operand_list = new il_operand_list_c; |
|
674 if (NULL == symbol->il_operand_list) ERROR; |
|
675 |
|
676 ((list_c *)symbol->il_operand_list)->insert_element(¶m_value, 0); |
|
677 |
|
678 generic_function_call_t fcall_param = { |
|
679 /* fcall_param.function_name = */ symbol->function_name, |
|
680 /* fcall_param.nonformal_operand_list = */ symbol->il_operand_list, |
|
681 /* fcall_param.formal_operand_list = */ NULL, |
|
682 /* enum {POU_FB, POU_function} POU_type = */ generic_function_call_t::POU_function, |
|
683 /* fcall_param.candidate_functions = */ symbol->candidate_functions, |
|
684 /* fcall_param.called_function_declaration = */ symbol->called_function_declaration, |
|
685 /* fcall_param.extensible_param_count = */ symbol->extensible_param_count |
|
686 }; |
|
687 |
|
688 narrow_function_invocation(symbol, fcall_param); |
|
689 set_datatype_in_prev_il_instructions(param_value.datatype, fake_prev_il_instruction); |
|
690 |
|
691 /* Undo the changes to the abstract syntax tree we made above... */ |
|
692 ((list_c *)symbol->il_operand_list)->remove_element(0); |
|
693 if (((list_c *)symbol->il_operand_list)->n == 0) { |
|
694 /* if the list becomes empty, then that means that it did not exist before we made these changes, so we delete it! */ |
|
695 delete symbol->il_operand_list; |
|
696 symbol->il_operand_list = NULL; |
|
697 } |
|
698 |
|
699 return NULL; |
|
700 } |
|
701 |
|
702 |
|
703 /* | il_expr_operator '(' [il_operand] eol_list [simple_instr_list] ')' */ |
|
704 // SYM_REF3(il_expression_c, il_expr_operator, il_operand, simple_instr_list); |
|
705 void *narrow_candidate_datatypes_c::visit(il_expression_c *symbol) { |
|
706 /* first handle the operation (il_expr_operator) that will use the result coming from the parenthesised IL list (i.e. simple_instr_list) */ |
|
707 symbol->il_expr_operator->datatype = symbol->datatype; |
|
708 il_operand = symbol->simple_instr_list; /* This is not a bug! The parenthesised expression will be used as the operator! */ |
|
709 symbol->il_expr_operator->accept(*this); |
|
710 |
|
711 /* now give the parenthesised IL list a chance to narrow the datatypes */ |
|
712 /* The datatype that is must return was set by the call symbol->il_expr_operator->accept(*this) */ |
|
713 il_instruction_c *save_fake_prev_il_instruction = fake_prev_il_instruction; /*this is not really necessary, but lets play it safe */ |
|
714 symbol->simple_instr_list->accept(*this); |
|
715 fake_prev_il_instruction = save_fake_prev_il_instruction; |
|
716 return NULL; |
|
717 } |
|
718 |
|
719 |
|
720 |
|
721 |
|
722 /* il_jump_operator label */ |
|
723 void *narrow_candidate_datatypes_c::visit(il_jump_operation_c *symbol) { |
|
724 /* recursive call to fill the datatype */ |
|
725 symbol->il_jump_operator->datatype = symbol->datatype; |
|
726 symbol->il_jump_operator->accept(*this); |
|
727 return NULL; |
|
728 } |
|
729 |
|
730 |
|
731 |
|
732 |
|
733 |
|
734 |
|
735 |
|
736 /* il_call_operator prev_declared_fb_name |
|
737 * | il_call_operator prev_declared_fb_name '(' ')' |
|
738 * | il_call_operator prev_declared_fb_name '(' eol_list ')' |
|
739 * | il_call_operator prev_declared_fb_name '(' il_operand_list ')' |
|
740 * | il_call_operator prev_declared_fb_name '(' eol_list il_param_list ')' |
|
741 */ |
|
742 /* NOTE: The parameter 'called_fb_declaration'is used to pass data between stage 3 and stage4 (although currently it is not used in stage 4 */ |
|
743 // SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list, symbol_c *called_fb_declaration) |
|
744 void *narrow_candidate_datatypes_c::visit(il_fb_call_c *symbol) { |
|
745 symbol_c *fb_decl = symbol->called_fb_declaration; |
|
746 |
|
747 /* Although a call to a non-declared FB is a semantic error, this is currently caught by stage 2! */ |
|
748 if (NULL == fb_decl) ERROR; |
|
749 if (NULL != symbol->il_operand_list) narrow_nonformal_call(symbol, fb_decl); |
|
750 if (NULL != symbol-> il_param_list) narrow_formal_call(symbol, fb_decl); |
|
751 |
|
752 /* Let the il_call_operator (CAL, CALC, or CALCN) set the datatype of prev_il_instruction... */ |
|
753 symbol->il_call_operator->datatype = symbol->datatype; |
|
754 symbol->il_call_operator->accept(*this); |
|
755 return NULL; |
|
756 } |
|
757 |
|
758 |
|
759 /* | function_name '(' eol_list [il_param_list] ')' */ |
|
760 /* NOTE: The parameter 'called_function_declaration' is used to pass data between the stage 3 and stage 4. */ |
|
761 // SYM_REF2(il_formal_funct_call_c, function_name, il_param_list, symbol_c *called_function_declaration; int extensible_param_count;) |
|
762 void *narrow_candidate_datatypes_c::visit(il_formal_funct_call_c *symbol) { |
|
763 generic_function_call_t fcall_param = { |
|
764 /* fcall_param.function_name = */ symbol->function_name, |
|
765 /* fcall_param.nonformal_operand_list = */ NULL, |
|
766 /* fcall_param.formal_operand_list = */ symbol->il_param_list, |
|
767 /* enum {POU_FB, POU_function} POU_type = */ generic_function_call_t::POU_function, |
|
768 /* fcall_param.candidate_functions = */ symbol->candidate_functions, |
|
769 /* fcall_param.called_function_declaration = */ symbol->called_function_declaration, |
|
770 /* fcall_param.extensible_param_count = */ symbol->extensible_param_count |
|
771 }; |
|
772 |
|
773 narrow_function_invocation(symbol, fcall_param); |
|
774 /* The desired datatype of the previous il instruction was already set by narrow_function_invocation() */ |
|
775 return NULL; |
|
776 } |
|
777 |
|
778 |
|
779 // void *visit(il_operand_list_c *symbol); |
|
780 |
|
781 |
|
782 /* | simple_instr_list il_simple_instruction */ |
|
783 /* This object is referenced by il_expression_c objects */ |
|
784 void *narrow_candidate_datatypes_c::visit(simple_instr_list_c *symbol) { |
|
785 if (symbol->n > 0) |
|
786 symbol->elements[symbol->n - 1]->datatype = symbol->datatype; |
|
787 |
|
788 for(int i = symbol->n-1; i >= 0; i--) { |
|
789 symbol->elements[i]->accept(*this); |
|
790 } |
|
791 return NULL; |
|
792 } |
|
793 |
|
794 |
|
795 // SYM_REF1(il_simple_instruction_c, il_simple_instruction, symbol_c *prev_il_instruction;) |
|
796 void *narrow_candidate_datatypes_c::visit(il_simple_instruction_c *symbol) { |
|
797 if (symbol->prev_il_instruction.size() > 1) ERROR; /* There should be no labeled insructions inside an IL expression! */ |
|
798 |
|
799 il_instruction_c tmp_prev_il_instruction(NULL, NULL); |
|
800 /* the narrow algorithm will need access to the intersected candidate_datatype lists of all prev_il_instructions, as well as the |
|
801 * list of the prev_il_instructions. |
|
802 * Instead of creating two 'global' (within the class) variables, we create a single il_instruction_c variable (fake_prev_il_instruction), |
|
803 * and shove that data into this single variable. |
|
804 */ |
|
805 if (symbol->prev_il_instruction.size() > 0) |
|
806 tmp_prev_il_instruction.candidate_datatypes = symbol->prev_il_instruction[0]->candidate_datatypes; |
|
807 tmp_prev_il_instruction.prev_il_instruction = symbol->prev_il_instruction; |
|
808 |
|
809 /* copy the candidate_datatypes list */ |
|
810 fake_prev_il_instruction = &tmp_prev_il_instruction; |
|
811 symbol->il_simple_instruction->datatype = symbol->datatype; |
|
812 symbol->il_simple_instruction->accept(*this); |
|
813 fake_prev_il_instruction = NULL; |
|
814 return NULL; |
|
815 } |
|
816 |
|
817 // void *visit(il_param_list_c *symbol); |
|
818 // void *visit(il_param_assignment_c *symbol); |
|
819 // void *visit(il_param_out_assignment_c *symbol); |
|
820 |
|
821 |
|
822 /*******************/ |
|
823 /* B 2.2 Operators */ |
|
824 /*******************/ |
|
825 void *narrow_candidate_datatypes_c::narrow_binary_operator(const struct widen_entry widen_table[], symbol_c *symbol, bool *deprecated_operation) { |
|
826 symbol_c *prev_instruction_type, *operand_type; |
|
827 int count = 0; |
|
828 |
|
829 if (NULL == symbol->datatype) |
|
830 /* next IL instructions were unable to determine the datatype this instruction should produce */ |
|
831 return NULL; |
|
832 |
|
833 if (NULL != deprecated_operation) |
|
834 *deprecated_operation = false; |
|
835 |
|
836 /* NOTE 1: the il_operand __may__ be pointing to a parenthesized list of IL instructions. |
|
837 * e.g. LD 33 |
|
838 * AND ( 45 |
|
839 * OR 56 |
|
840 * ) |
|
841 * When we handle the first 'AND' IL_operator, the il_operand will point to an simple_instr_list_c. |
|
842 * In this case, when we call il_operand->accept(*this);, the prev_il_instruction pointer will be overwritten! |
|
843 * |
|
844 * We must therefore set the prev_il_instruction->datatype = symbol->datatype; |
|
845 * __before__ calling il_operand->accept(*this) !! |
|
846 * |
|
847 * NOTE 2: We do not need to call prev_il_instruction->accept(*this), as the object to which prev_il_instruction |
|
848 * is pointing to will be later narrowed by the call from the for() loop of the instruction_list_c |
|
849 * (or simple_instr_list_c), which iterates backwards. |
|
850 */ |
|
851 for(unsigned int i = 0; i < fake_prev_il_instruction->candidate_datatypes.size(); i++) { |
|
852 for(unsigned int j = 0; j < il_operand->candidate_datatypes.size(); j++) { |
|
853 prev_instruction_type = fake_prev_il_instruction->candidate_datatypes[i]; |
|
854 operand_type = il_operand->candidate_datatypes[j]; |
|
855 if (is_widening_compatible(widen_table, prev_instruction_type, operand_type, symbol->datatype, deprecated_operation)) { |
|
856 /* set the desired datatype of the previous il instruction */ |
|
857 set_datatype_in_prev_il_instructions(prev_instruction_type, fake_prev_il_instruction); |
|
858 /* set the datatype for the operand */ |
|
859 il_operand->datatype = operand_type; |
|
860 |
|
861 count ++; |
|
862 } |
|
863 } |
|
864 } |
|
865 // if (count > 1) ERROR; /* Since we also support SAFE data types, this assertion is not necessarily always tru! */ |
|
866 if (is_type_valid(symbol->datatype) && (count <= 0)) ERROR; |
|
867 |
|
868 il_operand->accept(*this); |
|
869 return NULL; |
|
870 } |
|
871 |
|
872 |
|
873 |
|
874 |
|
875 |
|
876 void *narrow_candidate_datatypes_c::handle_il_instruction(symbol_c *symbol) { |
|
877 if (NULL == symbol->datatype) |
|
878 /* next IL instructions were unable to determine the datatype this instruction should produce */ |
|
879 return NULL; |
|
880 /* NOTE 1: the il_operand __may__ be pointing to a parenthesized list of IL instructions. |
|
881 * e.g. LD 33 |
|
882 * AND ( 45 |
|
883 * OR 56 |
|
884 * ) |
|
885 * When we handle the first 'AND' IL_operator, the il_operand will point to an simple_instr_list_c. |
|
886 * In this case, when we call il_operand->accept(*this);, the prev_il_instruction pointer will be overwritten! |
|
887 * |
|
888 * We must therefore set the prev_il_instruction->datatype = symbol->datatype; |
|
889 * __before__ calling il_operand->accept(*this) !! |
|
890 * |
|
891 * NOTE 2: We do not need to call prev_il_instruction->accept(*this), as the object to which prev_il_instruction |
|
892 * is pointing to will be later narrowed by the call from the for() loop of the instruction_list_c |
|
893 * (or simple_instr_list_c), which iterates backwards. |
|
894 */ |
|
895 /* set the desired datatype of the previous il instruction */ |
|
896 set_datatype_in_prev_il_instructions(symbol->datatype, fake_prev_il_instruction); |
|
897 |
|
898 /* set the datatype for the operand */ |
|
899 il_operand->datatype = symbol->datatype; |
|
900 il_operand->accept(*this); |
|
901 return NULL; |
|
902 } |
|
903 |
|
904 |
|
905 |
|
906 |
|
907 void *narrow_candidate_datatypes_c::visit(LD_operator_c *symbol) { |
|
908 if (NULL == symbol->datatype) |
|
909 /* next IL instructions were unable to determine the datatype this instruction should produce */ |
|
910 return NULL; |
|
911 /* set the datatype for the operand */ |
|
912 il_operand->datatype = symbol->datatype; |
|
913 il_operand->accept(*this); |
|
914 return NULL; |
|
915 } |
|
916 |
|
917 |
|
918 void *narrow_candidate_datatypes_c::visit(LDN_operator_c *symbol) { |
|
919 if (NULL == symbol->datatype) |
|
920 /* next IL instructions were unable to determine the datatype this instruction should produce */ |
|
921 return NULL; |
|
922 /* set the datatype for the operand */ |
|
923 il_operand->datatype = symbol->datatype; |
|
924 il_operand->accept(*this); |
|
925 return NULL; |
|
926 } |
|
927 |
|
928 void *narrow_candidate_datatypes_c::visit(ST_operator_c *symbol) { |
|
929 if (symbol->candidate_datatypes.size() != 1) |
|
930 return NULL; |
|
931 symbol->datatype = symbol->candidate_datatypes[0]; |
|
932 /* set the datatype for the operand */ |
|
933 il_operand->datatype = symbol->datatype; |
|
934 il_operand->accept(*this); |
|
935 /* set the desired datatype of the previous il instruction */ |
|
936 set_datatype_in_prev_il_instructions(symbol->datatype, fake_prev_il_instruction); |
|
937 return NULL; |
|
938 } |
|
939 |
|
940 void *narrow_candidate_datatypes_c::visit(STN_operator_c *symbol) { |
|
941 if (symbol->candidate_datatypes.size() != 1) |
|
942 return NULL; |
|
943 symbol->datatype = symbol->candidate_datatypes[0]; |
|
944 /* set the datatype for the operand */ |
|
945 il_operand->datatype = symbol->datatype; |
|
946 il_operand->accept(*this); |
|
947 /* set the desired datatype of the previous il instruction */ |
|
948 set_datatype_in_prev_il_instructions(symbol->datatype, fake_prev_il_instruction); |
|
949 return NULL; |
|
950 } |
|
951 |
|
952 void *narrow_candidate_datatypes_c::visit(NOT_operator_c *symbol) { |
|
953 /* NOTE: the standard allows syntax in which the NOT operator is followed by an optional <il_operand> |
|
954 * NOT [<il_operand>] |
|
955 * However, it does not define the semantic of the NOT operation when the <il_operand> is specified. |
|
956 * We therefore consider it an error if an il_operand is specified! |
|
957 */ |
|
958 /* We do not change the data type, we simply invert the bits in bit types! */ |
|
959 /* So, we set the desired datatype of the previous il instruction */ |
|
960 set_datatype_in_prev_il_instructions(symbol->datatype, fake_prev_il_instruction); |
|
961 return NULL; |
|
962 } |
|
963 |
|
964 void *narrow_candidate_datatypes_c::visit(S_operator_c *symbol) { |
|
965 /* TODO: what if this is a FB call? */ |
|
966 return handle_il_instruction(symbol); |
|
967 } |
|
968 void *narrow_candidate_datatypes_c::visit(R_operator_c *symbol) { |
|
969 /* TODO: what if this is a FB call? */ |
|
970 return handle_il_instruction(symbol); |
|
971 } |
|
972 |
|
973 |
|
974 void *narrow_candidate_datatypes_c::visit( S1_operator_c *symbol) {return narrow_implicit_il_fb_call(symbol, "S1", symbol->called_fb_declaration);} |
|
975 void *narrow_candidate_datatypes_c::visit( R1_operator_c *symbol) {return narrow_implicit_il_fb_call(symbol, "R1", symbol->called_fb_declaration);} |
|
976 void *narrow_candidate_datatypes_c::visit( CLK_operator_c *symbol) {return narrow_implicit_il_fb_call(symbol, "CLK", symbol->called_fb_declaration);} |
|
977 void *narrow_candidate_datatypes_c::visit( CU_operator_c *symbol) {return narrow_implicit_il_fb_call(symbol, "CU", symbol->called_fb_declaration);} |
|
978 void *narrow_candidate_datatypes_c::visit( CD_operator_c *symbol) {return narrow_implicit_il_fb_call(symbol, "CD", symbol->called_fb_declaration);} |
|
979 void *narrow_candidate_datatypes_c::visit( PV_operator_c *symbol) {return narrow_implicit_il_fb_call(symbol, "PV", symbol->called_fb_declaration);} |
|
980 void *narrow_candidate_datatypes_c::visit( IN_operator_c *symbol) {return narrow_implicit_il_fb_call(symbol, "IN", symbol->called_fb_declaration);} |
|
981 void *narrow_candidate_datatypes_c::visit( PT_operator_c *symbol) {return narrow_implicit_il_fb_call(symbol, "PT", symbol->called_fb_declaration);} |
|
982 |
|
983 void *narrow_candidate_datatypes_c::visit( AND_operator_c *symbol) {return narrow_binary_operator(widen_AND_table, symbol);} |
|
984 void *narrow_candidate_datatypes_c::visit( OR_operator_c *symbol) {return narrow_binary_operator( widen_OR_table, symbol);} |
|
985 void *narrow_candidate_datatypes_c::visit( XOR_operator_c *symbol) {return narrow_binary_operator(widen_XOR_table, symbol);} |
|
986 void *narrow_candidate_datatypes_c::visit(ANDN_operator_c *symbol) {return narrow_binary_operator(widen_AND_table, symbol);} |
|
987 void *narrow_candidate_datatypes_c::visit( ORN_operator_c *symbol) {return narrow_binary_operator( widen_OR_table, symbol);} |
|
988 void *narrow_candidate_datatypes_c::visit(XORN_operator_c *symbol) {return narrow_binary_operator(widen_XOR_table, symbol);} |
|
989 void *narrow_candidate_datatypes_c::visit( ADD_operator_c *symbol) {return narrow_binary_operator(widen_ADD_table, symbol, &(symbol->deprecated_operation));} |
|
990 void *narrow_candidate_datatypes_c::visit( SUB_operator_c *symbol) {return narrow_binary_operator(widen_SUB_table, symbol, &(symbol->deprecated_operation));} |
|
991 void *narrow_candidate_datatypes_c::visit( MUL_operator_c *symbol) {return narrow_binary_operator(widen_MUL_table, symbol, &(symbol->deprecated_operation));} |
|
992 void *narrow_candidate_datatypes_c::visit( DIV_operator_c *symbol) {return narrow_binary_operator(widen_DIV_table, symbol, &(symbol->deprecated_operation));} |
|
993 void *narrow_candidate_datatypes_c::visit( MOD_operator_c *symbol) {return narrow_binary_operator(widen_MOD_table, symbol);} |
|
994 void *narrow_candidate_datatypes_c::visit( GT_operator_c *symbol) {return narrow_binary_operator(widen_CMP_table, symbol);} |
|
995 void *narrow_candidate_datatypes_c::visit( GE_operator_c *symbol) {return narrow_binary_operator(widen_CMP_table, symbol);} |
|
996 void *narrow_candidate_datatypes_c::visit( EQ_operator_c *symbol) {return narrow_binary_operator(widen_CMP_table, symbol);} |
|
997 void *narrow_candidate_datatypes_c::visit( LT_operator_c *symbol) {return narrow_binary_operator(widen_CMP_table, symbol);} |
|
998 void *narrow_candidate_datatypes_c::visit( LE_operator_c *symbol) {return narrow_binary_operator(widen_CMP_table, symbol);} |
|
999 void *narrow_candidate_datatypes_c::visit( NE_operator_c *symbol) {return narrow_binary_operator(widen_CMP_table, symbol);} |
|
1000 |
|
1001 |
|
1002 |
|
1003 |
|
1004 void *narrow_candidate_datatypes_c::narrow_conditional_flow_control_IL_instruction(symbol_c *symbol) { |
|
1005 /* if the next IL instructions needs us to provide a datatype other than a bool, |
|
1006 * then we have an internal compiler error - most likely in fill_candidate_datatypes_c |
|
1007 */ |
|
1008 if ((NULL != symbol->datatype) && (!is_ANY_BOOL_compatible(symbol->datatype))) ERROR; |
|
1009 if (symbol->candidate_datatypes.size() > 1) ERROR; |
|
1010 |
|
1011 /* NOTE: If there is no IL instruction following this CALC, CALCN, JMPC, JMPC, ..., instruction, |
|
1012 * we must still provide a bool_type_name_c datatype (if possible, i.e. if it exists in the candidate datatype list). |
|
1013 * If it is not possible, we set it to NULL |
|
1014 */ |
|
1015 if (symbol->candidate_datatypes.size() == 0) symbol->datatype = NULL; |
|
1016 else symbol->datatype = symbol->candidate_datatypes[0]; /* i.e. a bool_type_name_c! */ |
|
1017 if ((NULL != symbol->datatype) && (!is_ANY_BOOL_compatible(symbol->datatype))) ERROR; |
|
1018 |
|
1019 /* set the required datatype of the previous IL instruction, i.e. a bool_type_name_c! */ |
|
1020 set_datatype_in_prev_il_instructions(symbol->datatype, fake_prev_il_instruction); |
|
1021 return NULL; |
|
1022 } |
|
1023 |
|
1024 |
|
1025 // SYM_REF0(CAL_operator_c) |
|
1026 // SYM_REF0(CALC_operator_c) |
|
1027 // SYM_REF0(CALCN_operator_c) |
|
1028 /* called from visit(il_fb_call_c *) {symbol->il_call_operator->accpet(*this)} */ |
|
1029 /* NOTE: The CAL, JMP and RET instructions simply set the desired datatype of the previous il instruction since they do not change the value in the current/default IL variable */ |
|
1030 /* called from il_fb_call_c (symbol->il_call_operator->accpet(*this) ) */ |
|
1031 void *narrow_candidate_datatypes_c::visit( CAL_operator_c *symbol) {set_datatype_in_prev_il_instructions(symbol->datatype, fake_prev_il_instruction); return NULL;} |
|
1032 void *narrow_candidate_datatypes_c::visit( RET_operator_c *symbol) {set_datatype_in_prev_il_instructions(symbol->datatype, fake_prev_il_instruction); return NULL;} |
|
1033 void *narrow_candidate_datatypes_c::visit( JMP_operator_c *symbol) {set_datatype_in_prev_il_instructions(symbol->datatype, fake_prev_il_instruction); return NULL;} |
|
1034 void *narrow_candidate_datatypes_c::visit( CALC_operator_c *symbol) {return narrow_conditional_flow_control_IL_instruction(symbol);} |
|
1035 void *narrow_candidate_datatypes_c::visit(CALCN_operator_c *symbol) {return narrow_conditional_flow_control_IL_instruction(symbol);} |
|
1036 void *narrow_candidate_datatypes_c::visit( RETC_operator_c *symbol) {return narrow_conditional_flow_control_IL_instruction(symbol);} |
|
1037 void *narrow_candidate_datatypes_c::visit(RETCN_operator_c *symbol) {return narrow_conditional_flow_control_IL_instruction(symbol);} |
|
1038 void *narrow_candidate_datatypes_c::visit( JMPC_operator_c *symbol) {return narrow_conditional_flow_control_IL_instruction(symbol);} |
|
1039 void *narrow_candidate_datatypes_c::visit(JMPCN_operator_c *symbol) {return narrow_conditional_flow_control_IL_instruction(symbol);} |
|
1040 |
|
1041 /* Symbol class handled together with function call checks */ |
|
1042 // void *visit(il_assign_operator_c *symbol, variable_name); |
|
1043 /* Symbol class handled together with function call checks */ |
|
1044 // void *visit(il_assign_operator_c *symbol, option, variable_name); |
|
1045 |
|
1046 |
|
1047 /***************************************/ |
|
1048 /* B.3 - Language ST (Structured Text) */ |
|
1049 /***************************************/ |
|
1050 /***********************/ |
|
1051 /* B 3.1 - Expressions */ |
|
1052 /***********************/ |
|
1053 void *narrow_candidate_datatypes_c::narrow_binary_expression(const struct widen_entry widen_table[], symbol_c *symbol, symbol_c *l_expr, symbol_c *r_expr, bool *deprecated_operation) { |
|
1054 symbol_c *l_type, *r_type; |
|
1055 int count = 0; |
|
1056 |
|
1057 if (NULL != deprecated_operation) |
|
1058 *deprecated_operation = false; |
|
1059 |
|
1060 for(unsigned int i = 0; i < l_expr->candidate_datatypes.size(); i++) { |
|
1061 for(unsigned int j = 0; j < r_expr->candidate_datatypes.size(); j++) { |
|
1062 /* test widening compatibility */ |
|
1063 l_type = l_expr->candidate_datatypes[i]; |
|
1064 r_type = r_expr->candidate_datatypes[j]; |
|
1065 if (is_widening_compatible(widen_table, l_type, r_type, symbol->datatype, deprecated_operation)) { |
|
1066 l_expr->datatype = l_type; |
|
1067 r_expr->datatype = r_type; |
|
1068 count ++; |
|
1069 } |
|
1070 } |
|
1071 } |
|
1072 // if (count > 1) ERROR; /* Since we also support SAFE data types, this assertion is not necessarily always tru! */ |
|
1073 if (is_type_valid(symbol->datatype) && (count <= 0)) ERROR; |
|
1074 |
|
1075 l_expr->accept(*this); |
|
1076 r_expr->accept(*this); |
|
1077 return NULL; |
|
1078 } |
|
1079 |
|
1080 |
|
1081 |
|
1082 void *narrow_candidate_datatypes_c::visit( or_expression_c *symbol) {return narrow_binary_expression( widen_OR_table, symbol, symbol->l_exp, symbol->r_exp);} |
|
1083 void *narrow_candidate_datatypes_c::visit( xor_expression_c *symbol) {return narrow_binary_expression(widen_XOR_table, symbol, symbol->l_exp, symbol->r_exp);} |
|
1084 void *narrow_candidate_datatypes_c::visit( and_expression_c *symbol) {return narrow_binary_expression(widen_AND_table, symbol, symbol->l_exp, symbol->r_exp);} |
|
1085 |
|
1086 void *narrow_candidate_datatypes_c::visit( equ_expression_c *symbol) {return narrow_binary_expression(widen_CMP_table, symbol, symbol->l_exp, symbol->r_exp);} |
|
1087 void *narrow_candidate_datatypes_c::visit(notequ_expression_c *symbol) {return narrow_binary_expression(widen_CMP_table, symbol, symbol->l_exp, symbol->r_exp);} |
|
1088 void *narrow_candidate_datatypes_c::visit( lt_expression_c *symbol) {return narrow_binary_expression(widen_CMP_table, symbol, symbol->l_exp, symbol->r_exp);} |
|
1089 void *narrow_candidate_datatypes_c::visit( gt_expression_c *symbol) {return narrow_binary_expression(widen_CMP_table, symbol, symbol->l_exp, symbol->r_exp);} |
|
1090 void *narrow_candidate_datatypes_c::visit( le_expression_c *symbol) {return narrow_binary_expression(widen_CMP_table, symbol, symbol->l_exp, symbol->r_exp);} |
|
1091 void *narrow_candidate_datatypes_c::visit( ge_expression_c *symbol) {return narrow_binary_expression(widen_CMP_table, symbol, symbol->l_exp, symbol->r_exp);} |
|
1092 |
|
1093 void *narrow_candidate_datatypes_c::visit( add_expression_c *symbol) {return narrow_binary_expression(widen_ADD_table, symbol, symbol->l_exp, symbol->r_exp, &symbol->deprecated_operation);} |
|
1094 void *narrow_candidate_datatypes_c::visit( sub_expression_c *symbol) {return narrow_binary_expression(widen_SUB_table, symbol, symbol->l_exp, symbol->r_exp, &symbol->deprecated_operation);} |
|
1095 void *narrow_candidate_datatypes_c::visit( mul_expression_c *symbol) {return narrow_binary_expression(widen_MUL_table, symbol, symbol->l_exp, symbol->r_exp, &symbol->deprecated_operation);} |
|
1096 void *narrow_candidate_datatypes_c::visit( div_expression_c *symbol) {return narrow_binary_expression(widen_DIV_table, symbol, symbol->l_exp, symbol->r_exp, &symbol->deprecated_operation);} |
|
1097 void *narrow_candidate_datatypes_c::visit( mod_expression_c *symbol) {return narrow_binary_expression(widen_MOD_table, symbol, symbol->l_exp, symbol->r_exp);} |
|
1098 void *narrow_candidate_datatypes_c::visit( power_expression_c *symbol) {return narrow_binary_expression(widen_EXPT_table,symbol, symbol->l_exp, symbol->r_exp);} |
|
1099 |
|
1100 |
|
1101 void *narrow_candidate_datatypes_c::visit(neg_expression_c *symbol) { |
|
1102 symbol->exp->datatype = symbol->datatype; |
|
1103 symbol->exp->accept(*this); |
|
1104 return NULL; |
|
1105 } |
|
1106 |
|
1107 |
|
1108 void *narrow_candidate_datatypes_c::visit(not_expression_c *symbol) { |
|
1109 symbol->exp->datatype = symbol->datatype; |
|
1110 symbol->exp->accept(*this); |
|
1111 return NULL; |
|
1112 } |
|
1113 |
|
1114 |
|
1115 |
|
1116 /* NOTE: The parameter 'called_function_declaration', 'extensible_param_count' and 'candidate_functions' are used to pass data between the stage 3 and stage 4. */ |
|
1117 /* formal_param_list -> may be NULL ! */ |
|
1118 /* nonformal_param_list -> may be NULL ! */ |
|
1119 // SYM_REF3(function_invocation_c, function_name, formal_param_list, nonformal_param_list, symbol_c *called_function_declaration; int extensible_param_count; std::vector <symbol_c *> candidate_functions;) |
|
1120 void *narrow_candidate_datatypes_c::visit(function_invocation_c *symbol) { |
|
1121 generic_function_call_t fcall_param = { |
|
1122 /* fcall_param.function_name = */ symbol->function_name, |
|
1123 /* fcall_param.nonformal_operand_list = */ symbol->nonformal_param_list, |
|
1124 /* fcall_param.formal_operand_list = */ symbol->formal_param_list, |
|
1125 /* enum {POU_FB, POU_function} POU_type = */ generic_function_call_t::POU_function, |
|
1126 /* fcall_param.candidate_functions = */ symbol->candidate_functions, |
|
1127 /* fcall_param.called_function_declaration = */ symbol->called_function_declaration, |
|
1128 /* fcall_param.extensible_param_count = */ symbol->extensible_param_count |
|
1129 }; |
|
1130 |
|
1131 narrow_function_invocation(symbol, fcall_param); |
|
1132 return NULL; |
|
1133 } |
|
1134 |
|
1135 /********************/ |
|
1136 /* B 3.2 Statements */ |
|
1137 /********************/ |
|
1138 |
|
1139 |
|
1140 /*********************************/ |
|
1141 /* B 3.2.1 Assignment Statements */ |
|
1142 /*********************************/ |
|
1143 |
|
1144 void *narrow_candidate_datatypes_c::visit(assignment_statement_c *symbol) { |
|
1145 if (symbol->candidate_datatypes.size() != 1) |
|
1146 return NULL; |
|
1147 symbol->datatype = symbol->candidate_datatypes[0]; |
|
1148 symbol->l_exp->datatype = symbol->datatype; |
|
1149 symbol->l_exp->accept(*this); |
|
1150 symbol->r_exp->datatype = symbol->datatype; |
|
1151 symbol->r_exp->accept(*this); |
|
1152 return NULL; |
|
1153 } |
|
1154 |
|
1155 |
|
1156 /*****************************************/ |
|
1157 /* B 3.2.2 Subprogram Control Statements */ |
|
1158 /*****************************************/ |
|
1159 |
|
1160 void *narrow_candidate_datatypes_c::visit(fb_invocation_c *symbol) { |
|
1161 /* Note: We do not use the symbol->called_fb_declaration value (set in fill_candidate_datatypes_c) |
|
1162 * because we try to identify any other datatype errors in the expressions used in the |
|
1163 * parameters to the FB call (e.g. fb_var(var1 * 56 + func(var * 43)) ) |
|
1164 * even it the call to the FB is invalid. |
|
1165 * This makes sense because it may be errors in those expressions which are |
|
1166 * making this an invalid call, so it makes sense to point them out to the user! |
|
1167 */ |
|
1168 symbol_c *fb_decl = search_varfb_instance_type->get_basetype_decl(symbol->fb_name); |
|
1169 |
|
1170 /* Although a call to a non-declared FB is a semantic error, this is currently caught by stage 2! */ |
|
1171 if (NULL == fb_decl) ERROR; |
|
1172 if (NULL != symbol->nonformal_param_list) narrow_nonformal_call(symbol, fb_decl); |
|
1173 if (NULL != symbol-> formal_param_list) narrow_formal_call(symbol, fb_decl); |
|
1174 |
|
1175 return NULL; |
|
1176 } |
|
1177 |
|
1178 |
|
1179 /********************************/ |
|
1180 /* B 3.2.3 Selection Statements */ |
|
1181 /********************************/ |
|
1182 |
|
1183 void *narrow_candidate_datatypes_c::visit(if_statement_c *symbol) { |
|
1184 for(unsigned int i = 0; i < symbol->expression->candidate_datatypes.size(); i++) { |
|
1185 if (is_ANY_BOOL_compatible(symbol->expression->candidate_datatypes[i])) |
|
1186 symbol->expression->datatype = symbol->expression->candidate_datatypes[i]; |
|
1187 } |
|
1188 symbol->expression->accept(*this); |
|
1189 if (NULL != symbol->statement_list) |
|
1190 symbol->statement_list->accept(*this); |
|
1191 if (NULL != symbol->elseif_statement_list) |
|
1192 symbol->elseif_statement_list->accept(*this); |
|
1193 if (NULL != symbol->else_statement_list) |
|
1194 symbol->else_statement_list->accept(*this); |
|
1195 return NULL; |
|
1196 } |
|
1197 |
|
1198 |
|
1199 void *narrow_candidate_datatypes_c::visit(elseif_statement_c *symbol) { |
|
1200 for (unsigned int i = 0; i < symbol->expression->candidate_datatypes.size(); i++) { |
|
1201 if (is_ANY_BOOL_compatible(symbol->expression->candidate_datatypes[i])) |
|
1202 symbol->expression->datatype = symbol->expression->candidate_datatypes[i]; |
|
1203 } |
|
1204 symbol->expression->accept(*this); |
|
1205 if (NULL != symbol->statement_list) |
|
1206 symbol->statement_list->accept(*this); |
|
1207 return NULL; |
|
1208 } |
|
1209 |
|
1210 /* CASE expression OF case_element_list ELSE statement_list END_CASE */ |
|
1211 // SYM_REF3(case_statement_c, expression, case_element_list, statement_list) |
|
1212 void *narrow_candidate_datatypes_c::visit(case_statement_c *symbol) { |
|
1213 for (unsigned int i = 0; i < symbol->expression->candidate_datatypes.size(); i++) { |
|
1214 if ((is_ANY_INT_type(symbol->expression->candidate_datatypes[i])) |
|
1215 || (search_base_type.type_is_enumerated(symbol->expression->candidate_datatypes[i]))) |
|
1216 symbol->expression->datatype = symbol->expression->candidate_datatypes[i]; |
|
1217 } |
|
1218 symbol->expression->accept(*this); |
|
1219 if (NULL != symbol->statement_list) |
|
1220 symbol->statement_list->accept(*this); |
|
1221 if (NULL != symbol->case_element_list) { |
|
1222 symbol->case_element_list->datatype = symbol->expression->datatype; |
|
1223 symbol->case_element_list->accept(*this); |
|
1224 } |
|
1225 return NULL; |
|
1226 } |
|
1227 |
|
1228 /* helper symbol for case_statement */ |
|
1229 // SYM_LIST(case_element_list_c) |
|
1230 void *narrow_candidate_datatypes_c::visit(case_element_list_c *symbol) { |
|
1231 for (int i = 0; i < symbol->n; i++) { |
|
1232 symbol->elements[i]->datatype = symbol->datatype; |
|
1233 symbol->elements[i]->accept(*this); |
|
1234 } |
|
1235 return NULL; |
|
1236 } |
|
1237 |
|
1238 /* case_list ':' statement_list */ |
|
1239 // SYM_REF2(case_element_c, case_list, statement_list) |
|
1240 void *narrow_candidate_datatypes_c::visit(case_element_c *symbol) { |
|
1241 symbol->case_list->datatype = symbol->datatype; |
|
1242 symbol->case_list->accept(*this); |
|
1243 symbol->statement_list->accept(*this); |
|
1244 return NULL; |
|
1245 } |
|
1246 |
|
1247 // SYM_LIST(case_list_c) |
|
1248 void *narrow_candidate_datatypes_c::visit(case_list_c *symbol) { |
|
1249 for (int i = 0; i < symbol->n; i++) { |
|
1250 for (unsigned int k = 0; k < symbol->elements[i]->candidate_datatypes.size(); k++) { |
|
1251 if (is_type_equal(symbol->datatype, symbol->elements[i]->candidate_datatypes[k])) |
|
1252 symbol->elements[i]->datatype = symbol->elements[i]->candidate_datatypes[k]; |
|
1253 } |
|
1254 /* NOTE: this may be an integer, a subrange_c, or a enumerated value! */ |
|
1255 symbol->elements[i]->accept(*this); |
|
1256 } |
|
1257 return NULL; |
|
1258 } |
|
1259 |
|
1260 |
|
1261 /********************************/ |
|
1262 /* B 3.2.4 Iteration Statements */ |
|
1263 /********************************/ |
|
1264 void *narrow_candidate_datatypes_c::visit(for_statement_c *symbol) { |
|
1265 /* Control variable */ |
|
1266 for(unsigned int i = 0; i < symbol->control_variable->candidate_datatypes.size(); i++) { |
|
1267 if (is_ANY_INT_type(symbol->control_variable->candidate_datatypes[i])) { |
|
1268 symbol->control_variable->datatype = symbol->control_variable->candidate_datatypes[i]; |
|
1269 } |
|
1270 } |
|
1271 symbol->control_variable->accept(*this); |
|
1272 /* BEG expression */ |
|
1273 for(unsigned int i = 0; i < symbol->beg_expression->candidate_datatypes.size(); i++) { |
|
1274 if (is_type_equal(symbol->control_variable->datatype,symbol->beg_expression->candidate_datatypes[i]) && |
|
1275 is_ANY_INT_type(symbol->beg_expression->candidate_datatypes[i])) { |
|
1276 symbol->beg_expression->datatype = symbol->beg_expression->candidate_datatypes[i]; |
|
1277 } |
|
1278 } |
|
1279 symbol->beg_expression->accept(*this); |
|
1280 /* END expression */ |
|
1281 for(unsigned int i = 0; i < symbol->end_expression->candidate_datatypes.size(); i++) { |
|
1282 if (is_type_equal(symbol->control_variable->datatype,symbol->end_expression->candidate_datatypes[i]) && |
|
1283 is_ANY_INT_type(symbol->end_expression->candidate_datatypes[i])) { |
|
1284 symbol->end_expression->datatype = symbol->end_expression->candidate_datatypes[i]; |
|
1285 } |
|
1286 } |
|
1287 symbol->end_expression->accept(*this); |
|
1288 /* BY expression */ |
|
1289 if (NULL != symbol->by_expression) { |
|
1290 for(unsigned int i = 0; i < symbol->by_expression->candidate_datatypes.size(); i++) { |
|
1291 if (is_type_equal(symbol->control_variable->datatype,symbol->by_expression->candidate_datatypes[i]) && |
|
1292 is_ANY_INT_type(symbol->by_expression->candidate_datatypes[i])) { |
|
1293 symbol->by_expression->datatype = symbol->by_expression->candidate_datatypes[i]; |
|
1294 } |
|
1295 } |
|
1296 symbol->by_expression->accept(*this); |
|
1297 } |
|
1298 if (NULL != symbol->statement_list) |
|
1299 symbol->statement_list->accept(*this); |
|
1300 return NULL; |
|
1301 } |
|
1302 |
|
1303 void *narrow_candidate_datatypes_c::visit(while_statement_c *symbol) { |
|
1304 for (unsigned int i = 0; i < symbol->expression->candidate_datatypes.size(); i++) { |
|
1305 if(is_BOOL_type(symbol->expression->candidate_datatypes[i])) |
|
1306 symbol->expression->datatype = symbol->expression->candidate_datatypes[i]; |
|
1307 } |
|
1308 symbol->expression->accept(*this); |
|
1309 if (NULL != symbol->statement_list) |
|
1310 symbol->statement_list->accept(*this); |
|
1311 return NULL; |
|
1312 } |
|
1313 |
|
1314 void *narrow_candidate_datatypes_c::visit(repeat_statement_c *symbol) { |
|
1315 for (unsigned int i = 0; i < symbol->expression->candidate_datatypes.size(); i++) { |
|
1316 if(is_BOOL_type(symbol->expression->candidate_datatypes[i])) |
|
1317 symbol->expression->datatype = symbol->expression->candidate_datatypes[i]; |
|
1318 } |
|
1319 symbol->expression->accept(*this); |
|
1320 if (NULL != symbol->statement_list) |
|
1321 symbol->statement_list->accept(*this); |
|
1322 return NULL; |
|
1323 } |
|
1324 |
|
1325 |
|
1326 |
|
1327 |
|
1328 |