|
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 public: |
|
63 search_var_instance_decl_c(symbol_c *search_scope) { |
|
64 this->search_scope = search_scope; |
|
65 this->search_name = NULL; |
|
66 this->current_type_decl = NULL; |
|
67 } |
|
68 |
|
69 symbol_c *get_decl(symbol_c *variable_instance_name) { |
|
70 this->search_name = variable_instance_name; |
|
71 return (symbol_c *)search_scope->accept(*this); |
|
72 } |
|
73 |
|
74 public: |
|
75 /***************************/ |
|
76 /* B 0 - Programming Model */ |
|
77 /***************************/ |
|
78 void *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 /* var1_list is one of the following... |
|
95 * simple_spec_init_c * |
|
96 * subrange_spec_init_c * |
|
97 * enumerated_spec_init_c * |
|
98 */ |
|
99 // SYM_REF2(var1_init_decl_c, var1_list, spec_init) |
|
100 void *visit(var1_init_decl_c *symbol) { |
|
101 current_type_decl = symbol->spec_init; |
|
102 return symbol->var1_list->accept(*this); |
|
103 } |
|
104 |
|
105 /* var1_list ',' variable_name */ |
|
106 // SYM_LIST(var1_list_c) |
|
107 void *visit(var1_list_c *symbol) { |
|
108 list_c *list = symbol; |
|
109 for(int i = 0; i < list->n; i++) { |
|
110 if (compare_identifiers(list->elements[i], search_name) == 0) |
|
111 /* by now, current_type_decl should be != NULL */ |
|
112 return current_type_decl; |
|
113 } |
|
114 return NULL; |
|
115 } |
|
116 |
|
117 /* name_list ':' function_block_type_name ASSIGN structure_initialization */ |
|
118 /* structure_initialization -> may be NULL ! */ |
|
119 void *visit(fb_name_decl_c *symbol) { |
|
120 current_type_decl = symbol->function_block_type_name; |
|
121 return symbol->fb_name_list->accept(*this); |
|
122 } |
|
123 |
|
124 /* name_list ',' fb_name */ |
|
125 void *visit(fb_name_list_c *symbol) { |
|
126 list_c *list = symbol; |
|
127 for(int i = 0; i < list->n; i++) { |
|
128 if (compare_identifiers(list->elements[i], search_name) == 0) |
|
129 /* by now, current_fb_declaration should be != NULL */ |
|
130 return current_type_decl; |
|
131 } |
|
132 return NULL; |
|
133 } |
|
134 |
|
135 /* var1_list ':' array_spec_init */ |
|
136 // SYM_REF2(array_var_init_decl_c, var1_list, array_spec_init) |
|
137 void *visit(array_var_init_decl_c *symbol) { |
|
138 current_type_decl = symbol->array_spec_init; |
|
139 return symbol->var1_list->accept(*this); |
|
140 } |
|
141 |
|
142 /* var1_list ':' initialized_structure */ |
|
143 // SYM_REF2(structured_var_init_decl_c, var1_list, initialized_structure) |
|
144 void *visit(structured_var_init_decl_c *symbol) { |
|
145 current_type_decl = symbol->initialized_structure; |
|
146 return symbol->var1_list->accept(*this); |
|
147 } |
|
148 |
|
149 /* var1_list ':' array_specification */ |
|
150 // SYM_REF2(array_var_declaration_c, var1_list, array_specification) |
|
151 void *visit(array_var_declaration_c *symbol) { |
|
152 current_type_decl = symbol->array_specification; |
|
153 return symbol->var1_list->accept(*this); |
|
154 } |
|
155 |
|
156 /* var1_list ':' structure_type_name */ |
|
157 // SYM_REF2(structured_var_declaration_c, var1_list, structure_type_name) |
|
158 void *visit(structured_var_declaration_c *symbol) { |
|
159 current_type_decl = symbol->structure_type_name; |
|
160 return symbol->var1_list->accept(*this); |
|
161 } |
|
162 |
|
163 /* [variable_name] location ':' located_var_spec_init */ |
|
164 /* variable_name -> may be NULL ! */ |
|
165 // SYM_REF4(located_var_decl_c, variable_name, location, located_var_spec_init, unused) |
|
166 // TODO!! |
|
167 |
|
168 /* global_var_name ':' (simple_specification|subrange_specification|enumerated_specification|array_specification|prev_declared_structure_type_name|function_block_type_name */ |
|
169 // SYM_REF2(external_declaration_c, global_var_name, specification) |
|
170 void *visit(external_declaration_c *symbol) { |
|
171 if (compare_identifiers(symbol->global_var_name, search_name) == 0) |
|
172 return symbol->specification; |
|
173 return NULL; |
|
174 } |
|
175 |
|
176 /*| global_var_spec ':' [located_var_spec_init|function_block_type_name] */ |
|
177 /* type_specification ->may be NULL ! */ |
|
178 // SYM_REF2(global_var_decl_c, global_var_spec, type_specification) |
|
179 // TODO!! |
|
180 |
|
181 /*| global_var_name location */ |
|
182 // SYM_REF2(global_var_spec_c, global_var_name, location) |
|
183 // TODO!! |
|
184 |
|
185 /* AT direct_variable */ |
|
186 // SYM_REF2(location_c, direct_variable, unused) |
|
187 // TODO!! |
|
188 |
|
189 /*| global_var_list ',' global_var_name */ |
|
190 // SYM_LIST(global_var_list_c) |
|
191 // TODO!! |
|
192 |
|
193 /* var1_list ':' single_byte_string_spec */ |
|
194 // SYM_REF2(single_byte_string_var_declaration_c, var1_list, single_byte_string_spec) |
|
195 void *visit(single_byte_string_var_declaration_c *symbol) { |
|
196 current_type_decl = symbol->single_byte_string_spec; |
|
197 return symbol->var1_list->accept(*this); |
|
198 } |
|
199 |
|
200 /* STRING ['[' integer ']'] [ASSIGN single_byte_character_string] */ |
|
201 /* integer ->may be NULL ! */ |
|
202 /* single_byte_character_string ->may be NULL ! */ |
|
203 // SYM_REF2(single_byte_string_spec_c, integer, single_byte_character_string) |
|
204 // TODO!! |
|
205 |
|
206 /* var1_list ':' double_byte_string_spec */ |
|
207 // SYM_REF2(double_byte_string_var_declaration_c, var1_list, double_byte_string_spec) |
|
208 void *visit(double_byte_string_var_declaration_c *symbol) { |
|
209 current_type_decl = symbol->double_byte_string_spec; |
|
210 return symbol->var1_list->accept(*this); |
|
211 } |
|
212 |
|
213 /* WSTRING ['[' integer ']'] [ASSIGN double_byte_character_string] */ |
|
214 /* integer ->may be NULL ! */ |
|
215 /* double_byte_character_string ->may be NULL ! */ |
|
216 // SYM_REF2(double_byte_string_spec_c, integer, double_byte_character_string) |
|
217 // TODO!! |
|
218 |
|
219 /* variable_name incompl_location ':' var_spec */ |
|
220 // SYM_REF4(incompl_located_var_decl_c, variable_name, incompl_location, var_spec, unused) |
|
221 // TODO!! |
|
222 |
|
223 /* AT incompl_location_token */ |
|
224 // SYM_TOKEN(incompl_location_c) |
|
225 // TODO!! |
|
226 |
|
227 |
|
228 /**************************************/ |
|
229 /* B.1.5 - Program organization units */ |
|
230 /**************************************/ |
|
231 /***********************/ |
|
232 /* B 1.5.1 - Functions */ |
|
233 /***********************/ |
|
234 // SYM_REF4(function_declaration_c, derived_function_name, type_name, var_declarations_list, function_body) |
|
235 void *visit(function_declaration_c *symbol) { |
|
236 /* functions have a variable named after themselves, to store |
|
237 * the variable that will be returned!! |
|
238 */ |
|
239 if (compare_identifiers(symbol->derived_function_name, search_name) == 0) |
|
240 return symbol->type_name; |
|
241 |
|
242 /* no need to search through all the body, so we only |
|
243 * visit the variable declarations...! |
|
244 */ |
|
245 return symbol->var_declarations_list->accept(*this); |
|
246 } |
|
247 |
|
248 /*****************************/ |
|
249 /* B 1.5.2 - Function Blocks */ |
|
250 /*****************************/ |
|
251 void *visit(function_block_declaration_c *symbol) { |
|
252 /* no need to search through all the body, so we only |
|
253 * visit the variable declarations...! |
|
254 */ |
|
255 return symbol->var_declarations->accept(*this); |
|
256 } |
|
257 |
|
258 /**********************/ |
|
259 /* B 1.5.3 - Programs */ |
|
260 /**********************/ |
|
261 void *visit(program_declaration_c *symbol) { |
|
262 /* no need to search through all the body, so we only |
|
263 * visit the variable declarations...! |
|
264 */ |
|
265 return symbol->var_declarations->accept(*this); |
|
266 } |
|
267 |
|
268 |
|
269 #if 0 |
|
270 /*********************/ |
|
271 /* B 1.4 - Variables */ |
|
272 /*********************/ |
|
273 SYM_REF2(symbolic_variable_c, var_name, unused) |
|
274 |
|
275 /********************************************/ |
|
276 /* B.1.4.1 Directly Represented Variables */ |
|
277 /********************************************/ |
|
278 SYM_TOKEN(direct_variable_c) |
|
279 |
|
280 /*************************************/ |
|
281 /* B.1.4.2 Multi-element Variables */ |
|
282 /*************************************/ |
|
283 /* subscripted_variable '[' subscript_list ']' */ |
|
284 SYM_REF2(array_variable_c, subscripted_variable, subscript_list) |
|
285 |
|
286 /* subscript_list ',' subscript */ |
|
287 SYM_LIST(subscript_list_c) |
|
288 |
|
289 /* record_variable '.' field_selector */ |
|
290 /* WARNING: input and/or output variables of function blocks |
|
291 * may be accessed as fields of a tructured variable! |
|
292 * Code handling a structured_variable_c must take |
|
293 * this into account! |
|
294 */ |
|
295 SYM_REF2(structured_variable_c, record_variable, field_selector) |
|
296 |
|
297 |
|
298 #endif |
|
299 }; |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |