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