source: translator/InitTweak/RemoveInit.cc @ b87a5ed

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 b87a5ed was 42e2ad7, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

fixed initialization of stack-allocated polymorphic variables

  • Property mode set to 100644
File size: 1.7 KB
Line 
1#include "RemoveInit.h"
2#include "SynTree/Declaration.h"
3#include "SynTree/Type.h"
4#include "SynTree/Expression.h"
5#include "SynTree/Statement.h"
6#include "SynTree/Initializer.h"
7#include "SynTree/Mutator.h"
8
9namespace InitTweak {
10
11namespace {
12const std::list<Label> noLabels;
13}
14
15void tweak( std::list< Declaration * > translationUnit ) {
16  RemoveInit remover;
17  mutateAll( translationUnit, remover );
18}
19
20void RemoveInit::mutateStatementList( std::list< Statement* > &statements ) {
21  for( std::list< Statement* >::iterator i = statements.begin(); i != statements.end(); ++i ) {
22    if( !stmtsToAddAfter.empty() ) {
23      statements.splice( i, stmtsToAddAfter );
24    }
25    *i = (*i)->acceptMutator( *this );
26  }
27  if( !stmtsToAddAfter.empty() ) {
28    statements.splice( statements.end(), stmtsToAddAfter );
29  }
30}
31
32CompoundStmt *RemoveInit::mutate(CompoundStmt *compoundStmt) {
33  mutateStatementList( compoundStmt->get_kids() );
34  return compoundStmt;
35}
36
37// in the case where an object has an initializer and a polymorphic type, insert an assignment
38// immediately after the declaration. This will (seemingly) cause the later phases to do the right
39// thing with the assignment
40ObjectDecl *RemoveInit::mutate( ObjectDecl *objDecl ) {
41  if (objDecl->get_init() && dynamic_cast<TypeInstType*>(objDecl->get_type())) {
42    if (SingleInit * single = dynamic_cast<SingleInit*>(objDecl->get_init())) {
43      UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) );
44      assign->get_args().push_back( new AddressExpr (new NameExpr( objDecl->get_name() ) ) );
45      assign->get_args().push_back( single->get_value()->clone() );
46      stmtsToAddAfter.push_back(new ExprStmt(noLabels, assign));
47    }
48  }
49  return objDecl;
50}
51} // namespace InitTweak
52
Note: See TracBrowser for help on using the repository browser.