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