source: translator/InitTweak/BasicInit.cc @ 51b7345

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 51b7345 was 51b7345, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

initial commit

  • Property mode set to 100644
File size: 7.9 KB
Line 
1#include <list>
2#include <cassert>
3#include <iostream>
4#include <iterator>
5#include <algorithm>
6
7#include "utility.h"
8
9#include "SynTree/Type.h"
10#include "SynTree/Statement.h"
11#include "SynTree/Expression.h"
12#include "SynTree/Declaration.h"
13#include "SynTree/Initializer.h"
14
15#include "BasicInit.h"
16#include "NameCollector.h"
17#include "NameAssociation.h"
18
19namespace InitTweak {
20
21  CompoundStmt* BasicInit::mutate(CompoundStmt *compoundStmt)
22  {
23    index.visit( compoundStmt );
24
25    std::list< Statement * > &kids = compoundStmt->get_kids();
26    std::list< Statement * > newKids;
27
28    for ( std::list< Statement * >::iterator i = kids.begin(); i!= kids.end(); i++ ) {
29      //BasicInit newMut(  );
30      (*i)->acceptMutator( *this );
31      newKids.push_back( *i );
32
33      if ( has_bindings() ) { //       if ( get_bindings() != 0  ) {
34        std::list< Statement *> newSt = get_statements();
35        //newSt.push_back( *i );
36        newKids.splice( newKids.end(), newSt );
37        bindings = 0;
38        stmts.clear();
39      }
40    }
41
42    compoundStmt->get_kids() = newKids;
43    return compoundStmt;
44  }
45
46  Statement * BasicInit::mutate(DeclStmt *declStmt) {
47
48    declStmt->accept( index );
49
50    ObjectDecl *odecl = 0;
51
52    if( ( odecl = dynamic_cast<ObjectDecl *>(declStmt->get_decl()) ) != 0 ){
53
54      Initializer *init = odecl->get_init();
55      if ( init == 0 ) return declStmt;
56
57      if ( Classify::type( odecl->get_type() ) == Classify::COMPOUND_T )
58        if ( Classify::initializer(init) == Classify::SINGLE_I )
59          throw( 0 ); // mismatch of type and initializer
60        else {
61          NameInCollection *col = Classify::declaration( odecl, &index );
62          bindings = NameAssociation< Expression *, BreakInitializer >::createNameAssoc(col);
63          bindings->add_value( std::string(""), BreakInitializer(init) );
64          BasicInit::build_statements( bindings, odecl->get_name(), stmts );
65        }
66      else
67        if ( Classify::initializer(init) == Classify::COMPOUND_I )
68          throw( 0 ); // mismatch of type and initializer
69        else {
70          // Single inits
71          SingleInit *sinit = dynamic_cast< SingleInit * > ( init );
72          assert( sinit != 0);
73
74          std::list<Expression *> args;
75          args.push_back( new AddressExpr( new NameExpr( odecl->get_name() )) );    // have to get address of object
76          args.push_back( sinit->get_value() );
77          // replace declaration with initialization
78          stmts.push_back(new ExprStmt(std::list<Label>(), new UntypedExpr(new NameExpr("?=?"), args)));
79        }
80
81      delete init;
82      odecl->set_init( 0 );
83    } else {
84      // no declaration statement
85    }
86
87    return declStmt;
88  }
89
90  ExprStmt *assignFromDecl( DeclStmt *declStmt )
91  {
92    ObjectDecl *decl;
93    if ( (decl = dynamic_cast<ObjectDecl *>( declStmt->get_decl() )) != 0 )
94      {
95        SingleInit *init;
96        if ( (init = dynamic_cast<SingleInit *>(decl->get_init())) != 0 )
97          {
98          }
99      }
100
101    return 0;
102  }
103
104  bool isDeclStmtP(Statement *stmt)
105  {
106    return ( dynamic_cast< DeclStmt *>(stmt) != 0 );
107  }
108
109  BasicInit::Classify::TypeKind BasicInit::Classify::type( Type *toClassify ) {
110    if ( toClassify == 0 ) return NULL_T;
111
112    if ( dynamic_cast< StructInstType * >(toClassify) ||
113         dynamic_cast< UnionInstType * >(toClassify) ||
114         dynamic_cast< ArrayType * >(toClassify)         )
115      return COMPOUND_T;
116    else
117      return SINGLE_T;
118  }
119
120  BasicInit::Classify::InitKind BasicInit::Classify::initializer( Initializer *init) {
121    if ( init == 0 ) return NULL_I;
122    if ( dynamic_cast< ListInit * >(init) )
123      return COMPOUND_I;
124    if ( dynamic_cast< SingleInit * >(init) )
125      return SINGLE_I;
126
127    return NULL_I; // shouldn't be here anyways
128  }
129
130  NameInCollection *
131  BasicInit::Classify::declaration( ObjectDecl *objdecl, SymTab::Indexer *index ) {
132    assert ( index != 0 );
133
134    ReferenceToType *reftype;
135    if( (reftype = dynamic_cast< StructInstType * >( objdecl->get_type() )) != 0 ){
136      StructDecl *strDecl = index->lookupStruct( reftype->get_name() );
137      if ( strDecl != 0 ) {
138        NameCollectionBuilder bld;
139        NameCollector nc( bld );
140        strDecl->accept( nc );
141        NameInCollection *col = nc.get_builder().get_collection();
142        nc.get_builder().set_collection( 0 );
143
144        return col;
145      } else
146        throw( SemanticError( std::string("No structure of name: ") + reftype->get_name() ) );
147    } else {
148      throw(SemanticError( reftype->get_name() + std::string("is not a reference to type") ));
149      return 0;
150    }
151  }
152
153  std::list< Statement * >
154  BasicInit::Classify::matchInit( NameInCollection *col,
155                                  ObjectDecl *toInitialize,
156                                  Initializer *init ) {
157    assert ( col != 0 );
158
159    std::list< Statement * > arranged(0); //( col->size() );
160    std::fill( arranged.begin(), arranged.end(), (Statement *)0 );
161    int current = 0;
162
163    if ( init == 0 )
164      // no initializer... shouldn't even bother... fix this.
165      return arranged;
166
167    {
168      ListInit *li = dynamic_cast< ListInit * >( init );
169
170      if ( li != 0 ) {
171        for( std::list<Initializer *>::iterator i = li->begin_initializers();
172                                                         i != li->end_initializers();
173              i++) {
174          std::list<Expression *> d_orig = (*i)->get_designators();
175
176          NameInCollection *corrsp; // corresponding element to this initializer
177          if ( ! d_orig.empty() ) {
178            // 1) has designators
179            std::list<NameExpr *> des;
180            std::transform( d_orig.begin(), d_orig.end(),
181                            std::back_inserter( des ), cast_ptr<Expression, NameExpr > );
182
183            for( std::list<NameExpr *>::iterator j = des.begin(); j != des.end(); j++ ) {
184              // check for existence of the element
185
186              if ( (corrsp = (*col)[ (*j)->get_name() ]) != 0 ) {
187                // current++;
188                SingleInit *sinit;
189                if ( (sinit = dynamic_cast< SingleInit * >( *i )) != 0 )
190                  arranged.push_back( constructAssgn( corrsp->get_name(), toInitialize, sinit ) );
191                else
192                  ; // recursive call to matchInit
193              } else
194                // error, member doesn't exist
195                return arranged; // throw( 0 ); // XXX
196            }
197          } else {
198            // 2) doesn't have designators
199            if ( (corrsp = (*col)[ current++ ]) != 0 ) {
200              SingleInit *sinit;
201              if ( (sinit = dynamic_cast< SingleInit * >( *i )) != 0 )
202                arranged.push_back( constructAssgn( corrsp->get_name(), toInitialize, sinit ) );
203              else
204                ; // recursive call to matchInit
205            } else {
206              // Shouldn't be here... probably too many elements in initializer?
207            }
208          }
209        }
210      }
211    }
212
213    return arranged;
214  }
215
216  Statement *BasicInit::Classify::constructAssgn( std::string membName, ObjectDecl *toInit, SingleInit *sinit ) {
217    std::list< Expression * > args;
218    args.push_back(new AddressExpr( new UntypedMemberExpr( membName, new NameExpr(toInit->get_name()) )));
219    args.push_back( sinit->get_value() );
220    Statement *ret = new ExprStmt(std::list<Label>(),
221                                                    new UntypedExpr(new NameExpr("?=?"), args));
222    return ret;
223  }
224
225  void BasicInit::build_statements( NameAssociation< Expression *, BreakInitializer > *assoc,
226                                    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>(), 
257                                               new UntypedExpr(new NameExpr("?=?"), args)) );
258      }
259    }
260
261    return;
262  }
263
264} // namespace InitTweak
265
Note: See TracBrowser for help on using the repository browser.