46 |
46 |
47 |
47 |
48 |
48 |
49 class generate_c_typedecl_c: public generate_c_base_c { |
49 class generate_c_typedecl_c: public generate_c_base_c { |
50 |
50 |
|
51 protected: |
|
52 stage4out_c &s4o_incl; |
|
53 |
51 private: |
54 private: |
52 symbol_c* current_type_name; |
55 symbol_c* current_type_name; |
53 search_base_type_c search_base_type; |
56 search_base_type_c search_base_type; |
54 |
57 |
|
58 generate_c_base_c *basedecl; |
|
59 |
55 public: |
60 public: |
56 generate_c_typedecl_c(stage4out_c *s4o_ptr): generate_c_base_c(s4o_ptr) { |
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) { |
57 current_typedefinition = none_td; |
62 current_typedefinition = none_td; |
58 current_basetypedeclaration = none_bd; |
63 current_basetypedeclaration = none_bd; |
|
64 basedecl = new generate_c_base_c(&s4o_incl); |
59 } |
65 } |
60 ~generate_c_typedecl_c(void) {} |
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 } |
61 |
74 |
62 typedef enum { |
75 typedef enum { |
63 none_td, |
76 none_td, |
64 subrange_td, |
77 subrange_td, |
65 array_td |
78 array_td |
86 void print_integer(unsigned int integer) { |
100 void print_integer(unsigned int integer) { |
87 char str[10]; |
101 char str[10]; |
88 sprintf(str, "%d", integer); |
102 sprintf(str, "%d", integer); |
89 s4o.print(str); |
103 s4o.print(str); |
90 } |
104 } |
|
105 |
|
106 void print_integer_incl(unsigned int integer) { |
|
107 char str[10]; |
|
108 sprintf(str, "%d", integer); |
|
109 s4o_incl.print(str); |
|
110 } |
|
111 |
|
112 void *print_list_incl(list_c *list, |
|
113 std::string pre_elem_str = "", |
|
114 std::string inter_elem_str = "", |
|
115 std::string post_elem_str = "", |
|
116 visitor_c *visitor = NULL) { |
|
117 if (visitor == NULL) visitor = this; |
|
118 |
|
119 if (list->n > 0) { |
|
120 //std::cout << "generate_c_base_c::print_list(n = " << list->n << ") 000\n"; |
|
121 s4o_incl.print(pre_elem_str); |
|
122 list->elements[0]->accept(*visitor); |
|
123 } |
|
124 |
|
125 for(int i = 1; i < list->n; i++) { |
|
126 //std::cout << "generate_c_base_c::print_list " << i << "\n"; |
|
127 s4o_incl.print(inter_elem_str); |
|
128 list->elements[i]->accept(*visitor); |
|
129 } |
|
130 |
|
131 if (list->n > 0) |
|
132 s4o_incl.print(post_elem_str); |
|
133 |
|
134 return NULL; |
|
135 } |
|
136 |
91 |
137 |
92 /***************************/ |
138 /***************************/ |
93 /* B 0 - Programming Model */ |
139 /* B 0 - Programming Model */ |
94 /***************************/ |
140 /***************************/ |
95 /* leave for derived classes... */ |
141 /* leave for derived classes... */ |
150 void *visit(subrange_type_declaration_c *symbol) { |
196 void *visit(subrange_type_declaration_c *symbol) { |
151 TRACE("subrange_type_declaration_c"); |
197 TRACE("subrange_type_declaration_c"); |
152 /* add this type declaration to the type symbol table... */ |
198 /* add this type declaration to the type symbol table... */ |
153 type_symtable.insert(symbol->subrange_type_name, symbol->subrange_spec_init); |
199 type_symtable.insert(symbol->subrange_type_name, symbol->subrange_spec_init); |
154 |
200 |
155 s4o.print("typedef "); |
201 s4o_incl.print("typedef "); |
156 current_basetypedeclaration = subrangebasetype_bd; |
202 current_basetypedeclaration = subrangebasetype_bd; |
157 symbol->subrange_spec_init->accept(*this); |
203 symbol->subrange_spec_init->accept(*this); |
158 current_basetypedeclaration = none_bd; |
204 current_basetypedeclaration = none_bd; |
159 s4o.print(" "); |
205 s4o_incl.print(" "); |
160 symbol->subrange_type_name->accept(*this); |
206 symbol->subrange_type_name->accept(*basedecl); |
161 s4o.print(";\n\n"); |
207 s4o_incl.print(";\n\n"); |
162 |
208 |
163 current_basetypedeclaration = subrangebasetypeexploration_bd; |
209 current_basetypedeclaration = subrangebasetypeexploration_bd; |
164 symbol->subrange_spec_init->accept(*this); |
210 symbol->subrange_spec_init->accept(*this); |
165 current_basetypedeclaration = none_bd; |
211 current_basetypedeclaration = none_bd; |
166 |
212 |
184 |
230 |
185 /* integer_type_name '(' subrange')' */ |
231 /* integer_type_name '(' subrange')' */ |
186 void *visit(subrange_specification_c *symbol) { |
232 void *visit(subrange_specification_c *symbol) { |
187 switch (current_basetypedeclaration) { |
233 switch (current_basetypedeclaration) { |
188 case subrangebasetype_bd: |
234 case subrangebasetype_bd: |
189 symbol->integer_type_name->accept(*this); |
235 symbol->integer_type_name->accept(*basedecl); |
190 break; |
236 break; |
191 case subrangebasetypeexploration_bd: |
237 case subrangebasetypeexploration_bd: |
192 search_base_type.explore_type(symbol->integer_type_name); |
238 search_base_type.explore_type(symbol->integer_type_name); |
193 break; |
239 break; |
194 case subrangetest_bd: |
240 case subrangetest_bd: |
230 void *visit(subrange_c *symbol) { |
276 void *visit(subrange_c *symbol) { |
231 int dimension; |
277 int dimension; |
232 switch (current_typedefinition) { |
278 switch (current_typedefinition) { |
233 case array_td: |
279 case array_td: |
234 if (current_basetypedeclaration == arraysubrange_bd) { |
280 if (current_basetypedeclaration == arraysubrange_bd) { |
235 s4o.print("["); |
281 s4o_incl.print("["); |
236 dimension = extract_integer(symbol->upper_limit) - extract_integer(symbol->lower_limit) + 1; |
282 dimension = extract_integer(symbol->upper_limit) - extract_integer(symbol->lower_limit) + 1; |
237 print_integer(dimension); |
283 print_integer_incl(dimension); |
238 s4o.print("]"); |
284 s4o_incl.print("]"); |
239 } |
285 } |
240 else |
286 else |
241 symbol->lower_limit->accept(*this); |
287 symbol->lower_limit->accept(*this); |
242 break; |
288 break; |
243 case subrange_td: |
289 case subrange_td: |
271 void *visit(enumerated_type_declaration_c *symbol) { |
317 void *visit(enumerated_type_declaration_c *symbol) { |
272 TRACE("enumerated_type_declaration_c"); |
318 TRACE("enumerated_type_declaration_c"); |
273 /* add this type declaration to the type symbol table... */ |
319 /* add this type declaration to the type symbol table... */ |
274 type_symtable.insert(symbol->enumerated_type_name, symbol->enumerated_spec_init); |
320 type_symtable.insert(symbol->enumerated_type_name, symbol->enumerated_spec_init); |
275 |
321 |
276 s4o.print("typedef enum {\n"); |
322 s4o_incl.print("typedef enum {\n"); |
277 s4o.indent_right(); |
323 s4o_incl.indent_right(); |
278 symbol->enumerated_spec_init->accept(*this); |
324 symbol->enumerated_spec_init->accept(*this); |
279 s4o.indent_left(); |
325 s4o_incl.indent_left(); |
280 s4o.print("} "); |
326 s4o_incl.print("} "); |
281 symbol->enumerated_type_name->accept(*this); |
327 symbol->enumerated_type_name->accept(*basedecl); |
282 s4o.print(";\n"); |
328 s4o_incl.print(";\n"); |
283 return NULL; |
329 return NULL; |
284 } |
330 } |
285 |
331 |
286 void *visit(enumerated_spec_init_c *symbol) { |
332 void *visit(enumerated_spec_init_c *symbol) { |
287 TRACE("enumerated_spec_init_c"); |
333 TRACE("enumerated_spec_init_c"); |
290 } |
336 } |
291 |
337 |
292 /* helper symbol for enumerated_specification->enumerated_spec_init */ |
338 /* helper symbol for enumerated_specification->enumerated_spec_init */ |
293 /* enumerated_value_list ',' enumerated_value */ |
339 /* enumerated_value_list ',' enumerated_value */ |
294 void *visit(enumerated_value_list_c *symbol) { |
340 void *visit(enumerated_value_list_c *symbol) { |
295 print_list(symbol, s4o.indent_spaces, ",\n"+s4o.indent_spaces, "\n"); |
341 print_list_incl(symbol, s4o_incl.indent_spaces, ",\n"+s4o_incl.indent_spaces, "\n"); |
296 return NULL; |
342 return NULL; |
297 } |
343 } |
298 |
344 |
299 /* enumerated_type_name '#' identifier */ |
345 /* enumerated_type_name '#' identifier */ |
300 void *visit(enumerated_value_c *symbol) { |
346 void *visit(enumerated_value_c *symbol) { |
301 symbol->value->accept(*this); |
347 symbol->value->accept(*basedecl); |
302 return NULL; |
348 return NULL; |
303 } |
349 } |
304 |
350 |
305 /* identifier ':' array_spec_init */ |
351 /* identifier ':' array_spec_init */ |
306 void *visit(array_type_declaration_c *symbol) { |
352 void *visit(array_type_declaration_c *symbol) { |
307 TRACE("array_type_declaration_c"); |
353 TRACE("array_type_declaration_c"); |
308 /* add this type declaration to the type symbol table... */ |
354 /* add this type declaration to the type symbol table... */ |
309 type_symtable.insert(symbol->identifier, symbol->array_spec_init); |
355 type_symtable.insert(symbol->identifier, symbol->array_spec_init); |
310 |
356 |
311 s4o.print("typedef "); |
357 s4o_incl.print("typedef "); |
312 current_basetypedeclaration = arraybasetype_bd; |
358 current_basetypedeclaration = arraybasetypeincl_bd; |
313 symbol->array_spec_init->accept(*this); |
359 symbol->array_spec_init->accept(*this); |
314 current_basetypedeclaration = none_bd; |
360 current_basetypedeclaration = none_bd; |
315 s4o.print(" "); |
361 s4o_incl.print(" "); |
316 symbol->identifier->accept(*this); |
362 symbol->identifier->accept(*basedecl); |
317 current_basetypedeclaration = arraysubrange_bd; |
363 current_basetypedeclaration = arraysubrange_bd; |
318 symbol->array_spec_init->accept(*this); |
364 symbol->array_spec_init->accept(*this); |
319 current_basetypedeclaration = none_bd; |
365 current_basetypedeclaration = none_bd; |
320 s4o.print(";\n"); |
366 s4o_incl.print(";\n"); |
321 |
367 |
322 search_base_type.explore_type(symbol->array_spec_init); |
368 search_base_type.explore_type(symbol->array_spec_init); |
323 if (search_base_type.base_is_subrange()) { |
369 if (search_base_type.base_is_subrange()) { |
324 s4o.print("#define __CHECK_"); |
370 s4o.print("#define __CHECK_"); |
325 symbol->identifier->accept(*this); |
371 symbol->identifier->accept(*this); |
352 /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */ |
398 /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */ |
353 void *visit(array_specification_c *symbol) { |
399 void *visit(array_specification_c *symbol) { |
354 switch (current_basetypedeclaration) { |
400 switch (current_basetypedeclaration) { |
355 case arraybasetype_bd: |
401 case arraybasetype_bd: |
356 symbol->non_generic_type_name->accept(*this); |
402 symbol->non_generic_type_name->accept(*this); |
|
403 break; |
|
404 case arraybasetypeincl_bd: |
|
405 symbol->non_generic_type_name->accept(*basedecl); |
357 break; |
406 break; |
358 case arraysubrange_bd: |
407 case arraysubrange_bd: |
359 case arraytranslateindex_bd: |
408 case arraytranslateindex_bd: |
360 symbol->array_subrange_list->accept(*this); |
409 symbol->array_subrange_list->accept(*this); |
361 break; |
410 break; |
402 void *visit(simple_type_declaration_c *symbol) { |
451 void *visit(simple_type_declaration_c *symbol) { |
403 TRACE("simple_type_declaration_c"); |
452 TRACE("simple_type_declaration_c"); |
404 /* add this type declaration to the type symbol table... */ |
453 /* add this type declaration to the type symbol table... */ |
405 type_symtable.insert(symbol->simple_type_name, symbol->simple_spec_init); |
454 type_symtable.insert(symbol->simple_type_name, symbol->simple_spec_init); |
406 |
455 |
407 s4o.print("typedef "); |
456 s4o_incl.print("typedef "); |
408 symbol->simple_spec_init->accept(*this); |
457 symbol->simple_spec_init->accept(*this); |
409 s4o.print(" "); |
458 s4o_incl.print(" "); |
410 symbol->simple_type_name->accept(*this); |
459 symbol->simple_type_name->accept(*basedecl); |
411 s4o.print(";\n"); |
460 s4o_incl.print(";\n"); |
412 return NULL; |
461 return NULL; |
413 } |
462 } |
414 |
463 |
415 /* simple_specification [ASSIGN constant] */ |
464 /* simple_specification [ASSIGN constant] */ |
416 //SYM_REF2(simple_spec_init_c, simple_specification, constant) |
465 //SYM_REF2(simple_spec_init_c, simple_specification, constant) |
417 // <constant> may be NULL |
466 // <constant> may be NULL |
418 void *visit(simple_spec_init_c *symbol) { |
467 void *visit(simple_spec_init_c *symbol) { |
419 TRACE("simple_spec_init_c"); |
468 TRACE("simple_spec_init_c"); |
420 symbol->simple_specification->accept(*this); |
469 symbol->simple_specification->accept(*basedecl); |
421 return NULL; |
470 return NULL; |
422 } |
471 } |
423 |
472 |
424 #if 0 |
473 #if 0 |
425 /* subrange_type_name ':' subrange_spec_init */ |
474 /* subrange_type_name ':' subrange_spec_init */ |
476 void *visit(structure_type_declaration_c *symbol) { |
525 void *visit(structure_type_declaration_c *symbol) { |
477 TRACE("structure_type_declaration_c"); |
526 TRACE("structure_type_declaration_c"); |
478 /* add this type declaration to the type symbol table... */ |
527 /* add this type declaration to the type symbol table... */ |
479 type_symtable.insert(symbol->structure_type_name, symbol->structure_specification); |
528 type_symtable.insert(symbol->structure_type_name, symbol->structure_specification); |
480 |
529 |
481 s4o.print("typedef "); |
530 s4o_incl.print("typedef "); |
482 symbol->structure_specification->accept(*this); |
531 symbol->structure_specification->accept(*this); |
483 s4o.print(" "); |
532 s4o_incl.print(" "); |
484 symbol->structure_type_name->accept(*this); |
533 symbol->structure_type_name->accept(*basedecl); |
485 s4o.print(";\n"); |
534 s4o_incl.print(";\n"); |
486 return NULL; |
535 return NULL; |
487 } |
536 } |
488 |
537 |
489 /* structure_type_name ASSIGN structure_initialization */ |
538 /* structure_type_name ASSIGN structure_initialization */ |
490 /* structure_initialization may be NULL ! */ |
539 /* structure_initialization may be NULL ! */ |