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;
|
|
70 |
|
|
71 |
symbol_c *il_default_variable_type;
|
|
72 |
symbol_c *il_operand_type;
|
|
73 |
|
|
74 |
|
|
75 |
public:
|
|
76 |
visit_expression_type_c(symbol_c *search_scope);
|
|
77 |
virtual ~visit_expression_type_c(void);
|
|
78 |
|
257
|
79 |
|
|
80 |
typedef struct {
|
|
81 |
symbol_c *value;
|
|
82 |
symbol_c *type;
|
|
83 |
} value_and_type_t;
|
|
84 |
|
204
|
85 |
/* A helper function... */
|
257
|
86 |
bool is_ANY_ELEMENTARY_type (symbol_c *type_symbol);
|
|
87 |
bool is_ANY_SAFEELEMENTARY_type (symbol_c *type_symbol);
|
|
88 |
bool is_ANY_ELEMENTARY_compatible (symbol_c *type_symbol);
|
|
89 |
|
|
90 |
bool is_ANY_MAGNITUDE_type (symbol_c *type_symbol);
|
|
91 |
bool is_ANY_SAFEMAGNITUDE_type (symbol_c *type_symbol);
|
|
92 |
bool is_ANY_MAGNITUDE_compatible (symbol_c *type_symbol);
|
|
93 |
|
|
94 |
bool is_ANY_DATE_type (symbol_c *type_symbol);
|
|
95 |
bool is_ANY_SAFEDATE_type (symbol_c *type_symbol);
|
|
96 |
bool is_ANY_DATE_compatible (symbol_c *type_symbol);
|
|
97 |
|
|
98 |
bool is_ANY_STRING_type (symbol_c *type_symbol);
|
|
99 |
bool is_ANY_SAFESTRING_type (symbol_c *type_symbol);
|
|
100 |
bool is_ANY_STRING_compatible (symbol_c *type_symbol);
|
|
101 |
|
|
102 |
bool is_ANY_INT_type (symbol_c *type_symbol);
|
|
103 |
bool is_ANY_SAFEINT_type (symbol_c *type_symbol);
|
|
104 |
bool is_ANY_INT_compatible (symbol_c *type_symbol);
|
|
105 |
|
|
106 |
bool is_ANY_REAL_type (symbol_c *type_symbol);
|
|
107 |
bool is_ANY_SAFEREAL_type (symbol_c *type_symbol);
|
|
108 |
bool is_ANY_REAL_compatible (symbol_c *type_symbol);
|
|
109 |
|
|
110 |
bool is_ANY_NUM_type (symbol_c *type_symbol);
|
|
111 |
bool is_ANY_SAFENUM_type (symbol_c *type_symbol);
|
|
112 |
bool is_ANY_NUM_compatible (symbol_c *type_symbol);
|
|
113 |
|
|
114 |
bool is_ANY_BIT_type (symbol_c *type_symbol);
|
|
115 |
bool is_ANY_SAFEBIT_type (symbol_c *type_symbol);
|
|
116 |
bool is_ANY_BIT_compatible (symbol_c *type_symbol);
|
|
117 |
|
|
118 |
bool is_BOOL_type (symbol_c *type_symbol);
|
|
119 |
bool is_SAFEBOOL_type (symbol_c *type_symbol);
|
|
120 |
bool is_ANY_BOOL_compatible (symbol_c *type_symbol);
|
|
121 |
|
|
122 |
bool is_nonneg_literal_integer_type (symbol_c *type_symbol);
|
|
123 |
bool is_literal_integer_type (symbol_c *type_symbol);
|
|
124 |
bool is_literal_real_type (symbol_c *type_symbol);
|
|
125 |
bool is_literal_bool_type (symbol_c *type_symbol);
|
204
|
126 |
|
|
127 |
/* Determine the common data type between two data types.
|
|
128 |
* If no common data type found, return NULL.
|
|
129 |
*
|
|
130 |
* If data types are identical, return the first (any would do...).
|
|
131 |
* If any of the datat types is a literal, we confirm that
|
|
132 |
* the literal uses less bits than the fixed size data type.
|
|
133 |
* e.g. BYTE and 1024 returns NULL
|
|
134 |
* BYTE and 255 returns BYTE
|
|
135 |
*
|
|
136 |
* If two literals, then return the literal that requires more bits...
|
|
137 |
*/
|
|
138 |
symbol_c *common_type__(symbol_c *first_type, symbol_c *second_type);
|
|
139 |
/* Determine the common data type between two data types.
|
|
140 |
* Unlike the common_type__() function, we stop the compiler with an ERROR
|
|
141 |
* if no common data type is found.
|
|
142 |
*/
|
|
143 |
symbol_c *common_type(symbol_c *first_type, symbol_c *second_type);
|
257
|
144 |
/* Return TRUE if the second (value) data type may be assigned to a variable of the first (variable) data type
|
|
145 |
* such as:
|
|
146 |
* var_type value_type
|
|
147 |
* BOOL BYTE#7 -> returns false
|
|
148 |
* INT INT#7 -> returns true
|
|
149 |
* INT 7 -> returns true
|
|
150 |
* REAL 7.89 -> returns true
|
|
151 |
* REAL 7 -> returns true
|
|
152 |
* INT 7.89 -> returns false
|
|
153 |
* SAFEBOOL BOOL#1 -> returns false !!!
|
|
154 |
* etc...
|
|
155 |
*
|
|
156 |
* NOTE: It is assumed that the var_type is the data type of an lvalue
|
|
157 |
*/
|
|
158 |
bool is_valid_assignment(symbol_c *var_type, symbol_c *value_type);
|
|
159 |
|
|
160 |
/* Return TRUE if there is a common data type, otherwise return FALSE
|
|
161 |
* i.e., return TRUE if both data types may be used simultaneously in an expression
|
|
162 |
* such as:
|
|
163 |
* BOOL#0 AND BYTE#7 -> returns false
|
|
164 |
* 0 AND BYTE#7 -> returns true
|
|
165 |
* INT#10 AND INT#7 -> returns true
|
|
166 |
* INT#10 AND 7 -> returns true
|
|
167 |
* REAL#34.3 AND 7.89 -> returns true
|
|
168 |
* REAL#34.3 AND 7 -> returns true
|
|
169 |
* INT#10 AND 7.89 -> returns false
|
|
170 |
* SAFEBOOL#0 AND BOOL#1 -> returns true !!!
|
|
171 |
* etc...
|
|
172 |
*/
|
204
|
173 |
bool is_compatible_type(symbol_c *first_type, symbol_c *second_type);
|
|
174 |
|
|
175 |
void compute_input_operatores(symbol_c *symbol, const char *input_operator);
|
|
176 |
void check_formal_parameter(symbol_c *call_param_name, symbol_c *call_param_type, symbol_c *f_decl);
|
|
177 |
|
|
178 |
/* check the semantics of a FB or Function non-formal call */
|
|
179 |
/* e.g. foo(1, 2, 3, 4); */
|
|
180 |
void check_nonformal_call(symbol_c *f_call, symbol_c *f_decl, bool use_il_defvar = false);
|
|
181 |
/* check the semantics of a FB or Function formal call */
|
|
182 |
/* e.g. foo(IN1 := 1, OUT1 =>x, EN := true); */
|
|
183 |
void check_formal_call(symbol_c *f_call, symbol_c *f_decl);
|
|
184 |
|
|
185 |
|
|
186 |
void *compute_standard_function_default(function_invocation_c *st_symbol, il_formal_funct_call_c *il_symbol);
|
|
187 |
void *compute_standard_function_il(il_function_call_c *symbol, symbol_c *param_data_type);
|
|
188 |
|
|
189 |
|
|
190 |
/* A helper function... */
|
|
191 |
typedef bool (visit_expression_type_c::*is_data_type_t)(symbol_c *type_symbol); /* a pointer to a function! */
|
257
|
192 |
// symbol_c *compute_boolean_expression(symbol_c *left_exp, symbol_c *right_exp, is_data_type_t is_data_type);
|
|
193 |
// symbol_c *compute_numeric_expression(symbol_c *left_exp, symbol_c *right_exp, is_data_type_t is_data_type);
|
|
194 |
symbol_c *compute_expression(symbol_c *left_exp, symbol_c *right_exp, is_data_type_t is_data_type);
|
204
|
195 |
|
|
196 |
/* a helper function... */
|
|
197 |
symbol_c *base_type(symbol_c *symbol);
|
|
198 |
|
|
199 |
/* a helper function... */
|
|
200 |
void *verify_null(symbol_c *symbol);
|
|
201 |
|
|
202 |
|
|
203 |
|
|
204 |
|
|
205 |
/*********************/
|
|
206 |
/* B 1.4 - Variables */
|
|
207 |
/*********************/
|
|
208 |
void *visit(symbolic_variable_c *symbol);
|
|
209 |
|
|
210 |
/********************************************/
|
|
211 |
/* B 1.4.1 - Directly Represented Variables */
|
|
212 |
/********************************************/
|
|
213 |
void *visit(direct_variable_c *symbol);
|
|
214 |
|
|
215 |
/*************************************/
|
|
216 |
/* B 1.4.2 - Multi-element variables */
|
|
217 |
/*************************************/
|
|
218 |
|
|
219 |
void *visit(array_variable_c *symbol);
|
|
220 |
void *visit(structured_variable_c *symbol);
|
|
221 |
|
|
222 |
/****************************************/
|
|
223 |
/* B.2 - Language IL (Instruction List) */
|
|
224 |
/****************************************/
|
|
225 |
/***********************************/
|
|
226 |
/* B 2.1 Instructions and Operands */
|
|
227 |
/***********************************/
|
|
228 |
// void *visit(instruction_list_c *symbol);
|
|
229 |
void *visit(il_simple_operation_c *symbol);
|
|
230 |
void *visit(il_function_call_c *symbol);
|
|
231 |
void *visit(il_expression_c *symbol);
|
|
232 |
// void *visit(il_jump_operation_c *symbol);
|
|
233 |
void *visit(il_fb_call_c *symbol);
|
|
234 |
void *visit(il_formal_funct_call_c *symbol);
|
|
235 |
/*
|
|
236 |
void *visit(il_operand_list_c *symbol);
|
|
237 |
void *visit(simple_instr_list_c *symbol);
|
|
238 |
void *visit(il_param_list_c *symbol);
|
|
239 |
void *visit(il_param_assignment_c *symbol);
|
|
240 |
void *visit(il_param_out_assignment_c *symbol);
|
|
241 |
*/
|
|
242 |
|
|
243 |
/*******************/
|
|
244 |
/* B 2.2 Operators */
|
|
245 |
/*******************/
|
|
246 |
void *visit(LD_operator_c *symbol);
|
|
247 |
void *visit(LDN_operator_c *symbol);
|
|
248 |
void *visit(ST_operator_c *symbol);
|
|
249 |
void *visit(STN_operator_c *symbol);
|
|
250 |
void *visit(NOT_operator_c *symbol);
|
|
251 |
void *visit(S_operator_c *symbol);
|
|
252 |
void *visit(R_operator_c *symbol);
|
|
253 |
void *visit(S1_operator_c *symbol);
|
|
254 |
void *visit(R1_operator_c *symbol);
|
|
255 |
void *visit(CLK_operator_c *symbol);
|
|
256 |
void *visit(CU_operator_c *symbol);
|
|
257 |
void *visit(CD_operator_c *symbol);
|
|
258 |
void *visit(PV_operator_c *symbol);
|
|
259 |
void *visit(IN_operator_c *symbol);
|
|
260 |
void *visit(PT_operator_c *symbol);
|
|
261 |
void *visit(AND_operator_c *symbol);
|
|
262 |
void *visit(OR_operator_c *symbol);
|
|
263 |
void *visit(XOR_operator_c *symbol);
|
|
264 |
void *visit(ANDN_operator_c *symbol);
|
|
265 |
void *visit(ORN_operator_c *symbol);
|
|
266 |
void *visit(XORN_operator_c *symbol);
|
|
267 |
void *visit(ADD_operator_c *symbol);
|
|
268 |
void *visit(SUB_operator_c *symbol);
|
|
269 |
void *visit(MUL_operator_c *symbol);
|
|
270 |
void *visit(DIV_operator_c *symbol);
|
|
271 |
void *visit(MOD_operator_c *symbol);
|
|
272 |
void *visit(GT_operator_c *symbol);
|
|
273 |
void *visit(GE_operator_c *symbol);
|
|
274 |
void *visit(EQ_operator_c *symbol);
|
|
275 |
void *visit(LT_operator_c *symbol);
|
|
276 |
void *visit(LE_operator_c *symbol);
|
|
277 |
void *visit(NE_operator_c *symbol);
|
|
278 |
void *visit(CAL_operator_c *symbol);
|
|
279 |
void *visit(CALC_operator_c *symbol);
|
|
280 |
void *visit(CALCN_operator_c *symbol);
|
|
281 |
void *visit(RET_operator_c *symbol);
|
|
282 |
void *visit(RETC_operator_c *symbol);
|
|
283 |
void *visit(RETCN_operator_c *symbol);
|
|
284 |
void *visit(JMP_operator_c *symbol);
|
|
285 |
void *visit(JMPC_operator_c *symbol);
|
|
286 |
void *visit(JMPCN_operator_c *symbol);
|
|
287 |
/* Symbol class handled together with function call checks */
|
|
288 |
// void *visit(il_assign_operator_c *symbol, variable_name);
|
|
289 |
/* Symbol class handled together with function call checks */
|
|
290 |
// void *visit(il_assign_operator_c *symbol, option, variable_name);
|
|
291 |
|
|
292 |
|
|
293 |
|
|
294 |
/***************************************/
|
|
295 |
/* B.3 - Language ST (Structured Text) */
|
|
296 |
/***************************************/
|
|
297 |
/***********************/
|
|
298 |
/* B 3.1 - Expressions */
|
|
299 |
/***********************/
|
|
300 |
void *visit(or_expression_c *symbol);
|
|
301 |
void *visit(xor_expression_c *symbol);
|
|
302 |
void *visit(and_expression_c *symbol);
|
|
303 |
void *visit(equ_expression_c *symbol);
|
|
304 |
void *visit(notequ_expression_c *symbol);
|
|
305 |
void *visit(lt_expression_c *symbol);
|
|
306 |
void *visit(gt_expression_c *symbol);
|
|
307 |
void *visit(le_expression_c *symbol);
|
|
308 |
void *visit(ge_expression_c *symbol);
|
|
309 |
void *visit(add_expression_c *symbol);
|
|
310 |
void *visit(sub_expression_c *symbol);
|
|
311 |
void *visit(mul_expression_c *symbol);
|
|
312 |
void *visit(div_expression_c *symbol);
|
|
313 |
void *visit(mod_expression_c *symbol);
|
|
314 |
void *visit(power_expression_c *symbol);
|
|
315 |
void *visit(neg_expression_c *symbol);
|
|
316 |
void *visit(not_expression_c *symbol);
|
|
317 |
void *visit(function_invocation_c *symbol);
|
|
318 |
|
|
319 |
/*********************************/
|
|
320 |
/* B 3.2.1 Assignment Statements */
|
|
321 |
/*********************************/
|
|
322 |
void *visit(assignment_statement_c *symbol);
|
|
323 |
|
|
324 |
/*****************************************/
|
|
325 |
/* B 3.2.2 Subprogram Control Statements */
|
|
326 |
/*****************************************/
|
|
327 |
void *visit(fb_invocation_c *symbol);
|
|
328 |
|
|
329 |
/********************************/
|
|
330 |
/* B 3.2.3 Selection Statements */
|
|
331 |
/********************************/
|
|
332 |
|
|
333 |
void *visit(if_statement_c *symbol);
|
|
334 |
// void *visit(elseif_statement_list_c *symbol);
|
|
335 |
void *visit(elseif_statement_c *symbol);
|
|
336 |
void *visit(case_statement_c *symbol);
|
|
337 |
// void *visit(case_element_list_c *symbol);
|
|
338 |
// void *visit(case_element_c *symbol);
|
|
339 |
void *visit(case_list_c *symbol);
|
|
340 |
|
|
341 |
/********************************/
|
|
342 |
/* B 3.2.4 Iteration Statements */
|
|
343 |
/********************************/
|
|
344 |
|
|
345 |
void *visit(for_statement_c *symbol);
|
|
346 |
void *visit(while_statement_c *symbol);
|
|
347 |
void *visit(repeat_statement_c *symbol);
|
|
348 |
|
|
349 |
|
|
350 |
//TODO: delete this functions. Why are they needed?
|
|
351 |
void *visit(program_declaration_c *symbol);
|
|
352 |
void *visit(function_declaration_c *symbol);
|
|
353 |
void *visit(function_block_declaration_c *symbol);
|
|
354 |
|
|
355 |
}; // visit_expression_type_c
|
|
356 |
|