source: translator/InitTweak/BasicInit.cc @ 51587aa

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 51587aa was 51587aa, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

licencing: fourth groups of files

  • Property mode set to 100644
File size: 8.4 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// XXX.cc --
8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
11// Last Modified By :
12// Last Modified On :
13// Update Count     : 0
14//
15#include <list>
16#include <cassert>
17#include <iostream>
18#include <iterator>
19#include <algorithm>
20
21#include "utility.h"
22
23#include "SynTree/Type.h"
24#include "SynTree/Statement.h"
25#include "SynTree/Expression.h"
26#include "SynTree/Declaration.h"
27#include "SynTree/Initializer.h"
28
29#include "BasicInit.h"
30#include "NameCollector.h"
31#include "NameAssociation.h"
32
33namespace 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 ) );
205                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
276} // namespace InitTweak
277
278// Local Variables: //
279// tab-width: 4 //
280// mode: c++ //
281// compile-command: "make install" //
282// End: //
Note: See TracBrowser for help on using the repository browser.