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 |
|
26 |
|
27 /* Determine the data type of a variable. |
|
28 * The variable may be a simple variable, a function block instance, a |
|
29 * struture element within a data structured type (a struct or a fb), or |
|
30 * an array element. |
|
31 * A mixture of array element of a structure element of a structure element |
|
32 * of a .... is also suported! |
|
33 * |
|
34 * A reference to the relevant base type __definition__ is returned. |
|
35 * This means that if we find that the variable is of type MY_INT, |
|
36 * which was previously declared to be |
|
37 * TYPE MY_INT: INT := 9; |
|
38 * this class wil return INT, and __not__ MY_INT !! |
|
39 * |
|
40 * |
|
41 * example: |
|
42 * window.points[1].coordinate.x |
|
43 * window.points[1].colour |
|
44 * etc... ARE ALLOWED! |
|
45 * |
|
46 * This class must be passed the scope within which the |
|
47 * variable was declared, and the variable name... |
|
48 */ |
|
49 class search_varfb_instance_type_c: public search_base_type_c { |
|
50 private: |
|
51 search_var_instance_decl_c search_var_instance_decl; |
|
52 decompose_var_instance_name_c *decompose_var_instance_name; |
|
53 symbol_c *current_structelement_name; |
|
54 bool search_base_type; |
|
55 |
|
56 public: |
|
57 search_varfb_instance_type_c(symbol_c *search_scope): search_var_instance_decl(search_scope) { |
|
58 this->decompose_var_instance_name = NULL; |
|
59 this->current_structelement_name = NULL; |
|
60 this->search_base_type = false; |
|
61 } |
|
62 |
|
63 symbol_c *get_type(symbol_c *variable_name, bool base_type = true) { |
|
64 this->current_structelement_name = NULL; |
|
65 this->decompose_var_instance_name = new decompose_var_instance_name_c(variable_name); |
|
66 this->search_base_type = base_type; |
|
67 if (NULL == decompose_var_instance_name) ERROR; |
|
68 |
|
69 /* find the part of the variable name that will appear in the |
|
70 * variable declaration, for e.g., in window.point.x, this would be |
|
71 * window! |
|
72 */ |
|
73 symbol_c *var_name_part = decompose_var_instance_name->next_part(); |
|
74 if (NULL == var_name_part) ERROR; |
|
75 |
|
76 /* Now we try to find the variable instance declaration, to determine its type... */ |
|
77 symbol_c *var_decl = search_var_instance_decl.get_decl(var_name_part); |
|
78 if (NULL == var_decl) { |
|
79 /* variable instance declaration not found! */ |
|
80 ERROR; |
|
81 } |
|
82 |
|
83 /* if it is a struct or function block, we must search the type |
|
84 * of the struct or function block member. |
|
85 * This is done by this class visiting the var_decl. |
|
86 * This class, while visiting, will recursively call |
|
87 * decompose_var_instance_name->get_next() when and if required... |
|
88 */ |
|
89 symbol_c *res = (symbol_c *)var_decl->accept(*this); |
|
90 if (NULL == res) ERROR; |
|
91 |
|
92 /* make sure that we have decomposed all strcuture elements of the variable name */ |
|
93 symbol_c *var_name = decompose_var_instance_name->next_part(); |
|
94 if (NULL != var_name) ERROR; |
|
95 |
|
96 return res; |
|
97 } |
|
98 |
|
99 unsigned int get_vartype(symbol_c *variable_name) { |
|
100 this->current_structelement_name = NULL; |
|
101 this->decompose_var_instance_name = new decompose_var_instance_name_c(variable_name); |
|
102 if (NULL == decompose_var_instance_name) ERROR; |
|
103 |
|
104 /* find the part of the variable name that will appear in the |
|
105 * variable declaration, for e.g., in window.point.x, this would be |
|
106 * window! |
|
107 */ |
|
108 symbol_c *var_name_part = decompose_var_instance_name->next_part(); |
|
109 if (NULL == var_name_part) ERROR; |
|
110 |
|
111 /* Now we try to find the variable instance declaration, to determine its type... */ |
|
112 symbol_c *var_decl = search_var_instance_decl.get_decl(var_name_part); |
|
113 if (NULL == var_decl) { |
|
114 /* variable instance declaration not found! */ |
|
115 return 0; |
|
116 } |
|
117 |
|
118 /* if it is a struct or function block, we must search the type |
|
119 * of the struct or function block member. |
|
120 * This is done by this class visiting the var_decl. |
|
121 * This class, while visiting, will recursively call |
|
122 * decompose_var_instance_name->get_next() when and if required... |
|
123 */ |
|
124 unsigned int res = search_var_instance_decl.get_vartype(); |
|
125 |
|
126 /* make sure that we have decomposed all strcuture elements of the variable name */ |
|
127 symbol_c *var_name = decompose_var_instance_name->next_part(); |
|
128 if (NULL != var_name) ERROR; |
|
129 |
|
130 return res; |
|
131 } |
|
132 |
|
133 private: |
|
134 /* a helper function... */ |
|
135 void *visit_list(list_c *list) { |
|
136 if (NULL == current_structelement_name) ERROR; |
|
137 |
|
138 for(int i = 0; i < list->n; i++) { |
|
139 void *res = list->elements[i]->accept(*this); |
|
140 if (res != NULL) |
|
141 return res; |
|
142 } |
|
143 /* not found! */ |
|
144 return NULL; |
|
145 } |
|
146 |
|
147 /* a helper function... */ |
|
148 void *base_type(symbol_c *symbol) { |
|
149 search_base_type_c search_base_type; |
|
150 return symbol->accept(search_base_type); |
|
151 } |
|
152 |
|
153 |
|
154 private: |
|
155 /* We override the base class' visitor to identifier_c. |
|
156 * This is so because the base class does not consider a function block |
|
157 * to be a type, unlike this class that allows a variable instance |
|
158 * of a function block type... |
|
159 */ |
|
160 void *visit(identifier_c *type_name) { |
|
161 /* look up the type declaration... */ |
|
162 symbol_c *fb_decl = function_block_type_symtable.find_value(type_name); |
|
163 if (fb_decl != function_block_type_symtable.end_value()) |
|
164 /* Type declaration found!! */ |
|
165 return fb_decl->accept(*this); |
|
166 |
|
167 /* No. It is not a function block, so we let |
|
168 * the base class take care of it... |
|
169 */ |
|
170 if (this->search_base_type) |
|
171 return search_base_type_c::visit(type_name); |
|
172 else |
|
173 return type_name; |
|
174 } |
|
175 |
|
176 /********************************/ |
|
177 /* B 1.3.3 - Derived data types */ |
|
178 /********************************/ |
|
179 |
|
180 /* identifier ':' array_spec_init */ |
|
181 void *visit(array_type_declaration_c *symbol) { |
|
182 return symbol->array_spec_init->accept(*this); |
|
183 } |
|
184 |
|
185 /* array_specification [ASSIGN array_initialization} */ |
|
186 /* array_initialization may be NULL ! */ |
|
187 void *visit(array_spec_init_c *symbol) { |
|
188 symbol_c *var_name = decompose_var_instance_name->next_part(); |
|
189 if (NULL != var_name) |
|
190 current_structelement_name = var_name; |
|
191 return symbol->array_specification->accept(*this); |
|
192 } |
|
193 |
|
194 /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */ |
|
195 void *visit(array_specification_c *symbol) { |
|
196 symbol_c *var_name = decompose_var_instance_name->next_part(); |
|
197 if (NULL != var_name) |
|
198 current_structelement_name = var_name; |
|
199 return symbol->non_generic_type_name->accept(*this); |
|
200 } |
|
201 |
|
202 /* structure_type_name ':' structure_specification */ |
|
203 void *visit(structure_type_declaration_c *symbol) { |
|
204 return symbol->structure_specification->accept(*this); |
|
205 /* NOTE: structure_specification will point to either a |
|
206 * initialized_structure_c |
|
207 * OR A |
|
208 * structure_element_declaration_list_c |
|
209 */ |
|
210 } |
|
211 |
|
212 /* structure_type_name ASSIGN structure_initialization */ |
|
213 /* structure_initialization may be NULL ! */ |
|
214 // SYM_REF2(initialized_structure_c, structure_type_name, structure_initialization) |
|
215 void *visit(initialized_structure_c *symbol) { |
|
216 /* make sure that we have decomposed all strcuture elements of the variable name */ |
|
217 symbol_c *var_name = decompose_var_instance_name->next_part(); |
|
218 if (NULL == var_name) { |
|
219 /* this is it... ! |
|
220 * No need to look any further... |
|
221 */ |
|
222 /* NOTE: we could simply do a |
|
223 * return (void *)symbol; |
|
224 * nevertheless, note that this search_varfb_instance_type_c |
|
225 * class inherits from the search_base_type_c class, |
|
226 * which means that it will usually return the base type, |
|
227 * and not the derived type (*). If we are to be consistent, |
|
228 * we should guarantee that we always return the base type. |
|
229 * To do this we could use |
|
230 * return (void *)symbol->accept(*this); |
|
231 * since this class inherits from the search_base_type_c. |
|
232 * However, in this case we don't want it to follow |
|
233 * the structs as this search_varfb_instance_type_c does. |
|
234 * We therefore have to create a new search_base_type_c |
|
235 * instance to search through this type without going |
|
236 * through the structs... |
|
237 */ |
|
238 return base_type(symbol->structure_type_name); |
|
239 } |
|
240 |
|
241 /* now search the structure declaration */ |
|
242 current_structelement_name = var_name; |
|
243 /* recursively find out the data type of var_name... */ |
|
244 return symbol->structure_type_name->accept(*this); |
|
245 } |
|
246 |
|
247 /* helper symbol for structure_declaration */ |
|
248 /* structure_declaration: STRUCT structure_element_declaration_list END_STRUCT */ |
|
249 /* structure_element_declaration_list structure_element_declaration ';' */ |
|
250 void *visit(structure_element_declaration_list_c *symbol) { |
|
251 return visit_list(symbol); |
|
252 } |
|
253 |
|
254 /* structure_element_name ':' spec_init */ |
|
255 void *visit(structure_element_declaration_c *symbol) { |
|
256 if (NULL == current_structelement_name) ERROR; |
|
257 |
|
258 if (compare_identifiers(symbol->structure_element_name, current_structelement_name) == 0) |
|
259 return symbol->spec_init->accept(*this); |
|
260 |
|
261 return NULL; |
|
262 } |
|
263 |
|
264 /* helper symbol for structure_initialization */ |
|
265 /* structure_initialization: '(' structure_element_initialization_list ')' */ |
|
266 /* structure_element_initialization_list ',' structure_element_initialization */ |
|
267 void *visit(structure_element_initialization_list_c *symbol) {ERROR; return NULL;} /* should never get called... */ |
|
268 /* structure_element_name ASSIGN value */ |
|
269 void *visit(structure_element_initialization_c *symbol) {ERROR; return NULL;} /* should never get called... */ |
|
270 |
|
271 |
|
272 |
|
273 /**************************************/ |
|
274 /* B.1.5 - Program organization units */ |
|
275 /**************************************/ |
|
276 /*****************************/ |
|
277 /* B 1.5.2 - Function Blocks */ |
|
278 /*****************************/ |
|
279 /* FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */ |
|
280 // SYM_REF4(function_block_declaration_c, fblock_name, var_declarations, fblock_body, unused) |
|
281 void *visit(function_block_declaration_c *symbol) { |
|
282 /* make sure that we have decomposed all strcuture elements of the variable name */ |
|
283 |
|
284 symbol_c *var_name = decompose_var_instance_name->next_part(); |
|
285 if (NULL == var_name) { |
|
286 /* this is it... ! |
|
287 * No need to look any further... |
|
288 * Note also that, unlike for the struct types, a function block may |
|
289 * not be defined based on another (i.e. no inheritance is allowed), |
|
290 * so this function block is already the most base type. |
|
291 * We simply return it. |
|
292 */ |
|
293 return (void *)symbol; |
|
294 } |
|
295 |
|
296 /* now search the function block declaration for the variable... */ |
|
297 search_var_instance_decl_c search_decl(symbol); |
|
298 symbol_c *var_decl = search_decl.get_decl(var_name); |
|
299 if (NULL == var_decl) { |
|
300 /* variable instance declaration not found! */ |
|
301 return NULL; |
|
302 } |
|
303 |
|
304 /* We have found the declaration. |
|
305 * Should we look any further? |
|
306 */ |
|
307 var_name = decompose_var_instance_name->next_part(); |
|
308 if (NULL == var_name) { |
|
309 /* this is it... ! */ |
|
310 return base_type(var_decl); |
|
311 } |
|
312 |
|
313 current_structelement_name = var_name; |
|
314 /* recursively find out the data type of var_name... */ |
|
315 return symbol->var_declarations->accept(*this); |
|
316 } |
|
317 |
|
318 }; |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|