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