|
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 /* |
|
27 * Determine the default initial value of a type declaration. |
|
28 * |
|
29 * This is part of the 4th stage that generates |
|
30 * a c++ source program equivalent to the IL and ST |
|
31 * code. |
|
32 */ |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 //#include <stdio.h> /* required for NULL */ |
|
38 //#include <string> |
|
39 //#include <iostream> |
|
40 |
|
41 //#include "../../util/symtable.hh" |
|
42 |
|
43 //#include "generate_cc.hh" |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 /* Given a type definition declration, determine its default |
|
50 * initial value. Note that types based on other types |
|
51 * may have to iterate through each type it is based on |
|
52 * to determine the initial value. |
|
53 * E.g. |
|
54 * TYPE |
|
55 * A_t : INT := 10; |
|
56 * B_t : A_t := 20; |
|
57 * C_t : B_t; |
|
58 * D_t : C_t := 40; |
|
59 * END_TYPE |
|
60 * Where the default initial value for C_t is 20! |
|
61 */ |
|
62 /* NOTE: The main program only needs one instance of |
|
63 * this class of object. This class |
|
64 * is therefore a singleton. |
|
65 */ |
|
66 class type_initial_value_c : public null_visitor_c { |
|
67 private: |
|
68 static type_initial_value_c *_instance; |
|
69 /* constants for the default values of elementary data types... */ |
|
70 static real_c *real_0; |
|
71 static integer_c *integer_0, *integer_1; |
|
72 static boolean_literal_c *bool_0; |
|
73 static date_literal_c *date_literal_0; |
|
74 static daytime_c *daytime_literal_0; |
|
75 static duration_c *time_0; |
|
76 static date_c *date_0; |
|
77 static time_of_day_c *tod_0; |
|
78 static date_and_time_c *dt_0; |
|
79 static single_byte_character_string_c *string_0; |
|
80 static double_byte_character_string_c *wstring_0; |
|
81 |
|
82 public: |
|
83 static type_initial_value_c *instance(void) { |
|
84 if (_instance != NULL) |
|
85 return _instance; |
|
86 |
|
87 _instance = new type_initial_value_c; |
|
88 |
|
89 real_0 = new real_c("0"); |
|
90 integer_0 = new integer_c("0"); |
|
91 integer_1 = new integer_c("1"); |
|
92 bool_0 = new boolean_literal_c(new bool_type_name_c(),new boolean_false_c()); |
|
93 /* FIXME: Our current implementation only allows dates from 1970 onwards, |
|
94 * but the standard defines the date 0001-01-01 as the default value |
|
95 * for the DATE data type. Untill we fix our implementation, we use 1970-01-01 |
|
96 * as our default value!! |
|
97 */ |
|
98 // date_literal_0 = new date_literal_c(integer_1, integer_1, integer_1); |
|
99 date_literal_0 = new date_literal_c(new integer_c("1970"), integer_1, integer_1); |
|
100 daytime_literal_0 = new daytime_c(integer_0, integer_0, real_0); |
|
101 time_0 = new duration_c(NULL, new seconds_c(integer_0, NULL)); // T#0S |
|
102 date_0 = new date_c(date_literal_0); // D#0001-01-01 |
|
103 tod_0 = new time_of_day_c(daytime_literal_0); // TOD#00:00:00 |
|
104 dt_0 = new date_and_time_c(date_literal_0, daytime_literal_0); // DT#0001-01-01-00:00:00 |
|
105 string_0 = new single_byte_character_string_c("''"); |
|
106 wstring_0 = new double_byte_character_string_c("\"\""); |
|
107 |
|
108 return _instance; |
|
109 } |
|
110 |
|
111 protected: |
|
112 type_initial_value_c(void) {} |
|
113 |
|
114 public: |
|
115 symbol_c *get(identifier_c *type_name) { |
|
116 TRACE("type_initial_value_c::get(): called "); |
|
117 return (symbol_c *)type_name->accept(*this); |
|
118 } |
|
119 |
|
120 |
|
121 private: |
|
122 void *handle_type_spec(symbol_c *base_type_name, symbol_c *type_spec_init) { |
|
123 if (type_spec_init != NULL) |
|
124 return type_spec_init; |
|
125 /* no initial value specified, so we return |
|
126 * the initial value of the type this type is based on... |
|
127 */ |
|
128 return base_type_name->accept(*this); |
|
129 } |
|
130 |
|
131 public: |
|
132 void *visit(identifier_c *type_name) { |
|
133 /* look up the type declaration... */ |
|
134 symbol_c *type_decl = type_symtable.find_value(type_name); |
|
135 if (type_decl == type_symtable.end_value()) |
|
136 /* Type declaration not found!! */ |
|
137 /* NOTE: Variables declared out of function block 'data types', |
|
138 * for eg: VAR timer: TON; END_VAR |
|
139 * do not have a default value, so (TON) will never be found in the |
|
140 * type symbol table. This means we cannot simply consider this |
|
141 * an error and abort, but must rather return a NULL. |
|
142 */ |
|
143 return NULL; |
|
144 |
|
145 return type_decl->accept(*this); |
|
146 } |
|
147 |
|
148 /***********************************/ |
|
149 /* B 1.3.1 - Elementary Data Types */ |
|
150 /***********************************/ |
|
151 void *visit(time_type_name_c *symbol) {return (void *)time_0;} |
|
152 void *visit(bool_type_name_c *symbol) {return (void *)bool_0;} |
|
153 void *visit(sint_type_name_c *symbol) {return (void *)integer_0;} |
|
154 void *visit(int_type_name_c *symbol) {return (void *)integer_0;} |
|
155 void *visit(dint_type_name_c *symbol) {return (void *)integer_0;} |
|
156 void *visit(lint_type_name_c *symbol) {return (void *)integer_0;} |
|
157 void *visit(usint_type_name_c *symbol) {return (void *)integer_0;} |
|
158 void *visit(uint_type_name_c *symbol) {return (void *)integer_0;} |
|
159 void *visit(udint_type_name_c *symbol) {return (void *)integer_0;} |
|
160 void *visit(ulint_type_name_c *symbol) {return (void *)integer_0;} |
|
161 void *visit(real_type_name_c *symbol) {return (void *)real_0;} |
|
162 void *visit(lreal_type_name_c *symbol) {return (void *)real_0;} |
|
163 void *visit(date_type_name_c *symbol) {return (void *)date_0;} |
|
164 void *visit(tod_type_name_c *symbol) {return (void *)tod_0;} |
|
165 void *visit(dt_type_name_c *symbol) {return (void *)dt_0;} |
|
166 void *visit(byte_type_name_c *symbol) {return (void *)integer_0;} |
|
167 void *visit(word_type_name_c *symbol) {return (void *)integer_0;} |
|
168 void *visit(dword_type_name_c *symbol) {return (void *)integer_0;} |
|
169 void *visit(lword_type_name_c *symbol) {return (void *)integer_0;} |
|
170 void *visit(string_type_name_c *symbol) {return (void *)string_0;} |
|
171 void *visit(wstring_type_name_c *symbol) {return (void *)wstring_0;} |
|
172 |
|
173 /********************************/ |
|
174 /* B 1.3.3 - Derived data types */ |
|
175 /********************************/ |
|
176 /* simple_type_name ':' simple_spec_init */ |
|
177 void *visit(simple_type_declaration_c *symbol) { |
|
178 return symbol->simple_spec_init->accept(*this); |
|
179 } |
|
180 /* simple_specification ASSIGN constant */ |
|
181 void *visit(simple_spec_init_c *symbol) { |
|
182 return handle_type_spec(symbol->simple_specification, symbol->constant); |
|
183 } |
|
184 /* subrange_type_name ':' subrange_spec_init */ |
|
185 void *visit(subrange_type_declaration_c *symbol) { |
|
186 return symbol->subrange_spec_init->accept(*this); |
|
187 } |
|
188 /* subrange_specification ASSIGN signed_integer */ |
|
189 void *visit(subrange_spec_init_c *symbol) { |
|
190 return handle_type_spec(symbol->subrange_specification, symbol->signed_integer); |
|
191 } |
|
192 /* integer_type_name '(' subrange')' */ |
|
193 void *visit(subrange_specification_c *symbol) { |
|
194 /* if no initial value explicitly given, then use the lowest value of the subrange */ |
|
195 return symbol->subrange->accept(*this); |
|
196 } |
|
197 /* signed_integer DOTDOT signed_integer */ |
|
198 void *visit(subrange_c *symbol) {return symbol->lower_limit;} |
|
199 /* enumerated_type_name ':' enumerated_spec_init */ |
|
200 void *visit(enumerated_type_declaration_c *symbol) { |
|
201 return symbol->enumerated_spec_init->accept(*this); |
|
202 } |
|
203 /* enumerated_specification ASSIGN enumerated_value */ |
|
204 void *visit(enumerated_spec_init_c *symbol) { |
|
205 return handle_type_spec(symbol->enumerated_specification, symbol->enumerated_value); |
|
206 } |
|
207 /* helper symbol for enumerated_specification->enumerated_spec_init */ |
|
208 /* enumerated_value_list ',' enumerated_value */ |
|
209 void *visit(enumerated_value_list_c *symbol) { |
|
210 /* if no initial value explicitly given, then use the lowest value of the subrange */ |
|
211 return (void *)symbol->elements[0]; |
|
212 } |
|
213 /* enumerated_type_name '#' identifier */ |
|
214 // SYM_REF2(enumerated_value_c, type, value) |
|
215 void *visit(enumerated_value_c *symbol) {ERROR; return NULL;} |
|
216 /* identifier ':' array_spec_init */ |
|
217 void *visit(array_type_declaration_c *symbol) { |
|
218 return symbol->array_spec_init->accept(*this); |
|
219 } |
|
220 /* array_specification [ASSIGN array_initialization} */ |
|
221 /* array_initialization may be NULL ! */ |
|
222 void *visit(array_spec_init_c *symbol) { |
|
223 return handle_type_spec(symbol->array_specification, symbol->array_initialization); |
|
224 } |
|
225 /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */ |
|
226 void *visit(array_specification_c *symbol) { |
|
227 symbol_c *init_value = (symbol_c *)symbol->non_generic_type_name->accept(*this); |
|
228 |
|
229 /* Now build a array_initial_elements_list_c list, and populate it |
|
230 * with 1 element of the array_initial_elements_c class |
|
231 */ |
|
232 /* The array_initial_elements_c will contain a reference to the init_value, |
|
233 * and another constant representing the number of elements in the array. |
|
234 * In essence, we are building the equivilant of the following ST/IL code: |
|
235 * New_array_t : ARRAY [1..30, 51..60] of INT := [40(XXX)]; |
|
236 * from the user given code |
|
237 * New_array_t : ARRAY [1..30, 51..60] of INT; |
|
238 * and replacing XXX with the default initial value of INT. |
|
239 */ |
|
240 /* now we need to determine the number of elements in the array... */ |
|
241 /* Easier said than done, as the array may have a list of subranges, as in the |
|
242 * example given above!! |
|
243 */ |
|
244 /* TODO !!!!!*/ |
|
245 /* For now, just assume an array with 1 element. |
|
246 * I (Mario) want to finish off this part of the code before getting boged down |
|
247 * in something else... |
|
248 */ |
|
249 // NOTE: We are leaking memory, as the integer will never get free'd!! |
|
250 integer_c *integer = new integer_c("1"); |
|
251 // NOTE: We are leaking memory, as the array_initial_elements will never get free'd!! |
|
252 array_initial_elements_c *array_initial_elements = new array_initial_elements_c(integer, init_value); |
|
253 // NOTE: We are leaking memory, as the array_initial_elements_list will never get free'd!! |
|
254 array_initial_elements_list_c *array_initial_elements_list = new array_initial_elements_list_c(); |
|
255 array_initial_elements_list->add_element(array_initial_elements); |
|
256 return (void *)array_initial_elements_list; |
|
257 } |
|
258 /* helper symbol for array_specification */ |
|
259 /* array_subrange_list ',' subrange */ |
|
260 void *visit(array_subrange_list_c *symbol) {ERROR; return NULL;} |
|
261 /* array_initialization: '[' array_initial_elements_list ']' */ |
|
262 /* helper symbol for array_initialization */ |
|
263 /* array_initial_elements_list ',' array_initial_elements */ |
|
264 void *visit(array_initial_elements_list_c *symbol) {ERROR; return NULL;} |
|
265 /* integer '(' [array_initial_element] ')' */ |
|
266 /* array_initial_element may be NULL ! */ |
|
267 void *visit(array_initial_elements_c *symbol) {ERROR; return NULL;} |
|
268 |
|
269 |
|
270 |
|
271 /* TODO: from this point forward... */ |
|
272 |
|
273 /* structure_type_name ':' structure_specification */ |
|
274 void *visit(structure_type_declaration_c *symbol) {return NULL;} |
|
275 /* structure_type_name ASSIGN structure_initialization */ |
|
276 /* structure_initialization may be NULL ! */ |
|
277 void *visit(initialized_structure_c *symbol) {return NULL;} |
|
278 /* helper symbol for structure_declaration */ |
|
279 /* structure_declaration: STRUCT structure_element_declaration_list END_STRUCT */ |
|
280 /* structure_element_declaration_list structure_element_declaration ';' */ |
|
281 void *visit(structure_element_declaration_list_c *symbol) {return NULL;} |
|
282 /* structure_element_name ':' *_spec_init */ |
|
283 void *visit(structure_element_declaration_c *symbol) {return NULL;} |
|
284 /* helper symbol for structure_initialization */ |
|
285 /* structure_initialization: '(' structure_element_initialization_list ')' */ |
|
286 /* structure_element_initialization_list ',' structure_element_initialization */ |
|
287 void *visit(structure_element_initialization_list_c *symbol) {return NULL;} |
|
288 /* structure_element_name ASSIGN value */ |
|
289 void *visit(structure_element_initialization_c *symbol) {return NULL;} |
|
290 /* string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */ |
|
291 /* |
|
292 * NOTE: |
|
293 * (Summary: Contrary to what is expected, the |
|
294 * string_type_declaration_c is not used to store |
|
295 * simple string type declarations that do not include |
|
296 * size limits. |
|
297 * For e.g.: |
|
298 * str1_type: STRING := "hello!" |
|
299 * will be stored in a simple_type_declaration_c |
|
300 * instead of a string_type_declaration_c. |
|
301 * The following: |
|
302 * str2_type: STRING [64] := "hello!" |
|
303 * will be stored in a sring_type_declaration_c |
|
304 * |
|
305 * Read on for why this is done... |
|
306 * End Summary) |
|
307 * |
|
308 * According to the spec, the valid construct |
|
309 * TYPE new_str_type : STRING := "hello!"; END_TYPE |
|
310 * has two possible routes to type_declaration... |
|
311 * |
|
312 * Route 1: |
|
313 * type_declaration: single_element_type_declaration |
|
314 * single_element_type_declaration: simple_type_declaration |
|
315 * simple_type_declaration: identifier ':' simple_spec_init |
|
316 * simple_spec_init: simple_specification ASSIGN constant |
|
317 * (shift: identifier <- 'new_str_type') |
|
318 * simple_specification: elementary_type_name |
|
319 * elementary_type_name: STRING |
|
320 * (shift: elementary_type_name <- STRING) |
|
321 * (reduce: simple_specification <- elementary_type_name) |
|
322 * (shift: constant <- "hello!") |
|
323 * (reduce: simple_spec_init: simple_specification ASSIGN constant) |
|
324 * (reduce: ...) |
|
325 * |
|
326 * |
|
327 * Route 2: |
|
328 * type_declaration: string_type_declaration |
|
329 * string_type_declaration: identifier ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init |
|
330 * (shift: identifier <- 'new_str_type') |
|
331 * elementary_string_type_name: STRING |
|
332 * (shift: elementary_string_type_name <- STRING) |
|
333 * (shift: string_type_declaration_size <- empty ) |
|
334 * string_type_declaration_init: ASSIGN character_string |
|
335 * (shift: character_string <- "hello!") |
|
336 * (reduce: string_type_declaration_init <- ASSIGN character_string) |
|
337 * (reduce: string_type_declaration <- identifier ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init ) |
|
338 * (reduce: type_declaration <- string_type_declaration) |
|
339 * |
|
340 * |
|
341 * At first glance it seems that removing route 1 would make |
|
342 * the most sense. Unfortunately the construct 'simple_spec_init' |
|
343 * shows up multiple times in other rules, so changing this construct |
|
344 * would also mean changing all the rules in which it appears. |
|
345 * I (Mario) therefore chose to remove route 2 instead. This means |
|
346 * that the above declaration gets stored in a |
|
347 * simple_type_declaration_c, and not in a string_type_declaration_c |
|
348 * as would be expected! |
|
349 */ |
|
350 /* string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */ |
|
351 #if 0 |
|
352 SYM_REF4(string_type_declaration_c, string_type_name, |
|
353 elementary_string_type_name, |
|
354 string_type_declaration_size, |
|
355 string_type_declaration_init) /* may be == NULL! */ |
|
356 #endif |
|
357 void *visit(string_type_declaration_c *symbol) {return NULL;} |
|
358 }; |
|
359 |
|
360 type_initial_value_c *type_initial_value_c::_instance = NULL; |
|
361 real_c *type_initial_value_c::real_0 = NULL; |
|
362 integer_c *type_initial_value_c::integer_0 = NULL; |
|
363 integer_c *type_initial_value_c::integer_1 = NULL; |
|
364 boolean_literal_c *type_initial_value_c::bool_0 = NULL; |
|
365 date_literal_c *type_initial_value_c::date_literal_0 = NULL; |
|
366 daytime_c *type_initial_value_c::daytime_literal_0 = NULL; |
|
367 duration_c *type_initial_value_c::time_0 = NULL; |
|
368 date_c *type_initial_value_c::date_0 = NULL; |
|
369 time_of_day_c *type_initial_value_c::tod_0 = NULL; |
|
370 date_and_time_c *type_initial_value_c::dt_0 = NULL; |
|
371 single_byte_character_string_c *type_initial_value_c::string_0 = NULL; |
|
372 double_byte_character_string_c *type_initial_value_c::wstring_0 = NULL; |
|
373 |
|
374 |
|
375 |
|
376 |
|
377 |
|
378 |