source: src/InitTweak/RemoveInit.cc @ 1e9d87b

aaron-thesisarm-ehcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationresolv-newwith_gc
Last change on this file since 1e9d87b was 1e9d87b, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

made RemoveInit? a PolyMutator?

  • Property mode set to 100644
File size: 3.8 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// RemoveInit.cc --
8//
9// Author           : Rob Schluntz
10// Created On       : Mon May 18 07:44:20 2015
11// Last Modified By : Rob Schluntz
12// Last Modified On : Thu Jan 07 11:34:33 2016
13// Update Count     : 23
14//
15
16#include "RemoveInit.h"
17#include "SynTree/Declaration.h"
18#include "SynTree/Type.h"
19#include "SynTree/Expression.h"
20#include "SynTree/Statement.h"
21#include "SynTree/Initializer.h"
22#include "SynTree/Mutator.h"
23#include "GenPoly/PolyMutator.h"
24
25namespace InitTweak {
26        namespace {
27                const std::list<Label> noLabels;
28        }
29
30        class RemoveInit : public GenPoly::PolyMutator {
31          public:
32                RemoveInit();
33                virtual ObjectDecl * mutate(ObjectDecl *objDecl);
34                virtual DeclarationWithType * mutate( FunctionDecl *functionDecl );
35
36                virtual Statement * mutate( ReturnStmt * returnStmt );
37
38                virtual CompoundStmt * mutate(CompoundStmt * compoundStmt);
39
40          protected:
41                std::list<DeclarationWithType*> returnVals;
42                UniqueName tempNamer;
43                std::string funcName;
44        };
45
46        void tweak( std::list< Declaration * > translationUnit ) {
47                RemoveInit remover;
48                mutateAll( translationUnit, remover );
49        }
50
51        RemoveInit::RemoveInit() : tempNamer( "_retVal" ) {}
52
53        CompoundStmt *RemoveInit::mutate(CompoundStmt *compoundStmt) {
54                mutateStatementList( compoundStmt->get_kids() );
55                return compoundStmt;
56        }
57
58        // in the case where an object has an initializer and a polymorphic type, insert an assignment immediately after the
59        // declaration. This will (seemingly) cause the later phases to do the right thing with the assignment
60        ObjectDecl *RemoveInit::mutate( ObjectDecl *objDecl ) {
61                if (objDecl->get_init() && dynamic_cast<TypeInstType*>(objDecl->get_type())) {
62                        if (SingleInit * single = dynamic_cast<SingleInit*>(objDecl->get_init())) {
63                                UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) );
64                                assign->get_args().push_back( new AddressExpr (new NameExpr( objDecl->get_name() ) ) );
65                                assign->get_args().push_back( single->get_value()->clone() );
66                                stmtsToAddAfter.push_back(new ExprStmt(noLabels, assign));
67                        } // if
68                } // if
69                return objDecl;
70        }
71
72        Statement *RemoveInit::mutate( ReturnStmt *returnStmt ) {
73                // update for multiple return values
74                assert( returnVals.size() == 0 || returnVals.size() == 1 );
75                // hands off if the function returns an lvalue - we don't want to allocate a temporary if a variable's address
76                // is being returned
77                if ( returnStmt->get_expr() && returnVals.size() == 1 && funcName != "?=?" && ! returnVals.front()->get_type()->get_isLvalue()  ) {
78                        ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, returnVals.front()->get_type()->clone(), 0 );
79                        stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) );
80
81                        UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) );
82                        assign->get_args().push_back( new AddressExpr (new NameExpr( newObj->get_name() ) ) );
83                        assign->get_args().push_back( returnStmt->get_expr() );
84                        stmtsToAdd.push_back(new ExprStmt(noLabels, assign));
85
86                        returnStmt->set_expr( new VariableExpr( newObj ) );
87                } // if
88                return returnStmt;
89        }
90
91        DeclarationWithType* RemoveInit::mutate( FunctionDecl *functionDecl ) {
92                std::list<DeclarationWithType*> oldReturnVals = returnVals;
93                std::string oldFuncName = funcName;
94
95                FunctionType * type = functionDecl->get_functionType();
96                returnVals = type->get_returnVals();
97                funcName = functionDecl->get_name();
98                DeclarationWithType * decl = Mutator::mutate( functionDecl );
99                returnVals = oldReturnVals;
100                funcName = oldFuncName;
101                return decl;
102        }
103} // namespace InitTweak
104
105// Local Variables: //
106// tab-width: 4 //
107// mode: c++ //
108// compile-command: "make install" //
109// End: //
Note: See TracBrowser for help on using the repository browser.