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, stuck-waitfor-destruct, with_gc
Children:
843054c2
Parents:
01aeade
Message:

licencing: sixth groups of files

Location:
translator/InitTweak
Files:
15 edited

Legend:

Unmodified
Added
Removed
  • translator/InitTweak/Association.cc

    r01aeade ra08ba92  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // XXX.cc --
     7// Association.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
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Tue May 19 16:25:09 2015
     13// Update Count     : 1
    1414//
     15
    1516#include "Association.h"
    1617
     
    2223const int RangeAssociation::RangeAssociation::UNDEF = -1;
    2324RangeAssociation::~RangeAssociation() {}
     25
    2426// Local Variables: //
    2527// tab-width: 4 //
  • translator/InitTweak/Association.h

    r01aeade ra08ba92  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // XXX.cc --
     7// Association.h --
    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 //
    15 #ifndef _ASSOCIATE_H_
    16 #define _ASSOCIATE_H_
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Tue May 19 16:27:09 2015
     13// Update Count     : 2
     14//
     15
     16#ifndef _ASSOCIATION_H_
     17#define _ASSOCIATION_H_
    1718
    1819#include <map>
     
    3435// ** exceptions
    3536class AssocException : public std::exception {
    36 public:
    37   AssocException() {}
    38   AssocException( std::string _what ) : what( _what ) {}
    39   ~AssocException() throw () {}
    40 
    41   std::string get_what() const { return what; }
    42   void set_what( std::string newValue ) { what = newValue; }
    43 private:
    44   std::string what;
     37  public:
     38        AssocException() {}
     39        AssocException( std::string _what ) : what( _what ) {}
     40        ~AssocException() throw () {}
     41
     42        std::string get_what() const { return what; }
     43        void set_what( std::string newValue ) { what = newValue; }
     44  private:
     45        std::string what;
    4546};
    4647
    4748// ** visitors
    4849class AssociationVisitor {
    49 public:
    50   AssociationVisitor() {}
    51   virtual ~AssociationVisitor() {}
    52 
    53   virtual void visit( SingleName * ) = 0;
    54   virtual void visit( PointAssociation * ) = 0;
    55   virtual void visit( RangeAssociation * ) = 0;
     50  public:
     51        AssociationVisitor() {}
     52        virtual ~AssociationVisitor() {}
     53
     54        virtual void visit( SingleName * ) = 0;
     55        virtual void visit( PointAssociation * ) = 0;
     56        virtual void visit( RangeAssociation * ) = 0;
    5657};
    5758
    5859// ** containers
    5960class Association {
    60  public:
    61   virtual ~Association();
    62 
    63   virtual Association *clone() = 0;
    64   virtual long int add_single( std::string, Expression *) = 0;
    65   virtual long int add_single( long int, Expression *expr) = 0;
    66 
    67   virtual Association *operator[]( int idx ) = 0;
    68   virtual Association *operator[]( std::string ) = 0;
    69 
    70   //  virtual AssociationIterator *get_iterator() = 0;
    71 
    72   virtual void accept( AssociationVisitor & ) = 0;
    73   virtual void display( std::ostream & ) = 0;
     61  public:
     62        virtual ~Association();
     63
     64        virtual Association *clone() = 0;
     65        virtual long int add_single( std::string, Expression *) = 0;
     66        virtual long int add_single( long int, Expression *expr) = 0;
     67
     68        virtual Association *operator[]( int idx ) = 0;
     69        virtual Association *operator[]( std::string ) = 0;
     70
     71        //  virtual AssociationIterator *get_iterator() = 0;
     72
     73        virtual void accept( AssociationVisitor & ) = 0;
     74        virtual void display( std::ostream & ) = 0;
    7475};
    7576
    7677class SingleName : public Association {
    77 public:
    78   SingleName( Expression *initExpr = 0 ) : expr( initExpr ) {}
    79   virtual ~SingleName();
    80 
    81   virtual SingleName *clone() {
    82     return 0; // XXX!
    83   }
    84 
    85   virtual long int add_single( long int idx, Expression *newExpr) {
    86     if ( expr == 0 ) //|| *expr == *newExpr )
    87       expr = newExpr;
    88     return 0;
    89   }
    90 
    91   virtual long int add_single( std::string str, Expression *newExpr) {
    92     if ( expr == 0 ) //|| *expr == *newExpr )
    93       expr = newExpr;
    94     return 0;
    95   }
    96 
    97   virtual Association *operator[]( int idx ) { assert(false); }
    98   virtual Association *operator[]( std::string idx ) { assert(false); }
    99 
    100   virtual void accept( AssociationVisitor &v ) { v.visit( this ); }
    101   virtual void display( std::ostream &os ) {
    102     os << "Single association" << std::endl;
    103   }
    104 
    105   Expression *get_expr() const { return expr; }
    106 
    107 private:
    108   Expression *expr;
    109   Expression *deflt;
     78  public:
     79        SingleName( Expression *initExpr = 0 ) : expr( initExpr ) {}
     80        virtual ~SingleName();
     81
     82        virtual SingleName *clone() {
     83                return 0; // XXX!
     84        }
     85
     86        virtual long int add_single( long int idx, Expression *newExpr) {
     87                if ( expr == 0 ) //|| *expr == *newExpr )
     88                        expr = newExpr;
     89                return 0;
     90        }
     91
     92        virtual long int add_single( std::string str, Expression *newExpr ) {
     93                if ( expr == 0 ) //|| *expr == *newExpr )
     94                        expr = newExpr;
     95                return 0;
     96        }
     97
     98        virtual Association *operator[]( int idx ) { assert(false); }
     99        virtual Association *operator[]( std::string idx ) { assert(false); }
     100
     101        virtual void accept( AssociationVisitor &v ) { v.visit( this ); }
     102        virtual void display( std::ostream &os ) {
     103                os << "Single association" << std::endl;
     104        }
     105
     106        Expression *get_expr() const { return expr; }
     107
     108  private:
     109        Expression *expr;
     110        Expression *deflt;
    110111};
    111112
    112113class PointAssociation : public Association {
    113 public:
    114   typedef std::map< std::string, std::pair< long int, Association *> > map_type;
    115 
    116   PointAssociation() {}
    117   PointAssociation( const PointAssociation &other ) {
    118     copy( other.anonym.begin(), other.anonym.end(), back_inserter( anonym ));
    119   }
    120 
    121   virtual ~PointAssociation();
    122 
    123   virtual PointAssociation *clone() {
    124     return ( new PointAssociation( *this ) );
    125   }
    126 
    127   virtual long int add_single( long int idx, Expression *expr) {
    128     long int ret;
    129 
    130     if ( idx >= (long int)ordering.size() ) throw AssocException("extra (spurious) members");
    131 
    132     if ( ordering[ idx ] == "")
    133       std::cerr << "Checkpoint 2" << std::endl;
    134     else {
    135       assert( table[ordering[idx]].second != 0 );
    136       ret = idx;
    137       table[ ordering[idx] ].second->add_single("", expr );
    138     }
    139     return ret;
    140   }
    141 
    142   virtual long int add_single( std::string idx, Expression *expr) {
    143     if ( idx == "" )
    144       std::cerr << "Checkpoint 1" << std::endl;
    145     else {
    146       map_type::iterator j;
    147       if (  (j = table.find( idx )) == table.end() )  // this doesn't amount for reachable members deeper down the structure, fix
    148         throw AssocException("No such member");
    149       else
    150         return add_single( j->second.first, expr );
    151     }
    152 
    153     return -1;
    154   }
    155 
    156   void add_member( std::string str ) {
    157     if ( table.find( str ) != table.end() ) return;
    158     ordering.push_back( str );
    159     if ( str != "" ) {
    160       std::pair<long int, Association *> p( ordering.size() - 1, 0 );
    161       table.insert( std::pair< std::string, std::pair<long int, Association *> >(str, p) );
    162     }
    163     return;
    164   }
    165 
    166   virtual void set_member( std::string str, Association *assoc ) {
    167     if ( str == "" )
    168       anonym.push_back( assoc );
    169     else  if ( table.find( str ) == table.end() )
    170       throw AssocException( "no such member" );
    171     else
    172       table[ str ] = std::pair<long int, Association *>(ordering.size() - 1, assoc);
    173 
    174     return;
    175   }
    176 
    177   virtual Association *operator[]( int idx ) {
    178     if ( ordering[idx] == "" ) {
    179       std::cerr << "Error, anonymous members not implemented yet" << std::endl;
    180       throw 0;
    181     } else
    182       return table[ ordering[idx] ].second;
    183   }
    184 
    185   virtual Association *operator[]( std::string idx ) {
    186     if ( table.find( idx ) == table.end() )
    187       throw AssocException("Member not found");
    188     else
    189       return table[ idx ].second;
    190   }
    191 
    192   /*
    193   virtual AssociationIterator *get_iterator() {
    194     PointAssocIterator *it;
    195     return it;
    196   }
    197   */
    198 
    199   void accept( AssociationVisitor &v ) { v.visit( this ); }
    200 
    201   virtual void display( std::ostream &os ) {
    202     os << "Point association: " << std::endl;
    203     for ( map_type::iterator i = table.begin(); i != table.end(); i++ ) {
    204       os << "Member [" << i->first << ", index = " << i->second.first << "]";
    205       if ( i->second.second != 0 )
    206         i->second.second->display( os );
    207       else
    208         std::cerr << "No recursive association" << std::endl;
    209 
    210       os << std::endl;
    211     }
    212   }
    213 
    214   const int size() const { return ordering.size(); }
    215 
    216 private:
    217   PointAssociation &operator=(const PointAssociation &);
    218   std::vector<std::string> ordering;
    219   std::list< Association * > anonym;
    220   std::map< std::string, std::pair<long int, Association *> > table;
     114  public:
     115        typedef std::map< std::string, std::pair< long int, Association *> > map_type;
     116
     117        PointAssociation() {}
     118        PointAssociation( const PointAssociation &other ) {
     119                copy( other.anonym.begin(), other.anonym.end(), back_inserter( anonym ));
     120        }
     121
     122        virtual ~PointAssociation();
     123
     124        virtual PointAssociation *clone() {
     125                return ( new PointAssociation( *this ) );
     126        }
     127
     128        virtual long int add_single( long int idx, Expression *expr) {
     129                long int ret;
     130
     131                if ( idx >= (long int)ordering.size() ) throw AssocException("extra (spurious) members");
     132
     133                if ( ordering[ idx ] == "")
     134                        std::cerr << "Checkpoint 2" << std::endl;
     135                else {
     136                        assert( table[ordering[idx]].second != 0 );
     137                        ret = idx;
     138                        table[ ordering[idx] ].second->add_single("", expr );
     139                } // if
     140                return ret;
     141        }
     142
     143        virtual long int add_single( std::string idx, Expression *expr) {
     144                if ( idx == "" )
     145                        std::cerr << "Checkpoint 1" << std::endl;
     146                else {
     147                        map_type::iterator j;
     148                        if (  (j = table.find( idx )) == table.end() )  // this doesn't amount for reachable members deeper down the structure, fix
     149                                throw AssocException("No such member");
     150                        else
     151                                return add_single( j->second.first, expr );
     152                } // if
     153
     154                return -1;
     155        }
     156
     157        void add_member( std::string str ) {
     158                if ( table.find( str ) != table.end() ) return;
     159                ordering.push_back( str );
     160                if ( str != "" ) {
     161                        std::pair<long int, Association *> p( ordering.size() - 1, 0 );
     162                        table.insert( std::pair< std::string, std::pair<long int, Association *> >(str, p) );
     163                } // if
     164                return;
     165        }
     166
     167        virtual void set_member( std::string str, Association *assoc ) {
     168                if ( str == "" )
     169                        anonym.push_back( assoc );
     170                else  if ( table.find( str ) == table.end() )
     171                        throw AssocException( "no such member" );
     172                else
     173                        table[ str ] = std::pair<long int, Association *>(ordering.size() - 1, assoc);
     174
     175                return;
     176        }
     177
     178        virtual Association *operator[]( int idx ) {
     179                if ( ordering[idx] == "" ) {
     180                        std::cerr << "Error, anonymous members not implemented yet" << std::endl;
     181                        throw 0;
     182                } else
     183                        return table[ ordering[idx] ].second;
     184        }
     185
     186        virtual Association *operator[]( std::string idx ) {
     187                if ( table.find( idx ) == table.end() )
     188                        throw AssocException("Member not found");
     189                else
     190                        return table[ idx ].second;
     191        }
     192
     193        /*
     194          virtual AssociationIterator *get_iterator() {
     195          PointAssocIterator *it;
     196          return it;
     197          }
     198        */
     199
     200        void accept( AssociationVisitor &v ) { v.visit( this ); }
     201
     202        virtual void display( std::ostream &os ) {
     203                os << "Point association: " << std::endl;
     204                for ( map_type::iterator i = table.begin(); i != table.end(); i++ ) {
     205                        os << "Member [" << i->first << ", index = " << i->second.first << "]";
     206                        if ( i->second.second != 0 )
     207                                i->second.second->display( os );
     208                        else
     209                                std::cerr << "No recursive association" << std::endl;
     210
     211                        os << std::endl;
     212                } // for
     213        }
     214
     215        const int size() const { return ordering.size(); }
     216
     217  private:
     218        PointAssociation &operator=(const PointAssociation &);
     219        std::vector<std::string> ordering;
     220        std::list< Association * > anonym;
     221        std::map< std::string, std::pair<long int, Association *> > table;
    221222};
    222223
    223224class RangeAssociation : public Association {
    224 public:
    225   static const int UNDEF;
    226   RangeAssociation( int _hi= UNDEF ) : hi( _hi ) {
    227     std::cerr << "Constructed RangeAssociation with: [" << hi << "]" << std::endl;
    228   }
    229 
    230   virtual ~RangeAssociation();
    231 
    232   virtual RangeAssociation *clone() {
    233     return 0; // XXX !!!!
    234   }
    235 
    236   virtual Association *operator[]( int idx ) {
    237     return 0; // XXX !!!
    238   }
    239 
    240   virtual Association *operator[]( std::string idx ) { assert(false); return 0; }
    241 
    242   /*
    243   virtual AssociationIterator *get_iterator() {
    244     RangeAssocIterator *it;
    245     return it;
    246   }
    247   */
    248 
    249   virtual long int add_single( long int idx, Expression *newExpr) { return 0; }
    250   virtual long int add_single( std::string, Expression *) { return 0; }
    251   void accept( AssociationVisitor &v ) { v.visit( this ); }
    252   virtual void display( std::ostream &os ) {
    253     os << "Range association, with limit: " << std::endl;
    254   }
    255 
    256 private:
    257   int hi;
    258   diet::diet_tree<int> tree;
    259   /*
    260   for ( diet_tree<int>::iterator i = tree.begin(); i != tree.end(); i++ )
    261     std::cout << "--(" << (*i).first << ", " << (*i).second << ")--" << std::endl;
    262   diet_tree<int> tree;
    263   tree.insert(100,200);
    264   */
     225  public:
     226        static const int UNDEF;
     227        RangeAssociation( int _hi= UNDEF ) : hi( _hi ) {
     228                std::cerr << "Constructed RangeAssociation with: [" << hi << "]" << std::endl;
     229        }
     230
     231        virtual ~RangeAssociation();
     232
     233        virtual RangeAssociation *clone() {
     234                return 0; // XXX !!!!
     235        }
     236
     237        virtual Association *operator[]( int idx ) {
     238                return 0; // XXX !!!
     239        }
     240
     241        virtual Association *operator[]( std::string idx ) { assert(false); return 0; }
     242
     243        /*
     244          virtual AssociationIterator *get_iterator() {
     245          RangeAssocIterator *it;
     246          return it;
     247          }
     248        */
     249
     250        virtual long int add_single( long int idx, Expression *newExpr) { return 0; }
     251        virtual long int add_single( std::string, Expression *) { return 0; }
     252        void accept( AssociationVisitor &v ) { v.visit( this ); }
     253        virtual void display( std::ostream &os ) {
     254                os << "Range association, with limit: " << std::endl;
     255        }
     256
     257  private:
     258        int hi;
     259        diet::diet_tree<int> tree;
     260        /*
     261          for ( diet_tree<int>::iterator i = tree.begin(); i != tree.end(); i++ )
     262          std::cout << "--(" << (*i).first << ", " << (*i).second << ")--" << std::endl;
     263          diet_tree<int> tree;
     264          tree.insert(100,200);
     265        */
    265266};
    266267
    267268// ** builders
    268269class AssociationBuilder {
    269 public:
    270   /* AssociationBuilder( Declaration * ) */
    271   virtual ~AssociationBuilder() {}
    272   virtual Association *get_assoc() = 0;
    273   virtual Association *grab_assoc() = 0;
    274   virtual void set_assoc(   Association * ) = 0;
     270  public:
     271        /* AssociationBuilder( Declaration * ) */
     272        virtual ~AssociationBuilder() {}
     273        virtual Association *get_assoc() = 0;
     274        virtual Association *grab_assoc() = 0;
     275        virtual void set_assoc(   Association * ) = 0;
    275276};
    276277
    277278class AssociationFiller {
    278 public:
    279   // AssociationFiller( Declaration * ) {}
    280   virtual ~AssociationFiller() {}
    281   virtual Association *get_assoc() = 0;
    282   virtual void set_assoc( Association * ) = 0;
    283 };
    284 
    285 #endif //#define _ASSOCIATE_H_
    286 
    287 /*
    288   Local Variables:
    289   mode: c++
    290   End:
    291 */
     279  public:
     280        // AssociationFiller( Declaration * ) {}
     281        virtual ~AssociationFiller() {}
     282        virtual Association *get_assoc() = 0;
     283        virtual void set_assoc( Association * ) = 0;
     284};
     285
     286#endif // _ASSOCIATION_H_
     287
    292288// Local Variables: //
    293289// tab-width: 4 //
  • translator/InitTweak/BasicInit.cc

    r01aeade ra08ba92  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // XXX.cc --
     7// BasicInit.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 16:30:43 2015
     13// Update Count     : 1
     14//
     15
    1516#include <list>
    1617#include <cassert>
     
    3233
    3334namespace InitTweak {
    34 
    35   CompoundStmt* BasicInit::mutate(CompoundStmt *compoundStmt)
    36   {
    37     index.visit( compoundStmt );
    38 
    39     std::list< Statement * > &kids = compoundStmt->get_kids();
    40     std::list< Statement * > newKids;
    41 
    42     for ( std::list< Statement * >::iterator i = kids.begin(); i!= kids.end(); i++ ) {
    43       //BasicInit newMut(  );
    44       (*i)->acceptMutator( *this );
    45       newKids.push_back( *i );
    46 
    47       if ( has_bindings() ) { //       if ( get_bindings() != 0  ) {
    48         std::list< Statement *> newSt = get_statements();
    49         //newSt.push_back( *i );
    50         newKids.splice( newKids.end(), newSt );
    51         bindings = 0;
    52         stmts.clear();
    53       }
    54     }
    55 
    56     compoundStmt->get_kids() = newKids;
    57     return compoundStmt;
    58   }
    59 
    60   Statement * BasicInit::mutate(DeclStmt *declStmt) {
    61 
    62     declStmt->accept( index );
    63 
    64     ObjectDecl *odecl = 0;
    65 
    66     if ( ( odecl = dynamic_cast<ObjectDecl *>(declStmt->get_decl()) ) != 0 ){
    67 
    68       Initializer *init = odecl->get_init();
    69       if ( init == 0 ) return declStmt;
    70 
    71       if ( Classify::type( odecl->get_type() ) == Classify::COMPOUND_T )
    72         if ( Classify::initializer(init) == Classify::SINGLE_I )
    73           throw( 0 ); // mismatch of type and initializer
    74         else {
    75           NameInCollection *col = Classify::declaration( odecl, &index );
    76           bindings = NameAssociation< Expression *, BreakInitializer >::createNameAssoc(col);
    77           bindings->add_value( std::string(""), BreakInitializer(init) );
    78           BasicInit::build_statements( bindings, odecl->get_name(), stmts );
    79         }
    80       else
    81         if ( Classify::initializer(init) == Classify::COMPOUND_I )
    82           throw( 0 ); // mismatch of type and initializer
    83         else {
    84           // Single inits
    85           SingleInit *sinit = dynamic_cast< SingleInit * > ( init );
    86           assert( sinit != 0);
    87 
    88           std::list<Expression *> args;
    89           args.push_back( new AddressExpr( new NameExpr( odecl->get_name() )) );    // have to get address of object
    90           args.push_back( sinit->get_value() );
    91           // replace declaration with initialization
    92           stmts.push_back(new ExprStmt(std::list<Label>(), new UntypedExpr(new NameExpr("?=?"), args)));
    93         }
    94 
    95       delete init;
    96       odecl->set_init( 0 );
    97     } else {
    98       // no declaration statement
    99     }
    100 
    101     return declStmt;
    102   }
    103 
    104   ExprStmt *assignFromDecl( DeclStmt *declStmt )
    105   {
    106     ObjectDecl *decl;
    107     if ( (decl = dynamic_cast<ObjectDecl *>( declStmt->get_decl() )) != 0 )
    108       {
    109         SingleInit *init;
    110         if ( (init = dynamic_cast<SingleInit *>(decl->get_init())) != 0 )
    111           {
    112           }
    113       }
    114 
    115     return 0;
    116   }
    117 
    118   bool isDeclStmtP(Statement *stmt)
    119   {
    120     return ( dynamic_cast< DeclStmt *>(stmt) != 0 );
    121   }
    122 
    123   BasicInit::Classify::TypeKind BasicInit::Classify::type( Type *toClassify ) {
    124     if ( toClassify == 0 ) return NULL_T;
    125 
    126     if ( dynamic_cast< StructInstType * >(toClassify) ||
    127          dynamic_cast< UnionInstType * >(toClassify) ||
    128          dynamic_cast< ArrayType * >(toClassify)         )
    129       return COMPOUND_T;
    130     else
    131       return SINGLE_T;
    132   }
    133 
    134   BasicInit::Classify::InitKind BasicInit::Classify::initializer( Initializer *init) {
    135     if ( init == 0 ) return NULL_I;
    136     if ( dynamic_cast< ListInit * >(init) )
    137       return COMPOUND_I;
    138     if ( dynamic_cast< SingleInit * >(init) )
    139       return SINGLE_I;
    140 
    141     return NULL_I; // shouldn't be here anyways
    142   }
    143 
    144   NameInCollection *
    145   BasicInit::Classify::declaration( ObjectDecl *objdecl, SymTab::Indexer *index ) {
    146     assert ( index != 0 );
    147 
    148     ReferenceToType *reftype;
    149     if ( (reftype = dynamic_cast< StructInstType * >( objdecl->get_type() )) != 0 ){
    150       StructDecl *strDecl = index->lookupStruct( reftype->get_name() );
    151       if ( strDecl != 0 ) {
    152         NameCollectionBuilder bld;
    153         NameCollector nc( bld );
    154         strDecl->accept( nc );
    155         NameInCollection *col = nc.get_builder().get_collection();
    156         nc.get_builder().set_collection( 0 );
    157 
    158         return col;
    159       } else
    160         throw( SemanticError( std::string("No structure of name: ") + reftype->get_name() ) );
    161     } else {
    162       throw(SemanticError( reftype->get_name() + std::string("is not a reference to type") ));
    163       return 0;
    164     }
    165   }
    166 
    167   std::list< Statement * >
    168   BasicInit::Classify::matchInit( NameInCollection *col,
    169                                   ObjectDecl *toInitialize,
    170                                   Initializer *init ) {
    171     assert ( col != 0 );
    172 
    173     std::list< Statement * > arranged(0); //( col->size() );
    174     std::fill( arranged.begin(), arranged.end(), (Statement *)0 );
    175     int current = 0;
    176 
    177     if ( init == 0 )
    178       // no initializer... shouldn't even bother... fix this.
    179       return arranged;
    180 
    181     {
    182       ListInit *li = dynamic_cast< ListInit * >( init );
    183 
    184       if ( li != 0 ) {
    185         for ( std::list<Initializer *>::iterator i = li->begin_initializers();
    186                                                          i != li->end_initializers();
    187               i++) {
    188           std::list<Expression *> d_orig = (*i)->get_designators();
    189 
    190           NameInCollection *corrsp; // corresponding element to this initializer
    191           if ( ! d_orig.empty() ) {
    192             // 1) has designators
    193             std::list<NameExpr *> des;
    194             std::transform( d_orig.begin(), d_orig.end(),
    195                             std::back_inserter( des ), cast_ptr<Expression, NameExpr > );
    196 
    197             for ( std::list<NameExpr *>::iterator j = des.begin(); j != des.end(); j++ ) {
    198               // check for existence of the element
    199 
    200               if ( (corrsp = (*col)[ (*j)->get_name() ]) != 0 ) {
    201                 // current++;
    202                 SingleInit *sinit;
    203                 if ( (sinit = dynamic_cast< SingleInit * >( *i )) != 0 )
    204                   arranged.push_back( constructAssgn( corrsp->get_name(), toInitialize, sinit ) );
     35        CompoundStmt* BasicInit::mutate(CompoundStmt *compoundStmt) {
     36                index.visit( compoundStmt );
     37
     38                std::list< Statement * > &kids = compoundStmt->get_kids();
     39                std::list< Statement * > newKids;
     40
     41                for ( std::list< Statement * >::iterator i = kids.begin(); i!= kids.end(); i++ ) {
     42                        //BasicInit newMut(  );
     43                        (*i)->acceptMutator( *this );
     44                        newKids.push_back( *i );
     45
     46                        if ( has_bindings() ) { //       if ( get_bindings() != 0  ) {
     47                                std::list< Statement *> newSt = get_statements();
     48                                //newSt.push_back( *i );
     49                                newKids.splice( newKids.end(), newSt );
     50                                bindings = 0;
     51                                stmts.clear();
     52                        } // if
     53                } // for
     54
     55                compoundStmt->get_kids() = newKids;
     56                return compoundStmt;
     57        }
     58
     59        Statement * BasicInit::mutate(DeclStmt *declStmt) {
     60                declStmt->accept( index );
     61
     62                ObjectDecl *odecl = 0;
     63
     64                if ( ( odecl = dynamic_cast<ObjectDecl *>(declStmt->get_decl()) ) != 0 ) {
     65                        Initializer *init = odecl->get_init();
     66                        if ( init == 0 ) return declStmt;
     67
     68                        if ( Classify::type( odecl->get_type() ) == Classify::COMPOUND_T )
     69                                if ( Classify::initializer(init) == Classify::SINGLE_I )
     70                                        throw( 0 ); // mismatch of type and initializer
     71                                else {
     72                                        NameInCollection *col = Classify::declaration( odecl, &index );
     73                                        bindings = NameAssociation< Expression *, BreakInitializer >::createNameAssoc(col);
     74                                        bindings->add_value( std::string(""), BreakInitializer(init) );
     75                                        BasicInit::build_statements( bindings, odecl->get_name(), stmts );
     76                                } // if
     77                        else
     78                                if ( Classify::initializer(init) == Classify::COMPOUND_I )
     79                                        throw( 0 ); // mismatch of type and initializer
     80                                else {
     81                                        // Single inits
     82                                        SingleInit *sinit = dynamic_cast< SingleInit * > ( init );
     83                                        assert( sinit != 0);
     84
     85                                        std::list<Expression *> args;
     86                                        args.push_back( new AddressExpr( new NameExpr( odecl->get_name() )) );    // have to get address of object
     87                                        args.push_back( sinit->get_value() );
     88                                        // replace declaration with initialization
     89                                        stmts.push_back(new ExprStmt(std::list<Label>(), new UntypedExpr(new NameExpr("?=?"), args)));
     90                                } // if
     91
     92                        delete init;
     93                        odecl->set_init( 0 );
     94                } else {
     95                        // no declaration statement
     96                } // if
     97
     98                return declStmt;
     99        }
     100
     101        ExprStmt *assignFromDecl( DeclStmt *declStmt ) {
     102                ObjectDecl *decl;
     103                if ( (decl = dynamic_cast<ObjectDecl *>( declStmt->get_decl() )) != 0 ) {
     104                        SingleInit *init;
     105                        if ( (init = dynamic_cast<SingleInit *>(decl->get_init())) != 0 ) {
     106                        } // if
     107                } // if
     108
     109                return 0;
     110        }
     111
     112        bool isDeclStmtP(Statement *stmt) {
     113                return ( dynamic_cast< DeclStmt *>(stmt) != 0 );
     114        }
     115
     116        BasicInit::Classify::TypeKind BasicInit::Classify::type( Type *toClassify ) {
     117                if ( toClassify == 0 ) return NULL_T;
     118
     119                if ( dynamic_cast< StructInstType * >(toClassify) ||
     120                         dynamic_cast< UnionInstType * >(toClassify) ||
     121                         dynamic_cast< ArrayType * >(toClassify)         )
     122                        return COMPOUND_T;
    205123                else
    206                   ; // recursive call to matchInit
    207               } else
    208                 // error, member doesn't exist
    209                 return arranged; // throw( 0 ); // XXX
    210             }
    211           } else {
    212             // 2) doesn't have designators
    213             if ( (corrsp = (*col)[ current++ ]) != 0 ) {
    214               SingleInit *sinit;
    215               if ( (sinit = dynamic_cast< SingleInit * >( *i )) != 0 )
    216                 arranged.push_back( constructAssgn( corrsp->get_name(), toInitialize, sinit ) );
    217               else
    218                 ; // recursive call to matchInit
    219             } else {
    220               // Shouldn't be here... probably too many elements in initializer?
    221             }
    222           }
    223         }
    224       }
    225     }
    226 
    227     return arranged;
    228   }
    229 
    230   Statement *BasicInit::Classify::constructAssgn( std::string membName, ObjectDecl *toInit, SingleInit *sinit ) {
    231     std::list< Expression * > args;
    232     args.push_back(new AddressExpr( new UntypedMemberExpr( membName, new NameExpr(toInit->get_name()) )));
    233     args.push_back( sinit->get_value() );
    234     Statement *ret = new ExprStmt(std::list<Label>(), new UntypedExpr(new NameExpr("?=?"), args));
    235     return ret;
    236   }
    237 
    238   void BasicInit::build_statements( NameAssociation< Expression *, BreakInitializer > *assoc,
    239                                     std::string aggName, std::list< Statement *> &stmts ) {
    240     assert( assoc != 0 );
    241     static std::list< std::string > prefix;
    242 
    243     NameInCollection *col = assoc->get_collection();
    244     if ( col->isComposite() ) {
    245       VariousNames *vc = dynamic_cast< VariousNames * >( col );
    246       for ( VariousNames::key_iterator it = vc->keys_begin(); it != vc->keys_end(); it++ ) {
    247         prefix.push_back( *it );
    248         if ( (*assoc)[ *it ] != 0 )
    249           build_statements( (*assoc)[ *it ], aggName, stmts );
    250 
    251         prefix.pop_back();
    252       }
    253     } else {
    254       SingleNameAssoc< Expression *, BreakInitializer > *sa = \
    255         dynamic_cast< SingleNameAssoc< Expression *, BreakInitializer > * >( assoc );
    256       assert( sa != 0 );
    257 
    258       Expression * rh = sa->get_data();
    259 
    260       if (rh != 0) {
    261         // construct assignment statement list
    262         Expression *lh = new NameExpr ( aggName );
    263         for ( std::list< std::string >::iterator i = prefix.begin(); i != prefix.end(); i++ )
    264           lh = new UntypedMemberExpr( *i, lh );
    265 
    266         std::list< Expression * > args;
    267         args.push_back( new AddressExpr(lh) );  args.push_back( rh );
    268 
    269         stmts.push_back( new ExprStmt(std::list<Label>(), new UntypedExpr(new NameExpr("?=?"), args)) );
    270       }
    271     }
    272 
    273     return;
    274   }
    275 
     124                        return SINGLE_T;
     125        }
     126
     127        BasicInit::Classify::InitKind BasicInit::Classify::initializer( Initializer *init) {
     128                if ( init == 0 ) return NULL_I;
     129                if ( dynamic_cast< ListInit * >(init) )
     130                        return COMPOUND_I;
     131                if ( dynamic_cast< SingleInit * >(init) )
     132                        return SINGLE_I;
     133
     134                return NULL_I; // shouldn't be here anyways
     135        }
     136
     137        NameInCollection * BasicInit::Classify::declaration( ObjectDecl *objdecl, SymTab::Indexer *index ) {
     138                assert ( index != 0 );
     139
     140                ReferenceToType *reftype;
     141                if ( (reftype = dynamic_cast< StructInstType * >( objdecl->get_type() )) != 0 ) {
     142                        StructDecl *strDecl = index->lookupStruct( reftype->get_name() );
     143                        if ( strDecl != 0 ) {
     144                                NameCollectionBuilder bld;
     145                                NameCollector nc( bld );
     146                                strDecl->accept( nc );
     147                                NameInCollection *col = nc.get_builder().get_collection();
     148                                nc.get_builder().set_collection( 0 );
     149
     150                                return col;
     151                        } else
     152                                throw( SemanticError( std::string("No structure of name: ") + reftype->get_name() ) );
     153                } else {
     154                        throw(SemanticError( reftype->get_name() + std::string("is not a reference to type") ));
     155                        return 0;
     156                } // if
     157        }
     158
     159        std::list< Statement * >
     160        BasicInit::Classify::matchInit( NameInCollection *col, ObjectDecl *toInitialize, Initializer *init ) {
     161                assert ( col != 0 );
     162
     163                std::list< Statement * > arranged(0); //( col->size() );
     164                std::fill( arranged.begin(), arranged.end(), (Statement *)0 );
     165                int current = 0;
     166
     167                if ( init == 0 )
     168                        // no initializer... shouldn't even bother... fix this.
     169                        return arranged;
     170
     171                ListInit *li = dynamic_cast< ListInit * >( init );
     172
     173                if ( li != 0 ) {
     174                        for ( std::list<Initializer *>::iterator i = li->begin_initializers();
     175                                  i != li->end_initializers();
     176                                  i++) {
     177                                std::list<Expression *> d_orig = (*i)->get_designators();
     178
     179                                NameInCollection *corrsp; // corresponding element to this initializer
     180                                if ( ! d_orig.empty() ) {
     181                                        // 1) has designators
     182                                        std::list<NameExpr *> des;
     183                                        std::transform( d_orig.begin(), d_orig.end(),
     184                                                                        std::back_inserter( des ), cast_ptr<Expression, NameExpr > );
     185
     186                                        for ( std::list<NameExpr *>::iterator j = des.begin(); j != des.end(); j++ ) {
     187                                                // check for existence of the element
     188
     189                                                if ( (corrsp = (*col)[ (*j)->get_name() ]) != 0 ) {
     190                                                        // current++;
     191                                                        SingleInit *sinit;
     192                                                        if ( (sinit = dynamic_cast< SingleInit * >( *i )) != 0 )
     193                                                                arranged.push_back( constructAssgn( corrsp->get_name(), toInitialize, sinit ) );
     194                                                        else
     195                                                                ; // recursive call to matchInit
     196                                                } else
     197                                                        // error, member doesn't exist
     198                                                        return arranged; // throw( 0 ); // XXX
     199                                        }
     200                                } else {
     201                                        // 2) doesn't have designators
     202                                        if ( (corrsp = (*col)[ current++ ]) != 0 ) {
     203                                                SingleInit *sinit;
     204                                                if ( (sinit = dynamic_cast< SingleInit * >( *i )) != 0 )
     205                                                        arranged.push_back( constructAssgn( corrsp->get_name(), toInitialize, sinit ) );
     206                                                else
     207                                                        ; // recursive call to matchInit
     208                                        } else {
     209                                                // Shouldn't be here... probably too many elements in initializer?
     210                                        } // if
     211                                } // if
     212                        } // for
     213                } // if
     214
     215                return arranged;
     216        }
     217
     218        Statement *BasicInit::Classify::constructAssgn( std::string membName, ObjectDecl *toInit, SingleInit *sinit ) {
     219                std::list< Expression * > args;
     220                args.push_back(new AddressExpr( new UntypedMemberExpr( membName, new NameExpr(toInit->get_name()) )));
     221                args.push_back( sinit->get_value() );
     222                Statement *ret = new ExprStmt(std::list<Label>(), new UntypedExpr(new NameExpr("?=?"), args));
     223                return ret;
     224        }
     225
     226        void BasicInit::build_statements( NameAssociation< Expression *, BreakInitializer > *assoc,  std::string aggName, std::list< Statement *> &stmts ) {
     227                assert( assoc != 0 );
     228                static std::list< std::string > prefix;
     229
     230                NameInCollection *col = assoc->get_collection();
     231                if ( col->isComposite() ) {
     232                        VariousNames *vc = dynamic_cast< VariousNames * >( col );
     233                        for ( VariousNames::key_iterator it = vc->keys_begin(); it != vc->keys_end(); it++ ) {
     234                                prefix.push_back( *it );
     235                                if ( (*assoc)[ *it ] != 0 )
     236                                        build_statements( (*assoc)[ *it ], aggName, stmts );
     237
     238                                prefix.pop_back();
     239                        }
     240                } else {
     241                        SingleNameAssoc< Expression *, BreakInitializer > *sa = \
     242                                dynamic_cast< SingleNameAssoc< Expression *, BreakInitializer > * >( assoc );
     243                        assert( sa != 0 );
     244
     245                        Expression * rh = sa->get_data();
     246
     247                        if (rh != 0) {
     248                                // construct assignment statement list
     249                                Expression *lh = new NameExpr ( aggName );
     250                                for ( std::list< std::string >::iterator i = prefix.begin(); i != prefix.end(); i++ )
     251                                        lh = new UntypedMemberExpr( *i, lh );
     252
     253                                std::list< Expression * > args;
     254                                args.push_back( new AddressExpr(lh) );  args.push_back( rh );
     255
     256                                stmts.push_back( new ExprStmt(std::list<Label>(), new UntypedExpr(new NameExpr("?=?"), args)) );
     257                        } // if
     258                } // if
     259
     260                return;
     261        }
    276262} // namespace InitTweak
    277263
  • translator/InitTweak/BasicInit.h

    r01aeade ra08ba92  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // XXX.cc --
     7// BasicInit.h --
    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 16:32:21 2015
     13// Update Count     : 3
     14//
     15
    1516#ifndef _BASINIT_H_
    1617#define _BASINIT_H_
     
    2829
    2930namespace InitTweak {
    30 
    31   bool isDeclStmtP(Statement *stmt);
    32 
    33   class BreakInitializer;
    34   class BreakDesignator;
    35 
    36   class BasicInit: public Mutator
    37     {
    38     public:
    39       BasicInit() : bindings( 0 ) {}
    40       BasicInit( SymTab::Indexer &_index ) : bindings( 0 ), index( _index ) {}
    41       BasicInit( const BasicInit &other ) {
    42         bindings = other.get_bindings();
    43         index = other.index;
    44       }
    45 
    46       ~BasicInit() { /* delete bindings; bindings = 0; */ }
    47 
    48       NameAssociation< Expression *, BreakInitializer > *get_bindings() const { return bindings; }
    49       void set_bindings( NameAssociation< Expression *, BreakInitializer > *newValue ) {
    50         bindings = newValue;
    51       }
    52 
    53       bool has_bindings() {
    54         return ( get_bindings() != 0 || ! stmts.empty() );
    55       }
    56 
    57       virtual ObjectDecl     *mutate( ObjectDecl *objectDecl )
    58       { index.visit( objectDecl ); return objectDecl; }
    59       virtual TypeDecl       *mutate( TypeDecl *typeDecl )
    60       { index.visit( typeDecl ); return typeDecl; }
    61       virtual TypedefDecl    *mutate( TypedefDecl *typeDecl )
    62       { index.visit( typeDecl ); return typeDecl; }
    63       virtual StructDecl     *mutate( StructDecl *aggregateDecl )
    64       { index.visit( aggregateDecl ); return aggregateDecl; }
    65       virtual UnionDecl      *mutate( UnionDecl *aggregateDecl )
    66       { index.visit( aggregateDecl ); return aggregateDecl; }
    67       virtual EnumDecl       *mutate( EnumDecl *aggregateDecl )
    68       { index.visit( aggregateDecl ); return aggregateDecl; }
    69 
    70       virtual Type           *mutate( StructInstType *aggrInst )
    71       { index.visit( aggrInst ); return aggrInst; }
    72       virtual Type           *mutate( UnionInstType *aggrInst )
    73       { index.visit( aggrInst ); return aggrInst; }
    74 
    75       virtual CompoundStmt   *mutate(CompoundStmt *compoundStmt);
    76       virtual Statement *mutate(DeclStmt *declStmt);
    77 
    78       std::list< Statement *> get_statements() const { return stmts;  }
    79 
    80       static void build_statements( NameAssociation< Expression *, BreakInitializer > *assoc,
    81                                     std::string aggName, std::list< Statement *> &stmts );
    82     private:
    83       NameAssociation< Expression *, BreakInitializer > *bindings;
    84       Statement *assignFromDecl( DeclStmt *declStmt );
    85       SymTab::Indexer index;
    86       std::list< Statement *> stmts;
    87 
    88       class Classify {
    89       public:
    90         enum TypeKind { NULL_T, SINGLE_T, COMPOUND_T };
    91         enum InitKind { NULL_I, SINGLE_I, COMPOUND_I };
    92 
    93         static TypeKind type( Type * );
    94         static InitKind initializer( Initializer *);
    95 
    96         static NameInCollection *declaration( ObjectDecl *objdecl, SymTab::Indexer *index );
    97         static std::list< Statement * >
    98            matchInit( NameInCollection *, ObjectDecl *, Initializer * );
    99         static Statement *constructAssgn( std::string membname, ObjectDecl *toInit, SingleInit *sinit );
    100 
    101         // static std::list< Statement * > constructListAssgn( NameAssociation<Expression *, BreakDesignator > assoc );
    102       };
    103   };
    104 
    105   class BreakInitializer {
    106     enum InitKind { EMPTY, SINGLE, COMPOUND };
    107 
    108     class BreakDesignator;
    109     typedef BreakDesignator NameSplitter;
    110 
    111   public:
    112     typedef std::list<Initializer *>::iterator element_iterator;
    113     typedef std::list< NameSplitter >::iterator name_iterator;
    114 
    115     BreakInitializer ( Initializer *_init ) : kind( EMPTY ), sinit(0), cinit(0) {
    116       std::list<Expression *> temp;
    117 
    118       if ( ( sinit=dynamic_cast< SingleInit * >(_init) ) != 0 ) {
    119         kind = SINGLE;
    120         temp = sinit->get_designators();
    121       } else if ( ( cinit=dynamic_cast< ListInit * >(_init) ) != 0 ) {
    122         kind = COMPOUND;
    123         temp = cinit->get_designators();
    124       }
    125 
    126       std::transform( temp.begin(), temp.end(), std::back_inserter( designators ),
    127                       ctor_noptr<NameSplitter, Expression *> );
    128 
    129     }
    130 
    131     //BreakInitializer( const BreakInitializer &other ) { this.col = other.col; }
    132     ~BreakInitializer () {}
    133 
    134     BreakInitializer set_name( NameSplitter &name ) {
    135       designators.clear();
    136       designators.push_back( name );
    137 
    138       return *this;
    139     }
    140 
    141     element_iterator element_begin() {
    142       assert( cinit != 0 );
    143       return cinit->begin_initializers();
    144     }
    145     element_iterator element_end() {
    146       assert( cinit != 0 );
    147       return cinit->end_initializers();
    148     }
    149 
    150     name_iterator names_begin() { return designators.begin(); }
    151     name_iterator names_end() { return designators.end(); }
    152 
    153     int names_size() const { return designators.size(); }
    154 
    155     bool has_index() const { return ! designators.empty(); }
    156     bool is_single() const { return kind == SINGLE; }
    157     bool is_composite() const { return kind == COMPOUND;  }
    158 
    159     Expression *get_value() {
    160       switch ( kind ) {
    161       case EMPTY:
    162         return 0;
    163         break;
    164       case SINGLE:
    165         return sinit->get_value();
    166         break;
    167       case COMPOUND:
    168         assert(false);
    169         break;
    170       default:
    171         assert(false);
    172       }
    173       return 0;
    174     }
    175 
    176     // attributes
    177   private:
    178     InitKind kind;
    179     SingleInit *sinit;
    180     ListInit *cinit;
    181     std::list< BreakDesignator > designators;
    182 
    183     // helper classes
    184   public:
    185     class BreakDesignator {
    186     public:
    187       BreakDesignator( Expression *exp ) {
    188         Expression *prfx = exp;
    189         UntypedMemberExpr *me = 0;
    190 
    191         do {
    192           if ( (me=dynamic_cast< UntypedMemberExpr * >( prfx )) == 0 ) break;
    193           blown_struct.push_front( me->get_member() );
    194           prfx = me->get_aggregate();
    195         } while ( prfx != 0 );
    196 
    197         NameExpr *ne;
    198         if ( (ne=dynamic_cast< NameExpr * >( prfx )) != 0 )
    199           blown_struct.push_front( ne->get_name() );
    200       }
    201 
    202       BreakDesignator( std::string name ) {
    203         blown_struct.push_front( name );
    204       }
    205 
    206       bool is_flat() const { return blown_struct.size() == 1; }
    207       bool is_nested() const { return blown_struct.size() > 1; }
    208 
    209       std::string get_name() { return blown_struct.front(); }
    210 
    211       BreakDesignator &name_remainder() {
    212         blown_struct.pop_front();
    213         return *this;
    214       }
    215 
    216     private:
    217       std::list< std::string > blown_struct;
    218     };
    219 
    220   };
    221 
     31        bool isDeclStmtP(Statement *stmt);
     32
     33        class BreakInitializer;
     34        class BreakDesignator;
     35
     36        class BasicInit: public Mutator {
     37          public:
     38                BasicInit() : bindings( 0 ) {}
     39                BasicInit( SymTab::Indexer &_index ) : bindings( 0 ), index( _index ) {}
     40                BasicInit( const BasicInit &other ) {
     41                        bindings = other.get_bindings();
     42                        index = other.index;
     43                }
     44
     45                ~BasicInit() { /* delete bindings; bindings = 0; */ }
     46
     47                NameAssociation< Expression *, BreakInitializer > *get_bindings() const { return bindings; }
     48                void set_bindings( NameAssociation< Expression *, BreakInitializer > *newValue ) {
     49                        bindings = newValue;
     50                }
     51
     52                bool has_bindings() {
     53                        return ( get_bindings() != 0 || ! stmts.empty() );
     54                }
     55
     56                virtual ObjectDecl     *mutate( ObjectDecl *objectDecl )
     57                        { index.visit( objectDecl ); return objectDecl; }
     58                virtual TypeDecl       *mutate( TypeDecl *typeDecl )
     59                        { index.visit( typeDecl ); return typeDecl; }
     60                virtual TypedefDecl    *mutate( TypedefDecl *typeDecl )
     61                        { index.visit( typeDecl ); return typeDecl; }
     62                virtual StructDecl     *mutate( StructDecl *aggregateDecl )
     63                        { index.visit( aggregateDecl ); return aggregateDecl; }
     64                virtual UnionDecl      *mutate( UnionDecl *aggregateDecl )
     65                        { index.visit( aggregateDecl ); return aggregateDecl; }
     66                virtual EnumDecl       *mutate( EnumDecl *aggregateDecl )
     67                        { index.visit( aggregateDecl ); return aggregateDecl; }
     68
     69                virtual Type           *mutate( StructInstType *aggrInst )
     70                        { index.visit( aggrInst ); return aggrInst; }
     71                virtual Type           *mutate( UnionInstType *aggrInst )
     72                        { index.visit( aggrInst ); return aggrInst; }
     73
     74                virtual CompoundStmt   *mutate(CompoundStmt *compoundStmt);
     75                virtual Statement *mutate(DeclStmt *declStmt);
     76
     77                std::list< Statement *> get_statements() const { return stmts;  }
     78
     79                static void build_statements( NameAssociation< Expression *, BreakInitializer > *assoc, std::string aggName, std::list< Statement *> &stmts );
     80          private:
     81                NameAssociation< Expression *, BreakInitializer > *bindings;
     82                Statement *assignFromDecl( DeclStmt *declStmt );
     83                SymTab::Indexer index;
     84                std::list< Statement *> stmts;
     85
     86                class Classify {
     87                  public:
     88                        enum TypeKind { NULL_T, SINGLE_T, COMPOUND_T };
     89                        enum InitKind { NULL_I, SINGLE_I, COMPOUND_I };
     90
     91                        static TypeKind type( Type * );
     92                        static InitKind initializer( Initializer *);
     93
     94                        static NameInCollection *declaration( ObjectDecl *objdecl, SymTab::Indexer *index );
     95                        static std::list< Statement * >
     96                        matchInit( NameInCollection *, ObjectDecl *, Initializer * );
     97                        static Statement *constructAssgn( std::string membname, ObjectDecl *toInit, SingleInit *sinit );
     98
     99                        // static std::list< Statement * > constructListAssgn( NameAssociation<Expression *, BreakDesignator > assoc );
     100                };
     101        };
     102
     103        class BreakInitializer {
     104                enum InitKind { EMPTY, SINGLE, COMPOUND };
     105
     106                class BreakDesignator;
     107                typedef BreakDesignator NameSplitter;
     108
     109          public:
     110                typedef std::list<Initializer *>::iterator element_iterator;
     111                typedef std::list< NameSplitter >::iterator name_iterator;
     112
     113                BreakInitializer ( Initializer *_init ) : kind( EMPTY ), sinit(0), cinit(0) {
     114                        std::list<Expression *> temp;
     115
     116                        if ( ( sinit=dynamic_cast< SingleInit * >(_init) ) != 0 ) {
     117                                kind = SINGLE;
     118                                temp = sinit->get_designators();
     119                        } else if ( ( cinit=dynamic_cast< ListInit * >(_init) ) != 0 ) {
     120                                kind = COMPOUND;
     121                                temp = cinit->get_designators();
     122                        } // if
     123
     124                        std::transform( temp.begin(), temp.end(), std::back_inserter( designators ), ctor_noptr<NameSplitter, Expression *> );
     125                }
     126
     127                //BreakInitializer( const BreakInitializer &other ) { this.col = other.col; }
     128                ~BreakInitializer () {}
     129
     130                BreakInitializer set_name( NameSplitter &name ) {
     131                        designators.clear();
     132                        designators.push_back( name );
     133
     134                        return *this;
     135                }
     136
     137                element_iterator element_begin() {
     138                        assert( cinit != 0 );
     139                        return cinit->begin_initializers();
     140                }
     141                element_iterator element_end() {
     142                        assert( cinit != 0 );
     143                        return cinit->end_initializers();
     144                }
     145
     146                name_iterator names_begin() { return designators.begin(); }
     147                name_iterator names_end() { return designators.end(); }
     148
     149                int names_size() const { return designators.size(); }
     150
     151                bool has_index() const { return ! designators.empty(); }
     152                bool is_single() const { return kind == SINGLE; }
     153                bool is_composite() const { return kind == COMPOUND;  }
     154
     155                Expression *get_value() {
     156                        switch ( kind ) {
     157                          case EMPTY:
     158                                return 0;
     159                                break;
     160                          case SINGLE:
     161                                return sinit->get_value();
     162                                break;
     163                          case COMPOUND:
     164                                assert(false);
     165                                break;
     166                          default:
     167                                assert(false);
     168                        } // switch
     169                        return 0;
     170                }
     171                // attributes
     172          private:
     173                InitKind kind;
     174                SingleInit *sinit;
     175                ListInit *cinit;
     176                std::list< BreakDesignator > designators;
     177                // helper classes
     178          public:
     179                class BreakDesignator {
     180                  public:
     181                        BreakDesignator( Expression *exp ) {
     182                                Expression *prfx = exp;
     183                                UntypedMemberExpr *me = 0;
     184
     185                                do {
     186                                        if ( (me=dynamic_cast< UntypedMemberExpr * >( prfx )) == 0 ) break;
     187                                        blown_struct.push_front( me->get_member() );
     188                                        prfx = me->get_aggregate();
     189                                } while ( prfx != 0 );
     190
     191                                NameExpr *ne;
     192                                if ( (ne=dynamic_cast< NameExpr * >( prfx )) != 0 )
     193                                        blown_struct.push_front( ne->get_name() );
     194                        }
     195
     196                        BreakDesignator( std::string name ) {
     197                                blown_struct.push_front( name );
     198                        }
     199
     200                        bool is_flat() const { return blown_struct.size() == 1; }
     201                        bool is_nested() const { return blown_struct.size() > 1; }
     202
     203                        std::string get_name() { return blown_struct.front(); }
     204
     205                        BreakDesignator &name_remainder() {
     206                                blown_struct.pop_front();
     207                                return *this;
     208                        }
     209
     210                  private:
     211                        std::list< std::string > blown_struct;
     212                };
     213        };
    222214} // namespace InitTweak
    223215
    224 #endif // #ifndef _BASINIT_H_
    225 
    226 /*
    227   Local Variables:
    228   mode: c++
    229   End:
    230 */
     216#endif // _BASINIT_H_
     217
    231218// Local Variables: //
    232219// tab-width: 4 //
  • translator/InitTweak/DeclarationHoister.cc

    r01aeade ra08ba92  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // XXX.cc --
     7// DeclarationHoister.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
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Tue May 19 16:33:20 2015
     13// Update Count     : 2
    1414//
     15
    1516#include <list>
    1617#include <cassert>
     
    2728#include "DeclarationHoister.h"
    2829
     30namespace InitTweak {
     31        CompoundStmt* DeclarationHoister::mutate( CompoundStmt *compoundStmt ) {
     32                typedef std::list<Statement *>::iterator stmt_it;
     33                // 1. collect Declaration Statements  in this scope
     34                std::list<Statement *> &kids = compoundStmt->get_kids();
     35                std::list<Statement *>::iterator result = kids.begin();
     36                std::list< stmt_it > decls;
    2937
    30 namespace InitTweak {
     38                while ( result !=  kids.end() ) {
     39                        result = std::find_if (result, kids.end(), cast_ptr< Statement, DeclStmt > );
    3140
    32   CompoundStmt* DeclarationHoister::mutate(CompoundStmt *compoundStmt) {
    33     typedef std::list<Statement *>::iterator stmt_it;
    34     // 1. collect Declaration Statements  in this scope
    35     std::list<Statement *> &kids = compoundStmt->get_kids();
    36     std::list<Statement *>::iterator result = kids.begin();
    37     std::list< stmt_it > decls;
     41                        if ( result != kids.end() ) {
     42                                decls.push_back( result );
     43                                std::advance( result, 1 );
     44                        } // if
     45                } // while
    3846
    39     while ( result !=  kids.end() ) {
    40       result = std::find_if (result, kids.end(), cast_ptr< Statement, DeclStmt > );
     47                for ( std::list< stmt_it >::reverse_iterator i = decls.rbegin(); i!= decls.rend(); i++ ) {
     48                        kids.push_front( **i );
     49                        kids.erase( *i );
     50                } // for
    4151
    42       if ( result != kids.end() ) {
    43         decls.push_back( result );
    44         std::advance( result, 1 );
    45       }
    46     }
    47 
    48     for ( std::list< stmt_it >::reverse_iterator i = decls.rbegin(); i!= decls.rend(); i++ ){
    49       kids.push_front( **i );
    50       kids.erase( *i );
    51     }
    52 
    53     return compoundStmt;
    54   }
     52                return compoundStmt;
     53        }
    5554} // namespace InitTweak
    56 
    57 
    58 
    59 
    60 
    61 
    62 
    6355
    6456// Local Variables: //
  • translator/InitTweak/DeclarationHoister.h

    r01aeade ra08ba92  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // XXX.cc --
     7// DeclarationHoister.h --
    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
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Tue May 19 16:33:48 2015
     13// Update Count     : 2
    1414//
     15
    1516#include "SynTree/Visitor.h"
    1617#include "SymTab/Indexer.h"
    1718
    1819namespace InitTweak {
     20        bool isDeclStmtP(Statement *stmt);
    1921
    20   bool isDeclStmtP(Statement *stmt);
    21 
    22   class DeclarationHoister: public Mutator {
    23   public:
    24     virtual CompoundStmt   *mutate(CompoundStmt *compoundStmt);
    25   };
    26 
     22        class DeclarationHoister: public Mutator {
     23          public:
     24                virtual CompoundStmt   *mutate(CompoundStmt *compoundStmt);
     25        };
    2726}  // namespace InitTweak
    2827
    29 /*
    30   Local Variables:
    31   mode: c++
    32   End:
    33 */
    3428// Local Variables: //
    3529// tab-width: 4 //
  • translator/InitTweak/InitExpander.cc

    r01aeade ra08ba92  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // XXX.cc --
     7// InitExpander.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
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Tue May 19 16:34:12 2015
     13// Update Count     : 1
    1414//
     15
    1516#include <list>
    1617#include <stack>
    1718#include <cassert>
    1819#include <algorithm>
    19 
    2020
    2121#include "utility.h"
     
    2424
    2525namespace InitTweak {
     26        InitExpander::InitExpander() {}
    2627
    27   InitExpander::InitExpander() {}
     28        InitExpander::~InitExpander() {}
    2829
    29   InitExpander::~InitExpander() {}
     30        ObjectDecl *InitExpander::mutate( ObjectDecl *objectDecl ) {
     31                index.visit( objectDecl );
    3032
    31   ObjectDecl *InitExpander::mutate( ObjectDecl *objectDecl ) {
    32     index.visit( objectDecl );
     33                if ( objectDecl->get_init() == 0 ) return objectDecl;
    3334
    34     if ( objectDecl->get_init() == 0 ) return objectDecl;
     35                InitModelBuilder builder( objectDecl );
     36                builder.get_assoc()->display( std::cerr ); // xxx
     37                InitModelFiller filler( builder.get_assoc(), objectDecl->get_init(), true );
     38                // filler.get_assoc()->display( std::cerr ); // xxx
     39                InitUnspooler exp;
     40                filler.get_assoc()->accept( exp );
     41                objectDecl->set_init( exp.grab_initializer() );
     42                objectDecl->get_init()->print( std::cerr );
    3543
    36     InitModelBuilder builder( objectDecl );
    37     builder.get_assoc()->display( std::cerr ); // xxx
    38     InitModelFiller filler( builder.get_assoc(), objectDecl->get_init(), true );
    39     // filler.get_assoc()->display( std::cerr ); // xxx
    40     InitUnspooler exp;
    41     filler.get_assoc()->accept( exp );
    42     objectDecl->set_init( exp.grab_initializer() );
    43     objectDecl->get_init()->print( std::cerr );
    44 
    45     return objectDecl;
    46   }
    47 
     44                return objectDecl;
     45        }
    4846} // namespace InitTweak
    4947
  • translator/InitTweak/InitExpander.h

    r01aeade ra08ba92  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // XXX.cc --
     7// InitExpander.h --
    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
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Tue May 19 16:35:33 2015
     13// Update Count     : 2
    1414//
     15
    1516#ifndef _INIT_EXPANDER_H_
    1617#define _INIT_EXPANDER_H_
     
    2728
    2829namespace InitTweak {
     30        class InitExpander : public Mutator {
     31                typedef Mutator Parent;
     32          public:
     33                InitExpander();
     34                ~InitExpander();
    2935
    30   class InitExpander : public Mutator
    31   {
    32     typedef Mutator Parent;
     36                virtual ObjectDecl *mutate( ObjectDecl * );
    3337
    34   public:
    35     InitExpander();
    36     ~InitExpander();
     38                // indexer runs
     39                virtual FunctionDecl   *mutate( FunctionDecl *functionDecl ) {
     40                        functionDecl->set_functionType( maybeMutate( functionDecl->get_functionType(), *this ) );
     41                        mutateAll( functionDecl->get_oldDecls(), *this );
     42                        functionDecl->set_statements( maybeMutate( functionDecl->get_statements(), *this ) );
    3743
    38     virtual ObjectDecl *mutate( ObjectDecl * );
     44                        index.visit( functionDecl );
     45                        return functionDecl;
     46                }
    3947
    40     // indexer runs
    41     virtual FunctionDecl   *mutate( FunctionDecl *functionDecl )
    42     {
    43       functionDecl->set_functionType( maybeMutate( functionDecl->get_functionType(), *this ) );
    44       mutateAll( functionDecl->get_oldDecls(), *this );
    45       functionDecl->set_statements( maybeMutate( functionDecl->get_statements(), *this ) );
     48                virtual TypeDecl *mutate( TypeDecl *typeDecl )
     49                        { index.visit( typeDecl ); return typeDecl; }
     50                virtual TypedefDecl *mutate( TypedefDecl *typeDecl )
     51                        { index.visit( typeDecl ); return typeDecl; }
     52                virtual StructDecl *mutate( StructDecl *aggregateDecl )
     53                        { index.visit( aggregateDecl ); return aggregateDecl; }
     54                virtual UnionDecl *mutate( UnionDecl *aggregateDecl )
     55                        { index.visit( aggregateDecl ); return aggregateDecl; }
     56                virtual EnumDecl *mutate( EnumDecl *aggregateDecl )
     57                        { index.visit( aggregateDecl ); return aggregateDecl; }
    4658
    47       index.visit( functionDecl );
    48       return functionDecl;
    49     }
    50 
    51     virtual TypeDecl       *mutate( TypeDecl *typeDecl )
    52     { index.visit( typeDecl ); return typeDecl; }
    53     virtual TypedefDecl    *mutate( TypedefDecl *typeDecl )
    54     { index.visit( typeDecl ); return typeDecl; }
    55     virtual StructDecl     *mutate( StructDecl *aggregateDecl )
    56     { index.visit( aggregateDecl ); return aggregateDecl; }
    57     virtual UnionDecl      *mutate( UnionDecl *aggregateDecl )
    58     { index.visit( aggregateDecl ); return aggregateDecl; }
    59     virtual EnumDecl       *mutate( EnumDecl *aggregateDecl )
    60     { index.visit( aggregateDecl ); return aggregateDecl; }
    61 
    62     virtual Type           *mutate( StructInstType *aggrInst )
    63     { index.visit( aggrInst ); return aggrInst; }
    64     virtual Type           *mutate( UnionInstType *aggrInst )
    65     { index.visit( aggrInst ); return aggrInst; }
    66 
    67   private:
    68     SymTab::Indexer index;
    69   };  // class InitExpander
    70 
     59                virtual Type *mutate( StructInstType *aggrInst )
     60                        { index.visit( aggrInst ); return aggrInst; }
     61                virtual Type *mutate( UnionInstType *aggrInst )
     62                        { index.visit( aggrInst ); return aggrInst; }
     63          private:
     64                SymTab::Indexer index;
     65        };  // class InitExpander
    7166} // namespace InitTweak
    7267
     68#endif // _INIT_EXPANDER_H_
    7369
    74 #endif // #ifndef _INIT_EXPANDER_H_
    75 
    76 /*
    77   Local Variables:
    78   mode: c++
    79   End:
    80 */
    8170// Local Variables: //
    8271// tab-width: 4 //
  • translator/InitTweak/InitModel.cc

    r01aeade ra08ba92  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // XXX.cc --
     7// InitModel.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 16:37:08 2015
     13// Update Count     : 1
     14//
     15
    1516#include "SynTree/Constant.h"
    1617#include "InitModel.h"
     
    2122
    2223namespace InitTweak {
    23 
    24   InitModelBuilder::InitModelBuilder( Declaration *_decl )
    25     : taken( false ), decl( 0 ), building(0) {
    26 
    27     ObjectDecl *_odecl = dynamic_cast< ObjectDecl * >( _decl );
    28     assert( _odecl != 0 );
    29     Type *objectType = _odecl->get_type();
    30 
    31     /* this to be replaced by dynamic dispatch */
    32     if ( dynamic_cast< BasicType * >(objectType) != 0 ) {
    33       if ( building == 0 ) building = new SingleName;
    34     } else if ( ReferenceToType *rt = dynamic_cast< ReferenceToType * >(objectType) ) {
    35       rt->accept( *this );
    36     } else if ( ArrayType *at = dynamic_cast< ArrayType * >(objectType) ) {
    37       at->accept( *this );
    38     } else // if (tuples)
    39       std::cerr << "Got something else" << std::endl;
    40 
    41     if ( decl != 0 ) init();
    42   }
    43 
    44   InitModelBuilder::~InitModelBuilder() { if ( ! taken ) { delete building; building = 0; } }
    45 
    46   void InitModelBuilder::init() {
    47     assert( decl != 0 );
    48     decl->accept( *this );
    49   }
    50 
    51   // Visitor interface
    52   void InitModelBuilder::visit( ArrayType *at ) {
    53     if ( building == 0 ) building = new RangeAssociation(interpretDimension( at->get_dimension() ));
    54     decl = 0;
    55     return;
    56   }
    57 
    58   void InitModelBuilder::visit( StructInstType *st ) {
    59     if ( building == 0 ) building = new PointAssociation;
    60     decl = st->get_baseStruct();
    61     return;
    62   }
    63 
    64   void InitModelBuilder::visit( UnionInstType *ut ) {
    65     decl = ut->get_baseUnion();
    66     return;
    67   }
    68   void InitModelBuilder::visit( EnumInstType * ) {}
    69 
    70   void InitModelBuilder::visit( StructDecl *aggregateDecl) {
    71     PointAssociation *pa = dynamic_cast< PointAssociation * >( building );
    72     assert( pa != 0 );
    73     std::list< Declaration * > mem = aggregateDecl->get_members();
    74 
    75     for ( std::list<Declaration *>::iterator i = mem.begin(); i != mem.end(); i++ ) {
    76       pa->add_member( (*i)->get_name() );
    77       InitModelBuilder rec(*i);
    78       pa->set_member( (*i)->get_name(), rec.grab_assoc() );
    79     }
    80 
    81     return;
    82   }
    83 
    84   void InitModelBuilder::visit( UnionDecl *) {}
    85   void InitModelBuilder::visit( EnumDecl *) {}
    86 
    87   // InitModelBuilder::ConstantFolder
    88   void InitModelBuilder::ConstantFolder::visit( ConstantExpr *cnst ) {
    89     Constant *c = cnst->get_constant();
    90     assert (c != 0);
    91     if ( BasicType *bt = dynamic_cast<BasicType *>( c->get_type() ) ) {
    92       if ( bt->isInteger() ) {
    93         // need more intelligence here, not necessarily base 10
    94         value = std::strtol( c->get_value().c_str(), NULL, 10 );
    95         return;
    96       } else
    97         std::cerr << "Basic type but not integer" << std::endl;
    98     }
    99     throw 0;
    100   }
    101 
    102   // InitModelFiller
    103   InitModelFiller::InitModelFiller( Association *_model, Initializer *_init, bool _topLevel )
    104       : model( _model ), orgInit( _init ), topLevel( _topLevel ), next( 0 ) {
    105     //next = model.begin();
    106     if ( orgInit != 0 ) init();
    107   }
    108 
    109   void InitModelFiller::init() {
    110     assert( model != 0 ); // change it into a reference
    111     assert( orgInit != 0 );
    112     orgInit->accept( *this );
    113   }
    114 
    115   void InitModelFiller::visit( SingleInit *singleInit ) {
    116     std::list< Expression *> &des = singleInit->get_designators();
    117 
    118     if ( topLevel ) {
    119       assert ( des.empty() );
    120       assert ( dynamic_cast< SingleName * >(model) != 0 );
    121       try {
    122         model->add_single( next++, singleInit->get_value() );
    123       } catch (...) {
    124         std::cerr << "Illegal initialization" << std::endl;
    125       }
    126       return;
    127     }
    128 
    129     if ( des.empty() ) {
    130       assert( model != 0 );
    131       try {
    132         model->add_single( next++, singleInit->get_value() );
    133       } catch ( AssocException &e ) {
    134         throw SemanticError( "Illegal initialization: " + e.get_what() );
    135       } catch ( ... ) {
    136         std::cerr << "Shouldn't be here" << std::endl;
    137       }
    138       return;
    139     }
    140 
    141     // not general enough (does not contend well with designated arrays)
    142     std::list<std::string> desnames;
    143     std::transform( des.begin(), des.end(), back_inserter(desnames), Initializer::designator_name );
    144 
    145     for ( std::list<std::string>::iterator i = desnames.begin(); i != desnames.end(); i++ ) {
    146       try {
    147         next = model->add_single( *i, singleInit->get_value() );
    148         next++;
    149       } catch ( AssocException &e ) {
    150         throw SemanticError( "Illegal initialization: " + e.get_what() );
    151       } catch ( ... ) {
    152         std::cerr << "Shouldn't happen, check association" << std::endl;
    153       }
    154     }
    155 
    156     return;
    157   }
    158 
    159   void InitModelFiller::visit( ListInit *listInit ) {
    160     assert( listInit != 0 );
    161 
    162     // designators
    163     std::list< Expression *> &des = listInit->get_designators();
    164     std::list< Initializer *> &ini = listInit->get_initializers();
    165 
    166     if (! des.empty() ) {
    167       if (topLevel)
    168         throw SemanticError( "Invalid initializer: designated at top level." );
    169 
    170       std::list<Expression *> des2;
    171       std::copy (des.begin(), des.end(), back_inserter( des2 ));
    172       std::list< Expression * > empty;
    173       listInit->set_designators( empty );
    174       for ( std::list<Expression *>::iterator i = des2.begin(); i != des2.end(); i++ ) {
    175         Association * newModel = 0;
    176         if ( NameExpr *n = dynamic_cast< NameExpr * >( *i ) )
    177           try {
    178             newModel = (*model)[ n->get_name() ];
    179           } catch( AssocException &e ) {
    180             std::cerr << "Didn't find member: " << e.get_what() << std::endl;
    181           }
    182         else // if ( RangeExpr *r = dynamic_cast< RangeExpr * >( *i ) )
    183           std::cerr << "Invalid designator specification" << std::endl;
    184 
    185         InitModelFiller rec( newModel, listInit, true );
    186       }
    187 
    188     } else
    189       if (topLevel) {
    190         topLevel = false;
    191         for ( std::list<Initializer*>::iterator i = ini.begin(); i != ini.end(); i++ )
    192           (*i)->accept(*this);
    193       } else
    194         // next available uninitialized member
    195         InitModelFiller rec( (*model)[next++], listInit, true );
    196   }
    197 
    198   void InitUnspooler::visit( SingleName *single ) {
    199     assert(init == 0 && single != 0);
    200     std::list< Expression * > empty;
    201     init = new SingleInit( single->get_expr(), empty );
    202     return;
    203   }
    204 
    205   void InitUnspooler::visit( PointAssociation *pa ) {
    206     assert( pa != 0 );
    207 
    208     std::list< Initializer * > contents;
    209     for ( int i = 0; i < pa->size(); i++ )
    210       if ( (*pa)[i] != 0 ) {
    211         InitUnspooler rec;
    212         (*pa)[i]->accept( rec );
    213         assert( rec.get_initializer() != 0 );
    214         contents.push_back( rec.grab_initializer() );
    215       }
    216 
    217     init = new ListInit( contents );
    218     return;
    219   }
    220 
    221 
    222 
     24        InitModelBuilder::InitModelBuilder( Declaration *_decl )
     25                : taken( false ), decl( 0 ), building(0) {
     26
     27                ObjectDecl *_odecl = dynamic_cast< ObjectDecl * >( _decl );
     28                assert( _odecl != 0 );
     29                Type *objectType = _odecl->get_type();
     30
     31                /* this to be replaced by dynamic dispatch */
     32                if ( dynamic_cast< BasicType * >(objectType) != 0 ) {
     33                        if ( building == 0 ) building = new SingleName;
     34                } else if ( ReferenceToType *rt = dynamic_cast< ReferenceToType * >(objectType) ) {
     35                        rt->accept( *this );
     36                } else if ( ArrayType *at = dynamic_cast< ArrayType * >(objectType) ) {
     37                        at->accept( *this );
     38                } else // if (tuples)
     39                        std::cerr << "Got something else" << std::endl;
     40
     41                if ( decl != 0 ) init();
     42        }
     43
     44        InitModelBuilder::~InitModelBuilder() { if ( ! taken ) { delete building; building = 0; } }
     45
     46        void InitModelBuilder::init() {
     47                assert( decl != 0 );
     48                decl->accept( *this );
     49        }
     50
     51        // Visitor interface
     52        void InitModelBuilder::visit( ArrayType *at ) {
     53                if ( building == 0 ) building = new RangeAssociation(interpretDimension( at->get_dimension() ));
     54                decl = 0;
     55                return;
     56        }
     57
     58        void InitModelBuilder::visit( StructInstType *st ) {
     59                if ( building == 0 ) building = new PointAssociation;
     60                decl = st->get_baseStruct();
     61                return;
     62        }
     63
     64        void InitModelBuilder::visit( UnionInstType *ut ) {
     65                decl = ut->get_baseUnion();
     66                return;
     67        }
     68        void InitModelBuilder::visit( EnumInstType * ) {}
     69
     70        void InitModelBuilder::visit( StructDecl *aggregateDecl) {
     71                PointAssociation *pa = dynamic_cast< PointAssociation * >( building );
     72                assert( pa != 0 );
     73                std::list< Declaration * > mem = aggregateDecl->get_members();
     74
     75                for ( std::list<Declaration *>::iterator i = mem.begin(); i != mem.end(); i++ ) {
     76                        pa->add_member( (*i)->get_name() );
     77                        InitModelBuilder rec(*i);
     78                        pa->set_member( (*i)->get_name(), rec.grab_assoc() );
     79                } // for
     80
     81                return;
     82        }
     83
     84        void InitModelBuilder::visit( UnionDecl *) {}
     85        void InitModelBuilder::visit( EnumDecl *) {}
     86
     87        // InitModelBuilder::ConstantFolder
     88        void InitModelBuilder::ConstantFolder::visit( ConstantExpr *cnst ) {
     89                Constant *c = cnst->get_constant();
     90                assert (c != 0);
     91                if ( BasicType *bt = dynamic_cast<BasicType *>( c->get_type() ) ) {
     92                        if ( bt->isInteger() ) {
     93                                // need more intelligence here, not necessarily base 10
     94                                value = std::strtol( c->get_value().c_str(), NULL, 10 );
     95                                return;
     96                        } else
     97                                std::cerr << "Basic type but not integer" << std::endl;
     98                } // if
     99                throw 0;
     100        }
     101
     102        // InitModelFiller
     103        InitModelFiller::InitModelFiller( Association *_model, Initializer *_init, bool _topLevel )
     104                : model( _model ), orgInit( _init ), topLevel( _topLevel ), next( 0 ) {
     105                //next = model.begin();
     106                if ( orgInit != 0 ) init();
     107        }
     108
     109        void InitModelFiller::init() {
     110                assert( model != 0 ); // change it into a reference
     111                assert( orgInit != 0 );
     112                orgInit->accept( *this );
     113        }
     114
     115        void InitModelFiller::visit( SingleInit *singleInit ) {
     116                std::list< Expression *> &des = singleInit->get_designators();
     117
     118                if ( topLevel ) {
     119                        assert ( des.empty() );
     120                        assert ( dynamic_cast< SingleName * >(model) != 0 );
     121                        try {
     122                                model->add_single( next++, singleInit->get_value() );
     123                        } catch (...) {
     124                                std::cerr << "Illegal initialization" << std::endl;
     125                        }
     126                        return;
     127                } // if
     128
     129                if ( des.empty() ) {
     130                        assert( model != 0 );
     131                        try {
     132                                model->add_single( next++, singleInit->get_value() );
     133                        } catch ( AssocException &e ) {
     134                                throw SemanticError( "Illegal initialization: " + e.get_what() );
     135                        } catch ( ... ) {
     136                                std::cerr << "Shouldn't be here" << std::endl;
     137                        } // try
     138                        return;
     139                } // if
     140
     141                // not general enough (does not contend well with designated arrays)
     142                std::list<std::string> desnames;
     143                std::transform( des.begin(), des.end(), back_inserter(desnames), Initializer::designator_name );
     144
     145                for ( std::list<std::string>::iterator i = desnames.begin(); i != desnames.end(); i++ ) {
     146                        try {
     147                                next = model->add_single( *i, singleInit->get_value() );
     148                                next++;
     149                        } catch ( AssocException &e ) {
     150                                throw SemanticError( "Illegal initialization: " + e.get_what() );
     151                        } catch ( ... ) {
     152                                std::cerr << "Shouldn't happen, check association" << std::endl;
     153                        } // try
     154                } // for
     155
     156                return;
     157        }
     158
     159        void InitModelFiller::visit( ListInit *listInit ) {
     160                assert( listInit != 0 );
     161
     162                // designators
     163                std::list< Expression *> &des = listInit->get_designators();
     164                std::list< Initializer *> &ini = listInit->get_initializers();
     165
     166                if ( ! des.empty() ) {
     167                        if (topLevel)
     168                                throw SemanticError( "Invalid initializer: designated at top level." );
     169
     170                        std::list<Expression *> des2;
     171                        std::copy (des.begin(), des.end(), back_inserter( des2 ));
     172                        std::list< Expression * > empty;
     173                        listInit->set_designators( empty );
     174                        for ( std::list<Expression *>::iterator i = des2.begin(); i != des2.end(); i++ ) {
     175                                Association * newModel = 0;
     176                                if ( NameExpr *n = dynamic_cast< NameExpr * >( *i ) )
     177                                        try {
     178                                                newModel = (*model)[ n->get_name() ];
     179                                        } catch( AssocException &e ) {
     180                                                std::cerr << "Didn't find member: " << e.get_what() << std::endl;
     181                                        }
     182                                else // if ( RangeExpr *r = dynamic_cast< RangeExpr * >( *i ) )
     183                                        std::cerr << "Invalid designator specification" << std::endl;
     184
     185                                InitModelFiller rec( newModel, listInit, true );
     186                        } // for
     187                } else
     188                        if (topLevel) {
     189                                topLevel = false;
     190                                for ( std::list<Initializer*>::iterator i = ini.begin(); i != ini.end(); i++ )
     191                                        (*i)->accept(*this);
     192                        } else
     193                                // next available uninitialized member
     194                                InitModelFiller rec( (*model)[next++], listInit, true );
     195        }
     196
     197        void InitUnspooler::visit( SingleName *single ) {
     198                assert(init == 0 && single != 0);
     199                std::list< Expression * > empty;
     200                init = new SingleInit( single->get_expr(), empty );
     201                return;
     202        }
     203
     204        void InitUnspooler::visit( PointAssociation *pa ) {
     205                assert( pa != 0 );
     206
     207                std::list< Initializer * > contents;
     208                for ( int i = 0; i < pa->size(); i++ )
     209                        if ( (*pa)[i] != 0 ) {
     210                                InitUnspooler rec;
     211                                (*pa)[i]->accept( rec );
     212                                assert( rec.get_initializer() != 0 );
     213                                contents.push_back( rec.grab_initializer() );
     214                        } // if
     215
     216                init = new ListInit( contents );
     217                return;
     218        }
    223219} // namespace InitTweak
     220
    224221// Local Variables: //
    225222// tab-width: 4 //
  • translator/InitTweak/InitModel.h

    r01aeade ra08ba92  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // XXX.cc --
     7// InitModel.h --
    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
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Tue May 19 16:37:52 2015
     13// Update Count     : 2
    1414//
     15
    1516#ifndef _INITTWEAK_MODEL_H_
    1617#define _INITTWEAK_MODEL_H_
     
    2627
    2728namespace InitTweak {
    28     class InitModelBuilder : public AssociationBuilder, public Visitor {
    29       public:
    30         InitModelBuilder( Declaration * );
    31         ~InitModelBuilder();
     29        class InitModelBuilder : public AssociationBuilder, public Visitor {
     30          public:
     31                InitModelBuilder( Declaration * );
     32                ~InitModelBuilder();
    3233
    33         virtual Association *grab_assoc() { taken = true; return building; }
    34         virtual Association *get_assoc() { return building; }
    35         void set_assoc( Association *newAssoc ) { building = newAssoc; }
     34                virtual Association *grab_assoc() { taken = true; return building; }
     35                virtual Association *get_assoc() { return building; }
     36                void set_assoc( Association *newAssoc ) { building = newAssoc; }
    3637
    37         void init();
    38         static int interpretDimension( Expression *exp ) {
    39             ConstantFolder folder( exp );
    40             try {
    41                 return folder.get_constant();
    42             } catch (...) {
    43                 throw SemanticError("Invalid array dimension");
    44             }
    45         }
     38                void init();
     39                static int interpretDimension( Expression *exp ) {
     40                        ConstantFolder folder( exp );
     41                        try {
     42                                return folder.get_constant();
     43                        } catch (...) {
     44                                throw SemanticError("Invalid array dimension");
     45                        }
     46                }
    4647
    47         // types
    48         virtual void visit( ArrayType * );
    49         virtual void visit( StructInstType * );
    50         virtual void visit( UnionInstType * );
    51         virtual void visit( EnumInstType * );
    52         virtual void visit( ContextInstType * ) { throw 0; }
    53         virtual void visit( TypeInstType * )    { throw 0; }
    54         // virtual void visit( TupleType *tupleType );
    55         // declarations
    56         virtual void visit( StructDecl *);
    57         virtual void visit( UnionDecl *);
    58         virtual void visit( EnumDecl *);
    59       private:
    60         class ConstantFolder : public Visitor {
    61           public:
    62             ConstantFolder( Expression *_expr = 0 ): expr(_expr) {}
    63             int get_constant() throw() { expr->accept( *this ); return value; }
    64             void set_constant( Expression *newExp ) { expr = newExp; }
    65             // Visitor interface
    66             void visit( Expression * ) { throw 0; }
    67             void visit( NameExpr * ) { throw 0; }
    68             void visit( CastExpr * ) { throw 0; }
    69             void visit( UntypedMemberExpr * ) { throw 0; }
    70             void visit( VariableExpr * ) { throw 0; }
    71             void visit( ConstantExpr * );
    72             void visit( SizeofExpr * ) { throw 0; }
    73             void visit( AttrExpr * ) { throw 0; }
    74             void visit( LogicalExpr * ) { throw 0; }
    75             void visit( ConditionalExpr * ) { throw 0; }
    76             void visit( CommaExpr * ) { throw 0; }
     48                // types
     49                virtual void visit( ArrayType * );
     50                virtual void visit( StructInstType * );
     51                virtual void visit( UnionInstType * );
     52                virtual void visit( EnumInstType * );
     53                virtual void visit( ContextInstType * ) { throw 0; }
     54                virtual void visit( TypeInstType * )    { throw 0; }
     55                // virtual void visit( TupleType *tupleType );
     56                // declarations
     57                virtual void visit( StructDecl *);
     58                virtual void visit( UnionDecl *);
     59                virtual void visit( EnumDecl *);
    7760          private:
    78             Expression *expr;
    79             int value;
     61                class ConstantFolder : public Visitor {
     62                  public:
     63                        ConstantFolder( Expression *_expr = 0 ): expr(_expr) {}
     64                        int get_constant() throw() { expr->accept( *this ); return value; }
     65                        void set_constant( Expression *newExp ) { expr = newExp; }
     66                        // Visitor interface
     67                        void visit( Expression * ) { throw 0; }
     68                        void visit( NameExpr * ) { throw 0; }
     69                        void visit( CastExpr * ) { throw 0; }
     70                        void visit( UntypedMemberExpr * ) { throw 0; }
     71                        void visit( VariableExpr * ) { throw 0; }
     72                        void visit( ConstantExpr * );
     73                        void visit( SizeofExpr * ) { throw 0; }
     74                        void visit( AttrExpr * ) { throw 0; }
     75                        void visit( LogicalExpr * ) { throw 0; }
     76                        void visit( ConditionalExpr * ) { throw 0; }
     77                        void visit( CommaExpr * ) { throw 0; }
     78                  private:
     79                        Expression *expr;
     80                        int value;
     81                };
     82
     83                bool taken;
     84                Declaration *decl;  // ?
     85                Association *building;
    8086        };
    8187
    82         bool taken;
    83         Declaration *decl;  // ?
    84         Association *building;
    85     };
     88        class InitModelFiller : public AssociationFiller, public Visitor {
     89          public:
     90                InitModelFiller( Association *, Initializer *, bool _topLevel = false );
     91                ~InitModelFiller() { /* pointers in here are not owned by object (never created by object either) */ }
     92                virtual Association *get_assoc() { return model; }
     93                virtual void set_assoc( Association *newAssoc ) { model = newAssoc; }
    8694
    87     class InitModelFiller : public AssociationFiller, public Visitor {
    88       public:
    89         InitModelFiller( Association *, Initializer *, bool _topLevel = false );
    90         ~InitModelFiller() { /* pointers in here are not owned by object (never created by object either) */ }
    91         virtual Association *get_assoc() { return model; }
    92         virtual void set_assoc( Association *newAssoc ) { model = newAssoc; }
     95                void init();
     96                // Visitor interface
     97                virtual void visit( SingleInit *singleInit );
     98                virtual void visit( ListInit *listInit );
     99          private:
     100                Association *model;
     101                Initializer *orgInit;
     102                bool topLevel;
     103                long int next;
     104        };
    93105
    94         void init();
    95         // Visitor interface
    96         virtual void visit( SingleInit *singleInit );
    97         virtual void visit( ListInit *listInit );
    98       private:
    99         Association *model;
    100         Initializer *orgInit;
    101         bool topLevel;
    102         long int next;
    103     };
     106        class InitUnspooler : public AssociationVisitor {
     107          public:
     108                InitUnspooler() : init(0), taken( false ) {}
     109                virtual ~InitUnspooler() { if ( ! taken && (init != 0)) { delete init; init = 0; } }
     110                Initializer *get_initializer() { return init; }
     111                Initializer *grab_initializer() { taken = true; return init; }
    104112
    105     class InitUnspooler : public AssociationVisitor {
    106       public:
    107         InitUnspooler() : init(0), taken( false ) {}
    108         virtual ~InitUnspooler() { if (! taken && (init != 0)) { delete init; init = 0; } }
    109         Initializer *get_initializer() { return init; }
    110         Initializer *grab_initializer() { taken = true; return init; }
    111 
    112         virtual void visit( SingleName * );
    113         virtual void visit( PointAssociation * );
    114         virtual void visit( RangeAssociation * ) { std::cerr << "InitUnspooler - In a range assoc" << std::endl; return; }
    115       private:
    116         Initializer *init;
    117         bool taken;
    118     };
    119 
     113                virtual void visit( SingleName * );
     114                virtual void visit( PointAssociation * );
     115                virtual void visit( RangeAssociation * ) { std::cerr << "InitUnspooler - In a range assoc" << std::endl; return; }
     116          private:
     117                Initializer *init;
     118                bool taken;
     119        };
    120120} // namespace InitTweak
    121121
    122122#endif // _INITTWEAK_MODEL_H_
    123123
    124 /*
    125   Local Variables:
    126   mode: c++
    127   End:
    128 */
    129124// Local Variables: //
    130125// tab-width: 4 //
  • translator/InitTweak/Mutate.cc

    r01aeade ra08ba92  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // XXX.cc --
     7// Mutate.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
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Tue May 19 16:38:19 2015
     13// Update Count     : 1
    1414//
     15
    1516#include "SynTree/Mutator.h"
    1617
     
    2223
    2324namespace InitTweak {
     25        void mutate( std::list< Declaration * > translationUnit ) {
     26                //BasicInit bi;
     27                InitExpander ini;
     28                //DeclarationHoister dh;
    2429
    25   void mutate( std::list< Declaration * > translationUnit )
    26   {
    27     //BasicInit bi;
    28     InitExpander ini;
    29     //DeclarationHoister dh;
    30 
    31     //mutateAll( translationUnit, bi );
    32     mutateAll( translationUnit, ini );
    33     //mutateAll( translationUnit, dh );
    34   }
    35 
     30                //mutateAll( translationUnit, bi );
     31                mutateAll( translationUnit, ini );
     32                //mutateAll( translationUnit, dh );
     33        }
    3634} // namespace InitTweak
    37 
    3835
    3936// Local Variables: //
  • translator/InitTweak/Mutate.h

    r01aeade ra08ba92  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // XXX.cc --
     7// Mutate.h --
    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
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Tue May 19 16:38:52 2015
     13// Update Count     : 2
    1414//
     15
    1516#ifndef INIT_MUTATE_H
    1617#define INIT_MUTATE_H
     
    2122
    2223namespace InitTweak {
    23 
    24   void mutate( std::list< Declaration* > translationUnit );
    25 
     24        void mutate( std::list< Declaration* > translationUnit );
    2625} // namespace InitTweak
    2726
    28 #endif // #ifndef INIT_MUTATE_H
     27#endif // INIT_MUTATE_H
    2928
    30 /*
    31   Local Variables:
    32   mode: c++
    33   End:
    34 */
    3529// Local Variables: //
    3630// tab-width: 4 //
  • translator/InitTweak/RemoveInit.cc

    r01aeade ra08ba92  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // XXX.cc --
     7// RemoveInit.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
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Tue May 19 16:39:32 2015
     13// Update Count     : 1
    1414//
     15
    1516#include "RemoveInit.h"
    1617#include "SynTree/Declaration.h"
     
    2223
    2324namespace InitTweak {
     25        namespace {
     26                const std::list<Label> noLabels;
     27        }
    2428
    25 namespace {
    26 const std::list<Label> noLabels;
    27 }
     29        void tweak( std::list< Declaration * > translationUnit ) {
     30                RemoveInit remover;
     31                mutateAll( translationUnit, remover );
     32        }
    2833
    29 void tweak( std::list< Declaration * > translationUnit ) {
    30   RemoveInit remover;
    31   mutateAll( translationUnit, remover );
    32 }
     34        void RemoveInit::mutateStatementList( std::list< Statement* > &statements ) {
     35                for ( std::list< Statement* >::iterator i = statements.begin(); i != statements.end(); ++i ) {
     36                        if ( ! stmtsToAddAfter.empty() ) {
     37                                statements.splice( i, stmtsToAddAfter );
     38                        } // if
     39                        *i = (*i)->acceptMutator( *this );
     40                } // for
     41                if ( ! stmtsToAddAfter.empty() ) {
     42                        statements.splice( statements.end(), stmtsToAddAfter );
     43                } // if
     44        }
    3345
    34 void RemoveInit::mutateStatementList( std::list< Statement* > &statements ) {
    35   for ( std::list< Statement* >::iterator i = statements.begin(); i != statements.end(); ++i ) {
    36     if ( ! stmtsToAddAfter.empty() ) {
    37       statements.splice( i, stmtsToAddAfter );
    38     }
    39     *i = (*i)->acceptMutator( *this );
    40   }
    41   if ( ! stmtsToAddAfter.empty() ) {
    42     statements.splice( statements.end(), stmtsToAddAfter );
    43   }
    44 }
    45 
    46 CompoundStmt *RemoveInit::mutate(CompoundStmt *compoundStmt) {
    47   mutateStatementList( compoundStmt->get_kids() );
    48   return compoundStmt;
    49 }
     46        CompoundStmt *RemoveInit::mutate(CompoundStmt *compoundStmt) {
     47                mutateStatementList( compoundStmt->get_kids() );
     48                return compoundStmt;
     49        }
    5050
    5151// in the case where an object has an initializer and a polymorphic type, insert an assignment
    5252// immediately after the declaration. This will (seemingly) cause the later phases to do the right
    5353// thing with the assignment
    54 ObjectDecl *RemoveInit::mutate( ObjectDecl *objDecl ) {
    55   if (objDecl->get_init() && dynamic_cast<TypeInstType*>(objDecl->get_type())) {
    56     if (SingleInit * single = dynamic_cast<SingleInit*>(objDecl->get_init())) {
    57       UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) );
    58       assign->get_args().push_back( new AddressExpr (new NameExpr( objDecl->get_name() ) ) );
    59       assign->get_args().push_back( single->get_value()->clone() );
    60       stmtsToAddAfter.push_back(new ExprStmt(noLabels, assign));
    61     }
    62   }
    63   return objDecl;
    64 }
     54        ObjectDecl *RemoveInit::mutate( ObjectDecl *objDecl ) {
     55                if (objDecl->get_init() && dynamic_cast<TypeInstType*>(objDecl->get_type())) {
     56                        if (SingleInit * single = dynamic_cast<SingleInit*>(objDecl->get_init())) {
     57                                UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) );
     58                                assign->get_args().push_back( new AddressExpr (new NameExpr( objDecl->get_name() ) ) );
     59                                assign->get_args().push_back( single->get_value()->clone() );
     60                                stmtsToAddAfter.push_back(new ExprStmt(noLabels, assign));
     61                        } // if
     62                } // if
     63                return objDecl;
     64        }
    6565} // namespace InitTweak
    6666
  • translator/InitTweak/RemoveInit.h

    r01aeade ra08ba92  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // XXX.cc --
     7// RemoveInit.h --
    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
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Tue May 19 16:40:11 2015
     13// Update Count     : 1
    1414//
    15 /*
    16  * This file is part of the Cforall project
    17  *
    18  * $Id: PolyMutator.h,v 1.8 2005/08/29 20:14:13 rcbilson Exp $
    19  *
    20  */
    2115
    2216#ifndef REMOVE_INIT_H
     
    3125
    3226namespace InitTweak {
     27        void tweak( std::list< Declaration * > translationUnit );
    3328
    34 void tweak( std::list< Declaration * > translationUnit );
    35 
    36 class RemoveInit : public Mutator {
    37 public:
    38   // RemoveInit();
    39   virtual ObjectDecl *mutate(ObjectDecl *objDecl);
    40   virtual CompoundStmt *mutate(CompoundStmt *compoundStmt);
    41 protected:
    42   std::list< Statement* > stmtsToAddAfter;
    43   void mutateStatementList( std::list< Statement* > &statements );
    44 };
    45 
     29        class RemoveInit : public Mutator {
     30          public:
     31                // RemoveInit();
     32                virtual ObjectDecl *mutate(ObjectDecl *objDecl);
     33                virtual CompoundStmt *mutate(CompoundStmt *compoundStmt);
     34          protected:
     35                std::list< Statement* > stmtsToAddAfter;
     36                void mutateStatementList( std::list< Statement* > &statements );
     37        };
    4638} // namespace
    4739
    48 #endif /* #ifndef GENPOLY_POLYMUTATOR_H */
     40#endif // GENPOLY_POLYMUTATOR_H
     41
    4942// Local Variables: //
    5043// tab-width: 4 //
  • translator/InitTweak/diet_map.h

    r01aeade ra08ba92  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // XXX.cc --
     7// diet_map.h --
    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 16:41:17 2015
     13// Update Count     : 2
     14//
     15
    1516#include <cassert>
    1617#include <string>
     
    1819
    1920namespace diet {
    20   /* A DIET ( Discrete Interval Encoding Tree ) range-map
    21    */
    22 
    23   class diet_tree_exception : public std::exception {
    24   public:
    25     diet_tree_exception() {}
    26     diet_tree_exception( std::string _what ) : what( _what ) {}
    27     ~diet_tree_exception() throw () {}
    28 
    29     std::string get_what() const { return what; }
    30     void set_what( std::string newValue ) { what = newValue; }
    31   private:
    32     std::string what;
    33   };
    34 
    35   template < typename T > class diet_tree_node;
    36   template < typename T > class diet_tree_iterator;
    37 
    38   template< typename key_type >
    39   class diet_tree {
    40     typedef key_type OrderedValue;
    41     typedef OrderedValue T;
    42     friend class diet_tree_iterator<T>;
    43   public:
    44     typedef OrderedValue value_type;
    45     typedef diet_tree_iterator<OrderedValue> iterator;
    46     typedef std::pair<value_type, value_type> pair_type;
    47 
    48     diet_tree() : root(0), left(0), right(0) {}
    49     ~diet_tree() {
    50       if ( root != 0 ) { delete root; root = 0; }
    51       if ( left != 0 ) { delete left; left = 0; }
    52       if ( right != 0 ) { delete right; right = 0; }
    53     }
    54 
    55     void insert( value_type _lo, value_type _hi ) {
    56       if ( _lo > _hi ) return; // throw exception?
    57       if ( root == 0 )
    58         root = new diet_tree_node<value_type>(_lo, _hi);
    59       else {
    60         value_type lo = root->get_lo(), hi = root->get_hi();
    61         if ( _lo < lo ) {
    62           if ( _hi > hi ) {
    63             /* can either minimize the work or minimize the number of nodes.
    64                Let's minimize the work. */
    65             if ( left == 0 ) left = new diet_tree<T>();
    66             left->insert( _lo, lo );
    67             if ( right == 0 ) right = new diet_tree<T>();
    68             right->insert( _hi, hi );
    69             return;
    70           } else if ( _hi < lo ) {
    71             if ( left == 0 ) left = new diet_tree<T>();
    72             left->insert( _lo, _hi );
    73           } else if ( _hi <= hi ) {
    74             if ( left == 0 ) left = new diet_tree<T>();
    75             left->insert( _lo, _hi );
    76             root->set_range(_hi,hi);
    77           }
    78         } else if (_lo >= lo && _hi <= hi ) {
    79           root->set_range(_lo,_hi);
    80         } else if ( _hi > hi) {
    81           if ( _lo > hi ) {
    82             if ( right == 0 ) right = new diet_tree<T>();
    83             right->insert( _lo, _hi );
    84           } else if ( _lo < hi ) {
    85             root->set_range(lo, _lo);
    86             if ( right == 0 ) right = new diet_tree<T>();
    87             right->insert(_lo, _hi);
    88           }
    89         }
    90       }
    91       return;
    92     }
    93 
    94     void insert( std::pair<value_type, value_type> p ) {
    95       insert(p.first, p.second);
    96     }
    97 
    98     pair_type get_range_pair() const {
    99       return pair_type(root->get_lo(),root->get_hi());
    100     }
    101 
    102     /*
    103     void display( std::ostream &os = std::cout ) {
    104       if ( root != 0 ) {
    105         if ( left != 0 ) left->display(os);
    106         os << "(" << root->get_lo() << ", " << root->get_hi() << ")" << std::endl;
    107         if ( right != 0 ) right->display(os);
    108       }
    109       return;
    110     }
    111     */
    112 
    113     iterator begin() { return iterator( this ); }
    114     iterator end() { return iterator( (diet_tree< value_type > *)0 ); }
    115 
    116   protected:
    117     diet_tree( diet_tree_node< OrderedValue > *_root ) : root( _root ) {}
    118   private:
    119     diet_tree_node< value_type > *root;
    120     diet_tree< value_type > *left, *right;
    121   };
    122 
    123   template< typename OrderedValue >
    124   class diet_tree_node {
    125   public:
    126     typedef OrderedValue value_type;
    127 
    128     diet_tree_node( const OrderedValue &_lo, const OrderedValue &_hi )
    129       : lo( _lo ), hi( _hi ) {
    130       if ( lo >= hi ) throw diet_tree_exception( "Invalid range" );
    131     }
    132 
    133     void set_range(const OrderedValue &newLo, const OrderedValue &newHi)
    134     { lo = newLo; hi = newHi; }
    135     OrderedValue get_lo() const { return lo; }
    136     OrderedValue get_hi() const { return hi; }
    137 
    138   private:
    139     OrderedValue lo, hi;
    140   };
    141 
    142   /* forward iterator */
    143   template < typename T >
    144   class diet_tree_iterator {
    145     typedef diet_tree_iterator<T> self;
    146     typedef typename diet_tree<T>::pair_type pair_type;
    147 
    148   public:
    149     //    typedef forward_iterator_tag iterator_category;
    150 
    151     diet_tree_iterator( diet_tree<T> *_tree ) : current( _tree ) {
    152       // current is allowed to be 0 only for `end'
    153       if (_tree != 0) go_leftmost();
    154     }
    155 
    156     ~diet_tree_iterator() {}
    157     pair_type operator*() {
    158       if ( current == 0 ) throw diet_tree_exception( "Invalid dereference" );
    159       return current->get_range_pair();
    160     }
    161 
    162     bool operator==( const diet_tree_iterator<T> &other ) { return current == other.current;  }
    163     bool operator!=( const diet_tree_iterator<T> &other ) { return current != other.current;  }
    164 
    165     diet_tree_iterator<T> operator++() {
    166       assert(current != 0);
    167       if ( current->right == 0 )
    168         if ( ! st.empty() )
    169           { current = st.top(); st.pop(); }
    170         else
    171           current = 0;
    172       else {
    173         current = current->right;
    174         go_leftmost();
    175       }
    176       return *this;
    177     }
    178 
    179     diet_tree_iterator<T> operator++(int) {
    180       self temp = *this;
    181       this->operator++();
    182       return temp;
    183     }
    184 
    185   private:
    186     void go_leftmost() {
    187       assert(current != 0);
    188       diet_tree<T> *next = 0;
    189       while ( current->left != 0 ) {
    190         next = current->left; st.push( current ); current = next;
    191       }
    192       return;
    193     }
    194 
    195     void defrag() {
    196       /* join adjacent trees */
    197       return;
    198     }
    199 
    200     diet_tree<T> *current;
    201     std::stack< diet_tree<T> * > st;
    202   };
    203 
    204   template < typename Key, typename Value >
    205   class diet_tree_assoc_node : public diet_tree_node<Key> {
    206   public:
    207     typedef Key key_type;
    208     typedef Value data_type;
    209     typedef std::pair<Key,Value> value_type;
    210   private:
    211     Value data;
    212   };
    213 
     21        /* A DIET ( Discrete Interval Encoding Tree ) range-map
     22         */
     23
     24        class diet_tree_exception : public std::exception {
     25          public:
     26                diet_tree_exception() {}
     27                diet_tree_exception( std::string _what ) : what( _what ) {}
     28                ~diet_tree_exception() throw () {}
     29
     30                std::string get_what() const { return what; }
     31                void set_what( std::string newValue ) { what = newValue; }
     32          private:
     33                std::string what;
     34        };
     35
     36        template < typename T > class diet_tree_node;
     37        template < typename T > class diet_tree_iterator;
     38
     39        template< typename key_type >
     40        class diet_tree {
     41                typedef key_type OrderedValue;
     42                typedef OrderedValue T;
     43                friend class diet_tree_iterator<T>;
     44          public:
     45                typedef OrderedValue value_type;
     46                typedef diet_tree_iterator<OrderedValue> iterator;
     47                typedef std::pair<value_type, value_type> pair_type;
     48
     49                diet_tree() : root(0), left(0), right(0) {}
     50                ~diet_tree() {
     51                        if ( root != 0 ) { delete root; root = 0; }
     52                        if ( left != 0 ) { delete left; left = 0; }
     53                        if ( right != 0 ) { delete right; right = 0; }
     54                }
     55
     56                void insert( value_type _lo, value_type _hi ) {
     57                        if ( _lo > _hi ) return; // throw exception?
     58                        if ( root == 0 )
     59                                root = new diet_tree_node<value_type>(_lo, _hi);
     60                        else {
     61                                value_type lo = root->get_lo(), hi = root->get_hi();
     62                                if ( _lo < lo ) {
     63                                        if ( _hi > hi ) {
     64                                                /* can either minimize the work or minimize the number of nodes.
     65                                                   Let's minimize the work. */
     66                                                if ( left == 0 ) left = new diet_tree<T>();
     67                                                left->insert( _lo, lo );
     68                                                if ( right == 0 ) right = new diet_tree<T>();
     69                                                right->insert( _hi, hi );
     70                                                return;
     71                                        } else if ( _hi < lo ) {
     72                                                if ( left == 0 ) left = new diet_tree<T>();
     73                                                left->insert( _lo, _hi );
     74                                        } else if ( _hi <= hi ) {
     75                                                if ( left == 0 ) left = new diet_tree<T>();
     76                                                left->insert( _lo, _hi );
     77                                                root->set_range(_hi,hi);
     78                                        }
     79                                } else if (_lo >= lo && _hi <= hi ) {
     80                                        root->set_range(_lo,_hi);
     81                                } else if ( _hi > hi) {
     82                                        if ( _lo > hi ) {
     83                                                if ( right == 0 ) right = new diet_tree<T>();
     84                                                right->insert( _lo, _hi );
     85                                        } else if ( _lo < hi ) {
     86                                                root->set_range(lo, _lo);
     87                                                if ( right == 0 ) right = new diet_tree<T>();
     88                                                right->insert(_lo, _hi);
     89                                        } // if
     90                                } // if
     91                        } // if
     92                        return;
     93                }
     94
     95                void insert( std::pair<value_type, value_type> p ) {
     96                        insert(p.first, p.second);
     97                }
     98
     99                pair_type get_range_pair() const {
     100                        return pair_type(root->get_lo(),root->get_hi());
     101                }
     102
     103                /*
     104                  void display( std::ostream &os = std::cout ) {
     105                  if ( root != 0 ) {
     106                  if ( left != 0 ) left->display(os);
     107                  os << "(" << root->get_lo() << ", " << root->get_hi() << ")" << std::endl;
     108                  if ( right != 0 ) right->display(os);
     109                  }
     110                  return;
     111                  }
     112                */
     113
     114                iterator begin() { return iterator( this ); }
     115                iterator end() { return iterator( (diet_tree< value_type > *)0 ); }
     116
     117          protected:
     118                diet_tree( diet_tree_node< OrderedValue > *_root ) : root( _root ) {}
     119          private:
     120                diet_tree_node< value_type > *root;
     121                diet_tree< value_type > *left, *right;
     122        };
     123
     124        template< typename OrderedValue >
     125        class diet_tree_node {
     126          public:
     127                typedef OrderedValue value_type;
     128
     129                diet_tree_node( const OrderedValue &_lo, const OrderedValue &_hi )
     130                        : lo( _lo ), hi( _hi ) {
     131                        if ( lo >= hi ) throw diet_tree_exception( "Invalid range" );
     132                }
     133
     134                void set_range(const OrderedValue &newLo, const OrderedValue &newHi)
     135                        { lo = newLo; hi = newHi; }
     136                OrderedValue get_lo() const { return lo; }
     137                OrderedValue get_hi() const { return hi; }
     138
     139          private:
     140                OrderedValue lo, hi;
     141        };
     142
     143        /* forward iterator */
     144        template < typename T >
     145        class diet_tree_iterator {
     146                typedef diet_tree_iterator<T> self;
     147                typedef typename diet_tree<T>::pair_type pair_type;
     148
     149          public:
     150                //    typedef forward_iterator_tag iterator_category;
     151
     152                diet_tree_iterator( diet_tree<T> *_tree ) : current( _tree ) {
     153                        // current is allowed to be 0 only for `end'
     154                        if (_tree != 0) go_leftmost();
     155                }
     156
     157                ~diet_tree_iterator() {}
     158                pair_type operator*() {
     159                        if ( current == 0 ) throw diet_tree_exception( "Invalid dereference" );
     160                        return current->get_range_pair();
     161                }
     162
     163                bool operator==( const diet_tree_iterator<T> &other ) { return current == other.current;  }
     164                bool operator!=( const diet_tree_iterator<T> &other ) { return current != other.current;  }
     165
     166                diet_tree_iterator<T> operator++() {
     167                        assert(current != 0);
     168                        if ( current->right == 0 )
     169                                if ( ! st.empty() )
     170                                        { current = st.top(); st.pop(); }
     171                                else
     172                                        current = 0;
     173                        else {
     174                                current = current->right;
     175                                go_leftmost();
     176                        } // if
     177                        return *this;
     178                }
     179
     180                diet_tree_iterator<T> operator++(int) {
     181                        self temp = *this;
     182                        this->operator++();
     183                        return temp;
     184                }
     185
     186          private:
     187                void go_leftmost() {
     188                        assert(current != 0);
     189                        diet_tree<T> *next = 0;
     190                        while ( current->left != 0 ) {
     191                                next = current->left; st.push( current ); current = next;
     192                        }
     193                        return;
     194                }
     195
     196                void defrag() {
     197                        /* join adjacent trees */
     198                        return;
     199                }
     200
     201                diet_tree<T> *current;
     202                std::stack< diet_tree<T> * > st;
     203        };
     204
     205        template < typename Key, typename Value >
     206        class diet_tree_assoc_node : public diet_tree_node<Key> {
     207          public:
     208                typedef Key key_type;
     209                typedef Value data_type;
     210                typedef std::pair<Key,Value> value_type;
     211          private:
     212                Value data;
     213        };
    214214} // namespace diet
    215215
    216 
    217 /*
    218   Local Variables:
    219   mode: c++
    220   End:
    221 */
    222216// Local Variables: //
    223217// tab-width: 4 //
Note: See TracChangeset for help on using the changeset viewer.