|
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 |
|
79 /* A helper function... */ |
|
80 bool is_ANY_ELEMENTARY_type(symbol_c *type_symbol); |
|
81 bool is_ANY_MAGNITUDE_type(symbol_c *type_symbol); |
|
82 bool is_ANY_DATE_type(symbol_c *type_symbol); |
|
83 bool is_ANY_STRING_type(symbol_c *type_symbol); |
|
84 bool is_ANY_INT_type(symbol_c *type_symbol); |
|
85 bool is_ANY_REAL_type(symbol_c *type_symbol); |
|
86 bool is_ANY_NUM_type(symbol_c *type_symbol); |
|
87 bool is_ANY_BIT_type(symbol_c *type_symbol); |
|
88 bool is_BOOL_type(symbol_c *type_symbol); |
|
89 |
|
90 bool is_literal_integer_type(symbol_c *type_symbol); |
|
91 bool is_literal_real_type(symbol_c *type_symbol); |
|
92 bool is_literal_bool_type(symbol_c *type_symbol); |
|
93 |
|
94 /* Determine the common data type between two data types. |
|
95 * If no common data type found, return NULL. |
|
96 * |
|
97 * If data types are identical, return the first (any would do...). |
|
98 * If any of the datat types is a literal, we confirm that |
|
99 * the literal uses less bits than the fixed size data type. |
|
100 * e.g. BYTE and 1024 returns NULL |
|
101 * BYTE and 255 returns BYTE |
|
102 * |
|
103 * If two literals, then return the literal that requires more bits... |
|
104 */ |
|
105 symbol_c *common_type__(symbol_c *first_type, symbol_c *second_type); |
|
106 /* Determine the common data type between two data types. |
|
107 * Unlike the common_type__() function, we stop the compiler with an ERROR |
|
108 * if no common data type is found. |
|
109 */ |
|
110 symbol_c *common_type(symbol_c *first_type, symbol_c *second_type); |
|
111 /* Return TRUE if there is a common data type, otherwise return FALSE */ |
|
112 bool is_compatible_type(symbol_c *first_type, symbol_c *second_type); |
|
113 |
|
114 void compute_input_operatores(symbol_c *symbol, const char *input_operator); |
|
115 void check_formal_parameter(symbol_c *call_param_name, symbol_c *call_param_type, symbol_c *f_decl); |
|
116 |
|
117 /* check the semantics of a FB or Function non-formal call */ |
|
118 /* e.g. foo(1, 2, 3, 4); */ |
|
119 void check_nonformal_call(symbol_c *f_call, symbol_c *f_decl, bool use_il_defvar = false); |
|
120 /* check the semantics of a FB or Function formal call */ |
|
121 /* e.g. foo(IN1 := 1, OUT1 =>x, EN := true); */ |
|
122 void check_formal_call(symbol_c *f_call, symbol_c *f_decl); |
|
123 |
|
124 |
|
125 void *compute_standard_function_default(function_invocation_c *st_symbol, il_formal_funct_call_c *il_symbol); |
|
126 void *compute_standard_function_il(il_function_call_c *symbol, symbol_c *param_data_type); |
|
127 |
|
128 |
|
129 /* A helper function... */ |
|
130 typedef bool (visit_expression_type_c::*is_data_type_t)(symbol_c *type_symbol); /* a pointer to a function! */ |
|
131 symbol_c *compute_boolean_expression(symbol_c *left_exp, symbol_c *right_exp, is_data_type_t is_data_type); |
|
132 symbol_c *compute_numeric_expression(symbol_c *left_exp, symbol_c *right_exp, is_data_type_t is_data_type); |
|
133 |
|
134 /* a helper function... */ |
|
135 symbol_c *base_type(symbol_c *symbol); |
|
136 |
|
137 /* a helper function... */ |
|
138 void *verify_null(symbol_c *symbol); |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 /*********************/ |
|
144 /* B 1.4 - Variables */ |
|
145 /*********************/ |
|
146 void *visit(symbolic_variable_c *symbol); |
|
147 |
|
148 /********************************************/ |
|
149 /* B 1.4.1 - Directly Represented Variables */ |
|
150 /********************************************/ |
|
151 void *visit(direct_variable_c *symbol); |
|
152 |
|
153 /*************************************/ |
|
154 /* B 1.4.2 - Multi-element variables */ |
|
155 /*************************************/ |
|
156 |
|
157 void *visit(array_variable_c *symbol); |
|
158 void *visit(structured_variable_c *symbol); |
|
159 |
|
160 /****************************************/ |
|
161 /* B.2 - Language IL (Instruction List) */ |
|
162 /****************************************/ |
|
163 /***********************************/ |
|
164 /* B 2.1 Instructions and Operands */ |
|
165 /***********************************/ |
|
166 // void *visit(instruction_list_c *symbol); |
|
167 void *visit(il_simple_operation_c *symbol); |
|
168 void *visit(il_function_call_c *symbol); |
|
169 void *visit(il_expression_c *symbol); |
|
170 // void *visit(il_jump_operation_c *symbol); |
|
171 void *visit(il_fb_call_c *symbol); |
|
172 void *visit(il_formal_funct_call_c *symbol); |
|
173 /* |
|
174 void *visit(il_operand_list_c *symbol); |
|
175 void *visit(simple_instr_list_c *symbol); |
|
176 void *visit(il_param_list_c *symbol); |
|
177 void *visit(il_param_assignment_c *symbol); |
|
178 void *visit(il_param_out_assignment_c *symbol); |
|
179 */ |
|
180 |
|
181 /*******************/ |
|
182 /* B 2.2 Operators */ |
|
183 /*******************/ |
|
184 void *visit(LD_operator_c *symbol); |
|
185 void *visit(LDN_operator_c *symbol); |
|
186 void *visit(ST_operator_c *symbol); |
|
187 void *visit(STN_operator_c *symbol); |
|
188 void *visit(NOT_operator_c *symbol); |
|
189 void *visit(S_operator_c *symbol); |
|
190 void *visit(R_operator_c *symbol); |
|
191 void *visit(S1_operator_c *symbol); |
|
192 void *visit(R1_operator_c *symbol); |
|
193 void *visit(CLK_operator_c *symbol); |
|
194 void *visit(CU_operator_c *symbol); |
|
195 void *visit(CD_operator_c *symbol); |
|
196 void *visit(PV_operator_c *symbol); |
|
197 void *visit(IN_operator_c *symbol); |
|
198 void *visit(PT_operator_c *symbol); |
|
199 void *visit(AND_operator_c *symbol); |
|
200 void *visit(OR_operator_c *symbol); |
|
201 void *visit(XOR_operator_c *symbol); |
|
202 void *visit(ANDN_operator_c *symbol); |
|
203 void *visit(ORN_operator_c *symbol); |
|
204 void *visit(XORN_operator_c *symbol); |
|
205 void *visit(ADD_operator_c *symbol); |
|
206 void *visit(SUB_operator_c *symbol); |
|
207 void *visit(MUL_operator_c *symbol); |
|
208 void *visit(DIV_operator_c *symbol); |
|
209 void *visit(MOD_operator_c *symbol); |
|
210 void *visit(GT_operator_c *symbol); |
|
211 void *visit(GE_operator_c *symbol); |
|
212 void *visit(EQ_operator_c *symbol); |
|
213 void *visit(LT_operator_c *symbol); |
|
214 void *visit(LE_operator_c *symbol); |
|
215 void *visit(NE_operator_c *symbol); |
|
216 void *visit(CAL_operator_c *symbol); |
|
217 void *visit(CALC_operator_c *symbol); |
|
218 void *visit(CALCN_operator_c *symbol); |
|
219 void *visit(RET_operator_c *symbol); |
|
220 void *visit(RETC_operator_c *symbol); |
|
221 void *visit(RETCN_operator_c *symbol); |
|
222 void *visit(JMP_operator_c *symbol); |
|
223 void *visit(JMPC_operator_c *symbol); |
|
224 void *visit(JMPCN_operator_c *symbol); |
|
225 /* Symbol class handled together with function call checks */ |
|
226 // void *visit(il_assign_operator_c *symbol, variable_name); |
|
227 /* Symbol class handled together with function call checks */ |
|
228 // void *visit(il_assign_operator_c *symbol, option, variable_name); |
|
229 |
|
230 |
|
231 |
|
232 /***************************************/ |
|
233 /* B.3 - Language ST (Structured Text) */ |
|
234 /***************************************/ |
|
235 /***********************/ |
|
236 /* B 3.1 - Expressions */ |
|
237 /***********************/ |
|
238 void *visit(or_expression_c *symbol); |
|
239 void *visit(xor_expression_c *symbol); |
|
240 void *visit(and_expression_c *symbol); |
|
241 void *visit(equ_expression_c *symbol); |
|
242 void *visit(notequ_expression_c *symbol); |
|
243 void *visit(lt_expression_c *symbol); |
|
244 void *visit(gt_expression_c *symbol); |
|
245 void *visit(le_expression_c *symbol); |
|
246 void *visit(ge_expression_c *symbol); |
|
247 void *visit(add_expression_c *symbol); |
|
248 void *visit(sub_expression_c *symbol); |
|
249 void *visit(mul_expression_c *symbol); |
|
250 void *visit(div_expression_c *symbol); |
|
251 void *visit(mod_expression_c *symbol); |
|
252 void *visit(power_expression_c *symbol); |
|
253 void *visit(neg_expression_c *symbol); |
|
254 void *visit(not_expression_c *symbol); |
|
255 void *visit(function_invocation_c *symbol); |
|
256 |
|
257 /*********************************/ |
|
258 /* B 3.2.1 Assignment Statements */ |
|
259 /*********************************/ |
|
260 void *visit(assignment_statement_c *symbol); |
|
261 |
|
262 /*****************************************/ |
|
263 /* B 3.2.2 Subprogram Control Statements */ |
|
264 /*****************************************/ |
|
265 void *visit(fb_invocation_c *symbol); |
|
266 |
|
267 /********************************/ |
|
268 /* B 3.2.3 Selection Statements */ |
|
269 /********************************/ |
|
270 |
|
271 void *visit(if_statement_c *symbol); |
|
272 // void *visit(elseif_statement_list_c *symbol); |
|
273 void *visit(elseif_statement_c *symbol); |
|
274 void *visit(case_statement_c *symbol); |
|
275 // void *visit(case_element_list_c *symbol); |
|
276 // void *visit(case_element_c *symbol); |
|
277 void *visit(case_list_c *symbol); |
|
278 |
|
279 /********************************/ |
|
280 /* B 3.2.4 Iteration Statements */ |
|
281 /********************************/ |
|
282 |
|
283 void *visit(for_statement_c *symbol); |
|
284 void *visit(while_statement_c *symbol); |
|
285 void *visit(repeat_statement_c *symbol); |
|
286 |
|
287 |
|
288 //TODO: delete this functions. Why are they needed? |
|
289 void *visit(program_declaration_c *symbol); |
|
290 void *visit(function_declaration_c *symbol); |
|
291 void *visit(function_block_declaration_c *symbol); |
|
292 |
|
293 }; // visit_expression_type_c |
|
294 |