author | Mario de Sousa <msousa@fe.up.pt> |
Fri, 01 Apr 2011 10:03:22 +0100 | |
changeset 263 | bcb92f5b9a91 |
parent 262 | 197ba42d78b2 |
child 266 | 2f6d8866ec8d |
permissions | -rwxr-xr-x |
204 | 1 |
/* |
2 |
* (c) 2009 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 |
/* Verify whether the semantic rules of data type compatibility are being followed. |
|
27 |
* |
|
28 |
* For example: |
|
29 |
*/ |
|
30 |
||
31 |
#include "visit_expression_type.hh" |
|
32 |
#include <typeinfo> |
|
33 |
#include <list> |
|
34 |
#include <string> |
|
35 |
#include <string.h> |
|
36 |
#include <strings.h> |
|
37 |
||
38 |
||
39 |
||
40 |
#define FIRST_(symbol1, symbol2) (((symbol1)->first_line < (symbol2)->first_line) ? (symbol1) : \ |
|
41 |
((symbol1)->first_line > (symbol2)->first_line) ? (symbol2) : \ |
|
42 |
((symbol1)->first_column < (symbol2)->first_column) ? (symbol1) : \ |
|
43 |
((symbol1)->first_column > (symbol2)->first_column) ? (symbol2) : \ |
|
44 |
(symbol1)) |
|
45 |
||
46 |
#define LAST_(symbol1, symbol2) (((symbol1)->last_line < (symbol2)->last_line) ? (symbol2) : \ |
|
47 |
((symbol1)->last_line > (symbol2)->last_line) ? (symbol1) : \ |
|
48 |
((symbol1)->last_column < (symbol2)->last_column) ? (symbol2) : \ |
|
49 |
((symbol1)->last_column > (symbol2)->last_column) ? (symbol1) : \ |
|
50 |
(symbol1)) |
|
51 |
||
52 |
#define STAGE3_ERROR(symbol1, symbol2, msg) { \ |
|
259
b6d7c71ff6d8
Adding debug conditional printf()s in stage 3.
Mario de Sousa <msousa@fe.up.pt>
parents:
258
diff
changeset
|
53 |
fprintf(stderr, "semantic error between (%d:%d) and (%d:%d): %s\n", \ |
204 | 54 |
FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column, \ |
55 |
LAST_(symbol1,symbol2) ->last_line, LAST_(symbol1,symbol2) ->last_column, \ |
|
259
b6d7c71ff6d8
Adding debug conditional printf()s in stage 3.
Mario de Sousa <msousa@fe.up.pt>
parents:
258
diff
changeset
|
56 |
msg); \ |
204 | 57 |
il_error = true; \ |
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
58 |
error_found = true; \ |
204 | 59 |
} |
60 |
||
61 |
||
259
b6d7c71ff6d8
Adding debug conditional printf()s in stage 3.
Mario de Sousa <msousa@fe.up.pt>
parents:
258
diff
changeset
|
62 |
/* set to 1 to see debug info during execution */ |
b6d7c71ff6d8
Adding debug conditional printf()s in stage 3.
Mario de Sousa <msousa@fe.up.pt>
parents:
258
diff
changeset
|
63 |
static int debug = 0; |
204 | 64 |
|
65 |
||
66 |
void *visit_expression_type_c::visit(program_declaration_c *symbol) { |
|
67 |
search_varfb_instance_type = new search_varfb_instance_type_c(symbol); |
|
68 |
symbol->var_declarations->accept(*this); |
|
259
b6d7c71ff6d8
Adding debug conditional printf()s in stage 3.
Mario de Sousa <msousa@fe.up.pt>
parents:
258
diff
changeset
|
69 |
if (debug) printf("checking semantics in body of program %s\n", ((token_c *)(symbol->program_type_name))->value); |
204 | 70 |
il_parenthesis_level = 0; |
71 |
il_error = false; |
|
72 |
il_default_variable_type = NULL; |
|
73 |
symbol->function_block_body->accept(*this); |
|
74 |
il_default_variable_type = NULL; |
|
75 |
delete search_varfb_instance_type; |
|
257 | 76 |
search_varfb_instance_type = NULL; |
204 | 77 |
return NULL; |
78 |
} |
|
79 |
||
80 |
void *visit_expression_type_c::visit(function_declaration_c *symbol) { |
|
81 |
search_varfb_instance_type = new search_varfb_instance_type_c(symbol); |
|
82 |
symbol->var_declarations_list->accept(*this); |
|
259
b6d7c71ff6d8
Adding debug conditional printf()s in stage 3.
Mario de Sousa <msousa@fe.up.pt>
parents:
258
diff
changeset
|
83 |
if (debug) printf("checking semantics in body of function %s\n", ((token_c *)(symbol->derived_function_name))->value); |
204 | 84 |
il_parenthesis_level = 0; |
85 |
il_error = false; |
|
86 |
il_default_variable_type = NULL; |
|
87 |
symbol->function_body->accept(*this); |
|
88 |
il_default_variable_type = NULL; |
|
89 |
delete search_varfb_instance_type; |
|
257 | 90 |
search_varfb_instance_type = NULL; |
204 | 91 |
return NULL; |
92 |
} |
|
93 |
||
94 |
void *visit_expression_type_c::visit(function_block_declaration_c *symbol) { |
|
95 |
search_varfb_instance_type = new search_varfb_instance_type_c(symbol); |
|
96 |
symbol->var_declarations->accept(*this); |
|
259
b6d7c71ff6d8
Adding debug conditional printf()s in stage 3.
Mario de Sousa <msousa@fe.up.pt>
parents:
258
diff
changeset
|
97 |
if (debug) printf("checking semantics in body of FB %s\n", ((token_c *)(symbol->fblock_name))->value); |
204 | 98 |
il_parenthesis_level = 0; |
99 |
il_error = false; |
|
100 |
il_default_variable_type = NULL; |
|
101 |
symbol->fblock_body->accept(*this); |
|
102 |
il_default_variable_type = NULL; |
|
103 |
delete search_varfb_instance_type; |
|
257 | 104 |
search_varfb_instance_type = NULL; |
204 | 105 |
return NULL; |
106 |
} |
|
107 |
||
108 |
||
109 |
||
110 |
||
111 |
||
112 |
||
113 |
||
114 |
||
115 |
||
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
116 |
visit_expression_type_c::visit_expression_type_c(symbol_c *ignore) { |
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
117 |
error_found = false; |
204 | 118 |
} |
119 |
||
120 |
visit_expression_type_c::~visit_expression_type_c(void) { |
|
121 |
} |
|
122 |
||
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
123 |
bool visit_expression_type_c::get_error_found(void) { |
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
124 |
return error_found; |
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
125 |
} |
204 | 126 |
|
257 | 127 |
|
128 |
||
129 |
/* NOTE on data type handling and literals... |
|
130 |
* ========================================== |
|
131 |
* |
|
132 |
* Literals that are explicitly type cast |
|
133 |
* e.g.: BYTE#42 |
|
134 |
* INT#65 |
|
135 |
* TIME#45h23m |
|
136 |
* etc... |
|
137 |
* are NOT considered literals in the following code. |
|
138 |
* Since they are type cast, and their data type is fixed and well known, |
|
139 |
* they are treated as a variable of that data type (except when determining lvalues) |
|
140 |
* In other words, when calling search_constant_type_c on these constants, it returns |
|
141 |
* a xxxxx_type_name_c, and not one of the xxxx_literal_c ! |
|
142 |
* |
|
143 |
* When the following code handles a literal, it is really a literal of unknown data type. |
|
144 |
* e.g. 42, may be considered an int, a byte, a word, etc... |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
145 |
* |
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
146 |
* NOTE: type_symbol == NULL is valid! |
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
147 |
* This will occur, for example, when and undefined/undeclared symbolic_variable is used in the program. |
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
148 |
* This will not be of any type, so we always return false. |
257 | 149 |
*/ |
150 |
||
204 | 151 |
/* A helper function... */ |
152 |
bool visit_expression_type_c::is_ANY_ELEMENTARY_type(symbol_c *type_symbol) { |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
153 |
if (type_symbol == NULL) {return false;} |
204 | 154 |
return is_ANY_MAGNITUDE_type(type_symbol) |
257 | 155 |
|| is_ANY_BIT_type (type_symbol) |
156 |
|| is_ANY_STRING_type (type_symbol) |
|
157 |
|| is_ANY_DATE_type (type_symbol); |
|
158 |
} |
|
159 |
||
160 |
/* A helper function... */ |
|
161 |
bool visit_expression_type_c::is_ANY_SAFEELEMENTARY_type(symbol_c *type_symbol) { |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
162 |
if (type_symbol == NULL) {return false;} |
257 | 163 |
return is_ANY_SAFEMAGNITUDE_type(type_symbol) |
164 |
|| is_ANY_SAFEBIT_type (type_symbol) |
|
165 |
|| is_ANY_SAFESTRING_type (type_symbol) |
|
166 |
|| is_ANY_SAFEDATE_type (type_symbol); |
|
167 |
} |
|
168 |
||
169 |
/* A helper function... */ |
|
170 |
bool visit_expression_type_c::is_ANY_ELEMENTARY_compatible(symbol_c *type_symbol) { |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
171 |
if (type_symbol == NULL) {return false;} |
257 | 172 |
/* NOTE: doing |
173 |
* return is_ANY_SAFEELEMENTARY_type() || is_ANY_ELEMENTARY_type() |
|
174 |
* is incorrect, as the literals would never be considered compatible... |
|
175 |
*/ |
|
176 |
return is_ANY_MAGNITUDE_compatible(type_symbol) |
|
177 |
|| is_ANY_BIT_compatible (type_symbol) |
|
178 |
|| is_ANY_STRING_compatible (type_symbol) |
|
179 |
|| is_ANY_DATE_compatible (type_symbol); |
|
204 | 180 |
} |
181 |
||
182 |
||
183 |
/* A helper function... */ |
|
184 |
bool visit_expression_type_c::is_ANY_MAGNITUDE_type(symbol_c *type_symbol) { |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
185 |
if (type_symbol == NULL) {return false;} |
204 | 186 |
if (typeid(*type_symbol) == typeid(time_type_name_c)) {return true;} |
187 |
return is_ANY_NUM_type(type_symbol); |
|
188 |
} |
|
189 |
||
257 | 190 |
/* A helper function... */ |
191 |
bool visit_expression_type_c::is_ANY_SAFEMAGNITUDE_type(symbol_c *type_symbol) { |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
192 |
if (type_symbol == NULL) {return false;} |
257 | 193 |
if (typeid(*type_symbol) == typeid(safetime_type_name_c)) {return true;} |
194 |
return is_ANY_SAFENUM_type(type_symbol); |
|
195 |
} |
|
196 |
||
197 |
/* A helper function... */ |
|
198 |
bool visit_expression_type_c::is_ANY_MAGNITUDE_compatible(symbol_c *type_symbol) { |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
199 |
if (type_symbol == NULL) {return false;} |
257 | 200 |
if (is_ANY_MAGNITUDE_type (type_symbol)) {return true;} |
201 |
if (is_ANY_SAFEMAGNITUDE_type(type_symbol)) {return true;} |
|
202 |
||
203 |
return is_ANY_NUM_compatible(type_symbol); |
|
204 |
} |
|
204 | 205 |
|
206 |
/* A helper function... */ |
|
207 |
bool visit_expression_type_c::is_ANY_NUM_type(symbol_c *type_symbol) { |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
208 |
if (type_symbol == NULL) {return false;} |
257 | 209 |
if (is_ANY_REAL_type(type_symbol)) {return true;} |
210 |
if (is_ANY_INT_type(type_symbol)) {return true;} |
|
211 |
return false; |
|
212 |
} |
|
213 |
||
214 |
/* A helper function... */ |
|
215 |
bool visit_expression_type_c::is_ANY_SAFENUM_type(symbol_c *type_symbol) { |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
216 |
if (type_symbol == NULL) {return false;} |
257 | 217 |
return is_ANY_SAFEREAL_type(type_symbol) |
218 |
|| is_ANY_SAFEINT_type (type_symbol); |
|
219 |
} |
|
220 |
||
221 |
/* A helper function... */ |
|
222 |
bool visit_expression_type_c::is_ANY_NUM_compatible(symbol_c *type_symbol) { |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
223 |
if (type_symbol == NULL) {return false;} |
257 | 224 |
if (is_ANY_REAL_compatible(type_symbol)) {return true;} |
225 |
if (is_ANY_INT_compatible(type_symbol)) {return true;} |
|
226 |
return false; |
|
227 |
} |
|
204 | 228 |
|
229 |
/* A helper function... */ |
|
230 |
bool visit_expression_type_c::is_ANY_DATE_type(symbol_c *type_symbol) { |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
231 |
if (type_symbol == NULL) {return false;} |
204 | 232 |
if (typeid(*type_symbol) == typeid(date_type_name_c)) {return true;} |
257 | 233 |
if (typeid(*type_symbol) == typeid(tod_type_name_c)) {return true;} |
234 |
if (typeid(*type_symbol) == typeid(dt_type_name_c)) {return true;} |
|
204 | 235 |
return false; |
236 |
} |
|
237 |
||
257 | 238 |
/* A helper function... */ |
239 |
bool visit_expression_type_c::is_ANY_SAFEDATE_type(symbol_c *type_symbol) { |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
240 |
if (type_symbol == NULL) {return false;} |
257 | 241 |
if (typeid(*type_symbol) == typeid(safedate_type_name_c)) {return true;} |
242 |
if (typeid(*type_symbol) == typeid(safetod_type_name_c)) {return true;} |
|
243 |
if (typeid(*type_symbol) == typeid(safedt_type_name_c)) {return true;} |
|
244 |
return false; |
|
245 |
} |
|
246 |
||
247 |
/* A helper function... */ |
|
248 |
bool visit_expression_type_c::is_ANY_DATE_compatible(symbol_c *type_symbol) { |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
249 |
if (type_symbol == NULL) {return false;} |
257 | 250 |
if (is_ANY_DATE_type (type_symbol)) {return true;} |
251 |
if (is_ANY_SAFEDATE_type(type_symbol)) {return true;} |
|
252 |
return false; |
|
253 |
} |
|
204 | 254 |
|
255 |
/* A helper function... */ |
|
256 |
bool visit_expression_type_c::is_ANY_STRING_type(symbol_c *type_symbol) { |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
257 |
if (type_symbol == NULL) {return false;} |
204 | 258 |
if (typeid(*type_symbol) == typeid(string_type_name_c)) {return true;} |
259 |
if (typeid(*type_symbol) == typeid(wstring_type_name_c)) {return true;} |
|
257 | 260 |
// TODO literal_string ??? |
204 | 261 |
return false; |
262 |
} |
|
263 |
||
257 | 264 |
/* A helper function... */ |
265 |
bool visit_expression_type_c::is_ANY_SAFESTRING_type(symbol_c *type_symbol) { |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
266 |
if (type_symbol == NULL) {return false;} |
257 | 267 |
if (typeid(*type_symbol) == typeid(safestring_type_name_c)) {return true;} |
268 |
if (typeid(*type_symbol) == typeid(safewstring_type_name_c)) {return true;} |
|
269 |
return false; |
|
270 |
} |
|
271 |
||
272 |
/* A helper function... */ |
|
273 |
bool visit_expression_type_c::is_ANY_STRING_compatible(symbol_c *type_symbol) { |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
274 |
if (type_symbol == NULL) {return false;} |
257 | 275 |
if (is_ANY_STRING_type (type_symbol)) {return true;} |
276 |
if (is_ANY_SAFESTRING_type(type_symbol)) {return true;} |
|
277 |
return false; |
|
278 |
} |
|
204 | 279 |
|
280 |
/* A helper function... */ |
|
281 |
bool visit_expression_type_c::is_ANY_INT_type(symbol_c *type_symbol) { |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
282 |
if (type_symbol == NULL) {return false;} |
204 | 283 |
if (typeid(*type_symbol) == typeid(sint_type_name_c)) {return true;} |
284 |
if (typeid(*type_symbol) == typeid(int_type_name_c)) {return true;} |
|
285 |
if (typeid(*type_symbol) == typeid(dint_type_name_c)) {return true;} |
|
286 |
if (typeid(*type_symbol) == typeid(lint_type_name_c)) {return true;} |
|
287 |
if (typeid(*type_symbol) == typeid(usint_type_name_c)) {return true;} |
|
288 |
if (typeid(*type_symbol) == typeid(uint_type_name_c)) {return true;} |
|
289 |
if (typeid(*type_symbol) == typeid(udint_type_name_c)) {return true;} |
|
290 |
if (typeid(*type_symbol) == typeid(ulint_type_name_c)) {return true;} |
|
291 |
return false; |
|
292 |
} |
|
293 |
||
257 | 294 |
/* A helper function... */ |
295 |
bool visit_expression_type_c::is_ANY_SAFEINT_type(symbol_c *type_symbol) { |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
296 |
if (type_symbol == NULL) {return false;} |
257 | 297 |
if (typeid(*type_symbol) == typeid(safesint_type_name_c)) {return true;} |
298 |
if (typeid(*type_symbol) == typeid(safeint_type_name_c)) {return true;} |
|
299 |
if (typeid(*type_symbol) == typeid(safedint_type_name_c)) {return true;} |
|
300 |
if (typeid(*type_symbol) == typeid(safelint_type_name_c)) {return true;} |
|
301 |
if (typeid(*type_symbol) == typeid(safeusint_type_name_c)) {return true;} |
|
302 |
if (typeid(*type_symbol) == typeid(safeuint_type_name_c)) {return true;} |
|
303 |
if (typeid(*type_symbol) == typeid(safeudint_type_name_c)) {return true;} |
|
304 |
if (typeid(*type_symbol) == typeid(safeulint_type_name_c)) {return true;} |
|
305 |
return false; |
|
306 |
} |
|
307 |
||
308 |
/* A helper function... */ |
|
309 |
bool visit_expression_type_c::is_ANY_INT_compatible(symbol_c *type_symbol) { |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
310 |
if (type_symbol == NULL) {return false;} |
257 | 311 |
if (is_ANY_INT_type (type_symbol)) {return true;} |
312 |
if (is_ANY_SAFEINT_type(type_symbol)) {return true;} |
|
313 |
if (is_literal_integer_type(type_symbol)) {return true;} |
|
314 |
return false; |
|
315 |
} |
|
204 | 316 |
|
317 |
/* A helper function... */ |
|
318 |
bool visit_expression_type_c::is_ANY_REAL_type(symbol_c *type_symbol) { |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
319 |
if (type_symbol == NULL) {return false;} |
204 | 320 |
if (typeid(*type_symbol) == typeid(real_type_name_c)) {return true;} |
321 |
if (typeid(*type_symbol) == typeid(lreal_type_name_c)) {return true;} |
|
322 |
return false; |
|
323 |
} |
|
324 |
||
257 | 325 |
/* A helper function... */ |
326 |
bool visit_expression_type_c::is_ANY_SAFEREAL_type(symbol_c *type_symbol) { |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
327 |
if (type_symbol == NULL) {return false;} |
257 | 328 |
if (typeid(*type_symbol) == typeid(safereal_type_name_c)) {return true;} |
329 |
if (typeid(*type_symbol) == typeid(safelreal_type_name_c)) {return true;} |
|
330 |
return false; |
|
331 |
} |
|
332 |
||
333 |
/* A helper function... */ |
|
334 |
bool visit_expression_type_c::is_ANY_REAL_compatible(symbol_c *type_symbol) { |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
335 |
if (type_symbol == NULL) {return false;} |
257 | 336 |
if (is_ANY_REAL_type (type_symbol)) {return true;} |
337 |
if (is_ANY_SAFEREAL_type(type_symbol)) {return true;} |
|
338 |
if (is_literal_real_type(type_symbol)) {return true;} |
|
339 |
return false; |
|
340 |
} |
|
204 | 341 |
|
342 |
/* A helper function... */ |
|
343 |
bool visit_expression_type_c::is_ANY_BIT_type(symbol_c *type_symbol) { |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
344 |
if (type_symbol == NULL) {return false;} |
257 | 345 |
if (typeid(*type_symbol) == typeid(bool_type_name_c)) {return true;} |
346 |
if (typeid(*type_symbol) == typeid(byte_type_name_c)) {return true;} |
|
347 |
if (typeid(*type_symbol) == typeid(word_type_name_c)) {return true;} |
|
348 |
if (typeid(*type_symbol) == typeid(dword_type_name_c)) {return true;} |
|
349 |
if (typeid(*type_symbol) == typeid(lword_type_name_c)) {return true;} |
|
204 | 350 |
return false; |
351 |
} |
|
352 |
||
257 | 353 |
/* A helper function... */ |
354 |
bool visit_expression_type_c::is_ANY_SAFEBIT_type(symbol_c *type_symbol) { |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
355 |
if (type_symbol == NULL) {return false;} |
257 | 356 |
if (typeid(*type_symbol) == typeid(safebool_type_name_c)) {return true;} |
357 |
if (typeid(*type_symbol) == typeid(safebyte_type_name_c)) {return true;} |
|
358 |
if (typeid(*type_symbol) == typeid(safeword_type_name_c)) {return true;} |
|
359 |
if (typeid(*type_symbol) == typeid(safedword_type_name_c)) {return true;} |
|
360 |
if (typeid(*type_symbol) == typeid(safelword_type_name_c)) {return true;} |
|
361 |
return false; |
|
362 |
} |
|
363 |
||
364 |
/* A helper function... */ |
|
365 |
bool visit_expression_type_c::is_ANY_BIT_compatible(symbol_c *type_symbol) { |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
366 |
if (type_symbol == NULL) {return false;} |
257 | 367 |
if (is_ANY_BIT_type (type_symbol)) {return true;} |
368 |
if (is_ANY_SAFEBIT_type(type_symbol)) {return true;} |
|
369 |
if (is_nonneg_literal_integer_type(type_symbol)) {return true;} |
|
370 |
if (is_literal_bool_type(type_symbol)) {return true;} |
|
371 |
return false; |
|
372 |
} |
|
204 | 373 |
|
374 |
/* A helper function... */ |
|
375 |
bool visit_expression_type_c::is_BOOL_type(symbol_c *type_symbol) { |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
376 |
if (type_symbol == NULL) {return false;} |
257 | 377 |
if (typeid(*type_symbol) == typeid(bool_type_name_c)) {return true;} |
204 | 378 |
return false; |
379 |
} |
|
380 |
||
257 | 381 |
/* A helper function... */ |
382 |
bool visit_expression_type_c::is_SAFEBOOL_type(symbol_c *type_symbol){ |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
383 |
if (type_symbol == NULL) {return false;} |
257 | 384 |
if (typeid(*type_symbol) == typeid(safebool_type_name_c)) {return true;} |
385 |
return false; |
|
386 |
} |
|
387 |
||
388 |
/* A helper function... */ |
|
389 |
bool visit_expression_type_c::is_ANY_BOOL_compatible(symbol_c *type_symbol) { |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
390 |
if (type_symbol == NULL) {return false;} |
257 | 391 |
if (is_BOOL_type (type_symbol)) {return true;} |
392 |
if (is_SAFEBOOL_type(type_symbol)) {return true;} |
|
393 |
if (is_literal_bool_type(type_symbol)) {return true;} |
|
394 |
return false; |
|
395 |
} |
|
396 |
||
397 |
||
398 |
#define is_type(type_name_symbol, type_name_class) (typeid(*type_name_symbol) == typeid(type_name_class)) |
|
399 |
||
204 | 400 |
|
401 |
#define sizeoftype(symbol) get_sizeof_datatype_c::getsize(symbol) |
|
402 |
||
403 |
||
404 |
/* A helper function... */ |
|
405 |
bool visit_expression_type_c::is_literal_integer_type(symbol_c *type_symbol) { |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
406 |
if (type_symbol == NULL) {return false;} |
257 | 407 |
if (typeid(*type_symbol) == typeid(neg_integer_c)) {return true;} |
408 |
return is_nonneg_literal_integer_type(type_symbol); |
|
409 |
} |
|
410 |
||
411 |
||
412 |
/* A helper function... */ |
|
413 |
bool visit_expression_type_c::is_nonneg_literal_integer_type(symbol_c *type_symbol) { |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
414 |
if (type_symbol == NULL) {return false;} |
204 | 415 |
if (typeid(*type_symbol) == typeid(integer_c)) {return true;} |
416 |
if (typeid(*type_symbol) == typeid(binary_integer_c)) {return true;} |
|
417 |
if (typeid(*type_symbol) == typeid(octal_integer_c)) {return true;} |
|
418 |
if (typeid(*type_symbol) == typeid(hex_integer_c)) {return true;} |
|
419 |
return false; |
|
420 |
} |
|
421 |
||
422 |
||
423 |
/* A helper function... */ |
|
424 |
bool visit_expression_type_c::is_literal_real_type(symbol_c *type_symbol) { |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
425 |
if (type_symbol == NULL) {return false;} |
257 | 426 |
if (typeid(*type_symbol) == typeid(real_c)) {return true;} |
427 |
if (typeid(*type_symbol) == typeid(neg_real_c)) {return true;} |
|
204 | 428 |
return false; |
429 |
} |
|
430 |
||
431 |
||
432 |
/* A helper function... */ |
|
433 |
bool visit_expression_type_c::is_literal_bool_type(symbol_c *type_symbol) { |
|
434 |
bool_type_name_c bool_t; |
|
435 |
||
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
436 |
if (type_symbol == NULL) {return false;} |
204 | 437 |
if (typeid(*type_symbol) == typeid(boolean_true_c)) {return true;} |
438 |
if (typeid(*type_symbol) == typeid(boolean_false_c)) {return true;} |
|
257 | 439 |
if (is_nonneg_literal_integer_type(type_symbol)) |
204 | 440 |
if (sizeoftype(&bool_t) >= sizeoftype(type_symbol)) {return true;} |
441 |
return false; |
|
442 |
} |
|
443 |
||
444 |
/* Determine the common data type between two data types. |
|
445 |
* If no common data type found, return NULL. |
|
446 |
* |
|
447 |
* If data types are identical, return the first (actually any would do...). |
|
448 |
* If any of the data types is a literal, we confirm that |
|
449 |
* the literal uses less bits than the fixed size data type. |
|
450 |
* e.g. BYTE and 1024 returns NULL |
|
451 |
* BYTE and 255 returns BYTE |
|
452 |
* |
|
453 |
* If two literals, then return the literal that requires more bits... |
|
454 |
*/ |
|
257 | 455 |
|
204 | 456 |
symbol_c *visit_expression_type_c::common_type__(symbol_c *first_type, symbol_c *second_type) { |
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
457 |
if (first_type == NULL && second_type == NULL) {return NULL;} |
204 | 458 |
if (first_type == NULL) {return second_type;} |
459 |
if (second_type == NULL) {return first_type;} |
|
460 |
||
461 |
if (is_literal_integer_type(first_type) && is_literal_integer_type(second_type)) |
|
462 |
{return ((sizeoftype(first_type) > sizeoftype(second_type))? first_type:second_type);} |
|
463 |
||
464 |
if (is_literal_real_type(first_type) && is_literal_real_type(second_type)) |
|
465 |
{return ((sizeoftype(first_type) > sizeoftype(second_type))? first_type:second_type);} |
|
466 |
||
467 |
if (is_literal_bool_type(first_type) && is_literal_bool_type(second_type)) |
|
468 |
{return first_type;} |
|
469 |
||
257 | 470 |
/* The following check can only be made after the is_literal_XXXX checks */ |
204 | 471 |
/* When two literals of the same type, with identical typeid's are checked, |
257 | 472 |
* we must return the one that occupies more bits... This is done above. |
204 | 473 |
*/ |
474 |
if (typeid(*first_type) == typeid(*second_type)) {return first_type;} |
|
475 |
||
257 | 476 |
/* NOTE Although a BOOL is also an ANY_BIT, we must check it explicitly since some |
477 |
* literal bool values are not literal integers... |
|
478 |
*/ |
|
479 |
if (is_BOOL_type(first_type) && is_literal_bool_type(second_type)) {return first_type;} |
|
480 |
if (is_BOOL_type(second_type) && is_literal_bool_type(first_type)) {return second_type;} |
|
481 |
||
482 |
if (is_SAFEBOOL_type(first_type) && is_literal_bool_type(second_type)) {return first_type;} |
|
483 |
if (is_SAFEBOOL_type(second_type) && is_literal_bool_type(first_type)) {return second_type;} |
|
484 |
||
485 |
if (is_SAFEBOOL_type(first_type) && is_BOOL_type(second_type)) {return second_type;} |
|
486 |
if (is_SAFEBOOL_type(second_type) && is_BOOL_type(first_type)) {return first_type;} |
|
487 |
||
488 |
if (is_ANY_BIT_type(first_type) && is_nonneg_literal_integer_type(second_type)) |
|
204 | 489 |
{return ((sizeoftype(first_type) >= sizeoftype(second_type))? first_type :NULL);} |
257 | 490 |
if (is_ANY_BIT_type(second_type) && is_nonneg_literal_integer_type(first_type)) |
204 | 491 |
{return ((sizeoftype(second_type) >= sizeoftype(first_type)) ? second_type:NULL);} |
492 |
||
257 | 493 |
if (is_ANY_SAFEBIT_type(first_type) && is_nonneg_literal_integer_type(second_type)) |
204 | 494 |
{return ((sizeoftype(first_type) >= sizeoftype(second_type))? first_type :NULL);} |
257 | 495 |
if (is_ANY_SAFEBIT_type(second_type) && is_nonneg_literal_integer_type(first_type)) |
204 | 496 |
{return ((sizeoftype(second_type) >= sizeoftype(first_type)) ? second_type:NULL);} |
497 |
||
257 | 498 |
if (is_ANY_SAFEBIT_type(first_type) && is_ANY_BIT_type(second_type)) |
499 |
{return ((sizeoftype(first_type) == sizeoftype(second_type))? second_type:NULL);} |
|
500 |
if (is_ANY_SAFEBIT_type(second_type) && is_ANY_BIT_type(first_type)) |
|
501 |
{return ((sizeoftype(first_type) == sizeoftype(second_type))? first_type :NULL);} |
|
502 |
||
503 |
if (is_ANY_INT_type(first_type) && is_literal_integer_type(second_type)) |
|
204 | 504 |
{return ((sizeoftype(first_type) >= sizeoftype(second_type))? first_type :NULL);} |
257 | 505 |
if (is_ANY_INT_type(second_type) && is_literal_integer_type(first_type)) |
204 | 506 |
{return ((sizeoftype(second_type) >= sizeoftype(first_type)) ? second_type:NULL);} |
507 |
||
257 | 508 |
if (is_ANY_SAFEINT_type(first_type) && is_literal_integer_type(second_type)) |
509 |
{return ((sizeoftype(first_type) >= sizeoftype(second_type))? first_type :NULL);} |
|
510 |
if (is_ANY_SAFEINT_type(second_type) && is_literal_integer_type(first_type)) |
|
511 |
{return ((sizeoftype(second_type) >= sizeoftype(first_type)) ? second_type:NULL);} |
|
512 |
||
513 |
if (is_ANY_SAFEINT_type(first_type) && is_ANY_INT_type(second_type)) |
|
514 |
{return ((sizeoftype(first_type) == sizeoftype(second_type))? second_type:NULL);} |
|
515 |
if (is_ANY_SAFEINT_type(second_type) && is_ANY_INT_type(first_type)) |
|
516 |
{return ((sizeoftype(first_type) == sizeoftype(second_type))? first_type :NULL);} |
|
517 |
||
518 |
if (is_ANY_REAL_type(first_type) && is_literal_real_type(second_type)) |
|
519 |
{return ((sizeoftype(first_type) >= sizeoftype(second_type))? first_type :NULL);} |
|
520 |
if (is_ANY_REAL_type(second_type) && is_literal_real_type(first_type)) |
|
521 |
{return ((sizeoftype(second_type) >= sizeoftype(first_type)) ? second_type:NULL);} |
|
522 |
||
523 |
if (is_ANY_SAFEREAL_type(first_type) && is_literal_real_type(second_type)) |
|
524 |
{return ((sizeoftype(first_type) >= sizeoftype(second_type))? first_type :NULL);} |
|
525 |
if (is_ANY_SAFEREAL_type(second_type) && is_literal_real_type(first_type)) |
|
526 |
{return ((sizeoftype(second_type) >= sizeoftype(first_type)) ? second_type:NULL);} |
|
527 |
||
528 |
if (is_ANY_SAFEREAL_type(first_type) && is_ANY_REAL_type(second_type)) |
|
529 |
{return ((sizeoftype(first_type) == sizeoftype(second_type))? second_type:NULL);} |
|
530 |
if (is_ANY_SAFEREAL_type(second_type) && is_ANY_REAL_type(first_type)) |
|
531 |
{return ((sizeoftype(first_type) == sizeoftype(second_type))? first_type :NULL);} |
|
532 |
||
533 |
/* the Time and Date types... */ |
|
534 |
if (is_type(first_type, safetime_type_name_c) && is_type(second_type, time_type_name_c)) {return second_type;} |
|
535 |
if (is_type(second_type, safetime_type_name_c) && is_type( first_type, time_type_name_c)) {return first_type;} |
|
536 |
||
537 |
if (is_type(first_type, safedate_type_name_c) && is_type(second_type, date_type_name_c)) {return second_type;} |
|
538 |
if (is_type(second_type, safedate_type_name_c) && is_type( first_type, date_type_name_c)) {return first_type;} |
|
539 |
||
540 |
if (is_type(first_type, safedt_type_name_c) && is_type(second_type, dt_type_name_c)) {return second_type;} |
|
541 |
if (is_type(second_type, safedt_type_name_c) && is_type( first_type, dt_type_name_c)) {return first_type;} |
|
542 |
||
543 |
if (is_type(first_type, safetod_type_name_c) && is_type(second_type, tod_type_name_c)) {return second_type;} |
|
544 |
if (is_type(second_type, safetod_type_name_c) && is_type( first_type, tod_type_name_c)) {return first_type;} |
|
545 |
||
204 | 546 |
/* no common type */ |
547 |
return NULL; |
|
548 |
} |
|
549 |
||
550 |
/* Determine the common data type between two data types. |
|
551 |
* Unlike the common_type__() function, we stop the compiler with an ERROR |
|
552 |
* if no common data type is found. |
|
553 |
*/ |
|
554 |
symbol_c *visit_expression_type_c::common_type(symbol_c *first_type, symbol_c *second_type) { |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
555 |
/* |
204 | 556 |
symbol_c *res = common_type__(first_type, second_type); |
557 |
if (NULL == res) ERROR; |
|
558 |
return res; |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
559 |
*/ |
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
560 |
return common_type__(first_type, second_type); |
204 | 561 |
} |
562 |
||
563 |
||
257 | 564 |
/* Return TRUE if the second (value) data type may be assigned to a variable of the first (variable) data type |
565 |
* such as: |
|
566 |
* var_type value_type |
|
567 |
* BOOL BYTE#7 -> returns false |
|
568 |
* INT INT#7 -> returns true |
|
569 |
* INT 7 -> returns true |
|
570 |
* REAL 7.89 -> returns true |
|
571 |
* REAL 7 -> returns true |
|
572 |
* INT 7.89 -> returns false |
|
573 |
* SAFEBOOL BOOL#1 -> returns false !!! |
|
574 |
* etc... |
|
575 |
* |
|
576 |
* NOTE: It is assumed that the var_type is the data type of an lvalue |
|
577 |
*/ |
|
578 |
bool visit_expression_type_c::is_valid_assignment(symbol_c *var_type, symbol_c *value_type) { |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
579 |
if (var_type == NULL) {/* STAGE3_ERROR(value_type, value_type, "Var_type == NULL"); */ return false;} |
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
580 |
if (value_type == NULL) {/* STAGE3_ERROR(var_type, var_type, "Value_type == NULL"); */ return false;} |
257 | 581 |
|
582 |
symbol_c *common_type = common_type__(var_type, value_type); |
|
583 |
if (NULL == common_type) |
|
584 |
return false; |
|
585 |
return (typeid(*var_type) == typeid(*common_type)); |
|
586 |
} |
|
587 |
||
588 |
||
204 | 589 |
/* Return TRUE if there is a common data type, otherwise return FALSE |
257 | 590 |
* i.e., return TRUE if both data types may be used simultaneously in an expression |
591 |
* such as: |
|
592 |
* BOOL#0 AND BYTE#7 -> returns false |
|
593 |
* 0 AND BYTE#7 -> returns true |
|
594 |
* INT#10 AND INT#7 -> returns true |
|
595 |
* INT#10 AND 7 -> returns true |
|
596 |
* REAL#34.3 AND 7.89 -> returns true |
|
597 |
* REAL#34.3 AND 7 -> returns true |
|
598 |
* INT#10 AND 7.89 -> returns false |
|
599 |
* SAFEBOOL#0 AND BOOL#1 -> returns true !!! |
|
600 |
* etc... |
|
204 | 601 |
*/ |
602 |
bool visit_expression_type_c::is_compatible_type(symbol_c *first_type, symbol_c *second_type) { |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
603 |
if (first_type == NULL || second_type == NULL) {return false;} |
204 | 604 |
return (NULL != common_type__(first_type, second_type)); |
605 |
} |
|
606 |
||
607 |
||
608 |
||
257 | 609 |
#define is_num_type is_ANY_NUM_compatible |
610 |
#define is_integer_type is_ANY_INT_compatible |
|
611 |
#define is_real_type is_ANY_REAL_compatible |
|
612 |
#define is_binary_type is_ANY_BIT_compatible |
|
204 | 613 |
/* actually the ROR, ROL, SHL, and SHR function also accept boolean type! */ |
257 | 614 |
#define is_nbinary_type is_ANY_BIT_compatible |
204 | 615 |
#define compute_standard_function_default visit_expression_type_c::compute_standard_function_default |
616 |
#define compute_standard_function_il visit_expression_type_c::compute_standard_function_il |
|
617 |
#define search_expression_type_c visit_expression_type_c |
|
618 |
#define search(x) search_f(x) |
|
619 |
#define next() next_nf() |
|
620 |
// #define search_constant_type_c::constant_int_type_name search_expression_type_c::integer |
|
621 |
#define constant_int_type_name integer |
|
622 |
#define is_same_type is_compatible_type |
|
623 |
#include "../absyntax_utils/search_type_code.c" |
|
624 |
#undef is_same_type |
|
625 |
#undef constant_int_type_name |
|
626 |
// #undef search_constant_type_c::constant_int_type_name |
|
627 |
#undef next |
|
628 |
#undef search |
|
629 |
#undef compute_standard_function_default |
|
630 |
#undef compute_standard_function_il |
|
631 |
#undef search_expression_type_c |
|
632 |
#undef is_real_type |
|
633 |
#undef is_binary_type |
|
634 |
#undef is_nbinary_type |
|
635 |
#undef is_integer_type |
|
636 |
#undef is_num_type |
|
637 |
||
638 |
||
639 |
||
640 |
||
641 |
||
642 |
||
643 |
/* A helper function... */ |
|
257 | 644 |
/* |
204 | 645 |
symbol_c *visit_expression_type_c::compute_boolean_expression(symbol_c *left_type, symbol_c *right_type, |
646 |
is_data_type_t is_data_type) { |
|
257 | 647 |
*/ |
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
648 |
symbol_c *visit_expression_type_c::compute_expression(symbol_c *left_type, symbol_c *right_type, is_data_type_t is_data_type, |
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
649 |
symbol_c *left_expr, symbol_c *right_expr) { |
204 | 650 |
bool error = false; |
651 |
||
652 |
if (!(this->*is_data_type)(left_type)) { |
|
263 | 653 |
if (debug) printf("visit_expression_type_c::compute_expression(): invalid left_type\n"); |
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
654 |
if (left_expr != NULL) |
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
655 |
STAGE3_ERROR(left_expr, left_expr, "Invalid data type of left operand."); |
204 | 656 |
error = true; |
657 |
} |
|
658 |
if (!(this->*is_data_type)(right_type)) { |
|
263 | 659 |
if (debug) printf("visit_expression_type_c::compute_expression(): invalid right_type\n"); |
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
660 |
if (right_expr != NULL) |
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
661 |
STAGE3_ERROR(right_expr, right_expr, "Invalid data type of right operand."); |
204 | 662 |
error = true; |
663 |
} |
|
664 |
if (!is_compatible_type(left_type, right_type)) { |
|
263 | 665 |
if (debug) printf("visit_expression_type_c::compute_expression(): left_type & right_type are incompatible\n"); |
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
666 |
if ((left_expr != NULL) && (right_expr != NULL)) |
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
667 |
STAGE3_ERROR(left_expr, right_expr, "Type mismatch between operands."); |
204 | 668 |
error = true; |
669 |
} |
|
670 |
||
671 |
if (error) |
|
672 |
return NULL; |
|
673 |
else |
|
674 |
return common_type(left_type, right_type); |
|
675 |
} |
|
676 |
||
677 |
||
257 | 678 |
# if 0 |
204 | 679 |
/* A helper function... */ |
680 |
symbol_c *visit_expression_type_c::compute_numeric_expression(symbol_c *left_type, symbol_c *right_type, |
|
681 |
is_data_type_t is_data_type) { |
|
257 | 682 |
bool error = false; |
683 |
||
684 |
if (!(this->*is_data_type)(left_type)) { |
|
685 |
STAGE3_ERROR(left_type, right_type, "Invalid data type of left operand."); |
|
686 |
error = true; |
|
687 |
} |
|
688 |
if (!(this->*is_data_type)(right_type)) { |
|
689 |
STAGE3_ERROR(left_type, right_type, "Invalid data type of right operand."); |
|
690 |
error = true; |
|
691 |
} |
|
692 |
if (!is_compatible_type(left_type, right_type)) { |
|
693 |
STAGE3_ERROR(left_type, right_type, "Type mismatch between operands."); |
|
694 |
error = true; |
|
695 |
} |
|
696 |
||
697 |
/* |
|
204 | 698 |
if (is_literal_integer_type(left_type) || is_literal_real_type(left_type)) { |
699 |
return right_type; |
|
700 |
} else { |
|
701 |
return left_type; |
|
702 |
} |
|
257 | 703 |
*/ |
704 |
||
705 |
if (error) |
|
706 |
return NULL; |
|
707 |
else |
|
708 |
return common_type(left_type, right_type); |
|
204 | 709 |
|
710 |
/* humour the compiler... */ |
|
257 | 711 |
/* |
712 |
return NULL; |
|
713 |
*/ |
|
714 |
} |
|
715 |
#endif |
|
204 | 716 |
|
717 |
||
718 |
||
719 |
||
720 |
||
721 |
/* A helper function... */ |
|
722 |
/* check the semantics of a FB or Function non-formal call */ |
|
723 |
/* e.g. foo(1, 2, 3, 4); */ |
|
724 |
void visit_expression_type_c::check_nonformal_call(symbol_c *f_call, symbol_c *f_decl, bool use_il_defvar) { |
|
725 |
symbol_c *call_param_value, *call_param_type, *param_type; |
|
726 |
identifier_c *param_name; |
|
727 |
function_param_iterator_c fp_iterator(f_decl); |
|
728 |
function_call_param_iterator_c fcp_iterator(f_call); |
|
729 |
||
730 |
/* if use_il_defvar, then the first parameter for the call comes from the il_default_variable */ |
|
731 |
if (use_il_defvar) { |
|
732 |
/* The first parameter of the function corresponds to the il_default_variable_type of the function call */ |
|
733 |
do { |
|
734 |
param_name = fp_iterator.next(); |
|
735 |
if(param_name == NULL) break; |
|
736 |
/* The EN and ENO parameters are default parameters. |
|
737 |
* In the non-formal invocation of a function there can be no assignment of |
|
738 |
* values to these parameters. Therefore, we ignore the parameters declared |
|
739 |
* in the function. |
|
740 |
*/ |
|
741 |
} while ((strcmp(param_name->value, "EN") == 0) || (strcmp(param_name->value, "ENO") == 0)); |
|
742 |
/* If the function does not have any parameters (param_name == NULL) |
|
743 |
* then we cannot compare its type with the il_default_variable_type. |
|
744 |
*/ |
|
745 |
if(param_name != NULL) { |
|
746 |
param_type = fp_iterator.param_type(); |
|
257 | 747 |
if(!is_valid_assignment(param_type, il_default_variable_type)) |
204 | 748 |
STAGE3_ERROR(f_call, f_call, "In function/FB call, first parameter has invalid data type."); |
749 |
} |
|
750 |
} // if (use_il_defvar) |
|
751 |
||
752 |
/* Iterating through the non-formal parameters of the function call */ |
|
753 |
while((call_param_value = fcp_iterator.next_nf()) != NULL) { |
|
754 |
/* Obtaining the type of the current parameter in the function call */ |
|
755 |
call_param_type = base_type((symbol_c*)call_param_value->accept(*this)); |
|
756 |
if (call_param_type == NULL) STAGE3_ERROR(call_param_value, call_param_value, "Could not determine data type of value being passed in function/FB call.");; |
|
757 |
||
758 |
/* Iterate to the next parameter of the function being called. |
|
759 |
* Get the name of that parameter, and ignore if EN or ENO. |
|
760 |
*/ |
|
761 |
do { |
|
762 |
param_name = fp_iterator.next(); |
|
763 |
/* If there is no parameter declared with that name */ |
|
764 |
if(param_name == NULL) {STAGE3_ERROR(f_call, f_call, "Too many parameters in function/FB call."); break;} |
|
765 |
} while ((strcmp(param_name->value, "EN") == 0) || (strcmp(param_name->value, "ENO") == 0)); |
|
766 |
||
767 |
if(param_name != NULL) { |
|
768 |
/* Get the parameter type */ |
|
769 |
param_type = fp_iterator.param_type(); |
|
770 |
/* If the declared parameter and the parameter from the function call do no have the same type */ |
|
257 | 771 |
if(!is_valid_assignment(param_type, call_param_type)) STAGE3_ERROR(call_param_value, call_param_value, "Type mismatch in function/FB call parameter."); |
204 | 772 |
} |
773 |
} |
|
774 |
} |
|
775 |
||
776 |
void visit_expression_type_c::compute_input_operatores(symbol_c *symbol, const char *input_operator){ |
|
777 |
symbol_c *call_param_type; |
|
778 |
symbol_c *fb_decl = il_operand_type; |
|
779 |
/* The following should never occur. The function block must be defined, |
|
780 |
* and the FB type being called MUST be in the symtable... |
|
781 |
* This was all already checked at stage 2! |
|
782 |
*/ |
|
783 |
if (NULL == fb_decl){ |
|
784 |
STAGE3_ERROR(symbol, symbol, "Parameter operator needs an instance of a function block operand."); |
|
785 |
ERROR; |
|
786 |
} |
|
787 |
||
788 |
/* Iterating through the formal parameters of the function call */ |
|
789 |
identifier_c call_param_name(input_operator); |
|
790 |
||
791 |
/* Obtaining the type of the value being passed in the function call */ |
|
792 |
call_param_type = il_default_variable_type; |
|
793 |
if (call_param_type == NULL) { |
|
794 |
STAGE3_ERROR(&call_param_name, &call_param_name, "Could not determine data type of value being passed in function/FB call."); |
|
795 |
/* The data value being passed is possibly any enumerated type value. |
|
796 |
* We do not yet handle semantic verification of enumerated types. |
|
797 |
*/ |
|
798 |
ERROR; |
|
799 |
} |
|
800 |
call_param_type = base_type(call_param_type); |
|
801 |
if (call_param_type == NULL) STAGE3_ERROR(&call_param_name, &call_param_name, "Could not determine data type of value being passed in function/FB call."); |
|
802 |
||
803 |
||
804 |
check_formal_parameter(&call_param_name, call_param_type, fb_decl); |
|
805 |
// return NULL; |
|
806 |
} |
|
807 |
||
808 |
void visit_expression_type_c::check_formal_parameter(symbol_c *call_param_name, symbol_c *call_param_type, symbol_c *f_decl) { |
|
809 |
symbol_c *param_type; |
|
810 |
identifier_c *param_name; |
|
811 |
function_param_iterator_c fp_iterator(f_decl); |
|
263 | 812 |
|
204 | 813 |
/* Find the corresponding parameter of the function being called */ |
814 |
param_name = fp_iterator.search(call_param_name); |
|
815 |
if(param_name == NULL) { |
|
816 |
STAGE3_ERROR(call_param_name, call_param_name, "Invalid parameter in function/FB call."); |
|
817 |
} else { |
|
818 |
/* Get the parameter type */ |
|
819 |
param_type = fp_iterator.param_type(); |
|
820 |
/* If the declared parameter and the parameter from the function call have the same type */ |
|
257 | 821 |
if(!is_valid_assignment(param_type, call_param_type)) STAGE3_ERROR(call_param_name, call_param_name, "Type mismatch function/FB call parameter."); |
204 | 822 |
} |
823 |
} |
|
824 |
||
825 |
||
826 |
/* A helper function... */ |
|
827 |
/* check the semantics of a FB or Function formal call */ |
|
828 |
/* e.g. foo(IN1 := 1, OUT1 =>x, EN := true); */ |
|
829 |
void visit_expression_type_c::check_formal_call(symbol_c *f_call, symbol_c *f_decl) { |
|
830 |
symbol_c *call_param_value, *call_param_type, *call_param_name, *param_type; |
|
831 |
symbol_c *verify_duplicate_param; |
|
832 |
identifier_c *param_name; |
|
833 |
function_param_iterator_c fp_iterator(f_decl); |
|
834 |
function_call_param_iterator_c fcp_iterator(f_call); |
|
835 |
||
836 |
/* Iterating through the formal parameters of the function call */ |
|
837 |
while((call_param_name = fcp_iterator.next_f()) != NULL) { |
|
838 |
||
839 |
/* Obtaining the value being passed in the function call */ |
|
840 |
call_param_value = fcp_iterator.get_current_value(); |
|
841 |
/* the following should never occur. If it does, then we have a bug in our code... */ |
|
842 |
if (NULL == call_param_value) ERROR; |
|
843 |
||
844 |
/* Checking if there are duplicated parameter values */ |
|
845 |
verify_duplicate_param = fcp_iterator.search_f(call_param_name); |
|
846 |
if(verify_duplicate_param != call_param_value){ |
|
847 |
STAGE3_ERROR(call_param_name, verify_duplicate_param, "Duplicated parameter values."); |
|
848 |
} |
|
849 |
||
850 |
/* Obtaining the type of the value being passed in the function call */ |
|
851 |
call_param_type = (symbol_c*)call_param_value->accept(*this); |
|
852 |
if (call_param_type == NULL) { |
|
853 |
STAGE3_ERROR(call_param_name, call_param_value, "Could not determine data type of value being passed in function/FB call."); |
|
854 |
/* The data value being passed is possibly any enumerated type value. |
|
855 |
* We do not yet handle semantic verification of enumerated types. |
|
856 |
*/ |
|
857 |
ERROR; |
|
858 |
} |
|
859 |
call_param_type = base_type(call_param_type); |
|
860 |
if (call_param_type == NULL) STAGE3_ERROR(call_param_name, call_param_value, "Could not determine data type of value being passed in function/FB call."); |
|
861 |
||
862 |
/* Find the corresponding parameter of the function being called */ |
|
863 |
param_name = fp_iterator.search(call_param_name); |
|
864 |
if(param_name == NULL) { |
|
865 |
STAGE3_ERROR(call_param_name, call_param_name, "Invalid parameter in function/FB call."); |
|
866 |
} else { |
|
867 |
/* Get the parameter type */ |
|
868 |
param_type = fp_iterator.param_type(); |
|
869 |
/* If the declared parameter and the parameter from the function call have the same type */ |
|
257 | 870 |
if(!is_valid_assignment(param_type, call_param_type)) STAGE3_ERROR(call_param_name, call_param_value, "Type mismatch function/FB call parameter."); |
204 | 871 |
} |
872 |
} |
|
873 |
} |
|
874 |
||
875 |
||
876 |
||
877 |
||
878 |
/* a helper function... */ |
|
879 |
symbol_c *visit_expression_type_c::base_type(symbol_c *symbol) { |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
880 |
/* NOTE: symbol == NULL is valid. It will occur when, for e.g., an undefined/undeclared symbolic_variable is used |
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
881 |
* in the code. |
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
882 |
*/ |
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
260
diff
changeset
|
883 |
if (symbol == NULL) return NULL; |
204 | 884 |
return (symbol_c *)symbol->accept(search_base_type); |
885 |
} |
|
886 |
||
887 |
||
888 |
/* a helper function... */ |
|
889 |
void *visit_expression_type_c::verify_null(symbol_c *symbol){ |
|
890 |
if(il_default_variable_type == NULL){ |
|
891 |
STAGE3_ERROR(symbol, symbol, "Il default variable can't be NULL."); |
|
892 |
} |
|
893 |
if(il_operand_type == NULL){ |
|
894 |
STAGE3_ERROR(symbol, symbol, "function requires an operand."); |
|
895 |
} |
|
896 |
return NULL; |
|
897 |
} |
|
898 |
||
899 |
||
900 |
||
901 |
||
902 |
/*********************/ |
|
903 |
/* B 1.4 - Variables */ |
|
904 |
/*********************/ |
|
905 |
||
906 |
void *visit_expression_type_c::visit(symbolic_variable_c *symbol) { |
|
907 |
return search_varfb_instance_type->get_type(symbol); |
|
908 |
} |
|
909 |
||
910 |
/********************************************/ |
|
911 |
/* B 1.4.1 - Directly Represented Variables */ |
|
912 |
/********************************************/ |
|
913 |
void *visit_expression_type_c::visit(direct_variable_c *symbol) { |
|
914 |
switch (symbol->value[2]) { |
|
915 |
case 'X': // bit - 1 bit |
|
916 |
return (void *)&bool_type_name; |
|
917 |
case 'B': // byte - 8 bits |
|
918 |
return (void *)&byte_type_name; |
|
919 |
case 'W': // word - 16 bits |
|
920 |
return (void *)&word_type_name; |
|
921 |
case 'D': // double word - 32 bits |
|
922 |
return (void *)&dword_type_name; |
|
923 |
case 'L': // long word - 64 bits |
|
924 |
return (void *)&lword_type_name; |
|
925 |
default: // if none of the above, then the empty string was used <=> boolean |
|
926 |
return (void *)&bool_type_name; |
|
927 |
} |
|
928 |
} |
|
929 |
||
930 |
/*************************************/ |
|
931 |
/* B 1.4.2 - Multi-element variables */ |
|
932 |
/*************************************/ |
|
933 |
void *visit_expression_type_c::visit(array_variable_c *symbol) { |
|
934 |
return search_varfb_instance_type->get_type(symbol); |
|
935 |
} |
|
936 |
||
937 |
void *visit_expression_type_c::visit(structured_variable_c *symbol) { |
|
938 |
return search_varfb_instance_type->get_type(symbol); |
|
939 |
} |
|
940 |
||
941 |
||
942 |
||
943 |
/****************************************/ |
|
944 |
/* B.2 - Language IL (Instruction List) */ |
|
945 |
/****************************************/ |
|
946 |
/***********************************/ |
|
947 |
/* B 2.1 Instructions and Operands */ |
|
948 |
/***********************************/ |
|
949 |
/*| instruction_list il_instruction */ |
|
950 |
/* The visitor of the base class search_visitor_c will handle calling each instruction in the list. |
|
951 |
* We do not need to do anything here... |
|
952 |
*/ |
|
953 |
// void *visit_expression_type_c::visit(instruction_list_c *symbol) |
|
954 |
||
955 |
/* | label ':' [il_incomplete_instruction] eol_list */ |
|
956 |
//SYM_REF2(il_instruction_c, label, il_instruction) |
|
957 |
// void *visit_expression_type_c::visit(il_instruction_c *symbol); |
|
958 |
||
959 |
||
960 |
/* | il_simple_operator [il_operand] */ |
|
961 |
// SYM_REF2(il_simple_operation_c, il_simple_operator, il_operand) |
|
962 |
void *visit_expression_type_c::visit(il_simple_operation_c *symbol) { |
|
963 |
if (il_error) |
|
964 |
return NULL; |
|
965 |
||
966 |
/* determine the data type of the operand */ |
|
967 |
if (symbol->il_operand != NULL){ |
|
968 |
il_operand_type = base_type((symbol_c *)symbol->il_operand->accept(*this)); |
|
969 |
} else { |
|
970 |
il_operand_type = NULL; |
|
971 |
} |
|
972 |
/* recursive call to see whether data types are compatible */ |
|
973 |
symbol->il_simple_operator->accept(*this); |
|
974 |
||
975 |
il_operand_type = NULL; |
|
976 |
return NULL; |
|
977 |
} |
|
978 |
||
979 |
// | function_name [il_operand_list] */ |
|
980 |
//SYM_REF2(il_function_call_c, function_name, il_operand_list) |
|
981 |
void *visit_expression_type_c::visit(il_function_call_c *symbol) { |
|
982 |
if (il_error) |
|
983 |
return NULL; |
|
984 |
||
985 |
/* First find the declaration of the function being called! */ |
|
986 |
function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name); |
|
987 |
||
988 |
symbol_c *return_data_type = NULL; |
|
989 |
||
990 |
if (f_decl == function_symtable.end_value()) { |
|
991 |
function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name); |
|
992 |
if (current_function_type == function_none) ERROR; |
|
993 |
/* This code is for the functions that the user did not declare and that are |
|
994 |
* part of the IL or ST languagem (built-in functions). |
|
995 |
* For now we won't do the semantics analysis for that kind of functions. |
|
996 |
*/ |
|
997 |
/* |
|
998 |
return_data_type = (symbol_c *)search_expression_type->compute_standard_function_default(NULL, symbol); |
|
999 |
if (NULL == return_data_type) ERROR; |
|
1000 |
||
1001 |
function_call_param_iterator_c fcp_iterator(symbol); |
|
1002 |
||
1003 |
int nb_param = 0; |
|
1004 |
if (symbol->il_param_list != NULL) |
|
1005 |
nb_param += ((list_c *)symbol->il_param_list)->n; |
|
1006 |
||
1007 |
identifier_c en_param_name("EN");*/ |
|
1008 |
/* Get the value from EN param */ |
|
1009 |
/*symbol_c *EN_param_value = fcp_iterator.search(&en_param_name); |
|
1010 |
if (EN_param_value == NULL) |
|
1011 |
EN_param_value = (symbol_c*)(new boolean_literal_c((symbol_c*)(new bool_type_name_c()), new boolean_true_c())); |
|
1012 |
else |
|
1013 |
nb_param --; |
|
1014 |
ADD_PARAM_LIST(EN_param_value, (symbol_c*)(new bool_type_name_c()), function_param_iterator_c::direction_in) |
|
1015 |
||
1016 |
identifier_c eno_param_name("EN0");*/ |
|
1017 |
/* Get the value from ENO param */ |
|
1018 |
/*symbol_c *ENO_param_value = fcp_iterator.search(&eno_param_name); |
|
1019 |
if (ENO_param_value != NULL) |
|
1020 |
nb_param --; |
|
1021 |
ADD_PARAM_LIST(ENO_param_value, (symbol_c*)(new bool_type_name_c()), function_param_iterator_c::direction_out) |
|
1022 |
||
1023 |
#include "st_code_gen.c" |
|
1024 |
*/ |
|
1025 |
} else { |
|
1026 |
/* determine the base data type returned by the function being called... */ |
|
1027 |
return_data_type = base_type(f_decl->type_name); |
|
1028 |
/* If the following occurs, then we must have some big bug in the syntax parser (stage 2)... */ |
|
1029 |
if (NULL == return_data_type) ERROR; |
|
1030 |
||
1031 |
/* check semantics of data passed in the function call... */ |
|
1032 |
check_nonformal_call(symbol, f_decl, true); |
|
1033 |
||
1034 |
/* set the new ddata type of the default variable for the following verifications... */ |
|
1035 |
il_default_variable_type = return_data_type; |
|
1036 |
} |
|
1037 |
return NULL; |
|
1038 |
} |
|
1039 |
||
1040 |
||
1041 |
/* | il_expr_operator '(' [il_operand] eol_list [simple_instr_list] ')' */ |
|
1042 |
// SYM_REF3(il_expression_c, il_expr_operator, il_operand, simple_instr_list); |
|
1043 |
void *visit_expression_type_c::visit(il_expression_c *symbol) { |
|
1044 |
if (il_error) |
|
1045 |
return NULL; |
|
1046 |
||
1047 |
symbol_c *il_default_variable_type_back = il_default_variable_type; |
|
1048 |
||
1049 |
il_parenthesis_level++; |
|
1050 |
||
1051 |
if(symbol->il_operand != NULL) { |
|
1052 |
il_default_variable_type = base_type((symbol_c *)symbol->il_operand->accept(*this)); |
|
1053 |
} else { |
|
1054 |
il_default_variable_type = NULL; |
|
1055 |
} |
|
1056 |
||
1057 |
if(symbol->simple_instr_list != NULL) { |
|
1058 |
symbol->simple_instr_list->accept(*this); |
|
1059 |
} |
|
1060 |
||
1061 |
il_parenthesis_level--; |
|
1062 |
if (il_parenthesis_level < 0) ERROR; |
|
1063 |
||
1064 |
il_operand_type = il_default_variable_type; |
|
1065 |
il_default_variable_type = il_default_variable_type_back; |
|
1066 |
||
1067 |
/* Now check the if the data type semantics of operation are correct, |
|
1068 |
* but only if no previous error has been found... |
|
1069 |
*/ |
|
1070 |
if (il_error) |
|
1071 |
return NULL; |
|
1072 |
symbol->il_expr_operator->accept(*this); |
|
1073 |
||
1074 |
return NULL; |
|
1075 |
} |
|
1076 |
||
1077 |
||
1078 |
#if 0 |
|
1079 |
/* il_jump_operator label */ |
|
1080 |
SYM_REF2(il_jump_operation_c, il_jump_operator, label) |
|
1081 |
void *visit_expression_type_c::visit(il_jump_operation_c *symbol); |
|
1082 |
#endif |
|
1083 |
||
1084 |
||
1085 |
/* il_call_operator prev_declared_fb_name |
|
1086 |
* | il_call_operator prev_declared_fb_name '(' ')' |
|
1087 |
* | il_call_operator prev_declared_fb_name '(' eol_list ')' |
|
1088 |
* | il_call_operator prev_declared_fb_name '(' il_operand_list ')' |
|
1089 |
* | il_call_operator prev_declared_fb_name '(' eol_list il_param_list ')' |
|
1090 |
*/ |
|
1091 |
/* SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list) */ |
|
1092 |
void *visit_expression_type_c::visit(il_fb_call_c *symbol) { |
|
1093 |
if (il_error) |
|
1094 |
return NULL; |
|
1095 |
||
1096 |
/* first check whether the il_default_variable is of the correct type |
|
1097 |
* for the CAL / CALC / CALCN operator being used... |
|
1098 |
*/ |
|
1099 |
symbol->il_call_operator->accept(*this); |
|
1100 |
||
1101 |
/* Now check the FB call itself... */ |
|
1102 |
||
1103 |
/* First we find the declaration of the FB type of the FB instance being called... */ |
|
1104 |
/* e.g. Function_block foo_fb_type |
|
1105 |
* ... |
|
1106 |
* End_Function_Block |
|
1107 |
* |
|
1108 |
* Program test |
|
1109 |
* var fb1 : foo_fb_type; end_var |
|
1110 |
* fb1(...) |
|
1111 |
* End_Program |
|
1112 |
* |
|
1113 |
* search_varfb_instance_type->get_type( identifier_c("fb1") ) |
|
1114 |
* in the scope of Program 'test' |
|
1115 |
* will return the fb declaration of foo_fb_type !! |
|
1116 |
*/ |
|
1117 |
#if 0 |
|
1118 |
symbol_c *fb_decl_symbol = search_varfb_instance_type->get_type(symbol->fb_name); |
|
1119 |
/* The following should never occur. The function block must be defined, |
|
1120 |
* and the FB type being called MUST be in the symtable... |
|
1121 |
* This was all already checked at stage 2! |
|
1122 |
*/ |
|
1123 |
if (NULL == fb_decl_symbol) ERROR; |
|
1124 |
||
1125 |
function_block_declaration_c *fb_decl = dynamic_cast<function_block_declaration_c *>(fb_decl_symbol); |
|
1126 |
/* should never occur. ... */ |
|
1127 |
if (NULL == fb_decl) ERROR; |
|
1128 |
#endif |
|
1129 |
symbol_c *fb_decl = search_varfb_instance_type->get_type(symbol->fb_name); |
|
1130 |
/* The following should never occur. The function block must be defined, |
|
1131 |
* and the FB type being called MUST be in the symtable... |
|
1132 |
* This was all already checked at stage 2! |
|
1133 |
*/ |
|
1134 |
if (NULL == fb_decl) ERROR; |
|
1135 |
||
1136 |
/* now check the semantics of the fb call... */ |
|
1137 |
/* If the syntax parser is working correctly, exactly one of the |
|
1138 |
* following two symbols will be NULL, while the other is != NULL. |
|
1139 |
*/ |
|
1140 |
if (NULL != symbol->il_operand_list) check_nonformal_call(symbol, fb_decl); |
|
1141 |
if (NULL != symbol->il_param_list) check_formal_call (symbol, fb_decl); |
|
1142 |
||
1143 |
return NULL; |
|
1144 |
} |
|
1145 |
||
1146 |
||
1147 |
||
1148 |
/* | function_name '(' eol_list [il_param_list] ')' */ |
|
1149 |
/* SYM_REF2(il_formal_funct_call_c, function_name, il_param_list) */ |
|
1150 |
void *visit_expression_type_c::visit(il_formal_funct_call_c *symbol) { |
|
1151 |
if (il_error) |
|
1152 |
return NULL; |
|
1153 |
||
1154 |
function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name); |
|
1155 |
||
1156 |
symbol_c *return_data_type = NULL; |
|
1157 |
||
1158 |
if (f_decl == function_symtable.end_value()) { |
|
1159 |
function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name); |
|
1160 |
if (current_function_type == function_none) ERROR; |
|
1161 |
||
1162 |
/* This code is for the functions that the user did not declare and that are |
|
1163 |
* part of the IL or ST languagem (built-in functions). |
|
1164 |
* For now we won't do the semantics analysis for that kind of functions. |
|
1165 |
*/ |
|
1166 |
#if 0 |
|
1167 |
return_data_type = (symbol_c *)search_expression_type->compute_standard_function_default(NULL, symbol); |
|
1168 |
if (NULL == return_data_type) ERROR; |
|
1169 |
||
1170 |
function_call_param_iterator_c fcp_iterator(symbol); |
|
1171 |
||
1172 |
int nb_param = 0; |
|
1173 |
if (symbol->il_param_list != NULL) |
|
1174 |
nb_param += ((list_c *)symbol->il_param_list)->n; |
|
1175 |
||
1176 |
identifier_c en_param_name("EN"); |
|
1177 |
/* Get the value from EN param */ |
|
1178 |
symbol_c *EN_param_value = fcp_iterator.search(&en_param_name); |
|
1179 |
if (EN_param_value == NULL) |
|
1180 |
EN_param_value = (symbol_c*)(new boolean_literal_c((symbol_c*)(new bool_type_name_c()), new boolean_true_c())); |
|
1181 |
else |
|
1182 |
nb_param --; |
|
1183 |
ADD_PARAM_LIST(EN_param_value, (symbol_c*)(new bool_type_name_c()), function_param_iterator_c::direction_in) |
|
1184 |
||
1185 |
identifier_c eno_param_name("EN0"); |
|
1186 |
/* Get the value from ENO param */ |
|
1187 |
symbol_c *ENO_param_value = fcp_iterator.search(&eno_param_name); |
|
1188 |
if (ENO_param_value != NULL) |
|
1189 |
nb_param --; |
|
1190 |
ADD_PARAM_LIST(ENO_param_value, (symbol_c*)(new bool_type_name_c()), function_param_iterator_c::direction_out) |
|
1191 |
||
1192 |
#include "st_code_gen.c" |
|
1193 |
#endif |
|
1194 |
} else { |
|
1195 |
/* determine the base data type returned by the function being called... */ |
|
1196 |
return_data_type = base_type(f_decl->type_name); |
|
1197 |
/* the following should never occur. If it does, then we have a bug in the syntax parser (stage 2)... */ |
|
1198 |
if (NULL == return_data_type) ERROR; |
|
1199 |
||
1200 |
/* check semantics of data passed in the function call... */ |
|
1201 |
check_formal_call(symbol, f_decl); |
|
1202 |
||
1203 |
/* the data type of the data returned by the function, and stored in the il default variable... */ |
|
1204 |
il_default_variable_type = return_data_type; |
|
1205 |
} |
|
1206 |
return NULL; |
|
1207 |
} |
|
1208 |
||
1209 |
||
1210 |
#if 0 |
|
1211 |
/* | il_operand_list ',' il_operand */ |
|
1212 |
SYM_LIST(il_operand_list_c) |
|
1213 |
void *visit_expression_type_c::visit(il_operand_list_c *symbol); |
|
1214 |
||
1215 |
/* | simple_instr_list il_simple_instruction */ |
|
1216 |
SYM_LIST(simple_instr_list_c) |
|
1217 |
void *visit_expression_type_c::visit(simple_instr_list_c *symbol); |
|
1218 |
||
1219 |
/* | il_initial_param_list il_param_instruction */ |
|
1220 |
SYM_LIST(il_param_list_c) |
|
1221 |
void *visit_expression_type_c::visit(il_param_list_c *symbol); |
|
1222 |
||
1223 |
/* il_assign_operator il_operand |
|
1224 |
* | il_assign_operator '(' eol_list simple_instr_list ')' |
|
1225 |
*/ |
|
1226 |
SYM_REF3(il_param_assignment_c, il_assign_operator, il_operand, simple_instr_list) |
|
1227 |
void *visit_expression_type_c::visit(il_param_assignment_c *symbol); |
|
1228 |
/* il_assign_out_operator variable */ |
|
1229 |
SYM_REF2(il_param_out_assignment_c, il_assign_out_operator, variable) |
|
1230 |
void *visit_expression_type_c::visit(il_param_out_assignment_c *symbol); |
|
1231 |
||
1232 |
#endif |
|
1233 |
||
1234 |
||
1235 |
/*******************/ |
|
1236 |
/* B 2.2 Operators */ |
|
1237 |
/*******************/ |
|
1238 |
||
1239 |
//SYM_REF0(LD_operator_c) |
|
1240 |
void *visit_expression_type_c::visit(LD_operator_c *symbol) { |
|
1241 |
if (0 == il_parenthesis_level) |
|
1242 |
il_error = false; |
|
1243 |
||
1244 |
if(il_operand_type == NULL) |
|
1245 |
STAGE3_ERROR(symbol, symbol, "LD operator requires an operand."); |
|
1246 |
il_default_variable_type = il_operand_type; |
|
1247 |
return NULL; |
|
1248 |
} |
|
1249 |
||
1250 |
// SYM_REF0(LDN_operator_c) |
|
1251 |
void *visit_expression_type_c::visit(LDN_operator_c *symbol) { |
|
1252 |
if(il_operand_type == NULL) |
|
1253 |
STAGE3_ERROR(symbol, symbol, "LDN operator requires an operand."); |
|
257 | 1254 |
if(!is_ANY_BIT_compatible(il_operand_type)) |
204 | 1255 |
STAGE3_ERROR(symbol, symbol, "invalid data type of LDN operand, should be of type ANY_BIT."); |
1256 |
il_default_variable_type = il_operand_type; |
|
1257 |
return NULL; |
|
1258 |
} |
|
1259 |
||
1260 |
// SYM_REF0(ST_operator_c) |
|
1261 |
void *visit_expression_type_c::visit(ST_operator_c *symbol) { |
|
1262 |
verify_null(symbol); |
|
257 | 1263 |
|
1264 |
if(!is_valid_assignment(il_operand_type, il_default_variable_type)) |
|
204 | 1265 |
STAGE3_ERROR(symbol, symbol, "Type mismatch in ST operation."); |
1266 |
/* TODO: check whether il_operand_type is an LVALUE !! */ |
|
1267 |
/* data type of il_default_variable_type is unchanged... */ |
|
1268 |
// il_default_variable_type = il_default_variable_type; |
|
1269 |
return NULL; |
|
1270 |
} |
|
1271 |
||
1272 |
// SYM_REF0(STN_operator_c) |
|
1273 |
void *visit_expression_type_c::visit(STN_operator_c *symbol) { |
|
1274 |
verify_null(symbol); |
|
257 | 1275 |
if(!is_valid_assignment(il_operand_type, il_default_variable_type)) |
204 | 1276 |
STAGE3_ERROR(symbol, symbol, "Type mismatch in ST operation."); |
1277 |
/* TODO: check whether il_operand_type is an LVALUE !! */ |
|
257 | 1278 |
if(!is_ANY_BIT_compatible(il_default_variable_type)) |
204 | 1279 |
STAGE3_ERROR(symbol, symbol, "invalid data type of il_default_variable for STN operand, should be of type ANY_BIT."); |
257 | 1280 |
if(!is_ANY_BIT_compatible(il_operand_type)) |
204 | 1281 |
STAGE3_ERROR(symbol, symbol, "invalid data type of STN operand, should be of type ANY_BIT."); |
1282 |
/* data type of il_default_variable_type is unchanged... */ |
|
1283 |
// il_default_variable_type = il_default_variable_type; |
|
1284 |
return NULL; |
|
1285 |
} |
|
1286 |
||
1287 |
//SYM_REF0(NOT_operator_c) |
|
1288 |
void *visit_expression_type_c::visit(NOT_operator_c *symbol) { |
|
1289 |
if(il_operand_type != NULL){ |
|
1290 |
STAGE3_ERROR(symbol, symbol, "NOT operator may not have an operand."); |
|
1291 |
return NULL; |
|
1292 |
} |
|
1293 |
if(il_default_variable_type == NULL) { |
|
1294 |
STAGE3_ERROR(symbol, symbol, "Il default variable should not be NULL."); |
|
1295 |
return NULL; |
|
1296 |
} |
|
257 | 1297 |
if(!is_ANY_BIT_compatible(il_default_variable_type)) { |
204 | 1298 |
STAGE3_ERROR(symbol, symbol, "Il default variable should be of type ANY_BIT."); |
1299 |
return NULL; |
|
1300 |
} |
|
1301 |
/* data type of il_default_variable_type is unchanged... */ |
|
1302 |
// il_default_variable_type = il_default_variable_type; |
|
1303 |
return NULL; |
|
1304 |
} |
|
1305 |
||
1306 |
// SYM_REF0(S_operator_c) |
|
1307 |
void *visit_expression_type_c::visit(S_operator_c *symbol) { |
|
1308 |
verify_null(symbol); |
|
1309 |
if (!is_BOOL_type(il_default_variable_type)) {STAGE3_ERROR(symbol, symbol, "IL default variable should be BOOL type.");} |
|
1310 |
if (!is_BOOL_type(il_operand_type)) {STAGE3_ERROR(symbol, symbol, "operator S requires operand of type BOOL.");} |
|
1311 |
/* TODO: check whether il_operand_type is an LVALUE !! */ |
|
1312 |
/* data type of il_default_variable_type is unchanged... */ |
|
1313 |
// il_default_variable_type = il_default_variable_type; |
|
1314 |
return NULL; |
|
1315 |
} |
|
1316 |
||
1317 |
// SYM_REF0(R_operator_c) |
|
1318 |
void *visit_expression_type_c::visit(R_operator_c *symbol) { |
|
1319 |
verify_null(symbol); |
|
1320 |
if (!is_BOOL_type(il_default_variable_type)) {STAGE3_ERROR(symbol, symbol, "IL default variable should be BOOL type.");} |
|
1321 |
if (!is_BOOL_type(il_operand_type)) {STAGE3_ERROR(symbol, symbol, "operator R requires operand of type BOOL.");} |
|
1322 |
/* TODO: check whether il_operand_type is an LVALUE !! */ |
|
1323 |
/* data type of il_default_variable_type is unchanged... */ |
|
1324 |
// il_default_variable_type = il_default_variable_type; |
|
1325 |
return NULL; |
|
1326 |
} |
|
1327 |
||
1328 |
||
1329 |
// SYM_REF0(S1_operator_c) |
|
1330 |
void *visit_expression_type_c::visit(S1_operator_c *symbol){ |
|
1331 |
compute_input_operatores(symbol, "S1"); |
|
1332 |
return NULL; |
|
1333 |
} |
|
1334 |
||
1335 |
// SYM_REF0(R1_operator_c) |
|
1336 |
void *visit_expression_type_c::visit(R1_operator_c *symbol) { |
|
1337 |
compute_input_operatores(symbol, "R1"); |
|
1338 |
return NULL; |
|
1339 |
} |
|
1340 |
||
1341 |
// SYM_REF0(CLK_operator_c) |
|
1342 |
void *visit_expression_type_c::visit(CLK_operator_c *symbol) { |
|
1343 |
compute_input_operatores(symbol, "CLK"); |
|
1344 |
return NULL; |
|
1345 |
} |
|
1346 |
||
1347 |
// SYM_REF0(CU_operator_c) |
|
1348 |
void *visit_expression_type_c::visit(CU_operator_c *symbol) { |
|
1349 |
compute_input_operatores(symbol, "CU"); |
|
1350 |
return NULL; |
|
1351 |
} |
|
1352 |
||
1353 |
// SYM_REF0(CD_operator_c) |
|
1354 |
void *visit_expression_type_c::visit(CD_operator_c *symbol) { |
|
1355 |
compute_input_operatores(symbol, "CD"); |
|
1356 |
return NULL; |
|
1357 |
} |
|
1358 |
||
1359 |
// SYM_REF0(PV_operator_c) |
|
1360 |
void *visit_expression_type_c::visit(PV_operator_c *symbol) { |
|
1361 |
compute_input_operatores(symbol, "PV"); |
|
1362 |
return NULL; |
|
1363 |
} |
|
1364 |
||
1365 |
// SYM_REF0(IN_operator_c) |
|
1366 |
void *visit_expression_type_c::visit(IN_operator_c *symbol) { |
|
1367 |
compute_input_operatores(symbol, "IN"); |
|
1368 |
return NULL; |
|
1369 |
} |
|
1370 |
||
1371 |
// SYM_REF0(PT_operator_c) |
|
1372 |
void *visit_expression_type_c::visit(PT_operator_c *symbol) { |
|
1373 |
compute_input_operatores(symbol, "PT"); |
|
1374 |
return NULL; |
|
1375 |
} |
|
1376 |
||
1377 |
//SYM_REF0(AND_operator_c) |
|
1378 |
void *visit_expression_type_c::visit(AND_operator_c *symbol) { |
|
1379 |
verify_null(symbol); |
|
257 | 1380 |
il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_BIT_compatible); |
204 | 1381 |
return NULL; |
1382 |
} |
|
1383 |
||
1384 |
//SYM_REF0(OR_operator_c) |
|
1385 |
void *visit_expression_type_c::visit(OR_operator_c *symbol) { |
|
1386 |
verify_null(symbol); |
|
257 | 1387 |
il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_BIT_compatible); |
204 | 1388 |
return NULL; |
1389 |
} |
|
1390 |
||
1391 |
//SYM_REF0(XOR_operator_c) |
|
1392 |
void *visit_expression_type_c::visit(XOR_operator_c *symbol) { |
|
1393 |
verify_null(symbol); |
|
257 | 1394 |
il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_BIT_compatible); |
204 | 1395 |
return NULL; |
1396 |
} |
|
1397 |
||
1398 |
// SYM_REF0(ANDN_operator_c) |
|
1399 |
void *visit_expression_type_c::visit(ANDN_operator_c *symbol) { |
|
1400 |
verify_null(symbol); |
|
257 | 1401 |
il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_BIT_compatible); |
204 | 1402 |
return NULL; |
1403 |
} |
|
1404 |
||
1405 |
// SYM_REF0(ORN_operator_c) |
|
1406 |
void *visit_expression_type_c::visit(ORN_operator_c *symbol) { |
|
1407 |
verify_null(symbol); |
|
257 | 1408 |
il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_BIT_compatible); |
204 | 1409 |
return NULL; |
1410 |
} |
|
1411 |
||
1412 |
// SYM_REF0(XORN_operator_c) |
|
1413 |
void *visit_expression_type_c::visit(XORN_operator_c *symbol) { |
|
1414 |
verify_null(symbol); |
|
257 | 1415 |
il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_BIT_compatible); |
204 | 1416 |
return NULL; |
1417 |
} |
|
1418 |
||
1419 |
// SYM_REF0(ADD_operator_c) |
|
1420 |
void *visit_expression_type_c::visit(ADD_operator_c *symbol) { |
|
1421 |
verify_null(symbol); |
|
1422 |
symbol_c *left_type = il_default_variable_type; |
|
1423 |
symbol_c *right_type = il_operand_type; |
|
257 | 1424 |
|
1425 |
/* The following is not required, it is already handled by compute_expression() ... */ |
|
1426 |
/* |
|
1427 |
if (is_type(left_type, time_type_name_c) && is_type(right_type, time_type_name_c)) |
|
204 | 1428 |
il_default_variable_type = &time_type_name; |
257 | 1429 |
*/ |
1430 |
||
1431 |
if (is_type(left_type, tod_type_name_c) && is_type(right_type, time_type_name_c)) |
|
204 | 1432 |
il_default_variable_type = &tod_type_name; |
257 | 1433 |
else if (is_type(left_type, safetod_type_name_c) && is_type(right_type, time_type_name_c)) |
1434 |
il_default_variable_type = &tod_type_name; |
|
1435 |
else if (is_type(left_type, tod_type_name_c) && is_type(right_type, safetime_type_name_c)) |
|
1436 |
il_default_variable_type = &tod_type_name; |
|
1437 |
else if (is_type(left_type, safetod_type_name_c) && is_type(right_type, safetime_type_name_c)) |
|
1438 |
il_default_variable_type = &safetod_type_name; |
|
1439 |
||
1440 |
else if (is_type(left_type, dt_type_name_c) && is_type(right_type, time_type_name_c)) |
|
204 | 1441 |
il_default_variable_type = &dt_type_name; |
257 | 1442 |
else if (is_type(left_type, safedt_type_name_c) && is_type(right_type, time_type_name_c)) |
1443 |
il_default_variable_type = &dt_type_name; |
|
1444 |
else if (is_type(left_type, dt_type_name_c) && is_type(right_type, safetime_type_name_c)) |
|
1445 |
il_default_variable_type = &dt_type_name; |
|
1446 |
else if (is_type(left_type, safedt_type_name_c) && is_type(right_type, safetime_type_name_c)) |
|
1447 |
il_default_variable_type = &safedt_type_name; |
|
1448 |
||
1449 |
else il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_MAGNITUDE_compatible); |
|
204 | 1450 |
return NULL; |
1451 |
} |
|
1452 |
||
1453 |
// SYM_REF0(SUB_operator_c) |
|
1454 |
void *visit_expression_type_c::visit(SUB_operator_c *symbol) { |
|
1455 |
verify_null(symbol); |
|
1456 |
symbol_c *left_type = il_default_variable_type; |
|
1457 |
symbol_c *right_type = il_operand_type;; |
|
257 | 1458 |
|
1459 |
/* The following is not required, it is already handled by compute_expression() ... */ |
|
1460 |
/* |
|
204 | 1461 |
if (typeid(*left_type) == typeid(time_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) |
1462 |
il_default_variable_type = &time_type_name; |
|
257 | 1463 |
*/ |
1464 |
||
1465 |
if (is_type(left_type, tod_type_name_c) && is_type(right_type, time_type_name_c)) |
|
1466 |
il_default_variable_type = &tod_type_name; |
|
1467 |
else if (is_type(left_type, safetod_type_name_c) && is_type(right_type, time_type_name_c)) |
|
1468 |
il_default_variable_type = &tod_type_name; |
|
1469 |
else if (is_type(left_type, tod_type_name_c) && is_type(right_type, safetime_type_name_c)) |
|
1470 |
il_default_variable_type = &tod_type_name; |
|
1471 |
else if (is_type(left_type, safetod_type_name_c) && is_type(right_type, safetime_type_name_c)) |
|
1472 |
il_default_variable_type = &safetod_type_name; |
|
1473 |
||
1474 |
else if (is_type(left_type, dt_type_name_c) && is_type(right_type, time_type_name_c)) |
|
1475 |
il_default_variable_type = &dt_type_name; |
|
1476 |
else if (is_type(left_type, safedt_type_name_c) && is_type(right_type, time_type_name_c)) |
|
1477 |
il_default_variable_type = &dt_type_name; |
|
1478 |
else if (is_type(left_type, dt_type_name_c) && is_type(right_type, safetime_type_name_c)) |
|
1479 |
il_default_variable_type = &dt_type_name; |
|
1480 |
else if (is_type(left_type, safedt_type_name_c) && is_type(right_type, safetime_type_name_c)) |
|
1481 |
il_default_variable_type = &safedt_type_name; |
|
1482 |
||
1483 |
else if (is_type(left_type, date_type_name_c) && is_type(right_type, date_type_name_c)) |
|
204 | 1484 |
il_default_variable_type = &time_type_name; |
257 | 1485 |
else if (is_type(left_type, safedate_type_name_c) && is_type(right_type, date_type_name_c)) |
204 | 1486 |
il_default_variable_type = &time_type_name; |
257 | 1487 |
else if (is_type(left_type, date_type_name_c) && is_type(right_type, safedate_type_name_c)) |
204 | 1488 |
il_default_variable_type = &time_type_name; |
257 | 1489 |
else if (is_type(left_type, safedate_type_name_c) && is_type(right_type, safedate_type_name_c)) |
1490 |
il_default_variable_type = &safetime_type_name; |
|
1491 |
||
1492 |
else if (is_type(left_type, tod_type_name_c) && is_type(right_type, tod_type_name_c)) |
|
1493 |
il_default_variable_type = &time_type_name; |
|
1494 |
else if (is_type(left_type, safetod_type_name_c) && is_type(right_type, tod_type_name_c)) |
|
1495 |
il_default_variable_type = &time_type_name; |
|
1496 |
else if (is_type(left_type, tod_type_name_c) && is_type(right_type, safetod_type_name_c)) |
|
1497 |
il_default_variable_type = &time_type_name; |
|
1498 |
else if (is_type(left_type, safetod_type_name_c) && is_type(right_type, safetod_type_name_c)) |
|
1499 |
il_default_variable_type = &safetime_type_name; |
|
1500 |
||
1501 |
else if (is_type(left_type, dt_type_name_c) && is_type(right_type, dt_type_name_c)) |
|
1502 |
il_default_variable_type = &time_type_name; |
|
1503 |
else if (is_type(left_type, safedt_type_name_c) && is_type(right_type, dt_type_name_c)) |
|
1504 |
il_default_variable_type = &time_type_name; |
|
1505 |
else if (is_type(left_type, dt_type_name_c) && is_type(right_type, safedt_type_name_c)) |
|
1506 |
il_default_variable_type = &time_type_name; |
|
1507 |
else if (is_type(left_type, safedt_type_name_c) && is_type(right_type, safedt_type_name_c)) |
|
1508 |
il_default_variable_type = &safetime_type_name; |
|
1509 |
||
1510 |
else il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_MAGNITUDE_compatible); |
|
204 | 1511 |
return NULL; |
1512 |
} |
|
1513 |
||
1514 |
// SYM_REF0(MUL_operator_c) |
|
1515 |
void *visit_expression_type_c::visit(MUL_operator_c *symbol) { |
|
1516 |
verify_null(symbol); |
|
1517 |
symbol_c *left_type = il_default_variable_type; |
|
1518 |
symbol_c *right_type = il_operand_type; |
|
257 | 1519 |
|
1520 |
if (is_type(left_type, time_type_name_c) && is_ANY_NUM_compatible(right_type)) |
|
204 | 1521 |
il_default_variable_type = &time_type_name; |
257 | 1522 |
else if (is_type(left_type, safetime_type_name_c) && is_ANY_NUM_type(right_type)) |
1523 |
il_default_variable_type = &time_type_name; |
|
1524 |
else if (is_type(left_type, safetime_type_name_c) && is_ANY_SAFENUM_type(right_type)) |
|
1525 |
il_default_variable_type = &safetime_type_name; |
|
1526 |
/* Since we have already checked for ANY_NUM_type and ANY_SAFENUM_type in the previous lines, |
|
1527 |
* this next line is really only to check for integers/reals of undefined type on 'right_type'... |
|
1528 |
*/ |
|
1529 |
else if (is_type(left_type, safetime_type_name_c) && is_ANY_NUM_compatible(right_type)) |
|
1530 |
il_default_variable_type = &safetime_type_name; |
|
1531 |
||
1532 |
else il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_NUM_compatible); |
|
204 | 1533 |
return NULL; |
1534 |
} |
|
1535 |
||
1536 |
// SYM_REF0(DIV_operator_c) |
|
1537 |
void *visit_expression_type_c::visit(DIV_operator_c *symbol) { |
|
1538 |
verify_null(symbol); |
|
1539 |
symbol_c *left_type = il_default_variable_type; |
|
1540 |
symbol_c *right_type = il_operand_type; |
|
257 | 1541 |
|
1542 |
if (is_type(left_type, time_type_name_c) && is_ANY_NUM_compatible(right_type)) |
|
204 | 1543 |
il_default_variable_type = &time_type_name; |
257 | 1544 |
else if (is_type(left_type, safetime_type_name_c) && is_ANY_NUM_type(right_type)) |
1545 |
il_default_variable_type = &time_type_name; |
|
1546 |
else if (is_type(left_type, safetime_type_name_c) && is_ANY_SAFENUM_type(right_type)) |
|
1547 |
il_default_variable_type = &safetime_type_name; |
|
1548 |
/* Since we have already checked for ANY_NUM_type and ANY_SAFENUM_type in the previous lines, |
|
1549 |
* this next line is really only to check for integers/reals of undefined type on 'right_type'... |
|
1550 |
*/ |
|
1551 |
else if (is_type(left_type, safetime_type_name_c) && is_ANY_NUM_compatible(right_type)) |
|
1552 |
il_default_variable_type = &safetime_type_name; |
|
1553 |
||
1554 |
else il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_NUM_compatible); |
|
204 | 1555 |
return NULL; |
1556 |
} |
|
1557 |
||
1558 |
// SYM_REF0(MOD_operator_c) |
|
1559 |
void *visit_expression_type_c::visit(MOD_operator_c *symbol) { |
|
1560 |
verify_null(symbol); |
|
257 | 1561 |
il_default_variable_type = compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_INT_compatible); |
204 | 1562 |
return NULL; |
1563 |
} |
|
1564 |
||
1565 |
// SYM_REF0(GT_operator_c) |
|
1566 |
void *visit_expression_type_c::visit(GT_operator_c *symbol) { |
|
1567 |
verify_null(symbol); |
|
257 | 1568 |
compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible); |
204 | 1569 |
il_default_variable_type = &search_expression_type_c::bool_type_name; |
1570 |
return NULL; |
|
1571 |
} |
|
1572 |
||
1573 |
//SYM_REF0(GE_operator_c) |
|
1574 |
void *visit_expression_type_c::visit(GE_operator_c *symbol) { |
|
1575 |
verify_null(symbol); |
|
257 | 1576 |
compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible); |
204 | 1577 |
il_default_variable_type = &search_expression_type_c::bool_type_name; |
1578 |
return NULL; |
|
1579 |
} |
|
1580 |
||
1581 |
//SYM_REF0(EQ_operator_c) |
|
1582 |
void *visit_expression_type_c::visit(EQ_operator_c *symbol) { |
|
1583 |
verify_null(symbol); |
|
257 | 1584 |
compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible); |
204 | 1585 |
il_default_variable_type = &search_expression_type_c::bool_type_name; |
1586 |
return NULL; |
|
1587 |
} |
|
1588 |
||
1589 |
//SYM_REF0(LT_operator_c) |
|
1590 |
void *visit_expression_type_c::visit(LT_operator_c *symbol) { |
|
1591 |
verify_null(symbol); |
|
257 | 1592 |
compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible); |
204 | 1593 |
il_default_variable_type = &search_expression_type_c::bool_type_name; |
1594 |
return NULL; |
|
1595 |
} |
|
1596 |
||
1597 |
//SYM_REF0(LE_operator_c) |
|
1598 |
void *visit_expression_type_c::visit(LE_operator_c *symbol) { |
|
1599 |
verify_null(symbol); |
|
257 | 1600 |
compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible); |
204 | 1601 |
il_default_variable_type = &search_expression_type_c::bool_type_name; |
1602 |
return NULL; |
|
1603 |
} |
|
1604 |
||
1605 |
//SYM_REF0(NE_operator_c) |
|
1606 |
void *visit_expression_type_c::visit(NE_operator_c *symbol) { |
|
1607 |
verify_null(symbol); |
|
257 | 1608 |
compute_expression(il_default_variable_type, il_operand_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible); |
204 | 1609 |
il_default_variable_type = &search_expression_type_c::bool_type_name; |
1610 |
return NULL; |
|
1611 |
} |
|
1612 |
||
1613 |
// SYM_REF0(CAL_operator_c) |
|
1614 |
void *visit_expression_type_c::visit(CAL_operator_c *symbol) { |
|
1615 |
return NULL; |
|
1616 |
} |
|
1617 |
||
1618 |
// SYM_REF0(CALC_operator_c) |
|
1619 |
void *visit_expression_type_c::visit(CALC_operator_c *symbol) { |
|
1620 |
if(il_default_variable_type == NULL) |
|
1621 |
STAGE3_ERROR(symbol, symbol, "CALC: il default variable should not be NULL."); |
|
1622 |
if (!is_BOOL_type(il_default_variable_type)) |
|
1623 |
STAGE3_ERROR(symbol, symbol, "CALC operator requires il_default_variable to be of type BOOL."); |
|
1624 |
return NULL; |
|
1625 |
} |
|
1626 |
||
1627 |
// SYM_REF0(CALCN_operator_c) |
|
1628 |
void *visit_expression_type_c::visit(CALCN_operator_c *symbol) { |
|
1629 |
if(il_default_variable_type == NULL) |
|
1630 |
STAGE3_ERROR(symbol, symbol, "CALCN: il_default_variable should not be NULL."); |
|
1631 |
if (!is_BOOL_type(il_default_variable_type)) |
|
1632 |
STAGE3_ERROR(symbol, symbol, "CALCN operator requires il_default_variable to be of type BOOL."); |
|
1633 |
return NULL; |
|
1634 |
} |
|
1635 |
||
1636 |
// SYM_REF0(RET_operator_c) |
|
1637 |
void *visit_expression_type_c::visit(RET_operator_c *symbol) { |
|
1638 |
return NULL; |
|
1639 |
} |
|
1640 |
||
1641 |
// SYM_REF0(RETC_operator_c) |
|
1642 |
void *visit_expression_type_c::visit(RETC_operator_c *symbol) { |
|
1643 |
if(il_default_variable_type == NULL) |
|
1644 |
STAGE3_ERROR(symbol, symbol, "RETC: il default variable should not be NULL."); |
|
1645 |
if (!is_BOOL_type(il_default_variable_type)) |
|
1646 |
STAGE3_ERROR(symbol, symbol, "RETC operator requires il_default_variable to be of type BOOL."); |
|
1647 |
return NULL; |
|
1648 |
} |
|
1649 |
||
1650 |
// SYM_REF0(RETCN_operator_c) |
|
1651 |
void *visit_expression_type_c::visit(RETCN_operator_c *symbol) { |
|
1652 |
if(il_default_variable_type == NULL) |
|
1653 |
STAGE3_ERROR(symbol, symbol, "RETCN: il_default_variable should not be NULL."); |
|
1654 |
if (!is_BOOL_type(il_default_variable_type)) |
|
1655 |
STAGE3_ERROR(symbol, symbol, "RETCN operator requires il_default_variable to be of type BOOL."); |
|
1656 |
return NULL; |
|
1657 |
} |
|
1658 |
||
1659 |
// SYM_REF0(JMP_operator_c) |
|
1660 |
void *visit_expression_type_c::visit(JMP_operator_c *symbol){ |
|
1661 |
return NULL; |
|
1662 |
} |
|
1663 |
||
1664 |
// SYM_REF0(JMPC_operator_c) |
|
1665 |
void *visit_expression_type_c::visit(JMPC_operator_c *symbol) { |
|
1666 |
if(il_default_variable_type == NULL) |
|
1667 |
STAGE3_ERROR(symbol, symbol, "JMPC: il default variable should not be NULL."); |
|
1668 |
if (!is_BOOL_type(il_default_variable_type)) |
|
1669 |
STAGE3_ERROR(symbol, symbol, "JMPC operator requires il_default_variable to be of type BOOL."); |
|
1670 |
return NULL; |
|
1671 |
} |
|
1672 |
||
1673 |
// SYM_REF0(JMPCN_operator_c) |
|
1674 |
void *visit_expression_type_c::visit(JMPCN_operator_c *symbol) { |
|
1675 |
if(il_default_variable_type == NULL) |
|
1676 |
STAGE3_ERROR(symbol, symbol, "JMPCN: il_default_variable should not be NULL."); |
|
1677 |
if (!is_BOOL_type(il_default_variable_type)) |
|
1678 |
STAGE3_ERROR(symbol, symbol, "JMPCN operator requires il_default_variable to be of type BOOL."); |
|
1679 |
return NULL; |
|
1680 |
} |
|
1681 |
||
1682 |
/* Symbol class handled together with function call checks */ |
|
1683 |
/* any_identifier ASSIGN */ |
|
1684 |
// SYM_REF1(il_assign_operator_c, variable_name) |
|
1685 |
// void *visit_expression_type_c::visit(il_assign_operator_c *symbol, variable_name); |
|
1686 |
||
1687 |
/* Symbol class handled together with function call checks */ |
|
1688 |
/*| [NOT] any_identifier SENDTO */ |
|
1689 |
// SYM_REF2(il_assign_out_operator_c, option, variable_name) |
|
1690 |
// void *visit_expression_type_c::visit(il_assign_operator_c *symbol, option, variable_name); |
|
1691 |
||
1692 |
||
1693 |
||
1694 |
||
1695 |
||
1696 |
/***************************************/ |
|
1697 |
/* B.3 - Language ST (Structured Text) */ |
|
1698 |
/***************************************/ |
|
1699 |
/***********************/ |
|
1700 |
/* B 3.1 - Expressions */ |
|
1701 |
/***********************/ |
|
1702 |
||
1703 |
void *visit_expression_type_c::visit(or_expression_c *symbol) { |
|
1704 |
symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
|
1705 |
symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
|
257 | 1706 |
return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_BIT_compatible); |
204 | 1707 |
} |
1708 |
||
1709 |
||
1710 |
void *visit_expression_type_c::visit(xor_expression_c *symbol) { |
|
1711 |
symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
|
1712 |
symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
|
257 | 1713 |
return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_BIT_compatible); |
204 | 1714 |
} |
1715 |
||
1716 |
||
1717 |
void *visit_expression_type_c::visit(and_expression_c *symbol) { |
|
1718 |
symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
|
1719 |
symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
|
257 | 1720 |
return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_BIT_compatible); |
204 | 1721 |
} |
1722 |
||
1723 |
||
1724 |
void *visit_expression_type_c::visit(equ_expression_c *symbol) { |
|
1725 |
symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
|
1726 |
symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
|
257 | 1727 |
compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible); |
204 | 1728 |
return &search_expression_type_c::bool_type_name; |
1729 |
} |
|
1730 |
||
1731 |
||
1732 |
void *visit_expression_type_c::visit(notequ_expression_c *symbol) { |
|
1733 |
symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
|
1734 |
symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
|
257 | 1735 |
compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible); |
204 | 1736 |
return &search_expression_type_c::bool_type_name; |
1737 |
} |
|
1738 |
||
1739 |
||
1740 |
void *visit_expression_type_c::visit(lt_expression_c *symbol) { |
|
1741 |
symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
|
1742 |
symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
|
257 | 1743 |
compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible); |
204 | 1744 |
return &search_expression_type_c::bool_type_name; |
1745 |
} |
|
1746 |
||
1747 |
||
1748 |
void *visit_expression_type_c::visit(gt_expression_c *symbol) { |
|
1749 |
symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
|
1750 |
symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
|
257 | 1751 |
compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible); |
204 | 1752 |
return &search_expression_type_c::bool_type_name; |
1753 |
} |
|
1754 |
||
1755 |
||
1756 |
void *visit_expression_type_c::visit(le_expression_c *symbol) { |
|
1757 |
symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
|
1758 |
symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
|
257 | 1759 |
compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible); |
204 | 1760 |
return &search_expression_type_c::bool_type_name; |
1761 |
} |
|
1762 |
||
1763 |
||
1764 |
void *visit_expression_type_c::visit(ge_expression_c *symbol) { |
|
1765 |
symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
|
1766 |
symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
|
257 | 1767 |
compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_ELEMENTARY_compatible); |
204 | 1768 |
return &search_expression_type_c::bool_type_name; |
1769 |
} |
|
1770 |
||
1771 |
||
1772 |
void *visit_expression_type_c::visit(add_expression_c *symbol) { |
|
1773 |
symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
|
1774 |
symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
|
257 | 1775 |
|
1776 |
/* The following is already checked in compute_expression */ |
|
1777 |
/* |
|
1778 |
if (is_type(left_type, time_type_name_c) && is_type(right_type, time_type_name_c)) |
|
1779 |
return (void *)&time_type_name; |
|
1780 |
*/ |
|
1781 |
||
1782 |
if (is_type(left_type, tod_type_name_c) && is_type(right_type, time_type_name_c)) |
|
1783 |
return (void *)&tod_type_name; |
|
1784 |
if (is_type(left_type, safetod_type_name_c) && is_type(right_type, time_type_name_c)) |
|
1785 |
return (void *)&tod_type_name; |
|
1786 |
if (is_type(left_type, tod_type_name_c) && is_type(right_type, safetime_type_name_c)) |
|
1787 |
return (void *)&tod_type_name; |
|
1788 |
if (is_type(left_type, safetod_type_name_c) && is_type(right_type, safetime_type_name_c)) |
|
1789 |
return (void *)&safetod_type_name; |
|
1790 |
||
1791 |
if (is_type(left_type, dt_type_name_c) && is_type(right_type, time_type_name_c)) |
|
1792 |
return (void *)&dt_type_name; |
|
1793 |
if (is_type(left_type, safedt_type_name_c) && is_type(right_type, time_type_name_c)) |
|
1794 |
return (void *)&dt_type_name; |
|
1795 |
if (is_type(left_type, dt_type_name_c) && is_type(right_type, safetime_type_name_c)) |
|
1796 |
return (void *)&dt_type_name; |
|
1797 |
if (is_type(left_type, safedt_type_name_c) && is_type(right_type, safetime_type_name_c)) |
|
1798 |
return (void *)&safedt_type_name; |
|
1799 |
||
1800 |
return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_MAGNITUDE_compatible); |
|
204 | 1801 |
} |
1802 |
||
1803 |
||
1804 |
void *visit_expression_type_c::visit(sub_expression_c *symbol) { |
|
1805 |
symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
|
1806 |
symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
|
257 | 1807 |
|
1808 |
/* The following is already checked in compute_expression */ |
|
1809 |
/* |
|
1810 |
if (is_type(left_type, time_type_name_c) && is_type(right_type, time_type_name_c)) |
|
1811 |
return (void *)&time_type_name; |
|
1812 |
*/ |
|
1813 |
||
1814 |
if (is_type(left_type, tod_type_name_c) && is_type(right_type, time_type_name_c)) |
|
1815 |
return (void *)&tod_type_name; |
|
1816 |
if (is_type(left_type, safetod_type_name_c) && is_type(right_type, time_type_name_c)) |
|
1817 |
return (void *)&tod_type_name; |
|
1818 |
if (is_type(left_type, tod_type_name_c) && is_type(right_type, safetime_type_name_c)) |
|
1819 |
return (void *)&tod_type_name; |
|
1820 |
if (is_type(left_type, safetod_type_name_c) && is_type(right_type, safetime_type_name_c)) |
|
1821 |
return (void *)&safetod_type_name; |
|
1822 |
||
1823 |
if (is_type(left_type, dt_type_name_c) && is_type(right_type, time_type_name_c)) |
|
1824 |
return (void *)&dt_type_name; |
|
1825 |
if (is_type(left_type, safedt_type_name_c) && is_type(right_type, time_type_name_c)) |
|
1826 |
return (void *)&dt_type_name; |
|
1827 |
if (is_type(left_type, dt_type_name_c) && is_type(right_type, safetime_type_name_c)) |
|
1828 |
return (void *)&dt_type_name; |
|
1829 |
if (is_type(left_type, safedt_type_name_c) && is_type(right_type, safetime_type_name_c)) |
|
1830 |
return (void *)&safedt_type_name; |
|
1831 |
||
1832 |
if (is_type(left_type, tod_type_name_c) && is_type(right_type, tod_type_name_c)) |
|
1833 |
return (void *)&time_type_name; |
|
1834 |
if (is_type(left_type, safetod_type_name_c) && is_type(right_type, tod_type_name_c)) |
|
1835 |
return (void *)&time_type_name; |
|
1836 |
if (is_type(left_type, tod_type_name_c) && is_type(right_type, safetod_type_name_c)) |
|
1837 |
return (void *)&time_type_name; |
|
1838 |
if (is_type(left_type, safetod_type_name_c) && is_type(right_type, safetod_type_name_c)) |
|
1839 |
return (void *)&safetime_type_name; |
|
1840 |
||
1841 |
if (is_type(left_type, date_type_name_c) && is_type(right_type, date_type_name_c)) |
|
1842 |
return (void *)&time_type_name; |
|
1843 |
if (is_type(left_type, safedate_type_name_c) && is_type(right_type, date_type_name_c)) |
|
1844 |
return (void *)&time_type_name; |
|
1845 |
if (is_type(left_type, date_type_name_c) && is_type(right_type, safedate_type_name_c)) |
|
1846 |
return (void *)&time_type_name; |
|
1847 |
if (is_type(left_type, safedate_type_name_c) && is_type(right_type, safedate_type_name_c)) |
|
1848 |
return (void *)&safetime_type_name; |
|
1849 |
||
1850 |
if (is_type(left_type, dt_type_name_c) && is_type(right_type, dt_type_name_c)) |
|
1851 |
return (void *)&time_type_name; |
|
1852 |
if (is_type(left_type, safedt_type_name_c) && is_type(right_type, dt_type_name_c)) |
|
1853 |
return (void *)&time_type_name; |
|
1854 |
if (is_type(left_type, dt_type_name_c) && is_type(right_type, safedt_type_name_c)) |
|
1855 |
return (void *)&time_type_name; |
|
1856 |
if (is_type(left_type, safedt_type_name_c) && is_type(right_type, safedt_type_name_c)) |
|
1857 |
return (void *)&safetime_type_name; |
|
1858 |
||
1859 |
return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_MAGNITUDE_compatible); |
|
204 | 1860 |
} |
1861 |
||
1862 |
||
1863 |
void *visit_expression_type_c::visit(mul_expression_c *symbol) { |
|
1864 |
symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
|
1865 |
symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
|
257 | 1866 |
|
1867 |
if (is_type(left_type, time_type_name_c) && is_ANY_NUM_compatible(right_type)) |
|
1868 |
return (void *)&time_type_name; |
|
1869 |
if (is_type(left_type, safetime_type_name_c) && is_ANY_NUM_type(right_type)) |
|
1870 |
return (void *)&time_type_name; |
|
1871 |
if (is_type(left_type, safetime_type_name_c) && is_ANY_SAFENUM_type(right_type)) |
|
1872 |
return (void *)&safetime_type_name; |
|
1873 |
/* Since we have already checked for ANY_NUM_type and ANY_SAFENUM_type in the previous lines, |
|
1874 |
* this next line is really only to check for integers/reals of undefined type on 'right_type'... |
|
1875 |
*/ |
|
1876 |
if (is_type(left_type, safetime_type_name_c) && is_ANY_NUM_compatible(right_type)) |
|
1877 |
return (void *)&safetime_type_name; |
|
1878 |
||
1879 |
return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_NUM_compatible); |
|
204 | 1880 |
} |
1881 |
||
1882 |
||
1883 |
void *visit_expression_type_c::visit(div_expression_c *symbol) { |
|
1884 |
symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
|
1885 |
symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
|
257 | 1886 |
|
1887 |
if (is_type(left_type, time_type_name_c) && is_ANY_NUM_compatible(right_type)) |
|
1888 |
return (void *)&time_type_name; |
|
1889 |
if (is_type(left_type, safetime_type_name_c) && is_ANY_NUM_type(right_type)) |
|
1890 |
return (void *)&time_type_name; |
|
1891 |
if (is_type(left_type, safetime_type_name_c) && is_ANY_SAFENUM_type(right_type)) |
|
1892 |
return (void *)&safetime_type_name; |
|
1893 |
/* Since we have already checked for ANY_NUM_type and ANY_SAFENUM_type in the previous lines, |
|
1894 |
* this next line is really only to check for integers/reals of undefined type on 'right_type'... |
|
1895 |
*/ |
|
1896 |
if (is_type(left_type, safetime_type_name_c) && is_ANY_NUM_compatible(right_type)) |
|
1897 |
return (void *)&safetime_type_name; |
|
1898 |
||
1899 |
return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_NUM_compatible); |
|
204 | 1900 |
} |
1901 |
||
1902 |
||
1903 |
void *visit_expression_type_c::visit(mod_expression_c *symbol) { |
|
1904 |
symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
|
1905 |
symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
|
257 | 1906 |
return compute_expression(left_type, right_type, &visit_expression_type_c::is_ANY_INT_compatible); |
204 | 1907 |
} |
1908 |
||
1909 |
||
1910 |
void *visit_expression_type_c::visit(power_expression_c *symbol) { |
|
1911 |
symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
|
1912 |
symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
|
257 | 1913 |
if (!is_ANY_REAL_compatible(left_type)) |
204 | 1914 |
STAGE3_ERROR(symbol->l_exp, symbol->l_exp, "first operand of ** operator has invalid data type, should be of type ANY_REAL."); |
257 | 1915 |
if (!is_ANY_NUM_compatible(right_type)) |
204 | 1916 |
STAGE3_ERROR(symbol->r_exp, symbol->r_exp, "second operand of ** operator has invalid data type, should be of type ANY_NUM."); |
1917 |
||
1918 |
return (void *)left_type; |
|
1919 |
} |
|
1920 |
||
1921 |
||
1922 |
void *visit_expression_type_c::visit(neg_expression_c *symbol) { |
|
1923 |
symbol_c *exp_type = base_type((symbol_c *)symbol->exp->accept(*this)); |
|
257 | 1924 |
if (!is_ANY_MAGNITUDE_compatible(exp_type)) |
204 | 1925 |
STAGE3_ERROR(symbol, symbol, "operand of negate expression '-' has invalid data type, should be of type ANY_MAGNITUDE."); |
1926 |
||
1927 |
return exp_type; |
|
1928 |
} |
|
1929 |
||
1930 |
||
1931 |
void *visit_expression_type_c::visit(not_expression_c *symbol) { |
|
1932 |
symbol_c *type = base_type((symbol_c *)symbol->exp->accept(*this)); |
|
257 | 1933 |
return compute_expression(type, type, &visit_expression_type_c::is_ANY_BIT_compatible); |
204 | 1934 |
} |
1935 |
||
1936 |
||
1937 |
void *visit_expression_type_c::visit(function_invocation_c *symbol) { |
|
1938 |
function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name); |
|
1939 |
if (f_decl == function_symtable.end_value()) { |
|
1940 |
/* TODO: the following code is for standard library functions. We do not yet support this... */ |
|
1941 |
void *res = compute_standard_function_default(symbol); |
|
1942 |
if (res != NULL) return res; |
|
1943 |
ERROR; |
|
1944 |
} |
|
1945 |
||
1946 |
/* now check the semantics of the function call... */ |
|
1947 |
/* If the syntax parser is working correctly, exactly one of the |
|
1948 |
* following two symbols will be NULL, while the other is != NULL. |
|
1949 |
*/ |
|
1950 |
if (symbol-> formal_param_list != NULL) check_formal_call (symbol, f_decl); |
|
1951 |
if (symbol->nonformal_param_list != NULL) check_nonformal_call(symbol, f_decl); |
|
1952 |
||
1953 |
return base_type(f_decl->type_name); |
|
1954 |
} |
|
1955 |
||
1956 |
/********************/ |
|
1957 |
/* B 3.2 Statements */ |
|
1958 |
/********************/ |
|
1959 |
// SYM_LIST(statement_list_c) |
|
1960 |
/* The visitor of the base class search_visitor_c will handle calling each instruction in the list. |
|
1961 |
* We do not need to do anything here... |
|
1962 |
*/ |
|
1963 |
// void *visit_expression_type_c::visit(statement_list_c *symbol) |
|
1964 |
||
1965 |
||
1966 |
/*********************************/ |
|
1967 |
/* B 3.2.1 Assignment Statements */ |
|
1968 |
/*********************************/ |
|
1969 |
||
1970 |
void *visit_expression_type_c::visit(assignment_statement_c *symbol) { |
|
1971 |
symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
|
1972 |
symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
|
1973 |
||
257 | 1974 |
if (!is_valid_assignment(left_type, right_type)) { |
204 | 1975 |
STAGE3_ERROR(symbol, symbol, "data type mismatch in assignment statement!\n"); |
1976 |
} |
|
1977 |
return NULL; |
|
1978 |
} |
|
1979 |
||
1980 |
||
1981 |
||
1982 |
/*****************************************/ |
|
1983 |
/* B 3.2.2 Subprogram Control Statements */ |
|
1984 |
/*****************************************/ |
|
1985 |
||
1986 |
/* RETURN */ |
|
1987 |
// SYM_REF0(return_statement_c) |
|
1988 |
||
1989 |
||
1990 |
/* fb_name '(' [param_assignment_list] ')' */ |
|
1991 |
/* param_assignment_list -> may be NULL ! */ |
|
1992 |
// SYM_REF3(fb_invocation_c, fb_name, formal_param_list, nonformal_param_list) |
|
1993 |
void *visit_expression_type_c::visit(fb_invocation_c *symbol) { |
|
1994 |
symbol_c *fb_decl = search_varfb_instance_type->get_type(symbol->fb_name); |
|
1995 |
/* The following should never occur. The function block must be defined, |
|
1996 |
* and the FB type being called MUST be in the symtable... |
|
1997 |
* This was all already checked at stage 2! |
|
1998 |
*/ |
|
1999 |
if (NULL == fb_decl) ERROR; |
|
2000 |
||
2001 |
/* now check the semantics of the fb call... */ |
|
2002 |
/* If the syntax parser is working correctly, exactly one of the |
|
2003 |
* following two symbols will be NULL, while the other is != NULL. |
|
2004 |
*/ |
|
2005 |
if (symbol-> formal_param_list != NULL) check_formal_call (symbol, fb_decl); |
|
2006 |
if (symbol->nonformal_param_list != NULL) check_nonformal_call(symbol, fb_decl); |
|
2007 |
||
2008 |
return NULL; |
|
2009 |
} |
|
2010 |
||
2011 |
||
2012 |
#if 0 |
|
2013 |
/* helper symbol for fb_invocation */ |
|
2014 |
/* param_assignment_list ',' param_assignment */ |
|
2015 |
SYM_LIST(param_assignment_list_c) |
|
2016 |
||
2017 |
/* variable_name ASSIGN expression */ |
|
2018 |
SYM_REF2(input_variable_param_assignment_c, variable_name, expression) |
|
2019 |
||
2020 |
/* [NOT] variable_name '=>' variable */ |
|
2021 |
SYM_REF3(output_variable_param_assignment_c, not_param, variable_name, variable) |
|
2022 |
||
2023 |
/* helper CLASS for output_variable_param_assignment */ |
|
2024 |
SYM_REF0(not_paramassign_c) |
|
2025 |
#endif |
|
2026 |
||
2027 |
/********************************/ |
|
2028 |
/* B 3.2.3 Selection Statements */ |
|
2029 |
/********************************/ |
|
2030 |
||
2031 |
/* IF expression THEN statement_list elseif_statement_list ELSE statement_list END_IF */ |
|
2032 |
// SYM_REF4(if_statement_c, expression, statement_list, elseif_statement_list, else_statement_list) |
|
2033 |
void *visit_expression_type_c::visit(if_statement_c *symbol) { |
|
2034 |
symbol_c *expr_type = base_type((symbol_c*)symbol->expression->accept(*this)); |
|
260
70dfd493e639
Error messages (stage3) now better identify the location of the error.
Mario de Sousa <msousa@fe.up.pt>
parents:
259
diff
changeset
|
2035 |
if (!is_BOOL_type(expr_type)) STAGE3_ERROR(symbol->expression,symbol->expression,"IF conditional expression is not of boolean type."); |
204 | 2036 |
if (NULL != symbol->statement_list) |
2037 |
symbol->statement_list->accept(*this); |
|
2038 |
if (NULL != symbol->elseif_statement_list) |
|
2039 |
symbol->elseif_statement_list->accept(*this); |
|
2040 |
if (NULL != symbol->else_statement_list) |
|
2041 |
symbol->else_statement_list->accept(*this); |
|
2042 |
return NULL; |
|
2043 |
} |
|
2044 |
||
2045 |
/* helper symbol for if_statement */ |
|
2046 |
// SYM_LIST(elseif_statement_list_c) |
|
2047 |
// void *visit_expression_type_c::visit(elseif_statement_list_c *symbol) { } |
|
2048 |
||
2049 |
/* helper symbol for elseif_statement_list */ |
|
2050 |
/* ELSIF expression THEN statement_list */ |
|
2051 |
// SYM_REF2(elseif_statement_c, expression, statement_list) |
|
2052 |
void *visit_expression_type_c::visit(elseif_statement_c *symbol) { |
|
2053 |
symbol_c *elseif_expr_type = base_type((symbol_c*)symbol->expression->accept(*this)); |
|
260
70dfd493e639
Error messages (stage3) now better identify the location of the error.
Mario de Sousa <msousa@fe.up.pt>
parents:
259
diff
changeset
|
2054 |
if(!is_BOOL_type(elseif_expr_type)) STAGE3_ERROR(symbol->expression,symbol->expression,"ELSIF conditional expression is not of boolean type."); |
204 | 2055 |
if (NULL != symbol->statement_list) |
2056 |
symbol->statement_list->accept(*this); |
|
2057 |
return NULL; |
|
2058 |
} |
|
2059 |
||
2060 |
||
2061 |
/* CASE expression OF case_element_list ELSE statement_list END_CASE */ |
|
2062 |
// SYM_REF3(case_statement_c, expression, case_element_list, statement_list) |
|
2063 |
void *visit_expression_type_c::visit(case_statement_c *symbol) { |
|
2064 |
case_expression_type = base_type((symbol_c*)symbol->expression->accept(*this)); |
|
2065 |
if (NULL != case_expression_type) { |
|
2066 |
if (NULL != symbol->case_element_list) |
|
2067 |
symbol->case_element_list->accept(*this); |
|
2068 |
} |
|
2069 |
if (NULL != symbol->statement_list) |
|
2070 |
symbol->statement_list->accept(*this); |
|
2071 |
return NULL; |
|
2072 |
} |
|
2073 |
||
2074 |
#if 0 |
|
2075 |
/* helper symbol for case_statement */ |
|
2076 |
// SYM_LIST(case_element_list_c) |
|
2077 |
// void *visit_expression_type_c::visit(case_element_list_c *symbol); |
|
2078 |
||
2079 |
/* case_list ':' statement_list */ |
|
2080 |
// SYM_REF2(case_element_c, case_list, statement_list) |
|
2081 |
void *visit_expression_type_c::visit(case_element_c *symbol); |
|
2082 |
#endif |
|
2083 |
||
2084 |
// SYM_LIST(case_list_c) |
|
2085 |
void *visit_expression_type_c::visit(case_list_c *symbol) { |
|
2086 |
symbol_c *element_type; |
|
2087 |
for(int i = 0; i < symbol->n; i++) { |
|
2088 |
element_type = (symbol_c *)symbol->elements[i]->accept(*this); |
|
2089 |
if (NULL == element_type) { |
|
2090 |
STAGE3_ERROR(symbol->elements[i], symbol->elements[i], "Case list element has undefined data type."); |
|
2091 |
} else { |
|
2092 |
element_type = base_type(element_type); |
|
2093 |
if (NULL != element_type){ |
|
257 | 2094 |
/* The CASE value is only used for comparison (and not assingment), so we only check for compatibility! */ |
204 | 2095 |
if (!is_compatible_type(case_expression_type, element_type)) |
2096 |
STAGE3_ERROR(symbol->elements[i], symbol->elements[i], "Invalid data type of case list element."); |
|
2097 |
} |
|
2098 |
} |
|
2099 |
} |
|
2100 |
return NULL; |
|
2101 |
} |
|
2102 |
||
2103 |
/********************************/ |
|
2104 |
/* B 3.2.4 Iteration Statements */ |
|
2105 |
/********************************/ |
|
2106 |
||
2107 |
/* FOR control_variable ASSIGN expression TO expression [BY expression] DO statement_list END_FOR */ |
|
2108 |
// SYM_REF5(for_statement_c, control_variable, beg_expression, end_expression, by_expression, statement_list) |
|
2109 |
void *visit_expression_type_c::visit(for_statement_c *symbol) { |
|
2110 |
symbol_c *var_type = (symbol_c*)symbol->control_variable->accept(*this); |
|
2111 |
if (NULL == var_type) ERROR; |
|
2112 |
var_type = base_type(var_type); |
|
2113 |
if (NULL == var_type) ERROR; |
|
2114 |
// ASSIGN |
|
2115 |
symbol_c *beg_expr_type = base_type((symbol_c*)symbol->beg_expression->accept(*this)); |
|
257 | 2116 |
if (NULL != beg_expr_type) { |
2117 |
/* The BEG value is assigned to the variable, so we check for assignment validity! */ |
|
2118 |
if(!is_valid_assignment(var_type, beg_expr_type)) |
|
260
70dfd493e639
Error messages (stage3) now better identify the location of the error.
Mario de Sousa <msousa@fe.up.pt>
parents:
259
diff
changeset
|
2119 |
STAGE3_ERROR(symbol->beg_expression, symbol->beg_expression, "Data type mismatch between control variable and initial value."); |
204 | 2120 |
} |
2121 |
// TO |
|
2122 |
symbol_c *end_expr_type = base_type((symbol_c*)symbol->end_expression->accept(*this)); |
|
2123 |
if (NULL != end_expr_type) { |
|
257 | 2124 |
/* The TO value is only used for comparison, so we only check for compatibility! */ |
2125 |
if(!is_compatible_type(var_type, end_expr_type)) |
|
260
70dfd493e639
Error messages (stage3) now better identify the location of the error.
Mario de Sousa <msousa@fe.up.pt>
parents:
259
diff
changeset
|
2126 |
STAGE3_ERROR(symbol->end_expression, symbol->end_expression, "Data type mismatch between control variable and final value."); |
204 | 2127 |
} |
2128 |
// BY |
|
2129 |
if(symbol->by_expression != NULL) { |
|
2130 |
symbol_c *by_expr_type = base_type((symbol_c*)symbol->by_expression->accept(*this)); |
|
2131 |
if (NULL != end_expr_type) { |
|
257 | 2132 |
/* The BY value is used in an expression (add, sub, ...), so we only check for compatibility! */ |
2133 |
if(!is_compatible_type(var_type, by_expr_type)) |
|
260
70dfd493e639
Error messages (stage3) now better identify the location of the error.
Mario de Sousa <msousa@fe.up.pt>
parents:
259
diff
changeset
|
2134 |
STAGE3_ERROR(symbol->by_expression, symbol->by_expression, "Data type mismatch between control variable and BY value."); |
204 | 2135 |
} |
2136 |
} |
|
2137 |
// DO |
|
2138 |
if (NULL != symbol->statement_list) |
|
2139 |
symbol->statement_list->accept(*this); |
|
2140 |
return NULL; |
|
2141 |
} |
|
2142 |
||
2143 |
||
2144 |
/* WHILE expression DO statement_list END_WHILE */ |
|
2145 |
// SYM_REF2(while_statement_c, expression, statement_list) |
|
2146 |
void *visit_expression_type_c::visit(while_statement_c *symbol) { |
|
2147 |
symbol_c *expr_type = base_type((symbol_c*)symbol->expression->accept(*this)); |
|
2148 |
if (NULL != expr_type) { |
|
2149 |
if(!is_BOOL_type(expr_type)) |
|
260
70dfd493e639
Error messages (stage3) now better identify the location of the error.
Mario de Sousa <msousa@fe.up.pt>
parents:
259
diff
changeset
|
2150 |
STAGE3_ERROR(symbol->expression,symbol->expression,"WHILE conditional expression is not of boolean type."); |
204 | 2151 |
} |
2152 |
||
2153 |
if (NULL != symbol->statement_list) |
|
2154 |
symbol->statement_list->accept(*this); |
|
2155 |
return NULL; |
|
2156 |
} |
|
2157 |
||
2158 |
/* REPEAT statement_list UNTIL expression END_REPEAT */ |
|
2159 |
// SYM_REF2(repeat_statement_c, statement_list, expression) |
|
2160 |
void *visit_expression_type_c::visit(repeat_statement_c *symbol) { |
|
2161 |
if (NULL != symbol->statement_list) |
|
2162 |
symbol->statement_list->accept(*this); |
|
2163 |
||
2164 |
symbol_c *expr_type = base_type((symbol_c*)symbol->expression->accept(*this)); |
|
2165 |
if (NULL != expr_type) { |
|
2166 |
if(!is_BOOL_type(expr_type)) |
|
260
70dfd493e639
Error messages (stage3) now better identify the location of the error.
Mario de Sousa <msousa@fe.up.pt>
parents:
259
diff
changeset
|
2167 |
STAGE3_ERROR(symbol->expression,symbol->expression,"REPEAT conditional expression is not of boolean type."); |
204 | 2168 |
} |
2169 |
return NULL; |
|
2170 |
} |
|
2171 |
||
2172 |
/* EXIT */ |
|
2173 |
// SYM_REF0(exit_statement_c) |
|
2174 |
||
2175 |
||
2176 |