source: src/InitTweak/GenInit.cc @ 2be1023

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

rework ctor/dtor generation to work properly with multidimensional arrays

  • Property mode set to 100644
File size: 9.2 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// GenInit.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 : Fri May 13 11:37:48 2016
13// Update Count     : 166
14//
15
16#include <stack>
17#include <list>
18#include "GenInit.h"
19#include "InitTweak.h"
20#include "SynTree/Declaration.h"
21#include "SynTree/Type.h"
22#include "SynTree/Expression.h"
23#include "SynTree/Statement.h"
24#include "SynTree/Initializer.h"
25#include "SynTree/Mutator.h"
26#include "SymTab/Autogen.h"
27#include "GenPoly/PolyMutator.h"
28
29namespace InitTweak {
30        namespace {
31                const std::list<Label> noLabels;
32                const std::list<Expression *> noDesignators;
33        }
34
35        class ReturnFixer : public GenPoly::PolyMutator {
36          public:
37                /// consistently allocates a temporary variable for the return value
38                /// of a function so that anything which the resolver decides can be constructed
39                /// into the return type of a function can be returned.
40                static void makeReturnTemp( std::list< Declaration * > &translationUnit );
41
42                ReturnFixer();
43
44                virtual DeclarationWithType * mutate( FunctionDecl *functionDecl );
45
46                virtual Statement * mutate( ReturnStmt * returnStmt );
47
48          protected:
49                std::list<DeclarationWithType*> returnVals;
50                UniqueName tempNamer;
51                std::string funcName;
52        };
53
54        class CtorDtor : public GenPoly::PolyMutator {
55          public:
56                /// create constructor and destructor statements for object declarations.
57                /// Destructors are inserted directly into the code, whereas constructors
58                /// will be added in after the resolver has run so that the initializer expression
59                /// is only removed if a constructor is found
60                static void generateCtorDtor( std::list< Declaration * > &translationUnit );
61
62                CtorDtor() : inFunction( false ) {}
63
64                virtual DeclarationWithType * mutate( ObjectDecl * );
65                virtual DeclarationWithType * mutate( FunctionDecl *functionDecl );
66                virtual Declaration* mutate( StructDecl *aggregateDecl );
67                virtual Declaration* mutate( UnionDecl *aggregateDecl );
68                virtual Declaration* mutate( EnumDecl *aggregateDecl );
69                virtual Declaration* mutate( TraitDecl *aggregateDecl );
70                virtual TypeDecl* mutate( TypeDecl *typeDecl );
71                virtual Declaration* mutate( TypedefDecl *typeDecl );
72
73                virtual Type * mutate( FunctionType *funcType );
74
75          protected:
76                bool inFunction;
77        };
78
79        void genInit( std::list< Declaration * > & translationUnit ) {
80                ReturnFixer::makeReturnTemp( translationUnit );
81                CtorDtor::generateCtorDtor( translationUnit );
82        }
83
84        void ReturnFixer::makeReturnTemp( std::list< Declaration * > & translationUnit ) {
85                ReturnFixer fixer;
86                mutateAll( translationUnit, fixer );
87        }
88
89        ReturnFixer::ReturnFixer() : tempNamer( "_retVal" ) {}
90
91        Statement *ReturnFixer::mutate( ReturnStmt *returnStmt ) {
92                // update for multiple return values
93                assert( returnVals.size() == 0 || returnVals.size() == 1 );
94                // hands off if the function returns an lvalue - we don't want to allocate a temporary if a variable's address
95                // is being returned
96                if ( returnStmt->get_expr() && returnVals.size() == 1 && funcName != "?=?" && ! returnVals.front()->get_type()->get_isLvalue() ) {
97                        // ensure return value is not destructed by explicitly creating
98                        // an empty SingleInit node wherein maybeConstruct is false
99                        ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, returnVals.front()->get_type()->clone(), new ListInit( std::list<Initializer*>(), noDesignators, false ) );
100                        stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) );
101
102                        // and explicitly create the constructor expression separately
103                        UntypedExpr *construct = new UntypedExpr( new NameExpr( "?{}" ) );
104                        construct->get_args().push_back( new AddressExpr( new VariableExpr( newObj ) ) );
105                        construct->get_args().push_back( returnStmt->get_expr() );
106                        stmtsToAdd.push_back(new ExprStmt(noLabels, construct));
107
108                        returnStmt->set_expr( new VariableExpr( newObj ) );
109                } // if
110                return returnStmt;
111        }
112
113        DeclarationWithType* ReturnFixer::mutate( FunctionDecl *functionDecl ) {
114                std::list<DeclarationWithType*> oldReturnVals = returnVals;
115                std::string oldFuncName = funcName;
116
117                FunctionType * type = functionDecl->get_functionType();
118                returnVals = type->get_returnVals();
119                funcName = functionDecl->get_name();
120                DeclarationWithType * decl = Mutator::mutate( functionDecl );
121                returnVals = oldReturnVals;
122                funcName = oldFuncName;
123                return decl;
124        }
125
126
127        void CtorDtor::generateCtorDtor( std::list< Declaration * > & translationUnit ) {
128                CtorDtor ctordtor;
129                mutateAll( translationUnit, ctordtor );
130        }
131
132        namespace {
133                Expression * makeCtorDtorExpr( std::string name, ObjectDecl * objDecl, std::list< Expression * > args ) {
134                        UntypedExpr * expr = new UntypedExpr( new NameExpr( name ) );
135                        expr->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) );
136                        expr->get_args().splice( expr->get_args().end(), args );
137                        return expr;
138                }
139        }
140
141        DeclarationWithType * CtorDtor::mutate( ObjectDecl * objDecl ) {
142                // hands off if designated or if @=
143                if ( tryConstruct( objDecl ) ) {
144                        if ( inFunction ) {
145                                if ( ArrayType * at = dynamic_cast< ArrayType * >( objDecl->get_type() ) ) {
146                                        // call into makeArrayFunction from validate.cc to generate calls to ctor/dtor for each element of array
147                                        // TODO: walk initializers and generate appropriate ctor if element has initializer.
148                                        // Initializer could be nested (depends on the depth of the array type on the object)
149
150                                        std::list< Expression * > args = makeInitList( objDecl->get_init() );
151                                        if ( args.empty() ) {
152                                                std::list< Statement * > ctor;
153                                                std::list< Statement * > dtor;
154
155                                                SymTab::genImplicitCall( NULL, new VariableExpr( objDecl ), "?{}", back_inserter( ctor ), objDecl );
156                                                SymTab::genImplicitCall( NULL, new VariableExpr( objDecl ), "^?{}", front_inserter( dtor ), objDecl, false );
157
158                                                // Currently genArrayCall produces a single Statement - a CompoundStmt
159                                                // which  wraps everything that needs to happen. As such, it's technically
160                                                // possible to use a Statement ** in the above calls, but this is inherently
161                                                // unsafe, so instead we take the slightly less efficient route, but will be
162                                                // immediately informed if somehow the above assumption is broken. In this case,
163                                                // we could always wrap the list of statements at this point with a CompoundStmt,
164                                                // but it seems reasonable at the moment for this to be done by genArrayCall
165                                                // itself
166                                                assert( ctor.size() == 1 && dynamic_cast< ImplicitCtorDtorStmt * >( ctor.front() ) );
167                                                assert( dtor.size() == 1 && dynamic_cast< ImplicitCtorDtorStmt * >( dtor.front() ) );
168                                                objDecl->set_init( new ConstructorInit( ctor.front(), dtor.front(), objDecl->get_init() ) );
169                                        } else {
170                                                // array came with an initializer list: initialize each element
171                                                // may have more initializers than elements in the array - need to check at each index that
172                                                // we haven't exceeded size. This requires precomputing the size because it might be a side-effecting
173                                                // computation.
174                                                // may have fewer initializers than elements in the array - need to default construct
175                                                // remaining elements.
176                                                // might be able to merge this with the case above.
177
178                                        }
179                                } else {
180                                        // it's sufficient to attempt to call the ctor/dtor for the given object and its initializer
181                                        Expression * ctor = makeCtorDtorExpr( "?{}", objDecl, makeInitList( objDecl->get_init() ) );
182                                        Expression * dtor = makeCtorDtorExpr( "^?{}", objDecl, std::list< Expression * >() );
183
184                                        // need to remember init expression, in case no ctors exist
185                                        // if ctor does exist, want to use ctor expression instead of init
186                                        // push this decision to the resolver
187                                        ExprStmt * ctorStmt = new ExprStmt( noLabels, ctor );
188                                        ExprStmt * dtorStmt = new ExprStmt( noLabels, dtor );
189                                        objDecl->set_init( new ConstructorInit( new ImplicitCtorDtorStmt( ctorStmt ), new ImplicitCtorDtorStmt( dtorStmt ), objDecl->get_init() ) );
190                                }
191                        }
192                }
193                return Mutator::mutate( objDecl );
194        }
195
196        DeclarationWithType * CtorDtor::mutate( FunctionDecl *functionDecl ) {
197                // parameters should not be constructed and destructed, so don't mutate FunctionType
198                bool oldInFunc = inFunction;
199                mutateAll( functionDecl->get_oldDecls(), *this );
200                inFunction = true;
201                functionDecl->set_statements( maybeMutate( functionDecl->get_statements(), *this ) );
202                inFunction = oldInFunc;
203                return functionDecl;
204        }
205
206        // should not traverse into any of these declarations to find objects
207        // that need to be constructed or destructed
208        Declaration* CtorDtor::mutate( StructDecl *aggregateDecl ) { return aggregateDecl; }
209        Declaration* CtorDtor::mutate( UnionDecl *aggregateDecl ) { return aggregateDecl; }
210        Declaration* CtorDtor::mutate( EnumDecl *aggregateDecl ) { return aggregateDecl; }
211        Declaration* CtorDtor::mutate( TraitDecl *aggregateDecl ) { return aggregateDecl; }
212        TypeDecl* CtorDtor::mutate( TypeDecl *typeDecl ) { return typeDecl; }
213        Declaration* CtorDtor::mutate( TypedefDecl *typeDecl ) { return typeDecl; }
214        Type* CtorDtor::mutate( FunctionType *funcType ) { return funcType; }
215
216} // namespace InitTweak
217
218// Local Variables: //
219// tab-width: 4 //
220// mode: c++ //
221// compile-command: "make install" //
222// End: //
Note: See TracBrowser for help on using the repository browser.