|
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 |
|
26 /* Determine the data type of an ST expression. |
|
27 * A reference to the relevant type definition is returned. |
|
28 * |
|
29 * For example: |
|
30 * 2 + 3 -> returns reference to a int_type_name_c object. |
|
31 * 22.2 - 5 -> returns reference to a real_type_name_c object. |
|
32 * etc... |
|
33 */ |
|
34 class search_expression_type_c: public search_constant_type_c { |
|
35 private: |
|
36 search_varfb_instance_type_c *search_varfb_instance_type; |
|
37 search_base_type_c search_base_type; |
|
38 |
|
39 public: |
|
40 search_expression_type_c(symbol_c *search_scope) { |
|
41 search_varfb_instance_type = new search_varfb_instance_type_c(search_scope); |
|
42 } |
|
43 |
|
44 virtual ~search_expression_type_c(void) { |
|
45 delete search_varfb_instance_type; |
|
46 } |
|
47 |
|
48 /* A helper function... */ |
|
49 bool is_bool_type(symbol_c *type_symbol) { |
|
50 return (typeid(*type_symbol) == typeid(bool_type_name_c)); |
|
51 } |
|
52 |
|
53 /* A helper function... */ |
|
54 bool is_time_compatible(symbol_c *type_symbol) { |
|
55 if (typeid(*type_symbol) == typeid(time_type_name_c)) {return true;} |
|
56 if (typeid(*type_symbol) == typeid(date_type_name_c)) {return true;} |
|
57 if (typeid(*type_symbol) == typeid(tod_type_name_c)) {return true;} |
|
58 if (typeid(*type_symbol) == typeid(dt_type_name_c)) {return true;} |
|
59 return false; |
|
60 } |
|
61 |
|
62 /* A helper function... */ |
|
63 bool is_numeric_compatible(symbol_c *type_symbol) { |
|
64 if (typeid(*type_symbol) == typeid(sint_type_name_c)) {return true;} |
|
65 if (typeid(*type_symbol) == typeid(int_type_name_c)) {return true;} |
|
66 if (typeid(*type_symbol) == typeid(dint_type_name_c)) {return true;} |
|
67 if (typeid(*type_symbol) == typeid(lint_type_name_c)) {return true;} |
|
68 if (typeid(*type_symbol) == typeid(usint_type_name_c)) {return true;} |
|
69 if (typeid(*type_symbol) == typeid(uint_type_name_c)) {return true;} |
|
70 if (typeid(*type_symbol) == typeid(udint_type_name_c)) {return true;} |
|
71 if (typeid(*type_symbol) == typeid(ulint_type_name_c)) {return true;} |
|
72 if (typeid(*type_symbol) == typeid(real_type_name_c)) {return true;} |
|
73 if (typeid(*type_symbol) == typeid(lreal_type_name_c)) {return true;} |
|
74 if (typeid(*type_symbol) == typeid(byte_type_name_c)) {return true;} |
|
75 if (typeid(*type_symbol) == typeid(word_type_name_c)) {return true;} |
|
76 if (typeid(*type_symbol) == typeid(dword_type_name_c)) {return true;} |
|
77 if (typeid(*type_symbol) == typeid(lword_type_name_c)) {return true;} |
|
78 return false; |
|
79 } |
|
80 |
|
81 private: |
|
82 |
|
83 static bool_type_name_c bool_type_name; |
|
84 |
|
85 /* A helper function... */ |
|
86 void *compute_boolean_expression(symbol_c *left_type, symbol_c *right_type) { |
|
87 if (typeid(*left_type) == typeid(bool_type_name_c) and typeid(*right_type) == typeid(bool_type_name_c)) {return (void *)left_type;} |
|
88 if (is_numeric_compatible(left_type) && is_numeric_compatible(right_type)) {return (void *)left_type;} |
|
89 ERROR; |
|
90 return NULL; |
|
91 } |
|
92 |
|
93 /* A helper function... */ |
|
94 void *compute_numeric_expression(symbol_c *left_type, symbol_c *right_type) { |
|
95 if (is_numeric_compatible(left_type) && is_numeric_compatible(right_type)) {return (void *)left_type;} |
|
96 ERROR; |
|
97 return NULL; |
|
98 } |
|
99 |
|
100 /* a helper function... */ |
|
101 symbol_c *base_type(symbol_c *symbol) { |
|
102 return (symbol_c *)symbol->accept(search_base_type); |
|
103 } |
|
104 |
|
105 /*********************/ |
|
106 /* B 1.4 - Variables */ |
|
107 /*********************/ |
|
108 |
|
109 void *visit(symbolic_variable_c *symbol) { |
|
110 symbol_c *res; |
|
111 |
|
112 /* Nope, now we assume it is a variable, and determine its type... */ |
|
113 res = search_varfb_instance_type->get_type(symbol); |
|
114 if (NULL != res) return res; |
|
115 |
|
116 return NULL; |
|
117 } |
|
118 |
|
119 |
|
120 /***************************************/ |
|
121 /* B.3 - Language ST (Structured Text) */ |
|
122 /***************************************/ |
|
123 /***********************/ |
|
124 /* B 3.1 - Expressions */ |
|
125 /***********************/ |
|
126 void *visit(or_expression_c *symbol) { |
|
127 symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
|
128 symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
|
129 return compute_boolean_expression(left_type, right_type); |
|
130 } |
|
131 |
|
132 void *visit(xor_expression_c *symbol) { |
|
133 symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
|
134 symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
|
135 return compute_boolean_expression(left_type, right_type); |
|
136 } |
|
137 |
|
138 void *visit(and_expression_c *symbol) { |
|
139 symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
|
140 symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
|
141 return compute_boolean_expression(left_type, right_type); |
|
142 } |
|
143 |
|
144 void *visit(equ_expression_c *symbol) {return (void *)&bool_type_name;} |
|
145 void *visit(notequ_expression_c *symbol) {return (void *)&bool_type_name;} |
|
146 void *visit(lt_expression_c *symbol) {return (void *)&bool_type_name;} |
|
147 void *visit(gt_expression_c *symbol) {return (void *)&bool_type_name;} |
|
148 void *visit(le_expression_c *symbol) {return (void *)&bool_type_name;} |
|
149 void *visit(ge_expression_c *symbol) {return (void *)&bool_type_name;} |
|
150 |
|
151 void *visit(add_expression_c *symbol) { |
|
152 symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
|
153 symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
|
154 if (is_time_compatible(left_type) && is_time_compatible(right_type)) {return left_type;} |
|
155 return compute_numeric_expression(left_type, right_type); |
|
156 } |
|
157 |
|
158 void *visit(sub_expression_c *symbol) { |
|
159 symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
|
160 symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
|
161 if (is_time_compatible(left_type) && is_time_compatible(right_type)) {return left_type;} |
|
162 return compute_numeric_expression(left_type, right_type); |
|
163 } |
|
164 |
|
165 void *visit(mul_expression_c *symbol) { |
|
166 symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
|
167 symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
|
168 if (is_time_compatible(left_type) && is_time_compatible(right_type)) {return left_type;} |
|
169 return compute_numeric_expression(left_type, right_type); |
|
170 } |
|
171 |
|
172 void *visit(div_expression_c *symbol) { |
|
173 symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
|
174 symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
|
175 return compute_numeric_expression(left_type, right_type); |
|
176 } |
|
177 |
|
178 void *visit(mod_expression_c *symbol) { |
|
179 symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
|
180 symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
|
181 return compute_numeric_expression(left_type, right_type); |
|
182 } |
|
183 |
|
184 void *visit(power_expression_c *symbol) { |
|
185 symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this)); |
|
186 symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this)); |
|
187 if (typeid(*left_type) == typeid(real_type_name_c) and typeid(*right_type) == typeid(sint_type_name_c)) {return (void *)left_type;} |
|
188 if (typeid(*left_type) == typeid(real_type_name_c) and typeid(*right_type) == typeid(int_type_name_c)) {return (void *)left_type;} |
|
189 if (typeid(*left_type) == typeid(real_type_name_c) and typeid(*right_type) == typeid(dint_type_name_c)) {return (void *)left_type;} |
|
190 if (typeid(*left_type) == typeid(real_type_name_c) and typeid(*right_type) == typeid(lint_type_name_c)) {return (void *)left_type;} |
|
191 if (typeid(*left_type) == typeid(real_type_name_c) and typeid(*right_type) == typeid(usint_type_name_c)) {return (void *)left_type;} |
|
192 if (typeid(*left_type) == typeid(real_type_name_c) and typeid(*right_type) == typeid(uint_type_name_c)) {return (void *)left_type;} |
|
193 if (typeid(*left_type) == typeid(real_type_name_c) and typeid(*right_type) == typeid(udint_type_name_c)) {return (void *)left_type;} |
|
194 if (typeid(*left_type) == typeid(real_type_name_c) and typeid(*right_type) == typeid(ulint_type_name_c)) {return (void *)left_type;} |
|
195 if (typeid(*left_type) == typeid(lreal_type_name_c) and typeid(*right_type) == typeid(sint_type_name_c)) {return (void *)left_type;} |
|
196 if (typeid(*left_type) == typeid(lreal_type_name_c) and typeid(*right_type) == typeid(int_type_name_c)) {return (void *)left_type;} |
|
197 if (typeid(*left_type) == typeid(lreal_type_name_c) and typeid(*right_type) == typeid(dint_type_name_c)) {return (void *)left_type;} |
|
198 if (typeid(*left_type) == typeid(lreal_type_name_c) and typeid(*right_type) == typeid(lint_type_name_c)) {return (void *)left_type;} |
|
199 if (typeid(*left_type) == typeid(lreal_type_name_c) and typeid(*right_type) == typeid(usint_type_name_c)) {return (void *)left_type;} |
|
200 if (typeid(*left_type) == typeid(lreal_type_name_c) and typeid(*right_type) == typeid(uint_type_name_c)) {return (void *)left_type;} |
|
201 if (typeid(*left_type) == typeid(lreal_type_name_c) and typeid(*right_type) == typeid(udint_type_name_c)) {return (void *)left_type;} |
|
202 if (typeid(*left_type) == typeid(lreal_type_name_c) and typeid(*right_type) == typeid(ulint_type_name_c)) {return (void *)left_type;} |
|
203 ERROR; |
|
204 return NULL; |
|
205 } |
|
206 |
|
207 void *visit(neg_expression_c *symbol) { |
|
208 symbol_c *exp_type = base_type((symbol_c *)symbol->exp->accept(*this)); |
|
209 if (typeid(*exp_type) == typeid(sint_type_name_c)) {return (void *)exp_type;} |
|
210 if (typeid(*exp_type) == typeid(int_type_name_c)) {return (void *)exp_type;} |
|
211 if (typeid(*exp_type) == typeid(dint_type_name_c)) {return (void *)exp_type;} |
|
212 if (typeid(*exp_type) == typeid(lint_type_name_c)) {return (void *)exp_type;} |
|
213 if (typeid(*exp_type) == typeid(usint_type_name_c)) {return (void *)exp_type;} |
|
214 if (typeid(*exp_type) == typeid(uint_type_name_c)) {return (void *)exp_type;} |
|
215 if (typeid(*exp_type) == typeid(udint_type_name_c)) {return (void *)exp_type;} |
|
216 if (typeid(*exp_type) == typeid(ulint_type_name_c)) {return (void *)exp_type;} |
|
217 if (typeid(*exp_type) == typeid(real_type_name_c)) {return (void *)exp_type;} |
|
218 if (typeid(*exp_type) == typeid(lreal_type_name_c)) {return (void *)exp_type;} |
|
219 if (typeid(*exp_type) == typeid(time_type_name_c)) {return (void *)exp_type;} |
|
220 ERROR; |
|
221 return NULL; |
|
222 } |
|
223 |
|
224 void *visit(not_expression_c *symbol) { |
|
225 symbol_c *exp_type = base_type((symbol_c *)symbol->exp->accept(*this)); |
|
226 if (typeid(*exp_type) == typeid(bool_type_name_c)) {return (void *)exp_type;} |
|
227 ERROR; |
|
228 return NULL; |
|
229 } |
|
230 |
|
231 void *visit(function_invocation_c *symbol) { |
|
232 function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name); |
|
233 |
|
234 if (f_decl == function_symtable.end_value()) |
|
235 /* should never occur. The function being called MUST be in the symtable... */ |
|
236 ERROR; |
|
237 |
|
238 return base_type(f_decl->type_name); |
|
239 } |
|
240 |
|
241 }; |
|
242 |
|
243 bool_type_name_c search_expression_type_c::bool_type_name; |
|
244 |