|
1 /* |
|
2 * (c) 2003 Mario de Sousa |
|
3 * |
|
4 * Offered to the public under the terms of the GNU General Public License |
|
5 * as published by the Free Software Foundation; either version 2 of the |
|
6 * License, or (at your option) any later version. |
|
7 * |
|
8 * This program is distributed in the hope that it will be useful, but |
|
9 * WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
|
11 * Public License for more details. |
|
12 * |
|
13 * This code is made available on the understanding that it will not be |
|
14 * used in safety-critical situations without a full and competent review. |
|
15 */ |
|
16 |
|
17 /* |
|
18 * An IEC 61131-3 IL and ST compiler. |
|
19 * |
|
20 * Based on the |
|
21 * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) |
|
22 * |
|
23 */ |
|
24 |
|
25 /* Determine the data type of a specific variable instance, including |
|
26 * function block instances. |
|
27 * A reference to the relevant variable declaration is returned. |
|
28 * The variable instance may NOT be a member of a structure of a memeber |
|
29 * of a structure of an element of an array of ... |
|
30 * |
|
31 * example: |
|
32 * window.points[1].coordinate.x |
|
33 * window.points[1].colour |
|
34 * etc... ARE NOT ALLOWED! |
|
35 * |
|
36 * This class must only be passed the name of the variable that will appear |
|
37 * in the variable declaration. In the above examples, this would be |
|
38 * 'window' !! |
|
39 * |
|
40 * |
|
41 * If you need to pass a complete name of a variable instance (such as |
|
42 * 'window.points[1].coordinate.x') use the search_varfb_instance_type_c instead! |
|
43 */ |
|
44 |
|
45 /* Note that current_type_decl that this class returns may reference the |
|
46 * name of a type, or the type declaration itself! |
|
47 * For an example of the first, consider a variable declared as ... |
|
48 * x : AAA; |
|
49 * where the AAA type is previously declared as whatever. |
|
50 * For an example of the second, consider a variable declared as ... |
|
51 * x : array of int [10]; ----> is allowed |
|
52 * |
|
53 * If it is the first, we will return a reference to the name, if the second |
|
54 * we return a reference to the declaration!! |
|
55 */ |
|
56 #include "absyntax_utils.hh" |
|
57 |
|
58 |
|
59 search_var_instance_decl_c::search_var_instance_decl_c(symbol_c *search_scope) { |
|
60 this->current_vartype = none_vt; |
|
61 this->search_scope = search_scope; |
|
62 this->search_name = NULL; |
|
63 this->current_type_decl = NULL; |
|
64 } |
|
65 |
|
66 symbol_c *search_var_instance_decl_c::get_decl(symbol_c *variable_instance_name) { |
|
67 this->search_name = variable_instance_name; |
|
68 return (symbol_c *)search_scope->accept(*this); |
|
69 } |
|
70 |
|
71 unsigned int search_var_instance_decl_c::get_vartype() { |
|
72 return current_vartype; |
|
73 } |
|
74 |
|
75 /***************************/ |
|
76 /* B 0 - Programming Model */ |
|
77 /***************************/ |
|
78 void *search_var_instance_decl_c::visit(library_c *symbol) { |
|
79 /* we do not want to search multiple declaration scopes, |
|
80 * so we do not visit all the functions, fucntion blocks, etc... |
|
81 */ |
|
82 return NULL; |
|
83 } |
|
84 |
|
85 |
|
86 |
|
87 /******************************************/ |
|
88 /* B 1.4.3 - Declaration & Initialisation */ |
|
89 /******************************************/ |
|
90 /* edge -> The F_EDGE or R_EDGE directive */ |
|
91 // SYM_REF2(edge_declaration_c, edge, var1_list) |
|
92 // TODO |
|
93 |
|
94 void *search_var_instance_decl_c::visit(input_declarations_c *symbol) { |
|
95 current_vartype = input_vt; |
|
96 void *res = symbol->input_declaration_list->accept(*this); |
|
97 if (res == NULL) { |
|
98 current_vartype = none_vt; |
|
99 } |
|
100 return res; |
|
101 } |
|
102 |
|
103 /* VAR_OUTPUT [RETAIN | NON_RETAIN] var_init_decl_list END_VAR */ |
|
104 /* option -> may be NULL ! */ |
|
105 void *search_var_instance_decl_c::visit(output_declarations_c *symbol) { |
|
106 current_vartype = output_vt; |
|
107 void *res = symbol->var_init_decl_list->accept(*this); |
|
108 if (res == NULL) { |
|
109 current_vartype = none_vt; |
|
110 } |
|
111 return res; |
|
112 } |
|
113 |
|
114 /* VAR_IN_OUT var_declaration_list END_VAR */ |
|
115 void *search_var_instance_decl_c::visit(input_output_declarations_c *symbol) { |
|
116 current_vartype = inoutput_vt; |
|
117 void *res = symbol->var_declaration_list->accept(*this); |
|
118 if (res == NULL) { |
|
119 current_vartype = none_vt; |
|
120 } |
|
121 return res; |
|
122 } |
|
123 |
|
124 /* VAR [CONSTANT] var_init_decl_list END_VAR */ |
|
125 /* option -> may be NULL ! */ |
|
126 /* helper symbol for input_declarations */ |
|
127 void *search_var_instance_decl_c::visit(var_declarations_c *symbol) { |
|
128 current_vartype = private_vt; |
|
129 void *res = symbol->var_init_decl_list->accept(*this); |
|
130 if (res == NULL) { |
|
131 current_vartype = none_vt; |
|
132 } |
|
133 return res; |
|
134 } |
|
135 |
|
136 /* VAR RETAIN var_init_decl_list END_VAR */ |
|
137 void *search_var_instance_decl_c::visit(retentive_var_declarations_c *symbol) { |
|
138 current_vartype = private_vt; |
|
139 void *res = symbol->var_init_decl_list->accept(*this); |
|
140 if (res == NULL) { |
|
141 current_vartype = none_vt; |
|
142 } |
|
143 return res; |
|
144 } |
|
145 |
|
146 /* VAR [CONSTANT|RETAIN|NON_RETAIN] located_var_decl_list END_VAR */ |
|
147 /* option -> may be NULL ! */ |
|
148 //SYM_REF2(located_var_declarations_c, option, located_var_decl_list) |
|
149 void *search_var_instance_decl_c::visit(located_var_declarations_c *symbol) { |
|
150 current_vartype = located_vt; |
|
151 void *res = symbol->located_var_decl_list->accept(*this); |
|
152 if (res == NULL) { |
|
153 current_vartype = none_vt; |
|
154 } |
|
155 return res; |
|
156 } |
|
157 |
|
158 /*| VAR_EXTERNAL [CONSTANT] external_declaration_list END_VAR */ |
|
159 /* option -> may be NULL ! */ |
|
160 //SYM_REF2(external_var_declarations_c, option, external_declaration_list) |
|
161 void *search_var_instance_decl_c::visit(external_var_declarations_c *symbol) { |
|
162 current_vartype = external_vt; |
|
163 void *res = symbol->external_declaration_list->accept(*this); |
|
164 if (res == NULL) { |
|
165 current_vartype = none_vt; |
|
166 } |
|
167 return res; |
|
168 } |
|
169 |
|
170 /*| VAR_GLOBAL [CONSTANT|RETAIN] global_var_decl_list END_VAR */ |
|
171 /* option -> may be NULL ! */ |
|
172 //SYM_REF2(global_var_declarations_c, option, global_var_decl_list) |
|
173 void *search_var_instance_decl_c::visit(global_var_declarations_c *symbol) { |
|
174 current_vartype = global_vt; |
|
175 void *res = symbol->global_var_decl_list->accept(*this); |
|
176 if (res == NULL) { |
|
177 current_vartype = none_vt; |
|
178 } |
|
179 return res; |
|
180 } |
|
181 |
|
182 /* var1_list is one of the following... |
|
183 * simple_spec_init_c * |
|
184 * subrange_spec_init_c * |
|
185 * enumerated_spec_init_c * |
|
186 */ |
|
187 // SYM_REF2(var1_init_decl_c, var1_list, spec_init) |
|
188 void *search_var_instance_decl_c::visit(var1_init_decl_c *symbol) { |
|
189 current_type_decl = symbol->spec_init; |
|
190 return symbol->var1_list->accept(*this); |
|
191 } |
|
192 |
|
193 /* var1_list ',' variable_name */ |
|
194 // SYM_LIST(var1_list_c) |
|
195 void *search_var_instance_decl_c::visit(var1_list_c *symbol) { |
|
196 list_c *list = symbol; |
|
197 for(int i = 0; i < list->n; i++) { |
|
198 if (compare_identifiers(list->elements[i], search_name) == 0) |
|
199 /* by now, current_type_decl should be != NULL */ |
|
200 return current_type_decl; |
|
201 } |
|
202 return NULL; |
|
203 } |
|
204 |
|
205 /* name_list ':' function_block_type_name ASSIGN structure_initialization */ |
|
206 /* structure_initialization -> may be NULL ! */ |
|
207 void *search_var_instance_decl_c::visit(fb_name_decl_c *symbol) { |
|
208 current_type_decl = symbol->function_block_type_name; |
|
209 return symbol->fb_name_list->accept(*this); |
|
210 } |
|
211 |
|
212 /* name_list ',' fb_name */ |
|
213 void *search_var_instance_decl_c::visit(fb_name_list_c *symbol) { |
|
214 list_c *list = symbol; |
|
215 for(int i = 0; i < list->n; i++) { |
|
216 if (compare_identifiers(list->elements[i], search_name) == 0) |
|
217 /* by now, current_fb_declaration should be != NULL */ |
|
218 return current_type_decl; |
|
219 } |
|
220 return NULL; |
|
221 } |
|
222 |
|
223 /* var1_list ':' array_spec_init */ |
|
224 // SYM_REF2(array_var_init_decl_c, var1_list, array_spec_init) |
|
225 void *search_var_instance_decl_c::visit(array_var_init_decl_c *symbol) { |
|
226 current_type_decl = symbol->array_spec_init; |
|
227 return symbol->var1_list->accept(*this); |
|
228 } |
|
229 |
|
230 /* var1_list ':' initialized_structure */ |
|
231 // SYM_REF2(structured_var_init_decl_c, var1_list, initialized_structure) |
|
232 void *search_var_instance_decl_c::visit(structured_var_init_decl_c *symbol) { |
|
233 current_type_decl = symbol->initialized_structure; |
|
234 return symbol->var1_list->accept(*this); |
|
235 } |
|
236 |
|
237 /* var1_list ':' array_specification */ |
|
238 // SYM_REF2(array_var_declaration_c, var1_list, array_specification) |
|
239 void *search_var_instance_decl_c::visit(array_var_declaration_c *symbol) { |
|
240 current_type_decl = symbol->array_specification; |
|
241 return symbol->var1_list->accept(*this); |
|
242 } |
|
243 |
|
244 /* var1_list ':' structure_type_name */ |
|
245 // SYM_REF2(structured_var_declaration_c, var1_list, structure_type_name) |
|
246 void *search_var_instance_decl_c::visit(structured_var_declaration_c *symbol) { |
|
247 current_type_decl = symbol->structure_type_name; |
|
248 return symbol->var1_list->accept(*this); |
|
249 } |
|
250 |
|
251 /* [variable_name] location ':' located_var_spec_init */ |
|
252 /* variable_name -> may be NULL ! */ |
|
253 // SYM_REF4(located_var_decl_c, variable_name, location, located_var_spec_init, unused) |
|
254 // TODO!! |
|
255 |
|
256 /* global_var_name ':' (simple_specification|subrange_specification|enumerated_specification|array_specification|prev_declared_structure_type_name|function_block_type_name */ |
|
257 // SYM_REF2(external_declaration_c, global_var_name, specification) |
|
258 void *search_var_instance_decl_c::visit(external_declaration_c *symbol) { |
|
259 if (compare_identifiers(symbol->global_var_name, search_name) == 0) |
|
260 return symbol->specification; |
|
261 return NULL; |
|
262 } |
|
263 |
|
264 /*| global_var_spec ':' [located_var_spec_init|function_block_type_name] */ |
|
265 /* type_specification ->may be NULL ! */ |
|
266 // SYM_REF2(global_var_decl_c, global_var_spec, type_specification) |
|
267 void *search_var_instance_decl_c::visit(global_var_decl_c *symbol) { |
|
268 if (symbol->type_specification != NULL) { |
|
269 current_type_decl = symbol->type_specification; |
|
270 return symbol->global_var_spec->accept(*this); |
|
271 } |
|
272 else |
|
273 return NULL; |
|
274 } |
|
275 |
|
276 /*| global_var_name location */ |
|
277 //SYM_REF2(global_var_spec_c, global_var_name, location) |
|
278 void *search_var_instance_decl_c::visit(global_var_spec_c *symbol) { |
|
279 if (symbol->global_var_name != NULL && compare_identifiers(symbol->global_var_name, search_name) == 0) |
|
280 return current_type_decl; |
|
281 else |
|
282 return symbol->location->accept(*this); |
|
283 } |
|
284 |
|
285 /*| global_var_list ',' global_var_name */ |
|
286 //SYM_LIST(global_var_list_c) |
|
287 void *search_var_instance_decl_c::visit(global_var_list_c *symbol) { |
|
288 list_c *list = symbol; |
|
289 for(int i = 0; i < list->n; i++) { |
|
290 if (compare_identifiers(list->elements[i], search_name) == 0) |
|
291 /* by now, current_type_decl should be != NULL */ |
|
292 return current_type_decl; |
|
293 } |
|
294 return NULL; |
|
295 } |
|
296 |
|
297 /* [variable_name] location ':' located_var_spec_init */ |
|
298 /* variable_name -> may be NULL ! */ |
|
299 //SYM_REF4(located_var_decl_c, variable_name, location, located_var_spec_init, unused) |
|
300 void *search_var_instance_decl_c::visit(located_var_decl_c *symbol) { |
|
301 if (symbol->variable_name != NULL && compare_identifiers(symbol->variable_name, search_name) == 0) |
|
302 return symbol->located_var_spec_init; |
|
303 else { |
|
304 current_type_decl = symbol->located_var_spec_init; |
|
305 return symbol->location->accept(*this); |
|
306 } |
|
307 } |
|
308 |
|
309 /*| global_var_spec ':' [located_var_spec_init|function_block_type_name] */ |
|
310 /* type_specification ->may be NULL ! */ |
|
311 // SYM_REF2(global_var_decl_c, global_var_spec, type_specification) |
|
312 // TODO!! |
|
313 |
|
314 /* AT direct_variable */ |
|
315 // SYM_REF2(location_c, direct_variable, unused) |
|
316 void *search_var_instance_decl_c::visit(location_c *symbol) { |
|
317 if (compare_identifiers(symbol->direct_variable, search_name) == 0) |
|
318 return current_type_decl; |
|
319 else |
|
320 return NULL; |
|
321 } |
|
322 |
|
323 /*| global_var_list ',' global_var_name */ |
|
324 // SYM_LIST(global_var_list_c) |
|
325 // TODO!! |
|
326 |
|
327 /* var1_list ':' single_byte_string_spec */ |
|
328 // SYM_REF2(single_byte_string_var_declaration_c, var1_list, single_byte_string_spec) |
|
329 void *search_var_instance_decl_c::visit(single_byte_string_var_declaration_c *symbol) { |
|
330 current_type_decl = symbol->single_byte_string_spec; |
|
331 return symbol->var1_list->accept(*this); |
|
332 } |
|
333 |
|
334 /* STRING ['[' integer ']'] [ASSIGN single_byte_character_string] */ |
|
335 /* integer ->may be NULL ! */ |
|
336 /* single_byte_character_string ->may be NULL ! */ |
|
337 // SYM_REF2(single_byte_string_spec_c, integer, single_byte_character_string) |
|
338 // TODO!! |
|
339 |
|
340 /* var1_list ':' double_byte_string_spec */ |
|
341 // SYM_REF2(double_byte_string_var_declaration_c, var1_list, double_byte_string_spec) |
|
342 void *search_var_instance_decl_c::visit(double_byte_string_var_declaration_c *symbol) { |
|
343 current_type_decl = symbol->double_byte_string_spec; |
|
344 return symbol->var1_list->accept(*this); |
|
345 } |
|
346 |
|
347 /* WSTRING ['[' integer ']'] [ASSIGN double_byte_character_string] */ |
|
348 /* integer ->may be NULL ! */ |
|
349 /* double_byte_character_string ->may be NULL ! */ |
|
350 // SYM_REF2(double_byte_string_spec_c, integer, double_byte_character_string) |
|
351 // TODO!! |
|
352 |
|
353 /* variable_name incompl_location ':' var_spec */ |
|
354 // SYM_REF4(incompl_located_var_decl_c, variable_name, incompl_location, var_spec, unused) |
|
355 // TODO!! |
|
356 |
|
357 /* AT incompl_location_token */ |
|
358 // SYM_TOKEN(incompl_location_c) |
|
359 // TODO!! |
|
360 |
|
361 |
|
362 /**************************************/ |
|
363 /* B.1.5 - Program organization units */ |
|
364 /**************************************/ |
|
365 /***********************/ |
|
366 /* B 1.5.1 - Functions */ |
|
367 /***********************/ |
|
368 // SYM_REF4(function_declaration_c, derived_function_name, type_name, var_declarations_list, function_body) |
|
369 void *search_var_instance_decl_c::visit(function_declaration_c *symbol) { |
|
370 /* functions have a variable named after themselves, to store |
|
371 * the variable that will be returned!! |
|
372 */ |
|
373 if (compare_identifiers(symbol->derived_function_name, search_name) == 0) |
|
374 return symbol->type_name; |
|
375 |
|
376 /* no need to search through all the body, so we only |
|
377 * visit the variable declarations...! |
|
378 */ |
|
379 return symbol->var_declarations_list->accept(*this); |
|
380 } |
|
381 |
|
382 /*****************************/ |
|
383 /* B 1.5.2 - Function Blocks */ |
|
384 /*****************************/ |
|
385 void *search_var_instance_decl_c::visit(function_block_declaration_c *symbol) { |
|
386 /* no need to search through all the body, so we only |
|
387 * visit the variable declarations...! |
|
388 */ |
|
389 return symbol->var_declarations->accept(*this); |
|
390 } |
|
391 |
|
392 /**********************/ |
|
393 /* B 1.5.3 - Programs */ |
|
394 /**********************/ |
|
395 void *search_var_instance_decl_c::visit(program_declaration_c *symbol) { |
|
396 /* no need to search through all the body, so we only |
|
397 * visit the variable declarations...! |
|
398 */ |
|
399 return symbol->var_declarations->accept(*this); |
|
400 } |
|
401 |
|
402 |
|
403 /********************************/ |
|
404 /* B 1.7 Configuration elements */ |
|
405 /********************************/ |
|
406 |
|
407 /* |
|
408 CONFIGURATION configuration_name |
|
409 optional_global_var_declarations |
|
410 (resource_declaration_list | single_resource_declaration) |
|
411 optional_access_declarations |
|
412 optional_instance_specific_initializations |
|
413 END_CONFIGURATION |
|
414 */ |
|
415 /* |
|
416 SYM_REF6(configuration_declaration_c, configuration_name, global_var_declarations, resource_declarations, access_declarations, instance_specific_initializations, unused) |
|
417 */ |
|
418 void *search_var_instance_decl_c::visit(configuration_declaration_c *symbol) { |
|
419 /* no need to search through all the configuration, so we only |
|
420 * visit the global variable declarations...! |
|
421 */ |
|
422 if (symbol->global_var_declarations != NULL) |
|
423 return symbol->global_var_declarations->accept(*this); |
|
424 else |
|
425 return NULL; |
|
426 } |
|
427 |
|
428 /* |
|
429 RESOURCE resource_name ON resource_type_name |
|
430 optional_global_var_declarations |
|
431 single_resource_declaration |
|
432 END_RESOURCE |
|
433 */ |
|
434 // SYM_REF4(resource_declaration_c, resource_name, resource_type_name, global_var_declarations, resource_declaration) |
|
435 void *search_var_instance_decl_c::visit(resource_declaration_c *symbol) { |
|
436 /* no need to search through all the resource, so we only |
|
437 * visit the global variable declarations...! |
|
438 */ |
|
439 if (symbol->global_var_declarations != NULL) |
|
440 return symbol->global_var_declarations->accept(*this); |
|
441 else |
|
442 return NULL; |
|
443 } |
|
444 |
|
445 /* task_configuration_list program_configuration_list */ |
|
446 // SYM_REF2(single_resource_declaration_c, task_configuration_list, program_configuration_list) |
|
447 void *search_var_instance_decl_c::visit(single_resource_declaration_c *symbol) { |
|
448 /* no need to search through all the resource, |
|
449 * and there is no global variable declarations...! |
|
450 */ |
|
451 return NULL; |
|
452 } |
|
453 |
|
454 #if 0 |
|
455 /*********************/ |
|
456 /* B 1.4 - Variables */ |
|
457 /*********************/ |
|
458 SYM_REF2(symbolic_variable_c, var_name, unused) |
|
459 |
|
460 /********************************************/ |
|
461 /* B.1.4.1 Directly Represented Variables */ |
|
462 /********************************************/ |
|
463 SYM_TOKEN(direct_variable_c) |
|
464 |
|
465 /*************************************/ |
|
466 /* B.1.4.2 Multi-element Variables */ |
|
467 /*************************************/ |
|
468 /* subscripted_variable '[' subscript_list ']' */ |
|
469 SYM_REF2(array_variable_c, subscripted_variable, subscript_list) |
|
470 |
|
471 /* subscript_list ',' subscript */ |
|
472 SYM_LIST(subscript_list_c) |
|
473 |
|
474 /* record_variable '.' field_selector */ |
|
475 /* WARNING: input and/or output variables of function blocks |
|
476 * may be accessed as fields of a tructured variable! |
|
477 * Code handling a structured_variable_c must take |
|
478 * this into account! |
|
479 */ |
|
480 SYM_REF2(structured_variable_c, record_variable, field_selector) |
|
481 |
|
482 |
|
483 |
|
484 }; |
|
485 #endif |