author | Catarina Boucinha <ccb@fe.up.pt> |
Thu, 02 Jul 2009 11:26:25 +0100 | |
changeset 201 | e657008f43d0 |
parent 196 | 1c0c7a664fc2 |
child 202 | da1a8186f86f |
permissions | -rw-r--r-- |
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 |
||
26 |
/* Determine the data type of an ST expression. |
|
27 |
* A reference to the relevant type definition is returned. |
|
28 |
* |
|
29 |
* For example: |
|
30 |
* 2 + 3 -> returns reference to a int_type_name_c object. |
|
31 |
* 22.2 - 5 -> returns reference to a real_type_name_c object. |
|
32 |
* etc... |
|
33 |
*/ |
|
34 |
||
35 |
||
36 |
||
37 |
#include "absyntax_utils.hh" |
|
38 |
#include <typeinfo> |
|
39 |
||
40 |
search_expression_type_c::search_expression_type_c(symbol_c *search_scope) { |
|
41 |
search_varfb_instance_type = new search_varfb_instance_type_c(search_scope); |
|
42 |
} |
|
43 |
||
44 |
search_expression_type_c::~search_expression_type_c(void) { |
|
45 |
delete search_varfb_instance_type; |
|
46 |
} |
|
47 |
||
48 |
/* A helper function... */ |
|
49 |
bool search_expression_type_c::is_bool_type(symbol_c *type_symbol) { |
|
50 |
bool_type_name_c tt; |
|
51 |
if (type_symbol == NULL) {return true;} |
|
52 |
return (typeid(*type_symbol) == typeid(bool_type_name_c)); |
|
53 |
} |
|
54 |
||
55 |
/* A helper function... */ |
|
56 |
bool search_expression_type_c::is_time_type(symbol_c *type_symbol) { |
|
57 |
if (type_symbol == NULL) {return true;} |
|
58 |
if (typeid(*type_symbol) == typeid(time_type_name_c)) {return true;} |
|
59 |
if (typeid(*type_symbol) == typeid(date_type_name_c)) {return true;} |
|
60 |
if (typeid(*type_symbol) == typeid(tod_type_name_c)) {return true;} |
|
61 |
if (typeid(*type_symbol) == typeid(dt_type_name_c)) {return true;} |
|
62 |
return false; |
|
63 |
} |
|
64 |
||
65 |
/* A helper function... */ |
|
66 |
bool search_expression_type_c::is_string_type(symbol_c *type_symbol) { |
|
67 |
if (type_symbol == NULL) {return true;} |
|
68 |
if (typeid(*type_symbol) == typeid(string_type_name_c)) {return true;} |
|
69 |
if (typeid(*type_symbol) == typeid(wstring_type_name_c)) {return true;} |
|
70 |
return false; |
|
71 |
} |
|
72 |
||
73 |
/* A helper function... */ |
|
74 |
bool search_expression_type_c::is_integer_type(symbol_c *type_symbol) { |
|
75 |
if (type_symbol == NULL) {return true;} |
|
76 |
if (typeid(*type_symbol) == typeid(sint_type_name_c)) {return true;} |
|
77 |
if (typeid(*type_symbol) == typeid(int_type_name_c)) {return true;} |
|
78 |
if (typeid(*type_symbol) == typeid(dint_type_name_c)) {return true;} |
|
79 |
if (typeid(*type_symbol) == typeid(lint_type_name_c)) {return true;} |
|
80 |
if (typeid(*type_symbol) == typeid(usint_type_name_c)) {return true;} |
|
81 |
if (typeid(*type_symbol) == typeid(uint_type_name_c)) {return true;} |
|
82 |
if (typeid(*type_symbol) == typeid(udint_type_name_c)) {return true;} |
|
83 |
if (typeid(*type_symbol) == typeid(ulint_type_name_c)) {return true;} |
|
84 |
if (typeid(*type_symbol) == typeid(constant_int_type_name_c)) {return true;} |
|
85 |
return false; |
|
86 |
} |
|
87 |
||
88 |
bool search_expression_type_c::is_real_type(symbol_c *type_symbol) { |
|
89 |
if (type_symbol == NULL) {return true;} |
|
90 |
if (typeid(*type_symbol) == typeid(real_type_name_c)) {return true;} |
|
91 |
if (typeid(*type_symbol) == typeid(lreal_type_name_c)) {return true;} |
|
92 |
if (typeid(*type_symbol) == typeid(constant_real_type_name_c)) {return true;} |
|
93 |
return false; |
|
94 |
} |
|
95 |
||
96 |
bool search_expression_type_c::is_num_type(symbol_c *type_symbol) { |
|
97 |
if (type_symbol == NULL) {return true;} |
|
98 |
return is_real_type(type_symbol) || is_integer_type(type_symbol); |
|
99 |
} |
|
100 |
||
101 |
bool search_expression_type_c::is_nbinary_type(symbol_c *type_symbol) { |
|
102 |
if (type_symbol == NULL) {return true;} |
|
103 |
if (typeid(*type_symbol) == typeid(byte_type_name_c)) {return true;} |
|
104 |
if (typeid(*type_symbol) == typeid(word_type_name_c)) {return true;} |
|
105 |
if (typeid(*type_symbol) == typeid(dword_type_name_c)) {return true;} |
|
106 |
if (typeid(*type_symbol) == typeid(lword_type_name_c)) {return true;} |
|
107 |
if (typeid(*type_symbol) == typeid(constant_int_type_name_c)) {return true;} |
|
108 |
return false; |
|
109 |
} |
|
110 |
||
111 |
bool search_expression_type_c::is_binary_type(symbol_c *type_symbol) { |
|
112 |
if (type_symbol == NULL) {return true;} |
|
113 |
if (typeid(*type_symbol) == typeid(bool_type_name_c)) {return true;} |
|
114 |
return is_nbinary_type(type_symbol); |
|
115 |
} |
|
116 |
||
117 |
bool search_expression_type_c::is_same_type(symbol_c *first_type, symbol_c *second_type) { |
|
118 |
if (first_type == NULL || second_type == NULL) {return true;} |
|
119 |
if (typeid(*first_type) == typeid(*second_type)) {return true;} |
|
120 |
if (is_integer_type(first_type) && (typeid(*second_type) == typeid(constant_int_type_name_c))) {return true;} |
|
121 |
if ((typeid(*first_type) == typeid(constant_int_type_name_c) && is_integer_type(second_type))) {return true;} |
|
122 |
if (is_binary_type(first_type) && (typeid(*second_type) == typeid(constant_int_type_name_c))) {return true;} |
|
123 |
if ((typeid(*first_type) == typeid(constant_int_type_name_c) && is_binary_type(second_type))) {return true;} |
|
124 |
if (is_real_type(first_type) && (typeid(*second_type) == typeid(constant_real_type_name_c))) {return true;} |
|
125 |
if ((typeid(*first_type) == typeid(constant_real_type_name_c) && is_real_type(second_type))) {return true;} |
|
126 |
return false; |
|
127 |
} |
|
128 |
||
129 |
symbol_c* search_expression_type_c::common_type(symbol_c *first_type, symbol_c *second_type) { |
|
130 |
if (first_type == NULL && second_type == NULL) {return NULL;} |
|
131 |
if (first_type == NULL) {return second_type;} |
|
132 |
if (second_type == NULL) {return first_type;} |
|
133 |
if (typeid(*first_type) == typeid(*second_type)) {return first_type;} |
|
194
e18690830555
Removing un-necessary symbol direct_variable_type_name_c from abstract syntax tree
mario
parents:
181
diff
changeset
|
134 |
if (is_integer_type(first_type) && (typeid(*second_type) == typeid(constant_int_type_name_c))) {return first_type;} |
e18690830555
Removing un-necessary symbol direct_variable_type_name_c from abstract syntax tree
mario
parents:
181
diff
changeset
|
135 |
if ((typeid(*first_type) == typeid(constant_int_type_name_c)) && is_integer_type(second_type)) {return second_type;} |
e18690830555
Removing un-necessary symbol direct_variable_type_name_c from abstract syntax tree
mario
parents:
181
diff
changeset
|
136 |
if (is_binary_type(first_type) && (typeid(*second_type) == typeid(constant_int_type_name_c))) {return first_type;} |
e18690830555
Removing un-necessary symbol direct_variable_type_name_c from abstract syntax tree
mario
parents:
181
diff
changeset
|
137 |
if ((typeid(*first_type) == typeid(constant_int_type_name_c)) && is_binary_type(second_type)) {return second_type;} |
e18690830555
Removing un-necessary symbol direct_variable_type_name_c from abstract syntax tree
mario
parents:
181
diff
changeset
|
138 |
if (is_real_type(first_type) && (typeid(*second_type) == typeid(constant_real_type_name_c))) {return first_type;} |
e18690830555
Removing un-necessary symbol direct_variable_type_name_c from abstract syntax tree
mario
parents:
181
diff
changeset
|
139 |
if ((typeid(*first_type) == typeid(constant_real_type_name_c)) && is_real_type(second_type)) {return second_type;} |
181 | 140 |
return NULL; |
141 |
} |
|
142 |
||
143 |
#define compute_standard_function_default search_expression_type_c::compute_standard_function_default |
|
144 |
#define compute_standard_function_il search_expression_type_c::compute_standard_function_il |
|
145 |
#include "search_type_code.c" |
|
146 |
#undef compute_standard_function_default |
|
147 |
#undef compute_standard_function_il |
|
148 |
||
149 |
/*static bool_type_name_c bool_type_name;*/ |
|
150 |
||
151 |
/* A helper function... */ |
|
152 |
void *search_expression_type_c::compute_boolean_expression(symbol_c *left_type, symbol_c *right_type) { |
|
153 |
if (!is_same_type(left_type, right_type)) |
|
154 |
ERROR; |
|
155 |
if (!is_bool_type(left_type) && !is_binary_type(left_type)) |
|
156 |
ERROR; |
|
157 |
if (typeid(*left_type) == typeid(constant_int_type_name_c)) {return (void *)right_type;} |
|
158 |
else {return (void *)left_type;} |
|
159 |
} |
|
160 |
||
161 |
/* A helper function... */ |
|
162 |
void *search_expression_type_c::compute_numeric_expression(symbol_c *left_type, symbol_c *right_type) { |
|
163 |
if (!is_same_type(left_type, right_type)) |
|
164 |
ERROR; |
|
165 |
if (!is_integer_type(left_type) && !is_real_type(left_type)) |
|
166 |
ERROR; |
|
167 |
if ((typeid(*left_type) == typeid(constant_int_type_name_c)) || (typeid(*left_type) == typeid(constant_real_type_name_c))) {return (void *)right_type;} |
|
168 |
else {return (void *)left_type;} |
|
169 |
return NULL; |
|
170 |
} |
|
171 |
||
172 |
/* a helper function... */ |
|
173 |
symbol_c *search_expression_type_c::base_type(symbol_c *symbol) { |
|
174 |
return (symbol_c *)symbol->accept(search_base_type); |
|
175 |
} |
|
176 |
||
177 |
/*********************/ |
|
178 |
/* B 1.4 - Variables */ |
|
179 |
/*********************/ |
|
180 |
||
181 |
void *search_expression_type_c::visit(symbolic_variable_c *symbol) { |
|
182 |
symbol_c *res; |
|
183 |
||
184 |
/* Nope, now we assume it is a variable, and determine its type... */ |
|
185 |
res = search_varfb_instance_type->get_type(symbol); |
|
186 |
if (NULL != res) return res; |
|
187 |
||
188 |
return NULL; |
|
189 |
} |
|
190 |
||
191 |
/********************************************/ |
|
192 |
/* B 1.4.1 - Directly Represented Variables */ |
|
193 |
/********************************************/ |
|
194 |
void *search_expression_type_c::visit(direct_variable_c *symbol) { |
|
196
1c0c7a664fc2
Fix problems with direct variables in expression while compiling resulting expression type
lbessard
parents:
194
diff
changeset
|
195 |
symbol_c *res; |
1c0c7a664fc2
Fix problems with direct variables in expression while compiling resulting expression type
lbessard
parents:
194
diff
changeset
|
196 |
|
1c0c7a664fc2
Fix problems with direct variables in expression while compiling resulting expression type
lbessard
parents:
194
diff
changeset
|
197 |
/* Nope, now we assume it is a variable, and determine its type... */ |
1c0c7a664fc2
Fix problems with direct variables in expression while compiling resulting expression type
lbessard
parents:
194
diff
changeset
|
198 |
res = search_varfb_instance_type->get_type(symbol); |
1c0c7a664fc2
Fix problems with direct variables in expression while compiling resulting expression type
lbessard
parents:
194
diff
changeset
|
199 |
if (NULL != res) return res; |
1c0c7a664fc2
Fix problems with direct variables in expression while compiling resulting expression type
lbessard
parents:
194
diff
changeset
|
200 |
|
1c0c7a664fc2
Fix problems with direct variables in expression while compiling resulting expression type
lbessard
parents:
194
diff
changeset
|
201 |
return NULL; |
181 | 202 |
} |
203 |
||
204 |
/*************************************/ |
|
205 |
/* B 1.4.2 - Multi-element variables */ |
|
206 |
/*************************************/ |
|
207 |
||
208 |
void *search_expression_type_c::visit(array_variable_c *symbol) { |
|
209 |
symbol_c *res; |
|
210 |
||
211 |
/* Nope, now we assume it is a variable, and determine its type... */ |
|
212 |
res = search_varfb_instance_type->get_type(symbol); |
|
213 |
if (NULL != res) return res; |
|
214 |
||
215 |
return NULL; |
|
216 |
} |
|
217 |
||
218 |
void *search_expression_type_c::visit(structured_variable_c *symbol) { |
|
219 |
symbol_c *res; |
|
220 |
||
221 |
/* Nope, now we assume it is a variable, and determine its type... */ |
|
222 |
res = search_varfb_instance_type->get_type(symbol); |
|
223 |
if (NULL != res) return res; |
|
224 |
||
225 |
return NULL; |
|
226 |
} |
|
227 |
||
228 |
/***************************************/ |
|
229 |
/* B.3 - Language ST (Structured Text) */ |
|
230 |
/***************************************/ |
|
231 |
/***********************/ |
|
232 |
/* B 3.1 - Expressions */ |
|
233 |
/***********************/ |
|
234 |
void *search_expression_type_c::visit(or_expression_c *symbol) { |
|
235 |
symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
|
236 |
symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
|
237 |
return compute_boolean_expression(left_type, right_type); |
|
238 |
} |
|
239 |
||
240 |
void *search_expression_type_c::visit(xor_expression_c *symbol) { |
|
241 |
symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
|
242 |
symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
|
243 |
return compute_boolean_expression(left_type, right_type); |
|
244 |
} |
|
245 |
||
246 |
void *search_expression_type_c::visit(and_expression_c *symbol) { |
|
247 |
symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
|
248 |
symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
|
249 |
return compute_boolean_expression(left_type, right_type); |
|
250 |
} |
|
251 |
||
252 |
void *search_expression_type_c::visit(equ_expression_c *symbol) {return (void *)&bool_type_name;} |
|
253 |
void *search_expression_type_c::visit(notequ_expression_c *symbol) {return (void *)&bool_type_name;} |
|
254 |
void *search_expression_type_c::visit(lt_expression_c *symbol) {return (void *)&bool_type_name;} |
|
255 |
void *search_expression_type_c::visit(gt_expression_c *symbol) {return (void *)&bool_type_name;} |
|
256 |
void *search_expression_type_c::visit(le_expression_c *symbol) {return (void *)&bool_type_name;} |
|
257 |
void *search_expression_type_c::visit(ge_expression_c *symbol) {return (void *)&bool_type_name;} |
|
258 |
||
259 |
void *search_expression_type_c::visit(add_expression_c *symbol) { |
|
260 |
symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
|
261 |
symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
|
262 |
if (typeid(*left_type) == typeid(time_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) {return (void *)&time_type_name;} |
|
263 |
if (typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) {return (void *)&tod_type_name;} |
|
264 |
if (typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) {return (void *)&dt_type_name;} |
|
265 |
return compute_numeric_expression(left_type, right_type); |
|
266 |
} |
|
267 |
||
268 |
void *search_expression_type_c::visit(sub_expression_c *symbol) { |
|
269 |
symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
|
270 |
symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
|
271 |
if (typeid(*left_type) == typeid(time_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) {return (void *)&time_type_name;} |
|
272 |
if (typeid(*left_type) == typeid(date_type_name_c) && typeid(*right_type) == typeid(date_type_name_c)) {return (void *)&time_type_name;} |
|
273 |
if (typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) {return (void *)&tod_type_name;} |
|
274 |
if (typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(tod_type_name_c)) {return (void *)&time_type_name;} |
|
275 |
if (typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) {return (void *)&dt_type_name;} |
|
276 |
if (typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(dt_type_name_c)) {return (void *)&time_type_name;} |
|
277 |
return compute_numeric_expression(left_type, right_type); |
|
278 |
} |
|
279 |
||
280 |
void *search_expression_type_c::visit(mul_expression_c *symbol) { |
|
281 |
symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
|
282 |
symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
|
283 |
if (typeid(*left_type) == typeid(time_type_name_c) && is_num_type(right_type)) { |
|
284 |
return (void *)&time_type_name; |
|
285 |
} |
|
286 |
return compute_numeric_expression(left_type, right_type); |
|
287 |
} |
|
288 |
||
289 |
void *search_expression_type_c::visit(div_expression_c *symbol) { |
|
290 |
symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
|
291 |
symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
|
292 |
if (typeid(*left_type) == typeid(time_type_name_c) && is_num_type(right_type)){ |
|
293 |
return (void *)&time_type_name; |
|
294 |
} |
|
295 |
return compute_numeric_expression(left_type, right_type); |
|
296 |
} |
|
297 |
||
298 |
void *search_expression_type_c::visit(mod_expression_c *symbol) { |
|
299 |
symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
|
300 |
symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
|
301 |
return compute_numeric_expression(left_type, right_type); |
|
302 |
} |
|
303 |
||
304 |
void *search_expression_type_c::visit(power_expression_c *symbol) { |
|
305 |
symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
|
306 |
symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
|
307 |
if (is_real_type(left_type) && is_num_type(right_type)) { |
|
308 |
return (void *)left_type; |
|
309 |
} |
|
310 |
ERROR; |
|
311 |
return NULL; |
|
312 |
} |
|
313 |
||
314 |
void *search_expression_type_c::visit(neg_expression_c *symbol) { |
|
315 |
symbol_c *exp_type = base_type((symbol_c *)symbol->exp->accept(*this)); |
|
316 |
if (is_num_type(exp_type) || typeid(*exp_type) == typeid(time_type_name_c)){ |
|
317 |
return (void *)exp_type; |
|
318 |
} |
|
319 |
ERROR; |
|
320 |
return NULL; |
|
321 |
} |
|
322 |
||
323 |
void *search_expression_type_c::visit(not_expression_c *symbol) { |
|
324 |
symbol_c *exp_type = base_type((symbol_c *)symbol->exp->accept(*this)); |
|
325 |
return compute_boolean_expression(exp_type, exp_type); |
|
326 |
} |
|
327 |
||
328 |
void *search_expression_type_c::visit(function_invocation_c *symbol) { |
|
329 |
function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name); |
|
330 |
if (f_decl == function_symtable.end_value()) { |
|
331 |
void *res = compute_standard_function_default(symbol); |
|
332 |
if (res == NULL) |
|
333 |
ERROR; |
|
334 |
return res; |
|
335 |
} |
|
336 |
return base_type(f_decl->type_name); |
|
337 |
} |
|
338 |
||
339 |
/*bool_type_name_c search_expression_type_c::bool_type_name;*/ |
|
340 |