|
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 /* TODO - things yet not checked by this data type checker... |
|
36 * |
|
37 * - check variable declarations |
|
38 * - check data type declarations |
|
39 * - check inside configurations (variable declarations) |
|
40 * - check SFC code |
|
41 * - must fix S and R IL functions (includes potientialy fixing stage4 code!) |
|
42 */ |
|
43 |
|
44 |
|
45 /* NOTE: The algorithm implemented here assumes that flow control analysis has already been completed! |
|
46 * BEFORE running this visitor, be sure to CALL the flow_control_analysis_c visitor! |
|
47 */ |
|
48 |
|
49 |
|
50 /* |
|
51 * Fill the candidate datatype list for all symbols that may legally 'have' a data type (e.g. variables, literals, function calls, expressions, etc.) |
|
52 * |
|
53 * The candidate datatype list will be filled with a list of all the data types that expression may legally take. |
|
54 * For example, the very simple literal '0' (as in foo := 0), may represent a: |
|
55 * BOOL, BYTE, WORD, DWORD, LWORD, USINT, SINT, UINT, INT, UDINT, DINT, ULINT, LINT (as well as the SAFE versions of these data tyes too!) |
|
56 */ |
|
57 |
|
58 #include <../main.hh> /* required for UINT64_MAX, INT64_MAX, INT64_MIN, ... */ |
|
59 #include "fill_candidate_datatypes.hh" |
|
60 #include "datatype_functions.hh" |
|
61 #include <typeinfo> |
|
62 #include <list> |
|
63 #include <string> |
|
64 #include <string.h> |
|
65 #include <strings.h> |
|
66 |
|
67 #define GET_CVALUE(dtype, symbol) ((symbol)->const_value._##dtype.value) |
|
68 #define VALID_CVALUE(dtype, symbol) (symbol_c::cs_const_value == (symbol)->const_value._##dtype.status) |
|
69 #define IS_OVERFLOW(dtype, symbol) (symbol_c::cs_overflow == (symbol)->const_value._##dtype.status) |
|
70 |
|
71 /* set to 1 to see debug info during execution */ |
|
72 static int debug = 0; |
|
73 |
|
74 fill_candidate_datatypes_c::fill_candidate_datatypes_c(symbol_c *ignore) { |
|
75 } |
|
76 |
|
77 fill_candidate_datatypes_c::~fill_candidate_datatypes_c(void) { |
|
78 } |
|
79 |
|
80 symbol_c *fill_candidate_datatypes_c::widening_conversion(symbol_c *left_type, symbol_c *right_type, const struct widen_entry widen_table[]) { |
|
81 int k; |
|
82 /* find a widening table entry compatible */ |
|
83 for (k = 0; NULL != widen_table[k].left; k++) |
|
84 if ((typeid(*left_type) == typeid(*widen_table[k].left)) && (typeid(*right_type) == typeid(*widen_table[k].right))) |
|
85 return widen_table[k].result; |
|
86 return NULL; |
|
87 } |
|
88 |
|
89 |
|
90 /* add a data type to a candidate data type list, while guaranteeing no duplicate entries! */ |
|
91 /* Returns true if it really did add the datatype to the list, or false if it was already present in the list! */ |
|
92 bool fill_candidate_datatypes_c::add_datatype_to_candidate_list(symbol_c *symbol, symbol_c *datatype) { |
|
93 /* If it is an invalid data type, do not insert! |
|
94 * NOTE: it reduces overall code size to do this test here, instead of doing every time before calling the add_datatype_to_candidate_list() function. |
|
95 */ |
|
96 if (!is_type_valid(datatype)) /* checks for NULL and invalid_type_name_c */ |
|
97 return false; |
|
98 |
|
99 if (search_in_candidate_datatype_list(datatype, symbol->candidate_datatypes) >= 0) |
|
100 /* already in the list, Just return! */ |
|
101 return false; |
|
102 |
|
103 /* not yet in the candidate data type list, so we insert it now! */ |
|
104 symbol->candidate_datatypes.push_back(datatype); |
|
105 return true; |
|
106 } |
|
107 |
|
108 |
|
109 bool fill_candidate_datatypes_c::add_2datatypes_to_candidate_list(symbol_c *symbol, symbol_c *datatype1, symbol_c *datatype2) { |
|
110 add_datatype_to_candidate_list(symbol, datatype1); |
|
111 add_datatype_to_candidate_list(symbol, datatype2); |
|
112 return true; |
|
113 } |
|
114 |
|
115 |
|
116 |
|
117 void fill_candidate_datatypes_c::remove_incompatible_datatypes(symbol_c *symbol) { |
|
118 #ifdef __REMOVE__ |
|
119 #error __REMOVE__ macro already exists. Choose another name! |
|
120 #endif |
|
121 #define __REMOVE__(datatype)\ |
|
122 remove_from_candidate_datatype_list(&search_constant_type_c::datatype, symbol->candidate_datatypes);\ |
|
123 remove_from_candidate_datatype_list(&search_constant_type_c::safe##datatype, symbol->candidate_datatypes); |
|
124 |
|
125 {/* Remove unsigned data types */ |
|
126 uint64_t value = 0; |
|
127 if (VALID_CVALUE( uint64, symbol)) value = GET_CVALUE(uint64, symbol); |
|
128 if (IS_OVERFLOW ( uint64, symbol)) value = (uint64_t)UINT32_MAX + (uint64_t)1; |
|
129 |
|
130 if (value > 1 ) {__REMOVE__(bool_type_name);} |
|
131 if (value > UINT8_MAX ) {__REMOVE__(usint_type_name); __REMOVE__( byte_type_name);} |
|
132 if (value > UINT16_MAX ) {__REMOVE__( uint_type_name); __REMOVE__( word_type_name);} |
|
133 if (value > UINT32_MAX ) {__REMOVE__(udint_type_name); __REMOVE__(dword_type_name);} |
|
134 if (IS_OVERFLOW( uint64, symbol)) {__REMOVE__(ulint_type_name); __REMOVE__(lword_type_name);} |
|
135 } |
|
136 |
|
137 {/* Remove signed data types */ |
|
138 int64_t value = 0; |
|
139 if (VALID_CVALUE( int64, symbol)) value = GET_CVALUE(int64, symbol); |
|
140 if (IS_OVERFLOW ( int64, symbol)) value = (int64_t)INT32_MAX + (int64_t)1; |
|
141 |
|
142 if ((value < INT8_MIN) || (value > INT8_MAX)) {__REMOVE__(sint_type_name);} |
|
143 if ((value < INT16_MIN) || (value > INT16_MAX)) {__REMOVE__( int_type_name);} |
|
144 if ((value < INT32_MIN) || (value > INT32_MAX)) {__REMOVE__(dint_type_name);} |
|
145 if (IS_OVERFLOW( int64, symbol)) {__REMOVE__(lint_type_name);} |
|
146 } |
|
147 |
|
148 {/* Remove floating point data types */ |
|
149 real64_t value = 0; |
|
150 if (VALID_CVALUE( real64, symbol)) value = GET_CVALUE(real64, symbol); |
|
151 if (IS_OVERFLOW ( real64, symbol)) value = (real64_t)REAL32_MAX + (real64_t)1; |
|
152 if (value > REAL32_MAX ) {__REMOVE__( real_type_name);} |
|
153 if (value < -REAL32_MAX ) {__REMOVE__( real_type_name);} |
|
154 if (IS_OVERFLOW( real64, symbol)) {__REMOVE__(lreal_type_name);} |
|
155 } |
|
156 #undef __REMOVE__ |
|
157 } |
|
158 |
|
159 |
|
160 /* returns true if compatible function/FB invocation, otherwise returns false */ |
|
161 /* Assumes that the candidate_datatype lists of all the parameters being passed haved already been filled in */ |
|
162 /* |
|
163 * All parameters being passed to the called function MUST be in the parameter list to which f_call points to! |
|
164 * This means that, for non formal function calls in IL, de current (default value) must be artificially added to the |
|
165 * beginning of the parameter list BEFORE calling handle_function_call(). |
|
166 */ |
|
167 bool fill_candidate_datatypes_c::match_nonformal_call(symbol_c *f_call, symbol_c *f_decl) { |
|
168 symbol_c *call_param_value, *param_datatype; |
|
169 identifier_c *param_name; |
|
170 function_param_iterator_c fp_iterator(f_decl); |
|
171 function_call_param_iterator_c fcp_iterator(f_call); |
|
172 int extensible_parameter_highest_index = -1; |
|
173 unsigned int i; |
|
174 |
|
175 /* Iterating through the non-formal parameters of the function call */ |
|
176 while((call_param_value = fcp_iterator.next_nf()) != NULL) { |
|
177 /* Iterate to the next parameter of the function being called. |
|
178 * Get the name of that parameter, and ignore if EN or ENO. |
|
179 */ |
|
180 do { |
|
181 param_name = fp_iterator.next(); |
|
182 /* If there is no other parameter declared, then we are passing too many parameters... */ |
|
183 if(param_name == NULL) return false; |
|
184 } while ((strcmp(param_name->value, "EN") == 0) || (strcmp(param_name->value, "ENO") == 0)); |
|
185 |
|
186 /* TODO: verify if it is lvalue when INOUT or OUTPUT parameters! */ |
|
187 /* Get the parameter type */ |
|
188 param_datatype = base_type(fp_iterator.param_type()); |
|
189 |
|
190 /* check whether one of the candidate_data_types of the value being passed is the same as the param_type */ |
|
191 if (search_in_candidate_datatype_list(param_datatype, call_param_value->candidate_datatypes) < 0) |
|
192 return false; /* return false if param_type not in the list! */ |
|
193 } |
|
194 /* call is compatible! */ |
|
195 return true; |
|
196 } |
|
197 |
|
198 |
|
199 |
|
200 /* returns true if compatible function/FB invocation, otherwise returns false */ |
|
201 /* Assumes that the candidate_datatype lists of all the parameters being passed haved already been filled in */ |
|
202 bool fill_candidate_datatypes_c::match_formal_call(symbol_c *f_call, symbol_c *f_decl, symbol_c **first_param_datatype) { |
|
203 symbol_c *call_param_value, *call_param_name, *param_datatype; |
|
204 symbol_c *verify_duplicate_param; |
|
205 identifier_c *param_name; |
|
206 function_param_iterator_c fp_iterator(f_decl); |
|
207 function_call_param_iterator_c fcp_iterator(f_call); |
|
208 int extensible_parameter_highest_index = -1; |
|
209 identifier_c *extensible_parameter_name; |
|
210 unsigned int i; |
|
211 bool is_first_param = true; |
|
212 |
|
213 /* Iterating through the formal parameters of the function call */ |
|
214 while((call_param_name = fcp_iterator.next_f()) != NULL) { |
|
215 /* Obtaining the value being passed in the function call */ |
|
216 call_param_value = fcp_iterator.get_current_value(); |
|
217 /* the following should never occur. If it does, then we have a bug in our code... */ |
|
218 if (NULL == call_param_value) ERROR; |
|
219 |
|
220 /* Obtaining the assignment direction: := (assign_in) or => (assign_out) */ |
|
221 function_call_param_iterator_c::assign_direction_t call_param_dir = fcp_iterator.get_assign_direction(); |
|
222 |
|
223 /* Checking if there are duplicated parameter values */ |
|
224 verify_duplicate_param = fcp_iterator.search_f(call_param_name); |
|
225 if(verify_duplicate_param != call_param_value) |
|
226 return false; |
|
227 |
|
228 /* Obtaining the type of the value being passed in the function call */ |
|
229 std::vector <symbol_c *>&call_param_types = call_param_value->candidate_datatypes; |
|
230 |
|
231 /* Find the corresponding parameter in function declaration */ |
|
232 param_name = fp_iterator.search(call_param_name); |
|
233 if(param_name == NULL) return false; |
|
234 /* Get the parameter data type */ |
|
235 param_datatype = base_type(fp_iterator.param_type()); |
|
236 /* Get the parameter direction: IN, OUT, IN_OUT */ |
|
237 function_param_iterator_c::param_direction_t param_dir = fp_iterator.param_direction(); |
|
238 |
|
239 /* check whether direction (IN, OUT, IN_OUT) and assignment types (:= , =>) are compatible !!! */ |
|
240 if (function_call_param_iterator_c::assign_in == call_param_dir) { |
|
241 if ((function_param_iterator_c::direction_in != param_dir) && |
|
242 (function_param_iterator_c::direction_inout != param_dir)) |
|
243 return false; |
|
244 } else if (function_call_param_iterator_c::assign_out == call_param_dir) { |
|
245 if ((function_param_iterator_c::direction_out != param_dir)) |
|
246 return false; |
|
247 } else ERROR; |
|
248 |
|
249 /* check whether one of the candidate_data_types of the value being passed is the same as the param_type */ |
|
250 if (search_in_candidate_datatype_list(param_datatype, call_param_types) < 0) |
|
251 return false; /* return false if param_type not in the list! */ |
|
252 |
|
253 /* If this is the first parameter, then copy the datatype to *first_param_datatype */ |
|
254 if (is_first_param) |
|
255 if (NULL != first_param_datatype) |
|
256 *first_param_datatype = param_datatype; |
|
257 is_first_param = false; |
|
258 } |
|
259 /* call is compatible! */ |
|
260 return true; |
|
261 } |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 /* Handle a generic function call! |
|
267 * Assumes that the parameter_list containing the values being passed in this function invocation |
|
268 * has already had all the candidate_datatype lists filled in! |
|
269 * |
|
270 * All parameters being passed to the called function MUST be in the parameter list to which f_call points to! |
|
271 * This means that, for non formal function calls in IL, de current (default value) must be artificially added to the |
|
272 * beginning of the parameter list BEFORE calling handle_function_call(). |
|
273 */ |
|
274 /* |
|
275 typedef struct { |
|
276 symbol_c *function_name, |
|
277 symbol_c *nonformal_operand_list, |
|
278 symbol_c * formal_operand_list, |
|
279 |
|
280 std::vector <symbol_c *> &candidate_functions, |
|
281 symbol_c &*called_function_declaration, |
|
282 int &extensible_param_count |
|
283 } generic_function_call_t; |
|
284 */ |
|
285 /* |
|
286 void narrow_candidate_datatypes_c::narrow_function_invocation(symbol_c *fcall, generic_function_call_t fcall_data) { |
|
287 void *fill_candidate_datatypes_c::handle_function_call(symbol_c *f_call, symbol_c *function_name, invocation_type_t invocation_type, |
|
288 std::vector <symbol_c *> *candidate_datatypes, |
|
289 std::vector <symbol_c *> *candidate_functions) { |
|
290 */ |
|
291 void fill_candidate_datatypes_c::handle_function_call(symbol_c *fcall, generic_function_call_t fcall_data) { |
|
292 function_declaration_c *f_decl; |
|
293 list_c *parameter_list; |
|
294 list_c *parameter_candidate_datatypes; |
|
295 symbol_c *returned_parameter_type; |
|
296 |
|
297 if (debug) std::cout << "function()\n"; |
|
298 |
|
299 function_symtable_t::iterator lower = function_symtable.lower_bound(fcall_data.function_name); |
|
300 function_symtable_t::iterator upper = function_symtable.upper_bound(fcall_data.function_name); |
|
301 /* If the name of the function being called is not found in the function symbol table, then this is an invalid call */ |
|
302 /* Since the lexical parser already checks for this, then if this occurs then we have an internal compiler error. */ |
|
303 if (lower == function_symtable.end()) ERROR; |
|
304 |
|
305 /* Look for all compatible function declarations, and add their return datatypes |
|
306 * to the candidate_datatype list of this function invocation. |
|
307 * |
|
308 * If only one function exists, we add its return datatype to the candidate_datatype list, |
|
309 * even if the parameters passed to it are invalid. |
|
310 * This guarantees that the remainder of the expression in which the function call is inserted |
|
311 * is treated as if the function call returns correctly, and therefore does not generate |
|
312 * spurious error messages. |
|
313 * Even if the parameters to the function call are invalid, doing this is still safe, as the |
|
314 * expressions inside the function call will themselves have erros and will guarantee that |
|
315 * compilation is aborted in stage3 (in print_datatypes_error_c). |
|
316 */ |
|
317 if (function_symtable.multiplicity(fcall_data.function_name) == 1) { |
|
318 f_decl = function_symtable.get_value(lower); |
|
319 returned_parameter_type = base_type(f_decl->type_name); |
|
320 if (add_datatype_to_candidate_list(fcall, returned_parameter_type)) |
|
321 /* we only add it to the function declaration list if this entry was not already present in the candidate datatype list! */ |
|
322 fcall_data.candidate_functions.push_back(f_decl); |
|
323 |
|
324 } |
|
325 for(; lower != upper; lower++) { |
|
326 bool compatible = false; |
|
327 |
|
328 f_decl = function_symtable.get_value(lower); |
|
329 /* Check if function declaration in symbol_table is compatible with parameters */ |
|
330 if (NULL != fcall_data.nonformal_operand_list) compatible=match_nonformal_call(fcall, f_decl); |
|
331 if (NULL != fcall_data. formal_operand_list) compatible= match_formal_call(fcall, f_decl); |
|
332 if (compatible) { |
|
333 /* Add the data type returned by the called functions. |
|
334 * However, only do this if this data type is not already present in the candidate_datatypes list_c |
|
335 */ |
|
336 returned_parameter_type = base_type(f_decl->type_name); |
|
337 if (add_datatype_to_candidate_list(fcall, returned_parameter_type)) |
|
338 /* we only add it to the function declaration list if this entry was not already present in the candidate datatype list! */ |
|
339 fcall_data.candidate_functions.push_back(f_decl); |
|
340 } |
|
341 } |
|
342 if (debug) std::cout << "end_function() [" << fcall->candidate_datatypes.size() << "] result.\n"; |
|
343 return; |
|
344 } |
|
345 |
|
346 |
|
347 /* handle implicit FB call in IL. |
|
348 * e.g. CLK ton_var |
|
349 * CU counter_var |
|
350 */ |
|
351 void *fill_candidate_datatypes_c::handle_implicit_il_fb_call(symbol_c *il_instruction, const char *param_name, symbol_c *&called_fb_declaration) { |
|
352 symbol_c *fb_type_id = search_varfb_instance_type->get_basetype_id(il_operand); |
|
353 /* Although a call to a non-declared FB is a semantic error, this is currently caught by stage 2! */ |
|
354 if (NULL == fb_type_id) ERROR; |
|
355 |
|
356 function_block_declaration_c *fb_decl = function_block_type_symtable.find_value(fb_type_id); |
|
357 if (function_block_type_symtable.end_value() == fb_decl) |
|
358 /* The il_operand is not the name of a FB instance. Most probably it is the name of a variable of some other type. |
|
359 * this is a semantic error. |
|
360 */ |
|
361 fb_decl = NULL; |
|
362 |
|
363 /* The narrow_candidate_datatypes_c does not rely on this called_fb_declaration pointer being == NULL to conclude that |
|
364 * we have a datatype incompatibility error, so we set it to fb_decl to allow the print_datatype_error_c to print out |
|
365 * more informative error messages! |
|
366 */ |
|
367 called_fb_declaration = fb_decl; |
|
368 |
|
369 /* This implicit FB call does not change the value stored in the current/default IL variable */ |
|
370 /* It does, however, require that the datatype be compatible with the input parameter of the FB being called. |
|
371 * If we were to follow the filling & narrowing algorithm correctly (implemented in fill_candidate_datatypes_c |
|
372 * & narrow_candidate_datatypes_c respectively), we should be restricting the candidate_datatpes to the datatypes |
|
373 * that are compatible to the FB call. |
|
374 * However, doing the above will often result in some very confusing error messages for the user, especially in the case |
|
375 * in which the FB call is wrong, so the resulting cadidate datatypes is an empty list. In this case, the user would see |
|
376 * many error messages related to the IL instructions that follow the FB call, even though those IL instructions may be perfectly |
|
377 * correct. |
|
378 * For now, we will simply let the narrow_candidate_datatypes_c verify if the datatypes are compatible (something that should be done |
|
379 * here). |
|
380 */ |
|
381 if (NULL != prev_il_instruction) |
|
382 il_instruction->candidate_datatypes = prev_il_instruction->candidate_datatypes; |
|
383 |
|
384 if (debug) std::cout << "handle_implicit_il_fb_call() [" << prev_il_instruction->candidate_datatypes.size() << "] ==> " << il_instruction->candidate_datatypes.size() << " result.\n"; |
|
385 return NULL; |
|
386 } |
|
387 |
|
388 |
|
389 |
|
390 |
|
391 /* handle a binary IL operator, like ADD, SUB, etc... */ |
|
392 void *fill_candidate_datatypes_c::handle_binary_operator(const struct widen_entry widen_table[], symbol_c *symbol, symbol_c *l_expr, symbol_c *r_expr) { |
|
393 if (NULL == l_expr) /* if no prev_il_instruction */ |
|
394 return NULL; |
|
395 |
|
396 for(unsigned int i = 0; i < l_expr->candidate_datatypes.size(); i++) |
|
397 for(unsigned int j = 0; j < r_expr->candidate_datatypes.size(); j++) |
|
398 /* NOTE: add_datatype_to_candidate_list() will only really add the datatype if it is != NULL !!! */ |
|
399 add_datatype_to_candidate_list(symbol, widening_conversion(l_expr->candidate_datatypes[i], r_expr->candidate_datatypes[j], widen_table)); |
|
400 remove_incompatible_datatypes(symbol); |
|
401 if (debug) std::cout << "[" << l_expr->candidate_datatypes.size() << "," << r_expr->candidate_datatypes.size() << "] ==> " << symbol->candidate_datatypes.size() << " result.\n"; |
|
402 return NULL; |
|
403 } |
|
404 |
|
405 |
|
406 /* handle a binary ST expression, like '+', '-', etc... */ |
|
407 void *fill_candidate_datatypes_c::handle_binary_expression(const struct widen_entry widen_table[], symbol_c *symbol, symbol_c *l_expr, symbol_c *r_expr) { |
|
408 l_expr->accept(*this); |
|
409 r_expr->accept(*this); |
|
410 return handle_binary_operator(widen_table, symbol, l_expr, r_expr); |
|
411 } |
|
412 |
|
413 |
|
414 |
|
415 |
|
416 /* a helper function... */ |
|
417 symbol_c *fill_candidate_datatypes_c::base_type(symbol_c *symbol) { |
|
418 /* NOTE: symbol == NULL is valid. It will occur when, for e.g., an undefined/undeclared symbolic_variable is used |
|
419 * in the code. |
|
420 */ |
|
421 if (symbol == NULL) return NULL; |
|
422 return (symbol_c *)symbol->accept(search_base_type); |
|
423 } |
|
424 |
|
425 /*********************/ |
|
426 /* B 1.2 - Constants */ |
|
427 /*********************/ |
|
428 /******************************/ |
|
429 /* B 1.2.1 - Numeric Literals */ |
|
430 /******************************/ |
|
431 #define sizeoftype(symbol) get_sizeof_datatype_c::getsize(symbol) |
|
432 |
|
433 void *fill_candidate_datatypes_c::handle_any_integer(symbol_c *symbol) { |
|
434 add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::bool_type_name, &search_constant_type_c::safebool_type_name); |
|
435 add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::byte_type_name, &search_constant_type_c::safebyte_type_name); |
|
436 add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::word_type_name, &search_constant_type_c::safeword_type_name); |
|
437 add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::dword_type_name, &search_constant_type_c::safedword_type_name); |
|
438 add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::lword_type_name, &search_constant_type_c::safelword_type_name); |
|
439 add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::sint_type_name, &search_constant_type_c::safesint_type_name); |
|
440 add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::int_type_name, &search_constant_type_c::safeint_type_name); |
|
441 add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::dint_type_name, &search_constant_type_c::safedint_type_name); |
|
442 add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::lint_type_name, &search_constant_type_c::safelint_type_name); |
|
443 add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::usint_type_name, &search_constant_type_c::safeusint_type_name); |
|
444 add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::uint_type_name, &search_constant_type_c::safeuint_type_name); |
|
445 add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::udint_type_name, &search_constant_type_c::safeudint_type_name); |
|
446 add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::ulint_type_name, &search_constant_type_c::safeulint_type_name); |
|
447 remove_incompatible_datatypes(symbol); |
|
448 if (debug) std::cout << "ANY_INT [" << symbol->candidate_datatypes.size()<< "]" << std::endl; |
|
449 return NULL; |
|
450 } |
|
451 |
|
452 |
|
453 |
|
454 void *fill_candidate_datatypes_c::handle_any_real(symbol_c *symbol) { |
|
455 add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::real_type_name, &search_constant_type_c::safereal_type_name); |
|
456 add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::lreal_type_name, &search_constant_type_c::safelreal_type_name); |
|
457 remove_incompatible_datatypes(symbol); |
|
458 if (debug) std::cout << "ANY_REAL [" << symbol->candidate_datatypes.size() << "]" << std::endl; |
|
459 return NULL; |
|
460 } |
|
461 |
|
462 |
|
463 |
|
464 void *fill_candidate_datatypes_c::handle_any_literal(symbol_c *symbol, symbol_c *symbol_value, symbol_c *symbol_type) { |
|
465 symbol_value->accept(*this); |
|
466 if (search_in_candidate_datatype_list(symbol_type, symbol_value->candidate_datatypes) >= 0) |
|
467 add_datatype_to_candidate_list(symbol, symbol_type); |
|
468 remove_incompatible_datatypes(symbol); |
|
469 if (debug) std::cout << "XXX_LITERAL [" << symbol->candidate_datatypes.size() << "]\n"; |
|
470 return NULL; |
|
471 } |
|
472 |
|
473 |
|
474 |
|
475 void *fill_candidate_datatypes_c::visit( real_c *symbol) {return handle_any_real(symbol);} |
|
476 void *fill_candidate_datatypes_c::visit(neg_real_c *symbol) {return handle_any_real(symbol);} |
|
477 |
|
478 |
|
479 |
|
480 void *fill_candidate_datatypes_c::visit(neg_integer_c *symbol) { |
|
481 add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::int_type_name, &search_constant_type_c::safeint_type_name); |
|
482 add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::sint_type_name, &search_constant_type_c::safesint_type_name); |
|
483 add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::dint_type_name, &search_constant_type_c::safedint_type_name); |
|
484 add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::lint_type_name, &search_constant_type_c::safelint_type_name); |
|
485 remove_incompatible_datatypes(symbol); |
|
486 if (debug) std::cout << "neg ANY_INT [" << symbol->candidate_datatypes.size() << "]" << std::endl; |
|
487 return NULL; |
|
488 } |
|
489 |
|
490 |
|
491 |
|
492 void *fill_candidate_datatypes_c::visit(integer_c *symbol) {return handle_any_integer(symbol);} |
|
493 void *fill_candidate_datatypes_c::visit(binary_integer_c *symbol) {return handle_any_integer(symbol);} |
|
494 void *fill_candidate_datatypes_c::visit(octal_integer_c *symbol) {return handle_any_integer(symbol);} |
|
495 void *fill_candidate_datatypes_c::visit(hex_integer_c *symbol) {return handle_any_integer(symbol);} |
|
496 |
|
497 |
|
498 |
|
499 // SYM_REF2(integer_literal_c, type, value) |
|
500 /* |
|
501 * integer_literal: |
|
502 * integer_type_name '#' signed_integer |
|
503 * | integer_type_name '#' binary_integer |
|
504 * | integer_type_name '#' octal_integer |
|
505 * | integer_type_name '#' hex_integer |
|
506 */ |
|
507 void *fill_candidate_datatypes_c::visit( integer_literal_c *symbol) {return handle_any_literal(symbol, symbol->value, symbol->type);} |
|
508 void *fill_candidate_datatypes_c::visit( real_literal_c *symbol) {return handle_any_literal(symbol, symbol->value, symbol->type);} |
|
509 void *fill_candidate_datatypes_c::visit(bit_string_literal_c *symbol) {return handle_any_literal(symbol, symbol->value, symbol->type);} |
|
510 |
|
511 void *fill_candidate_datatypes_c::visit( boolean_literal_c *symbol) { |
|
512 if (NULL != symbol->type) return handle_any_literal(symbol, symbol->value, symbol->type); |
|
513 |
|
514 symbol->value->accept(*this); |
|
515 symbol->candidate_datatypes = symbol->value->candidate_datatypes; |
|
516 return NULL; |
|
517 } |
|
518 |
|
519 |
|
520 void *fill_candidate_datatypes_c::visit(boolean_true_c *symbol) { |
|
521 add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::bool_type_name, &search_constant_type_c::safebool_type_name); |
|
522 return NULL; |
|
523 } |
|
524 |
|
525 void *fill_candidate_datatypes_c::visit(boolean_false_c *symbol) { |
|
526 add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::bool_type_name, &search_constant_type_c::safebool_type_name); |
|
527 return NULL; |
|
528 } |
|
529 |
|
530 /*******************************/ |
|
531 /* B.1.2.2 Character Strings */ |
|
532 /*******************************/ |
|
533 void *fill_candidate_datatypes_c::visit(double_byte_character_string_c *symbol) { |
|
534 add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::wstring_type_name, &search_constant_type_c::safewstring_type_name); |
|
535 return NULL; |
|
536 } |
|
537 |
|
538 void *fill_candidate_datatypes_c::visit(single_byte_character_string_c *symbol) { |
|
539 add_2datatypes_to_candidate_list(symbol, &search_constant_type_c::string_type_name, &search_constant_type_c::safestring_type_name); |
|
540 return NULL; |
|
541 } |
|
542 |
|
543 /***************************/ |
|
544 /* B 1.2.3 - Time Literals */ |
|
545 /***************************/ |
|
546 /************************/ |
|
547 /* B 1.2.3.1 - Duration */ |
|
548 /************************/ |
|
549 void *fill_candidate_datatypes_c::visit(duration_c *symbol) { |
|
550 /* TODO: check whether the literal follows the rules specified in section '2.2.3.1 Duration' of the standard! */ |
|
551 |
|
552 add_datatype_to_candidate_list(symbol, symbol->type_name); |
|
553 if (debug) std::cout << "TIME_LITERAL [" << symbol->candidate_datatypes.size() << "]\n"; |
|
554 return NULL; |
|
555 } |
|
556 |
|
557 /************************************/ |
|
558 /* B 1.2.3.2 - Time of day and Date */ |
|
559 /************************************/ |
|
560 void *fill_candidate_datatypes_c::visit(time_of_day_c *symbol) {add_datatype_to_candidate_list(symbol, symbol->type_name); return NULL;} |
|
561 void *fill_candidate_datatypes_c::visit(date_c *symbol) {add_datatype_to_candidate_list(symbol, symbol->type_name); return NULL;} |
|
562 void *fill_candidate_datatypes_c::visit(date_and_time_c *symbol) {add_datatype_to_candidate_list(symbol, symbol->type_name); return NULL;} |
|
563 |
|
564 /**********************/ |
|
565 /* B 1.3 - Data types */ |
|
566 /**********************/ |
|
567 /********************************/ |
|
568 /* B 1.3.3 - Derived data types */ |
|
569 /********************************/ |
|
570 |
|
571 /* simple_specification ASSIGN constant */ |
|
572 // SYM_REF2(simple_spec_init_c, simple_specification, constant) |
|
573 void *fill_candidate_datatypes_c::visit(simple_spec_init_c *symbol) { |
|
574 if (NULL != symbol->constant) symbol->constant->accept(*this); |
|
575 add_datatype_to_candidate_list(symbol->simple_specification, base_type(symbol->simple_specification)); |
|
576 symbol->candidate_datatypes = symbol->simple_specification->candidate_datatypes; |
|
577 /* NOTE: Even if the constant and the type are of incompatible data types, we let the |
|
578 * simple_spec_init_c object inherit the data type of the type declaration (simple_specification) |
|
579 * This will let us produce more informative error messages when checking data type compatibility |
|
580 * with located variables (AT %QW3.4 : WORD). |
|
581 */ |
|
582 // if (NULL != symbol->constant) intersect_candidate_datatype_list(symbol /*origin, dest.*/, symbol->constant /*with*/); |
|
583 return NULL; |
|
584 } |
|
585 |
|
586 /* signed_integer DOTDOT signed_integer */ |
|
587 // SYM_REF2(subrange_c, lower_limit, upper_limit) |
|
588 void *fill_candidate_datatypes_c::visit(subrange_c *symbol) { |
|
589 symbol->lower_limit->accept(*this); |
|
590 symbol->upper_limit->accept(*this); |
|
591 |
|
592 for (unsigned int u = 0; u < symbol->upper_limit->candidate_datatypes.size(); u++) { |
|
593 for(unsigned int l = 0; l < symbol->lower_limit->candidate_datatypes.size(); l++) { |
|
594 if (is_type_equal(symbol->upper_limit->candidate_datatypes[u], symbol->lower_limit->candidate_datatypes[l])) |
|
595 add_datatype_to_candidate_list(symbol, symbol->lower_limit->candidate_datatypes[l]); |
|
596 } |
|
597 } |
|
598 return NULL; |
|
599 } |
|
600 |
|
601 /* TYPE type_declaration_list END_TYPE */ |
|
602 // SYM_REF1(data_type_declaration_c, type_declaration_list) |
|
603 /* NOTE: Not required. already handled by iterator_visitor_c base class */ |
|
604 /* |
|
605 void *fill_candidate_datatypes_c::visit(data_type_declaration_c *symbol) { |
|
606 symbol->type_declaration_list->accept(*this); |
|
607 return NULL; |
|
608 } |
|
609 */ |
|
610 |
|
611 void *fill_candidate_datatypes_c::visit(enumerated_value_c *symbol) { |
|
612 symbol_c *enumerated_type; |
|
613 |
|
614 if (NULL != symbol->type) |
|
615 enumerated_type = symbol->type; |
|
616 else { |
|
617 enumerated_type = enumerated_value_symtable.find_value(symbol->value); |
|
618 if (enumerated_type == enumerated_value_symtable.end_value()) |
|
619 enumerated_type = NULL; |
|
620 } |
|
621 enumerated_type = base_type(enumerated_type); |
|
622 if (NULL != enumerated_type) |
|
623 add_datatype_to_candidate_list(symbol, enumerated_type); |
|
624 |
|
625 if (debug) std::cout << "ENUMERATE [" << symbol->candidate_datatypes.size() << "]\n"; |
|
626 return NULL; |
|
627 } |
|
628 |
|
629 |
|
630 /*********************/ |
|
631 /* B 1.4 - Variables */ |
|
632 /*********************/ |
|
633 void *fill_candidate_datatypes_c::visit(symbolic_variable_c *symbol) { |
|
634 add_datatype_to_candidate_list(symbol, search_varfb_instance_type->get_basetype_decl(symbol)); /* will only add if non NULL */ |
|
635 if (debug) std::cout << "VAR [" << symbol->candidate_datatypes.size() << "]\n"; |
|
636 return NULL; |
|
637 } |
|
638 |
|
639 |
|
640 /********************************************/ |
|
641 /* B 1.4.1 - Directly Represented Variables */ |
|
642 /********************************************/ |
|
643 void *fill_candidate_datatypes_c::visit(direct_variable_c *symbol) { |
|
644 /* Comment added by mario: |
|
645 * The following code is safe, actually, as the lexical parser guarantees the correct IEC61131-3 syntax was used. |
|
646 */ |
|
647 /* However, we should probably add an assertion in case we later change the lexical parser! */ |
|
648 /* if (symbol->value == NULL) ERROR; |
|
649 * if (symbol->value[0] == '\0') ERROR; |
|
650 * if (symbol->value[1] == '\0') ERROR; |
|
651 */ |
|
652 switch (symbol->value[2]) { |
|
653 case 'x': case 'X': /* bit - 1 bit */ add_datatype_to_candidate_list(symbol, &search_constant_type_c::bool_type_name); break; |
|
654 case 'b': case 'B': /* byte - 8 bits */ add_datatype_to_candidate_list(symbol, &search_constant_type_c::byte_type_name); break; |
|
655 case 'w': case 'W': /* word - 16 bits */ add_datatype_to_candidate_list(symbol, &search_constant_type_c::word_type_name); break; |
|
656 case 'd': case 'D': /* dword - 32 bits */ add_datatype_to_candidate_list(symbol, &search_constant_type_c::dword_type_name); break; |
|
657 case 'l': case 'L': /* lword - 64 bits */ add_datatype_to_candidate_list(symbol, &search_constant_type_c::lword_type_name); break; |
|
658 /* if none of the above, then the empty string was used <=> boolean */ |
|
659 default: add_datatype_to_candidate_list(symbol, &search_constant_type_c::bool_type_name); break; |
|
660 } |
|
661 return NULL; |
|
662 } |
|
663 |
|
664 /*************************************/ |
|
665 /* B 1.4.2 - Multi-element variables */ |
|
666 /*************************************/ |
|
667 /* subscripted_variable '[' subscript_list ']' */ |
|
668 // SYM_REF2(array_variable_c, subscripted_variable, subscript_list) |
|
669 void *fill_candidate_datatypes_c::visit(array_variable_c *symbol) { |
|
670 /* get the declaration of the data type __stored__ in the array... */ |
|
671 /* if we were to want the data type of the array itself, then we should call_param_name |
|
672 * search_varfb_instance_type->get_basetype_decl(symbol->subscripted_variable) |
|
673 */ |
|
674 symbol_c *result = search_varfb_instance_type->get_basetype_decl(symbol); |
|
675 if (NULL != result) add_datatype_to_candidate_list(symbol, result); |
|
676 |
|
677 /* recursively call the subscript list, so we can check the data types of the expressions used for the subscripts */ |
|
678 symbol->subscript_list->accept(*this); |
|
679 |
|
680 if (debug) std::cout << "ARRAY_VAR [" << symbol->candidate_datatypes.size() << "]\n"; |
|
681 return NULL; |
|
682 } |
|
683 |
|
684 |
|
685 /* subscript_list ',' subscript */ |
|
686 // SYM_LIST(subscript_list_c) |
|
687 /* NOTE: we inherit from iterator visitor, so we do not need to implement this method... */ |
|
688 // void *fill_candidate_datatypes_c::visit(subscript_list_c *symbol) |
|
689 |
|
690 |
|
691 /* record_variable '.' field_selector */ |
|
692 /* WARNING: input and/or output variables of function blocks |
|
693 * may be accessed as fields of a structured variable! |
|
694 * Code handling a structured_variable_c must take |
|
695 * this into account! |
|
696 */ |
|
697 // SYM_REF2(structured_variable_c, record_variable, field_selector) |
|
698 /* NOTE: We do not need to recursively determine the data types of each field_selector, as the search_varfb_instance_type |
|
699 * will do that for us. So we determine the candidate datatypes only for the full structured_variable. |
|
700 */ |
|
701 void *fill_candidate_datatypes_c::visit(structured_variable_c *symbol) { |
|
702 add_datatype_to_candidate_list(symbol, search_varfb_instance_type->get_basetype_decl(symbol)); /* will only add if non NULL */ |
|
703 return NULL; |
|
704 } |
|
705 |
|
706 |
|
707 |
|
708 /******************************************/ |
|
709 /* B 1.4.3 - Declaration & Initialisation */ |
|
710 /******************************************/ |
|
711 |
|
712 void *fill_candidate_datatypes_c::visit(var1_list_c *symbol) { |
|
713 #if 0 /* We don't really need to set the datatype of each variable. We just check the declaration itself! */ |
|
714 for(int i = 0; i < symbol->n; i++) { |
|
715 add_datatype_to_candidate_list(symbol->elements[i], search_varfb_instance_type->get_basetype_decl(symbol->elements[i])); /* will only add if non NULL */ |
|
716 } |
|
717 #endif |
|
718 return NULL; |
|
719 } |
|
720 |
|
721 |
|
722 /* AT direct_variable */ |
|
723 // SYM_REF1(location_c, direct_variable) |
|
724 void *fill_candidate_datatypes_c::visit(location_c *symbol) { |
|
725 /* This is a special situation. |
|
726 * |
|
727 * The reason is that a located variable may be declared to be of any data type, as long as the size |
|
728 * matches the location (lines 1 3 and 4 of table 17). For example: |
|
729 * var1 AT %MB42.0 : BYTE; |
|
730 * var1 AT %MB42.1 : SINT; |
|
731 * var1 AT %MB42.2 : USINT; |
|
732 * var1 AT %MW64 : INT; |
|
733 * var1 AT %MD56 : DINT; |
|
734 * var1 AT %MD57 : REAL; |
|
735 * are all valid!! |
|
736 * |
|
737 * However, when used inside an expression, the direct variable (uses the same syntax as the location |
|
738 * of a located variable) is limited to the following (ANY_BIT) data types: |
|
739 * %MX --> BOOL |
|
740 * %MB --> BYTE |
|
741 * %MW --> WORD |
|
742 * %MD --> DWORD |
|
743 * %ML --> LWORD |
|
744 * |
|
745 * So, in order to be able to analyse expressions with direct variables |
|
746 * e.g: var1 := 66 OR %MW34 |
|
747 * where the direct variable may only take the ANY_BIT data types, the fill_candidate_datatypes_c |
|
748 * considers that only the ANY_BIT data types are allowed for a direct variable. |
|
749 * However, it appears from the examples in the standard (lines 1 3 and 4 of table 17) |
|
750 * a location may have any data type (presumably as long as the size in bits match). |
|
751 * For this reason, a location_c may have more allowable data types than a direct_variable_c |
|
752 */ |
|
753 |
|
754 symbol->direct_variable->accept(*this); |
|
755 for (unsigned int i = 0; i < symbol->direct_variable->candidate_datatypes.size(); i++) { |
|
756 switch (get_sizeof_datatype_c::getsize(symbol->direct_variable->candidate_datatypes[i])) { |
|
757 case 1: /* bit - 1 bit */ |
|
758 add_datatype_to_candidate_list(symbol, &search_constant_type_c::bool_type_name); |
|
759 add_datatype_to_candidate_list(symbol, &search_constant_type_c::safebool_type_name); |
|
760 break; |
|
761 case 8: /* byte - 8 bits */ |
|
762 add_datatype_to_candidate_list(symbol, &search_constant_type_c::byte_type_name); |
|
763 add_datatype_to_candidate_list(symbol, &search_constant_type_c::safebyte_type_name); |
|
764 add_datatype_to_candidate_list(symbol, &search_constant_type_c::sint_type_name); |
|
765 add_datatype_to_candidate_list(symbol, &search_constant_type_c::safesint_type_name); |
|
766 add_datatype_to_candidate_list(symbol, &search_constant_type_c::usint_type_name); |
|
767 add_datatype_to_candidate_list(symbol, &search_constant_type_c::safeusint_type_name); |
|
768 break; |
|
769 case 16: /* word - 16 bits */ |
|
770 add_datatype_to_candidate_list(symbol, &search_constant_type_c::word_type_name); |
|
771 add_datatype_to_candidate_list(symbol, &search_constant_type_c::safeword_type_name); |
|
772 add_datatype_to_candidate_list(symbol, &search_constant_type_c::int_type_name); |
|
773 add_datatype_to_candidate_list(symbol, &search_constant_type_c::safeint_type_name); |
|
774 add_datatype_to_candidate_list(symbol, &search_constant_type_c::uint_type_name); |
|
775 add_datatype_to_candidate_list(symbol, &search_constant_type_c::safeuint_type_name); |
|
776 break; |
|
777 case 32: /* dword - 32 bits */ |
|
778 add_datatype_to_candidate_list(symbol, &search_constant_type_c::dword_type_name); |
|
779 add_datatype_to_candidate_list(symbol, &search_constant_type_c::safedword_type_name); |
|
780 add_datatype_to_candidate_list(symbol, &search_constant_type_c::dint_type_name); |
|
781 add_datatype_to_candidate_list(symbol, &search_constant_type_c::safedint_type_name); |
|
782 add_datatype_to_candidate_list(symbol, &search_constant_type_c::udint_type_name); |
|
783 add_datatype_to_candidate_list(symbol, &search_constant_type_c::safeudint_type_name); |
|
784 add_datatype_to_candidate_list(symbol, &search_constant_type_c::real_type_name); |
|
785 add_datatype_to_candidate_list(symbol, &search_constant_type_c::safereal_type_name); |
|
786 break; |
|
787 case 64: /* lword - 64 bits */ |
|
788 add_datatype_to_candidate_list(symbol, &search_constant_type_c::lword_type_name); |
|
789 add_datatype_to_candidate_list(symbol, &search_constant_type_c::safelword_type_name); |
|
790 add_datatype_to_candidate_list(symbol, &search_constant_type_c::lint_type_name); |
|
791 add_datatype_to_candidate_list(symbol, &search_constant_type_c::safelint_type_name); |
|
792 add_datatype_to_candidate_list(symbol, &search_constant_type_c::ulint_type_name); |
|
793 add_datatype_to_candidate_list(symbol, &search_constant_type_c::safeulint_type_name); |
|
794 add_datatype_to_candidate_list(symbol, &search_constant_type_c::lreal_type_name); |
|
795 add_datatype_to_candidate_list(symbol, &search_constant_type_c::safelreal_type_name); |
|
796 break; |
|
797 default: /* if none of the above, then no valid datatype allowed... */ |
|
798 break; |
|
799 } /* switch() */ |
|
800 } /* for */ |
|
801 |
|
802 return NULL; |
|
803 } |
|
804 |
|
805 |
|
806 /* [variable_name] location ':' located_var_spec_init */ |
|
807 /* variable_name -> may be NULL ! */ |
|
808 // SYM_REF3(located_var_decl_c, variable_name, location, located_var_spec_init) |
|
809 void *fill_candidate_datatypes_c::visit(located_var_decl_c *symbol) { |
|
810 symbol->located_var_spec_init->accept(*this); |
|
811 symbol->location->accept(*this); |
|
812 if (NULL != symbol->variable_name) { |
|
813 symbol->variable_name->candidate_datatypes = symbol->location->candidate_datatypes; |
|
814 intersect_candidate_datatype_list(symbol->variable_name /*origin, dest.*/, symbol->located_var_spec_init /*with*/); |
|
815 } |
|
816 return NULL; |
|
817 } |
|
818 |
|
819 |
|
820 |
|
821 |
|
822 |
|
823 /************************************/ |
|
824 /* B 1.5 Program organization units */ |
|
825 /************************************/ |
|
826 /*********************/ |
|
827 /* B 1.5.1 Functions */ |
|
828 /*********************/ |
|
829 void *fill_candidate_datatypes_c::visit(function_declaration_c *symbol) { |
|
830 if (debug) printf("Filling candidate data types list of function %s\n", ((token_c *)(symbol->derived_function_name))->value); |
|
831 search_varfb_instance_type = new search_varfb_instance_type_c(symbol); |
|
832 symbol->var_declarations_list->accept(*this); |
|
833 symbol->function_body->accept(*this); |
|
834 delete search_varfb_instance_type; |
|
835 search_varfb_instance_type = NULL; |
|
836 return NULL; |
|
837 } |
|
838 |
|
839 /***************************/ |
|
840 /* B 1.5.2 Function blocks */ |
|
841 /***************************/ |
|
842 void *fill_candidate_datatypes_c::visit(function_block_declaration_c *symbol) { |
|
843 if (debug) printf("Filling candidate data types list of FB %s\n", ((token_c *)(symbol->fblock_name))->value); |
|
844 search_varfb_instance_type = new search_varfb_instance_type_c(symbol); |
|
845 symbol->var_declarations->accept(*this); |
|
846 symbol->fblock_body->accept(*this); |
|
847 delete search_varfb_instance_type; |
|
848 search_varfb_instance_type = NULL; |
|
849 return NULL; |
|
850 } |
|
851 |
|
852 /**********************/ |
|
853 /* B 1.5.3 - Programs */ |
|
854 /**********************/ |
|
855 void *fill_candidate_datatypes_c::visit(program_declaration_c *symbol) { |
|
856 if (debug) printf("Filling candidate data types list in program %s\n", ((token_c *)(symbol->program_type_name))->value); |
|
857 search_varfb_instance_type = new search_varfb_instance_type_c(symbol); |
|
858 symbol->var_declarations->accept(*this); |
|
859 symbol->function_block_body->accept(*this); |
|
860 delete search_varfb_instance_type; |
|
861 search_varfb_instance_type = NULL; |
|
862 return NULL; |
|
863 } |
|
864 |
|
865 |
|
866 |
|
867 /********************************/ |
|
868 /* B 1.7 Configuration elements */ |
|
869 /********************************/ |
|
870 void *fill_candidate_datatypes_c::visit(configuration_declaration_c *symbol) { |
|
871 // TODO !!! |
|
872 /* for the moment we must return NULL so semantic analysis of remaining code is not interrupted! */ |
|
873 return NULL; |
|
874 } |
|
875 |
|
876 /****************************************/ |
|
877 /* B.2 - Language IL (Instruction List) */ |
|
878 /****************************************/ |
|
879 /***********************************/ |
|
880 /* B 2.1 Instructions and Operands */ |
|
881 /***********************************/ |
|
882 |
|
883 /*| instruction_list il_instruction */ |
|
884 // SYM_LIST(instruction_list_c) |
|
885 void *fill_candidate_datatypes_c::visit(instruction_list_c *symbol) { |
|
886 /* In order to fill the data type candidates correctly |
|
887 * in IL instruction lists containing JMPs to labels that come before the JMP instruction |
|
888 * itself, we need to run the fill candidate datatypes algorithm twice on the Instruction List. |
|
889 * e.g.: ... |
|
890 * ld 23 |
|
891 * label1:st byte_var |
|
892 * ld 34 |
|
893 * JMP label1 |
|
894 * |
|
895 * Note that the second time we run the algorithm, most of the candidate datatypes are already filled |
|
896 * in, so it will be able to produce tha correct candidate datatypes for the IL instruction referenced |
|
897 * by the label, as in the 2nd pass we already know the candidate datatypes of the JMP instruction! |
|
898 */ |
|
899 for(int j = 0; j < 2; j++) { |
|
900 for(int i = 0; i < symbol->n; i++) { |
|
901 symbol->elements[i]->accept(*this); |
|
902 } |
|
903 } |
|
904 return NULL; |
|
905 } |
|
906 |
|
907 |
|
908 |
|
909 /* | label ':' [il_incomplete_instruction] eol_list */ |
|
910 // SYM_REF2(il_instruction_c, label, il_instruction) |
|
911 // void *visit(instruction_list_c *symbol); |
|
912 void *fill_candidate_datatypes_c::visit(il_instruction_c *symbol) { |
|
913 if (NULL == symbol->il_instruction) { |
|
914 /* This empty/null il_instruction does not change the value of the current/default IL variable. |
|
915 * So it inherits the candidate_datatypes from it's previous IL instructions! |
|
916 */ |
|
917 intersect_prev_candidate_datatype_lists(symbol); |
|
918 } else { |
|
919 il_instruction_c fake_prev_il_instruction = *symbol; |
|
920 intersect_prev_candidate_datatype_lists(&fake_prev_il_instruction); |
|
921 |
|
922 if (symbol->prev_il_instruction.size() == 0) prev_il_instruction = NULL; |
|
923 else prev_il_instruction = &fake_prev_il_instruction; |
|
924 symbol->il_instruction->accept(*this); |
|
925 prev_il_instruction = NULL; |
|
926 |
|
927 /* This object has (inherits) the same candidate datatypes as the il_instruction */ |
|
928 symbol->candidate_datatypes = symbol->il_instruction->candidate_datatypes; |
|
929 } |
|
930 |
|
931 return NULL; |
|
932 } |
|
933 |
|
934 |
|
935 |
|
936 void *fill_candidate_datatypes_c::visit(il_simple_operation_c *symbol) { |
|
937 /* determine the data type of the operand */ |
|
938 if (NULL != symbol->il_operand) { |
|
939 symbol->il_operand->accept(*this); |
|
940 } |
|
941 /* recursive call to fill the candidate data types list */ |
|
942 il_operand = symbol->il_operand; |
|
943 symbol->il_simple_operator->accept(*this); |
|
944 il_operand = NULL; |
|
945 /* This object has (inherits) the same candidate datatypes as the il_simple_operator */ |
|
946 symbol->candidate_datatypes = symbol->il_simple_operator->candidate_datatypes; |
|
947 return NULL; |
|
948 } |
|
949 |
|
950 |
|
951 /* | function_name [il_operand_list] */ |
|
952 /* NOTE: The parameters 'called_function_declaration' and 'extensible_param_count' are used to pass data between the stage 3 and stage 4. */ |
|
953 // SYM_REF2(il_function_call_c, function_name, il_operand_list, symbol_c *called_function_declaration; int extensible_param_count;) |
|
954 void *fill_candidate_datatypes_c::visit(il_function_call_c *symbol) { |
|
955 /* The first parameter of a non formal function call in IL will be the 'current value' (i.e. the prev_il_instruction) |
|
956 * In order to be able to handle this without coding special cases, we will simply prepend that symbol |
|
957 * to the il_operand_list, and remove it after calling handle_function_call(). |
|
958 * |
|
959 * However, if no further paramters are given, then il_operand_list will be NULL, and we will |
|
960 * need to create a new object to hold the pointer to prev_il_instruction. |
|
961 */ |
|
962 if (NULL == symbol->il_operand_list) symbol->il_operand_list = new il_operand_list_c; |
|
963 if (NULL == symbol->il_operand_list) ERROR; |
|
964 |
|
965 symbol->il_operand_list->accept(*this); |
|
966 |
|
967 if (NULL != prev_il_instruction) { |
|
968 ((list_c *)symbol->il_operand_list)->insert_element(prev_il_instruction, 0); |
|
969 |
|
970 generic_function_call_t fcall_param = { |
|
971 /* fcall_param.function_name = */ symbol->function_name, |
|
972 /* fcall_param.nonformal_operand_list = */ symbol->il_operand_list, |
|
973 /* fcall_param.formal_operand_list = */ NULL, |
|
974 /* enum {POU_FB, POU_function} POU_type = */ generic_function_call_t::POU_function, |
|
975 /* fcall_param.candidate_functions = */ symbol->candidate_functions, |
|
976 /* fcall_param.called_function_declaration = */ symbol->called_function_declaration, |
|
977 /* fcall_param.extensible_param_count = */ symbol->extensible_param_count |
|
978 }; |
|
979 handle_function_call(symbol, fcall_param); |
|
980 |
|
981 /* Undo the changes to the abstract syntax tree we made above... */ |
|
982 ((list_c *)symbol->il_operand_list)->remove_element(0); |
|
983 } |
|
984 |
|
985 /* Undo the changes to the abstract syntax tree we made above... */ |
|
986 if (((list_c *)symbol->il_operand_list)->n == 0) { |
|
987 /* if the list becomes empty, then that means that it did not exist before we made these changes, so we delete it! */ |
|
988 delete symbol->il_operand_list; |
|
989 symbol->il_operand_list = NULL; |
|
990 } |
|
991 |
|
992 if (debug) std::cout << "il_function_call_c [" << symbol->candidate_datatypes.size() << "] result.\n"; |
|
993 return NULL; |
|
994 } |
|
995 |
|
996 |
|
997 /* | il_expr_operator '(' [il_operand] eol_list [simple_instr_list] ')' */ |
|
998 // SYM_REF3(il_expression_c, il_expr_operator, il_operand, simple_instr_list); |
|
999 void *fill_candidate_datatypes_c::visit(il_expression_c *symbol) { |
|
1000 symbol_c *prev_il_instruction_backup = prev_il_instruction; |
|
1001 |
|
1002 if (NULL != symbol->il_operand) |
|
1003 symbol->il_operand->accept(*this); |
|
1004 |
|
1005 if(symbol->simple_instr_list != NULL) |
|
1006 symbol->simple_instr_list->accept(*this); |
|
1007 |
|
1008 /* Now check the if the data type semantics of operation are correct, */ |
|
1009 il_operand = symbol->simple_instr_list; |
|
1010 prev_il_instruction = prev_il_instruction_backup; |
|
1011 symbol->il_expr_operator->accept(*this); |
|
1012 il_operand = NULL; |
|
1013 |
|
1014 /* This object has the same candidate datatypes as the il_expr_operator. */ |
|
1015 symbol->candidate_datatypes = symbol->il_expr_operator->candidate_datatypes; |
|
1016 return NULL; |
|
1017 } |
|
1018 |
|
1019 |
|
1020 void *fill_candidate_datatypes_c::visit(il_jump_operation_c *symbol) { |
|
1021 /* recursive call to fill the candidate data types list */ |
|
1022 il_operand = NULL; |
|
1023 symbol->il_jump_operator->accept(*this); |
|
1024 il_operand = NULL; |
|
1025 /* This object has the same candidate datatypes as the il_jump_operator. */ |
|
1026 symbol->candidate_datatypes = symbol->il_jump_operator->candidate_datatypes; |
|
1027 return NULL; |
|
1028 } |
|
1029 |
|
1030 |
|
1031 /* il_call_operator prev_declared_fb_name |
|
1032 * | il_call_operator prev_declared_fb_name '(' ')' |
|
1033 * | il_call_operator prev_declared_fb_name '(' eol_list ')' |
|
1034 * | il_call_operator prev_declared_fb_name '(' il_operand_list ')' |
|
1035 * | il_call_operator prev_declared_fb_name '(' eol_list il_param_list ')' |
|
1036 */ |
|
1037 /* 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 */ |
|
1038 // SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list, symbol_c *called_fb_declaration) |
|
1039 void *fill_candidate_datatypes_c::visit(il_fb_call_c *symbol) { |
|
1040 /* We do not call |
|
1041 * fb_decl = search_varfb_instance_type->get_basetype_decl(symbol->fb_name); |
|
1042 * because we want to make sure it is a FB instance, and not some other data type... |
|
1043 */ |
|
1044 symbol_c *fb_type_id = search_varfb_instance_type->get_basetype_id(symbol->fb_name); |
|
1045 /* Although a call to a non-declared FB is a semantic error, this is currently caught by stage 2! */ |
|
1046 if (NULL == fb_type_id) ERROR; |
|
1047 |
|
1048 function_block_declaration_c *fb_decl = function_block_type_symtable.find_value(fb_type_id); |
|
1049 if (function_block_type_symtable.end_value() == fb_decl) |
|
1050 /* The fb_name not the name of a FB instance. Most probably it is the name of a variable of some other type. */ |
|
1051 fb_decl = NULL; |
|
1052 |
|
1053 /* Although a call to a non-declared FB is a semantic error, this is currently caught by stage 2! */ |
|
1054 if (NULL == fb_decl) ERROR; |
|
1055 |
|
1056 if (symbol-> il_param_list != NULL) symbol->il_param_list->accept(*this); |
|
1057 if (symbol->il_operand_list != NULL) symbol->il_operand_list->accept(*this); |
|
1058 |
|
1059 /* The print_datatypes_error_c does not rely on this called_fb_declaration pointer being != NULL to conclude that |
|
1060 * we have a datat type incompatibility error, so setting it to the correct fb_decl is actually safe, |
|
1061 * as the compiler will never reach the compilation stage! |
|
1062 */ |
|
1063 symbol->called_fb_declaration = fb_decl; |
|
1064 |
|
1065 /* Let the il_call_operator (CAL, CALC, or CALCN) determine the candidate datatypes of the il_fb_call_c... */ |
|
1066 /* NOTE: We ignore whether the call is 'compatible' or not when filling in the candidate datatypes list. |
|
1067 * Even if it is not compatible, we fill in the candidate datatypes list correctly so that the following |
|
1068 * IL instructions may be handled correctly and debuged. |
|
1069 * Doing this is actually safe, as the parameter_list will still contain errors that will be found by |
|
1070 * print_datatypes_error_c, so the code will never reach stage 4! |
|
1071 */ |
|
1072 symbol->il_call_operator->accept(*this); |
|
1073 symbol->candidate_datatypes = symbol->il_call_operator->candidate_datatypes; |
|
1074 |
|
1075 if (debug) std::cout << "FB [] ==> " << symbol->candidate_datatypes.size() << " result.\n"; |
|
1076 return NULL; |
|
1077 } |
|
1078 |
|
1079 |
|
1080 /* | function_name '(' eol_list [il_param_list] ')' */ |
|
1081 /* NOTE: The parameter 'called_function_declaration' is used to pass data between the stage 3 and stage 4. */ |
|
1082 // SYM_REF2(il_formal_funct_call_c, function_name, il_param_list, symbol_c *called_function_declaration; int extensible_param_count;) |
|
1083 void *fill_candidate_datatypes_c::visit(il_formal_funct_call_c *symbol) { |
|
1084 symbol->il_param_list->accept(*this); |
|
1085 |
|
1086 generic_function_call_t fcall_param = { |
|
1087 /* fcall_param.function_name = */ symbol->function_name, |
|
1088 /* fcall_param.nonformal_operand_list = */ NULL, |
|
1089 /* fcall_param.formal_operand_list = */ symbol->il_param_list, |
|
1090 /* enum {POU_FB, POU_function} POU_type = */ generic_function_call_t::POU_function, |
|
1091 /* fcall_param.candidate_functions = */ symbol->candidate_functions, |
|
1092 /* fcall_param.called_function_declaration = */ symbol->called_function_declaration, |
|
1093 /* fcall_param.extensible_param_count = */ symbol->extensible_param_count |
|
1094 }; |
|
1095 handle_function_call(symbol, fcall_param); |
|
1096 |
|
1097 if (debug) std::cout << "il_formal_funct_call_c [" << symbol->candidate_datatypes.size() << "] result.\n"; |
|
1098 return NULL; |
|
1099 } |
|
1100 |
|
1101 |
|
1102 // void *visit(il_operand_list_c *symbol); |
|
1103 |
|
1104 |
|
1105 /* | simple_instr_list il_simple_instruction */ |
|
1106 /* This object is referenced by il_expression_c objects */ |
|
1107 void *fill_candidate_datatypes_c::visit(simple_instr_list_c *symbol) { |
|
1108 if (symbol->n <= 0) |
|
1109 return NULL; /* List is empty! Nothing to do. */ |
|
1110 |
|
1111 for(int i = 0; i < symbol->n; i++) |
|
1112 symbol->elements[i]->accept(*this); |
|
1113 |
|
1114 /* This object has (inherits) the same candidate datatypes as the last il_instruction */ |
|
1115 symbol->candidate_datatypes = symbol->elements[symbol->n-1]->candidate_datatypes; |
|
1116 |
|
1117 if (debug) std::cout << "simple_instr_list_c [" << symbol->candidate_datatypes.size() << "] result.\n"; |
|
1118 return NULL; |
|
1119 } |
|
1120 |
|
1121 |
|
1122 |
|
1123 |
|
1124 // SYM_REF1(il_simple_instruction_c, il_simple_instruction, symbol_c *prev_il_instruction;) |
|
1125 void *fill_candidate_datatypes_c::visit(il_simple_instruction_c *symbol) { |
|
1126 if (symbol->prev_il_instruction.size() > 1) ERROR; /* There should be no labeled insructions inside an IL expression! */ |
|
1127 if (symbol->prev_il_instruction.size() == 0) prev_il_instruction = NULL; |
|
1128 else prev_il_instruction = symbol->prev_il_instruction[0]; |
|
1129 symbol->il_simple_instruction->accept(*this); |
|
1130 prev_il_instruction = NULL; |
|
1131 |
|
1132 /* This object has (inherits) the same candidate datatypes as the il_simple_instruction it points to */ |
|
1133 symbol->candidate_datatypes = symbol->il_simple_instruction->candidate_datatypes; |
|
1134 return NULL; |
|
1135 } |
|
1136 |
|
1137 |
|
1138 /* |
|
1139 void *visit(il_param_list_c *symbol); |
|
1140 void *visit(il_param_assignment_c *symbol); |
|
1141 void *visit(il_param_out_assignment_c *symbol); |
|
1142 */ |
|
1143 |
|
1144 /*******************/ |
|
1145 /* B 2.2 Operators */ |
|
1146 /*******************/ |
|
1147 void *fill_candidate_datatypes_c::visit(LD_operator_c *symbol) { |
|
1148 for(unsigned int i = 0; i < il_operand->candidate_datatypes.size(); i++) { |
|
1149 add_datatype_to_candidate_list(symbol, il_operand->candidate_datatypes[i]); |
|
1150 } |
|
1151 if (debug) std::cout << "LD [" << il_operand->candidate_datatypes.size() << "] ==> " << symbol->candidate_datatypes.size() << " result.\n"; |
|
1152 return NULL; |
|
1153 } |
|
1154 |
|
1155 void *fill_candidate_datatypes_c::visit(LDN_operator_c *symbol) { |
|
1156 for(unsigned int i = 0; i < il_operand->candidate_datatypes.size(); i++) { |
|
1157 if (is_ANY_BIT_compatible(il_operand->candidate_datatypes[i])) |
|
1158 add_datatype_to_candidate_list(symbol, il_operand->candidate_datatypes[i]); |
|
1159 } |
|
1160 if (debug) std::cout << "LDN [" << il_operand->candidate_datatypes.size() << "] ==> " << symbol->candidate_datatypes.size() << " result.\n"; |
|
1161 return NULL; |
|
1162 } |
|
1163 |
|
1164 void *fill_candidate_datatypes_c::visit(ST_operator_c *symbol) { |
|
1165 symbol_c *prev_instruction_type, *operand_type; |
|
1166 |
|
1167 if (NULL == prev_il_instruction) return NULL; |
|
1168 for (unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) { |
|
1169 for(unsigned int j = 0; j < il_operand->candidate_datatypes.size(); j++) { |
|
1170 prev_instruction_type = prev_il_instruction->candidate_datatypes[i]; |
|
1171 operand_type = il_operand->candidate_datatypes[j]; |
|
1172 if (is_type_equal(prev_instruction_type, operand_type)) |
|
1173 add_datatype_to_candidate_list(symbol, prev_instruction_type); |
|
1174 } |
|
1175 } |
|
1176 if (debug) std::cout << "ST [" << prev_il_instruction->candidate_datatypes.size() << "," << il_operand->candidate_datatypes.size() << "] ==> " << symbol->candidate_datatypes.size() << " result.\n"; |
|
1177 return NULL; |
|
1178 } |
|
1179 |
|
1180 void *fill_candidate_datatypes_c::visit(STN_operator_c *symbol) { |
|
1181 symbol_c *prev_instruction_type, *operand_type; |
|
1182 |
|
1183 if (NULL == prev_il_instruction) return NULL; |
|
1184 for (unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) { |
|
1185 for(unsigned int j = 0; j < il_operand->candidate_datatypes.size(); j++) { |
|
1186 prev_instruction_type = prev_il_instruction->candidate_datatypes[i]; |
|
1187 operand_type = il_operand->candidate_datatypes[j]; |
|
1188 if (is_type_equal(prev_instruction_type,operand_type) && is_ANY_BIT_compatible(operand_type)) |
|
1189 add_datatype_to_candidate_list(symbol, prev_instruction_type); |
|
1190 } |
|
1191 } |
|
1192 if (debug) std::cout << "STN [" << prev_il_instruction->candidate_datatypes.size() << "," << il_operand->candidate_datatypes.size() << "] ==> " << symbol->candidate_datatypes.size() << " result.\n"; |
|
1193 return NULL; |
|
1194 } |
|
1195 |
|
1196 void *fill_candidate_datatypes_c::visit(NOT_operator_c *symbol) { |
|
1197 /* NOTE: the standard allows syntax in which the NOT operator is followed by an optional <il_operand> |
|
1198 * NOT [<il_operand>] |
|
1199 * However, it does not define the semantic of the NOT operation when the <il_operand> is specified. |
|
1200 * We therefore consider it an error if an il_operand is specified! |
|
1201 * We do not need to generate an error message. This error will be caught somewhere else! |
|
1202 */ |
|
1203 if (NULL == prev_il_instruction) return NULL; |
|
1204 for (unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) { |
|
1205 if (is_ANY_BIT_compatible(prev_il_instruction->candidate_datatypes[i])) |
|
1206 add_datatype_to_candidate_list(symbol, prev_il_instruction->candidate_datatypes[i]); |
|
1207 } |
|
1208 if (debug) std::cout << "NOT_operator [" << prev_il_instruction->candidate_datatypes.size() << "] ==> " << symbol->candidate_datatypes.size() << " result.\n"; |
|
1209 return NULL; |
|
1210 } |
|
1211 |
|
1212 |
|
1213 void *fill_candidate_datatypes_c::visit(S_operator_c *symbol) { |
|
1214 /* TODO: what if this is a FB call ?? */ |
|
1215 symbol_c *prev_instruction_type, *operand_type; |
|
1216 |
|
1217 if (NULL == prev_il_instruction) return NULL; |
|
1218 for (unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) { |
|
1219 for(unsigned int j = 0; j < il_operand->candidate_datatypes.size(); j++) { |
|
1220 prev_instruction_type = prev_il_instruction->candidate_datatypes[i]; |
|
1221 operand_type = il_operand->candidate_datatypes[j]; |
|
1222 /* TODO: I believe the following is wrong! The data types of prev_instruction_type and operand_type DO NOT have to be equal. |
|
1223 * the prev_instruction_type MUST be BOOL compatible. |
|
1224 * I am not too sure about operand_type, does it have to be BOOL compatible, or can it be ANY_BIT compatible? Must check! |
|
1225 */ |
|
1226 if (is_type_equal(prev_instruction_type,operand_type) && is_ANY_BOOL_compatible(operand_type)) |
|
1227 add_datatype_to_candidate_list(symbol, prev_instruction_type); |
|
1228 } |
|
1229 } |
|
1230 if (debug) std::cout << "S [" << prev_il_instruction->candidate_datatypes.size() << "," << il_operand->candidate_datatypes.size() << "] ==> " << symbol->candidate_datatypes.size() << " result.\n"; |
|
1231 return NULL; |
|
1232 } |
|
1233 |
|
1234 |
|
1235 void *fill_candidate_datatypes_c::visit(R_operator_c *symbol) { |
|
1236 /* TODO: what if this is a FB call ?? */ |
|
1237 symbol_c *prev_instruction_type, *operand_type; |
|
1238 |
|
1239 if (NULL == prev_il_instruction) return NULL; |
|
1240 for (unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) { |
|
1241 for(unsigned int j = 0; j < il_operand->candidate_datatypes.size(); j++) { |
|
1242 prev_instruction_type = prev_il_instruction->candidate_datatypes[i]; |
|
1243 operand_type = il_operand->candidate_datatypes[j]; |
|
1244 /* TODO: I believe the following is wrong! The data types of prev_instruction_type and operand_type DO NOT have to be equal. |
|
1245 * the prev_instruction_type MUST be BOOL compatible. |
|
1246 * I am not too sure about operand_type, does it have to be BOOL compatible, or can it be ANY_BIT compatible? Must check! |
|
1247 */ |
|
1248 if (is_type_equal(prev_instruction_type,operand_type) && is_ANY_BOOL_compatible(operand_type)) |
|
1249 add_datatype_to_candidate_list(symbol, prev_instruction_type); |
|
1250 } |
|
1251 } |
|
1252 if (debug) std::cout << "R [" << prev_il_instruction->candidate_datatypes.size() << "," << il_operand->candidate_datatypes.size() << "] ==> " << symbol->candidate_datatypes.size() << " result.\n"; |
|
1253 return NULL; |
|
1254 } |
|
1255 |
|
1256 |
|
1257 void *fill_candidate_datatypes_c::visit( S1_operator_c *symbol) {return handle_implicit_il_fb_call(symbol, "S1", symbol->called_fb_declaration);} |
|
1258 void *fill_candidate_datatypes_c::visit( R1_operator_c *symbol) {return handle_implicit_il_fb_call(symbol, "R1", symbol->called_fb_declaration);} |
|
1259 void *fill_candidate_datatypes_c::visit( CLK_operator_c *symbol) {return handle_implicit_il_fb_call(symbol, "CLK", symbol->called_fb_declaration);} |
|
1260 void *fill_candidate_datatypes_c::visit( CU_operator_c *symbol) {return handle_implicit_il_fb_call(symbol, "CU", symbol->called_fb_declaration);} |
|
1261 void *fill_candidate_datatypes_c::visit( CD_operator_c *symbol) {return handle_implicit_il_fb_call(symbol, "CD", symbol->called_fb_declaration);} |
|
1262 void *fill_candidate_datatypes_c::visit( PV_operator_c *symbol) {return handle_implicit_il_fb_call(symbol, "PV", symbol->called_fb_declaration);} |
|
1263 void *fill_candidate_datatypes_c::visit( IN_operator_c *symbol) {return handle_implicit_il_fb_call(symbol, "IN", symbol->called_fb_declaration);} |
|
1264 void *fill_candidate_datatypes_c::visit( PT_operator_c *symbol) {return handle_implicit_il_fb_call(symbol, "PT", symbol->called_fb_declaration);} |
|
1265 |
|
1266 void *fill_candidate_datatypes_c::visit( AND_operator_c *symbol) {return handle_binary_operator(widen_AND_table, symbol, prev_il_instruction, il_operand);} |
|
1267 void *fill_candidate_datatypes_c::visit( OR_operator_c *symbol) {return handle_binary_operator( widen_OR_table, symbol, prev_il_instruction, il_operand);} |
|
1268 void *fill_candidate_datatypes_c::visit( XOR_operator_c *symbol) {return handle_binary_operator(widen_XOR_table, symbol, prev_il_instruction, il_operand);} |
|
1269 void *fill_candidate_datatypes_c::visit(ANDN_operator_c *symbol) {return handle_binary_operator(widen_AND_table, symbol, prev_il_instruction, il_operand);} |
|
1270 void *fill_candidate_datatypes_c::visit( ORN_operator_c *symbol) {return handle_binary_operator( widen_OR_table, symbol, prev_il_instruction, il_operand);} |
|
1271 void *fill_candidate_datatypes_c::visit(XORN_operator_c *symbol) {return handle_binary_operator(widen_XOR_table, symbol, prev_il_instruction, il_operand);} |
|
1272 |
|
1273 void *fill_candidate_datatypes_c::visit( ADD_operator_c *symbol) {return handle_binary_operator(widen_ADD_table, symbol, prev_il_instruction, il_operand);} |
|
1274 void *fill_candidate_datatypes_c::visit( SUB_operator_c *symbol) {return handle_binary_operator(widen_SUB_table, symbol, prev_il_instruction, il_operand);} |
|
1275 void *fill_candidate_datatypes_c::visit( MUL_operator_c *symbol) {return handle_binary_operator(widen_MUL_table, symbol, prev_il_instruction, il_operand);} |
|
1276 void *fill_candidate_datatypes_c::visit( DIV_operator_c *symbol) {return handle_binary_operator(widen_DIV_table, symbol, prev_il_instruction, il_operand);} |
|
1277 void *fill_candidate_datatypes_c::visit( MOD_operator_c *symbol) {return handle_binary_operator(widen_MOD_table, symbol, prev_il_instruction, il_operand);} |
|
1278 |
|
1279 void *fill_candidate_datatypes_c::visit( GT_operator_c *symbol) {return handle_binary_operator(widen_CMP_table, symbol, prev_il_instruction, il_operand);} |
|
1280 void *fill_candidate_datatypes_c::visit( GE_operator_c *symbol) {return handle_binary_operator(widen_CMP_table, symbol, prev_il_instruction, il_operand);} |
|
1281 void *fill_candidate_datatypes_c::visit( EQ_operator_c *symbol) {return handle_binary_operator(widen_CMP_table, symbol, prev_il_instruction, il_operand);} |
|
1282 void *fill_candidate_datatypes_c::visit( LT_operator_c *symbol) {return handle_binary_operator(widen_CMP_table, symbol, prev_il_instruction, il_operand);} |
|
1283 void *fill_candidate_datatypes_c::visit( LE_operator_c *symbol) {return handle_binary_operator(widen_CMP_table, symbol, prev_il_instruction, il_operand);} |
|
1284 void *fill_candidate_datatypes_c::visit( NE_operator_c *symbol) {return handle_binary_operator(widen_CMP_table, symbol, prev_il_instruction, il_operand);} |
|
1285 |
|
1286 |
|
1287 |
|
1288 void *fill_candidate_datatypes_c::handle_conditional_il_flow_control_operator(symbol_c *symbol) { |
|
1289 if (NULL == prev_il_instruction) return NULL; |
|
1290 for (unsigned int i = 0; i < prev_il_instruction->candidate_datatypes.size(); i++) { |
|
1291 if (is_ANY_BOOL_compatible(prev_il_instruction->candidate_datatypes[i])) |
|
1292 add_datatype_to_candidate_list(symbol, prev_il_instruction->candidate_datatypes[i]); |
|
1293 } |
|
1294 return NULL; |
|
1295 } |
|
1296 |
|
1297 void *fill_candidate_datatypes_c::visit( CAL_operator_c *symbol) {if (NULL != prev_il_instruction) symbol->candidate_datatypes = prev_il_instruction->candidate_datatypes; return NULL;} |
|
1298 void *fill_candidate_datatypes_c::visit( RET_operator_c *symbol) {if (NULL != prev_il_instruction) symbol->candidate_datatypes = prev_il_instruction->candidate_datatypes; return NULL;} |
|
1299 void *fill_candidate_datatypes_c::visit( JMP_operator_c *symbol) {if (NULL != prev_il_instruction) symbol->candidate_datatypes = prev_il_instruction->candidate_datatypes; return NULL;} |
|
1300 void *fill_candidate_datatypes_c::visit( CALC_operator_c *symbol) {return handle_conditional_il_flow_control_operator(symbol);} |
|
1301 void *fill_candidate_datatypes_c::visit(CALCN_operator_c *symbol) {return handle_conditional_il_flow_control_operator(symbol);} |
|
1302 void *fill_candidate_datatypes_c::visit( RETC_operator_c *symbol) {return handle_conditional_il_flow_control_operator(symbol);} |
|
1303 void *fill_candidate_datatypes_c::visit(RETCN_operator_c *symbol) {return handle_conditional_il_flow_control_operator(symbol);} |
|
1304 void *fill_candidate_datatypes_c::visit( JMPC_operator_c *symbol) {return handle_conditional_il_flow_control_operator(symbol);} |
|
1305 void *fill_candidate_datatypes_c::visit(JMPCN_operator_c *symbol) {return handle_conditional_il_flow_control_operator(symbol);} |
|
1306 |
|
1307 |
|
1308 |
|
1309 |
|
1310 /* Symbol class handled together with function call checks */ |
|
1311 // void *visit(il_assign_operator_c *symbol, variable_name); |
|
1312 /* Symbol class handled together with function call checks */ |
|
1313 // void *visit(il_assign_operator_c *symbol, option, variable_name); |
|
1314 |
|
1315 /***************************************/ |
|
1316 /* B.3 - Language ST (Structured Text) */ |
|
1317 /***************************************/ |
|
1318 /***********************/ |
|
1319 /* B 3.1 - Expressions */ |
|
1320 /***********************/ |
|
1321 void *fill_candidate_datatypes_c::visit( or_expression_c *symbol) {return handle_binary_expression(widen_OR_table, symbol, symbol->l_exp, symbol->r_exp);} |
|
1322 void *fill_candidate_datatypes_c::visit( xor_expression_c *symbol) {return handle_binary_expression(widen_XOR_table, symbol, symbol->l_exp, symbol->r_exp);} |
|
1323 void *fill_candidate_datatypes_c::visit( and_expression_c *symbol) {return handle_binary_expression(widen_AND_table, symbol, symbol->l_exp, symbol->r_exp);} |
|
1324 |
|
1325 void *fill_candidate_datatypes_c::visit( equ_expression_c *symbol) {return handle_binary_expression(widen_CMP_table, symbol, symbol->l_exp, symbol->r_exp);} |
|
1326 void *fill_candidate_datatypes_c::visit(notequ_expression_c *symbol) {return handle_binary_expression(widen_CMP_table, symbol, symbol->l_exp, symbol->r_exp);} |
|
1327 void *fill_candidate_datatypes_c::visit( lt_expression_c *symbol) {return handle_binary_expression(widen_CMP_table, symbol, symbol->l_exp, symbol->r_exp);} |
|
1328 void *fill_candidate_datatypes_c::visit( gt_expression_c *symbol) {return handle_binary_expression(widen_CMP_table, symbol, symbol->l_exp, symbol->r_exp);} |
|
1329 void *fill_candidate_datatypes_c::visit( le_expression_c *symbol) {return handle_binary_expression(widen_CMP_table, symbol, symbol->l_exp, symbol->r_exp);} |
|
1330 void *fill_candidate_datatypes_c::visit( ge_expression_c *symbol) {return handle_binary_expression(widen_CMP_table, symbol, symbol->l_exp, symbol->r_exp);} |
|
1331 |
|
1332 |
|
1333 /* The following code is correct when handling the addition of 2 symbolic_variables |
|
1334 * In this case, adding two variables (e.g. USINT_var1 + USINT_var2) will always yield |
|
1335 * the same data type, even if the result of the adition could not fit inside the same |
|
1336 * data type (due to overflowing) |
|
1337 * |
|
1338 * However, when adding two literals (e.g. USINT#42 + USINT#3) |
|
1339 * we should be able to detect overflows of the result, and therefore not consider |
|
1340 * that the result may be of type USINT. |
|
1341 * Currently we do not yet detect these overflows, and allow handling the sum of two USINTs |
|
1342 * as always resulting in an USINT, even in the following expression |
|
1343 * (USINT#65535 + USINT#2). |
|
1344 * |
|
1345 * In the future we can add some code to reduce |
|
1346 * all the expressions that are based on literals into the resulting literal |
|
1347 * value (maybe some visitor class that will run before or after data type |
|
1348 * checking). Since this class will have to be very careful to make sure it implements the same mathematical |
|
1349 * details (e.g. how to round and truncate numbers) as defined in IEC 61131-3, we will leave this to the future. |
|
1350 * Also, the question will arise if we should also replace calls to standard |
|
1351 * functions if the input parameters are all literals (e.g. ADD(42, 42)). This |
|
1352 * means this class will be more difficult than it appears at first. |
|
1353 */ |
|
1354 void *fill_candidate_datatypes_c::visit( add_expression_c *symbol) {return handle_binary_expression(widen_ADD_table, symbol, symbol->l_exp, symbol->r_exp);} |
|
1355 void *fill_candidate_datatypes_c::visit( sub_expression_c *symbol) {return handle_binary_expression(widen_SUB_table, symbol, symbol->l_exp, symbol->r_exp);} |
|
1356 void *fill_candidate_datatypes_c::visit( mul_expression_c *symbol) {return handle_binary_expression(widen_MUL_table, symbol, symbol->l_exp, symbol->r_exp);} |
|
1357 void *fill_candidate_datatypes_c::visit( div_expression_c *symbol) {return handle_binary_expression(widen_DIV_table, symbol, symbol->l_exp, symbol->r_exp);} |
|
1358 void *fill_candidate_datatypes_c::visit( mod_expression_c *symbol) {return handle_binary_expression(widen_MOD_table, symbol, symbol->l_exp, symbol->r_exp);} |
|
1359 void *fill_candidate_datatypes_c::visit(power_expression_c *symbol) {return handle_binary_expression(widen_EXPT_table, symbol, symbol->l_exp, symbol->r_exp);} |
|
1360 |
|
1361 |
|
1362 void *fill_candidate_datatypes_c::visit(neg_expression_c *symbol) { |
|
1363 /* NOTE: The standard defines the syntax for this 'negation' operation, but |
|
1364 * does not define the its semantics. |
|
1365 * |
|
1366 * We could be tempted to consider that the semantics of the |
|
1367 * 'negation' operation are similar/identical to the semantics of the |
|
1368 * SUB expression/operation. This would include assuming that the |
|
1369 * possible datatypes for the 'negation' operation is also |
|
1370 * the same as those for the SUB expression/operation, namely ANY_MAGNITUDE. |
|
1371 * |
|
1372 * However, this would then mean that the following ST code would be |
|
1373 * syntactically and semantically correct: |
|
1374 * uint_var := - (uint_var); |
|
1375 * |
|
1376 * According to the standard, the above code should result in a |
|
1377 * runtime error, when we try to apply a negative value to the |
|
1378 * UINT typed variable 'uint_var'. |
|
1379 * |
|
1380 * It is much easier for the compiler to detect this at compile time, |
|
1381 * and it is probably safer to the resulting code too. |
|
1382 * |
|
1383 * To detect these tyes of errors at compile time, the easisest solution |
|
1384 * is to only allow ANY_NUM datatytpes that are signed. |
|
1385 * So, that is what we do here! |
|
1386 */ |
|
1387 symbol->exp->accept(*this); |
|
1388 for (unsigned int i = 0; i < symbol->exp->candidate_datatypes.size(); i++) { |
|
1389 if (is_ANY_signed_MAGNITUDE_compatible(symbol->exp->candidate_datatypes[i])) |
|
1390 add_datatype_to_candidate_list(symbol, symbol->exp->candidate_datatypes[i]); |
|
1391 } |
|
1392 if (debug) std::cout << "neg [" << symbol->exp->candidate_datatypes.size() << "] ==> " << symbol->candidate_datatypes.size() << " result.\n"; |
|
1393 return NULL; |
|
1394 } |
|
1395 |
|
1396 |
|
1397 void *fill_candidate_datatypes_c::visit(not_expression_c *symbol) { |
|
1398 symbol->exp->accept(*this); |
|
1399 for (unsigned int i = 0; i < symbol->exp->candidate_datatypes.size(); i++) { |
|
1400 if (is_ANY_BIT_compatible(symbol->exp->candidate_datatypes[i])) |
|
1401 add_datatype_to_candidate_list(symbol, symbol->exp->candidate_datatypes[i]); |
|
1402 } |
|
1403 if (debug) std::cout << "not [" << symbol->exp->candidate_datatypes.size() << "] ==> " << symbol->candidate_datatypes.size() << " result.\n"; |
|
1404 return NULL; |
|
1405 } |
|
1406 |
|
1407 |
|
1408 void *fill_candidate_datatypes_c::visit(function_invocation_c *symbol) { |
|
1409 if (NULL != symbol->formal_param_list) symbol-> formal_param_list->accept(*this); |
|
1410 else if (NULL != symbol->nonformal_param_list) symbol->nonformal_param_list->accept(*this); |
|
1411 else ERROR; |
|
1412 |
|
1413 generic_function_call_t fcall_param = { |
|
1414 /* fcall_param.function_name = */ symbol->function_name, |
|
1415 /* fcall_param.nonformal_operand_list = */ symbol->nonformal_param_list, |
|
1416 /* fcall_param.formal_operand_list = */ symbol->formal_param_list, |
|
1417 /* enum {POU_FB, POU_function} POU_type = */ generic_function_call_t::POU_function, |
|
1418 /* fcall_param.candidate_functions = */ symbol->candidate_functions, |
|
1419 /* fcall_param.called_function_declaration = */ symbol->called_function_declaration, |
|
1420 /* fcall_param.extensible_param_count = */ symbol->extensible_param_count |
|
1421 }; |
|
1422 handle_function_call(symbol, fcall_param); |
|
1423 |
|
1424 if (debug) std::cout << "function_invocation_c [" << symbol->candidate_datatypes.size() << "] result.\n"; |
|
1425 return NULL; |
|
1426 } |
|
1427 |
|
1428 |
|
1429 |
|
1430 /********************/ |
|
1431 /* B 3.2 Statements */ |
|
1432 /********************/ |
|
1433 // SYM_LIST(statement_list_c) |
|
1434 /* The visitor of the base class search_visitor_c will handle calling each instruction in the list. |
|
1435 * We do not need to do anything here... |
|
1436 */ |
|
1437 // void *fill_candidate_datatypes_c::visit(statement_list_c *symbol) |
|
1438 |
|
1439 |
|
1440 /*********************************/ |
|
1441 /* B 3.2.1 Assignment Statements */ |
|
1442 /*********************************/ |
|
1443 void *fill_candidate_datatypes_c::visit(assignment_statement_c *symbol) { |
|
1444 symbol_c *left_type, *right_type; |
|
1445 |
|
1446 symbol->l_exp->accept(*this); |
|
1447 symbol->r_exp->accept(*this); |
|
1448 for (unsigned int i = 0; i < symbol->l_exp->candidate_datatypes.size(); i++) { |
|
1449 for(unsigned int j = 0; j < symbol->r_exp->candidate_datatypes.size(); j++) { |
|
1450 left_type = symbol->l_exp->candidate_datatypes[i]; |
|
1451 right_type = symbol->r_exp->candidate_datatypes[j]; |
|
1452 if (is_type_equal(left_type, right_type)) |
|
1453 add_datatype_to_candidate_list(symbol, left_type); |
|
1454 } |
|
1455 } |
|
1456 if (debug) std::cout << ":= [" << symbol->l_exp->candidate_datatypes.size() << "," << symbol->r_exp->candidate_datatypes.size() << "] ==> " << symbol->candidate_datatypes.size() << " result.\n"; |
|
1457 return NULL; |
|
1458 } |
|
1459 |
|
1460 /*****************************************/ |
|
1461 /* B 3.2.2 Subprogram Control Statements */ |
|
1462 /*****************************************/ |
|
1463 void *fill_candidate_datatypes_c::visit(fb_invocation_c *symbol) { |
|
1464 symbol_c *fb_type_id = search_varfb_instance_type->get_basetype_id(symbol->fb_name); |
|
1465 /* Although a call to a non-declared FB is a semantic error, this is currently caught by stage 2! */ |
|
1466 if (NULL == fb_type_id) ERROR; |
|
1467 |
|
1468 function_block_declaration_c *fb_decl = function_block_type_symtable.find_value(fb_type_id); |
|
1469 if (function_block_type_symtable.end_value() == fb_decl) |
|
1470 /* The fb_name not the name of a FB instance. Most probably it is the name of a variable of some other type. */ |
|
1471 fb_decl = NULL; |
|
1472 |
|
1473 /* Although a call to a non-declared FB is a semantic error, this is currently caught by stage 2! */ |
|
1474 if (NULL == fb_decl) ERROR; |
|
1475 |
|
1476 if (symbol-> formal_param_list != NULL) symbol->formal_param_list->accept(*this); |
|
1477 if (symbol->nonformal_param_list != NULL) symbol->nonformal_param_list->accept(*this); |
|
1478 |
|
1479 /* The print_datatypes_error_c does not rely on this called_fb_declaration pointer being != NULL to conclude that |
|
1480 * we have a datat type incompatibility error, so setting it to the correct fb_decl is actually safe, |
|
1481 * as the compiler will never reach the compilation stage! |
|
1482 */ |
|
1483 symbol->called_fb_declaration = fb_decl; |
|
1484 |
|
1485 if (debug) std::cout << "FB [] ==> " << symbol->candidate_datatypes.size() << " result.\n"; |
|
1486 return NULL; |
|
1487 } |
|
1488 |
|
1489 |
|
1490 |
|
1491 /********************************/ |
|
1492 /* B 3.2.3 Selection Statements */ |
|
1493 /********************************/ |
|
1494 void *fill_candidate_datatypes_c::visit(if_statement_c *symbol) { |
|
1495 symbol->expression->accept(*this); |
|
1496 if (NULL != symbol->statement_list) |
|
1497 symbol->statement_list->accept(*this); |
|
1498 if (NULL != symbol->elseif_statement_list) |
|
1499 symbol->elseif_statement_list->accept(*this); |
|
1500 if (NULL != symbol->else_statement_list) |
|
1501 symbol->else_statement_list->accept(*this); |
|
1502 return NULL; |
|
1503 } |
|
1504 |
|
1505 |
|
1506 void *fill_candidate_datatypes_c::visit(elseif_statement_c *symbol) { |
|
1507 symbol->expression->accept(*this); |
|
1508 if (NULL != symbol->statement_list) |
|
1509 symbol->statement_list->accept(*this); |
|
1510 return NULL; |
|
1511 } |
|
1512 |
|
1513 /* CASE expression OF case_element_list ELSE statement_list END_CASE */ |
|
1514 // SYM_REF3(case_statement_c, expression, case_element_list, statement_list) |
|
1515 void *fill_candidate_datatypes_c::visit(case_statement_c *symbol) { |
|
1516 symbol->expression->accept(*this); |
|
1517 if (NULL != symbol->case_element_list) |
|
1518 symbol->case_element_list->accept(*this); |
|
1519 if (NULL != symbol->statement_list) |
|
1520 symbol->statement_list->accept(*this); |
|
1521 return NULL; |
|
1522 } |
|
1523 |
|
1524 |
|
1525 /* helper symbol for case_statement */ |
|
1526 // SYM_LIST(case_element_list_c) |
|
1527 /* NOTE: visitor method for case_element_list_c is not required since we inherit from iterator_visitor_c */ |
|
1528 |
|
1529 /* case_list ':' statement_list */ |
|
1530 // SYM_REF2(case_element_c, case_list, statement_list) |
|
1531 /* NOTE: visitor method for case_element_c is not required since we inherit from iterator_visitor_c */ |
|
1532 |
|
1533 // SYM_LIST(case_list_c) |
|
1534 /* NOTE: visitor method for case_list_c is not required since we inherit from iterator_visitor_c */ |
|
1535 |
|
1536 /********************************/ |
|
1537 /* B 3.2.4 Iteration Statements */ |
|
1538 /********************************/ |
|
1539 |
|
1540 void *fill_candidate_datatypes_c::visit(for_statement_c *symbol) { |
|
1541 symbol->control_variable->accept(*this); |
|
1542 symbol->beg_expression->accept(*this); |
|
1543 symbol->end_expression->accept(*this); |
|
1544 if (NULL != symbol->by_expression) |
|
1545 symbol->by_expression->accept(*this); |
|
1546 if (NULL != symbol->statement_list) |
|
1547 symbol->statement_list->accept(*this); |
|
1548 return NULL; |
|
1549 } |
|
1550 |
|
1551 |
|
1552 void *fill_candidate_datatypes_c::visit(while_statement_c *symbol) { |
|
1553 symbol->expression->accept(*this); |
|
1554 if (NULL != symbol->statement_list) |
|
1555 symbol->statement_list->accept(*this); |
|
1556 return NULL; |
|
1557 } |
|
1558 |
|
1559 |
|
1560 void *fill_candidate_datatypes_c::visit(repeat_statement_c *symbol) { |
|
1561 symbol->expression->accept(*this); |
|
1562 if (NULL != symbol->statement_list) |
|
1563 symbol->statement_list->accept(*this); |
|
1564 return NULL; |
|
1565 } |
|
1566 |
|
1567 |
|
1568 |
|
1569 |
|
1570 |
|
1571 |