|
1 /* |
|
2 * matiec - a compiler for the programming languages defined in IEC 61131-3 |
|
3 * |
|
4 * Copyright (C) 2009-2011 Mario de Sousa (msousa@fe.up.pt) |
|
5 * Copyright (C) 2011-2012 Manuele Conti (manuele.conti@sirius-es.it) |
|
6 * Copyright (C) 2011-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 print_datatypes_error_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 |
|
57 /* In IL code, once we find a type mismatch error, it is best to |
|
58 * ignore any further errors until the end of the logical operation, |
|
59 * i.e. until the next LD. |
|
60 * However, we cannot clear the il_error flag on all LD operations, |
|
61 * as these may also be used within parenthesis. LD operations |
|
62 * within parenthesis may not clear the error flag. |
|
63 * We therefore need a counter to know how deep inside a parenthesis |
|
64 * structure we are. |
|
65 */ |
|
66 int il_parenthesis_level; |
|
67 bool il_error; |
|
68 bool error_found; |
|
69 |
|
70 /* the current data type of the data stored in the IL stack, i.e. the default variable */ |
|
71 symbol_c *prev_il_instruction; |
|
72 /* the current IL operand being analyzed - its symbol and its data type */ |
|
73 symbol_c *il_operand_type; |
|
74 symbol_c *il_operand; |
|
75 |
|
76 |
|
77 public: |
|
78 print_datatypes_error_c(symbol_c *ignore); |
|
79 virtual ~print_datatypes_error_c(void); |
|
80 int get_error_found(); |
|
81 |
|
82 /* a helper function... */ |
|
83 symbol_c *base_type(symbol_c *symbol); |
|
84 |
|
85 /*********************/ |
|
86 /* B 1.2 - Constants */ |
|
87 /*********************/ |
|
88 /******************************/ |
|
89 /* B 1.2.1 - Numeric Literals */ |
|
90 /******************************/ |
|
91 void *visit(real_c *symbol); |
|
92 void *visit(integer_c *symbol); |
|
93 void *visit(neg_real_c *symbol); |
|
94 void *visit(neg_integer_c *symbol); |
|
95 void *visit(binary_integer_c *symbol); |
|
96 void *visit(octal_integer_c *symbol); |
|
97 void *visit(hex_integer_c *symbol); |
|
98 void *visit(integer_literal_c *symbol); |
|
99 void *visit(real_literal_c *symbol); |
|
100 void *visit(bit_string_literal_c *symbol); |
|
101 void *visit(boolean_literal_c *symbol); |
|
102 void *visit(boolean_true_c *symbol); |
|
103 void *visit(boolean_false_c *symbol); |
|
104 |
|
105 /*******************************/ |
|
106 /* B.1.2.2 Character Strings */ |
|
107 /*******************************/ |
|
108 void *visit(double_byte_character_string_c *symbol); |
|
109 void *visit(single_byte_character_string_c *symbol); |
|
110 |
|
111 /***************************/ |
|
112 /* B 1.2.3 - Time Literals */ |
|
113 /***************************/ |
|
114 /************************/ |
|
115 /* B 1.2.3.1 - Duration */ |
|
116 /************************/ |
|
117 void *visit(duration_c *symbol); |
|
118 |
|
119 /************************************/ |
|
120 /* B 1.2.3.2 - Time of day and Date */ |
|
121 /************************************/ |
|
122 void *visit(time_of_day_c *symbol); |
|
123 void *visit(date_c *symbol); |
|
124 void *visit(date_and_time_c *symbol); |
|
125 |
|
126 /**********************/ |
|
127 /* B 1.3 - Data types */ |
|
128 /**********************/ |
|
129 /********************************/ |
|
130 /* B 1.3.3 - Derived data types */ |
|
131 /********************************/ |
|
132 void *visit(data_type_declaration_c *symbol); |
|
133 void *visit(enumerated_value_c *symbol); |
|
134 |
|
135 /*********************/ |
|
136 /* B 1.4 - Variables */ |
|
137 /*********************/ |
|
138 void *visit(symbolic_variable_c *symbol); |
|
139 |
|
140 /********************************************/ |
|
141 /* B 1.4.1 - Directly Represented Variables */ |
|
142 /********************************************/ |
|
143 void *visit(direct_variable_c *symbol); |
|
144 |
|
145 /*************************************/ |
|
146 /* B 1.4.2 - Multi-element variables */ |
|
147 /*************************************/ |
|
148 void *visit(array_variable_c *symbol); |
|
149 void *visit(structured_variable_c *symbol); |
|
150 |
|
151 /**************************************/ |
|
152 /* B 1.5 - Program organization units */ |
|
153 /**************************************/ |
|
154 /***********************/ |
|
155 /* B 1.5.1 - Functions */ |
|
156 /***********************/ |
|
157 void *visit(function_declaration_c *symbol); |
|
158 |
|
159 /*****************************/ |
|
160 /* B 1.5.2 - Function blocks */ |
|
161 /*****************************/ |
|
162 void *visit(function_block_declaration_c *symbol); |
|
163 |
|
164 /**********************/ |
|
165 /* B 1.5.3 - Programs */ |
|
166 /**********************/ |
|
167 void *visit(program_declaration_c *symbol); |
|
168 |
|
169 /********************************/ |
|
170 /* B 1.7 Configuration elements */ |
|
171 /********************************/ |
|
172 void *visit(configuration_declaration_c *symbol); |
|
173 |
|
174 /****************************************/ |
|
175 /* B.2 - Language IL (Instruction List) */ |
|
176 /****************************************/ |
|
177 /***********************************/ |
|
178 /* B 2.1 Instructions and Operands */ |
|
179 /***********************************/ |
|
180 // void *visit(instruction_list_c *symbol); |
|
181 void *visit(il_simple_operation_c *symbol); |
|
182 void *visit(il_function_call_c *symbol); |
|
183 void *visit(il_expression_c *symbol); |
|
184 void *visit(il_fb_call_c *symbol); |
|
185 void *visit(il_formal_funct_call_c *symbol); |
|
186 /* |
|
187 void *visit(il_operand_list_c *symbol); |
|
188 void *visit(simple_instr_list_c *symbol); |
|
189 void *visit(il_param_list_c *symbol); |
|
190 void *visit(il_param_assignment_c *symbol); |
|
191 void *visit(il_param_out_assignment_c *symbol); |
|
192 */ |
|
193 |
|
194 /*******************/ |
|
195 /* B 2.2 Operators */ |
|
196 /*******************/ |
|
197 void *visit(LD_operator_c *symbol); |
|
198 void *visit(LDN_operator_c *symbol); |
|
199 void *visit(ST_operator_c *symbol); |
|
200 void *visit(STN_operator_c *symbol); |
|
201 void *visit(NOT_operator_c *symbol); |
|
202 void *visit(S_operator_c *symbol); |
|
203 void *visit(R_operator_c *symbol); |
|
204 void *visit(S1_operator_c *symbol); |
|
205 void *visit(R1_operator_c *symbol); |
|
206 void *visit(CLK_operator_c *symbol); |
|
207 void *visit(CU_operator_c *symbol); |
|
208 void *visit(CD_operator_c *symbol); |
|
209 void *visit(PV_operator_c *symbol); |
|
210 void *visit(IN_operator_c *symbol); |
|
211 void *visit(PT_operator_c *symbol); |
|
212 void *visit(AND_operator_c *symbol); |
|
213 void *visit(OR_operator_c *symbol); |
|
214 void *visit(XOR_operator_c *symbol); |
|
215 void *visit(ANDN_operator_c *symbol); |
|
216 void *visit(ORN_operator_c *symbol); |
|
217 void *visit(XORN_operator_c *symbol); |
|
218 void *visit(ADD_operator_c *symbol); |
|
219 void *visit(SUB_operator_c *symbol); |
|
220 void *visit(MUL_operator_c *symbol); |
|
221 void *visit(DIV_operator_c *symbol); |
|
222 void *visit(MOD_operator_c *symbol); |
|
223 void *visit(GT_operator_c *symbol); |
|
224 void *visit(GE_operator_c *symbol); |
|
225 void *visit(EQ_operator_c *symbol); |
|
226 void *visit(LT_operator_c *symbol); |
|
227 void *visit(LE_operator_c *symbol); |
|
228 void *visit(NE_operator_c *symbol); |
|
229 void *visit(CAL_operator_c *symbol); |
|
230 void *visit(CALC_operator_c *symbol); |
|
231 void *visit(CALCN_operator_c *symbol); |
|
232 void *visit(RET_operator_c *symbol); |
|
233 void *visit(RETC_operator_c *symbol); |
|
234 void *visit(RETCN_operator_c *symbol); |
|
235 void *visit(JMP_operator_c *symbol); |
|
236 void *visit(JMPC_operator_c *symbol); |
|
237 void *visit(JMPCN_operator_c *symbol); |
|
238 /* Symbol class handled together with function call checks */ |
|
239 // void *visit(il_assign_operator_c *symbol, variable_name); |
|
240 /* Symbol class handled together with function call checks */ |
|
241 // void *visit(il_assign_operator_c *symbol, option, variable_name); |
|
242 |
|
243 /***************************************/ |
|
244 /* B.3 - Language ST (Structured Text) */ |
|
245 /***************************************/ |
|
246 /***********************/ |
|
247 /* B 3.1 - Expressions */ |
|
248 /***********************/ |
|
249 void *visit(or_expression_c *symbol); |
|
250 void *visit(xor_expression_c *symbol); |
|
251 void *visit(and_expression_c *symbol); |
|
252 void *visit(equ_expression_c *symbol); |
|
253 void *visit(notequ_expression_c *symbol); |
|
254 void *visit(lt_expression_c *symbol); |
|
255 void *visit(gt_expression_c *symbol); |
|
256 void *visit(le_expression_c *symbol); |
|
257 void *visit(ge_expression_c *symbol); |
|
258 void *visit(add_expression_c *symbol); |
|
259 void *visit(sub_expression_c *symbol); |
|
260 void *visit(mul_expression_c *symbol); |
|
261 void *visit(div_expression_c *symbol); |
|
262 void *visit(mod_expression_c *symbol); |
|
263 void *visit(power_expression_c *symbol); |
|
264 void *visit(neg_expression_c *symbol); |
|
265 void *visit(not_expression_c *symbol); |
|
266 void *visit(function_invocation_c *symbol); |
|
267 |
|
268 /*********************************/ |
|
269 /* B 3.2.1 Assignment Statements */ |
|
270 /*********************************/ |
|
271 void *visit(assignment_statement_c *symbol); |
|
272 |
|
273 /*****************************************/ |
|
274 /* B 3.2.2 Subprogram Control Statements */ |
|
275 /*****************************************/ |
|
276 //void *visit(fb_invocation_c *symbol); |
|
277 |
|
278 /********************************/ |
|
279 /* B 3.2.3 Selection Statements */ |
|
280 /********************************/ |
|
281 void *visit(if_statement_c *symbol); |
|
282 // void *visit(elseif_statement_list_c *symbol); |
|
283 void *visit(elseif_statement_c *symbol); |
|
284 void *visit(case_statement_c *symbol); |
|
285 // void *visit(case_element_list_c *symbol); |
|
286 // void *visit(case_element_c *symbol); |
|
287 // void *visit(case_list_c *symbol); |
|
288 |
|
289 /********************************/ |
|
290 /* B 3.2.4 Iteration Statements */ |
|
291 /********************************/ |
|
292 void *visit(for_statement_c *symbol); |
|
293 void *visit(while_statement_c *symbol); |
|
294 void *visit(repeat_statement_c *symbol); |
|
295 |
|
296 }; // print_datatypes_error_c |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |