Changeset 17cd4eb


Ignore:
Timestamp:
Jan 7, 2015, 6:04:42 PM (9 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
Children:
0b8cd722
Parents:
d9a0e76
Message:

fixed restrict, fixed parameter copy, introduced name table for types, changed variable after to string

Files:
26 edited

Legend:

Unmodified
Added
Removed
  • libcfa/prelude.cf

    rd9a0e76 r17cd4eb  
    88// Created On       : Sat Nov 29 07:23:41 2014
    99// Last Modified By : Peter A. Buhr
    10 // Last Modified On : Mon Dec 15 16:20:30 2014
    11 // Update Count     : 27
     10// Last Modified On : Wed Jan  7 17:42:15 2015
     11// Update Count     : 28
    1212//
    1313
    1414// Following line added from stddef.h by build
     15
    1516typedef long int ptrdiff_t;
    1617
  • translator/CodeGen/CodeGenerator2.cc

    rd9a0e76 r17cd4eb  
    2020
    2121namespace CodeGen {
    22 
    23   int CodeGenerator2::tabsize = 4;
    24 
    25   CodeGenerator2::CodeGenerator2( std::ostream &os ) : cur_indent( 0 ), insideFunction( false ), before(os), after() { }
    26 
    27   CodeGenerator2::CodeGenerator2(std::ostream &os, std::string init, int indent, bool infunp )
    28     : cur_indent( indent ), insideFunction( infunp ), before(os), after()
    29   {
    30     //before << std::string( init );
    31   }
    32 
    33   CodeGenerator2::CodeGenerator2(std::ostream &os, char *init, int indent, bool infunp )
    34     : cur_indent( indent ), insideFunction( infunp ), before(os), after()
    35   {
    36     //before << std::string( init );
    37   }
    38 
    39   CodeGenerator2::~CodeGenerator2() {
    40     after.freeze( false );
    41   }
    42 
    43   string
    44   mangleName( DeclarationWithType *decl ) {
    45     if( decl->get_mangleName() != "" ) {
    46       return decl->get_mangleName();
    47     } else {
    48       return decl->get_name();
    49     }
    50   }
    51  
    52   //*** Declarations
    53   void CodeGenerator2::visit(FunctionDecl *functionDecl){
    54  
    55     handleStorageClass( functionDecl );
    56     before << genType( functionDecl->get_functionType(), mangleName( functionDecl ) );
    57 
    58     // how to get this to the Functype?
    59     std::list< Declaration * > olds = functionDecl->get_oldDecls();
    60     if( ! olds.empty() ) {
    61       before << " /* function has old declaration */";
    62     }
    63 
    64     // acceptAll( functionDecl->get_oldDecls(), *this );
    65     if( functionDecl->get_statements() ) {
    66       functionDecl->get_statements()->accept(*this);
    67     }
    68   }
    69 
    70   void CodeGenerator2::visit(ObjectDecl *objectDecl){
    71 
    72     handleStorageClass( objectDecl );
    73     before << genType( objectDecl->get_type(), mangleName( objectDecl ) );
     22    int CodeGenerator2::tabsize = 4;
     23
     24    CodeGenerator2::CodeGenerator2( std::ostream &os ) : cur_indent( 0 ), insideFunction( false ), before( os ), after() { }
     25
     26    CodeGenerator2::CodeGenerator2( std::ostream &os, std::string init, int indent, bool infunp )
     27        : cur_indent( indent ), insideFunction( infunp ), before( os )
     28    {
     29        //before << std::string( init );
     30    }
     31
     32    CodeGenerator2::CodeGenerator2( std::ostream &os, char *init, int indent, bool infunp )
     33        : cur_indent( indent ), insideFunction( infunp ), before( os )
     34    {
     35        //before << std::string( init );
     36    }
     37
     38    string mangleName( DeclarationWithType *decl ) {
     39        if ( decl->get_mangleName() != "" ) {
     40            return decl->get_mangleName();
     41        } else {
     42            return decl->get_name();
     43        } // if
     44    }
     45 
     46    //*** Declarations
     47    void CodeGenerator2::visit( FunctionDecl *functionDecl ) {
     48        handleStorageClass( functionDecl );
     49        before << genType( functionDecl->get_functionType(), mangleName( functionDecl ) );
     50
     51        // how to get this to the Functype?
     52        std::list< Declaration * > olds = functionDecl->get_oldDecls();
     53        if ( ! olds.empty() ) {
     54            before << " /* function has old declaration */";
     55        } // if
     56
     57        // acceptAll( functionDecl->get_oldDecls(), *this );
     58        if ( functionDecl->get_statements() ) {
     59            functionDecl->get_statements()->accept(*this );
     60        } // if
     61    }
     62
     63    void CodeGenerator2::visit( ObjectDecl *objectDecl ) {
     64        handleStorageClass( objectDecl );
     65        before << genType( objectDecl->get_type(), mangleName( objectDecl ) );
    7466   
    75     if( objectDecl->get_init() ) {
    76       before << " = ";
    77       objectDecl->get_init()->accept( *this );
    78     }
    79     if( objectDecl->get_bitfieldWidth() ) {
    80       before << ":";
    81       objectDecl->get_bitfieldWidth()->accept( *this );
    82     }
    83   }
    84 
    85   void
    86   CodeGenerator2::handleAggregate( AggregateDecl *aggDecl )
    87   {
    88     if ( aggDecl->get_name() != "" )
    89       before << aggDecl->get_name();
     67        if ( objectDecl->get_init() ) {
     68            before << " = ";
     69            objectDecl->get_init()->accept( *this );
     70        } // if
     71        if ( objectDecl->get_bitfieldWidth() ) {
     72            before << ":";
     73            objectDecl->get_bitfieldWidth()->accept( *this );
     74        } // if
     75    }
     76
     77    void CodeGenerator2::handleAggregate( AggregateDecl *aggDecl ) {
     78        if ( aggDecl->get_name() != "" )
     79            before << aggDecl->get_name();
    9080   
    91     std::list< Declaration * > &memb = aggDecl->get_members();
    92 
    93     if( ! memb.empty() ){
    94       before << endl << string(cur_indent, ' ') << "{" << endl;
    95 
    96       cur_indent += CodeGenerator2::tabsize;
    97       for(std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++){
    98         before << string(cur_indent, ' ');
    99         (*i)->accept(*this);
    100         before << ";" << endl;
    101       }
    102 
    103       cur_indent -= CodeGenerator2::tabsize;
    104 
    105       before << string(cur_indent, ' ') << "}";
    106     }
    107   }
    108 
    109   void CodeGenerator2::visit( StructDecl *structDecl ){
    110     before << "struct ";
    111     handleAggregate( structDecl );
    112   }
    113 
    114   void
    115   CodeGenerator2::visit(UnionDecl *aggregateDecl)
    116   {
    117     before << "union ";
    118     handleAggregate( aggregateDecl );
    119   }
    120  
    121   void
    122   CodeGenerator2::visit(EnumDecl *aggDecl)
    123   {
    124     before << "enum ";
    125 
    126     if ( aggDecl->get_name() != "" )
    127       before << aggDecl->get_name();
     81        std::list< Declaration * > &memb = aggDecl->get_members();
     82
     83        if ( ! memb.empty() ) {
     84            before << endl << string( cur_indent, ' ' ) << "{" << endl;
     85
     86            cur_indent += CodeGenerator2::tabsize;
     87            for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
     88                before << string( cur_indent, ' ' );
     89                (*i)->accept(*this );
     90                before << ";" << endl;
     91            }
     92
     93            cur_indent -= CodeGenerator2::tabsize;
     94
     95            before << string( cur_indent, ' ' ) << "}";
     96        } // if
     97    }
     98
     99    void CodeGenerator2::visit( StructDecl *structDecl ) {
     100        before << "struct ";
     101        handleAggregate( structDecl );
     102    }
     103
     104    void CodeGenerator2::visit( UnionDecl *aggregateDecl ) {
     105        before << "union ";
     106        handleAggregate( aggregateDecl );
     107    }
     108 
     109    void CodeGenerator2::visit( EnumDecl *aggDecl ) {
     110        before << "enum ";
     111
     112        if ( aggDecl->get_name() != "" )
     113            before << aggDecl->get_name();
    128114   
    129     std::list< Declaration* > &memb = aggDecl->get_members();
    130 
    131     if( ! memb.empty() ){
    132       before << endl << "{" << endl;
    133 
    134       cur_indent += CodeGenerator2::tabsize;
    135       for(std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++){
    136         ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i );
    137         assert( obj );
    138         before << string(cur_indent, ' ') << mangleName( obj );
    139         if( obj->get_init() ) {
    140           before << " = ";
    141           obj->get_init()->accept(*this);
     115        std::list< Declaration* > &memb = aggDecl->get_members();
     116
     117        if ( ! memb.empty() ) {
     118            before << endl << "{" << endl;
     119
     120            cur_indent += CodeGenerator2::tabsize;
     121            for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
     122                ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i );
     123                assert( obj );
     124                before << string( cur_indent, ' ' ) << mangleName( obj );
     125                if ( obj->get_init() ) {
     126                    before << " = ";
     127                    obj->get_init()->accept(*this );
     128                } // if
     129                before << "," << endl;
     130            }
     131
     132            cur_indent -= CodeGenerator2::tabsize;
     133
     134            before << "}" << endl;
     135        } // if
     136    }
     137 
     138    void CodeGenerator2::visit( ContextDecl *aggregateDecl ) {}
     139 
     140    void CodeGenerator2::visit( TypedefDecl *typeDecl ) {
     141        before << "typedef ";
     142        before << genType( typeDecl->get_base(), typeDecl->get_name() );
     143    }
     144 
     145    void CodeGenerator2::visit( TypeDecl *typeDecl ) {
     146        // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes,
     147        // still to be done
     148        before << "extern unsigned long " << typeDecl->get_name();
     149        if ( typeDecl->get_base() ) {
     150            before << " = sizeof( " << genType( typeDecl->get_base(), "" ) << " )";
     151        } // if
     152    }
     153
     154    void CodeGenerator2::visit( SingleInit *init ) {
     155        init->get_value()->accept( *this );
     156    }
     157
     158    void CodeGenerator2::visit( ListInit *init ) {
     159        before << "{ ";
     160        genCommaList( init->begin_initializers(), init->end_initializers() );
     161        before << " }";
     162    }
     163
     164    void CodeGenerator2::visit( Constant *constant ) {
     165        before << constant->get_value() ;
     166    }
     167
     168    //*** Expressions
     169    void CodeGenerator2::visit( ApplicationExpr *applicationExpr ) {
     170        if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
     171            OperatorInfo opInfo;
     172            if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) {
     173                std::list< Expression* >::iterator arg = applicationExpr->get_args().begin();
     174                switch ( opInfo.type ) {
     175                  case OT_PREFIXASSIGN:
     176                  case OT_POSTFIXASSIGN:
     177                  case OT_INFIXASSIGN:
     178                    {
     179                        assert( arg != applicationExpr->get_args().end() );
     180                        if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
     181           
     182                            *arg = addrExpr->get_arg();
     183                        } else {
     184                            UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) );
     185                            newExpr->get_args().push_back( *arg );
     186                            *arg = newExpr;
     187                        } // if
     188                        break;
     189                    }
     190         
     191                  default:
     192                    // do nothing
     193                    ;
     194                }
     195       
     196                switch ( opInfo.type ) {
     197                  case OT_INDEX:
     198                    assert( applicationExpr->get_args().size() == 2 );
     199                    (*arg++)->accept( *this );
     200                    before << "[";
     201                    (*arg)->accept( *this );
     202                    before << "]";
     203                    break;
     204         
     205                  case OT_CALL:
     206                    // there are no intrinsic definitions of the function call operator
     207                    assert( false );
     208                    break;
     209         
     210                  case OT_PREFIX:
     211                  case OT_PREFIXASSIGN:
     212                    assert( applicationExpr->get_args().size() == 1 );
     213                    before << "(";
     214                    before << opInfo.symbol;
     215                    (*arg)->accept( *this );
     216                    before << ")";
     217                    break;
     218         
     219                  case OT_POSTFIX:
     220                  case OT_POSTFIXASSIGN:
     221                    assert( applicationExpr->get_args().size() == 1 );
     222                    (*arg)->accept( *this );
     223                    before << opInfo.symbol;
     224                    break;
     225
     226                  case OT_INFIX:
     227                  case OT_INFIXASSIGN:
     228                    assert( applicationExpr->get_args().size() == 2 );
     229                    before << "(";
     230                    (*arg++)->accept( *this );
     231                    before << opInfo.symbol;
     232                    (*arg)->accept( *this );
     233                    before << ")";
     234                    break;
     235         
     236                  case OT_CONSTANT:
     237                    // there are no intrinsic definitions of 0 or 1 as functions
     238                    assert( false );
     239                }
     240            } else {
     241                varExpr->accept( *this );
     242                before << "(";
     243                genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
     244                before << ")";
     245            } // if
     246        } else {
     247            applicationExpr->get_function()->accept( *this );
     248            before << "(";
     249            genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
     250            before << ")";
     251        } // if
     252    }
     253 
     254    void CodeGenerator2::visit( UntypedExpr *untypedExpr ) {
     255        if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
     256            OperatorInfo opInfo;
     257            if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
     258                std::list< Expression* >::iterator arg = untypedExpr->get_args().begin();
     259                switch ( opInfo.type ) {
     260                  case OT_INDEX:
     261                    assert( untypedExpr->get_args().size() == 2 );
     262                    (*arg++)->accept( *this );
     263                    before << "[";
     264                    (*arg)->accept( *this );
     265                    before << "]";
     266                    break;
     267         
     268                  case OT_CALL:
     269                    assert( false );
     270                    break;
     271         
     272                  case OT_PREFIX:
     273                  case OT_PREFIXASSIGN:
     274                    assert( untypedExpr->get_args().size() == 1 );
     275                    before << "(";
     276                    before << opInfo.symbol;
     277                    (*arg)->accept( *this );
     278                    before << ")";
     279                    break;
     280         
     281                  case OT_POSTFIX:
     282                  case OT_POSTFIXASSIGN:
     283                    assert( untypedExpr->get_args().size() == 1 );
     284                    (*arg)->accept( *this );
     285                    before << opInfo.symbol;
     286                    break;
     287 
     288                  case OT_INFIX:
     289                  case OT_INFIXASSIGN:
     290                    assert( untypedExpr->get_args().size() == 2 );
     291                    before << "(";
     292                    (*arg++)->accept( *this );
     293                    before << opInfo.symbol;
     294                    (*arg)->accept( *this );
     295                    before << ")";
     296                    break;
     297         
     298                  case OT_CONSTANT:
     299                    // there are no intrinsic definitions of 0 or 1 as functions
     300                    assert( false );
     301                }
     302            } else {
     303                nameExpr->accept( *this );
     304                before << "(";
     305                genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
     306                before << ")";
     307            } // if
     308        } else {
     309            untypedExpr->get_function()->accept( *this );
     310            before << "(";
     311            genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
     312            before << ")";
     313        } // if
     314    }
     315 
     316    void CodeGenerator2::visit( NameExpr *nameExpr ) {
     317        OperatorInfo opInfo;
     318        if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
     319            assert( opInfo.type == OT_CONSTANT );
     320            before << opInfo.symbol;
     321        } else {
     322            before << nameExpr->get_name();
     323        } // if
     324    }
     325 
     326    void CodeGenerator2::visit( AddressExpr *addressExpr ) {
     327        before << "(&";
     328        // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address
     329        if ( VariableExpr *variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
     330            before << mangleName( variableExpr->get_var() );
     331        } else {
     332            addressExpr->get_arg()->accept( *this );
     333        } // if
     334        before << ")";
     335    }
     336
     337    void CodeGenerator2::visit( CastExpr *castExpr ) {
     338        before << "((";
     339        if ( castExpr->get_results().empty() ) {
     340            before << "void" ;
     341        } else {
     342            before << genType( castExpr->get_results().front(), "" );
     343        } // if
     344        before << ")";
     345        castExpr->get_arg()->accept( *this );
     346        before << ")";
     347    }
     348 
     349    void CodeGenerator2::visit( UntypedMemberExpr *memberExpr ) {
     350        assert( false );
     351    }
     352 
     353    void CodeGenerator2::visit( MemberExpr *memberExpr ) {
     354        memberExpr->get_aggregate()->accept( *this );
     355        before << "." << mangleName( memberExpr->get_member() );
     356    }
     357 
     358    void CodeGenerator2::visit( VariableExpr *variableExpr ) {
     359        OperatorInfo opInfo;
     360        if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
     361            before << opInfo.symbol;
     362        } else {
     363            before << mangleName( variableExpr->get_var() );
     364        } // if
     365    }
     366 
     367    void CodeGenerator2::visit( ConstantExpr *constantExpr ) {
     368        assert( constantExpr->get_constant() );
     369        constantExpr->get_constant()->accept( *this );
     370    }
     371 
     372    void CodeGenerator2::visit( SizeofExpr *sizeofExpr ) {
     373        before << "sizeof(";
     374        if ( sizeofExpr->get_isType() ) {
     375            before << genType( sizeofExpr->get_type(), "" );
     376        } else {
     377            sizeofExpr->get_expr()->accept( *this );
     378        } // if
     379        before << ")";
     380    }
     381 
     382    void CodeGenerator2::visit( LogicalExpr *logicalExpr ) {
     383        before << "(";
     384        logicalExpr->get_arg1()->accept( *this );
     385        if ( logicalExpr->get_isAnd() ) {
     386            before << " && ";
     387        } else {
     388            before << " || ";
     389        } // if
     390        logicalExpr->get_arg2()->accept( *this );
     391        before << ")";
     392    }
     393 
     394    void CodeGenerator2::visit( ConditionalExpr *conditionalExpr ) {
     395        before << "(";
     396        conditionalExpr->get_arg1()->accept( *this );
     397        before << " ? ";
     398        conditionalExpr->get_arg2()->accept( *this );
     399        before << " : ";
     400        conditionalExpr->get_arg3()->accept( *this );
     401        before << ")";
     402    }
     403 
     404    void CodeGenerator2::visit( CommaExpr *commaExpr ) {
     405        before << "(";
     406        commaExpr->get_arg1()->accept( *this );
     407        before << " , ";
     408        commaExpr->get_arg2()->accept( *this );
     409        before << ")";
     410    }
     411 
     412    void CodeGenerator2::visit( TupleExpr *tupleExpr ) {}
     413 
     414    void CodeGenerator2::visit( TypeExpr *typeExpr ) {}
     415 
     416 
     417    //*** Statements
     418    void CodeGenerator2::visit( CompoundStmt *compoundStmt ) {
     419        std::list<Statement*> ks = compoundStmt->get_kids();
     420
     421        before << endl << string( cur_indent, ' ' ) << "{" << endl;
     422
     423        cur_indent += CodeGenerator2::tabsize;
     424
     425        for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end();  i++) {
     426            before << string( cur_indent, ' ' ) << printLabels( (*i)->get_labels() )  ;
     427            (*i)->accept(*this );
     428            shift_left();
     429            before << endl;
    142430        }
    143         before << "," << endl;
    144       }
    145 
    146       cur_indent -= CodeGenerator2::tabsize;
    147 
    148       before << "}" << endl;
    149     }
    150   }
    151  
    152   void
    153   CodeGenerator2::visit(ContextDecl *aggregateDecl)
    154   {
    155   }
    156  
    157   void
    158   CodeGenerator2::visit(TypedefDecl *typeDecl)
    159   {
    160     before << "typedef ";
    161     before << genType( typeDecl->get_base(), typeDecl->get_name() );
    162   }
    163  
    164   void
    165   CodeGenerator2::visit(TypeDecl *typeDecl)
    166   {
    167     // really, we should mutate this into something that isn't a TypeDecl
    168     // but that requires large-scale changes, still to be done
    169     before << "extern unsigned long " << typeDecl->get_name();
    170     if( typeDecl->get_base() ) {
    171       before << " = sizeof( " << genType( typeDecl->get_base(), "" ) << " )";
    172     }
    173   }
    174 
    175   //*** Initializer
    176   void
    177   CodeGenerator2::visit(SingleInit *init)
    178   {
    179     init->get_value()->accept( *this );
    180   }
    181 
    182   void
    183   CodeGenerator2::visit(ListInit *init)
    184   {
    185     before << "{ ";
    186     genCommaList( init->begin_initializers(), init->end_initializers() );
    187     before << " }";
    188   }
    189 
    190   //*** Constant
    191   void CodeGenerator2::visit(Constant *constant) {
    192     before << constant->get_value() ;
    193   }
    194 
    195   //*** Expressions
    196   void
    197   CodeGenerator2::visit(ApplicationExpr *applicationExpr)
    198   {
    199     if( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
    200       OperatorInfo opInfo;
    201       if( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) {
    202         std::list< Expression* >::iterator arg = applicationExpr->get_args().begin();
    203         switch( opInfo.type ) {
    204         case OT_PREFIXASSIGN:
    205         case OT_POSTFIXASSIGN:
    206         case OT_INFIXASSIGN:
    207         {
    208           assert( arg != applicationExpr->get_args().end() );
    209           if( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
    210            
    211             *arg = addrExpr->get_arg();
    212           } else {
    213             UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) );
    214             newExpr->get_args().push_back( *arg );
    215             *arg = newExpr;
    216           }
    217           break;
    218         }
    219          
    220         default:
    221           // do nothing
    222           ;
    223         }
    224        
    225         switch( opInfo.type ) {
    226         case OT_INDEX:
    227           assert( applicationExpr->get_args().size() == 2 );
    228           (*arg++)->accept( *this );
    229           before << "[";
    230           (*arg)->accept( *this );
    231           before << "]";
    232           break;
    233          
    234         case OT_CALL:
    235           // there are no intrinsic definitions of the function call operator
    236           assert( false );
    237           break;
    238          
    239         case OT_PREFIX:
    240         case OT_PREFIXASSIGN:
    241           assert( applicationExpr->get_args().size() == 1 );
    242           before << "(";
    243           before << opInfo.symbol;
    244           (*arg)->accept( *this );
    245           before << ")";
    246           break;
    247          
    248         case OT_POSTFIX:
    249         case OT_POSTFIXASSIGN:
    250           assert( applicationExpr->get_args().size() == 1 );
    251           (*arg)->accept( *this );
    252           before << opInfo.symbol;
    253           break;
    254 
    255         case OT_INFIX:
    256         case OT_INFIXASSIGN:
    257           assert( applicationExpr->get_args().size() == 2 );
    258           before << "(";
    259           (*arg++)->accept( *this );
    260           before << opInfo.symbol;
    261           (*arg)->accept( *this );
    262           before << ")";
    263           break;
    264          
    265         case OT_CONSTANT:
    266           // there are no intrinsic definitions of 0 or 1 as functions
    267           assert( false );
    268         }
    269       } else {
    270         varExpr->accept( *this );
    271         before << "(";
    272         genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
    273         before << ")";
    274       }
    275     } else {
    276       applicationExpr->get_function()->accept( *this );
    277       before << "(";
    278       genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
    279       before << ")";
    280     }
    281   }
    282  
    283   void
    284   CodeGenerator2::visit(UntypedExpr *untypedExpr)
    285   {
    286     if( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
    287       OperatorInfo opInfo;
    288       if( operatorLookup( nameExpr->get_name(), opInfo ) ) {
    289         std::list< Expression* >::iterator arg = untypedExpr->get_args().begin();
    290         switch( opInfo.type ) {
    291         case OT_INDEX:
    292           assert( untypedExpr->get_args().size() == 2 );
    293           (*arg++)->accept( *this );
    294           before << "[";
    295           (*arg)->accept( *this );
    296           before << "]";
    297           break;
    298          
    299         case OT_CALL:
    300           assert( false );
    301           break;
    302          
    303         case OT_PREFIX:
    304         case OT_PREFIXASSIGN:
    305           assert( untypedExpr->get_args().size() == 1 );
    306           before << "(";
    307           before << opInfo.symbol;
    308           (*arg)->accept( *this );
    309           before << ")";
    310           break;
    311          
    312         case OT_POSTFIX:
    313         case OT_POSTFIXASSIGN:
    314           assert( untypedExpr->get_args().size() == 1 );
    315           (*arg)->accept( *this );
    316           before << opInfo.symbol;
    317           break;
    318  
    319         case OT_INFIX:
    320         case OT_INFIXASSIGN:
    321           assert( untypedExpr->get_args().size() == 2 );
    322           before << "(";
    323           (*arg++)->accept( *this );
    324           before << opInfo.symbol;
    325           (*arg)->accept( *this );
    326           before << ")";
    327           break;
    328          
    329         case OT_CONSTANT:
    330           // there are no intrinsic definitions of 0 or 1 as functions
    331           assert( false );
    332         }
    333       } else {
    334         nameExpr->accept( *this );
    335         before << "(";
    336         genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
    337         before << ")";
    338       }
    339     } else {
    340       untypedExpr->get_function()->accept( *this );
    341       before << "(";
    342       genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
    343       before << ")";
    344     }
    345   }
    346  
    347   void
    348   CodeGenerator2::visit(NameExpr *nameExpr)
    349   {
    350     OperatorInfo opInfo;
    351     if( operatorLookup( nameExpr->get_name(), opInfo ) ) {
    352       assert( opInfo.type == OT_CONSTANT );
    353       before << opInfo.symbol;
    354     } else {
    355       before << nameExpr->get_name();
    356     }
    357   }
    358  
    359   void
    360   CodeGenerator2::visit(AddressExpr *addressExpr)
    361   {
    362     before << "(&";
    363     // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address
    364     if( VariableExpr *variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
    365       before << mangleName( variableExpr->get_var() );
    366     } else {
    367       addressExpr->get_arg()->accept( *this );
    368     }
    369     before << ")";
    370   }
    371 
    372   void
    373   CodeGenerator2::visit(CastExpr *castExpr)
    374   {
    375     before << "((";
    376     if( castExpr->get_results().empty() ) {
    377       before << "void" ;
    378     } else {
    379       before << genType( castExpr->get_results().front(), "" );
    380     }
    381     before << ")";
    382     castExpr->get_arg()->accept( *this );
    383     before << ")";
    384   }
    385  
    386   void
    387   CodeGenerator2::visit(UntypedMemberExpr *memberExpr)
    388   {
    389     assert( false );
    390   }
    391  
    392   void
    393   CodeGenerator2::visit(MemberExpr *memberExpr)
    394   {
    395     memberExpr->get_aggregate()->accept( *this );
    396     before << "." << mangleName( memberExpr->get_member() );
    397   }
    398  
    399   void
    400   CodeGenerator2::visit(VariableExpr *variableExpr)
    401   {
    402     OperatorInfo opInfo;
    403     if( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
    404       before << opInfo.symbol;
    405     } else {
    406       before << mangleName( variableExpr->get_var() );
    407     }
    408   }
    409  
    410   void
    411   CodeGenerator2::visit(ConstantExpr *constantExpr)
    412   {
    413     assert( constantExpr->get_constant() );
    414     constantExpr->get_constant()->accept( *this );
    415   }
    416  
    417   void
    418   CodeGenerator2::visit(SizeofExpr *sizeofExpr)
    419   {
    420     before << "sizeof(";
    421     if( sizeofExpr->get_isType() ) {
    422       before << genType( sizeofExpr->get_type(), "" );
    423     } else {
    424       sizeofExpr->get_expr()->accept( *this );
    425     }
    426     before << ")";
    427  }
    428  
    429   void
    430   CodeGenerator2::visit(LogicalExpr *logicalExpr)
    431   {
    432     before << "(";
    433     logicalExpr->get_arg1()->accept( *this );
    434     if( logicalExpr->get_isAnd() ) {
    435       before << " && ";
    436     } else {
    437       before << " || ";
    438     }
    439     logicalExpr->get_arg2()->accept( *this );
    440     before << ")";
    441   }
    442  
    443   void
    444   CodeGenerator2::visit(ConditionalExpr *conditionalExpr)
    445   {
    446     before << "(";
    447     conditionalExpr->get_arg1()->accept( *this );
    448     before << " ? ";
    449     conditionalExpr->get_arg2()->accept( *this );
    450     before << " : ";
    451     conditionalExpr->get_arg3()->accept( *this );
    452     before << ")";
    453   }
    454  
    455   void
    456   CodeGenerator2::visit(CommaExpr *commaExpr)
    457   {
    458     before << "(";
    459     commaExpr->get_arg1()->accept( *this );
    460     before << " , ";
    461     commaExpr->get_arg2()->accept( *this );
    462     before << ")";
    463   }
    464  
    465   void
    466   CodeGenerator2::visit(TupleExpr *tupleExpr)
    467   {
    468   }
    469  
    470   void
    471   CodeGenerator2::visit(TypeExpr *typeExpr)
    472   {
    473   }
    474  
    475  
    476   //*** Statements
    477   void CodeGenerator2::visit(CompoundStmt *compoundStmt){
    478     std::list<Statement*> ks = compoundStmt->get_kids();
    479 
    480     before << endl << string(cur_indent,' ') << "{" << endl;
    481 
    482     cur_indent += CodeGenerator2::tabsize;
    483 
    484     for(std::list<Statement *>::iterator i = ks.begin(); i != ks.end();  i++){
    485       before << string(cur_indent, ' ') << printLabels( (*i)->get_labels() )  ;
    486       (*i)->accept(*this);
    487       shift_left();
    488       before << endl;
    489     }
    490     cur_indent -= CodeGenerator2::tabsize;
    491 
    492     before << string(cur_indent,' ') << "}" << endl;
    493   }
    494 
    495   void CodeGenerator2::visit(ExprStmt *exprStmt){
    496     if(exprStmt != 0){
    497       exprStmt->get_expr()->accept( *this );
    498       shift_left();
    499       before << ";" ;
    500     }
    501   }
    502 
    503   void CodeGenerator2::visit(IfStmt *ifStmt){
    504     before << "if (";
    505     ifStmt->get_condition()->accept(*this);
    506     after << ")" << endl ;
    507     shift_left();
    508 
    509     cur_indent += CodeGenerator2::tabsize;
    510     before << string(cur_indent, ' ');
    511     ifStmt->get_thenPart()->accept(*this);
    512     cur_indent -= CodeGenerator2::tabsize;
    513     shift_left(); before << endl;
    514 
    515     if(ifStmt->get_elsePart() != 0){
    516       before << string(cur_indent, ' ') << " else " << endl ;
    517 
    518       cur_indent += CodeGenerator2::tabsize;
    519       ifStmt->get_elsePart()->accept(*this);
    520       cur_indent -= CodeGenerator2::tabsize;
    521     }
    522   }
    523 
    524   void CodeGenerator2::visit(SwitchStmt *switchStmt){
    525     //before << /* "\r" << */ string(cur_indent, ' ') << CodeGenerator2::printLabels( switchStmt->get_labels() )
    526     before << "switch (" ;
    527     switchStmt->get_condition()->accept(*this);
    528     after << ")" << std::endl ;
    529     shift_left();
    530 
    531     before << string(cur_indent, ' ') << "{" << std::endl;
    532     cur_indent += CodeGenerator2::tabsize;
    533 
    534     std::list< Statement * > stmts = switchStmt->get_branches();
    535     bool lastBreak = false;
    536 
    537     // horrible, horrible hack
    538     if( dynamic_cast<BranchStmt *>(stmts.back()) != 0 ) {
    539       lastBreak = true;
    540       stmts.pop_back();
    541     }
    542     acceptAll(stmts, *this );
    543     if ( lastBreak ) {
    544       Statement *st = switchStmt->get_branches().back();
    545       before << CodeGenerator2::printLabels(st->get_labels());
    546       st->accept(*this);
    547     }
     431        cur_indent -= CodeGenerator2::tabsize;
     432
     433        before << string( cur_indent, ' ' ) << "}" << endl;
     434    }
     435
     436    void CodeGenerator2::visit( ExprStmt *exprStmt ) {
     437        if ( exprStmt != 0 ) {
     438            exprStmt->get_expr()->accept( *this );
     439            shift_left();
     440            before << ";" ;
     441        } // if
     442    }
     443
     444    void CodeGenerator2::visit( IfStmt *ifStmt ) {
     445        before << "if (";
     446        ifStmt->get_condition()->accept(*this );
     447        after += ")\n";
     448        shift_left();
     449
     450        cur_indent += CodeGenerator2::tabsize;
     451        before << string( cur_indent, ' ' );
     452        ifStmt->get_thenPart()->accept(*this );
     453        cur_indent -= CodeGenerator2::tabsize;
     454        shift_left(); before << endl;
     455
     456        if ( ifStmt->get_elsePart() != 0) {
     457            before << string( cur_indent, ' ' ) << " else " << endl ;
     458
     459            cur_indent += CodeGenerator2::tabsize;
     460            ifStmt->get_elsePart()->accept(*this );
     461            cur_indent -= CodeGenerator2::tabsize;
     462        } // if
     463    }
     464
     465    void CodeGenerator2::visit( SwitchStmt *switchStmt ) {
     466        //before << /* "\r" << */ string( cur_indent, ' ' ) << CodeGenerator2::printLabels( switchStmt->get_labels() )
     467        before << "switch (" ;
     468        switchStmt->get_condition()->accept(*this );
     469        after += ")\n";
     470        shift_left();
     471
     472        before << string( cur_indent, ' ' ) << "{" << std::endl;
     473        cur_indent += CodeGenerator2::tabsize;
     474
     475        std::list< Statement * > stmts = switchStmt->get_branches();
     476        bool lastBreak = false;
     477
     478        // horrible, horrible hack
     479        if ( dynamic_cast<BranchStmt *>( stmts.back()) != 0 ) {
     480            lastBreak = true;
     481            stmts.pop_back();
     482        } // if
     483        acceptAll( stmts, *this );
     484        if ( lastBreak ) {
     485            Statement *st = switchStmt->get_branches().back();
     486            before << CodeGenerator2::printLabels( st->get_labels());
     487            st->accept(*this );
     488        } // if
    548489     
    549     cur_indent -= CodeGenerator2::tabsize;
    550 
    551     before << /* "\r" << */ string(cur_indent, ' ') << "}" << endl ;
    552   }
    553 
    554   void CodeGenerator2::visit(CaseStmt *caseStmt){
    555     before << string(cur_indent, ' ');
    556     if (caseStmt->isDefault())
    557       before << "default "  ;
    558     else {
    559       before << "case "  ;
    560       caseStmt->get_condition()->accept(*this);
    561     }
    562     after << ":" << std::endl ;
    563     shift_left();
    564 
    565     std::list<Statement *> sts = caseStmt->get_statements();
    566 
    567     cur_indent += CodeGenerator2::tabsize;
    568     for(std::list<Statement *>::iterator i = sts.begin(); i != sts.end();  i++){
    569       before << /* "\r" << */ string(cur_indent, ' ') << printLabels( (*i)->get_labels() )  ;
    570       (*i)->accept(*this);
    571       shift_left();
    572       before << ";" << endl;
    573     }
    574     cur_indent -= CodeGenerator2::tabsize;
    575   }
    576 
    577   void CodeGenerator2::visit(BranchStmt *branchStmt){
    578     switch(branchStmt->get_type()){
    579     case BranchStmt::Goto:
    580       if( ! branchStmt->get_target().empty() )
    581         before << "goto " << branchStmt->get_target();
    582       else {
    583         if ( branchStmt->get_computedTarget() != 0 ) {
    584           before << "goto *";
    585           branchStmt->get_computedTarget()->accept(*this);
     490        cur_indent -= CodeGenerator2::tabsize;
     491
     492        before << /* "\r" << */ string( cur_indent, ' ' ) << "}" << endl ;
     493    }
     494
     495    void CodeGenerator2::visit( CaseStmt *caseStmt ) {
     496        before << string( cur_indent, ' ' );
     497        if ( caseStmt->isDefault())
     498            before << "default "  ;
     499        else {
     500            before << "case "  ;
     501            caseStmt->get_condition()->accept(*this );
     502        } // if
     503        after += ":\n";
     504        shift_left();
     505
     506        std::list<Statement *> sts = caseStmt->get_statements();
     507
     508        cur_indent += CodeGenerator2::tabsize;
     509        for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end();  i++) {
     510            before << /* "\r" << */ string( cur_indent, ' ' ) << printLabels( (*i)->get_labels() )  ;
     511            (*i)->accept(*this );
     512            shift_left();
     513            before << ";" << endl;
    586514        }
    587       }
    588       break;
    589     case BranchStmt::Break:
    590       before << "break";
    591       break;
    592     case BranchStmt::Continue:
    593       before << "continue";
    594       break;
    595     }
    596     before << ";";
    597   }
    598 
    599 
    600   void CodeGenerator2::visit(ReturnStmt *returnStmt){
    601     before << "return ";
    602 
    603     // xxx -- check for null expression;
    604     if ( returnStmt->get_expr() ){
    605       returnStmt->get_expr()->accept( *this );
    606     }
    607     after << ";";
    608   }
    609 
    610   void CodeGenerator2::visit(WhileStmt *whileStmt){
    611     if( whileStmt->get_isDoWhile() )
    612       before << "do" ;
    613     else {
    614       before << "while(" ;
    615       whileStmt->get_condition()->accept(*this);
    616       after << ")";
    617     }
    618     after << "{" << endl  ;
    619     shift_left();
    620 
    621     whileStmt->get_body()->accept( *this );
    622 
    623     before << /* "\r" << */ string(cur_indent, ' ') << "}" ;
    624 
    625     if( whileStmt->get_isDoWhile() ){
    626       before << " while(" ;
    627       whileStmt->get_condition()->accept(*this);
    628       after << ");";
    629     }
    630 
    631     after << endl;
    632   }
    633 
    634   void CodeGenerator2::visit(ForStmt *forStmt){
    635     before << "for (";
    636 
    637     if( forStmt->get_initialization() != 0 )
    638       forStmt->get_initialization()->accept( *this );
    639     else
    640       before << ";";
    641     shift_left();
    642 
    643     if( forStmt->get_condition() != 0 )
    644       forStmt->get_condition()->accept( *this );
    645     shift_left(); before << ";";
    646 
    647     if( forStmt->get_increment() != 0 )
    648       forStmt->get_increment()->accept( *this );
    649     shift_left(); before << ")" << endl;
    650 
    651     if( forStmt->get_body() != 0 ){
    652       cur_indent += CodeGenerator2::tabsize;
    653       before << string(cur_indent, ' ') << CodeGenerator2::printLabels( forStmt->get_body()->get_labels() );
    654       forStmt->get_body()->accept( *this );
    655       cur_indent -= CodeGenerator2::tabsize;
    656     }
    657   }
    658 
    659   void CodeGenerator2::visit(NullStmt *nullStmt){
    660     //before << /* "\r" << */ string(cur_indent, ' ') << CodeGenerator2::printLabels( nullStmt->get_labels() );
    661     before << "/* null statement */ ;";
    662   }
    663 
    664   void CodeGenerator2::visit(DeclStmt *declStmt){
    665     declStmt->get_decl()->accept( *this );
     515        cur_indent -= CodeGenerator2::tabsize;
     516    }
     517
     518    void CodeGenerator2::visit( BranchStmt *branchStmt ) {
     519        switch ( branchStmt->get_type()) {
     520          case BranchStmt::Goto:
     521            if ( ! branchStmt->get_target().empty() )
     522                before << "goto " << branchStmt->get_target();
     523            else {
     524                if ( branchStmt->get_computedTarget() != 0 ) {
     525                    before << "goto *";
     526                    branchStmt->get_computedTarget()->accept(*this );
     527                } // if
     528            } // if
     529            break;
     530          case BranchStmt::Break:
     531            before << "break";
     532            break;
     533          case BranchStmt::Continue:
     534            before << "continue";
     535            break;
     536        }
     537        before << ";";
     538    }
     539
     540
     541    void CodeGenerator2::visit( ReturnStmt *returnStmt ) {
     542        before << "return ";
     543
     544        // xxx -- check for null expression;
     545        if ( returnStmt->get_expr() ) {
     546            returnStmt->get_expr()->accept( *this );
     547        } // if
     548        after += ";";
     549    }
     550
     551    void CodeGenerator2::visit( WhileStmt *whileStmt ) {
     552        if ( whileStmt->get_isDoWhile() )
     553            before << "do" ;
     554        else {
     555            before << "while(" ;
     556            whileStmt->get_condition()->accept(*this );
     557            after += ")";
     558        } // if
     559        after += "{\n";
     560        shift_left();
     561
     562        whileStmt->get_body()->accept( *this );
     563
     564        before << /* "\r" << */ string( cur_indent, ' ' ) << "}" ;
     565
     566        if ( whileStmt->get_isDoWhile() ) {
     567            before << " while(" ;
     568            whileStmt->get_condition()->accept(*this );
     569            after += ");";
     570        } // if
     571
     572        after += "\n";
     573    }
     574
     575    void CodeGenerator2::visit( ForStmt *forStmt ) {
     576        before << "for (";
     577
     578        if ( forStmt->get_initialization() != 0 )
     579            forStmt->get_initialization()->accept( *this );
     580        else
     581            before << ";";
     582        shift_left();
     583
     584        if ( forStmt->get_condition() != 0 )
     585            forStmt->get_condition()->accept( *this );
     586        shift_left(); before << ";";
     587
     588        if ( forStmt->get_increment() != 0 )
     589            forStmt->get_increment()->accept( *this );
     590        shift_left(); before << ")" << endl;
     591
     592        if ( forStmt->get_body() != 0 ) {
     593            cur_indent += CodeGenerator2::tabsize;
     594            before << string( cur_indent, ' ' ) << CodeGenerator2::printLabels( forStmt->get_body()->get_labels() );
     595            forStmt->get_body()->accept( *this );
     596            cur_indent -= CodeGenerator2::tabsize;
     597        } // if
     598    }
     599
     600    void CodeGenerator2::visit( NullStmt *nullStmt ) {
     601        //before << /* "\r" << */ string( cur_indent, ' ' ) << CodeGenerator2::printLabels( nullStmt->get_labels() );
     602        before << "/* null statement */ ;";
     603    }
     604
     605    void CodeGenerator2::visit( DeclStmt *declStmt ) {
     606        declStmt->get_decl()->accept( *this );
    666607   
    667     if( doSemicolon( declStmt->get_decl() ) ) {
    668       after << ";";
    669     }
    670     shift_left();
    671   }
    672 
    673   std::string CodeGenerator2::printLabels ( std::list< Label > &l ) {
    674     std::string str("");
    675     l.unique();
    676 
    677     for( std::list< Label >::iterator i = l.begin(); i != l.end(); i++)
    678       str += *i + ": ";
    679 
    680     return str;
    681   }
    682 
    683   void CodeGenerator2::shift_left(){
    684     before << string(after.str(), after.pcount());
    685 
    686     after.freeze( false );
    687     after.seekp(0);
    688   }
    689 
    690   void
    691   CodeGenerator2::handleStorageClass( Declaration *decl )
    692   {
    693     switch( decl->get_storageClass() ) {
    694     case Declaration::NoStorageClass:
    695       break;
    696     case Declaration::Auto:
    697       break;
    698     case Declaration::Static:
    699       before << "static ";
    700       break;
    701     case Declaration::Extern:
    702       before << "extern ";
    703       break;
    704     case Declaration::Register:
    705       before << "register ";
    706       break;
    707     case Declaration::Fortran:
    708       before << "fortran ";
    709       break;
    710     }
    711   }
    712 
     608        if ( doSemicolon( declStmt->get_decl() ) ) {
     609            after += ";";
     610        } // if
     611        shift_left();
     612    }
     613
     614    std::string CodeGenerator2::printLabels( std::list< Label > &l ) {
     615        std::string str( "" );
     616        l.unique();
     617
     618        for ( std::list< Label >::iterator i = l.begin(); i != l.end(); i++ )
     619            str += *i + ": ";
     620
     621        return str;
     622    }
     623
     624    void CodeGenerator2::shift_left() {
     625        before << after;
     626        after = "";
     627    }
     628
     629    void CodeGenerator2::handleStorageClass( Declaration *decl ) {
     630        switch ( decl->get_storageClass() ) {
     631          case Declaration::NoStorageClass:
     632            break;
     633          case Declaration::Auto:
     634            break;
     635          case Declaration::Static:
     636            before << "static ";
     637            break;
     638          case Declaration::Extern:
     639            before << "extern ";
     640            break;
     641          case Declaration::Register:
     642            before << "register ";
     643            break;
     644          case Declaration::Fortran:
     645            before << "fortran ";
     646            break;
     647        }
     648    }
    713649} // namespace CodeGen
    714 
    715 
  • translator/CodeGen/CodeGenerator2.h

    rd9a0e76 r17cd4eb  
    1010
    1111namespace CodeGen {
     12    class CodeGenerator2 : public Visitor {
     13      public:
     14        static int tabsize;
    1215
    13   class CodeGenerator2 : public Visitor
    14     {
    15     public:
    16       static int tabsize;
     16        CodeGenerator2( std::ostream &os );
     17        CodeGenerator2( std::ostream &os, std::string, int indent = 0, bool infun = false );
     18        CodeGenerator2( std::ostream &os, char *, int indent = 0, bool infun = false );
    1719
    18       CodeGenerator2( std::ostream &os );
    19       CodeGenerator2(std::ostream &os, std::string, int indent = 0, bool infun = false );
    20       CodeGenerator2(std::ostream &os, char *, int indent = 0, bool infun = false );
     20        CodeGenerator2( CodeGenerator2 & );
    2121
    22       CodeGenerator2( CodeGenerator2 & );
     22        //*** Declaration
     23        virtual void visit( StructDecl * );
     24        virtual void visit( FunctionDecl * );
     25        virtual void visit( ObjectDecl * );
     26        virtual void visit( UnionDecl *aggregateDecl );
     27        virtual void visit( EnumDecl *aggregateDecl );
     28        virtual void visit( ContextDecl *aggregateDecl );
     29        virtual void visit( TypedefDecl *typeDecl );
     30        virtual void visit( TypeDecl *typeDecl );
    2331
    24       virtual ~CodeGenerator2();
     32        //*** Initializer
     33        virtual void visit( SingleInit * );
     34        virtual void visit( ListInit * );
    2535
    26       //*** Declaration
    27       virtual void visit(StructDecl *);
    28       virtual void visit(FunctionDecl *);
    29       virtual void visit(ObjectDecl *);
    30       virtual void visit(UnionDecl *aggregateDecl);
    31       virtual void visit(EnumDecl *aggregateDecl);
    32       virtual void visit(ContextDecl *aggregateDecl);
    33       virtual void visit(TypedefDecl *typeDecl);
    34       virtual void visit(TypeDecl *typeDecl);
     36        //*** Constant
     37        virtual void visit( Constant * );
    3538
    36       //*** Initializer
    37       virtual void visit(SingleInit *);
    38       virtual void visit(ListInit *);
     39        //*** Expression
     40        virtual void visit( ApplicationExpr *applicationExpr );
     41        virtual void visit( UntypedExpr *untypedExpr );
     42        virtual void visit( NameExpr *nameExpr );
     43        virtual void visit( AddressExpr *addressExpr );
     44        virtual void visit( CastExpr *castExpr );
     45        virtual void visit( UntypedMemberExpr *memberExpr );
     46        virtual void visit( MemberExpr *memberExpr );
     47        virtual void visit( VariableExpr *variableExpr );
     48        virtual void visit( ConstantExpr *constantExpr );
     49        virtual void visit( SizeofExpr *sizeofExpr );
     50        virtual void visit( LogicalExpr *logicalExpr );
     51        virtual void visit( ConditionalExpr *conditionalExpr );
     52        virtual void visit( CommaExpr *commaExpr );
     53        virtual void visit( TupleExpr *tupleExpr );
     54        virtual void visit( TypeExpr *typeExpr );
    3955
    40       //*** Constant
    41       virtual void visit(Constant *);
     56        //*** Statements
     57        virtual void visit( CompoundStmt * );
     58        virtual void visit( ExprStmt * );
     59        virtual void visit( IfStmt * );
     60        virtual void visit( SwitchStmt * );
     61        virtual void visit( CaseStmt * );
     62        virtual void visit( BranchStmt * );
     63        virtual void visit( ReturnStmt * );
     64        virtual void visit( WhileStmt * );
     65        virtual void visit( ForStmt * );
     66        virtual void visit( NullStmt * );
     67        virtual void visit( DeclStmt * );
    4268
    43       //*** Expression
    44       virtual void visit(ApplicationExpr *applicationExpr);
    45       virtual void visit(UntypedExpr *untypedExpr);
    46       virtual void visit(NameExpr *nameExpr);
    47       virtual void visit(AddressExpr *addressExpr);
    48       virtual void visit(CastExpr *castExpr);
    49       virtual void visit(UntypedMemberExpr *memberExpr);
    50       virtual void visit(MemberExpr *memberExpr);
    51       virtual void visit(VariableExpr *variableExpr);
    52       virtual void visit(ConstantExpr *constantExpr);
    53       virtual void visit(SizeofExpr *sizeofExpr);
    54       virtual void visit(LogicalExpr *logicalExpr);
    55       virtual void visit(ConditionalExpr *conditionalExpr);
    56       virtual void visit(CommaExpr *commaExpr);
    57       virtual void visit(TupleExpr *tupleExpr);
    58       virtual void visit(TypeExpr *typeExpr);
     69        std::string get_string( void );
     70        void add_string_left( std::string s ) { before << s; }
     71        void shift_left();
     72        template< class Iterator > void genCommaList( Iterator begin, Iterator end );
     73      private:
     74        int cur_indent;
     75        bool insideFunction;
     76        std::ostream &before;
     77        std::string after;
    5978
    60       //*** Statements
    61       virtual void visit(CompoundStmt *);
    62       virtual void visit(ExprStmt *);
    63       virtual void visit(IfStmt *);
    64       virtual void visit(SwitchStmt *);
    65       virtual void visit(CaseStmt *);
    66       virtual void visit(BranchStmt *);
    67       virtual void visit(ReturnStmt *);
    68       virtual void visit(WhileStmt *);
    69       virtual void visit(ForStmt *);
    70       virtual void visit(NullStmt *);
    71       virtual void visit(DeclStmt *);
    72 
    73       std::string get_string(void);
    74       void add_string_left(std::string s) { before << s; }
    75       void shift_left();
    76       template< class Iterator > void genCommaList( Iterator begin, Iterator end );
    77 
    78     private:
    79       int cur_indent;
    80       bool insideFunction;
    81       std::ostream &before;
    82       std::ostrstream after;
    83 
    84       static std::string printLabels ( std::list < Label > & );
    85       void handleStorageClass( Declaration *decl );
    86       void handleAggregate( AggregateDecl *aggDecl );
    87       void handleTypedef( NamedTypeDecl *namedType );
     79        static std::string printLabels ( std::list < Label > & );
     80        void handleStorageClass( Declaration *decl );
     81        void handleAggregate( AggregateDecl *aggDecl );
     82        void handleTypedef( NamedTypeDecl *namedType );
    8883
    8984    };
    9085   
    91   template< class Iterator >
    92   void
    93   CodeGenerator2::genCommaList( Iterator begin, Iterator end )
    94   {
    95     if( begin == end ) return;
    96    
    97     for( ;; ) {
    98       (*begin++)->accept( *this );
    99      
    100       if( begin == end ) return;
    101      
    102       before << ", ";
     86    template< class Iterator >
     87    void CodeGenerator2::genCommaList( Iterator begin, Iterator end ) {
     88        if ( begin == end ) return;
     89
     90        for ( ;; ) {
     91            (*begin++)->accept( *this );
     92            if ( begin == end ) return;
     93            before << ", ";
     94        }
    10395    }
    104   }
    10596 
    106   inline bool
    107   doSemicolon( Declaration* decl )
    108   {
    109     if( FunctionDecl* func = dynamic_cast< FunctionDecl* >( decl ) ) {
    110       return !func->get_statements();
     97    inline bool doSemicolon( Declaration* decl ) {
     98        if ( FunctionDecl* func = dynamic_cast< FunctionDecl* >( decl ) ) {
     99            return !func->get_statements();
     100        }
     101        return true;
    111102    }
    112     return true;
    113   }
    114  
    115103} // namespace CodeGen
    116104
  • translator/CodeGen/GenType.cc

    rd9a0e76 r17cd4eb  
    1 /*
    2  * This file is part of the Cforall project
    3  *
    4  * $Id: GenType.cc,v 1.6 2005/08/29 20:14:12 rcbilson Exp $
    5  *
    6  */
    7 
    81#include <strstream>
    92#include <cassert>
     
    169
    1710namespace CodeGen {
     11    class GenType : public Visitor {
     12      public:
     13        GenType( const std::string &typeString );
     14        std::string get_typeString() const { return typeString; }
     15        void set_typeString( const std::string &newValue ) { typeString = newValue; }
     16 
     17        virtual void visit( FunctionType *funcType );
     18        virtual void visit( VoidType *voidType );
     19        virtual void visit( BasicType *basicType );
     20        virtual void visit( PointerType *pointerType );
     21        virtual void visit( ArrayType *arrayType );
     22        virtual void visit( StructInstType *structInst );
     23        virtual void visit( UnionInstType *unionInst );
     24        virtual void visit( EnumInstType *enumInst );
     25        virtual void visit( TypeInstType *typeInst );
     26 
     27      private:
     28        void handleQualifiers( Type *type );
     29        void genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic );
     30 
     31        std::string typeString;
     32    };
    1833
    19 class GenType : public Visitor
    20 {
    21 public:
    22   GenType( const std::string &typeString );
    23   std::string get_typeString() const { return typeString; }
    24   void set_typeString( const std::string &newValue ) { typeString = newValue; }
     34    std::string genType( Type *type, const std::string &baseString ) {
     35        GenType gt( baseString );
     36        type->accept( gt );
     37        return gt.get_typeString();
     38    }
     39
     40    GenType::GenType( const std::string &typeString ) : typeString( typeString ) {}
     41
     42    void GenType::visit( VoidType *voidType ) {
     43        typeString = "void " + typeString;
     44        handleQualifiers( voidType );
     45    }
     46
     47    void GenType::visit( BasicType *basicType ) {
     48        BasicType::Kind kind = basicType->get_kind();
     49        assert( 0 <= kind && kind < BasicType::NUMBER_OF_BASIC_TYPES );
     50        typeString = std::string( BasicType::typeNames[kind] ) + " " + typeString;
     51        handleQualifiers( basicType );
     52    }
     53
     54    void GenType::genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic ) {
     55        std::ostrstream os;
     56        if ( typeString != "" ) {
     57            if ( typeString[ 0 ] == '*' ) {
     58                os << "(" << typeString << ")";
     59            } else {
     60                os << typeString;
     61            } // if
     62        } // if
     63        os << "[";
     64
     65        if ( isStatic ) {
     66            os << "static ";
     67        } // if
     68        if ( qualifiers.isConst ) {
     69            os << "const ";
     70        } // if
     71        if ( qualifiers.isVolatile ) {
     72            os << "volatile ";
     73        } // if
     74        if ( qualifiers.isRestrict ) {
     75            os << "__restrict ";
     76        } // if
     77        if ( isVarLen ) {
     78            os << "*";
     79        } // if
     80        if ( dimension != 0 ) {
     81            CodeGenerator2 cg( os );
     82            dimension->accept( cg );
     83        } // if
     84        os << "]";
     85
     86        typeString = std::string( os.str(), os.pcount() );
    2587 
    26   virtual void visit( FunctionType *funcType );
    27   virtual void visit( VoidType *voidType );
    28   virtual void visit( BasicType *basicType );
    29   virtual void visit( PointerType *pointerType );
    30   virtual void visit( ArrayType *arrayType );
    31   virtual void visit( StructInstType *structInst );
    32   virtual void visit( UnionInstType *unionInst );
    33   virtual void visit( EnumInstType *enumInst );
    34   virtual void visit( TypeInstType *typeInst );
     88        base->accept( *this );
     89    }
     90
     91    void GenType::visit( PointerType *pointerType ) {
     92        assert( pointerType->get_base() != 0);
     93        if ( pointerType->get_isStatic() || pointerType->get_isVarLen() || pointerType->get_dimension() ) {
     94            genArray( pointerType->get_qualifiers(), pointerType->get_base(), pointerType->get_dimension(), pointerType->get_isVarLen(), pointerType->get_isStatic() );
     95        } else {
     96            handleQualifiers( pointerType );
     97            if ( typeString[ 0 ] == '?' ) {
     98                typeString = "* " + typeString;
     99            } else {
     100                typeString = "*" + typeString;
     101            } // if
     102            pointerType->get_base()->accept( *this );
     103        } // if
     104    }
     105
     106    void GenType::visit( ArrayType *arrayType ){
     107        genArray( arrayType->get_qualifiers(), arrayType->get_base(), arrayType->get_dimension(), arrayType->get_isVarLen(), arrayType->get_isStatic() );
     108    }
     109
     110    void GenType::visit( FunctionType *funcType ) {
     111        std::ostrstream os;
     112
     113        if ( typeString != "" ) {
     114            if ( typeString[ 0 ] == '*' ) {
     115                os << "(" << typeString << ")";
     116            } else {
     117                os << typeString;
     118            } // if
     119        } // if
    35120 
    36 private:
    37   void handleQualifiers( Type *type );
    38   void genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic );
     121        /************* parameters ***************/
     122
     123        const std::list<DeclarationWithType *> &pars = funcType->get_parameters();
     124
     125        if ( pars.empty() ) {
     126            if ( funcType->get_isVarArgs() ) {
     127                os << "()";
     128            } else {
     129                os << "(void)";
     130            } // if
     131        } else {
     132            CodeGenerator2 cg( os );
     133            os << "(" ;
     134
     135            cg.genCommaList( pars.begin(), pars.end() );
     136
     137            if ( funcType->get_isVarArgs() ){
     138                os << ", ...";
     139            } // if
     140            os << ")";
     141        } // if
    39142 
    40   std::string typeString;
    41 };
     143        typeString = std::string( os.str(), os.pcount() );
    42144
    43 std::string
    44 genType( Type *type, const std::string &baseString )
    45 {
    46   GenType gt( baseString );
    47   type->accept( gt );
    48   return gt.get_typeString();
    49 }
     145        if ( funcType->get_returnVals().size() == 0 ) {
     146            typeString = "void " + typeString;
     147        } else {
     148            funcType->get_returnVals().front()->get_type()->accept( *this );
     149        } // if
     150    }
    50151
    51 GenType::GenType( const std::string &typeString )
    52   : typeString( typeString )
    53 {
    54 }
     152    void GenType::visit( StructInstType *structInst )  {
     153        typeString = "struct " + structInst->get_name() + " " + typeString;
     154        handleQualifiers( structInst );
     155    }
    55156
    56 void GenType::visit(VoidType *voidType){
    57   typeString = "void " + typeString;
    58   handleQualifiers( voidType );
    59 }
     157    void GenType::visit( UnionInstType *unionInst ) {
     158        typeString = "union " + unionInst->get_name() + " " + typeString;
     159        handleQualifiers( unionInst );
     160    }
    60161
    61 void GenType::visit(BasicType *basicType)
    62 {
    63   std::string typeWords;
    64   switch(basicType->get_kind()){
    65   case BasicType::Bool:
    66     typeWords = "_Bool";
    67     break;
    68   case BasicType::Char:
    69   case BasicType::SignedChar:
    70     typeWords = "char";
    71     break;
    72   case BasicType::UnsignedChar:
    73     typeWords = "unsigned char";
    74     break;
    75   case BasicType::ShortSignedInt:
    76     typeWords = "short";
    77     break;
    78   case BasicType::ShortUnsignedInt:
    79     typeWords = "short unsigned";
    80     break;
    81   case BasicType::SignedInt:
    82     typeWords = "int";
    83     break;
    84   case BasicType::UnsignedInt:
    85     typeWords = "unsigned int";
    86     break;
    87   case BasicType::LongSignedInt:
    88     typeWords = "long int";
    89     break;
    90   case BasicType::LongUnsignedInt:
    91     typeWords = "long unsigned int";
    92     break;
    93   case BasicType::LongLongSignedInt:
    94     typeWords = "long long int";
    95     break;
    96   case BasicType::LongLongUnsignedInt:
    97     typeWords = "long long unsigned int";
    98     break;
    99   case BasicType::Float:
    100     typeWords = "float";
    101     break;
    102   case BasicType::Double:
    103     typeWords = "double";
    104     break;
    105   case BasicType::LongDouble:
    106     typeWords = "long double";
    107     break;
    108   case BasicType::FloatComplex:
    109     typeWords = "float _Complex";
    110     break;
    111   case BasicType::DoubleComplex:
    112     typeWords = "double _Complex";
    113     break;
    114   case BasicType::LongDoubleComplex:
    115     typeWords = "long double _Complex";
    116     break;
    117   case BasicType::FloatImaginary:
    118     typeWords = "float _Imaginary";
    119     break;
    120   case BasicType::DoubleImaginary:
    121     typeWords = "double _Imaginary";
    122     break;
    123   case BasicType::LongDoubleImaginary:
    124     typeWords = "long double _Imaginary";
    125     break;
    126   default:
    127     assert( false );
    128   }
    129   typeString = typeWords + " " + typeString;
    130   handleQualifiers( basicType );
    131 }
     162    void GenType::visit( EnumInstType *enumInst ) {
     163        typeString = "enum " + enumInst->get_name() + " " + typeString;
     164        handleQualifiers( enumInst );
     165    }
    132166
    133 void
    134 GenType::genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic )
    135 {
    136   std::ostrstream os;
    137   if( typeString != "" ) {
    138     if( typeString[ 0 ] == '*' ) {
    139       os << "(" << typeString << ")";
    140     } else {
    141       os << typeString;
     167    void GenType::visit( TypeInstType *typeInst ) {
     168        typeString = typeInst->get_name() + " " + typeString;
     169        handleQualifiers( typeInst );
    142170    }
    143   }
    144   os << "[";
    145   if( isStatic ) {
    146     os << "static ";
    147   }
    148   if( qualifiers.isConst ) {
    149     os << "const ";
    150   }
    151   if( qualifiers.isVolatile ) {
    152     os << "volatile ";
    153   }
    154   if( isVarLen ) {
    155     os << "*";
    156   }
    157   if( dimension != 0 ) {
    158     CodeGenerator2 cg( os );
    159     dimension->accept( cg );
    160   }
    161   os << "]";
    162  
    163   typeString = std::string( os.str(), os.pcount() );
    164  
    165   base->accept ( *this );
    166 }
    167171
    168 void GenType::visit(PointerType *pointerType) {
    169   assert(pointerType->get_base() != 0);
    170   if( pointerType->get_isStatic() || pointerType->get_isVarLen() || pointerType->get_dimension() ) {
    171     genArray( pointerType->get_qualifiers(), pointerType->get_base(), pointerType->get_dimension(), pointerType->get_isVarLen(), pointerType->get_isStatic() );
    172   } else {
    173     handleQualifiers( pointerType );
    174     if( typeString[ 0 ] == '?' ) {
    175       typeString = "* " + typeString;
    176     } else {
    177       typeString = "*" + typeString;
     172    void GenType::handleQualifiers( Type *type ) {
     173        if ( type->get_isConst() ) {
     174            typeString = "const " + typeString;
     175        } // if
     176        if ( type->get_isVolatile() ) {
     177            typeString = "volatile " + typeString;
     178        } // if
     179        if ( type->get_isRestrict() ) {
     180            typeString = "__restrict " + typeString;
     181        } // if
    178182    }
    179     pointerType->get_base()->accept ( *this );
    180   }
    181 }
    182 
    183 void GenType::visit(ArrayType *arrayType){
    184   genArray( arrayType->get_qualifiers(), arrayType->get_base(), arrayType->get_dimension(), arrayType->get_isVarLen(), arrayType->get_isStatic() );
    185 }
    186 
    187 void GenType::visit(FunctionType *funcType) {
    188   std::ostrstream os;
    189 
    190   if( typeString != "" ) {
    191     if( typeString[ 0 ] == '*' ) {
    192       os << "(" << typeString << ")";
    193     } else {
    194       os << typeString;
    195     }
    196   }
    197  
    198   /************* parameters ***************/
    199 
    200   const std::list<DeclarationWithType*> &pars = funcType->get_parameters();
    201 
    202   if( pars.empty() ) {
    203     if( funcType->get_isVarArgs() ) {
    204       os << "()";
    205     } else {
    206       os << "(void)";
    207     }
    208   } else {
    209     CodeGenerator2 cg( os );
    210     os << "(" ;
    211 
    212     cg.genCommaList( pars.begin(), pars.end() );
    213    
    214     if( funcType->get_isVarArgs() ){
    215       os << ", ...";
    216     }
    217     os << ")";
    218   }
    219  
    220   typeString = std::string( os.str(), os.pcount() );
    221 
    222   if( funcType->get_returnVals().size() == 0 ) {
    223     typeString = "void " + typeString;
    224   } else {
    225 
    226     funcType->get_returnVals().front()->get_type()->accept( *this );
    227 
    228   }
    229  
    230 }
    231 
    232 void GenType::visit( StructInstType *structInst )
    233 {
    234   typeString = "struct " + structInst->get_name() + " " + typeString;
    235   handleQualifiers( structInst );
    236 }
    237 
    238 void
    239 GenType::visit( UnionInstType *unionInst )
    240 {
    241   typeString = "union " + unionInst->get_name() + " " + typeString;
    242   handleQualifiers( unionInst );
    243 }
    244 
    245 void
    246 GenType::visit( EnumInstType *enumInst )
    247 {
    248   typeString = "enum " + enumInst->get_name() + " " + typeString;
    249   handleQualifiers( enumInst );
    250 }
    251 
    252 void
    253 GenType::visit( TypeInstType *typeInst )
    254 {
    255   typeString = typeInst->get_name() + " " + typeString;
    256   handleQualifiers( typeInst );
    257 }
    258 
    259 void
    260 GenType::handleQualifiers( Type *type )
    261 {
    262   if( type->get_isConst() ) {
    263     typeString = "const " + typeString;
    264   }
    265   if( type->get_isVolatile() ) {
    266     typeString = "volatile " + typeString;
    267   }
    268   if( type->get_isRestrict() ) {
    269     typeString = "__restrict " + typeString;
    270   }
    271 }
    272 
    273183} // namespace CodeGen
  • translator/CodeGen/GenType.h

    rd9a0e76 r17cd4eb  
    1 /*
    2  * This file is part of the Cforall project
    3  *
    4  * $Id: GenType.h,v 1.2 2005/08/29 20:14:12 rcbilson Exp $
    5  *
    6  */
    7 
    81#ifndef CODEGEN_GENTYPE_H
    92#define CODEGEN_GENTYPE_H
     
    136
    147namespace CodeGen {
    15 
    16 std::string genType( Type *type, const std::string &baseString );
    17 
     8    std::string genType( Type *type, const std::string &baseString );
    189} // namespace CodeGen
    1910
    20 #endif /* #ifndef CODEGEN_GENTYPE_H */
     11#endif // CODEGEN_GENTYPE_H
  • translator/CodeGen/Generate.cc

    rd9a0e76 r17cd4eb  
    1212
    1313namespace CodeGen {
     14    void generate( std::list< Declaration* > translationUnit, std::ostream &os, bool doIntrinsics ) {
     15        CodeGen::CodeGenerator2 cgv( os );
    1416
    15   void generate( std::list< Declaration* > translationUnit, std::ostream &os, bool doIntrinsics ) {
    16 
    17     CodeGen::CodeGenerator2 cgv( os );
    18 
    19     for(std::list<Declaration *>::iterator i = translationUnit.begin(); i != translationUnit.end();  i++)
    20       {
    21         if( LinkageSpec::isGeneratable( (*i)->get_linkage() ) && (doIntrinsics || !LinkageSpec::isBuiltin( (*i)->get_linkage() ) ) ) {
    22           (*i)->accept(cgv);
    23           cgv.shift_left();
    24           if( doSemicolon( *i ) ) {
    25             os << ";";
    26           }
    27           os << std::endl;
    28         }
    29       }
    30 
    31   }
    32 
     17        for ( std::list<Declaration *>::iterator i = translationUnit.begin(); i != translationUnit.end();  i++ ) {
     18            if ( LinkageSpec::isGeneratable( (*i)->get_linkage() ) && (doIntrinsics || ! LinkageSpec::isBuiltin( (*i)->get_linkage() ) ) ) {
     19                (*i)->accept(cgv);
     20                cgv.shift_left();
     21                if ( doSemicolon( *i ) ) {
     22                    os << ";";
     23                } // if
     24                os << std::endl;
     25            } // if
     26        } // for
     27    }
    3328} // namespace CodeGen
    34 
    35 
  • translator/GenPoly/CopyParams.cc

    rd9a0e76 r17cd4eb  
    1 /*
    2  * This file is part of the Cforall project
    3  *
    4  * $Id: CopyParams.cc,v 1.3 2005/08/29 20:14:13 rcbilson Exp $
    5  *
    6  */
    7 
    81#include <set>
    92#include <map>
     
    1912
    2013namespace GenPoly {
     14    class CopyParams : public Visitor {
     15      public:
     16        CopyParams();
     17 
     18        virtual void visit( FunctionDecl *funcDecl );
     19        virtual void visit( AddressExpr *addrExpr );
    2120
    22 class CopyParams : public Visitor
    23 {
    24 public:
    25   CopyParams();
    26  
    27   virtual void visit( FunctionDecl *funcDecl );
    28   virtual void visit( AddressExpr *addrExpr );
     21      private:
     22        std::set< UniqueId > modVars;
     23        UniqueName namer;
     24    };
    2925
    30 private:
    31   std::set< UniqueId > modVars;
    32   UniqueName namer;
    33 };
     26    void copyParams( std::list< Declaration* > &translationUnit ) {
     27        CopyParams copier;
     28        acceptAll( translationUnit, copier );
     29    }
    3430
    35 void
    36 copyParams( std::list< Declaration* > &translationUnit )
    37 {
    38   CopyParams copier;
    39   acceptAll( translationUnit, copier );
    40 }
     31    CopyParams::CopyParams() : namer( "_cp" ) {}
    4132
    42 CopyParams::CopyParams()
    43   : namer( "_cp" )
    44 {
    45 }
     33    static const std::list< Label > noLabels;
    4634
    47 static const std::list< Label > noLabels;
     35    void CopyParams::visit( FunctionDecl *funcDecl ) {
     36        if ( funcDecl->get_statements() ) {
     37            funcDecl->get_statements()->accept( *this );
     38   
     39            if ( ! modVars.empty() ) {
     40                std::map< std::string, DeclarationWithType* > assignOps;
     41                // assume the assignment operator is the first assert param after any "type" parameter
     42                for ( std::list< TypeDecl* >::const_iterator tyVar = funcDecl->get_functionType()->get_forall().begin(); tyVar != funcDecl->get_functionType()->get_forall().end(); ++tyVar ) {
     43                    if ( (*tyVar)->get_kind() == TypeDecl::Any ) {
     44                        assert( !(*tyVar)->get_assertions().empty() );
     45                        assignOps[ (*tyVar)->get_name() ] = (*tyVar)->get_assertions().front();
     46                    } // if
     47                } // for
     48                for( std::list< DeclarationWithType* >::iterator param = funcDecl->get_functionType()->get_parameters().begin(); param != funcDecl->get_functionType()->get_parameters().end(); ++param ) {
     49                    std::set< UniqueId >::const_iterator var = modVars.find( (*param)->get_uniqueId() );
     50                    if ( var != modVars.end() ) {
     51                        TypeInstType *typeInst = dynamic_cast< TypeInstType* >( (*param)->get_type() );
     52                        assert( typeInst );
     53                        std::map< std::string, DeclarationWithType* >::const_iterator assignOp = assignOps.find( typeInst->get_name() );
     54                        if ( assignOp != assignOps.end() ) {
     55                            DeclarationWithType *oldParam = *param;
     56                            *param = (*param)->clone();
     57                            (*param)->set_mangleName( namer.newName( (*param)->get_mangleName() ) );
     58                            ApplicationExpr *assign = new ApplicationExpr( new VariableExpr( assignOp->second ) );
     59                            assign->get_args().push_back( new VariableExpr( oldParam ) );
     60                            assign->get_args().push_back( new VariableExpr( *param ) );
     61                            funcDecl->get_statements()->get_kids().push_front( new ExprStmt( noLabels, assign ) );
     62                            funcDecl->get_statements()->get_kids().push_front( new DeclStmt( noLabels, oldParam ) );
     63                        } // if
     64                        modVars.erase( var );
     65                    } // if
     66                } // for
     67            } // if
     68        } // if
     69    }
    4870
    49 void
    50 CopyParams::visit( FunctionDecl *funcDecl )
    51 {
    52   if( funcDecl->get_statements() ) {
    53     funcDecl->get_statements()->accept( *this );
    54    
    55     if( !modVars.empty() ) {
    56       std::map< std::string, DeclarationWithType* > assignOps;
    57       // assume that the assignment operator is the first assert param after any "type" parameter
    58       for( std::list< TypeDecl* >::const_iterator tyVar = funcDecl->get_functionType()->get_forall().begin(); tyVar != funcDecl->get_functionType()->get_forall().end(); ++tyVar ) {
    59         if( (*tyVar)->get_kind() == TypeDecl::Any ) {
    60           assert( !(*tyVar)->get_assertions().empty() );
    61           assignOps[ (*tyVar)->get_name() ] = (*tyVar)->get_assertions().front();
    62         }
    63       }
    64       for( std::list< DeclarationWithType* >::iterator param = funcDecl->get_functionType()->get_parameters().begin(); param != funcDecl->get_functionType()->get_parameters().end(); ++param ) {
    65         std::set< UniqueId >::const_iterator var = modVars.find( (*param)->get_uniqueId() );
    66         if( var != modVars.end() ) {
    67           TypeInstType *typeInst = dynamic_cast< TypeInstType* >( (*param)->get_type() );
    68           assert( typeInst );
    69           std::map< std::string, DeclarationWithType* >::const_iterator assignOp = assignOps.find( typeInst->get_name() );
    70           if( assignOp != assignOps.end() ) {
    71             DeclarationWithType *oldParam = *param;
    72             *param = (*param)->clone();
    73             (*param)->set_name( namer.newName() );
    74             ApplicationExpr *assign = new ApplicationExpr( new VariableExpr( assignOp->second ) );
    75             assign->get_args().push_back( new VariableExpr( oldParam ) );
    76             assign->get_args().push_back( new VariableExpr( *param ) );
    77             funcDecl->get_statements()->get_kids().push_front( new ExprStmt( noLabels, assign ) );
    78             funcDecl->get_statements()->get_kids().push_front( new DeclStmt( noLabels, oldParam ) );
    79           }
    80           modVars.erase( var );
    81         }
    82       }
     71    // this test is insufficient because it is possible for values to be modified by being passed to other polymorphic
     72    // routines (e.g., assignment operators) without having their addresses explicitly taken. Some thought is needed to
     73    // make sure that all of the correct cases are identified where copies are necessary.
     74    //
     75    // As a temporary measure, for correctness at the expense of performance, ignore the modVars list entirely and copy
     76    // every parameter of TypeInstType* when visiting the FunctionDecl.
     77    void CopyParams::visit( AddressExpr *addrExpr ) {
     78        if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( addrExpr->get_arg() ) ) {
     79            if ( dynamic_cast< TypeInstType* >( varExpr->get_var()->get_type() ) ) {
     80                modVars.insert( varExpr->get_var()->get_uniqueId() );
     81            } // if
     82        } // if
    8383    }
    84   }
    85 }
    86 
    87 void
    88 CopyParams::visit( AddressExpr *addrExpr )
    89 {
    90   if( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( addrExpr->get_arg() ) ) {
    91     if( dynamic_cast< TypeInstType* >( varExpr->get_var()->get_type() ) ) {
    92       modVars.insert( varExpr->get_var()->get_uniqueId() );
    93     }
    94   }
    95 }
    96 
    9784} // namespace GenPoly
  • translator/Parser/DeclarationNode.cc

    rd9a0e76 r17cd4eb  
    1919/* these must remain in the same order as the corresponding DeclarationNode enumerations */
    2020const char *DeclarationNode::qualifierName[] = { "const", "restrict", "volatile", "lvalue" };
    21 const char *DeclarationNode::basicTypeName[] = { "char", "int", "float", "double", "void", "bool", "complex", "imaginary" };
     21const char *DeclarationNode::basicTypeName[] = { "char", "int", "float", "double", "void", "_Bool", "_Complex", "_Imaginary" };
    2222const char *DeclarationNode::modifierName[] = { "signed", "unsigned", "short", "long" };
    2323const char *DeclarationNode::tyConName[] = { "struct", "union", "context" };
     
    799799                } else if ( StructDecl *agg = dynamic_cast< StructDecl* >( decl ) ) {
    800800                    StructInstType *inst = new StructInstType( Type::Qualifiers(), agg->get_name() );
    801                    *out++ = new ObjectDecl( "", Declaration::NoStorageClass, linkage, 0, inst, 0 );
     801                    *out++ = new ObjectDecl( "", Declaration::NoStorageClass, linkage, 0, inst, 0 );
    802802                    delete agg;
    803803                } else if ( UnionDecl *agg = dynamic_cast< UnionDecl* >( decl ) ) {
    804804                    UnionInstType *inst = new UnionInstType( Type::Qualifiers(), agg->get_name() );
    805                    *out++ = new ObjectDecl( "", Declaration::NoStorageClass, linkage, 0, inst, 0 );
     805                    *out++ = new ObjectDecl( "", Declaration::NoStorageClass, linkage, 0, inst, 0 );
    806806                }
    807807            }
  • translator/Parser/InitializerNode.cc

    rd9a0e76 r17cd4eb  
    4343        if ( designator != 0 ) {
    4444            os << "designated by: (";
    45             ExpressionNode  *curdes = designator;
     45            ExpressionNode *curdes = designator;
    4646            while( curdes != 0) {
    4747                curdes->printOneLine(os);
    4848                curdes = (ExpressionNode *)(curdes->get_link());
    4949                if ( curdes ) os << ", ";
    50             }
     50            } // while
    5151            os << ")";
    52         }
     52        } // if
    5353        if ( expr ) expr->printOneLine(os);
    5454    } else {  // It's an aggregate
     
    5757            next_init()->printOneLine(os);
    5858        if (aggregate) os << "--]";
    59     }
     59    } // if
    6060
    6161    InitializerNode *moreInit;
     
    8888        if ( get_expression() != 0)
    8989            return new SingleInit( get_expression()->build(), designators );
    90     }
     90    } // if
    9191
    9292    return 0;
  • translator/Parser/ParseNode.h

    rd9a0e76 r17cd4eb  
    456456  ExpressionNode *get_expression() const { return expr; }
    457457
    458   InitializerNode *set_designators( ExpressionNode *des ) { designator = des;  return this; }
     458  InitializerNode *set_designators( ExpressionNode *des ) { designator = des; return this; }
    459459  ExpressionNode *get_designators() const { return designator; }
    460460
  • translator/Parser/TypeData.cc

    rd9a0e76 r17cd4eb  
    560560            if ( *i == DeclarationNode::Void ) {
    561561                if ( basic->typeSpec.size() != 1 || !basic->modifiers.empty() ) {
    562                     throw SemanticError( "invalid type specifier \"void\" in type ", this );
     562                    throw SemanticError( "invalid type specifier \"void\" in type: ", this );
    563563                } else {
    564564                    return new VoidType( buildQualifiers() );
     
    571571              case DeclarationNode::Float:
    572572                if ( sawDouble ) {
    573                     throw SemanticError( "invalid type specifier \"float\" in type ", this );
     573                    throw SemanticError( "invalid type specifier \"float\" in type: ", this );
    574574                } else {
    575575                    switch ( ret ) {
     
    581581                        break;
    582582                      default:
    583                         throw SemanticError( "invalid type specifier \"float\" in type ", this );
     583                        throw SemanticError( "invalid type specifier \"float\" in type: ", this );
    584584                    }
    585585                }
     
    587587              case DeclarationNode::Double:
    588588                if ( sawDouble ) {
    589                     throw SemanticError( "duplicate type specifier \"double\" in type ", this );
     589                    throw SemanticError( "duplicate type specifier \"double\" in type: ", this );
    590590                } else {
    591591                    switch ( ret ) {
     
    594594                        break;
    595595                      default:
    596                         throw SemanticError( "invalid type specifier \"double\" in type ", this );
     596                        throw SemanticError( "invalid type specifier \"double\" in type: ", this );
    597597                    }
    598598                }
     
    609609                    break;
    610610                  default:
    611                     throw SemanticError( "invalid type specifier \"complex\" in type ", this );
     611                    throw SemanticError( "invalid type specifier \"_Complex\" in type: ", this );
    612612                }
    613613                break;
     
    623623                    break;
    624624                  default:
    625                     throw SemanticError( "invalid type specifier \"imaginary\" in type ", this );
     625                    throw SemanticError( "invalid type specifier \"_Imaginary\" in type: ", this );
    626626                }
    627627                break;
    628628       
    629629              default:
    630                 throw SemanticError( std::string( "invalid type specifier \"" ) + DeclarationNode::basicTypeName[ *i ] + "\" in type ", this );
     630                throw SemanticError( std::string( "invalid type specifier \"" ) + DeclarationNode::basicTypeName[ *i ] + "\" in type: ", this );
    631631            }
    632632        }
     
    666666                    break;
    667667                  default:
    668                     throw SemanticError( "invalid type modifier \"long\" in type ", this );
     668                    throw SemanticError( "invalid type modifier \"long\" in type: ", this );
    669669                }
    670670            }
     
    683683                    break;
    684684                  default:
    685                     throw SemanticError( "invalid type modifier \"short\" in type ", this );
     685                    throw SemanticError( "invalid type modifier \"short\" in type: ", this );
    686686                }
    687687            }
     
    692692                ret = BasicType::SignedInt;
    693693            } else if ( sawSigned ) {
    694                 throw SemanticError( "duplicate type modifer \"signed\" in type ", this );
     694                throw SemanticError( "duplicate type modifer \"signed\" in type: ", this );
    695695            } else {
    696696                switch ( ret ) {
     
    702702                    break;
    703703                  default:
    704                     throw SemanticError( "invalid type modifer \"signed\" in type ", this );
     704                    throw SemanticError( "invalid type modifer \"signed\" in type: ", this );
    705705                }
    706706            }
     
    711711                ret = BasicType::UnsignedInt;
    712712            } else if ( sawSigned ) {
    713                 throw SemanticError( "invalid type modifer \"unsigned\" in type ", this );
     713                throw SemanticError( "invalid type modifer \"unsigned\" in type: ", this );
    714714            } else {
    715715                switch ( ret ) {
     
    727727                    break;
    728728                  default:
    729                     throw SemanticError( "invalid type modifer \"unsigned\" in type ", this );
     729                    throw SemanticError( "invalid type modifer \"unsigned\" in type: ", this );
    730730                }
    731731            }
  • translator/Parser/cfa.y

    rd9a0e76 r17cd4eb  
    1010 * Created On       : Sat Sep  1 20:22:55 2001
    1111 * Last Modified By : Peter A. Buhr
    12  * Last Modified On : Tue Nov 25 23:52:54 2014
    13  * Update Count     : 890
     12 * Last Modified On : Wed Jan  7 09:19:57 2015
     13 * Update Count     : 906
    1414 */
    1515
     
    294294                /* ENUMERATIONconstant is not included here; it is treated as a variable with type
    295295                   "enumeration constant". */
    296         INTEGERconstant         { $$ = new ConstantNode(ConstantNode::Integer,   $1); }
    297         | FLOATINGconstant      { $$ = new ConstantNode(ConstantNode::Float,     $1); }
    298         | CHARACTERconstant     { $$ = new ConstantNode(ConstantNode::Character, $1); }
     296        INTEGERconstant                                 { $$ = new ConstantNode(ConstantNode::Integer,   $1); }
     297        | FLOATINGconstant                              { $$ = new ConstantNode(ConstantNode::Float,     $1); }
     298        | CHARACTERconstant                             { $$ = new ConstantNode(ConstantNode::Character, $1); }
    299299        ;
    300300
     
    320320
    321321string_literal_list:                                    /* juxtaposed strings are concatenated */
    322         STRINGliteral                           { $$ = new ConstantNode(ConstantNode::String, $1); }
    323         | string_literal_list STRINGliteral     { $$ = $1->append( $2 ); }
     322        STRINGliteral                                   { $$ = new ConstantNode(ConstantNode::String, $1); }
     323        | string_literal_list STRINGliteral             { $$ = $1->append( $2 ); }
    324324        ;
    325325
     
    352352                { $$ = new CompositeExprNode($1, $3); }
    353353        | postfix_expression '.' no_attr_identifier
    354                 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::FieldSel), $1,
    355                                              new VarRefNode($3)); }
     354                { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::FieldSel), $1, new VarRefNode($3)); }
    356355        | postfix_expression '.' '[' push field_list pop ']' /* CFA, tuple field selector */
    357356        | postfix_expression ARROW no_attr_identifier
    358                 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::PFieldSel), $1,
    359                                              new VarRefNode($3)); }
     357                { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::PFieldSel), $1, new VarRefNode($3)); }
    360358        | postfix_expression ARROW '[' push field_list pop ']' /* CFA, tuple field selector */
    361359        | postfix_expression ICR
     
    371369        argument_expression
    372370        | argument_expression_list ',' argument_expression
    373                                                 { $$ = (ExpressionNode *)($1->set_link($3)); }
     371                                                        { $$ = (ExpressionNode *)($1->set_link($3)); }
    374372        ;
    375373
     
    379377        | assignment_expression
    380378        | no_attr_identifier ':' assignment_expression
    381                                                 { $$ = $3->set_asArgName($1); }
     379                                                        { $$ = $3->set_asArgName($1); }
    382380                /* Only a list of no_attr_identifier_or_typedef_name is allowed in this context. However, there
    383381                   is insufficient look ahead to distinguish between this list of parameter names and a tuple,
     
    391389field_list:                                             /* CFA, tuple field selector */
    392390        field
    393         | field_list ',' field                  { $$ = (ExpressionNode *)$1->set_link( $3 ); }
     391        | field_list ',' field                          { $$ = (ExpressionNode *)$1->set_link( $3 ); }
    394392        ;
    395393
    396394field:                                                  /* CFA, tuple field selector */
    397395        no_attr_identifier
    398                                                 { $$ = new VarRefNode( $1 ); }
     396                                                        { $$ = new VarRefNode( $1 ); }
    399397        | no_attr_identifier '.' field
    400                                                 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::FieldSel), new VarRefNode( $1 ), $3); }
     398                                                        { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::FieldSel), new VarRefNode( $1 ), $3); }
    401399        | no_attr_identifier '.' '[' push field_list pop ']'
    402                                                 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::FieldSel), new VarRefNode( $1 ), $5); }
     400                                                        { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::FieldSel), new VarRefNode( $1 ), $5); }
    403401        | no_attr_identifier ARROW field
    404                                                 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::PFieldSel), new VarRefNode( $1 ), $3); }
     402                                                        { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::PFieldSel), new VarRefNode( $1 ), $3); }
    405403        | no_attr_identifier ARROW '[' push field_list pop ']'
    406                                                 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::PFieldSel), new VarRefNode( $1 ), $5); }
     404                                                        { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::PFieldSel), new VarRefNode( $1 ), $5); }
    407405        ;
    408406
     
    440438                { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::AlignOf), new TypeValueNode($3)); }
    441439        | ANDAND no_attr_identifier                     /* GCC, address of label */
    442                 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::LabelAddress),
    443                                              new VarRefNode($2, true)); }
     440                { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::LabelAddress), new VarRefNode($2, true)); }
    444441        ;
    445442
    446443unary_operator:
    447         '&'                               { $$ = new OperatorNode(OperatorNode::AddressOf); }
    448         | '+'                             { $$ = new OperatorNode(OperatorNode::UnPlus); }
    449         | '-'                             { $$ = new OperatorNode(OperatorNode::UnMinus); }
    450         | '~'                             { $$ = new OperatorNode(OperatorNode::BitNeg); }
     444        '&'                                             { $$ = new OperatorNode(OperatorNode::AddressOf); }
     445        | '+'                                           { $$ = new OperatorNode(OperatorNode::UnPlus); }
     446        | '-'                                           { $$ = new OperatorNode(OperatorNode::UnMinus); }
     447        | '~'                                           { $$ = new OperatorNode(OperatorNode::BitNeg); }
    451448        ;
    452449
     
    454451        unary_expression
    455452        | '(' type_name_no_function ')' cast_expression
    456                                                 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Cast),
    457                                                                        new TypeValueNode($2), $4); }
     453                                                        { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Cast), new TypeValueNode($2), $4); }
    458454        | '(' type_name_no_function ')' tuple
    459                                                 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Cast),
    460                                                                        new TypeValueNode($2), $4); }
     455                                                        { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Cast), new TypeValueNode($2), $4); }
    461456        ;
    462457
     
    464459        cast_expression
    465460        | multiplicative_expression '*' cast_expression
    466                                                 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Mul),$1,$3); }
     461                                                        { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Mul),$1,$3); }
    467462        | multiplicative_expression '/' cast_expression
    468                                                 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Div),$1,$3); }
     463                                                        { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Div),$1,$3); }
    469464        | multiplicative_expression '%' cast_expression
    470                                                 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Mod),$1,$3); }
     465                                                        { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Mod),$1,$3); }
    471466        ;
    472467
     
    474469        multiplicative_expression
    475470        | additive_expression '+' multiplicative_expression
    476                                                 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Plus),$1,$3); }
     471                                                        { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Plus),$1,$3); }
    477472        | additive_expression '-' multiplicative_expression
    478                                                 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Minus),$1,$3); }
     473                                                        { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Minus),$1,$3); }
    479474        ;
    480475
     
    482477        additive_expression
    483478        | shift_expression LS additive_expression
    484                                                 { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::LShift),$1,$3); }
     479                                                        { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::LShift),$1,$3); }
    485480        | shift_expression RS additive_expression
    486481                                                { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::RShift),$1,$3); }
     
    558553        conditional_expression
    559554        | unary_expression '=' assignment_expression
    560                                                 { $$ =new CompositeExprNode(new OperatorNode(OperatorNode::Assign), $1, $3); }
     555                                                        { $$ =new CompositeExprNode(new OperatorNode(OperatorNode::Assign), $1, $3); }
    561556        | unary_expression assignment_operator assignment_expression
    562                                                 { $$ =new CompositeExprNode($2, $1, $3); }
     557                                                        { $$ =new CompositeExprNode($2, $1, $3); }
    563558        | tuple assignment_opt                          /* CFA, tuple expression */
    564559                {
     
    12091204                        typedefTable.enterScope();
    12101205                }
    1211           type_parameter_list ')'               /* CFA */
     1206          type_parameter_list ')'                       /* CFA */
    12121207                {
    12131208                        typedefTable.leaveScope();
     
    16301625
    16311626initializer_opt:
    1632         /* empty */                             { $$ = 0; }
    1633         | '=' initializer                       { $$ = $2; }
     1627        /* empty */                                     { $$ = 0; }
     1628        | '=' initializer                               { $$ = $2; }
    16341629        ;
    16351630
    16361631initializer:
    1637         assignment_expression                   { $$ = new InitializerNode($1); }
    1638         | '{' initializer_list comma_opt '}'    { $$ = new InitializerNode($2, true); }
     1632        assignment_expression                           { $$ = new InitializerNode($1); }
     1633        | '{' initializer_list comma_opt '}'            { $$ = new InitializerNode($2, true); }
    16391634        ;
    16401635
    16411636initializer_list:
    16421637        initializer
    1643         | designation initializer                            { $$ = $2->set_designators( $1 ); }
    1644         | initializer_list ',' initializer                   { $$ = (InitializerNode *)( $1->set_link($3) ); }
     1638        | designation initializer                       { $$ = $2->set_designators( $1 ); }
     1639        | initializer_list ',' initializer              { $$ = (InitializerNode *)( $1->set_link($3) ); }
    16451640        | initializer_list ',' designation initializer
    1646                                            { $$ = (InitializerNode *)( $1->set_link( $4->set_designators($3) ) ); }
     1641                                                        { $$ = (InitializerNode *)( $1->set_link( $4->set_designators($3) ) ); }
    16471642        ;
    16481643
     
    16591654designation:
    16601655        designator_list ':'                             /* ANSI99, CFA uses ":" instead of "=" */
    1661         | no_attr_identifier_or_typedef_name ':'                /* GCC, field name */
    1662                                                        { $$ = new VarRefNode( $1 ); }
     1656        | no_attr_identifier_or_typedef_name ':'        /* GCC, field name */
     1657                                                        { $$ = new VarRefNode( $1 ); }
    16631658        ;
    16641659
    16651660designator_list:                                        /* ANSI99 */
    16661661        designator
    1667         | designator_list designator                   { $$ = (ExpressionNode *)($1->set_link( $2 )); }
     1662        | designator_list designator                    { $$ = (ExpressionNode *)($1->set_link( $2 )); }
    16681663        ;
    16691664
    16701665designator:
    16711666        '.' no_attr_identifier_or_typedef_name          /* ANSI99, field name */
    1672                                                        { $$ = new VarRefNode( $2 ); }
     1667                                                        { $$ = new VarRefNode( $2 ); }
    16731668        | '[' push assignment_expression pop ']'        /* ANSI99, single array element */
    16741669                /* assignment_expression used instead of constant_expression because of shift/reduce conflicts
    16751670                   with tuple. */
    1676                                                        { $$ = $3; }
     1671                                                        { $$ = $3; }
    16771672        | '[' push subrange pop ']'                     /* CFA, multiple array elements */
    1678                                                        { $$ = $3; }
     1673                                                        { $$ = $3; }
    16791674        | '[' push constant_expression ELLIPSIS constant_expression pop ']' /* GCC, multiple array elements */
    1680                                                        { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Range), $3, $5); }
     1675                                                        { $$ = new CompositeExprNode(new OperatorNode(OperatorNode::Range), $3, $5); }
    16811676        | '.' '[' push field_list pop ']'               /* CFA, tuple field selector */
    1682                                                        { $$ = $4; }
     1677                                                        { $$ = $4; }
    16831678        ;
    16841679
     
    24922487        ISO/IEC 9899:1999 Section 6.7.5.2(1) : "The optional type qualifiers and the keyword static shall
    24932488        appear only in a declaration of a function parameter with an array type, and then only in the
    2494         outermost array type derivation."
    2495    */
     2489        outermost array type derivation." */
    24962490
    24972491array_parameter_1st_dimension:
    24982492        '[' push pop ']'
    24992493                { $$ = DeclarationNode::newArray( 0, 0, false ); }
     2494        // multi_array_dimension handles the '[' '*' ']' case
    25002495        | '[' push type_qualifier_list '*' pop ']'      /* remaining ANSI99 */
    25012496                { $$ = DeclarationNode::newVarArray( $3 ); }
     2497        | '[' push type_qualifier_list pop ']'
     2498                { $$ = DeclarationNode::newArray( 0, $3, false ); }
     2499        // multi_array_dimension handles the '[' assignment_expression ']' case
    25022500        | '[' push type_qualifier_list assignment_expression pop ']'
    25032501                { $$ = DeclarationNode::newArray( $4, $3, false ); }
    2504         | '[' push STATIC assignment_expression pop ']'
    2505                 { $$ = DeclarationNode::newArray( $4, 0, true ); }
    2506         | '[' push STATIC type_qualifier_list assignment_expression pop ']'
     2502        | '[' push STATIC type_qualifier_list_opt assignment_expression pop ']'
    25072503                { $$ = DeclarationNode::newArray( $5, $4, true ); }
     2504        | '[' push type_qualifier_list STATIC assignment_expression pop ']'
     2505                { $$ = DeclarationNode::newArray( $5, $3, true ); }
    25082506        ;
    25092507
  • translator/ResolvExpr/Resolver.cc

    rd9a0e76 r17cd4eb  
    4747            (*i)->print( std::cerr );
    4848            (*i)->accept( resolver );
    49         }
     49        } // for
    5050#endif
    5151    }
     
    8181                for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) {
    8282                    i->print( std::cout );
    83                 }
    84             }
     83                } // for
     84            } // if
    8585#endif
    8686            assert( finder.get_alternatives().size() == 1 );
     
    9898            } else {
    9999                return true;
    100             }
     100            } // if
    101101        }
    102102 
     
    112112                for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) {
    113113                    i->print( std::cout );
    114                 }
    115             }
     114                } // for
     115            } // if
    116116#endif
    117117            Expression *newExpr = 0;
     
    124124                        newExpr = i->expr->clone();
    125125                        newEnv = &i->env;
    126                     }
    127                 }
    128             }
     126                    } // if
     127                } // if
     128            } // for
    129129            if ( !newExpr ) {
    130130                throw SemanticError( "Too many interpretations for switch control expression", untyped );
    131             }
     131            } // if
    132132            finishExpr( newExpr, *newEnv );
    133133            return newExpr;
     
    147147            Type *new_type = resolveTypeof( typeDecl->get_base(), *this );
    148148            typeDecl->set_base( new_type );
    149         }
     149        } // if
    150150        SymTab::Indexer::visit( typeDecl );
    151151    }
     
    163163        for ( std::list< DeclarationWithType * >::const_iterator i = functionDecl->get_functionType()->get_returnVals().begin(); i != functionDecl->get_functionType()->get_returnVals().end(); ++i ) {
    164164            functionReturn.push_back( (*i)->get_type() );
    165         }
     165        } // for
    166166        SymTab::Indexer::visit( functionDecl );
    167167        functionReturn = oldFunctionReturn;
     
    173173            delete exprStmt->get_expr();
    174174            exprStmt->set_expr( newExpr );
    175         }
     175        } // if
    176176    }
    177177
     
    196196            delete forStmt->get_condition();
    197197            forStmt->set_condition( newExpr );
    198         }
     198        } // if
    199199 
    200200        if ( forStmt->get_increment() ) {
     
    202202            delete forStmt->get_increment();
    203203            forStmt->set_increment( newExpr );
    204         }
     204        } // if
    205205 
    206206        Visitor::visit( forStmt );
     
    236236            delete castExpr;
    237237            returnStmt->set_expr( newExpr );
    238         }
     238        } // if
    239239    }
    240240
    241241    void Resolver::visit( SingleInit *singleInit ) {
    242         // if ( singleInit->get_value() ) {
    243         //     CastExpr *castExpr = new CastExpr( singleInit->get_value(), initContext->clone() );
    244         //     Expression *newExpr = findSingleExpression( castExpr, *this );
    245         //     delete castExpr;
    246         //     singleInit->set_value( newExpr );
    247         // }
    248         // singleInit->get_value()->accept( *this );
     242        if ( singleInit->get_value() ) {
     243            CastExpr *castExpr = new CastExpr( singleInit->get_value(), initContext->clone() );
     244            Expression *newExpr = findSingleExpression( castExpr, *this );
     245            delete castExpr;
     246            singleInit->set_value( newExpr );
     247        } // if
     248        singleInit->get_value()->accept( *this );
    249249    }
    250250
  • translator/SymTab/Indexer.cc

    rd9a0e76 r17cd4eb  
    1 /*
    2  * This file is part of the Cforall project
    3  *
    4  * $Id: Indexer.cc,v 1.12 2005/08/29 20:14:17 rcbilson Exp $
    5  *
    6  */
    7 
    81#include "SynTree/Declaration.h"
    92#include "SynTree/Type.h"
     
    158#include "utility.h"
    169
    17 #define debugPrint(x) if( doDebug ) { std::cout << x; }
     10#define debugPrint(x) if ( doDebug ) { std::cout << x; }
    1811
    1912namespace SymTab {
    20 
    21 Indexer::Indexer( bool useDebug )
    22   : doDebug( useDebug )
    23 {
    24 }
    25 
    26 Indexer::~Indexer()
    27 {
    28 }
    29 
    30 void
    31 Indexer::visit( ObjectDecl *objectDecl )
    32 {
    33   maybeAccept( objectDecl->get_type(), *this );
    34   maybeAccept( objectDecl->get_init(), *this );
    35   maybeAccept( objectDecl->get_bitfieldWidth(), *this );
    36   if( objectDecl->get_name() != "" ) {
    37     debugPrint( "Adding object " << objectDecl->get_name() << std::endl );
    38     idTable.addDecl( objectDecl );
    39   }
    40 }
    41 
    42 void
    43 Indexer::visit( FunctionDecl *functionDecl )
    44 {
    45   if( functionDecl->get_name() == "" ) return;
    46   debugPrint( "Adding function " << functionDecl->get_name() << std::endl );
    47   idTable.addDecl( functionDecl );
    48   enterScope();
    49   maybeAccept( functionDecl->get_functionType(), *this );
    50   acceptAll( functionDecl->get_oldDecls(), *this );
    51   maybeAccept( functionDecl->get_statements(), *this );
    52   leaveScope();
    53 }
     13    Indexer::Indexer( bool useDebug ) : doDebug( useDebug ) {}
     14
     15    Indexer::~Indexer() {}
     16
     17    void Indexer::visit( ObjectDecl *objectDecl ) {
     18        maybeAccept( objectDecl->get_type(), *this );
     19        maybeAccept( objectDecl->get_init(), *this );
     20        maybeAccept( objectDecl->get_bitfieldWidth(), *this );
     21        if ( objectDecl->get_name() != "" ) {
     22            debugPrint( "Adding object " << objectDecl->get_name() << std::endl );
     23            idTable.addDecl( objectDecl );
     24        }
     25    }
     26
     27    void Indexer::visit( FunctionDecl *functionDecl ) {
     28        if ( functionDecl->get_name() == "" ) return;
     29        debugPrint( "Adding function " << functionDecl->get_name() << std::endl );
     30        idTable.addDecl( functionDecl );
     31        enterScope();
     32        maybeAccept( functionDecl->get_functionType(), *this );
     33        acceptAll( functionDecl->get_oldDecls(), *this );
     34        maybeAccept( functionDecl->get_statements(), *this );
     35        leaveScope();
     36    }
    5437
    5538/********
     
    7255 */
    7356
    74 void
    75 Indexer::visit( TypeDecl *typeDecl )
    76 {
    77   // see A NOTE ON THE ORDER OF TRAVERSAL, above
    78   // note that assertions come after the type is added to the symtab, since they aren't part
    79   // of the type proper and may depend on the type itself
    80   enterScope();
    81   acceptAll( typeDecl->get_parameters(), *this );
    82   maybeAccept( typeDecl->get_base(), *this );
    83   leaveScope();
    84   debugPrint( "Adding type " << typeDecl->get_name() << std::endl );
    85   typeTable.add( typeDecl );
    86   acceptAll( typeDecl->get_assertions(), *this );
    87 }
    88 
    89 void
    90 Indexer::visit( TypedefDecl *typeDecl )
    91 {
    92   enterScope();
    93   acceptAll( typeDecl->get_parameters(), *this );
    94   maybeAccept( typeDecl->get_base(), *this );
    95   leaveScope();
    96   debugPrint( "Adding typedef " << typeDecl->get_name() << std::endl );
    97   typeTable.add( typeDecl );
    98 }
    99 
    100 void
    101 Indexer::visit( StructDecl *aggregateDecl )
    102 {
    103   // make up a forward declaration and add it before processing the members
    104   StructDecl fwdDecl( aggregateDecl->get_name() );
    105   cloneAll( aggregateDecl->get_parameters(), fwdDecl.get_parameters() );
    106   debugPrint( "Adding fwd decl for struct " << fwdDecl.get_name() << std::endl );
    107   structTable.add( &fwdDecl );
    108  
    109   enterScope();
    110   acceptAll( aggregateDecl->get_parameters(), *this );
    111   acceptAll( aggregateDecl->get_members(), *this );
    112   leaveScope();
    113  
    114   debugPrint( "Adding struct " << aggregateDecl->get_name() << std::endl );
    115   // this addition replaces the forward declaration
    116   structTable.add( aggregateDecl );
    117 }
    118 
    119 void
    120 Indexer::visit( UnionDecl *aggregateDecl )
    121 {
    122   // make up a forward declaration and add it before processing the members
    123   UnionDecl fwdDecl( aggregateDecl->get_name() );
    124   cloneAll( aggregateDecl->get_parameters(), fwdDecl.get_parameters() );
    125   debugPrint( "Adding fwd decl for union " << fwdDecl.get_name() << std::endl );
    126   unionTable.add( &fwdDecl );
    127  
    128   enterScope();
    129   acceptAll( aggregateDecl->get_parameters(), *this );
    130   acceptAll( aggregateDecl->get_members(), *this );
    131   leaveScope();
    132  
    133   debugPrint( "Adding union " << aggregateDecl->get_name() << std::endl );
    134   unionTable.add( aggregateDecl );
    135 }
    136 
    137 void
    138 Indexer::visit( EnumDecl *aggregateDecl )
    139 {
    140   debugPrint( "Adding enum " << aggregateDecl->get_name() << std::endl );
    141   enumTable.add( aggregateDecl );
    142   // unlike structs, contexts, and unions, enums inject their members into the
    143   // global scope
    144   acceptAll( aggregateDecl->get_members(), *this );
    145 }
    146 
    147 void
    148 Indexer::visit( ContextDecl *aggregateDecl )
    149 {
    150   enterScope();
    151   acceptAll( aggregateDecl->get_parameters(), *this );
    152   acceptAll( aggregateDecl->get_members(), *this );
    153   leaveScope();
    154  
    155   debugPrint( "Adding context " << aggregateDecl->get_name() << std::endl );
    156   contextTable.add( aggregateDecl );
    157 }
    158 
    159 void
    160 Indexer::visit( CompoundStmt *compoundStmt )
    161 {
    162   enterScope();
    163   acceptAll( compoundStmt->get_kids(), *this );
    164   leaveScope();
    165 }
    166 
    167 void
    168 Indexer::visit( ContextInstType *contextInst )
    169 {
    170   acceptAll( contextInst->get_parameters(), *this );
    171   acceptAll( contextInst->get_members(), *this );
    172 }
    173 
    174 void
    175 Indexer::visit( StructInstType *structInst )
    176 {
    177   if( !structTable.lookup( structInst->get_name() ) ) {
    178     debugPrint( "Adding struct " << structInst->get_name() << " from implicit forward declaration" << std::endl );
    179     structTable.add( structInst->get_name() );
    180   }
    181   enterScope();
    182   acceptAll( structInst->get_parameters(), *this );
    183   leaveScope();
    184 }
    185 
    186 void
    187 Indexer::visit( UnionInstType *unionInst )
    188 {
    189   if( !unionTable.lookup( unionInst->get_name() ) ) {
    190     debugPrint( "Adding union " << unionInst->get_name() << " from implicit forward declaration" << std::endl );
    191     unionTable.add( unionInst->get_name() );
    192   }
    193   enterScope();
    194   acceptAll( unionInst->get_parameters(), *this );
    195   leaveScope();
    196 }
    197 
    198 void
    199 Indexer::lookupId( const std::string &id, std::list< DeclarationWithType* > &list ) const
    200 {
    201   idTable.lookupId( id, list );
    202 }
    203 
    204 NamedTypeDecl *
    205 Indexer::lookupType( const std::string &id ) const
    206 {
    207   return typeTable.lookup( id );
    208 }
    209 
    210 StructDecl *
    211 Indexer::lookupStruct( const std::string &id ) const
    212 {
    213   return structTable.lookup( id );
    214 }
    215 
    216 EnumDecl *
    217 Indexer::lookupEnum( const std::string &id ) const
    218 {
    219   return enumTable.lookup( id );
    220 }
    221 
    222 UnionDecl *
    223 Indexer::lookupUnion( const std::string &id ) const
    224 {
    225   return unionTable.lookup( id );
    226 }
    227 
    228 ContextDecl *
    229 Indexer::lookupContext( const std::string &id ) const
    230 {
    231   return contextTable.lookup( id );
    232 }
    233 
    234 void
    235 Indexer::enterScope()
    236 {
    237   if( doDebug ) {
    238     std::cout << "--- Entering scope" << std::endl;
    239   }
    240   idTable.enterScope();
    241   typeTable.enterScope();
    242   structTable.enterScope();
    243   enumTable.enterScope();
    244   unionTable.enterScope();
    245   contextTable.enterScope();
    246 }
    247 
    248 void
    249 Indexer::leaveScope()
    250 {
    251   using std::cout;
    252   using std::endl;
    253  
    254   if( doDebug ) {
    255     cout << "--- Leaving scope containing" << endl;
    256     idTable.dump( cout );
    257     typeTable.dump( cout );
    258     structTable.dump( cout );
    259     enumTable.dump( cout );
    260     unionTable.dump( cout );
    261     contextTable.dump( cout );
    262   }
    263   idTable.leaveScope();
    264   typeTable.leaveScope();
    265   structTable.leaveScope();
    266   enumTable.leaveScope();
    267   unionTable.leaveScope();
    268   contextTable.leaveScope();
    269 }
    270 
    271 void
    272 Indexer::print( std::ostream &os, int indent ) const
    273 {
    274     idTable.dump( os );
    275     typeTable.dump( os );
    276     structTable.dump( os );
    277     enumTable.dump( os );
    278     unionTable.dump( os );
    279     contextTable.dump( os );
    280 }
    281 
     57    void Indexer::visit( TypeDecl *typeDecl ) {
     58        // see A NOTE ON THE ORDER OF TRAVERSAL, above
     59        // note that assertions come after the type is added to the symtab, since they aren't part
     60        // of the type proper and may depend on the type itself
     61        enterScope();
     62        acceptAll( typeDecl->get_parameters(), *this );
     63        maybeAccept( typeDecl->get_base(), *this );
     64        leaveScope();
     65        debugPrint( "Adding type " << typeDecl->get_name() << std::endl );
     66        typeTable.add( typeDecl );
     67        acceptAll( typeDecl->get_assertions(), *this );
     68    }
     69
     70    void Indexer::visit( TypedefDecl *typeDecl ) {
     71        enterScope();
     72        acceptAll( typeDecl->get_parameters(), *this );
     73        maybeAccept( typeDecl->get_base(), *this );
     74        leaveScope();
     75        debugPrint( "Adding typedef " << typeDecl->get_name() << std::endl );
     76        typeTable.add( typeDecl );
     77    }
     78
     79    void Indexer::visit( StructDecl *aggregateDecl ) {
     80        // make up a forward declaration and add it before processing the members
     81        StructDecl fwdDecl( aggregateDecl->get_name() );
     82        cloneAll( aggregateDecl->get_parameters(), fwdDecl.get_parameters() );
     83        debugPrint( "Adding fwd decl for struct " << fwdDecl.get_name() << std::endl );
     84        structTable.add( &fwdDecl );
     85 
     86        enterScope();
     87        acceptAll( aggregateDecl->get_parameters(), *this );
     88        acceptAll( aggregateDecl->get_members(), *this );
     89        leaveScope();
     90 
     91        debugPrint( "Adding struct " << aggregateDecl->get_name() << std::endl );
     92        // this addition replaces the forward declaration
     93        structTable.add( aggregateDecl );
     94    }
     95
     96    void Indexer::visit( UnionDecl *aggregateDecl ) {
     97        // make up a forward declaration and add it before processing the members
     98        UnionDecl fwdDecl( aggregateDecl->get_name() );
     99        cloneAll( aggregateDecl->get_parameters(), fwdDecl.get_parameters() );
     100        debugPrint( "Adding fwd decl for union " << fwdDecl.get_name() << std::endl );
     101        unionTable.add( &fwdDecl );
     102 
     103        enterScope();
     104        acceptAll( aggregateDecl->get_parameters(), *this );
     105        acceptAll( aggregateDecl->get_members(), *this );
     106        leaveScope();
     107 
     108        debugPrint( "Adding union " << aggregateDecl->get_name() << std::endl );
     109        unionTable.add( aggregateDecl );
     110    }
     111
     112    void Indexer::visit( EnumDecl *aggregateDecl ) {
     113        debugPrint( "Adding enum " << aggregateDecl->get_name() << std::endl );
     114        enumTable.add( aggregateDecl );
     115        // unlike structs, contexts, and unions, enums inject their members into the global scope
     116        acceptAll( aggregateDecl->get_members(), *this );
     117    }
     118
     119    void Indexer::visit( ContextDecl *aggregateDecl ) {
     120        enterScope();
     121        acceptAll( aggregateDecl->get_parameters(), *this );
     122        acceptAll( aggregateDecl->get_members(), *this );
     123        leaveScope();
     124 
     125        debugPrint( "Adding context " << aggregateDecl->get_name() << std::endl );
     126        contextTable.add( aggregateDecl );
     127    }
     128
     129    void Indexer::visit( CompoundStmt *compoundStmt ) {
     130        enterScope();
     131        acceptAll( compoundStmt->get_kids(), *this );
     132        leaveScope();
     133    }
     134
     135    void Indexer::visit( ContextInstType *contextInst ) {
     136        acceptAll( contextInst->get_parameters(), *this );
     137        acceptAll( contextInst->get_members(), *this );
     138    }
     139
     140    void Indexer::visit( StructInstType *structInst ) {
     141        if ( ! structTable.lookup( structInst->get_name() ) ) {
     142            debugPrint( "Adding struct " << structInst->get_name() << " from implicit forward declaration" << std::endl );
     143            structTable.add( structInst->get_name() );
     144        }
     145        enterScope();
     146        acceptAll( structInst->get_parameters(), *this );
     147        leaveScope();
     148    }
     149
     150    void Indexer::visit( UnionInstType *unionInst ) {
     151        if ( ! unionTable.lookup( unionInst->get_name() ) ) {
     152            debugPrint( "Adding union " << unionInst->get_name() << " from implicit forward declaration" << std::endl );
     153            unionTable.add( unionInst->get_name() );
     154        }
     155        enterScope();
     156        acceptAll( unionInst->get_parameters(), *this );
     157        leaveScope();
     158    }
     159
     160    void Indexer::lookupId( const std::string &id, std::list< DeclarationWithType* > &list ) const {
     161        idTable.lookupId( id, list );
     162    }
     163
     164    NamedTypeDecl *Indexer::lookupType( const std::string &id ) const {
     165        return typeTable.lookup( id );
     166    }
     167
     168    StructDecl *Indexer::lookupStruct( const std::string &id ) const {
     169        return structTable.lookup( id );
     170    }
     171
     172    EnumDecl *Indexer::lookupEnum( const std::string &id ) const {
     173        return enumTable.lookup( id );
     174    }
     175
     176    UnionDecl *Indexer::lookupUnion( const std::string &id ) const {
     177        return unionTable.lookup( id );
     178    }
     179
     180    ContextDecl  * Indexer::lookupContext( const std::string &id ) const {
     181        return contextTable.lookup( id );
     182    }
     183
     184    void Indexer::enterScope() {
     185        if ( doDebug ) {
     186            std::cout << "--- Entering scope" << std::endl;
     187        }
     188        idTable.enterScope();
     189        typeTable.enterScope();
     190        structTable.enterScope();
     191        enumTable.enterScope();
     192        unionTable.enterScope();
     193        contextTable.enterScope();
     194    }
     195
     196    void Indexer::leaveScope() {
     197        using std::cout;
     198        using std::endl;
     199 
     200        if ( doDebug ) {
     201            cout << "--- Leaving scope containing" << endl;
     202            idTable.dump( cout );
     203            typeTable.dump( cout );
     204            structTable.dump( cout );
     205            enumTable.dump( cout );
     206            unionTable.dump( cout );
     207            contextTable.dump( cout );
     208        }
     209        idTable.leaveScope();
     210        typeTable.leaveScope();
     211        structTable.leaveScope();
     212        enumTable.leaveScope();
     213        unionTable.leaveScope();
     214        contextTable.leaveScope();
     215    }
     216
     217    void Indexer::print( std::ostream &os, int indent ) const {
     218        idTable.dump( os );
     219        typeTable.dump( os );
     220        structTable.dump( os );
     221        enumTable.dump( os );
     222        unionTable.dump( os );
     223        contextTable.dump( os );
     224    }
    282225} // namespace SymTab
  • translator/SymTab/Indexer.h

    rd9a0e76 r17cd4eb  
    1 /*
    2  * This file is part of the Cforall project
    3  *
    4  * A class that indexes the syntax tree.  It is intended to be subclassed by a visitor class
    5  * that wants to use the index.
    6  *
    7  * $Id: Indexer.h,v 1.9 2005/08/29 20:14:18 rcbilson Exp $
    8  *
    9  */
    10 
    111#ifndef SYMTAB_INDEXER_H
    122#define SYMTAB_INDEXER_H
     
    2212
    2313namespace SymTab {
     14    class Indexer : public Visitor {
     15      public:
     16        Indexer( bool useDebug = false );
     17        virtual ~Indexer();
    2418
    25 class Indexer : public Visitor
    26 {
    27 public:
    28   Indexer( bool useDebug = false );
    29   virtual ~Indexer();
     19        //using Visitor::visit;
     20        virtual void visit( ObjectDecl *objectDecl );
     21        virtual void visit( FunctionDecl *functionDecl );
     22        virtual void visit( TypeDecl *typeDecl );
     23        virtual void visit( TypedefDecl *typeDecl );
     24        virtual void visit( StructDecl *aggregateDecl );
     25        virtual void visit( UnionDecl *aggregateDecl );
     26        virtual void visit( EnumDecl *aggregateDecl );
     27        virtual void visit( ContextDecl *aggregateDecl );
    3028
    31 ///   using Visitor::visit;
    32   virtual void visit( ObjectDecl *objectDecl );
    33   virtual void visit( FunctionDecl *functionDecl );
    34   virtual void visit( TypeDecl *typeDecl );
    35   virtual void visit( TypedefDecl *typeDecl );
    36   virtual void visit( StructDecl *aggregateDecl );
    37   virtual void visit( UnionDecl *aggregateDecl );
    38   virtual void visit( EnumDecl *aggregateDecl );
    39   virtual void visit( ContextDecl *aggregateDecl );
     29        virtual void visit( CompoundStmt *compoundStmt );
    4030
    41   virtual void visit( CompoundStmt *compoundStmt );
     31        virtual void visit( ContextInstType *contextInst );
     32        virtual void visit( StructInstType *contextInst );
     33        virtual void visit( UnionInstType *contextInst );
     34 
     35        // when using an indexer manually (e.g., within a mutator traversal), it is necessary to tell the indexer
     36        // explicitly when scopes begin and end
     37        void enterScope();
     38        void leaveScope();
    4239
    43   virtual void visit( ContextInstType *contextInst );
    44   virtual void visit( StructInstType *contextInst );
    45   virtual void visit( UnionInstType *contextInst );
     40        void lookupId( const std::string &id, std::list< DeclarationWithType* >& ) const;
     41        NamedTypeDecl *lookupType( const std::string &id ) const;
     42        StructDecl *lookupStruct( const std::string &id ) const;
     43        EnumDecl *lookupEnum( const std::string &id ) const;
     44        UnionDecl *lookupUnion( const std::string &id ) const;
     45        ContextDecl *lookupContext( const std::string &id ) const;
    4646 
    47   // when using an indexer manually (e.g., within a mutator traversal), it is
    48   // necessary to tell the indexer explicitly when scopes begin and end
    49   void enterScope();
    50   void leaveScope();
    51 
    52   void lookupId( const std::string &id, std::list< DeclarationWithType* >& ) const;
    53   NamedTypeDecl *lookupType( const std::string &id ) const;
    54   StructDecl *lookupStruct( const std::string &id ) const;
    55   EnumDecl *lookupEnum( const std::string &id ) const;
    56   UnionDecl *lookupUnion( const std::string &id ) const;
    57   ContextDecl *lookupContext( const std::string &id ) const;
     47        void print( std::ostream &os, int indent = 0 ) const;
     48      private:
     49        IdTable idTable;
     50        TypeTable typeTable;
     51        StructTable structTable;
     52        EnumTable enumTable;
     53        UnionTable unionTable;
     54        ContextTable contextTable;
    5855 
    59   void print( std::ostream &os, int indent = 0 ) const;
    60 
    61  private:
    62   IdTable idTable;
    63   TypeTable typeTable;
    64   StructTable structTable;
    65   EnumTable enumTable;
    66   UnionTable unionTable;
    67   ContextTable contextTable;
    68  
    69   bool doDebug;         // display debugging trace
    70 };
    71 
     56        bool doDebug;                                   // display debugging trace
     57    };
    7258} // namespace SymTab
    7359
  • translator/SymTab/Validate.cc

    rd9a0e76 r17cd4eb  
    185185            if ( ! visitor.get_declsToAdd().empty() ) {
    186186                translationUnit.splice( addBefore ? i : next, visitor.get_declsToAdd() );
    187             }
     187            } // if
    188188            i = next;
    189         }
     189        } // while
    190190    }
    191191
     
    206206                if ( doDelete ) {
    207207                    delete *i;
    208                 }
     208                } // if
    209209                declList.erase( i );
    210             }
     210            } // if
    211211            i = next;
    212         }
     212        } // while
    213213    }
    214214
     
    227227            Visitor::visit( aggregateDecl );
    228228            inStruct = false;
    229         }
     229        } // if
    230230        // Always remove the hoisted aggregate from the inner structure.
    231231        filter( aggregateDecl->get_members(), isStructOrUnion, false );
     
    275275        // Set the type of each member of the enumeration to be EnumConstant
    276276 
    277         for( std::list< Declaration * >::iterator i = enumDecl->get_members().begin(); i != enumDecl->get_members().end(); ++i ) {
     277        for ( std::list< Declaration * >::iterator i = enumDecl->get_members().begin(); i != enumDecl->get_members().end(); ++i ) {
    278278            ObjectDecl *obj = dynamic_cast< ObjectDecl * >( *i );
    279279            assert( obj );
    280280            obj->set_type( new EnumInstType( Type::Qualifiers( true, false, false, false ), enumDecl->get_name() ) );
    281         }
     281        } // for
    282282        Parent::visit( enumDecl );
    283283    }
     
    285285    namespace {
    286286        template< typename DWTIterator >
    287         void
    288         fixFunctionList( DWTIterator begin, DWTIterator end, FunctionType *func ) {
     287        void fixFunctionList( DWTIterator begin, DWTIterator end, FunctionType *func ) {
    289288            // the only case in which "void" is valid is where it is the only one in the list; then
    290289            // it should be removed entirely
     
    300299                if ( i != end ) {
    301300                    throw SemanticError( "invalid type void in function type ", func );
    302                 }
     301                } // if
    303302            } else {
    304303                ++i;
    305                 for( ; i != end; ++i ) {
     304                for ( ; i != end; ++i ) {
    306305                    FixFunction fixer;
    307306                    *i = (*i )->acceptMutator( fixer );
    308307                    if ( fixer.get_isVoid() ) {
    309308                        throw SemanticError( "invalid type void in function type ", func );
    310                     }
    311                 }
    312             }
     309                    } // if
     310                } // for
     311            } // if
    313312        }
    314313    }
     
    326325        } else {
    327326            indexer = this;
    328         }
     327        } // if
    329328    }
    330329
     
    336335            assert( ! structInst->get_baseStruct() || structInst->get_baseStruct()->get_members().empty() || ! st->get_members().empty() );
    337336            structInst->set_baseStruct( st );
    338         }
     337        } // if
    339338        if ( ! st || st->get_members().empty() ) {
    340339            // use of forward declaration
    341340            forwardStructs[ structInst->get_name() ].push_back( structInst );
    342         }
     341        } // if
    343342    }
    344343
     
    349348        if ( un ) {
    350349            unionInst->set_baseUnion( un );
    351         }
     350        } // if
    352351        if ( ! un || un->get_members().empty() ) {
    353352            // use of forward declaration
    354353            forwardUnions[ unionInst->get_name() ].push_back( unionInst );
    355         }
     354        } // if
    356355    }
    357356
     
    361360        if ( ! ctx ) {
    362361            throw SemanticError( "use of undeclared context " + contextInst->get_name() );
    363         }
    364         for( std::list< TypeDecl * >::const_iterator i = ctx->get_parameters().begin(); i != ctx->get_parameters().end(); ++i ) {
    365             for( std::list< DeclarationWithType * >::const_iterator assert = (*i )->get_assertions().begin(); assert != (*i )->get_assertions().end(); ++assert ) {
     362        } // if
     363        for ( std::list< TypeDecl * >::const_iterator i = ctx->get_parameters().begin(); i != ctx->get_parameters().end(); ++i ) {
     364            for ( std::list< DeclarationWithType * >::const_iterator assert = (*i )->get_assertions().begin(); assert != (*i )->get_assertions().end(); ++assert ) {
    366365                if ( ContextInstType *otherCtx = dynamic_cast< ContextInstType * >(*assert ) ) {
    367366                    cloneAll( otherCtx->get_members(), contextInst->get_members() );
    368367                } else {
    369368                    contextInst->get_members().push_back( (*assert )->clone() );
    370                 }
    371             }
    372         }
     369                } // if
     370            } // for
     371        } // for
    373372        applySubstitution( ctx->get_parameters().begin(), ctx->get_parameters().end(), contextInst->get_parameters().begin(), ctx->get_members().begin(), ctx->get_members().end(), back_inserter( contextInst->get_members() ) );
    374373    }
     
    378377            ForwardStructsType::iterator fwds = forwardStructs.find( structDecl->get_name() );
    379378            if ( fwds != forwardStructs.end() ) {
    380                 for( std::list< StructInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
     379                for ( std::list< StructInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
    381380                    (*inst )->set_baseStruct( structDecl );
    382                 }
     381                } // for
    383382                forwardStructs.erase( fwds );
    384             }
    385         }
     383            } // if
     384        } // if
    386385        Indexer::visit( structDecl );
    387386    }
     
    391390            ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl->get_name() );
    392391            if ( fwds != forwardUnions.end() ) {
    393                 for( std::list< UnionInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
     392                for ( std::list< UnionInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
    394393                    (*inst )->set_baseUnion( unionDecl );
    395                 }
     394                } // for
    396395                forwardUnions.erase( fwds );
    397             }
    398         }
     396            } // if
     397        } // if
    399398        Indexer::visit( unionDecl );
    400399    }
     
    404403            if ( TypeDecl *typeDecl = dynamic_cast< TypeDecl * >( namedTypeDecl ) ) {
    405404                typeInst->set_isFtype( typeDecl->get_kind() == TypeDecl::Ftype );
    406             }
    407         }
     405            } // if
     406        } // if
    408407    }
    409408
     
    413412        } else {
    414413            indexer = this;
    415         }
     414        } // if
    416415    }
    417416
    418417    void forallFixer( Type *func ) {
    419418        // Fix up assertions
    420         for( std::list< TypeDecl * >::iterator type = func->get_forall().begin(); type != func->get_forall().end(); ++type ) {
     419        for ( std::list< TypeDecl * >::iterator type = func->get_forall().begin(); type != func->get_forall().end(); ++type ) {
    421420            std::list< DeclarationWithType * > toBeDone, nextRound;
    422421            toBeDone.splice( toBeDone.end(), (*type )->get_assertions() );
    423422            while ( ! toBeDone.empty() ) {
    424                 for( std::list< DeclarationWithType * >::iterator assertion = toBeDone.begin(); assertion != toBeDone.end(); ++assertion ) {
     423                for ( std::list< DeclarationWithType * >::iterator assertion = toBeDone.begin(); assertion != toBeDone.end(); ++assertion ) {
    425424                    if ( ContextInstType *ctx = dynamic_cast< ContextInstType * >( (*assertion )->get_type() ) ) {
    426                         for( std::list< Declaration * >::const_iterator i = ctx->get_members().begin(); i != ctx->get_members().end(); ++i ) {
     425                        for ( std::list< Declaration * >::const_iterator i = ctx->get_members().begin(); i != ctx->get_members().end(); ++i ) {
    427426                            DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *i );
    428427                            assert( dwt );
     
    449448        if ( PointerType *pointer = dynamic_cast< PointerType * >( object->get_type() ) ) {
    450449            forallFixer( pointer->get_base() );
    451         }
     450        } // if
    452451        Parent::visit( object );
    453452        object->fixUniqueId();
     
    546545        assignDecl->fixUniqueId();
    547546 
    548         for( std::list< Declaration * >::const_iterator member = aggregateDecl->get_members().begin(); member != aggregateDecl->get_members().end(); ++member ) {
     547        for ( std::list< Declaration * >::const_iterator member = aggregateDecl->get_members().begin(); member != aggregateDecl->get_members().end(); ++member ) {
    549548            if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *member ) ) {
    550549                if ( ArrayType *array = dynamic_cast< ArrayType * >( dwt->get_type() ) ) {
     
    552551                } else {
    553552                    makeScalarAssignment( srcParam, dstParam, dwt, back_inserter( assignDecl->get_statements()->get_kids() ) );
    554                 }
    555             }
    556         }
     553                } // if
     554            } // if
     555        } // for
    557556        assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
    558557 
     
    594593            declsToAdd.push_back( makeStructAssignment( structDecl, structInst, functionNesting ) );
    595594            structsDone.insert( structDecl->get_name() );
    596         }
     595        } // if
    597596    }
    598597
     
    602601            unionInst->set_baseUnion( unionDecl );
    603602            declsToAdd.push_back( makeUnionAssignment( unionDecl, unionInst, functionNesting ) );
    604         }
     603        } // if
    605604    }
    606605
     
    617616            assign->get_args().push_back( new CastExpr( new VariableExpr( src ), typeDecl->get_base()->clone() ) );
    618617            stmts->get_kids().push_back( new ReturnStmt( std::list< Label >(), assign ) );
    619         }
     618        } // if
    620619        FunctionType *type = new FunctionType( Type::Qualifiers(), false );
    621620        type->get_returnVals().push_back( new ObjectDecl( "", Declaration::NoStorageClass, LinkageSpec::Cforall, 0, typeInst, 0 ) );
     
    628627    void addDecls( std::list< Declaration * > &declsToAdd, std::list< Statement * > &statements, std::list< Statement * >::iterator i ) {
    629628        if ( ! declsToAdd.empty() ) {
    630             for( std::list< Declaration * >::iterator decl = declsToAdd.begin(); decl != declsToAdd.end(); ++decl ) {
     629            for ( std::list< Declaration * >::iterator decl = declsToAdd.begin(); decl != declsToAdd.end(); ++decl ) {
    631630                statements.insert( i, new DeclStmt( noLabels, *decl ) );
    632             }
     631            } // for
    633632            declsToAdd.clear();
    634         }
     633        } // if
    635634    }
    636635
    637636    void AddStructAssignment::visit( FunctionType *) {
    638         // ensure that we don't add assignment ops for types defined
    639         // as part of the function
     637        // ensure that we don't add assignment ops for types defined as part of the function
    640638    }
    641639
    642640    void AddStructAssignment::visit( PointerType *) {
    643         // ensure that we don't add assignment ops for types defined
    644         // as part of the pointer
     641        // ensure that we don't add assignment ops for types defined as part of the pointer
    645642    }
    646643
    647644    void AddStructAssignment::visit( ContextDecl *) {
    648         // ensure that we don't add assignment ops for types defined
    649         // as part of the context
     645        // ensure that we don't add assignment ops for types defined as part of the context
    650646    }
    651647
     
    714710            delete typeInst;
    715711            return ret;
    716         }
     712        } // if
    717713        return typeInst;
    718714    }
     
    721717        Declaration *ret = Mutator::mutate( tyDecl );
    722718        typedefNames[ tyDecl->get_name() ] = tyDecl;
    723         if ( AggregateDecl *aggDecl = dynamic_cast< AggregateDecl * >( tyDecl->get_base() ) ) {
    724             tyDecl->set_base( 0 );
    725             delete tyDecl;
    726             return aggDecl;
     719        // When a typedef is a forward declaration:
     720        //    typedef struct screen SCREEN;
     721        // the declaration portion must be retained:
     722        //    struct screen;
     723        // because the expansion of the typedef is:
     724        //    void rtn( SCREEN *p ) => void rtn( struct screen *p )
     725        // hence the type-name "screen" must be defined.
     726        // Note, qualifiers on the typedef are superfluous for the forward declaration.
     727        if ( StructInstType *aggDecl = dynamic_cast< StructInstType * >( tyDecl->get_base() ) ) {
     728            return new StructDecl( aggDecl->get_name() );
     729        } else if ( UnionInstType *aggDecl = dynamic_cast< UnionInstType * >( tyDecl->get_base() ) ) {
     730            return new UnionDecl( aggDecl->get_name() );
    727731        } else {
    728732            return ret;
    729         }
     733        } // if
    730734    }
    731735
     
    734738        if ( i != typedefNames.end() ) {
    735739            typedefNames.erase( i ) ;
    736         }
     740        } // if
    737741        return typeDecl;
    738742    }
     
    770774                    delete *i;
    771775                    compoundStmt->get_kids().erase( i );
    772                 }
    773             }
     776                } // if
     777            } // if
    774778            i = next;
    775         }
     779        } // while
    776780        typedefNames = oldNames;
    777781        return ret;
  • translator/SynTree/BasicType.cc

    rd9a0e76 r17cd4eb  
    1 /*
    2  * This file is part of the Cforall project
    3  *
    4  * $Id: BasicType.cc,v 1.10 2005/08/29 20:59:25 rcbilson Exp $
    5  *
    6  */
    7 
    81#include <cassert>
    92
     
    114
    125
    13 BasicType::BasicType( const Type::Qualifiers &tq, Kind bt )
    14     : Type( tq ), kind( bt )
    15 {
    16 }
     6BasicType::BasicType( const Type::Qualifiers &tq, Kind bt ) : Type( tq ), kind( bt ) {}
    177
    18 void
    19 BasicType::print( std::ostream &os, int indent ) const
    20 {
     8void BasicType::print( std::ostream &os, int indent ) const {
    219    static const char *kindNames[] = { 
    22         "bool", "char", "signed char", "unsigned char", "short signed int", "short unsigned int",
     10        "_Bool", "char", "signed char", "unsigned char", "short signed int", "short unsigned int",
    2311        "signed int", "unsigned int", "long signed int", "long unsigned int", "long long signed int",
    24         "long long unsigned int", "float", "double", "long double", "float complex", "double complex",
    25         "long double complex", "float imaginary", "double imaginary", "long double imaginary"
     12        "long long unsigned int", "float", "double", "long double", "float _Complex", "double _Complex",
     13        "long double _Complex", "float _Imaginary", "double _Imaginary", "long double _Imaginary"
    2614    };
    2715
     
    3018}
    3119
    32 bool
    33 BasicType::isInteger() const
    34 {
     20bool BasicType::isInteger() const {
    3521    switch( kind ) {
    36     case Bool:
    37     case Char:
    38     case SignedChar:
    39     case UnsignedChar:
    40     case ShortSignedInt:
    41     case ShortUnsignedInt:
    42     case SignedInt:
    43     case UnsignedInt:
    44     case LongSignedInt:
    45     case LongUnsignedInt:
    46     case LongLongSignedInt:
    47     case LongLongUnsignedInt:
     22      case Bool:
     23      case Char:
     24      case SignedChar:
     25      case UnsignedChar:
     26      case ShortSignedInt:
     27      case ShortUnsignedInt:
     28      case SignedInt:
     29      case UnsignedInt:
     30      case LongSignedInt:
     31      case LongUnsignedInt:
     32      case LongLongSignedInt:
     33      case LongLongUnsignedInt:
    4834        return true;
    4935
    50     case Float:
    51     case Double:
    52     case LongDouble:
    53     case FloatComplex:
    54     case DoubleComplex:
    55     case LongDoubleComplex:
    56     case FloatImaginary:
    57     case DoubleImaginary:
    58     case LongDoubleImaginary:
     36      case Float:
     37      case Double:
     38      case LongDouble:
     39      case FloatComplex:
     40      case DoubleComplex:
     41      case LongDoubleComplex:
     42      case FloatImaginary:
     43      case DoubleImaginary:
     44      case LongDoubleImaginary:
    5945        return false;
    6046
    61     case NUMBER_OF_BASIC_TYPES:
     47      case NUMBER_OF_BASIC_TYPES:
    6248        assert( false );
    6349    }
  • translator/SynTree/Declaration.h

    rd9a0e76 r17cd4eb  
    1 /*
    2  * This file is part of the Cforall project
    3  *
    4  * $Id: Declaration.h,v 1.22 2005/08/29 20:59:25 rcbilson Exp $
    5  *
    6  */
    7 
    81#ifndef DECLARATION_H
    92#define DECLARATION_H
     
    158
    169
    17 class Declaration
    18 {
    19 public:
    20     enum StorageClass
    21     { 
     10class Declaration {
     11  public:
     12    enum StorageClass { 
    2213        NoStorageClass,
    2314        Auto,
     
    2718        Fortran
    2819    }; 
    29    
     20
    3021    Declaration( const std::string &name, StorageClass sc, LinkageSpec::Type linkage );
    3122    Declaration( const Declaration &other );
     
    3930    void set_linkage( LinkageSpec::Type newValue ) { linkage = newValue; }
    4031    UniqueId get_uniqueId() const { return uniqueId; }
    41    
     32
    4233    void fixUniqueId( void );
    4334    virtual Declaration *clone() const = 0;
     
    4637    virtual void print( std::ostream &os, int indent = 0 ) const = 0;
    4738    virtual void printShort( std::ostream &os, int indent = 0 ) const = 0;
    48    
     39
    4940    static const char* storageClassName[]; 
    50    
     41
    5142    static void dumpIds( std::ostream &os );
    5243    static Declaration *declFromId( UniqueId id );
    53    
    54 private:
     44  private:
    5545    std::string name;
    5646    StorageClass storageClass;
     
    5949};
    6050
    61 class DeclarationWithType : public Declaration
    62 {
    63 public:
     51class DeclarationWithType : public Declaration {
     52  public:
    6453    DeclarationWithType( const std::string &name, StorageClass sc, LinkageSpec::Type linkage );
    6554    DeclarationWithType( const DeclarationWithType &other );
     
    6857    std::string get_mangleName() const { return mangleName; }
    6958    void set_mangleName( std::string newValue ) { mangleName = newValue; }
    70    
     59
    7160    virtual DeclarationWithType *clone() const = 0;
    7261    virtual DeclarationWithType *acceptMutator( Mutator &m ) = 0;
    73    
     62
    7463    virtual Type *get_type() const = 0;
    7564    virtual void set_type(Type *) = 0;
    76    
    77 private:
    78     // this represents the type with all types and typedefs expanded
    79     // it is generated by SymTab::Validate::Pass2
     65  private:
     66    // this represents the type with all types and typedefs expanded it is generated by SymTab::Validate::Pass2
    8067    std::string mangleName;
    8168};
    8269
    83 class ObjectDecl : public DeclarationWithType
    84 {
     70class ObjectDecl : public DeclarationWithType {
    8571    typedef DeclarationWithType Parent;
    86 
    87 public:
     72  public:
    8873    ObjectDecl( const std::string &name, StorageClass sc, LinkageSpec::Type linkage, Expression *bitfieldWidth, Type *type, Initializer *init );
    8974    ObjectDecl( const ObjectDecl &other );
     
    9782    Expression *get_bitfieldWidth() const { return bitfieldWidth; }
    9883    void set_bitfieldWidth( Expression *newValue ) { bitfieldWidth = newValue; }
    99    
     84
    10085    virtual ObjectDecl *clone() const { return new ObjectDecl( *this ); }
    10186    virtual void accept( Visitor &v ) { v.visit( this ); }
     
    10388    virtual void print( std::ostream &os, int indent = 0 ) const;
    10489    virtual void printShort( std::ostream &os, int indent = 0 ) const;
    105    
    106 private:
     90  private:
    10791    Type *type;
    10892    Initializer *init;
     
    11094};
    11195
    112 class FunctionDecl : public DeclarationWithType
    113 {
     96class FunctionDecl : public DeclarationWithType {
    11497    typedef DeclarationWithType Parent;
    115 
    116 public:
     98  public:
    11799    FunctionDecl( const std::string &name, StorageClass sc, LinkageSpec::Type linkage, FunctionType *type, CompoundStmt *statements, bool isInline );
    118100    FunctionDecl( const FunctionDecl &other );
     
    130112    std::list< std::string >& get_oldIdents() { return oldIdents; }
    131113    std::list< Declaration* >& get_oldDecls() { return oldDecls; }
    132    
     114
    133115    virtual FunctionDecl *clone() const { return new FunctionDecl( *this ); }
    134116    virtual void accept( Visitor &v ) { v.visit( this ); }
     
    136118    virtual void print( std::ostream &os, int indent = 0 ) const;
    137119    virtual void printShort( std::ostream &os, int indent = 0 ) const;
    138    
    139 private:
     120  private:
    140121    FunctionType *type;
    141122    CompoundStmt *statements;
     
    145126};
    146127
    147 class NamedTypeDecl : public Declaration
    148 {
     128class NamedTypeDecl : public Declaration {
    149129    typedef Declaration Parent;
    150 
    151 public:
     130  public:
    152131    NamedTypeDecl( const std::string &name, StorageClass sc, Type *type );
    153132    NamedTypeDecl( const TypeDecl &other );
     
    162141    virtual void print( std::ostream &os, int indent = 0 ) const;
    163142    virtual void printShort( std::ostream &os, int indent = 0 ) const;
    164    
    165 protected:
     143  protected:
    166144    virtual std::string typeString() const = 0;
    167    
    168 private:
     145  private:
    169146    Type *base;
    170147    std::list< TypeDecl* > parameters;
     
    172149};
    173150
    174 class TypeDecl : public NamedTypeDecl
    175 {
     151class TypeDecl : public NamedTypeDecl {
    176152    typedef NamedTypeDecl Parent;
    177 
    178 public:
     153  public:
    179154    enum Kind { Any, Dtype, Ftype };
    180155
    181156    TypeDecl( const std::string &name, StorageClass sc, Type *type, Kind kind );
    182157    TypeDecl( const TypeDecl &other );
    183    
     158
    184159    Kind get_kind() const { return kind; }
    185160
     
    187162    virtual void accept( Visitor &v ) { v.visit( this ); }
    188163    virtual TypeDecl *acceptMutator( Mutator &m ) { return m.mutate( this ); }
    189 
    190 private:
     164  private:
    191165    virtual std::string typeString() const;
    192166    Kind kind;
    193167};
    194    
    195 class TypedefDecl : public NamedTypeDecl
    196 {
     168
     169class TypedefDecl : public NamedTypeDecl {
    197170    typedef NamedTypeDecl Parent;
    198 
    199 public:
     171  public:
    200172    TypedefDecl( const std::string &name, StorageClass sc, Type *type ) : Parent( name, sc, type ) {}
    201173    TypedefDecl( const TypedefDecl &other ) : Parent( other ) {}
    202    
     174
    203175    virtual TypedefDecl *clone() const { return new TypedefDecl( *this ); }
    204176    virtual void accept( Visitor &v ) { v.visit( this ); }
    205177    virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
    206 
    207 private:
    208     virtual std::string typeString() const;
    209 };
    210    
    211 class AggregateDecl : public Declaration
    212 {
     178  private:
     179    virtual std::string typeString() const;
     180};
     181
     182class AggregateDecl : public Declaration {
    213183    typedef Declaration Parent;
    214 
    215 public:
     184  public:
    216185    AggregateDecl( const std::string &name );
    217186    AggregateDecl( const AggregateDecl &other );
     
    220189    std::list<Declaration*>& get_members() { return members; }
    221190    std::list<TypeDecl*>& get_parameters() { return parameters; }
    222    
    223     virtual void print( std::ostream &os, int indent = 0 ) const;
    224     virtual void printShort( std::ostream &os, int indent = 0 ) const;
    225 
    226 protected:
     191
     192    virtual void print( std::ostream &os, int indent = 0 ) const;
     193    virtual void printShort( std::ostream &os, int indent = 0 ) const;
     194  protected:
    227195    virtual std::string typeString() const = 0;
    228    
    229 private:
     196
     197  private:
    230198    std::list<Declaration*> members;
    231199    std::list<TypeDecl*> parameters;
    232200};
    233201
    234 class StructDecl : public AggregateDecl
    235 {
    236     typedef AggregateDecl Parent;
    237 
    238 public:
     202class StructDecl : public AggregateDecl {
     203    typedef AggregateDecl Parent;
     204  public:
    239205    StructDecl( const std::string &name ) : Parent( name ) {}
    240206    StructDecl( const StructDecl &other ) : Parent( other ) {}
    241    
     207
    242208    virtual StructDecl *clone() const { return new StructDecl( *this ); }
    243209    virtual void accept( Visitor &v ) { v.visit( this ); }
    244210    virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
    245211
    246 private:
    247     virtual std::string typeString() const;
    248 };
    249    
    250 class UnionDecl : public AggregateDecl
    251 {
    252     typedef AggregateDecl Parent;
    253 
    254 public:
     212  private:
     213    virtual std::string typeString() const;
     214};
     215
     216class UnionDecl : public AggregateDecl {
     217    typedef AggregateDecl Parent;
     218  public:
    255219    UnionDecl( const std::string &name ) : Parent( name ) {}
    256220    UnionDecl( const UnionDecl &other ) : Parent( other ) {}
    257    
     221
    258222    virtual UnionDecl *clone() const { return new UnionDecl( *this ); }
    259223    virtual void accept( Visitor &v ) { v.visit( this ); }
    260224    virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
    261 
    262 private:
    263     virtual std::string typeString() const;
    264 };
    265    
    266 class EnumDecl : public AggregateDecl
    267 {
    268     typedef AggregateDecl Parent;
    269 
    270 public:
     225  private:
     226    virtual std::string typeString() const;
     227};
     228
     229class EnumDecl : public AggregateDecl {
     230    typedef AggregateDecl Parent;
     231  public:
    271232    EnumDecl( const std::string &name ) : Parent( name ) {}
    272233    EnumDecl( const EnumDecl &other ) : Parent( other ) {}
    273    
     234
    274235    virtual EnumDecl *clone() const { return new EnumDecl( *this ); }
    275236    virtual void accept( Visitor &v ) { v.visit( this ); }
    276237    virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
    277 
    278 private:
    279     virtual std::string typeString() const;
    280 };
    281    
    282 class ContextDecl : public AggregateDecl
    283 {
    284     typedef AggregateDecl Parent;
    285 
    286 public:
     238  private:
     239    virtual std::string typeString() const;
     240};
     241
     242class ContextDecl : public AggregateDecl {
     243    typedef AggregateDecl Parent;
     244  public:
    287245    ContextDecl( const std::string &name ) : Parent( name ) {}
    288246    ContextDecl( const ContextDecl &other ) : Parent( other ) {}
    289    
     247
    290248    virtual ContextDecl *clone() const { return new ContextDecl( *this ); }
    291249    virtual void accept( Visitor &v ) { v.visit( this ); }
    292250    virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
    293 
    294 private:
    295     virtual std::string typeString() const;
    296 };
    297    
    298 
    299 
    300 #endif /* #ifndef DECLARATION_H */
     251  private:
     252    virtual std::string typeString() const;
     253};
     254
     255#endif // DECLARATION_H
  • translator/SynTree/NamedTypeDecl.cc

    rd9a0e76 r17cd4eb  
    66NamedTypeDecl::NamedTypeDecl( const std::string &name, StorageClass sc, Type *base )
    77    : Parent( name, sc, LinkageSpec::Cforall ), base( base )
    8 {
    9 }
     8{}
    109
    1110NamedTypeDecl::NamedTypeDecl( const TypeDecl &other )
     
    1615}
    1716
    18 NamedTypeDecl::~NamedTypeDecl()
    19 {
     17NamedTypeDecl::~NamedTypeDecl() {
    2018    delete base;
    2119    deleteAll( parameters );
     
    2321}
    2422
    25 void
    26 NamedTypeDecl::print( std::ostream &os, int indent ) const
    27 {
     23void NamedTypeDecl::print( std::ostream &os, int indent ) const {
    2824    using namespace std;
    2925   
    30     if( get_name() != "" ) {
     26    if ( get_name() != "" ) {
    3127        os << get_name() << ": a ";
    32     }
    33     if( get_storageClass() != NoStorageClass ) {
     28    } // if
     29    if ( get_storageClass() != NoStorageClass ) {
    3430        os << storageClassName[ get_storageClass() ] << ' ';
    35     }
     31    } // if
    3632    os << typeString();
    37     if( base ) {
     33    if ( base ) {
    3834        os << " for ";
    3935        base->print( os, indent );
    40     }
    41     if( !parameters.empty() ) {
     36    } // if
     37    if ( !parameters.empty() ) {
    4238        os << endl << string( indent, ' ' ) << "with parameters" << endl;
    4339        printAll( parameters, os, indent+2 );
    44     }
    45     if( !assertions.empty() ) {
     40    } // if
     41    if ( !assertions.empty() ) {
    4642        os << endl << string( indent, ' ' ) << "with assertions" << endl;
    4743        printAll( assertions, os, indent+2 );
    48     }
     44    } // if
    4945}
    5046
    51 void
    52 NamedTypeDecl::printShort( std::ostream &os, int indent ) const
    53 {
     47void NamedTypeDecl::printShort( std::ostream &os, int indent ) const {
    5448    using namespace std;
    5549   
    56     if( get_name() != "" ) {
     50    if ( get_name() != "" ) {
    5751        os << get_name() << ": a ";
    58     }
    59     if( get_storageClass() != NoStorageClass ) {
     52    } // if
     53    if ( get_storageClass() != NoStorageClass ) {
    6054        os << storageClassName[ get_storageClass() ] << ' ';
    61     }
     55    } // if
    6256    os << typeString();
    63     if( base ) {
     57    if ( base ) {
    6458        os << " for ";
    6559        base->print( os, indent );
    66     }
    67     if( !parameters.empty() ) {
     60    } // if
     61    if ( !parameters.empty() ) {
    6862        os << endl << string( indent, ' ' ) << "with parameters" << endl;
    6963        printAll( parameters, os, indent+2 );
    70     }
     64    } // if
    7165}
    7266
    7367std::string TypedefDecl::typeString() const { return "typedef"; }
    74    
  • translator/SynTree/Type.cc

    rd9a0e76 r17cd4eb  
    1 /*
    2  * This file is part of the Cforall project
    3  *
    4  * $Id: Type.cc,v 1.6 2005/08/29 20:59:26 rcbilson Exp $
    5  *
    6  */
    7 
    81#include "SynTree.h"
    92#include "Visitor.h"
     
    125#include "utility.h"
    136
     7const char *BasicType::typeNames[BasicType::NUMBER_OF_BASIC_TYPES] = {
     8    "_Bool",
     9    "char",
     10    "char",
     11    "unsigned char",
     12    "short",
     13    "short unsigned",
     14    "int",
     15    "unsigned int",
     16    "long int",
     17    "long unsigned int",
     18    "long long int",
     19    "long long unsigned int",
     20    "float",
     21    "double",
     22    "long double",
     23    "float _Complex",
     24    "double _Complex",
     25    "long double _Complex",
     26    "float _Imaginary",
     27    "double _Imaginary",
     28    "long double _Imaginary",
     29};
    1430
    15 Type::Type( const Qualifiers &tq )
    16     : tq( tq )
    17 {
    18 }
     31Type::Type( const Qualifiers &tq ) : tq( tq ) {}
    1932
    20 Type::Type( const Type &other )
    21     : tq( other.tq )
    22 {
     33Type::Type( const Type &other ) : tq( other.tq ) {
    2334    cloneAll( other.forall, forall );
    2435}
    2536
    26 Type::~Type()
    27 {
     37Type::~Type() {
    2838    deleteAll( forall );
    2939}
    3040
    31 void
    32 Type::print( std::ostream &os, int indent ) const
    33 {
    34     if( !forall.empty() ) {
     41void Type::print( std::ostream &os, int indent ) const {
     42    if ( !forall.empty() ) {
    3543        os << "forall" << std::endl;
    3644        printAll( forall, os, indent + 4 );
    3745        os << std::string( indent+2, ' ' );
    38     }
    39     if( tq.isConst ) {
     46    } // if
     47    if ( tq.isConst ) {
    4048        os << "const ";
    41     }
    42     if( tq.isVolatile ) {
     49    } // if
     50    if ( tq.isVolatile ) {
    4351        os << "volatile ";
    44     }
    45     if( tq.isRestrict ) {
     52    } // if
     53    if ( tq.isRestrict ) {
    4654        os << "restrict ";
    47     }
    48     if( tq.isLvalue ) {
     55    } // if
     56    if ( tq.isLvalue ) {
    4957        os << "lvalue ";
    50     }
     58    } // if
    5159}
    52 
  • translator/SynTree/Type.h

    rd9a0e76 r17cd4eb  
    9090    }; 
    9191
     92    static const char *typeNames[];                     // string names for basic types, MUST MATCH with Kind
     93
    9294    BasicType( const Type::Qualifiers &tq, Kind bt );
    9395
  • translator/Tests/Parser/Makefile

    rd9a0e76 r17cd4eb  
    44TESTS = $(EXPECTED:Expected/%=%)
    55TEST_IN = $(TESTS:.tst=.c)
    6 DIFF = /software/gnu/bin/diff#diff
     6DIFF = diff
    77
    88%.tst:%.c $(CFA)
     
    1515          $(DIFF) -B -w Expected/$$i $$i | tee -a report; \
    1616        done
     17
     18clean:
     19        rm -f *.tst
  • translator/examples/Makefile

    rd9a0e76 r17cd4eb  
    2020
    2121${EXEC1} : ${OBJECTS1}                          # link step 1st executable
    22         ${CC} ${CCFLAGS} $^ -o $@               # additional object files before $^
     22        ${CC} ${CFLAGS} $^ -o $@                # additional object files before $^
    2323
    2424${EXEC2} : ${OBJECTS2}                          # link step 2nd executable
    25         ${CC} ${CCFLAGS} $^ -o $@               # additional object files before $^
     25        ${CC} ${CFLAGS} $^ -o $@                # additional object files before $^
    2626
    2727${OBJECTS} : ${MAKEFILE_NAME}                   # OPTIONAL : changes to this file => recompile
  • translator/examples/includes.c

    rd9a0e76 r17cd4eb  
    2525//#include <wchar.h>        // FAILS -- includes locale.h
    2626//#include <wctype.h>       // FAILS -- includes locale.h
     27#include <curses.h>
    2728#else
    28 #include <curses.h>
    29 #endif
     29#include <aio.h>
     30#endif // 0
    3031
    3132// Local Variables: //
  • translator/examples/swap.c

    rd9a0e76 r17cd4eb  
    44
    55forall( type T )
    6 void swap( T left, T right ) {
    7     T temp = left;
    8     left = right;
    9     right = temp;
     6T swap( T *left, T *right ) {
     7    T temp;
     8    temp = *left;
     9    *left = *right;
     10    *right = temp;
     11    return *right;
    1012}
    1113
    1214int main() {
    1315    int x = 1, y = 2;
    14     printf( "%d %d", x, y );
    15     swap( x, y );
    16     printf( "%d %d", x, y );
     16    printf( "%d %d\n", x, y );
     17    int w;
     18    w = swap( &x, &y );
     19    printf( "%d %d %d\n", w, x, y );
    1720}
    1821
  • translator/examples/vector_test.c

    rd9a0e76 r17cd4eb  
    2222    sout << "Array elements:\n";
    2323//    write_all( begin( vec ), end( vec ), sout );
    24     sout << "\n";
    25 #if 0
    26     write_reverse( begin( vec ), end( vec ), sout );
    27     sout << "\n";
    28 
    29 #endif
     24//    sout << "\n";
    3025    for ( int index = 0; index <= last( vec ); index += 1 ) {
    3126        sout << vec[ index ] << " ";
    3227    }
    3328    sout << "\n";
     29#if 1
     30    sout << "Array elements reversed:\n";
     31    write_reverse( begin( vec ), end( vec ), sout );
     32    sout << "\n";
     33#endif
    3434}
    3535
Note: See TracChangeset for help on using the changeset viewer.