author | Mario de Sousa <msousa@fe.up.pt> |
Tue, 05 Apr 2011 19:42:33 +0100 | |
changeset 284 | 64a4504633d4 |
parent 279 | c0453b7f99df |
child 321 | a96399ab57c2 |
permissions | -rwxr-xr-x |
181 | 1 |
/* |
265
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
2 |
* matiec - a compiler for the programming languages defined in IEC 61131-3 |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
3 |
* |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
4 |
* Copyright (C) 2003-2011 Mario de Sousa (msousa@fe.up.pt) |
279
c0453b7f99df
Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents:
265
diff
changeset
|
5 |
* Copyright (C) 2007-2011 Laurent Bessard and Edouard Tisserant |
265
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
6 |
* |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
7 |
* This program is free software: you can redistribute it and/or modify |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
8 |
* it under the terms of the GNU General Public License as published by |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
9 |
* the Free Software Foundation, either version 3 of the License, or |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
10 |
* (at your option) any later version. |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
11 |
* |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
12 |
* This program is distributed in the hope that it will be useful, |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
15 |
* GNU General Public License for more details. |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
16 |
* |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
18 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
19 |
* |
181 | 20 |
* |
21 |
* This code is made available on the understanding that it will not be |
|
22 |
* used in safety-critical situations without a full and competent review. |
|
23 |
*/ |
|
24 |
||
25 |
/* |
|
265
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
238
diff
changeset
|
26 |
* An IEC 61131-3 compiler. |
181 | 27 |
* |
28 |
* Based on the |
|
29 |
* FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) |
|
30 |
* |
|
31 |
*/ |
|
32 |
||
33 |
/* Determine the data type of a variable. |
|
34 |
* The variable may be a simple variable, a function block instance, a |
|
35 |
* struture element within a data structured type (a struct or a fb), or |
|
36 |
* an array element. |
|
37 |
* A mixture of array element of a structure element of a structure element |
|
38 |
* of a .... is also suported! |
|
39 |
* |
|
40 |
* A reference to the relevant base type __definition__ is returned. |
|
41 |
* This means that if we find that the variable is of type MY_INT, |
|
42 |
* which was previously declared to be |
|
43 |
* TYPE MY_INT: INT := 9; |
|
44 |
* this class wil return INT, and __not__ MY_INT !! |
|
45 |
* |
|
46 |
* |
|
47 |
* example: |
|
48 |
* window.points[1].coordinate.x |
|
49 |
* window.points[1].colour |
|
50 |
* etc... ARE ALLOWED! |
|
51 |
* |
|
52 |
* This class must be passed the scope within which the |
|
53 |
* variable was declared, and the variable name... |
|
54 |
*/ |
|
55 |
||
56 |
||
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
57 |
/* |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
181
diff
changeset
|
58 |
* 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
|
59 |
* 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
|
60 |
*/ |
181 | 61 |
#include "absyntax_utils.hh" |
62 |
||
63 |
||
64 |
search_varfb_instance_type_c::search_varfb_instance_type_c(symbol_c *search_scope): search_var_instance_decl(search_scope) { |
|
65 |
this->decompose_var_instance_name = NULL; |
|
66 |
this->current_structelement_name = NULL; |
|
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
67 |
this->current_rawtype = NULL; |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
68 |
} |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
69 |
|
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
70 |
symbol_c *search_varfb_instance_type_c::get_type(symbol_c *variable_name) { |
181 | 71 |
this->current_structelement_name = NULL; |
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
72 |
this->current_rawtype = NULL; |
181 | 73 |
this->decompose_var_instance_name = new decompose_var_instance_name_c(variable_name); |
74 |
if (NULL == decompose_var_instance_name) ERROR; |
|
75 |
||
76 |
/* find the part of the variable name that will appear in the |
|
77 |
* variable declaration, for e.g., in window.point.x, this would be |
|
78 |
* window! |
|
79 |
*/ |
|
80 |
symbol_c *var_name_part = decompose_var_instance_name->next_part(); |
|
81 |
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
|
82 |
|
181 | 83 |
/* Now we try to find the variable instance declaration, to determine its type... */ |
84 |
symbol_c *var_decl = search_var_instance_decl.get_decl(var_name_part); |
|
85 |
if (NULL == var_decl) { |
|
86 |
/* variable instance declaration not found! */ |
|
87 |
ERROR; |
|
88 |
} |
|
89 |
||
90 |
/* if it is a struct or function block, we must search the type |
|
91 |
* of the struct or function block member. |
|
92 |
* This is done by this class visiting the var_decl. |
|
93 |
* This class, while visiting, will recursively call |
|
94 |
* decompose_var_instance_name->get_next() when and if required... |
|
95 |
*/ |
|
96 |
symbol_c *res = (symbol_c *)var_decl->accept(*this); |
|
97 |
if (NULL == res) ERROR; |
|
98 |
||
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
99 |
/* make sure that we have decomposed all structure elements of the variable name */ |
181 | 100 |
symbol_c *var_name = decompose_var_instance_name->next_part(); |
101 |
if (NULL != var_name) ERROR; |
|
102 |
||
103 |
return res; |
|
104 |
} |
|
105 |
||
106 |
unsigned int search_varfb_instance_type_c::get_vartype(symbol_c *variable_name) { |
|
107 |
this->current_structelement_name = NULL; |
|
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
108 |
this->current_rawtype = NULL; |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
109 |
this->is_complex = false; |
181 | 110 |
this->decompose_var_instance_name = new decompose_var_instance_name_c(variable_name); |
111 |
if (NULL == decompose_var_instance_name) ERROR; |
|
112 |
||
113 |
/* find the part of the variable name that will appear in the |
|
114 |
* variable declaration, for e.g., in window.point.x, this would be |
|
115 |
* window! |
|
116 |
*/ |
|
117 |
symbol_c *var_name_part = decompose_var_instance_name->next_part(); |
|
118 |
if (NULL == var_name_part) ERROR; |
|
119 |
||
120 |
/* Now we try to find the variable instance declaration, to determine its type... */ |
|
121 |
symbol_c *var_decl = search_var_instance_decl.get_decl(var_name_part); |
|
122 |
if (NULL == var_decl) { |
|
123 |
/* variable instance declaration not found! */ |
|
124 |
return 0; |
|
125 |
} |
|
126 |
||
127 |
/* if it is a struct or function block, we must search the type |
|
128 |
* of the struct or function block member. |
|
129 |
* This is done by this class visiting the var_decl. |
|
130 |
* This class, while visiting, will recursively call |
|
131 |
* decompose_var_instance_name->get_next() when and if required... |
|
132 |
*/ |
|
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
133 |
var_decl->accept(*this); |
181 | 134 |
unsigned int res = search_var_instance_decl.get_vartype(); |
135 |
||
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
136 |
/* make sure that we have decomposed all structure elements of the variable name */ |
181 | 137 |
symbol_c *var_name = decompose_var_instance_name->next_part(); |
138 |
if (NULL != var_name) ERROR; |
|
139 |
||
140 |
return res; |
|
141 |
} |
|
142 |
||
226
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
143 |
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
|
144 |
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
|
145 |
if (this->current_rawtype != NULL) |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
146 |
return this->current_rawtype; |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
147 |
else |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
148 |
return rawtype; |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
149 |
} |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
150 |
|
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
151 |
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
|
152 |
return this->is_complex; |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
153 |
} |
29f8ffc203c1
Adding support for setter and getter on complex datatype in ST
laurent
parents:
202
diff
changeset
|
154 |
|
181 | 155 |
/* a helper function... */ |
156 |
void *search_varfb_instance_type_c::visit_list(list_c *list) { |
|
157 |
if (NULL == current_structelement_name) ERROR; |
|
158 |
||
159 |
for(int i = 0; i < list->n; i++) { |
|
160 |
void *res = list->elements[i]->accept(*this); |
|
161 |
if (res != NULL) |
|
162 |
return res; |
|
163 |
} |
|
164 |
/* not found! */ |
|
165 |
return NULL; |
|
166 |
} |
|
167 |
||
168 |
/* a helper function... */ |
|
169 |
void *search_varfb_instance_type_c::base_type(symbol_c *symbol) { |
|
170 |
search_base_type_c search_base_type; |
|
171 |
return symbol->accept(search_base_type); |
|
172 |
} |
|
173 |
||
174 |
/* We override the base class' visitor to identifier_c. |
|
175 |
* This is so because the base class does not consider a function block |
|
176 |
* to be a type, unlike this class that allows a variable instance |
|
177 |
* of a function block type... |
|
178 |
*/ |
|
179 |
void *search_varfb_instance_type_c::visit(identifier_c *type_name) { |
|
180 |
/* look up the type declaration... */ |
|
181 |
symbol_c *fb_decl = function_block_type_symtable.find_value(type_name); |
|
182 |
if (fb_decl != function_block_type_symtable.end_value()) |
|
183 |
/* Type declaration found!! */ |
|
184 |
return fb_decl->accept(*this); |
|
185 |
||
235 | 186 |
this->current_rawtype = type_name; |
187 |
||
181 | 188 |
/* No. It is not a function block, so we let |
189 |
* the base class take care of it... |
|
190 |
*/ |
|
238
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
235
diff
changeset
|
191 |
if (NULL == decompose_var_instance_name->next_part(false)) { |
235 | 192 |
return base_type(type_name); |
193 |
} |
|
238
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
235
diff
changeset
|
194 |
else { |
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
235
diff
changeset
|
195 |
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
|
196 |
} |
181 | 197 |
} |
198 |
||
199 |
/********************************/ |
|
200 |
/* B 1.3.3 - Derived data types */ |
|
201 |
/********************************/ |
|
202 |
||
203 |
/* identifier ':' array_spec_init */ |
|
204 |
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
|
205 |
this->is_complex = true; |
181 | 206 |
return symbol->array_spec_init->accept(*this); |
207 |
} |
|
208 |
||
238
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
235
diff
changeset
|
209 |
/* array_specification [ASSIGN array_initialization] */ |
181 | 210 |
/* array_initialization may be NULL ! */ |
211 |
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
|
212 |
this->is_complex = true; |
181 | 213 |
return symbol->array_specification->accept(*this); |
214 |
} |
|
238
0919986a5c98
Bug when trying to get type of elements in a big complex structure fixed
laurent
parents:
235
diff
changeset
|
215 |
|
181 | 216 |
/* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */ |
217 |
void *search_varfb_instance_type_c::visit(array_specification_c *symbol) { |
|
235 | 218 |
this->is_complex = true; |
181 | 219 |
return symbol->non_generic_type_name->accept(*this); |
220 |
} |
|
221 |
||
222 |
/* structure_type_name ':' structure_specification */ |
|
223 |
void *search_varfb_instance_type_c::visit(structure_type_declaration_c *symbol) { |
|
235 | 224 |
this->is_complex = true; |
181 | 225 |
return symbol->structure_specification->accept(*this); |
226 |
/* NOTE: structure_specification will point to either a |
|
227 |
* initialized_structure_c |
|
228 |
* OR A |
|
229 |
* structure_element_declaration_list_c |
|
230 |
*/ |
|
231 |
} |
|
232 |
||
233 |
/* structure_type_name ASSIGN structure_initialization */ |
|
234 |
/* structure_initialization may be NULL ! */ |
|
235 |
// SYM_REF2(initialized_structure_c, structure_type_name, structure_initialization) |
|
236 |
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
|
237 |
this->is_complex = true; |
181 | 238 |
/* recursively find out the data type of var_name... */ |
239 |
return symbol->structure_type_name->accept(*this); |
|
240 |
} |
|
241 |
||
242 |
/* helper symbol for structure_declaration */ |
|
243 |
/* structure_declaration: STRUCT structure_element_declaration_list END_STRUCT */ |
|
244 |
/* structure_element_declaration_list structure_element_declaration ';' */ |
|
245 |
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
|
246 |
/* 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
|
247 |
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
|
248 |
/* now search the structure declaration */ |
181 | 249 |
return visit_list(symbol); |
250 |
} |
|
251 |
||
252 |
/* structure_element_name ':' spec_init */ |
|
253 |
void *search_varfb_instance_type_c::visit(structure_element_declaration_c *symbol) { |
|
254 |
if (NULL == current_structelement_name) ERROR; |
|
255 |
||
256 |
if (compare_identifiers(symbol->structure_element_name, current_structelement_name) == 0) |
|
257 |
return symbol->spec_init->accept(*this); |
|
258 |
||
259 |
return NULL; |
|
260 |
} |
|
261 |
||
262 |
/* helper symbol for structure_initialization */ |
|
263 |
/* structure_initialization: '(' structure_element_initialization_list ')' */ |
|
264 |
/* structure_element_initialization_list ',' structure_element_initialization */ |
|
265 |
void *search_varfb_instance_type_c::visit(structure_element_initialization_list_c *symbol) {ERROR; return NULL;} /* should never get called... */ |
|
266 |
/* structure_element_name ASSIGN value */ |
|
267 |
void *search_varfb_instance_type_c::visit(structure_element_initialization_c *symbol) {ERROR; return NULL;} /* should never get called... */ |
|
268 |
||
269 |
||
270 |
||
271 |
/**************************************/ |
|
272 |
/* B.1.5 - Program organization units */ |
|
273 |
/**************************************/ |
|
274 |
/*****************************/ |
|
275 |
/* B 1.5.2 - Function Blocks */ |
|
276 |
/*****************************/ |
|
277 |
/* FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */ |
|
278 |
// SYM_REF4(function_block_declaration_c, fblock_name, var_declarations, fblock_body, unused) |
|
279 |
void *search_varfb_instance_type_c::visit(function_block_declaration_c *symbol) { |
|
280 |
/* make sure that we have decomposed all strcuture elements of the variable name */ |
|
281 |
||
282 |
symbol_c *var_name = decompose_var_instance_name->next_part(); |
|
283 |
if (NULL == var_name) { |
|
284 |
/* this is it... ! |
|
285 |
* No need to look any further... |
|
286 |
* Note also that, unlike for the struct types, a function block may |
|
287 |
* not be defined based on another (i.e. no inheritance is allowed), |
|
288 |
* so this function block is already the most base type. |
|
289 |
* We simply return it. |
|
290 |
*/ |
|
291 |
return (void *)symbol; |
|
292 |
} |
|
293 |
||
294 |
/* now search the function block declaration for the variable... */ |
|
295 |
search_var_instance_decl_c search_decl(symbol); |
|
296 |
symbol_c *var_decl = search_decl.get_decl(var_name); |
|
297 |
if (NULL == var_decl) { |
|
298 |
/* variable instance declaration not found! */ |
|
299 |
return NULL; |
|
300 |
} |
|
301 |
||
302 |
/* We have found the declaration. |
|
303 |
* Should we look any further? |
|
304 |
*/ |
|
305 |
var_name = decompose_var_instance_name->next_part(); |
|
306 |
if (NULL == var_name) { |
|
307 |
/* this is it... ! */ |
|
308 |
return base_type(var_decl); |
|
309 |
} |
|
310 |
||
311 |
current_structelement_name = var_name; |
|
312 |
/* recursively find out the data type of var_name... */ |
|
313 |
return symbol->var_declarations->accept(*this); |
|
314 |
} |