author | laurent |
Thu, 22 Apr 2010 00:29:30 +0200 | |
changeset 239 | b3063ca418c1 |
parent 221 | c6aed7e5f070 |
child 279 | c0453b7f99df |
permissions | -rwxr-xr-x |
70 | 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 |
* Conversion of type declaration constructs. |
|
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 |
||
38 |
||
39 |
//#include <stdio.h> /* required for NULL */ |
|
40 |
//#include <string> |
|
41 |
//#include <iostream> |
|
42 |
||
43 |
//#include "../../util/symtable.hh" |
|
44 |
||
45 |
||
139
668a54686827
added missing includes on some platform (gentoo/gcc-4.3.1)
etisserant
parents:
123
diff
changeset
|
46 |
#include <stdlib.h> |
70 | 47 |
|
48 |
||
49 |
class generate_c_typedecl_c: public generate_c_base_c { |
|
50 |
||
121 | 51 |
protected: |
52 |
stage4out_c &s4o_incl; |
|
53 |
||
98 | 54 |
private: |
55 |
symbol_c* current_type_name; |
|
56 |
search_base_type_c search_base_type; |
|
57 |
||
121 | 58 |
generate_c_base_c *basedecl; |
59 |
||
70 | 60 |
public: |
121 | 61 |
generate_c_typedecl_c(stage4out_c *s4o_ptr, stage4out_c *s4o_incl_ptr): generate_c_base_c(s4o_ptr), s4o_incl(*s4o_incl_ptr) { |
98 | 62 |
current_typedefinition = none_td; |
63 |
current_basetypedeclaration = none_bd; |
|
121 | 64 |
basedecl = new generate_c_base_c(&s4o_incl); |
98 | 65 |
} |
121 | 66 |
generate_c_typedecl_c(stage4out_c *s4o_ptr): generate_c_base_c(s4o_ptr), s4o_incl(*s4o_ptr) { |
67 |
current_typedefinition = none_td; |
|
68 |
current_basetypedeclaration = none_bd; |
|
69 |
basedecl = new generate_c_base_c(&s4o_incl); |
|
70 |
} |
|
71 |
~generate_c_typedecl_c(void) { |
|
72 |
delete basedecl; |
|
73 |
} |
|
70 | 74 |
|
98 | 75 |
typedef enum { |
76 |
none_td, |
|
77 |
subrange_td, |
|
78 |
array_td |
|
79 |
} typedefinition_t; |
|
80 |
||
81 |
typedefinition_t current_typedefinition; |
|
82 |
||
83 |
typedef enum { |
|
84 |
none_bd, |
|
85 |
subrangebasetype_bd, |
|
86 |
subrangetest_bd, |
|
87 |
arraybasetype_bd, |
|
121 | 88 |
arraybasetypeincl_bd, |
98 | 89 |
arraysubrange_bd, |
90 |
arraytranslateindex_bd |
|
91 |
} basetypedeclaration_t; |
|
92 |
||
93 |
basetypedeclaration_t current_basetypedeclaration; |
|
94 |
||
95 |
int extract_integer(symbol_c *integer) { |
|
96 |
return atoi(((integer_c *)integer)->value); |
|
97 |
} |
|
98 |
||
99 |
void print_integer(unsigned int integer) { |
|
100 |
char str[10]; |
|
101 |
sprintf(str, "%d", integer); |
|
102 |
s4o.print(str); |
|
103 |
} |
|
70 | 104 |
|
121 | 105 |
void print_integer_incl(unsigned int integer) { |
106 |
char str[10]; |
|
107 |
sprintf(str, "%d", integer); |
|
108 |
s4o_incl.print(str); |
|
109 |
} |
|
110 |
||
111 |
void *print_list_incl(list_c *list, |
|
112 |
std::string pre_elem_str = "", |
|
113 |
std::string inter_elem_str = "", |
|
114 |
std::string post_elem_str = "", |
|
115 |
visitor_c *visitor = NULL) { |
|
116 |
if (visitor == NULL) visitor = this; |
|
117 |
||
118 |
if (list->n > 0) { |
|
119 |
//std::cout << "generate_c_base_c::print_list(n = " << list->n << ") 000\n"; |
|
120 |
s4o_incl.print(pre_elem_str); |
|
121 |
list->elements[0]->accept(*visitor); |
|
122 |
} |
|
123 |
||
124 |
for(int i = 1; i < list->n; i++) { |
|
125 |
//std::cout << "generate_c_base_c::print_list " << i << "\n"; |
|
126 |
s4o_incl.print(inter_elem_str); |
|
127 |
list->elements[i]->accept(*visitor); |
|
128 |
} |
|
129 |
||
130 |
if (list->n > 0) |
|
131 |
s4o_incl.print(post_elem_str); |
|
132 |
||
133 |
return NULL; |
|
134 |
} |
|
135 |
||
136 |
||
70 | 137 |
/***************************/ |
138 |
/* B 0 - Programming Model */ |
|
139 |
/***************************/ |
|
140 |
/* leave for derived classes... */ |
|
141 |
||
142 |
/*************************/ |
|
143 |
/* B.1 - Common elements */ |
|
144 |
/*************************/ |
|
145 |
/*******************************************/ |
|
146 |
/* B 1.1 - Letters, digits and identifiers */ |
|
147 |
/*******************************************/ |
|
148 |
/* done in base class(es) */ |
|
149 |
||
150 |
/*********************/ |
|
151 |
/* B 1.2 - Constants */ |
|
152 |
/*********************/ |
|
153 |
/* originally empty... */ |
|
154 |
||
155 |
/******************************/ |
|
156 |
/* B 1.2.1 - Numeric Literals */ |
|
157 |
/******************************/ |
|
158 |
/* done in base class(es) */ |
|
159 |
||
160 |
/*******************************/ |
|
161 |
/* B.1.2.2 Character Strings */ |
|
162 |
/*******************************/ |
|
163 |
/* done in base class(es) */ |
|
164 |
||
165 |
/***************************/ |
|
166 |
/* B 1.2.3 - Time Literals */ |
|
167 |
/***************************/ |
|
168 |
/************************/ |
|
169 |
/* B 1.2.3.1 - Duration */ |
|
170 |
/************************/ |
|
171 |
/* done in base class(es) */ |
|
172 |
||
173 |
/************************************/ |
|
174 |
/* B 1.2.3.2 - Time of day and Date */ |
|
175 |
/************************************/ |
|
176 |
/* done in base class(es) */ |
|
177 |
||
178 |
/**********************/ |
|
179 |
/* B.1.3 - Data types */ |
|
180 |
/**********************/ |
|
181 |
/***********************************/ |
|
182 |
/* B 1.3.1 - Elementary Data Types */ |
|
183 |
/***********************************/ |
|
184 |
/* done in base class(es) */ |
|
185 |
||
186 |
/********************************/ |
|
187 |
/* B.1.3.2 - Generic data types */ |
|
188 |
/********************************/ |
|
189 |
/* originally empty... */ |
|
190 |
||
191 |
/********************************/ |
|
192 |
/* B 1.3.3 - Derived data types */ |
|
193 |
/********************************/ |
|
98 | 194 |
/* subrange_type_name ':' subrange_spec_init */ |
195 |
void *visit(subrange_type_declaration_c *symbol) { |
|
196 |
TRACE("subrange_type_declaration_c"); |
|
197 |
||
221
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
178
diff
changeset
|
198 |
s4o_incl.print("__DECLARE_DERIVED_TYPE("); |
98 | 199 |
current_basetypedeclaration = subrangebasetype_bd; |
200 |
symbol->subrange_spec_init->accept(*this); |
|
201 |
current_basetypedeclaration = none_bd; |
|
221
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
178
diff
changeset
|
202 |
s4o_incl.print(", "); |
121 | 203 |
symbol->subrange_type_name->accept(*basedecl); |
221
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
178
diff
changeset
|
204 |
s4o_incl.print(")\n"); |
98 | 205 |
|
206 |
current_type_name = symbol->subrange_type_name; |
|
207 |
||
208 |
current_basetypedeclaration = subrangetest_bd; |
|
209 |
symbol->subrange_spec_init->accept(*this); |
|
210 |
current_basetypedeclaration = none_bd; |
|
211 |
||
212 |
return NULL; |
|
213 |
} |
|
214 |
||
215 |
/* subrange_specification ASSIGN signed_integer */ |
|
70 | 216 |
void *visit(subrange_spec_init_c *symbol) { |
217 |
TRACE("subrange_spec_init_c"); |
|
98 | 218 |
current_typedefinition = subrange_td; |
219 |
symbol->subrange_specification->accept(*this); |
|
220 |
current_typedefinition = none_td; |
|
221 |
return NULL; |
|
222 |
} |
|
223 |
||
224 |
/* integer_type_name '(' subrange')' */ |
|
225 |
void *visit(subrange_specification_c *symbol) { |
|
226 |
switch (current_basetypedeclaration) { |
|
227 |
case subrangebasetype_bd: |
|
121 | 228 |
symbol->integer_type_name->accept(*basedecl); |
98 | 229 |
break; |
230 |
case subrangetest_bd: |
|
231 |
if (symbol->subrange != NULL) { |
|
232 |
current_type_name->accept(*this); |
|
233 |
s4o.print(" __CHECK_"); |
|
234 |
current_type_name->accept(*this); |
|
235 |
s4o.print("("); |
|
236 |
current_type_name->accept(*this); |
|
237 |
s4o.print(" value) {\n"); |
|
238 |
s4o.indent_right(); |
|
239 |
||
123 | 240 |
if (search_base_type.type_is_subrange(symbol->integer_type_name)) { |
98 | 241 |
s4o.print(s4o.indent_spaces + "value = __CHECK_"); |
242 |
symbol->integer_type_name->accept(*this); |
|
243 |
s4o.print("(value);\n"); |
|
244 |
} |
|
245 |
||
246 |
symbol->subrange->accept(*this); |
|
247 |
||
248 |
s4o.indent_left(); |
|
249 |
s4o.print("}\n"); |
|
250 |
} |
|
251 |
else { |
|
252 |
s4o.print("#define __CHECK_"); |
|
253 |
current_type_name->accept(*this); |
|
254 |
s4o.print(" __CHECK_"); |
|
255 |
symbol->integer_type_name->accept(*this); |
|
256 |
s4o.print("\n"); |
|
257 |
} |
|
258 |
break; |
|
259 |
default: |
|
260 |
break; |
|
261 |
} |
|
262 |
return NULL; |
|
263 |
} |
|
264 |
||
265 |
/* signed_integer DOTDOT signed_integer */ |
|
266 |
void *visit(subrange_c *symbol) { |
|
267 |
int dimension; |
|
268 |
switch (current_typedefinition) { |
|
269 |
case array_td: |
|
270 |
if (current_basetypedeclaration == arraysubrange_bd) { |
|
121 | 271 |
s4o_incl.print("["); |
98 | 272 |
dimension = extract_integer(symbol->upper_limit) - extract_integer(symbol->lower_limit) + 1; |
121 | 273 |
print_integer_incl(dimension); |
274 |
s4o_incl.print("]"); |
|
98 | 275 |
} |
276 |
else |
|
277 |
symbol->lower_limit->accept(*this); |
|
278 |
break; |
|
279 |
case subrange_td: |
|
280 |
s4o.print(s4o.indent_spaces + "if (value < "); |
|
281 |
symbol->lower_limit->accept(*this); |
|
282 |
s4o.print(")\n"); |
|
283 |
s4o.indent_right(); |
|
284 |
s4o.print(s4o.indent_spaces + "return "); |
|
285 |
symbol->lower_limit->accept(*this); |
|
286 |
s4o.print(";\n"); |
|
287 |
s4o.indent_left(); |
|
288 |
s4o.print(s4o.indent_spaces + "else if (value > "); |
|
289 |
symbol->upper_limit->accept(*this); |
|
290 |
s4o.print(")\n"); |
|
291 |
s4o.indent_right(); |
|
292 |
s4o.print(s4o.indent_spaces + "return "); |
|
293 |
symbol->upper_limit->accept(*this); |
|
294 |
s4o.print(";\n"); |
|
295 |
s4o.indent_left(); |
|
296 |
s4o.print(s4o.indent_spaces + "else\n"); |
|
297 |
s4o.indent_right(); |
|
298 |
s4o.print(s4o.indent_spaces + "return value;\n"); |
|
299 |
s4o.indent_left(); |
|
300 |
default: |
|
301 |
break; |
|
302 |
} |
|
303 |
return NULL; |
|
304 |
} |
|
305 |
||
306 |
/* enumerated_type_name ':' enumerated_spec_init */ |
|
307 |
void *visit(enumerated_type_declaration_c *symbol) { |
|
308 |
TRACE("enumerated_type_declaration_c"); |
|
309 |
||
121 | 310 |
s4o_incl.print("typedef enum {\n"); |
311 |
s4o_incl.indent_right(); |
|
98 | 312 |
symbol->enumerated_spec_init->accept(*this); |
121 | 313 |
s4o_incl.indent_left(); |
314 |
s4o_incl.print("} "); |
|
315 |
symbol->enumerated_type_name->accept(*basedecl); |
|
316 |
s4o_incl.print(";\n"); |
|
70 | 317 |
return NULL; |
318 |
} |
|
319 |
||
320 |
void *visit(enumerated_spec_init_c *symbol) { |
|
321 |
TRACE("enumerated_spec_init_c"); |
|
98 | 322 |
symbol->enumerated_specification->accept(*this); |
323 |
return NULL; |
|
324 |
} |
|
325 |
||
326 |
/* helper symbol for enumerated_specification->enumerated_spec_init */ |
|
327 |
/* enumerated_value_list ',' enumerated_value */ |
|
328 |
void *visit(enumerated_value_list_c *symbol) { |
|
121 | 329 |
print_list_incl(symbol, s4o_incl.indent_spaces, ",\n"+s4o_incl.indent_spaces, "\n"); |
98 | 330 |
return NULL; |
331 |
} |
|
332 |
||
333 |
/* enumerated_type_name '#' identifier */ |
|
334 |
void *visit(enumerated_value_c *symbol) { |
|
121 | 335 |
symbol->value->accept(*basedecl); |
98 | 336 |
return NULL; |
337 |
} |
|
338 |
||
339 |
/* identifier ':' array_spec_init */ |
|
340 |
void *visit(array_type_declaration_c *symbol) { |
|
341 |
TRACE("array_type_declaration_c"); |
|
342 |
||
221
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
178
diff
changeset
|
343 |
s4o_incl.print("__DECLARE_ARRAY_TYPE("); |
121 | 344 |
current_basetypedeclaration = arraybasetypeincl_bd; |
98 | 345 |
symbol->array_spec_init->accept(*this); |
346 |
current_basetypedeclaration = none_bd; |
|
221
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
178
diff
changeset
|
347 |
s4o_incl.print(","); |
121 | 348 |
symbol->identifier->accept(*basedecl); |
221
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
178
diff
changeset
|
349 |
s4o_incl.print(","); |
98 | 350 |
current_basetypedeclaration = arraysubrange_bd; |
351 |
symbol->array_spec_init->accept(*this); |
|
352 |
current_basetypedeclaration = none_bd; |
|
221
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
178
diff
changeset
|
353 |
s4o_incl.print(")\n"); |
98 | 354 |
|
174 | 355 |
if (search_base_type.type_is_subrange(symbol->identifier)) { |
98 | 356 |
s4o.print("#define __CHECK_"); |
357 |
symbol->identifier->accept(*this); |
|
358 |
s4o.print(" __CHECK_"); |
|
359 |
current_basetypedeclaration = arraybasetype_bd; |
|
360 |
symbol->array_spec_init->accept(*this); |
|
361 |
current_basetypedeclaration = none_bd; |
|
362 |
s4o.print("\n"); |
|
363 |
} |
|
364 |
||
365 |
current_type_name = symbol->identifier; |
|
366 |
current_basetypedeclaration = arraytranslateindex_bd; |
|
367 |
symbol->array_spec_init->accept(*this); |
|
368 |
current_basetypedeclaration = none_bd; |
|
369 |
s4o.print("\n"); |
|
370 |
||
371 |
return NULL; |
|
372 |
} |
|
373 |
||
160 | 374 |
/* array_specification [ASSIGN array_initialization] */ |
98 | 375 |
/* array_initialization may be NULL ! */ |
376 |
void *visit(array_spec_init_c *symbol) { |
|
377 |
TRACE("array_spec_init_c"); |
|
160 | 378 |
|
379 |
identifier_c *array_type_name = dynamic_cast<identifier_c *>(symbol->array_specification); |
|
380 |
||
381 |
if (array_type_name == NULL) { |
|
382 |
current_typedefinition = array_td; |
|
383 |
symbol->array_specification->accept(*this); |
|
384 |
current_typedefinition = none_td; |
|
385 |
} |
|
386 |
else |
|
387 |
symbol->array_specification->accept(*basedecl); |
|
98 | 388 |
return NULL; |
389 |
} |
|
390 |
||
391 |
/* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */ |
|
392 |
void *visit(array_specification_c *symbol) { |
|
393 |
switch (current_basetypedeclaration) { |
|
394 |
case arraybasetype_bd: |
|
395 |
symbol->non_generic_type_name->accept(*this); |
|
396 |
break; |
|
121 | 397 |
case arraybasetypeincl_bd: |
398 |
symbol->non_generic_type_name->accept(*basedecl); |
|
399 |
break; |
|
98 | 400 |
case arraysubrange_bd: |
401 |
case arraytranslateindex_bd: |
|
402 |
symbol->array_subrange_list->accept(*this); |
|
403 |
break; |
|
404 |
default: |
|
405 |
break; |
|
406 |
} |
|
407 |
return NULL; |
|
408 |
} |
|
409 |
||
410 |
/* helper symbol for array_specification */ |
|
411 |
/* array_subrange_list ',' subrange */ |
|
412 |
void *visit(array_subrange_list_c *symbol) { |
|
413 |
if (current_basetypedeclaration == arraytranslateindex_bd) { |
|
414 |
for (int i = 0; i < symbol->n; i++) { |
|
415 |
s4o.print("#define __"); |
|
416 |
current_type_name->accept(*this); |
|
417 |
s4o.print("_TRANSIDX"); |
|
418 |
print_integer(i); |
|
419 |
s4o.print("(index) (index) - "); |
|
420 |
symbol->elements[i]->accept(*this); |
|
421 |
s4o.print("\n"); |
|
422 |
} |
|
423 |
} |
|
424 |
else |
|
425 |
print_list(symbol); |
|
70 | 426 |
return NULL; |
427 |
} |
|
428 |
||
429 |
/* TYPE type_declaration_list END_TYPE */ |
|
430 |
void *visit(data_type_declaration_c *symbol) { |
|
431 |
TRACE("data_type_declaration_c"); |
|
432 |
symbol->type_declaration_list->accept(*this); |
|
433 |
s4o.print("\n\n"); |
|
434 |
return NULL; |
|
435 |
} |
|
436 |
||
437 |
/* helper symbol for data_type_declaration */ |
|
438 |
void *visit(type_declaration_list_c *symbol) { |
|
439 |
TRACE("type_declaration_list_c"); |
|
160 | 440 |
return print_list_incl(symbol, "", "\n", "\n"); |
70 | 441 |
} |
442 |
||
443 |
/* simple_type_name ':' simple_spec_init */ |
|
444 |
void *visit(simple_type_declaration_c *symbol) { |
|
445 |
TRACE("simple_type_declaration_c"); |
|
446 |
||
221
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
178
diff
changeset
|
447 |
s4o_incl.print("__DECLARE_DERIVED_TYPE"); |
70 | 448 |
symbol->simple_spec_init->accept(*this); |
221
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
178
diff
changeset
|
449 |
s4o_incl.print(","); |
121 | 450 |
symbol->simple_type_name->accept(*basedecl); |
221
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
178
diff
changeset
|
451 |
s4o_incl.print(");\n"); |
70 | 452 |
return NULL; |
453 |
} |
|
454 |
||
455 |
/* simple_specification [ASSIGN constant] */ |
|
456 |
//SYM_REF2(simple_spec_init_c, simple_specification, constant) |
|
457 |
// <constant> may be NULL |
|
458 |
void *visit(simple_spec_init_c *symbol) { |
|
459 |
TRACE("simple_spec_init_c"); |
|
121 | 460 |
symbol->simple_specification->accept(*basedecl); |
70 | 461 |
return NULL; |
462 |
} |
|
463 |
||
464 |
#if 0 |
|
465 |
/* subrange_type_name ':' subrange_spec_init */ |
|
466 |
SYM_REF2(subrange_type_declaration_c, subrange_type_name, subrange_spec_init) |
|
467 |
||
468 |
/* subrange_specification ASSIGN signed_integer */ |
|
469 |
SYM_REF2(subrange_spec_init_c, subrange_specification, signed_integer) |
|
470 |
||
471 |
/* integer_type_name '(' subrange')' */ |
|
472 |
SYM_REF2(subrange_specification_c, integer_type_name, subrange) |
|
473 |
||
474 |
/* signed_integer DOTDOT signed_integer */ |
|
475 |
SYM_REF2(subrange_c, lower_limit, upper_limit) |
|
476 |
||
477 |
/* enumerated_type_name ':' enumerated_spec_init */ |
|
478 |
SYM_REF2(enumerated_type_declaration_c, enumerated_type_name, enumerated_spec_init) |
|
479 |
||
480 |
/* enumerated_specification ASSIGN enumerated_value */ |
|
481 |
SYM_REF2(enumerated_spec_init_c, enumerated_specification, enumerated_value) |
|
482 |
||
483 |
/* helper symbol for enumerated_specification->enumerated_spec_init */ |
|
484 |
/* enumerated_value_list ',' enumerated_value */ |
|
485 |
SYM_LIST(enumerated_value_list_c) |
|
486 |
||
487 |
/* enumerated_type_name '#' identifier */ |
|
488 |
SYM_REF2(enumerated_value_c, type, value) |
|
489 |
||
490 |
/* identifier ':' array_spec_init */ |
|
491 |
SYM_REF2(array_type_declaration_c, identifier, array_spec_init) |
|
492 |
||
493 |
/* array_specification [ASSIGN array_initialization} */ |
|
494 |
/* array_initialization may be NULL ! */ |
|
495 |
SYM_REF2(array_spec_init_c, array_specification, array_initialization) |
|
496 |
||
497 |
/* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */ |
|
498 |
SYM_REF2(array_specification_c, array_subrange_list, non_generic_type_name) |
|
499 |
||
500 |
/* helper symbol for array_specification */ |
|
501 |
/* array_subrange_list ',' subrange */ |
|
502 |
SYM_LIST(array_subrange_list_c) |
|
503 |
||
504 |
/* array_initialization: '[' array_initial_elements_list ']' */ |
|
505 |
/* helper symbol for array_initialization */ |
|
506 |
/* array_initial_elements_list ',' array_initial_elements */ |
|
507 |
SYM_LIST(array_initial_elements_list_c) |
|
508 |
||
509 |
/* integer '(' [array_initial_element] ')' */ |
|
510 |
/* array_initial_element may be NULL ! */ |
|
511 |
SYM_REF2(array_initial_elements_c, integer, array_initial_element) |
|
512 |
#endif |
|
513 |
||
514 |
/* structure_type_name ':' structure_specification */ |
|
515 |
//SYM_REF2(structure_type_declaration_c, structure_type_name, structure_specification) |
|
516 |
void *visit(structure_type_declaration_c *symbol) { |
|
517 |
TRACE("structure_type_declaration_c"); |
|
518 |
||
221
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
178
diff
changeset
|
519 |
s4o_incl.print("__DECLARE_STRUCT_TYPE("); |
70 | 520 |
symbol->structure_specification->accept(*this); |
221
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
178
diff
changeset
|
521 |
s4o_incl.print(","); |
121 | 522 |
symbol->structure_type_name->accept(*basedecl); |
221
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
178
diff
changeset
|
523 |
s4o_incl.print(");\n"); |
70 | 524 |
return NULL; |
525 |
} |
|
526 |
||
527 |
/* structure_type_name ASSIGN structure_initialization */ |
|
528 |
/* structure_initialization may be NULL ! */ |
|
529 |
//SYM_REF2(initialized_structure_c, structure_type_name, structure_initialization) |
|
530 |
void *visit(initialized_structure_c *symbol) { |
|
531 |
TRACE("initialized_structure_c"); |
|
164 | 532 |
|
533 |
symbol->structure_type_name->accept(*basedecl); |
|
534 |
||
70 | 535 |
return NULL; |
536 |
} |
|
537 |
||
538 |
/* helper symbol for structure_declaration */ |
|
539 |
/* structure_declaration: STRUCT structure_element_declaration_list END_STRUCT */ |
|
540 |
/* structure_element_declaration_list structure_element_declaration ';' */ |
|
541 |
//SYM_LIST(structure_element_declaration_list_c) |
|
542 |
void *visit(structure_element_declaration_list_c *symbol) { |
|
543 |
TRACE("structure_element_declaration_list_c"); |
|
160 | 544 |
s4o_incl.print("struct {\n"); |
545 |
s4o_incl.indent_right(); |
|
546 |
s4o_incl.print(s4o_incl.indent_spaces); |
|
547 |
||
548 |
print_list_incl(symbol, "", s4o_incl.indent_spaces, ""); |
|
549 |
||
550 |
s4o_incl.indent_left(); |
|
551 |
s4o_incl.print(s4o_incl.indent_spaces); |
|
552 |
s4o_incl.print("}"); |
|
70 | 553 |
return NULL; |
554 |
} |
|
555 |
||
556 |
/* structure_element_name ':' spec_init */ |
|
557 |
//SYM_REF2(structure_element_declaration_c, structure_element_name, spec_init) |
|
558 |
void *visit(structure_element_declaration_c *symbol) { |
|
559 |
TRACE("structure_element_declaration_c"); |
|
560 |
||
561 |
symbol->spec_init->accept(*this); |
|
160 | 562 |
s4o_incl.print(" "); |
563 |
symbol->structure_element_name->accept(*basedecl); |
|
564 |
s4o_incl.print(";\n"); |
|
565 |
s4o_incl.print(s4o.indent_spaces); |
|
70 | 566 |
|
567 |
return NULL; |
|
568 |
} |
|
569 |
||
570 |
/* helper symbol for structure_initialization */ |
|
571 |
/* structure_initialization: '(' structure_element_initialization_list ')' */ |
|
572 |
/* structure_element_initialization_list ',' structure_element_initialization */ |
|
573 |
//SYM_LIST(structure_element_initialization_list_c) |
|
574 |
void *visit(structure_element_initialization_list_c *symbol) { |
|
575 |
TRACE("structure_element_initialization_list_c"); |
|
576 |
||
577 |
// TODO ??? |
|
160 | 578 |
//ERROR; |
70 | 579 |
return NULL; |
580 |
} |
|
581 |
||
582 |
/* structure_element_name ASSIGN value */ |
|
583 |
//SYM_REF2(structure_element_initialization_c, structure_element_name, value) |
|
584 |
void *visit(structure_element_initialization_c *symbol) { |
|
585 |
TRACE("structure_element_initialization_c"); |
|
586 |
||
587 |
// TODO ??? |
|
160 | 588 |
//ERROR; |
70 | 589 |
return NULL; |
590 |
} |
|
591 |
||
592 |
#if 0 |
|
593 |
/* string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */ |
|
594 |
/* |
|
595 |
* NOTE: |
|
596 |
* (Summary: Contrary to what is expected, the |
|
597 |
* string_type_declaration_c is not used to store |
|
598 |
* simple string type declarations that do not include |
|
599 |
* size limits. |
|
600 |
* For e.g.: |
|
601 |
* str1_type: STRING := "hello!" |
|
602 |
* will be stored in a simple_type_declaration_c |
|
603 |
* instead of a string_type_declaration_c. |
|
604 |
* The following: |
|
605 |
* str2_type: STRING [64] := "hello!" |
|
606 |
* will be stored in a sring_type_declaration_c |
|
607 |
* |
|
608 |
* Read on for why this is done... |
|
609 |
* End Summary) |
|
610 |
* |
|
611 |
* According to the spec, the valid construct |
|
612 |
* TYPE new_str_type : STRING := "hello!"; END_TYPE |
|
613 |
* has two possible routes to type_declaration... |
|
614 |
* |
|
615 |
* Route 1: |
|
616 |
* type_declaration: single_element_type_declaration |
|
617 |
* single_element_type_declaration: simple_type_declaration |
|
618 |
* simple_type_declaration: identifier ':' simple_spec_init |
|
619 |
* simple_spec_init: simple_specification ASSIGN constant |
|
620 |
* (shift: identifier <- 'new_str_type') |
|
621 |
* simple_specification: elementary_type_name |
|
622 |
* elementary_type_name: STRING |
|
623 |
* (shift: elementary_type_name <- STRING) |
|
624 |
* (reduce: simple_specification <- elementary_type_name) |
|
625 |
* (shift: constant <- "hello!") |
|
626 |
* (reduce: simple_spec_init: simple_specification ASSIGN constant) |
|
627 |
* (reduce: ...) |
|
628 |
* |
|
629 |
* |
|
630 |
* Route 2: |
|
631 |
* type_declaration: string_type_declaration |
|
632 |
* string_type_declaration: identifier ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init |
|
633 |
* (shift: identifier <- 'new_str_type') |
|
634 |
* elementary_string_type_name: STRING |
|
635 |
* (shift: elementary_string_type_name <- STRING) |
|
636 |
* (shift: string_type_declaration_size <- empty ) |
|
637 |
* string_type_declaration_init: ASSIGN character_string |
|
638 |
* (shift: character_string <- "hello!") |
|
639 |
* (reduce: string_type_declaration_init <- ASSIGN character_string) |
|
640 |
* (reduce: string_type_declaration <- identifier ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init ) |
|
641 |
* (reduce: type_declaration <- string_type_declaration) |
|
642 |
* |
|
643 |
* |
|
644 |
* At first glance it seems that removing route 1 would make |
|
645 |
* the most sense. Unfortunately the construct 'simple_spec_init' |
|
646 |
* shows up multiple times in other rules, so changing this construct |
|
647 |
* would also mean changing all the rules in which it appears. |
|
648 |
* I (Mario) therefore chose to remove route 2 instead. This means |
|
649 |
* that the above declaration gets stored in a |
|
650 |
* simple_type_declaration_c, and not in a string_type_declaration_c |
|
651 |
* as would be expected! |
|
652 |
*/ |
|
653 |
/* string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */ |
|
654 |
SYM_REF4(string_type_declaration_c, string_type_name, |
|
655 |
elementary_string_type_name, |
|
656 |
string_type_declaration_size, |
|
657 |
string_type_declaration_init) /* may be == NULL! */ |
|
658 |
#endif |
|
659 |
||
660 |
/*********************/ |
|
661 |
/* B 1.4 - Variables */ |
|
662 |
/*********************/ |
|
663 |
/* done in base class(es) */ |
|
664 |
||
665 |
/********************************************/ |
|
666 |
/* B.1.4.1 Directly Represented Variables */ |
|
667 |
/********************************************/ |
|
668 |
// direct_variable: direct_variable_token {$$ = new direct_variable_c($1);}; |
|
669 |
void *visit(direct_variable_c *symbol) { |
|
670 |
TRACE("direct_variable_c"); |
|
671 |
/* Do not use print_token() as it will change everything into uppercase */ |
|
672 |
if (strlen(symbol->value) == 0) ERROR; |
|
673 |
return s4o.printlocation(symbol->value + 1); |
|
674 |
} |
|
675 |
||
676 |
||
677 |
/*************************************/ |
|
678 |
/* B.1.4.2 Multi-element Variables */ |
|
679 |
/*************************************/ |
|
680 |
/* done in base class(es) */ |
|
681 |
||
682 |
/******************************************/ |
|
683 |
/* B 1.4.3 - Declaration & Initialisation */ |
|
684 |
/******************************************/ |
|
685 |
/* leave for derived classes... */ |
|
686 |
||
687 |
/**************************************/ |
|
688 |
/* B.1.5 - Program organization units */ |
|
689 |
/**************************************/ |
|
690 |
/***********************/ |
|
691 |
/* B 1.5.1 - Functions */ |
|
692 |
/***********************/ |
|
693 |
/* leave for derived classes... */ |
|
694 |
||
695 |
/*****************************/ |
|
696 |
/* B 1.5.2 - Function Blocks */ |
|
697 |
/*****************************/ |
|
698 |
/* leave for derived classes... */ |
|
699 |
||
700 |
/**********************/ |
|
701 |
/* B 1.5.3 - Programs */ |
|
702 |
/**********************/ |
|
703 |
/* leave for derived classes... */ |
|
704 |
||
705 |
/*********************************************/ |
|
706 |
/* B.1.6 Sequential function chart elements */ |
|
707 |
/*********************************************/ |
|
708 |
||
709 |
/********************************/ |
|
710 |
/* B 1.7 Configuration elements */ |
|
711 |
/********************************/ |
|
712 |
/* leave for derived classes... */ |
|
713 |
||
714 |
/****************************************/ |
|
715 |
/* B.2 - Language IL (Instruction List) */ |
|
716 |
/****************************************/ |
|
717 |
/***********************************/ |
|
718 |
/* B 2.1 Instructions and Operands */ |
|
719 |
/***********************************/ |
|
720 |
/* leave for derived classes... */ |
|
721 |
||
722 |
/*******************/ |
|
723 |
/* B 2.2 Operators */ |
|
724 |
/*******************/ |
|
725 |
/* leave for derived classes... */ |
|
726 |
||
727 |
||
728 |
/***************************************/ |
|
729 |
/* B.3 - Language ST (Structured Text) */ |
|
730 |
/***************************************/ |
|
731 |
/***********************/ |
|
732 |
/* B 3.1 - Expressions */ |
|
733 |
/***********************/ |
|
734 |
/* leave for derived classes... */ |
|
735 |
||
736 |
/********************/ |
|
737 |
/* B 3.2 Statements */ |
|
738 |
/********************/ |
|
739 |
/* leave for derived classes... */ |
|
740 |
||
741 |
/*********************************/ |
|
742 |
/* B 3.2.1 Assignment Statements */ |
|
743 |
/*********************************/ |
|
744 |
/* leave for derived classes... */ |
|
745 |
||
746 |
/*****************************************/ |
|
747 |
/* B 3.2.2 Subprogram Control Statements */ |
|
748 |
/*****************************************/ |
|
749 |
/* leave for derived classes... */ |
|
750 |
||
751 |
/********************************/ |
|
752 |
/* B 3.2.3 Selection Statements */ |
|
753 |
/********************************/ |
|
754 |
/* leave for derived classes... */ |
|
755 |
||
756 |
/********************************/ |
|
757 |
/* B 3.2.4 Iteration Statements */ |
|
758 |
/********************************/ |
|
759 |
/* leave for derived classes... */ |
|
760 |
||
761 |
||
762 |
||
763 |
||
764 |
}; /* generate_c_typedecl_c */ |
|
765 |
||
766 |
||
767 |