source: src/CodeGen/CodeGenerator.cc@ d029162e

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since d029162e was 4e24610, checked in by Rob Schluntz <rschlunt@…>, 10 years ago

Add constructor attribute to global initializer function, don't try to fix const globals, add Constructor/Destructor attributes to FunctionDecl

  • Property mode set to 100644
File size: 21.0 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// CodeGenerator.cc --
8//
9// Author : Richard C. Bilson
10// Created On : Mon May 18 07:44:20 2015
11// Last Modified By : Rob Schluntz
12// Last Modified On : Fri May 06 11:39:01 2016
13// Update Count : 243
14//
15
16#include <algorithm>
17#include <iostream>
18#include <cassert>
19#include <list>
20
21#include "Parser/ParseNode.h"
22
23#include "SynTree/Declaration.h"
24#include "SynTree/Expression.h"
25#include "SynTree/Initializer.h"
26#include "SynTree/Statement.h"
27#include "SynTree/Type.h"
28
29#include "Common/utility.h"
30#include "Common/UnimplementedError.h"
31
32#include "CodeGenerator.h"
33#include "OperatorTable.h"
34#include "GenType.h"
35
36using namespace std;
37
38namespace CodeGen {
39 int CodeGenerator::tabsize = 4;
40
41 // the kinds of statements that would ideally be followed by whitespace
42 bool wantSpacing( Statement * stmt) {
43 return dynamic_cast< IfStmt * >( stmt ) || dynamic_cast< CompoundStmt * >( stmt ) ||
44 dynamic_cast< WhileStmt * >( stmt ) || dynamic_cast< ForStmt * > ( stmt ) || dynamic_cast< SwitchStmt *>( stmt );
45 }
46
47 ostream & CodeGenerator::Indenter::operator()( ostream & output ) {
48 return output << string( cg.cur_indent, ' ' );
49 }
50
51 ostream & operator<<( ostream & output, CodeGenerator::Indenter &indent ) {
52 return indent( output );
53 }
54
55 CodeGenerator::CodeGenerator( std::ostream &os ) : indent( *this), cur_indent( 0 ), insideFunction( false ), output( os ) { }
56
57 CodeGenerator::CodeGenerator( std::ostream &os, std::string init, int indentation, bool infunp )
58 : indent( *this), cur_indent( indentation ), insideFunction( infunp ), output( os ) {
59 //output << std::string( init );
60 }
61
62 CodeGenerator::CodeGenerator( std::ostream &os, char *init, int indentation, bool infunp )
63 : indent( *this ), cur_indent( indentation ), insideFunction( infunp ), output( os ) {
64 //output << std::string( init );
65 }
66
67 string mangleName( DeclarationWithType *decl ) {
68 if ( decl->get_mangleName() != "" ) {
69 return decl->get_mangleName();
70 } else {
71 return decl->get_name();
72 } // if
73 }
74
75 //*** Declarations
76 void CodeGenerator::visit( FunctionDecl *functionDecl ) {
77 handleStorageClass( functionDecl );
78 if ( functionDecl->get_isInline() ) {
79 output << "inline ";
80 } // if
81 if ( functionDecl->get_isNoreturn() ) {
82 output << "_Noreturn ";
83 } // if
84 output << genType( functionDecl->get_functionType(), mangleName( functionDecl ) );
85
86 // generalize this
87 switch ( functionDecl->get_attribute() ) {
88 case FunctionDecl::Constructor:
89 output << " __attribute__ ((constructor))";
90 break;
91 case FunctionDecl::Destructor:
92 output << " __attribute__ ((destructor))";
93 break;
94 default:
95 break;
96 }
97
98 // how to get this to the Functype?
99 std::list< Declaration * > olds = functionDecl->get_oldDecls();
100 if ( ! olds.empty() ) {
101 output << " /* function has old declaration */";
102 } // if
103
104 // acceptAll( functionDecl->get_oldDecls(), *this );
105 if ( functionDecl->get_statements() ) {
106 functionDecl->get_statements()->accept( *this );
107 } // if
108 }
109
110 void CodeGenerator::visit( ObjectDecl *objectDecl ) {
111 handleStorageClass( objectDecl );
112 output << genType( objectDecl->get_type(), mangleName( objectDecl ) );
113
114 if ( objectDecl->get_init() ) {
115 output << " = ";
116 objectDecl->get_init()->accept( *this );
117 } // if
118 if ( objectDecl->get_bitfieldWidth() ) {
119 output << ":";
120 objectDecl->get_bitfieldWidth()->accept( *this );
121 } // if
122 }
123
124 void CodeGenerator::handleAggregate( AggregateDecl *aggDecl ) {
125 if ( aggDecl->get_name() != "" )
126 output << aggDecl->get_name();
127
128 std::list< Declaration * > &memb = aggDecl->get_members();
129
130 if ( ! memb.empty() ) {
131 output << " {" << endl;
132
133 cur_indent += CodeGenerator::tabsize;
134 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) {
135 output << indent;
136 (*i)->accept( *this );
137 output << ";" << endl;
138 }
139
140 cur_indent -= CodeGenerator::tabsize;
141
142 output << indent << "}";
143 } // if
144 }
145
146 void CodeGenerator::visit( StructDecl *structDecl ) {
147 output << "struct ";
148 handleAggregate( structDecl );
149 }
150
151 void CodeGenerator::visit( UnionDecl *aggregateDecl ) {
152 output << "union ";
153 handleAggregate( aggregateDecl );
154 }
155
156 void CodeGenerator::visit( EnumDecl *aggDecl ) {
157 output << "enum ";
158
159 if ( aggDecl->get_name() != "" )
160 output << aggDecl->get_name();
161
162 std::list< Declaration* > &memb = aggDecl->get_members();
163
164 if ( ! memb.empty() ) {
165 output << " {" << endl;
166
167 cur_indent += CodeGenerator::tabsize;
168 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) {
169 ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i );
170 assert( obj );
171 output << indent << mangleName( obj );
172 if ( obj->get_init() ) {
173 output << " = ";
174 obj->get_init()->accept( *this );
175 } // if
176 output << "," << endl;
177 } // for
178
179 cur_indent -= CodeGenerator::tabsize;
180
181 output << indent << "}";
182 } // if
183 }
184
185 void CodeGenerator::visit( TraitDecl *aggregateDecl ) {}
186
187 void CodeGenerator::visit( TypedefDecl *typeDecl ) {
188 output << "typedef ";
189 output << genType( typeDecl->get_base(), typeDecl->get_name() );
190 }
191
192 void CodeGenerator::visit( TypeDecl *typeDecl ) {
193 // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes,
194 // still to be done
195 output << "extern unsigned long " << typeDecl->get_name();
196 if ( typeDecl->get_base() ) {
197 output << " = sizeof( " << genType( typeDecl->get_base(), "" ) << " )";
198 } // if
199 }
200
201 void CodeGenerator::printDesignators( std::list< Expression * > & designators ) {
202 typedef std::list< Expression * > DesignatorList;
203 if ( designators.size() == 0 ) return;
204 for ( DesignatorList::iterator iter = designators.begin(); iter != designators.end(); ++iter ) {
205 if ( dynamic_cast< NameExpr * >( *iter ) ) {
206 // if expression is a name, then initializing aggregate member
207 output << ".";
208 (*iter)->accept( *this );
209 } else {
210 // if not a simple name, it has to be a constant expression, i.e. an array designator
211 output << "[";
212 (*iter)->accept( *this );
213 output << "]";
214 }
215 }
216 output << " = ";
217 }
218
219 void CodeGenerator::visit( SingleInit *init ) {
220 printDesignators( init->get_designators() );
221 init->get_value()->accept( *this );
222 }
223
224 void CodeGenerator::visit( ListInit *init ) {
225 printDesignators( init->get_designators() );
226 output << "{ ";
227 genCommaList( init->begin_initializers(), init->end_initializers() );
228 output << " }";
229 }
230
231 void CodeGenerator::visit( Constant *constant ) {
232 output << constant->get_value() ;
233 }
234
235 //*** Expressions
236 void CodeGenerator::visit( ApplicationExpr *applicationExpr ) {
237 if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
238 OperatorInfo opInfo;
239 if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) {
240 std::list< Expression* >::iterator arg = applicationExpr->get_args().begin();
241 switch ( opInfo.type ) {
242 case OT_PREFIXASSIGN:
243 case OT_POSTFIXASSIGN:
244 case OT_INFIXASSIGN:
245 {
246 assert( arg != applicationExpr->get_args().end() );
247 if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
248
249 *arg = addrExpr->get_arg();
250 } else {
251 UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) );
252 newExpr->get_args().push_back( *arg );
253 *arg = newExpr;
254 } // if
255 break;
256 }
257
258 default:
259 // do nothing
260 ;
261 }
262
263 switch ( opInfo.type ) {
264 case OT_INDEX:
265 assert( applicationExpr->get_args().size() == 2 );
266 (*arg++)->accept( *this );
267 output << "[";
268 (*arg)->accept( *this );
269 output << "]";
270 break;
271
272 case OT_CALL:
273 // there are no intrinsic definitions of the function call operator
274 assert( false );
275 break;
276
277 case OT_PREFIX:
278 case OT_PREFIXASSIGN:
279 assert( applicationExpr->get_args().size() == 1 );
280 output << "(";
281 output << opInfo.symbol;
282 (*arg)->accept( *this );
283 output << ")";
284 break;
285
286 case OT_POSTFIX:
287 case OT_POSTFIXASSIGN:
288 assert( applicationExpr->get_args().size() == 1 );
289 (*arg)->accept( *this );
290 output << opInfo.symbol;
291 break;
292
293 case OT_INFIX:
294 case OT_INFIXASSIGN:
295 assert( applicationExpr->get_args().size() == 2 );
296 output << "(";
297 (*arg++)->accept( *this );
298 output << opInfo.symbol;
299 (*arg)->accept( *this );
300 output << ")";
301 break;
302
303 case OT_CONSTANT:
304 case OT_LABELADDRESS:
305 // there are no intrinsic definitions of 0/1 or label addresses as functions
306 assert( false );
307 }
308 } else {
309 varExpr->accept( *this );
310 output << "(";
311 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
312 output << ")";
313 } // if
314 } else {
315 applicationExpr->get_function()->accept( *this );
316 output << "(";
317 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
318 output << ")";
319 } // if
320 }
321
322 void CodeGenerator::visit( UntypedExpr *untypedExpr ) {
323 if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
324 OperatorInfo opInfo;
325 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
326 std::list< Expression* >::iterator arg = untypedExpr->get_args().begin();
327 switch ( opInfo.type ) {
328 case OT_INDEX:
329 assert( untypedExpr->get_args().size() == 2 );
330 (*arg++)->accept( *this );
331 output << "[";
332 (*arg)->accept( *this );
333 output << "]";
334 break;
335
336 case OT_CALL:
337 assert( false );
338 break;
339
340 case OT_PREFIX:
341 case OT_PREFIXASSIGN:
342 case OT_LABELADDRESS:
343 assert( untypedExpr->get_args().size() == 1 );
344 output << "(";
345 output << opInfo.symbol;
346 (*arg)->accept( *this );
347 output << ")";
348 break;
349
350 case OT_POSTFIX:
351 case OT_POSTFIXASSIGN:
352 assert( untypedExpr->get_args().size() == 1 );
353 (*arg)->accept( *this );
354 output << opInfo.symbol;
355 break;
356
357 case OT_INFIX:
358 case OT_INFIXASSIGN:
359 assert( untypedExpr->get_args().size() == 2 );
360 output << "(";
361 (*arg++)->accept( *this );
362 output << opInfo.symbol;
363 (*arg)->accept( *this );
364 output << ")";
365 break;
366
367 case OT_CONSTANT:
368 // there are no intrinsic definitions of 0 or 1 as functions
369 assert( false );
370 }
371 } else {
372 nameExpr->accept( *this );
373 output << "(";
374 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
375 output << ")";
376 } // if
377 } else {
378 untypedExpr->get_function()->accept( *this );
379 output << "(";
380 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
381 output << ")";
382 } // if
383 }
384
385 void CodeGenerator::visit( NameExpr *nameExpr ) {
386 OperatorInfo opInfo;
387 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
388 assert( opInfo.type == OT_CONSTANT );
389 output << opInfo.symbol;
390 } else {
391 output << nameExpr->get_name();
392 } // if
393 }
394
395 void CodeGenerator::visit( AddressExpr *addressExpr ) {
396 output << "(&";
397 // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address
398 if ( VariableExpr *variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
399 output << mangleName( variableExpr->get_var() );
400 } else {
401 addressExpr->get_arg()->accept( *this );
402 } // if
403 output << ")";
404 }
405
406 void CodeGenerator::visit( CastExpr *castExpr ) {
407 output << "(";
408 if ( castExpr->get_results().empty() ) {
409 output << "(void)" ;
410 } else if ( ! castExpr->get_results().front()->get_isLvalue() ) {
411 // at least one result type of cast, but not an lvalue
412 output << "(";
413 output << genType( castExpr->get_results().front(), "" );
414 output << ")";
415 } else {
416 // otherwise, the cast is to an lvalue type, so the cast
417 // should be dropped, since the result of a cast is
418 // never an lvalue in C
419 }
420 castExpr->get_arg()->accept( *this );
421 output << ")";
422 }
423
424 void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) {
425 assert( false );
426 }
427
428 void CodeGenerator::visit( MemberExpr *memberExpr ) {
429 memberExpr->get_aggregate()->accept( *this );
430 output << "." << mangleName( memberExpr->get_member() );
431 }
432
433 void CodeGenerator::visit( VariableExpr *variableExpr ) {
434 OperatorInfo opInfo;
435 if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
436 output << opInfo.symbol;
437 } else {
438 output << mangleName( variableExpr->get_var() );
439 } // if
440 }
441
442 void CodeGenerator::visit( ConstantExpr *constantExpr ) {
443 assert( constantExpr->get_constant() );
444 constantExpr->get_constant()->accept( *this );
445 }
446
447 void CodeGenerator::visit( SizeofExpr *sizeofExpr ) {
448 output << "sizeof(";
449 if ( sizeofExpr->get_isType() ) {
450 output << genType( sizeofExpr->get_type(), "" );
451 } else {
452 sizeofExpr->get_expr()->accept( *this );
453 } // if
454 output << ")";
455 }
456
457 void CodeGenerator::visit( AlignofExpr *alignofExpr ) {
458 // use GCC extension to avoid bumping std to C11
459 output << "__alignof__(";
460 if ( alignofExpr->get_isType() ) {
461 output << genType( alignofExpr->get_type(), "" );
462 } else {
463 alignofExpr->get_expr()->accept( *this );
464 } // if
465 output << ")";
466 }
467
468 void CodeGenerator::visit( UntypedOffsetofExpr *offsetofExpr ) {
469 assert( false && "UntypedOffsetofExpr should not reach code generation" );
470 }
471
472 void CodeGenerator::visit( OffsetofExpr *offsetofExpr ) {
473 // use GCC builtin
474 output << "__builtin_offsetof(";
475 output << genType( offsetofExpr->get_type(), "" );
476 output << ", " << mangleName( offsetofExpr->get_member() );
477 output << ")";
478 }
479
480 void CodeGenerator::visit( OffsetPackExpr *offsetPackExpr ) {
481 assert( false && "OffsetPackExpr should not reach code generation" );
482 }
483
484 void CodeGenerator::visit( LogicalExpr *logicalExpr ) {
485 output << "(";
486 logicalExpr->get_arg1()->accept( *this );
487 if ( logicalExpr->get_isAnd() ) {
488 output << " && ";
489 } else {
490 output << " || ";
491 } // if
492 logicalExpr->get_arg2()->accept( *this );
493 output << ")";
494 }
495
496 void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) {
497 output << "(";
498 conditionalExpr->get_arg1()->accept( *this );
499 output << " ? ";
500 conditionalExpr->get_arg2()->accept( *this );
501 output << " : ";
502 conditionalExpr->get_arg3()->accept( *this );
503 output << ")";
504 }
505
506 void CodeGenerator::visit( CommaExpr *commaExpr ) {
507 output << "(";
508 commaExpr->get_arg1()->accept( *this );
509 output << " , ";
510 commaExpr->get_arg2()->accept( *this );
511 output << ")";
512 }
513
514 void CodeGenerator::visit( TupleExpr *tupleExpr ) {}
515
516 void CodeGenerator::visit( TypeExpr *typeExpr ) {}
517
518 void CodeGenerator::visit( AsmExpr *asmExpr ) {
519 if ( asmExpr->get_inout() ) {
520 output << "[ ";
521 asmExpr->get_inout()->accept( *this );
522 output << " ] ";
523 } // if
524 asmExpr->get_constraint()->accept( *this );
525 output << " ( ";
526 asmExpr->get_operand()->accept( *this );
527 output << " )";
528 }
529
530 //*** Statements
531 void CodeGenerator::visit( CompoundStmt *compoundStmt ) {
532 std::list<Statement*> ks = compoundStmt->get_kids();
533 output << "{" << endl;
534
535 cur_indent += CodeGenerator::tabsize;
536
537 for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++ ) {
538 output << indent << printLabels( (*i)->get_labels() );
539 (*i)->accept( *this );
540
541 output << endl;
542 if ( wantSpacing( *i ) ) {
543 output << endl;
544 }
545 }
546 cur_indent -= CodeGenerator::tabsize;
547
548 output << indent << "}";
549 }
550
551 void CodeGenerator::visit( ExprStmt *exprStmt ) {
552 // I don't see why this check is necessary.
553 // If this starts to cause problems then put it back in,
554 // with an explanation
555 assert( exprStmt );
556
557 // if ( exprStmt != 0 ) {
558 exprStmt->get_expr()->accept( *this );
559 output << ";" ;
560 // } // if
561 }
562
563 void CodeGenerator::visit( AsmStmt *asmStmt ) {
564 output << "asm ";
565 if ( asmStmt->get_voltile() ) output << "volatile ";
566 if ( ! asmStmt->get_gotolabels().empty() ) output << "goto ";
567 output << "( ";
568 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this );
569 output << " : ";
570 genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() );
571 output << " : ";
572 genCommaList( asmStmt->get_input().begin(), asmStmt->get_input().end() );
573 output << " : ";
574 genCommaList( asmStmt->get_clobber().begin(), asmStmt->get_clobber().end() );
575 if ( ! asmStmt->get_gotolabels().empty() ) {
576 output << " : ";
577 for ( std::list<Label>::iterator begin = asmStmt->get_gotolabels().begin();; ) {
578 output << *begin++;
579 if ( begin == asmStmt->get_gotolabels().end() ) break;
580 output << ", ";
581 } // for
582 } // if
583 output << " );" ;
584 }
585
586 void CodeGenerator::visit( IfStmt *ifStmt ) {
587 output << "if ( ";
588 ifStmt->get_condition()->accept( *this );
589 output << " ) ";
590
591 ifStmt->get_thenPart()->accept( *this );
592
593 if ( ifStmt->get_elsePart() != 0) {
594 output << " else ";
595 ifStmt->get_elsePart()->accept( *this );
596 } // if
597 }
598
599 void CodeGenerator::visit( SwitchStmt *switchStmt ) {
600 output << "switch ( " ;
601 switchStmt->get_condition()->accept( *this );
602 output << " ) ";
603
604 output << "{" << std::endl;
605 cur_indent += CodeGenerator::tabsize;
606
607 acceptAll( switchStmt->get_branches(), *this );
608
609 cur_indent -= CodeGenerator::tabsize;
610
611 output << indent << "}";
612 }
613
614 void CodeGenerator::visit( CaseStmt *caseStmt ) {
615 output << indent;
616 if ( caseStmt->isDefault()) {
617 output << "default";
618 } else {
619 output << "case ";
620 caseStmt->get_condition()->accept( *this );
621 } // if
622 output << ":\n";
623
624 std::list<Statement *> sts = caseStmt->get_statements();
625
626 cur_indent += CodeGenerator::tabsize;
627 for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) {
628 output << indent << printLabels( (*i)->get_labels() ) ;
629 (*i)->accept( *this );
630 output << endl;
631 }
632 cur_indent -= CodeGenerator::tabsize;
633 }
634
635 void CodeGenerator::visit( BranchStmt *branchStmt ) {
636 switch ( branchStmt->get_type()) {
637 case BranchStmt::Goto:
638 if ( ! branchStmt->get_target().empty() )
639 output << "goto " << branchStmt->get_target();
640 else {
641 if ( branchStmt->get_computedTarget() != 0 ) {
642 output << "goto *";
643 branchStmt->get_computedTarget()->accept( *this );
644 } // if
645 } // if
646 break;
647 case BranchStmt::Break:
648 output << "break";
649 break;
650 case BranchStmt::Continue:
651 output << "continue";
652 break;
653 }
654 output << ";";
655 }
656
657
658 void CodeGenerator::visit( ReturnStmt *returnStmt ) {
659 output << "return ";
660
661 // xxx -- check for null expression;
662 if ( returnStmt->get_expr() ) {
663 returnStmt->get_expr()->accept( *this );
664 } // if
665 output << ";";
666 }
667
668 void CodeGenerator::visit( WhileStmt *whileStmt ) {
669 if ( whileStmt->get_isDoWhile() )
670 output << "do" ;
671 else {
672 output << "while (" ;
673 whileStmt->get_condition()->accept( *this );
674 output << ")";
675 } // if
676 output << " ";
677
678 output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() );
679 whileStmt->get_body()->accept( *this );
680
681 output << indent;
682
683 if ( whileStmt->get_isDoWhile() ) {
684 output << " while (" ;
685 whileStmt->get_condition()->accept( *this );
686 output << ");";
687 } // if
688 }
689
690 void CodeGenerator::visit( ForStmt *forStmt ) {
691 // initialization is always hoisted, so don't
692 // bother doing anything with that
693 output << "for (;";
694
695 if ( forStmt->get_condition() != 0 )
696 forStmt->get_condition()->accept( *this );
697 output << ";";
698
699 if ( forStmt->get_increment() != 0 )
700 forStmt->get_increment()->accept( *this );
701 output << ") ";
702
703 if ( forStmt->get_body() != 0 ) {
704 output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
705 forStmt->get_body()->accept( *this );
706 } // if
707 }
708
709 void CodeGenerator::visit( NullStmt *nullStmt ) {
710 //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
711 output << "/* null statement */ ;";
712 }
713
714 void CodeGenerator::visit( DeclStmt *declStmt ) {
715 declStmt->get_decl()->accept( *this );
716
717 if ( doSemicolon( declStmt->get_decl() ) ) {
718 output << ";";
719 } // if
720 }
721
722 std::string CodeGenerator::printLabels( std::list< Label > &l ) {
723 std::string str( "" );
724 l.unique(); // assumes a sorted list. Why not use set?
725
726 for ( std::list< Label >::iterator i = l.begin(); i != l.end(); i++ )
727 str += *i + ": ";
728
729 return str;
730 }
731
732 void CodeGenerator::handleStorageClass( Declaration *decl ) {
733 switch ( decl->get_storageClass() ) {
734 case DeclarationNode::Extern:
735 output << "extern ";
736 break;
737 case DeclarationNode::Static:
738 output << "static ";
739 break;
740 case DeclarationNode::Auto:
741 // silently drop storage class
742 break;
743 case DeclarationNode::Register:
744 output << "register ";
745 break;
746 case DeclarationNode::Inline:
747 output << "inline ";
748 break;
749 case DeclarationNode::Fortran:
750 output << "fortran ";
751 break;
752 case DeclarationNode::Noreturn:
753 output << "_Noreturn ";
754 break;
755 case DeclarationNode::Threadlocal:
756 output << "_Thread_local ";
757 break;
758 case DeclarationNode::NoStorageClass:
759 break;
760 } // switch
761 }
762} // namespace CodeGen
763
764// Local Variables: //
765// tab-width: 4 //
766// mode: c++ //
767// compile-command: "make install" //
768// End: //
Note: See TracBrowser for help on using the repository browser.