author | laurent |
Thu, 22 Apr 2010 00:29:30 +0200 | |
changeset 239 | b3063ca418c1 |
parent 238 | 0919986a5c98 |
child 265 | 4d222f46f8cc |
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 |
||
235 | 178 |
this->current_rawtype = type_name; |
179 |
||
181 | 180 |
/* No. It is not a function block, so we let |
181 |
* the base class take care of it... |
|
182 |
*/ |
|
238
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
235
diff
changeset
|
183 |
if (NULL == decompose_var_instance_name->next_part(false)) { |
235 | 184 |
return base_type(type_name); |
185 |
} |
|
238
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
235
diff
changeset
|
186 |
else { |
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
235
diff
changeset
|
187 |
return search_base_type_c::visit(type_name); |
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
235
diff
changeset
|
188 |
} |
181 | 189 |
} |
190 |
||
191 |
/********************************/ |
|
192 |
/* B 1.3.3 - Derived data types */ |
|
193 |
/********************************/ |
|
194 |
||
195 |
/* identifier ':' array_spec_init */ |
|
196 |
void *search_varfb_instance_type_c::visit(array_type_declaration_c *symbol) { |
|
238
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
235
diff
changeset
|
197 |
this->is_complex = true; |
181 | 198 |
return symbol->array_spec_init->accept(*this); |
199 |
} |
|
200 |
||
238
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
235
diff
changeset
|
201 |
/* array_specification [ASSIGN array_initialization] */ |
181 | 202 |
/* array_initialization may be NULL ! */ |
203 |
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
|
204 |
this->is_complex = true; |
181 | 205 |
return symbol->array_specification->accept(*this); |
206 |
} |
|
238
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
235
diff
changeset
|
207 |
|
181 | 208 |
/* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */ |
209 |
void *search_varfb_instance_type_c::visit(array_specification_c *symbol) { |
|
235 | 210 |
this->is_complex = true; |
181 | 211 |
return symbol->non_generic_type_name->accept(*this); |
212 |
} |
|
213 |
||
214 |
/* structure_type_name ':' structure_specification */ |
|
215 |
void *search_varfb_instance_type_c::visit(structure_type_declaration_c *symbol) { |
|
235 | 216 |
this->is_complex = true; |
181 | 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; |
181 | 230 |
/* recursively find out the data type of var_name... */ |
231 |
return symbol->structure_type_name->accept(*this); |
|
232 |
} |
|
233 |
||
234 |
/* helper symbol for structure_declaration */ |
|
235 |
/* structure_declaration: STRUCT structure_element_declaration_list END_STRUCT */ |
|
236 |
/* structure_element_declaration_list structure_element_declaration ';' */ |
|
237 |
void *search_varfb_instance_type_c::visit(structure_element_declaration_list_c *symbol) { |
|
238
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
235
diff
changeset
|
238 |
/* make sure that we have decomposed all structure elements of the variable name */ |
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
235
diff
changeset
|
239 |
current_structelement_name = decompose_var_instance_name->next_part(); |
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
235
diff
changeset
|
240 |
/* now search the structure declaration */ |
181 | 241 |
return visit_list(symbol); |
242 |
} |
|
243 |
||
244 |
/* structure_element_name ':' spec_init */ |
|
245 |
void *search_varfb_instance_type_c::visit(structure_element_declaration_c *symbol) { |
|
246 |
if (NULL == current_structelement_name) ERROR; |
|
247 |
||
248 |
if (compare_identifiers(symbol->structure_element_name, current_structelement_name) == 0) |
|
249 |
return symbol->spec_init->accept(*this); |
|
250 |
||
251 |
return NULL; |
|
252 |
} |
|
253 |
||
254 |
/* helper symbol for structure_initialization */ |
|
255 |
/* structure_initialization: '(' structure_element_initialization_list ')' */ |
|
256 |
/* structure_element_initialization_list ',' structure_element_initialization */ |
|
257 |
void *search_varfb_instance_type_c::visit(structure_element_initialization_list_c *symbol) {ERROR; return NULL;} /* should never get called... */ |
|
258 |
/* structure_element_name ASSIGN value */ |
|
259 |
void *search_varfb_instance_type_c::visit(structure_element_initialization_c *symbol) {ERROR; return NULL;} /* should never get called... */ |
|
260 |
||
261 |
||
262 |
||
263 |
/**************************************/ |
|
264 |
/* B.1.5 - Program organization units */ |
|
265 |
/**************************************/ |
|
266 |
/*****************************/ |
|
267 |
/* B 1.5.2 - Function Blocks */ |
|
268 |
/*****************************/ |
|
269 |
/* FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */ |
|
270 |
// SYM_REF4(function_block_declaration_c, fblock_name, var_declarations, fblock_body, unused) |
|
271 |
void *search_varfb_instance_type_c::visit(function_block_declaration_c *symbol) { |
|
272 |
/* make sure that we have decomposed all strcuture elements of the variable name */ |
|
273 |
||
274 |
symbol_c *var_name = decompose_var_instance_name->next_part(); |
|
275 |
if (NULL == var_name) { |
|
276 |
/* this is it... ! |
|
277 |
* No need to look any further... |
|
278 |
* Note also that, unlike for the struct types, a function block may |
|
279 |
* not be defined based on another (i.e. no inheritance is allowed), |
|
280 |
* so this function block is already the most base type. |
|
281 |
* We simply return it. |
|
282 |
*/ |
|
283 |
return (void *)symbol; |
|
284 |
} |
|
285 |
||
286 |
/* now search the function block declaration for the variable... */ |
|
287 |
search_var_instance_decl_c search_decl(symbol); |
|
288 |
symbol_c *var_decl = search_decl.get_decl(var_name); |
|
289 |
if (NULL == var_decl) { |
|
290 |
/* variable instance declaration not found! */ |
|
291 |
return NULL; |
|
292 |
} |
|
293 |
||
294 |
/* We have found the declaration. |
|
295 |
* Should we look any further? |
|
296 |
*/ |
|
297 |
var_name = decompose_var_instance_name->next_part(); |
|
298 |
if (NULL == var_name) { |
|
299 |
/* this is it... ! */ |
|
300 |
return base_type(var_decl); |
|
301 |
} |
|
302 |
||
303 |
current_structelement_name = var_name; |
|
304 |
/* recursively find out the data type of var_name... */ |
|
305 |
return symbol->var_declarations->accept(*this); |
|
306 |
} |