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 /* Note that current_type_decl that this class returns may reference the |
|
45 * name of a type, or the type declaration itself! |
|
46 * For an example of the first, consider a variable declared as ... |
|
47 * x : AAA; |
|
48 * where the AAA type is previously declared as whatever. |
|
49 * For an example of the second, consider a variable declared as ... |
|
50 * x : array of int [10]; ----> is allowed |
|
51 * |
|
52 * If it is the first, we will return a reference to the name, if the second |
|
53 * we return a reference to the declaration!! |
|
54 */ |
|
55 class search_var_instance_decl_c: public search_visitor_c { |
|
56 |
|
57 private: |
|
58 symbol_c *search_scope; |
|
59 symbol_c *search_name; |
|
60 symbol_c *current_type_decl; |
|
61 |
|
62 /* variable used to store the type of variable currently being processed... */ |
|
63 /* Will contain a single value of generate_cc_vardecl_c::XXXX_vt */ |
|
64 unsigned int current_vartype; |
|
65 |
|
66 public: |
|
67 search_var_instance_decl_c(symbol_c *search_scope) { |
|
68 this->current_vartype = none_vt; |
|
69 this->search_scope = search_scope; |
|
70 this->search_name = NULL; |
|
71 this->current_type_decl = NULL; |
|
72 } |
|
73 |
|
74 symbol_c *get_decl(symbol_c *variable_instance_name) { |
|
75 this->search_name = variable_instance_name; |
|
76 return (symbol_c *)search_scope->accept(*this); |
|
77 } |
|
78 |
|
79 unsigned int get_vartype() { |
|
80 return current_vartype; |
|
81 } |
|
82 |
|
83 public: |
|
84 |
|
85 /* the types of variables that need to be processed... */ |
|
86 static const unsigned int none_vt = 0x0000; |
|
87 static const unsigned int input_vt = 0x0001; // VAR_INPUT |
|
88 static const unsigned int output_vt = 0x0002; // VAR_OUTPUT |
|
89 static const unsigned int inoutput_vt = 0x0004; // VAR_IN_OUT |
|
90 static const unsigned int private_vt = 0x0008; // VAR |
|
91 static const unsigned int temp_vt = 0x0010; // VAR_TEMP |
|
92 static const unsigned int external_vt = 0x0020; // VAR_EXTERNAL |
|
93 static const unsigned int global_vt = 0x0040; // VAR_GLOBAL |
|
94 static const unsigned int located_vt = 0x0080; // VAR <var_name> AT <location> |
|
95 |
|
96 |
|
97 /***************************/ |
|
98 /* B 0 - Programming Model */ |
|
99 /***************************/ |
|
100 void *visit(library_c *symbol) { |
|
101 /* we do not want to search multiple declaration scopes, |
|
102 * so we do not visit all the functions, fucntion blocks, etc... |
|
103 */ |
|
104 return NULL; |
|
105 } |
|
106 |
|
107 |
|
108 |
|
109 /******************************************/ |
|
110 /* B 1.4.3 - Declaration & Initialisation */ |
|
111 /******************************************/ |
|
112 /* edge -> The F_EDGE or R_EDGE directive */ |
|
113 // SYM_REF2(edge_declaration_c, edge, var1_list) |
|
114 // TODO |
|
115 |
|
116 void *visit(input_declarations_c *symbol) { |
|
117 current_vartype = input_vt; |
|
118 void *res = symbol->input_declaration_list->accept(*this); |
|
119 if (res == NULL) { |
|
120 current_vartype = none_vt; |
|
121 } |
|
122 return res; |
|
123 } |
|
124 |
|
125 /* VAR_OUTPUT [RETAIN | NON_RETAIN] var_init_decl_list END_VAR */ |
|
126 /* option -> may be NULL ! */ |
|
127 void *visit(output_declarations_c *symbol) { |
|
128 current_vartype = output_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_IN_OUT var_declaration_list END_VAR */ |
|
137 void *visit(input_output_declarations_c *symbol) { |
|
138 current_vartype = inoutput_vt; |
|
139 void *res = symbol->var_declaration_list->accept(*this); |
|
140 if (res == NULL) { |
|
141 current_vartype = none_vt; |
|
142 } |
|
143 return res; |
|
144 } |
|
145 |
|
146 /* VAR [CONSTANT] var_init_decl_list END_VAR */ |
|
147 /* option -> may be NULL ! */ |
|
148 /* helper symbol for input_declarations */ |
|
149 void *visit(var_declarations_c *symbol) { |
|
150 current_vartype = private_vt; |
|
151 void *res = symbol->var_init_decl_list->accept(*this); |
|
152 if (res == NULL) { |
|
153 current_vartype = none_vt; |
|
154 } |
|
155 return res; |
|
156 } |
|
157 |
|
158 /* VAR RETAIN var_init_decl_list END_VAR */ |
|
159 void *visit(retentive_var_declarations_c *symbol) { |
|
160 current_vartype = private_vt; |
|
161 void *res = symbol->var_init_decl_list->accept(*this); |
|
162 if (res == NULL) { |
|
163 current_vartype = none_vt; |
|
164 } |
|
165 return res; |
|
166 } |
|
167 |
|
168 /* VAR [CONSTANT|RETAIN|NON_RETAIN] located_var_decl_list END_VAR */ |
|
169 /* option -> may be NULL ! */ |
|
170 //SYM_REF2(located_var_declarations_c, option, located_var_decl_list) |
|
171 void *visit(located_var_declarations_c *symbol) { |
|
172 current_vartype = located_vt; |
|
173 void *res = symbol->located_var_decl_list->accept(*this); |
|
174 if (res == NULL) { |
|
175 current_vartype = none_vt; |
|
176 } |
|
177 return res; |
|
178 } |
|
179 |
|
180 /*| VAR_EXTERNAL [CONSTANT] external_declaration_list END_VAR */ |
|
181 /* option -> may be NULL ! */ |
|
182 //SYM_REF2(external_var_declarations_c, option, external_declaration_list) |
|
183 void *visit(external_var_declarations_c *symbol) { |
|
184 current_vartype = external_vt; |
|
185 void *res = symbol->external_declaration_list->accept(*this); |
|
186 if (res == NULL) { |
|
187 current_vartype = none_vt; |
|
188 } |
|
189 return res; |
|
190 } |
|
191 |
|
192 /*| VAR_GLOBAL [CONSTANT|RETAIN] global_var_decl_list END_VAR */ |
|
193 /* option -> may be NULL ! */ |
|
194 //SYM_REF2(global_var_declarations_c, option, global_var_decl_list) |
|
195 void *visit(global_var_declarations_c *symbol) { |
|
196 current_vartype = global_vt; |
|
197 void *res = symbol->global_var_decl_list->accept(*this); |
|
198 if (res == NULL) { |
|
199 current_vartype = none_vt; |
|
200 } |
|
201 return res; |
|
202 } |
|
203 |
|
204 /* var1_list is one of the following... |
|
205 * simple_spec_init_c * |
|
206 * subrange_spec_init_c * |
|
207 * enumerated_spec_init_c * |
|
208 */ |
|
209 // SYM_REF2(var1_init_decl_c, var1_list, spec_init) |
|
210 void *visit(var1_init_decl_c *symbol) { |
|
211 current_type_decl = symbol->spec_init; |
|
212 return symbol->var1_list->accept(*this); |
|
213 } |
|
214 |
|
215 /* var1_list ',' variable_name */ |
|
216 // SYM_LIST(var1_list_c) |
|
217 void *visit(var1_list_c *symbol) { |
|
218 list_c *list = symbol; |
|
219 for(int i = 0; i < list->n; i++) { |
|
220 if (compare_identifiers(list->elements[i], search_name) == 0) |
|
221 /* by now, current_type_decl should be != NULL */ |
|
222 return current_type_decl; |
|
223 } |
|
224 return NULL; |
|
225 } |
|
226 |
|
227 /* name_list ':' function_block_type_name ASSIGN structure_initialization */ |
|
228 /* structure_initialization -> may be NULL ! */ |
|
229 void *visit(fb_name_decl_c *symbol) { |
|
230 current_type_decl = symbol->function_block_type_name; |
|
231 return symbol->fb_name_list->accept(*this); |
|
232 } |
|
233 |
|
234 /* name_list ',' fb_name */ |
|
235 void *visit(fb_name_list_c *symbol) { |
|
236 list_c *list = symbol; |
|
237 for(int i = 0; i < list->n; i++) { |
|
238 if (compare_identifiers(list->elements[i], search_name) == 0) |
|
239 /* by now, current_fb_declaration should be != NULL */ |
|
240 return current_type_decl; |
|
241 } |
|
242 return NULL; |
|
243 } |
|
244 |
|
245 /* var1_list ':' array_spec_init */ |
|
246 // SYM_REF2(array_var_init_decl_c, var1_list, array_spec_init) |
|
247 void *visit(array_var_init_decl_c *symbol) { |
|
248 current_type_decl = symbol->array_spec_init; |
|
249 return symbol->var1_list->accept(*this); |
|
250 } |
|
251 |
|
252 /* var1_list ':' initialized_structure */ |
|
253 // SYM_REF2(structured_var_init_decl_c, var1_list, initialized_structure) |
|
254 void *visit(structured_var_init_decl_c *symbol) { |
|
255 current_type_decl = symbol->initialized_structure; |
|
256 return symbol->var1_list->accept(*this); |
|
257 } |
|
258 |
|
259 /* var1_list ':' array_specification */ |
|
260 // SYM_REF2(array_var_declaration_c, var1_list, array_specification) |
|
261 void *visit(array_var_declaration_c *symbol) { |
|
262 current_type_decl = symbol->array_specification; |
|
263 return symbol->var1_list->accept(*this); |
|
264 } |
|
265 |
|
266 /* var1_list ':' structure_type_name */ |
|
267 // SYM_REF2(structured_var_declaration_c, var1_list, structure_type_name) |
|
268 void *visit(structured_var_declaration_c *symbol) { |
|
269 current_type_decl = symbol->structure_type_name; |
|
270 return symbol->var1_list->accept(*this); |
|
271 } |
|
272 |
|
273 /* [variable_name] location ':' located_var_spec_init */ |
|
274 /* variable_name -> may be NULL ! */ |
|
275 // SYM_REF4(located_var_decl_c, variable_name, location, located_var_spec_init, unused) |
|
276 // TODO!! |
|
277 |
|
278 /* global_var_name ':' (simple_specification|subrange_specification|enumerated_specification|array_specification|prev_declared_structure_type_name|function_block_type_name */ |
|
279 // SYM_REF2(external_declaration_c, global_var_name, specification) |
|
280 void *visit(external_declaration_c *symbol) { |
|
281 if (compare_identifiers(symbol->global_var_name, search_name) == 0) |
|
282 return symbol->specification; |
|
283 return NULL; |
|
284 } |
|
285 |
|
286 /*| global_var_spec ':' [located_var_spec_init|function_block_type_name] */ |
|
287 /* type_specification ->may be NULL ! */ |
|
288 // SYM_REF2(global_var_decl_c, global_var_spec, type_specification) |
|
289 void *visit(global_var_decl_c *symbol) { |
|
290 if (symbol->type_specification != NULL) { |
|
291 current_type_decl = symbol->type_specification; |
|
292 return symbol->global_var_spec->accept(*this); |
|
293 } |
|
294 else |
|
295 return NULL; |
|
296 } |
|
297 |
|
298 /*| global_var_name location */ |
|
299 //SYM_REF2(global_var_spec_c, global_var_name, location) |
|
300 void *visit(global_var_spec_c *symbol) { |
|
301 if (symbol->global_var_name != NULL && compare_identifiers(symbol->global_var_name, search_name) == 0) |
|
302 return current_type_decl; |
|
303 else |
|
304 return symbol->location->accept(*this); |
|
305 } |
|
306 |
|
307 /*| global_var_list ',' global_var_name */ |
|
308 //SYM_LIST(global_var_list_c) |
|
309 void *visit(global_var_list_c *symbol) { |
|
310 list_c *list = symbol; |
|
311 for(int i = 0; i < list->n; i++) { |
|
312 if (compare_identifiers(list->elements[i], search_name) == 0) |
|
313 /* by now, current_type_decl should be != NULL */ |
|
314 return current_type_decl; |
|
315 } |
|
316 return NULL; |
|
317 } |
|
318 |
|
319 /* [variable_name] location ':' located_var_spec_init */ |
|
320 /* variable_name -> may be NULL ! */ |
|
321 //SYM_REF4(located_var_decl_c, variable_name, location, located_var_spec_init, unused) |
|
322 void *visit(located_var_decl_c *symbol) { |
|
323 if (symbol->variable_name != NULL && compare_identifiers(symbol->variable_name, search_name) == 0) { |
|
324 return symbol->located_var_spec_init; |
|
325 } |
|
326 else |
|
327 return NULL; |
|
328 } |
|
329 |
|
330 /*| global_var_spec ':' [located_var_spec_init|function_block_type_name] */ |
|
331 /* type_specification ->may be NULL ! */ |
|
332 // SYM_REF2(global_var_decl_c, global_var_spec, type_specification) |
|
333 // TODO!! |
|
334 |
|
335 /*| global_var_name location */ |
|
336 // SYM_REF2(global_var_spec_c, global_var_name, location) |
|
337 // TODO!! |
|
338 |
|
339 /* AT direct_variable */ |
|
340 // SYM_REF2(location_c, direct_variable, unused) |
|
341 // TODO!! |
|
342 |
|
343 /*| global_var_list ',' global_var_name */ |
|
344 // SYM_LIST(global_var_list_c) |
|
345 // TODO!! |
|
346 |
|
347 /* var1_list ':' single_byte_string_spec */ |
|
348 // SYM_REF2(single_byte_string_var_declaration_c, var1_list, single_byte_string_spec) |
|
349 void *visit(single_byte_string_var_declaration_c *symbol) { |
|
350 current_type_decl = symbol->single_byte_string_spec; |
|
351 return symbol->var1_list->accept(*this); |
|
352 } |
|
353 |
|
354 /* STRING ['[' integer ']'] [ASSIGN single_byte_character_string] */ |
|
355 /* integer ->may be NULL ! */ |
|
356 /* single_byte_character_string ->may be NULL ! */ |
|
357 // SYM_REF2(single_byte_string_spec_c, integer, single_byte_character_string) |
|
358 // TODO!! |
|
359 |
|
360 /* var1_list ':' double_byte_string_spec */ |
|
361 // SYM_REF2(double_byte_string_var_declaration_c, var1_list, double_byte_string_spec) |
|
362 void *visit(double_byte_string_var_declaration_c *symbol) { |
|
363 current_type_decl = symbol->double_byte_string_spec; |
|
364 return symbol->var1_list->accept(*this); |
|
365 } |
|
366 |
|
367 /* WSTRING ['[' integer ']'] [ASSIGN double_byte_character_string] */ |
|
368 /* integer ->may be NULL ! */ |
|
369 /* double_byte_character_string ->may be NULL ! */ |
|
370 // SYM_REF2(double_byte_string_spec_c, integer, double_byte_character_string) |
|
371 // TODO!! |
|
372 |
|
373 /* variable_name incompl_location ':' var_spec */ |
|
374 // SYM_REF4(incompl_located_var_decl_c, variable_name, incompl_location, var_spec, unused) |
|
375 // TODO!! |
|
376 |
|
377 /* AT incompl_location_token */ |
|
378 // SYM_TOKEN(incompl_location_c) |
|
379 // TODO!! |
|
380 |
|
381 |
|
382 /**************************************/ |
|
383 /* B.1.5 - Program organization units */ |
|
384 /**************************************/ |
|
385 /***********************/ |
|
386 /* B 1.5.1 - Functions */ |
|
387 /***********************/ |
|
388 // SYM_REF4(function_declaration_c, derived_function_name, type_name, var_declarations_list, function_body) |
|
389 void *visit(function_declaration_c *symbol) { |
|
390 /* functions have a variable named after themselves, to store |
|
391 * the variable that will be returned!! |
|
392 */ |
|
393 if (compare_identifiers(symbol->derived_function_name, search_name) == 0) |
|
394 return symbol->type_name; |
|
395 |
|
396 /* no need to search through all the body, so we only |
|
397 * visit the variable declarations...! |
|
398 */ |
|
399 return symbol->var_declarations_list->accept(*this); |
|
400 } |
|
401 |
|
402 /*****************************/ |
|
403 /* B 1.5.2 - Function Blocks */ |
|
404 /*****************************/ |
|
405 void *visit(function_block_declaration_c *symbol) { |
|
406 /* no need to search through all the body, so we only |
|
407 * visit the variable declarations...! |
|
408 */ |
|
409 return symbol->var_declarations->accept(*this); |
|
410 } |
|
411 |
|
412 /**********************/ |
|
413 /* B 1.5.3 - Programs */ |
|
414 /**********************/ |
|
415 void *visit(program_declaration_c *symbol) { |
|
416 /* no need to search through all the body, so we only |
|
417 * visit the variable declarations...! |
|
418 */ |
|
419 return symbol->var_declarations->accept(*this); |
|
420 } |
|
421 |
|
422 |
|
423 /********************************/ |
|
424 /* B 1.7 Configuration elements */ |
|
425 /********************************/ |
|
426 |
|
427 /* |
|
428 CONFIGURATION configuration_name |
|
429 optional_global_var_declarations |
|
430 (resource_declaration_list | single_resource_declaration) |
|
431 optional_access_declarations |
|
432 optional_instance_specific_initializations |
|
433 END_CONFIGURATION |
|
434 */ |
|
435 /* |
|
436 SYM_REF6(configuration_declaration_c, configuration_name, global_var_declarations, resource_declarations, access_declarations, instance_specific_initializations, unused) |
|
437 */ |
|
438 void *visit(configuration_declaration_c *symbol) { |
|
439 /* no need to search through all the configuration, so we only |
|
440 * visit the global variable declarations...! |
|
441 */ |
|
442 if (symbol->global_var_declarations != NULL) |
|
443 return symbol->global_var_declarations->accept(*this); |
|
444 else |
|
445 return NULL; |
|
446 } |
|
447 |
|
448 /* |
|
449 RESOURCE resource_name ON resource_type_name |
|
450 optional_global_var_declarations |
|
451 single_resource_declaration |
|
452 END_RESOURCE |
|
453 */ |
|
454 // SYM_REF4(resource_declaration_c, resource_name, resource_type_name, global_var_declarations, resource_declaration) |
|
455 void *visit(resource_declaration_c *symbol) { |
|
456 /* no need to search through all the resource, so we only |
|
457 * visit the global variable declarations...! |
|
458 */ |
|
459 if (symbol->global_var_declarations != NULL) |
|
460 return symbol->global_var_declarations->accept(*this); |
|
461 else |
|
462 return NULL; |
|
463 } |
|
464 |
|
465 /* task_configuration_list program_configuration_list */ |
|
466 // SYM_REF2(single_resource_declaration_c, task_configuration_list, program_configuration_list) |
|
467 void *visit(single_resource_declaration_c *symbol) { |
|
468 /* no need to search through all the resource, |
|
469 * and there is no global variable declarations...! |
|
470 */ |
|
471 return NULL; |
|
472 } |
|
473 |
|
474 #if 0 |
|
475 /*********************/ |
|
476 /* B 1.4 - Variables */ |
|
477 /*********************/ |
|
478 SYM_REF2(symbolic_variable_c, var_name, unused) |
|
479 |
|
480 /********************************************/ |
|
481 /* B.1.4.1 Directly Represented Variables */ |
|
482 /********************************************/ |
|
483 SYM_TOKEN(direct_variable_c) |
|
484 |
|
485 /*************************************/ |
|
486 /* B.1.4.2 Multi-element Variables */ |
|
487 /*************************************/ |
|
488 /* subscripted_variable '[' subscript_list ']' */ |
|
489 SYM_REF2(array_variable_c, subscripted_variable, subscript_list) |
|
490 |
|
491 /* subscript_list ',' subscript */ |
|
492 SYM_LIST(subscript_list_c) |
|
493 |
|
494 /* record_variable '.' field_selector */ |
|
495 /* WARNING: input and/or output variables of function blocks |
|
496 * may be accessed as fields of a tructured variable! |
|
497 * Code handling a structured_variable_c must take |
|
498 * this into account! |
|
499 */ |
|
500 SYM_REF2(structured_variable_c, record_variable, field_selector) |
|
501 |
|
502 |
|
503 #endif |
|
504 }; |
|
505 |
|