| 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 |
|
|---|
| 9 | namespace InitTweak {
|
|---|
| 10 |
|
|---|
| 11 | namespace {
|
|---|
| 12 | const std::list<Label> noLabels;
|
|---|
| 13 | }
|
|---|
| 14 |
|
|---|
| 15 | void tweak( std::list< Declaration * > translationUnit ) {
|
|---|
| 16 | RemoveInit remover;
|
|---|
| 17 | mutateAll( translationUnit, remover );
|
|---|
| 18 | }
|
|---|
| 19 |
|
|---|
| 20 | void 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 |
|
|---|
| 32 | CompoundStmt *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
|
|---|
| 40 | ObjectDecl *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 |
|
|---|