source: src/CodeGen/CodeGenerator.cc @ 5b2f5bb

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 5b2f5bb was 5b2f5bb, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

choose destructor at at object declaration (CtorInit?), overhaul and simplification of resolver code for CtorInit?

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