|
1 /* |
|
2 * matiec - a compiler for the programming languages defined in IEC 61131-3 |
|
3 * |
|
4 * Copyright (C) 2009-2012 Mario de Sousa (msousa@fe.up.pt) |
|
5 * Copyright (C) 2012 Manuele Conti (manuele.conti@sirius-es.it) |
|
6 * Copyright (C) 2012 Matteo Facchinetti (matteo.facchinetti@sirius-es.it) |
|
7 * |
|
8 * |
|
9 * This program is free software: you can redistribute it and/or modify |
|
10 * it under the terms of the GNU General Public License as published by |
|
11 * the Free Software Foundation, either version 3 of the License, or |
|
12 * (at your option) any later version. |
|
13 * |
|
14 * This program is distributed in the hope that it will be useful, |
|
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17 * GNU General Public License for more details. |
|
18 * |
|
19 * You should have received a copy of the GNU General Public License |
|
20 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21 * |
|
22 * |
|
23 * This code is made available on the understanding that it will not be |
|
24 * used in safety-critical situations without a full and competent review. |
|
25 */ |
|
26 |
|
27 /* |
|
28 * An IEC 61131-3 compiler. |
|
29 * |
|
30 * Based on the |
|
31 * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) |
|
32 * |
|
33 */ |
|
34 |
|
35 |
|
36 #include "../absyntax_utils/absyntax_utils.hh" |
|
37 |
|
38 class fill_candidate_datatypes_c: public iterator_visitor_c { |
|
39 |
|
40 private: |
|
41 search_varfb_instance_type_c *search_varfb_instance_type; |
|
42 search_base_type_c search_base_type; |
|
43 /* When calling a function block, we must first find it's type, |
|
44 * by searching through the declarations of the variables currently |
|
45 * in scope. |
|
46 * This class does just that... |
|
47 * A new object instance is instantiated whenever we start checking semantics |
|
48 * for a function block type declaration, or a program declaration. |
|
49 * This object instance will then later be called while the |
|
50 * function block's or the program's body is being handled. |
|
51 * |
|
52 * Note that functions cannot contain calls to function blocks, |
|
53 * so we do not create an object instance when handling |
|
54 * a function declaration. |
|
55 */ |
|
56 // search_var_instance_decl_c *search_var_instance_decl; |
|
57 |
|
58 /* This variable was created to pass information from |
|
59 * fill_candidate_datatypes_c::visit(case_statement_c *symbol) function to |
|
60 * fill_candidate_datatypes_c::visit(case_list_c *symbol) function. |
|
61 */ |
|
62 symbol_c *case_expression_type; |
|
63 |
|
64 /* In IL code, once we find a type mismatch error, it is best to |
|
65 * ignore any further errors until the end of the logical operation, |
|
66 * i.e. until the next LD. |
|
67 * However, we cannot clear the il_error flag on all LD operations, |
|
68 * as these may also be used within parenthesis. LD operations |
|
69 * within parenthesis may not clear the error flag. |
|
70 * We therefore need a counter to know how deep inside a parenthesis |
|
71 * structure we are. |
|
72 */ |
|
73 int il_parenthesis_level; |
|
74 bool error_found; |
|
75 |
|
76 /* the current data type of the data stored in the IL stack, i.e. the default variable */ |
|
77 symbol_c *prev_il_instruction; |
|
78 /* the current IL operand being analyzed - its symbol and its data type */ |
|
79 symbol_c *il_operand_type; |
|
80 symbol_c *il_operand; |
|
81 symbol_c *widening_conversion(symbol_c *left_type, symbol_c *right_type, const struct widen_entry widen_table[]); |
|
82 |
|
83 public: |
|
84 fill_candidate_datatypes_c(symbol_c *ignore); |
|
85 virtual ~fill_candidate_datatypes_c(void); |
|
86 |
|
87 /* Match a function declaration with a function call through their parameters.*/ |
|
88 void match_nonformal_call(symbol_c *f_call, symbol_c *f_decl, int *error_count = NULL); |
|
89 void match_formal_call(symbol_c *f_call, symbol_c *f_decl, int *error_count = NULL); |
|
90 |
|
91 void *compute_standard_function_default(function_invocation_c *st_symbol, il_formal_funct_call_c *il_symbol); |
|
92 void *compute_standard_function_il(il_function_call_c *symbol, symbol_c *param_data_type); |
|
93 |
|
94 /* a helper function... */ |
|
95 symbol_c *base_type(symbol_c *symbol); |
|
96 |
|
97 /*********************/ |
|
98 /* B 1.2 - Constants */ |
|
99 /*********************/ |
|
100 /******************************/ |
|
101 /* B 1.2.1 - Numeric Literals */ |
|
102 /******************************/ |
|
103 void *visit(real_c *symbol); |
|
104 void *visit(integer_c *symbol); |
|
105 void *visit(neg_real_c *symbol); |
|
106 void *visit(neg_integer_c *symbol); |
|
107 void *visit(binary_integer_c *symbol); |
|
108 void *visit(octal_integer_c *symbol); |
|
109 void *visit(hex_integer_c *symbol); |
|
110 void *visit(integer_literal_c *symbol); |
|
111 void *visit(real_literal_c *symbol); |
|
112 void *visit(bit_string_literal_c *symbol); |
|
113 void *visit(boolean_literal_c *symbol); |
|
114 void *visit(boolean_true_c *symbol); |
|
115 void *visit(boolean_false_c *symbol); |
|
116 |
|
117 /*******************************/ |
|
118 /* B.1.2.2 Character Strings */ |
|
119 /*******************************/ |
|
120 void *visit(double_byte_character_string_c *symbol); |
|
121 void *visit(single_byte_character_string_c *symbol); |
|
122 |
|
123 /***************************/ |
|
124 /* B 1.2.3 - Time Literals */ |
|
125 /***************************/ |
|
126 /************************/ |
|
127 /* B 1.2.3.1 - Duration */ |
|
128 /************************/ |
|
129 void *visit(duration_c *symbol); |
|
130 |
|
131 /************************************/ |
|
132 /* B 1.2.3.2 - Time of day and Date */ |
|
133 /************************************/ |
|
134 void *visit(time_of_day_c *symbol); |
|
135 void *visit(date_c *symbol); |
|
136 void *visit(date_and_time_c *symbol); |
|
137 |
|
138 |
|
139 /**********************/ |
|
140 /* B 1.3 - Data types */ |
|
141 /**********************/ |
|
142 /********************************/ |
|
143 /* B 1.3.3 - Derived data types */ |
|
144 /********************************/ |
|
145 void *visit(subrange_c *symbol); |
|
146 void *visit(data_type_declaration_c *symbol); |
|
147 void *visit(enumerated_value_c *symbol); |
|
148 |
|
149 /*********************/ |
|
150 /* B 1.4 - Variables */ |
|
151 /*********************/ |
|
152 void *visit(symbolic_variable_c *symbol); |
|
153 |
|
154 /********************************************/ |
|
155 /* B 1.4.1 - Directly Represented Variables */ |
|
156 /********************************************/ |
|
157 void *visit(direct_variable_c *symbol); |
|
158 |
|
159 /*************************************/ |
|
160 /* B 1.4.2 - Multi-element variables */ |
|
161 /*************************************/ |
|
162 void *visit(array_variable_c *symbol); |
|
163 void *visit(structured_variable_c *symbol); |
|
164 |
|
165 /**************************************/ |
|
166 /* B 1.5 - Program organization units */ |
|
167 /**************************************/ |
|
168 /***********************/ |
|
169 /* B 1.5.1 - Functions */ |
|
170 /***********************/ |
|
171 void *visit(function_declaration_c *symbol); |
|
172 |
|
173 /*****************************/ |
|
174 /* B 1.5.2 - Function blocks */ |
|
175 /*****************************/ |
|
176 void *visit(function_block_declaration_c *symbol); |
|
177 |
|
178 /**********************/ |
|
179 /* B 1.5.3 - Programs */ |
|
180 /**********************/ |
|
181 void *visit(program_declaration_c *symbol); |
|
182 |
|
183 /********************************/ |
|
184 /* B 1.7 Configuration elements */ |
|
185 /********************************/ |
|
186 void *visit(configuration_declaration_c *symbol); |
|
187 |
|
188 /****************************************/ |
|
189 /* B.2 - Language IL (Instruction List) */ |
|
190 /****************************************/ |
|
191 /***********************************/ |
|
192 /* B 2.1 Instructions and Operands */ |
|
193 /***********************************/ |
|
194 // void *visit(instruction_list_c *symbol); |
|
195 void *visit(il_simple_operation_c *symbol); |
|
196 void *visit(il_function_call_c *symbol); |
|
197 void *visit(il_expression_c *symbol); |
|
198 void *visit(il_jump_operation_c *symbol); |
|
199 void *visit(il_fb_call_c *symbol); |
|
200 void *visit(il_formal_funct_call_c *symbol); |
|
201 /* |
|
202 void *visit(il_operand_list_c *symbol); |
|
203 void *visit(simple_instr_list_c *symbol); |
|
204 void *visit(il_param_list_c *symbol); |
|
205 void *visit(il_param_assignment_c *symbol); |
|
206 void *visit(il_param_out_assignment_c *symbol); |
|
207 */ |
|
208 |
|
209 /*******************/ |
|
210 /* B 2.2 Operators */ |
|
211 /*******************/ |
|
212 void *visit(LD_operator_c *symbol); |
|
213 void *visit(LDN_operator_c *symbol); |
|
214 void *visit(ST_operator_c *symbol); |
|
215 void *visit(STN_operator_c *symbol); |
|
216 void *visit(NOT_operator_c *symbol); |
|
217 void *visit(S_operator_c *symbol); |
|
218 void *visit(R_operator_c *symbol); |
|
219 void *visit(S1_operator_c *symbol); |
|
220 void *visit(R1_operator_c *symbol); |
|
221 void *visit(CLK_operator_c *symbol); |
|
222 void *visit(CU_operator_c *symbol); |
|
223 void *visit(CD_operator_c *symbol); |
|
224 void *visit(PV_operator_c *symbol); |
|
225 void *visit(IN_operator_c *symbol); |
|
226 void *visit(PT_operator_c *symbol); |
|
227 void *visit(AND_operator_c *symbol); |
|
228 void *visit(OR_operator_c *symbol); |
|
229 void *visit(XOR_operator_c *symbol); |
|
230 void *visit(ANDN_operator_c *symbol); |
|
231 void *visit(ORN_operator_c *symbol); |
|
232 void *visit(XORN_operator_c *symbol); |
|
233 void *visit(ADD_operator_c *symbol); |
|
234 void *visit(SUB_operator_c *symbol); |
|
235 void *visit(MUL_operator_c *symbol); |
|
236 void *visit(DIV_operator_c *symbol); |
|
237 void *visit(MOD_operator_c *symbol); |
|
238 void *visit(GT_operator_c *symbol); |
|
239 void *visit(GE_operator_c *symbol); |
|
240 void *visit(EQ_operator_c *symbol); |
|
241 void *visit(LT_operator_c *symbol); |
|
242 void *visit(LE_operator_c *symbol); |
|
243 void *visit(NE_operator_c *symbol); |
|
244 void *visit(CAL_operator_c *symbol); |
|
245 void *visit(CALC_operator_c *symbol); |
|
246 void *visit(CALCN_operator_c *symbol); |
|
247 void *visit(RET_operator_c *symbol); |
|
248 void *visit(RETC_operator_c *symbol); |
|
249 void *visit(RETCN_operator_c *symbol); |
|
250 void *visit(JMP_operator_c *symbol); |
|
251 void *visit(JMPC_operator_c *symbol); |
|
252 void *visit(JMPCN_operator_c *symbol); |
|
253 /* Symbol class handled together with function call checks */ |
|
254 // void *visit(il_assign_operator_c *symbol, variable_name); |
|
255 /* Symbol class handled together with function call checks */ |
|
256 // void *visit(il_assign_operator_c *symbol, option, variable_name); |
|
257 |
|
258 |
|
259 /***************************************/ |
|
260 /* B.3 - Language ST (Structured Text) */ |
|
261 /***************************************/ |
|
262 /***********************/ |
|
263 /* B 3.1 - Expressions */ |
|
264 /***********************/ |
|
265 void *visit(or_expression_c *symbol); |
|
266 void *visit(xor_expression_c *symbol); |
|
267 void *visit(and_expression_c *symbol); |
|
268 void *visit(equ_expression_c *symbol); |
|
269 void *visit(notequ_expression_c *symbol); |
|
270 void *visit(lt_expression_c *symbol); |
|
271 void *visit(gt_expression_c *symbol); |
|
272 void *visit(le_expression_c *symbol); |
|
273 void *visit(ge_expression_c *symbol); |
|
274 void *visit(add_expression_c *symbol); |
|
275 void *visit(sub_expression_c *symbol); |
|
276 void *visit(mul_expression_c *symbol); |
|
277 void *visit(div_expression_c *symbol); |
|
278 void *visit(mod_expression_c *symbol); |
|
279 void *visit(power_expression_c *symbol); |
|
280 void *visit(neg_expression_c *symbol); |
|
281 void *visit(not_expression_c *symbol); |
|
282 void *visit(function_invocation_c *symbol); |
|
283 |
|
284 /*********************************/ |
|
285 /* B 3.2.1 Assignment Statements */ |
|
286 /*********************************/ |
|
287 void *visit(assignment_statement_c *symbol); |
|
288 |
|
289 /*****************************************/ |
|
290 /* B 3.2.2 Subprogram Control Statements */ |
|
291 /*****************************************/ |
|
292 //void *visit(fb_invocation_c *symbol); |
|
293 |
|
294 /********************************/ |
|
295 /* B 3.2.3 Selection Statements */ |
|
296 /********************************/ |
|
297 void *visit(if_statement_c *symbol); |
|
298 // void *visit(elseif_statement_list_c *symbol); |
|
299 void *visit(elseif_statement_c *symbol); |
|
300 void *visit(case_statement_c *symbol); |
|
301 |
|
302 /********************************/ |
|
303 /* B 3.2.4 Iteration Statements */ |
|
304 /********************************/ |
|
305 void *visit(for_statement_c *symbol); |
|
306 void *visit(while_statement_c *symbol); |
|
307 void *visit(repeat_statement_c *symbol); |
|
308 |
|
309 }; // fill_candidate_datatypes_c |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |