Ignore:
Timestamp:
May 19, 2015, 4:58:14 PM (11 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:
843054c2
Parents:
01aeade
Message:

licencing: sixth groups of files

File:
1 edited

Legend:

Unmodified
Added
Removed
  • translator/ControlStruct/CaseRangeMutator.cc

    r01aeade ra08ba92  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // XXX.cc --
     7// CaseRangeMutator.cc --
    88//
    99// Author           : Richard C. Bilson
    1010// Created On       : Mon May 18 07:44:20 2015
    11 // Last Modified By :
    12 // Last Modified On :
    13 // Update Count     : 0
    14 //
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Tue May 19 13:00:28 2015
     13// Update Count     : 3
     14//
     15
    1516#include <list>
    1617#include <cassert>
     
    2728
    2829namespace ControlStruct {
    29     Statement *CaseRangeMutator::mutate(ChooseStmt *chooseStmt) {
    30         /* There shouldn't be any `choose' statements by now, throw an exception or something. */
    31         throw( 0 ) ; /* FIXME */
    32     }
    33 
    34     Statement *CaseRangeMutator::mutate(SwitchStmt *switchStmt) {
    35         std::list< Statement * > &cases = switchStmt->get_branches();
    36 
    37         // a `for' would be more natural... all this contortions are because `replace' invalidates the iterator
    38         std::list< Statement * >::iterator i = cases.begin();
    39         while (  i != cases.end() ) {
    40             (*i)->acceptMutator( *this );
    41 
    42             if ( ! newCaseLabels.empty() ) {
    43                 std::list< Statement * > newCases;
    44 
    45                 // transform( newCaseLabels.begin(), newCaseLabels.end(), bnd1st( ptr_fun( ctor< CaseStmt, Label, Expression * > ) ) );
    46 
    47                 for ( std::list< Expression * >::iterator j = newCaseLabels.begin();
    48                       j != newCaseLabels.end(); j++ ) {
    49                     std::list<Label> emptyLabels;
    50                     std::list< Statement *> emptyStmts;
    51                     newCases.push_back( new CaseStmt( emptyLabels, *j, emptyStmts ) );
    52                 }
    53 
    54                 if ( CaseStmt *currentCase = dynamic_cast< CaseStmt * > ( *i ) )
    55                     if ( ! currentCase->get_statements().empty() ) {
    56                         CaseStmt *lastCase = dynamic_cast< CaseStmt * > ( newCases.back() );
    57                         if ( lastCase == 0 ) { throw ( 0 ); /* FIXME */ } // something is very wrong, as I just made these, and they were all cases
    58                         // transfer the statement block (if any) to the new list:
    59                         lastCase->set_statements( currentCase->get_statements() );
    60                     }
    61                 std::list< Statement * >::iterator j = i; advance( j, 1 );
    62                 replace ( cases, i, newCases );
    63                 i = j;
    64                 newCaseLabels.clear();
    65             } else
    66                 i++;
    67         } // while
    68 
    69         return switchStmt;
    70     }
    71 
    72     Statement *CaseRangeMutator::mutate(FallthruStmt *fallthruStmt) {
    73         //delete fallthruStmt;
    74         return new NullStmt();
    75     }
    76 
    77     Statement *CaseRangeMutator::mutate(CaseStmt *caseStmt) {
    78         UntypedExpr *cond;
    79         if ( (cond = dynamic_cast< UntypedExpr * >( caseStmt->get_condition() )) != 0 ) {
    80             NameExpr *nmfunc;
    81             if ( (nmfunc = dynamic_cast< NameExpr *>( cond->get_function() )) != 0 ) {
    82                 if ( nmfunc->get_name() == std::string("Range") ) {
    83                     assert( cond->get_args().size() == 2 );
    84                     std::list<Expression *>::iterator i = cond->get_args().begin();
    85                     Expression *lo = *i, *hi = *(++i); // "unnecessary" temporaries
    86                     fillRange( lo, hi);
    87                 }
    88             }
    89         } else if ( TupleExpr *tcond = dynamic_cast< TupleExpr * >( caseStmt->get_condition() ) ) {
    90             // case list
    91             assert( ! tcond->get_exprs().empty() );
    92             for ( std::list< Expression * >::iterator i = tcond->get_exprs().begin(); i != tcond->get_exprs().end(); i++ )
    93                 newCaseLabels.push_back( *i ); // do I need to clone them?
    94         } // if
    95 
    96         std::list< Statement * > &stmts = caseStmt->get_statements();
    97         mutateAll ( stmts, *this );
    98 
    99         return caseStmt;
    100     }
    101 
    102     void CaseRangeMutator::fillRange(Expression *lo, Expression *hi) {
    103         // generate the actual range (and check for consistency)
    104         Constant *c_lo, *c_hi;
    105         ConstantExpr *ce_lo, *ce_hi;
    106         ce_lo = dynamic_cast< ConstantExpr * >( lo );
    107         ce_hi = dynamic_cast< ConstantExpr * >( hi );
    108 
    109         if ( ce_lo && ce_hi ) {
    110             c_lo = ce_lo->get_constant(); c_hi = ce_hi->get_constant();
    111         } /* else {
    112              if ( ! ce_lo ) ;
    113              if ( ! ce_hi ) ;
    114              } */
    115         BasicType *ty_lo = dynamic_cast< BasicType * >( c_lo->get_type() ),
    116             *ty_hi = dynamic_cast< BasicType * >( c_hi->get_type() );
    117    
    118         if ( ! ty_lo || ! ty_hi )
    119             return; // one of them is not a constant
    120 
    121         switch ( ty_lo->get_kind() ) {
    122           case BasicType::Char:
    123           case BasicType::UnsignedChar:
    124             switch ( ty_hi->get_kind() ){
     30        Statement *CaseRangeMutator::mutate( ChooseStmt *chooseStmt ) {
     31                // There shouldn't be any `choose' statements by now, throw an exception or something.
     32                throw( 0 ) ; /* FIXME */
     33        }
     34
     35        Statement *CaseRangeMutator::mutate( SwitchStmt *switchStmt ) {
     36                std::list< Statement * > &cases = switchStmt->get_branches();
     37
     38                // a `for' would be more natural... all this contortions are because `replace' invalidates the iterator
     39                std::list< Statement * >::iterator i = cases.begin();
     40                while ( i != cases.end() ) {
     41                        (*i )->acceptMutator( *this );
     42
     43                        if ( ! newCaseLabels.empty() ) {
     44                                std::list< Statement * > newCases;
     45
     46                                // transform( newCaseLabels.begin(), newCaseLabels.end(), bnd1st( ptr_fun( ctor< CaseStmt, Label, Expression * > ) ) );
     47
     48                                for ( std::list< Expression * >::iterator j = newCaseLabels.begin();
     49                                          j != newCaseLabels.end(); j++ ) {
     50                                        std::list<Label> emptyLabels;
     51                                        std::list< Statement *> emptyStmts;
     52                                        newCases.push_back( new CaseStmt( emptyLabels, *j, emptyStmts ) );
     53                                } // for
     54
     55                                if ( CaseStmt *currentCase = dynamic_cast< CaseStmt * > ( *i ) )
     56                                        if ( ! currentCase->get_statements().empty() ) {
     57                                                CaseStmt *lastCase = dynamic_cast< CaseStmt * > ( newCases.back() );
     58                                                if ( lastCase == 0 ) { throw ( 0 ); /* FIXME */ } // something is very wrong, as I just made these, and they were all cases
     59                                                // transfer the statement block ( if any ) to the new list:
     60                                                lastCase->set_statements( currentCase->get_statements() );
     61                                        } // if
     62                                std::list< Statement * >::iterator j = i; advance( j, 1 );
     63                                replace ( cases, i, newCases );
     64                                i = j;
     65                                newCaseLabels.clear();
     66                        } else
     67                                i++;
     68                } // while
     69
     70                return switchStmt;
     71        }
     72
     73        Statement *CaseRangeMutator::mutate( FallthruStmt *fallthruStmt ) {
     74                //delete fallthruStmt;
     75                return new NullStmt();
     76        }
     77
     78        Statement *CaseRangeMutator::mutate( CaseStmt *caseStmt ) {
     79                UntypedExpr *cond;
     80                if ( ( cond = dynamic_cast< UntypedExpr * >( caseStmt->get_condition() )) != 0 ) {
     81                        NameExpr *nmfunc;
     82                        if ( ( nmfunc = dynamic_cast< NameExpr *>( cond->get_function() )) != 0 ) {
     83                                if ( nmfunc->get_name() == std::string("Range") ) {
     84                                        assert( cond->get_args().size() == 2 );
     85                                        std::list<Expression *>::iterator i = cond->get_args().begin();
     86                                        Expression *lo = *i, *hi = *(++i ); // "unnecessary" temporaries
     87                                        fillRange( lo, hi );
     88                                } // if
     89                        } // if
     90                } else if ( TupleExpr *tcond = dynamic_cast< TupleExpr * >( caseStmt->get_condition() ) ) {
     91                        // case list
     92                        assert( ! tcond->get_exprs().empty() );
     93                        for ( std::list< Expression * >::iterator i = tcond->get_exprs().begin(); i != tcond->get_exprs().end(); i++ )
     94                                newCaseLabels.push_back( *i ); // do I need to clone them?
     95                } // if
     96
     97                std::list< Statement * > &stmts = caseStmt->get_statements();
     98                mutateAll ( stmts, *this );
     99
     100                return caseStmt;
     101        }
     102
     103        void CaseRangeMutator::fillRange( Expression *lo, Expression *hi ) {
     104                // generate the actual range ( and check for consistency )
     105                Constant *c_lo, *c_hi;
     106                ConstantExpr *ce_lo, *ce_hi;
     107                ce_lo = dynamic_cast< ConstantExpr * >( lo );
     108                ce_hi = dynamic_cast< ConstantExpr * >( hi );
     109
     110                if ( ce_lo && ce_hi ) {
     111                        c_lo = ce_lo->get_constant(); c_hi = ce_hi->get_constant();
     112                } /* else {
     113                         if ( ! ce_lo ) ;
     114                         if ( ! ce_hi ) ;
     115                         } */
     116                BasicType *ty_lo = dynamic_cast< BasicType * >( c_lo->get_type() ),
     117                        *ty_hi = dynamic_cast< BasicType * >( c_hi->get_type() );
     118       
     119                if ( ! ty_lo || ! ty_hi )
     120                        return; // one of them is not a constant
     121
     122                switch ( ty_lo->get_kind() ) {
    125123                  case BasicType::Char:
    126124                  case BasicType::UnsignedChar:
    127                     // first case, they are both printable ASCII characters represented as 'x'
    128                     if ( c_lo->get_value().size() == 3 && c_hi->get_value().size() == 3 ) {
    129                         char ch_lo = (c_lo->get_value())[1], ch_hi = (c_hi->get_value())[1];
    130 
    131                         if ( ch_lo > ch_hi ) { char t=ch_lo; ch_lo=ch_hi; ch_hi=t; }
    132 
    133                         for ( char c = ch_lo; c <=  ch_hi; c++ ){
    134                             Type::Qualifiers q;
    135                             Constant cnst( new BasicType(q, BasicType::Char),
    136                                            std::string("'") + c + std::string("'") );
    137                             newCaseLabels.push_back( new ConstantExpr( cnst ) );
     125                        switch ( ty_hi->get_kind() ) {
     126                          case BasicType::Char:
     127                          case BasicType::UnsignedChar:
     128                                // first case, they are both printable ASCII characters represented as 'x'
     129                                if ( c_lo->get_value().size() == 3 && c_hi->get_value().size() == 3 ) {
     130                                        char ch_lo = ( c_lo->get_value())[1], ch_hi = ( c_hi->get_value())[1];
     131
     132                                        if ( ch_lo > ch_hi ) { char t=ch_lo; ch_lo=ch_hi; ch_hi=t; }
     133
     134                                        for ( char c = ch_lo; c <=  ch_hi; c++ ) {
     135                                                Type::Qualifiers q;
     136                                                Constant cnst( new BasicType( q, BasicType::Char ),
     137                                                                           std::string("'") + c + std::string("'") );
     138                                                newCaseLabels.push_back( new ConstantExpr( cnst ) );
     139                                        } // for
     140
     141                                        return;
     142                                } // if
     143                                break;
     144                          default:
     145                                // error: incompatible constants
     146                                break;
     147                        } // switch
     148                        break;
     149                  case BasicType::ShortSignedInt:
     150                  case BasicType::ShortUnsignedInt:
     151                  case BasicType::SignedInt:
     152                  case BasicType::UnsignedInt:
     153                  case BasicType::LongSignedInt:
     154                  case BasicType::LongUnsignedInt:
     155                  case BasicType::LongLongSignedInt:
     156                  case BasicType::LongLongUnsignedInt:
     157                        switch ( ty_hi->get_kind() ) {
     158                          case BasicType::ShortSignedInt:
     159                          case BasicType::ShortUnsignedInt:
     160                          case BasicType::SignedInt:
     161                          case BasicType::UnsignedInt:
     162                          case BasicType::LongSignedInt:
     163                          case BasicType::LongUnsignedInt:
     164                          case BasicType::LongLongSignedInt:
     165                          case BasicType::LongLongUnsignedInt: {
     166                                  int i_lo = atoi( c_lo->get_value().c_str()),
     167                                          i_hi = atoi( c_hi->get_value().c_str());
     168
     169                                  if ( i_lo > i_hi ) { int t=i_lo; i_lo=i_hi; i_hi=t; }
     170
     171                                  for ( int c = i_lo; c <=  i_hi; c++ ) {
     172                                          Type::Qualifiers q;
     173                                          Constant cnst( new BasicType( q, ty_hi->get_kind()), // figure can't hurt (used to think in positives)
     174                                                                         toString< int >( c ) );
     175                                          newCaseLabels.push_back( new ConstantExpr( cnst ) );
     176                                  }
     177
     178                                  return;
     179                          }
     180                          default:
     181                                // error: incompatible constants
     182                                break;
    138183                        }
    139 
     184                        break;
     185                  default:
     186                        break;
     187                } // switch
     188
     189                /* End: */{
     190                        // invalid range, signal a warning (it still generates the two case labels)
     191                        newCaseLabels.push_back( lo );
     192                        newCaseLabels.push_back( hi );
    140193                        return;
    141                     }
    142                     break;
    143                   default:
    144                     // error: incompatible constants
    145                     break;
    146194                }
    147             break;
    148           case BasicType::ShortSignedInt:
    149           case BasicType::ShortUnsignedInt:
    150           case BasicType::SignedInt:
    151           case BasicType::UnsignedInt:
    152           case BasicType::LongSignedInt:
    153           case BasicType::LongUnsignedInt:
    154           case BasicType::LongLongSignedInt:
    155           case BasicType::LongLongUnsignedInt:
    156             switch ( ty_hi->get_kind() ) {
    157               case BasicType::ShortSignedInt:
    158               case BasicType::ShortUnsignedInt:
    159               case BasicType::SignedInt:
    160               case BasicType::UnsignedInt:
    161               case BasicType::LongSignedInt:
    162               case BasicType::LongUnsignedInt:
    163               case BasicType::LongLongSignedInt:
    164               case BasicType::LongLongUnsignedInt: {
    165                   int i_lo = atoi(c_lo->get_value().c_str()),
    166                       i_hi = atoi(c_hi->get_value().c_str());
    167 
    168                   if ( i_lo > i_hi ) { int t=i_lo; i_lo=i_hi; i_hi=t; }
    169 
    170                   for ( int c = i_lo; c <=  i_hi; c++ ){
    171                       Type::Qualifiers q;
    172                       Constant cnst( new BasicType(q, ty_hi->get_kind()), // figure can't hurt (used to think in positives)
    173                                      toString< int >( c ) );
    174                       newCaseLabels.push_back( new ConstantExpr( cnst ) );
    175                   }
    176 
    177                   return;
    178               }
    179               default:
    180                 // error: incompatible constants
    181                 break;
    182             }
    183             break;
    184           default:
    185             break;
    186         } // switch
    187 
    188         /* End: */{
    189             // invalid range, signal a warning (it still generates the two case labels)
    190             newCaseLabels.push_back( lo );
    191             newCaseLabels.push_back( hi );
    192             return;
    193         }
    194     }
     195        }
    195196} // namespace ControlStruct
     197
    196198// Local Variables: //
    197199// tab-width: 4 //
Note: See TracChangeset for help on using the changeset viewer.