author | Mario de Sousa <msousa@fe.up.pt> |
Thu, 31 Mar 2011 21:06:36 +0100 | |
changeset 262 | 197ba42d78b2 |
parent 257 | 90782e241346 |
child 266 | 2f6d8866ec8d |
permissions | -rwxr-xr-x |
204 | 1 |
/* |
2 |
* (c) 2003 Mario de Sousa |
|
3 |
* |
|
4 |
* Offered to the public under the terms of the GNU General Public License |
|
5 |
* as published by the Free Software Foundation; either version 2 of the |
|
6 |
* License, or (at your option) any later version. |
|
7 |
* |
|
8 |
* This program is distributed in the hope that it will be useful, but |
|
9 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
|
11 |
* Public License for more details. |
|
12 |
* |
|
13 |
* This code is made available on the understanding that it will not be |
|
14 |
* used in safety-critical situations without a full and competent review. |
|
15 |
*/ |
|
16 |
||
17 |
/* |
|
18 |
* An IEC 61131-3 IL and ST compiler. |
|
19 |
* |
|
20 |
* Based on the |
|
21 |
* FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) |
|
22 |
* |
|
23 |
*/ |
|
24 |
||
25 |
/* Verify whether the semantic rules of data type compatibility are being followed. |
|
26 |
* |
|
27 |
* For example: |
|
28 |
*/ |
|
29 |
||
30 |
#include "../absyntax_utils/absyntax_utils.hh" |
|
31 |
||
32 |
class visit_expression_type_c: public search_constant_type_c { |
|
33 |
||
34 |
private: |
|
35 |
search_varfb_instance_type_c *search_varfb_instance_type; |
|
36 |
search_base_type_c search_base_type; |
|
37 |
||
38 |
/* When calling a function block, we must first find it's type, |
|
39 |
* by searching through the declarations of the variables currently |
|
40 |
* in scope. |
|
41 |
* This class does just that... |
|
42 |
* A new bject instance is instantiated whenever we start checking semantics |
|
43 |
* for a function block type declaration, or a program declaration. |
|
44 |
* This object instance will then later be called while the |
|
45 |
* function block's or the program's body is being handled. |
|
46 |
* |
|
47 |
* Note that functions cannot contain calls to function blocks, |
|
48 |
* so we do not create an object instance when handling |
|
49 |
* a function declaration. |
|
50 |
*/ |
|
51 |
// search_var_instance_decl_c *search_var_instance_decl; |
|
52 |
||
53 |
/* This variable was created to pass information from |
|
54 |
* visit_expression_type_c::visit(case_statement_c *symbol) function to |
|
55 |
* visit_expression_type_c::visit(case_list_c *symbol) function. |
|
56 |
*/ |
|
57 |
symbol_c *case_expression_type; |
|
58 |
||
59 |
/* In IL code, once we find a type mismatch error, it is best to |
|
60 |
* ignore any further errors until the end of the logicl operation, |
|
61 |
* i.e. until the next LD. |
|
62 |
* However, we cannot clear the il_error flag on all LD operations, |
|
63 |
* as these may also be used within parenthesis. LD operations |
|
64 |
* within parenthesis may not clear the error flag. |
|
65 |
* We therefore need a counter to know how deep inside a parenthesis |
|
66 |
* structure we are. |
|
67 |
*/ |
|
68 |
int il_parenthesis_level; |
|
69 |
bool il_error; |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
257
diff
changeset
|
70 |
bool error_found; |
204 | 71 |
|
72 |
symbol_c *il_default_variable_type; |
|
73 |
symbol_c *il_operand_type; |
|
74 |
||
75 |
||
76 |
public: |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
257
diff
changeset
|
77 |
visit_expression_type_c(symbol_c *ignore); |
204 | 78 |
virtual ~visit_expression_type_c(void); |
79 |
||
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
257
diff
changeset
|
80 |
bool get_error_found(void); |
257 | 81 |
|
82 |
typedef struct { |
|
83 |
symbol_c *value; |
|
84 |
symbol_c *type; |
|
85 |
} value_and_type_t; |
|
86 |
||
204 | 87 |
/* A helper function... */ |
257 | 88 |
bool is_ANY_ELEMENTARY_type (symbol_c *type_symbol); |
89 |
bool is_ANY_SAFEELEMENTARY_type (symbol_c *type_symbol); |
|
90 |
bool is_ANY_ELEMENTARY_compatible (symbol_c *type_symbol); |
|
91 |
||
92 |
bool is_ANY_MAGNITUDE_type (symbol_c *type_symbol); |
|
93 |
bool is_ANY_SAFEMAGNITUDE_type (symbol_c *type_symbol); |
|
94 |
bool is_ANY_MAGNITUDE_compatible (symbol_c *type_symbol); |
|
95 |
||
96 |
bool is_ANY_DATE_type (symbol_c *type_symbol); |
|
97 |
bool is_ANY_SAFEDATE_type (symbol_c *type_symbol); |
|
98 |
bool is_ANY_DATE_compatible (symbol_c *type_symbol); |
|
99 |
||
100 |
bool is_ANY_STRING_type (symbol_c *type_symbol); |
|
101 |
bool is_ANY_SAFESTRING_type (symbol_c *type_symbol); |
|
102 |
bool is_ANY_STRING_compatible (symbol_c *type_symbol); |
|
103 |
||
104 |
bool is_ANY_INT_type (symbol_c *type_symbol); |
|
105 |
bool is_ANY_SAFEINT_type (symbol_c *type_symbol); |
|
106 |
bool is_ANY_INT_compatible (symbol_c *type_symbol); |
|
107 |
||
108 |
bool is_ANY_REAL_type (symbol_c *type_symbol); |
|
109 |
bool is_ANY_SAFEREAL_type (symbol_c *type_symbol); |
|
110 |
bool is_ANY_REAL_compatible (symbol_c *type_symbol); |
|
111 |
||
112 |
bool is_ANY_NUM_type (symbol_c *type_symbol); |
|
113 |
bool is_ANY_SAFENUM_type (symbol_c *type_symbol); |
|
114 |
bool is_ANY_NUM_compatible (symbol_c *type_symbol); |
|
115 |
||
116 |
bool is_ANY_BIT_type (symbol_c *type_symbol); |
|
117 |
bool is_ANY_SAFEBIT_type (symbol_c *type_symbol); |
|
118 |
bool is_ANY_BIT_compatible (symbol_c *type_symbol); |
|
119 |
||
120 |
bool is_BOOL_type (symbol_c *type_symbol); |
|
121 |
bool is_SAFEBOOL_type (symbol_c *type_symbol); |
|
122 |
bool is_ANY_BOOL_compatible (symbol_c *type_symbol); |
|
123 |
||
124 |
bool is_nonneg_literal_integer_type (symbol_c *type_symbol); |
|
125 |
bool is_literal_integer_type (symbol_c *type_symbol); |
|
126 |
bool is_literal_real_type (symbol_c *type_symbol); |
|
127 |
bool is_literal_bool_type (symbol_c *type_symbol); |
|
204 | 128 |
|
129 |
/* Determine the common data type between two data types. |
|
130 |
* If no common data type found, return NULL. |
|
131 |
* |
|
132 |
* If data types are identical, return the first (any would do...). |
|
133 |
* If any of the datat types is a literal, we confirm that |
|
134 |
* the literal uses less bits than the fixed size data type. |
|
135 |
* e.g. BYTE and 1024 returns NULL |
|
136 |
* BYTE and 255 returns BYTE |
|
137 |
* |
|
138 |
* If two literals, then return the literal that requires more bits... |
|
139 |
*/ |
|
140 |
symbol_c *common_type__(symbol_c *first_type, symbol_c *second_type); |
|
141 |
/* Determine the common data type between two data types. |
|
142 |
* Unlike the common_type__() function, we stop the compiler with an ERROR |
|
143 |
* if no common data type is found. |
|
144 |
*/ |
|
145 |
symbol_c *common_type(symbol_c *first_type, symbol_c *second_type); |
|
257 | 146 |
/* Return TRUE if the second (value) data type may be assigned to a variable of the first (variable) data type |
147 |
* such as: |
|
148 |
* var_type value_type |
|
149 |
* BOOL BYTE#7 -> returns false |
|
150 |
* INT INT#7 -> returns true |
|
151 |
* INT 7 -> returns true |
|
152 |
* REAL 7.89 -> returns true |
|
153 |
* REAL 7 -> returns true |
|
154 |
* INT 7.89 -> returns false |
|
155 |
* SAFEBOOL BOOL#1 -> returns false !!! |
|
156 |
* etc... |
|
157 |
* |
|
158 |
* NOTE: It is assumed that the var_type is the data type of an lvalue |
|
159 |
*/ |
|
160 |
bool is_valid_assignment(symbol_c *var_type, symbol_c *value_type); |
|
161 |
||
162 |
/* Return TRUE if there is a common data type, otherwise return FALSE |
|
163 |
* i.e., return TRUE if both data types may be used simultaneously in an expression |
|
164 |
* such as: |
|
165 |
* BOOL#0 AND BYTE#7 -> returns false |
|
166 |
* 0 AND BYTE#7 -> returns true |
|
167 |
* INT#10 AND INT#7 -> returns true |
|
168 |
* INT#10 AND 7 -> returns true |
|
169 |
* REAL#34.3 AND 7.89 -> returns true |
|
170 |
* REAL#34.3 AND 7 -> returns true |
|
171 |
* INT#10 AND 7.89 -> returns false |
|
172 |
* SAFEBOOL#0 AND BOOL#1 -> returns true !!! |
|
173 |
* etc... |
|
174 |
*/ |
|
204 | 175 |
bool is_compatible_type(symbol_c *first_type, symbol_c *second_type); |
176 |
||
177 |
void compute_input_operatores(symbol_c *symbol, const char *input_operator); |
|
178 |
void check_formal_parameter(symbol_c *call_param_name, symbol_c *call_param_type, symbol_c *f_decl); |
|
179 |
||
180 |
/* check the semantics of a FB or Function non-formal call */ |
|
181 |
/* e.g. foo(1, 2, 3, 4); */ |
|
182 |
void check_nonformal_call(symbol_c *f_call, symbol_c *f_decl, bool use_il_defvar = false); |
|
183 |
/* check the semantics of a FB or Function formal call */ |
|
184 |
/* e.g. foo(IN1 := 1, OUT1 =>x, EN := true); */ |
|
185 |
void check_formal_call(symbol_c *f_call, symbol_c *f_decl); |
|
186 |
||
187 |
||
188 |
void *compute_standard_function_default(function_invocation_c *st_symbol, il_formal_funct_call_c *il_symbol); |
|
189 |
void *compute_standard_function_il(il_function_call_c *symbol, symbol_c *param_data_type); |
|
190 |
||
191 |
||
192 |
/* A helper function... */ |
|
193 |
typedef bool (visit_expression_type_c::*is_data_type_t)(symbol_c *type_symbol); /* a pointer to a function! */ |
|
257 | 194 |
// symbol_c *compute_boolean_expression(symbol_c *left_exp, symbol_c *right_exp, is_data_type_t is_data_type); |
195 |
// symbol_c *compute_numeric_expression(symbol_c *left_exp, symbol_c *right_exp, is_data_type_t is_data_type); |
|
262
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
257
diff
changeset
|
196 |
// symbol_c *compute_expression(symbol_c *left_exp, symbol_c *right_exp, 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:
257
diff
changeset
|
197 |
symbol_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:
257
diff
changeset
|
198 |
symbol_c *left_expr=NULL, symbol_c *right_expr=NULL); |
197ba42d78b2
Do not crash when checking semantics of undeclared symbolic_variable.
Mario de Sousa <msousa@fe.up.pt>
parents:
257
diff
changeset
|
199 |
|
204 | 200 |
|
201 |
/* a helper function... */ |
|
202 |
symbol_c *base_type(symbol_c *symbol); |
|
203 |
||
204 |
/* a helper function... */ |
|
205 |
void *verify_null(symbol_c *symbol); |
|
206 |
||
207 |
/*********************/ |
|
208 |
/* B 1.4 - Variables */ |
|
209 |
/*********************/ |
|
210 |
void *visit(symbolic_variable_c *symbol); |
|
211 |
||
212 |
/********************************************/ |
|
213 |
/* B 1.4.1 - Directly Represented Variables */ |
|
214 |
/********************************************/ |
|
215 |
void *visit(direct_variable_c *symbol); |
|
216 |
||
217 |
/*************************************/ |
|
218 |
/* B 1.4.2 - Multi-element variables */ |
|
219 |
/*************************************/ |
|
220 |
||
221 |
void *visit(array_variable_c *symbol); |
|
222 |
void *visit(structured_variable_c *symbol); |
|
223 |
||
224 |
/****************************************/ |
|
225 |
/* B.2 - Language IL (Instruction List) */ |
|
226 |
/****************************************/ |
|
227 |
/***********************************/ |
|
228 |
/* B 2.1 Instructions and Operands */ |
|
229 |
/***********************************/ |
|
230 |
// void *visit(instruction_list_c *symbol); |
|
231 |
void *visit(il_simple_operation_c *symbol); |
|
232 |
void *visit(il_function_call_c *symbol); |
|
233 |
void *visit(il_expression_c *symbol); |
|
234 |
// void *visit(il_jump_operation_c *symbol); |
|
235 |
void *visit(il_fb_call_c *symbol); |
|
236 |
void *visit(il_formal_funct_call_c *symbol); |
|
237 |
/* |
|
238 |
void *visit(il_operand_list_c *symbol); |
|
239 |
void *visit(simple_instr_list_c *symbol); |
|
240 |
void *visit(il_param_list_c *symbol); |
|
241 |
void *visit(il_param_assignment_c *symbol); |
|
242 |
void *visit(il_param_out_assignment_c *symbol); |
|
243 |
*/ |
|
244 |
||
245 |
/*******************/ |
|
246 |
/* B 2.2 Operators */ |
|
247 |
/*******************/ |
|
248 |
void *visit(LD_operator_c *symbol); |
|
249 |
void *visit(LDN_operator_c *symbol); |
|
250 |
void *visit(ST_operator_c *symbol); |
|
251 |
void *visit(STN_operator_c *symbol); |
|
252 |
void *visit(NOT_operator_c *symbol); |
|
253 |
void *visit(S_operator_c *symbol); |
|
254 |
void *visit(R_operator_c *symbol); |
|
255 |
void *visit(S1_operator_c *symbol); |
|
256 |
void *visit(R1_operator_c *symbol); |
|
257 |
void *visit(CLK_operator_c *symbol); |
|
258 |
void *visit(CU_operator_c *symbol); |
|
259 |
void *visit(CD_operator_c *symbol); |
|
260 |
void *visit(PV_operator_c *symbol); |
|
261 |
void *visit(IN_operator_c *symbol); |
|
262 |
void *visit(PT_operator_c *symbol); |
|
263 |
void *visit(AND_operator_c *symbol); |
|
264 |
void *visit(OR_operator_c *symbol); |
|
265 |
void *visit(XOR_operator_c *symbol); |
|
266 |
void *visit(ANDN_operator_c *symbol); |
|
267 |
void *visit(ORN_operator_c *symbol); |
|
268 |
void *visit(XORN_operator_c *symbol); |
|
269 |
void *visit(ADD_operator_c *symbol); |
|
270 |
void *visit(SUB_operator_c *symbol); |
|
271 |
void *visit(MUL_operator_c *symbol); |
|
272 |
void *visit(DIV_operator_c *symbol); |
|
273 |
void *visit(MOD_operator_c *symbol); |
|
274 |
void *visit(GT_operator_c *symbol); |
|
275 |
void *visit(GE_operator_c *symbol); |
|
276 |
void *visit(EQ_operator_c *symbol); |
|
277 |
void *visit(LT_operator_c *symbol); |
|
278 |
void *visit(LE_operator_c *symbol); |
|
279 |
void *visit(NE_operator_c *symbol); |
|
280 |
void *visit(CAL_operator_c *symbol); |
|
281 |
void *visit(CALC_operator_c *symbol); |
|
282 |
void *visit(CALCN_operator_c *symbol); |
|
283 |
void *visit(RET_operator_c *symbol); |
|
284 |
void *visit(RETC_operator_c *symbol); |
|
285 |
void *visit(RETCN_operator_c *symbol); |
|
286 |
void *visit(JMP_operator_c *symbol); |
|
287 |
void *visit(JMPC_operator_c *symbol); |
|
288 |
void *visit(JMPCN_operator_c *symbol); |
|
289 |
/* Symbol class handled together with function call checks */ |
|
290 |
// void *visit(il_assign_operator_c *symbol, variable_name); |
|
291 |
/* Symbol class handled together with function call checks */ |
|
292 |
// void *visit(il_assign_operator_c *symbol, option, variable_name); |
|
293 |
||
294 |
||
295 |
||
296 |
/***************************************/ |
|
297 |
/* B.3 - Language ST (Structured Text) */ |
|
298 |
/***************************************/ |
|
299 |
/***********************/ |
|
300 |
/* B 3.1 - Expressions */ |
|
301 |
/***********************/ |
|
302 |
void *visit(or_expression_c *symbol); |
|
303 |
void *visit(xor_expression_c *symbol); |
|
304 |
void *visit(and_expression_c *symbol); |
|
305 |
void *visit(equ_expression_c *symbol); |
|
306 |
void *visit(notequ_expression_c *symbol); |
|
307 |
void *visit(lt_expression_c *symbol); |
|
308 |
void *visit(gt_expression_c *symbol); |
|
309 |
void *visit(le_expression_c *symbol); |
|
310 |
void *visit(ge_expression_c *symbol); |
|
311 |
void *visit(add_expression_c *symbol); |
|
312 |
void *visit(sub_expression_c *symbol); |
|
313 |
void *visit(mul_expression_c *symbol); |
|
314 |
void *visit(div_expression_c *symbol); |
|
315 |
void *visit(mod_expression_c *symbol); |
|
316 |
void *visit(power_expression_c *symbol); |
|
317 |
void *visit(neg_expression_c *symbol); |
|
318 |
void *visit(not_expression_c *symbol); |
|
319 |
void *visit(function_invocation_c *symbol); |
|
320 |
||
321 |
/*********************************/ |
|
322 |
/* B 3.2.1 Assignment Statements */ |
|
323 |
/*********************************/ |
|
324 |
void *visit(assignment_statement_c *symbol); |
|
325 |
||
326 |
/*****************************************/ |
|
327 |
/* B 3.2.2 Subprogram Control Statements */ |
|
328 |
/*****************************************/ |
|
329 |
void *visit(fb_invocation_c *symbol); |
|
330 |
||
331 |
/********************************/ |
|
332 |
/* B 3.2.3 Selection Statements */ |
|
333 |
/********************************/ |
|
334 |
||
335 |
void *visit(if_statement_c *symbol); |
|
336 |
// void *visit(elseif_statement_list_c *symbol); |
|
337 |
void *visit(elseif_statement_c *symbol); |
|
338 |
void *visit(case_statement_c *symbol); |
|
339 |
// void *visit(case_element_list_c *symbol); |
|
340 |
// void *visit(case_element_c *symbol); |
|
341 |
void *visit(case_list_c *symbol); |
|
342 |
||
343 |
/********************************/ |
|
344 |
/* B 3.2.4 Iteration Statements */ |
|
345 |
/********************************/ |
|
346 |
||
347 |
void *visit(for_statement_c *symbol); |
|
348 |
void *visit(while_statement_c *symbol); |
|
349 |
void *visit(repeat_statement_c *symbol); |
|
350 |
||
351 |
||
352 |
//TODO: delete this functions. Why are they needed? |
|
353 |
void *visit(program_declaration_c *symbol); |
|
354 |
void *visit(function_declaration_c *symbol); |
|
355 |
void *visit(function_block_declaration_c *symbol); |
|
356 |
||
357 |
}; // visit_expression_type_c |
|
358 |