Changeset cf16f94 for src/InitTweak


Ignore:
Timestamp:
Dec 15, 2015, 4:09:13 PM (9 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, with_gc
Children:
4389966
Parents:
b0b958a
Message:

create temporary return variable for return expressions, remove unnecessary code after temporary-return-variable change, fix missing lvalue qualifiers, change stream operator to |

Location:
src/InitTweak
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/InitTweak/RemoveInit.cc

    rb0b958a rcf16f94  
    77// RemoveInit.cc --
    88//
    9 // Author           : Rodolfo G. Esteves
     9// Author           : Rob Schluntz
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue May 19 16:39:32 2015
    13 // Update Count     : 1
     12// Last Modified On : Tue Dec 15 15:37:26 2015
     13// Update Count     : 15
    1414//
    1515
     
    2626                const std::list<Label> noLabels;
    2727        }
     28       
     29        class RemoveInit : public Mutator {
     30          public:
     31                RemoveInit();
     32                virtual ObjectDecl * mutate(ObjectDecl *objDecl);
     33                virtual DeclarationWithType * mutate( FunctionDecl *functionDecl );
     34
     35                virtual Statement * mutate( ReturnStmt * returnStmt );
     36               
     37                virtual CompoundStmt * mutate(CompoundStmt * compoundStmt);
     38               
     39          protected:
     40                std::list< Statement* > stmtsToAddBefore;
     41                std::list< Statement* > stmtsToAddAfter;
     42                void mutateStatementList( std::list< Statement* > &statements );
     43
     44                std::list<DeclarationWithType*> returnVals;
     45                UniqueName tempNamer;
     46                std::string funcName;
     47        };
    2848
    2949        void tweak( std::list< Declaration * > translationUnit ) {
     
    3252        }
    3353
     54        RemoveInit::RemoveInit() : tempNamer( "_retVal" ) {}
     55       
    3456        void RemoveInit::mutateStatementList( std::list< Statement* > &statements ) {
    3557                for ( std::list< Statement* >::iterator i = statements.begin(); i != statements.end(); ++i ) {
     
    3860                        } // if
    3961                        *i = (*i)->acceptMutator( *this );
     62                        if ( ! stmtsToAddBefore.empty() ) {
     63                                statements.splice( i, stmtsToAddBefore );
     64                        } // if
    4065                } // for
    4166                if ( ! stmtsToAddAfter.empty() ) {
     
    4974        }
    5075
    51 // in the case where an object has an initializer and a polymorphic type, insert an assignment
    52 // immediately after the declaration. This will (seemingly) cause the later phases to do the right
    53 // thing with the assignment
     76        // in the case where an object has an initializer and a polymorphic type, insert an assignment immediately after the
     77        // declaration. This will (seemingly) cause the later phases to do the right thing with the assignment
    5478        ObjectDecl *RemoveInit::mutate( ObjectDecl *objDecl ) {
    5579                if (objDecl->get_init() && dynamic_cast<TypeInstType*>(objDecl->get_type())) {
     
    6387                return objDecl;
    6488        }
     89
     90        Statement *RemoveInit::mutate( ReturnStmt *returnStmt ) {
     91                // update for multiple return values
     92                assert( returnVals.size() == 0 || returnVals.size() == 1 );
     93                // hands off if the function returns an lvalue - we don't want to allocate a temporary if a variable's address
     94                // is being returned
     95                if ( returnStmt->get_expr() && returnVals.size() == 1 && funcName != "?=?" && ! returnVals.front()->get_type()->get_isLvalue()  ) {
     96                        ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, returnVals.front()->get_type()->clone(), 0 );
     97                        stmtsToAddBefore.push_back( new DeclStmt( noLabels, newObj ) );
     98                       
     99                        UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) );
     100                        assign->get_args().push_back( new AddressExpr (new NameExpr( newObj->get_name() ) ) );
     101                        assign->get_args().push_back( returnStmt->get_expr() );
     102                        stmtsToAddBefore.push_back(new ExprStmt(noLabels, assign));
     103
     104                        returnStmt->set_expr( new VariableExpr( newObj ) );
     105                } // if
     106                return returnStmt;
     107        }
     108
     109        DeclarationWithType* RemoveInit::mutate( FunctionDecl *functionDecl ) {
     110                std::list<DeclarationWithType*> oldReturnVals = returnVals;
     111                std::string oldFuncName = funcName;
     112               
     113                FunctionType * type = functionDecl->get_functionType();
     114                returnVals = type->get_returnVals();
     115                funcName = functionDecl->get_name();
     116                DeclarationWithType * decl = Mutator::mutate( functionDecl );
     117                returnVals = oldReturnVals;
     118                funcName = oldFuncName;
     119                return decl;
     120        }
    65121} // namespace InitTweak
    66122
  • src/InitTweak/RemoveInit.h

    rb0b958a rcf16f94  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue May 19 16:40:11 2015
    13 // Update Count     : 1
     12// Last Modified On : Fri Nov 27 17:00:47 2015
     13// Update Count     : 2
    1414//
    1515
     
    2727        /// Adds assignment statements for polymorphic type initializers
    2828        void tweak( std::list< Declaration * > translationUnit );
    29 
    30         class RemoveInit : public Mutator {
    31           public:
    32                 // RemoveInit();
    33                 virtual ObjectDecl *mutate(ObjectDecl *objDecl);
    34                 virtual CompoundStmt *mutate(CompoundStmt *compoundStmt);
    35           protected:
    36                 std::list< Statement* > stmtsToAddAfter;
    37                 void mutateStatementList( std::list< Statement* > &statements );
    38         };
    3929} // namespace
    4030
Note: See TracChangeset for help on using the changeset viewer.