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 |
|
---|
29 | namespace 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 initializer and generate appropriate copy ctor if element has initializer
|
---|
148 | std::list< Expression * > args = makeInitList( objDecl->get_init() );
|
---|
149 | if ( args.empty() ) {
|
---|
150 | std::list< Statement * > ctor;
|
---|
151 | std::list< Statement * > dtor;
|
---|
152 |
|
---|
153 | SymTab::makeArrayFunction( NULL, new VariableExpr( objDecl ), at, "?{}", back_inserter( ctor ) );
|
---|
154 | SymTab::makeArrayFunction( NULL, new VariableExpr( objDecl ), at, "^?{}", front_inserter( dtor ), false );
|
---|
155 |
|
---|
156 | // Currently makeArrayFunction produces a single Statement - a CompoundStmt
|
---|
157 | // which wraps everything that needs to happen. As such, it's technically
|
---|
158 | // possible to use a Statement ** in the above calls, but this is inherently
|
---|
159 | // unsafe, so instead we take the slightly less efficient route, but will be
|
---|
160 | // immediately informed if somehow the above assumption is broken. In this case,
|
---|
161 | // we could always wrap the list of statements at this point with a CompoundStmt,
|
---|
162 | // but it seems reasonable at the moment for this to be done by makeArrayFunction
|
---|
163 | // itself
|
---|
164 | assert( ctor.size() == 1 );
|
---|
165 | assert( dtor.size() == 1 );
|
---|
166 | objDecl->set_init( new ConstructorInit( new ImplicitCtorDtorStmt( ctor.front() ), new ImplicitCtorDtorStmt( dtor.front() ), objDecl->get_init() ) );
|
---|
167 | } else {
|
---|
168 | // array came with an initializer list: initialize each element
|
---|
169 | // may have more initializers than elements in the array - need to check at each index that
|
---|
170 | // we haven't exceeded size. This requires precomputing the size because it might be a side-effecting
|
---|
171 | // computation.
|
---|
172 | // may have fewer initializers than eleemnts in the array - need to default construct
|
---|
173 | // remaining elements.
|
---|
174 | // might be able to merge this with the case above.
|
---|
175 | }
|
---|
176 | } else {
|
---|
177 | // it's sufficient to attempt to call the ctor/dtor for the given object and its initializer
|
---|
178 | Expression * ctor = makeCtorDtorExpr( "?{}", objDecl, makeInitList( objDecl->get_init() ) );
|
---|
179 | Expression * dtor = makeCtorDtorExpr( "^?{}", objDecl, std::list< Expression * >() );
|
---|
180 |
|
---|
181 | // need to remember init expression, in case no ctors exist
|
---|
182 | // if ctor does exist, want to use ctor expression instead of init
|
---|
183 | // push this decision to the resolver
|
---|
184 | ExprStmt * ctorStmt = new ExprStmt( noLabels, ctor );
|
---|
185 | ExprStmt * dtorStmt = new ExprStmt( noLabels, dtor );
|
---|
186 | objDecl->set_init( new ConstructorInit( new ImplicitCtorDtorStmt( ctorStmt ), new ImplicitCtorDtorStmt( dtorStmt ), objDecl->get_init() ) );
|
---|
187 | }
|
---|
188 | }
|
---|
189 | }
|
---|
190 | return Mutator::mutate( objDecl );
|
---|
191 | }
|
---|
192 |
|
---|
193 | DeclarationWithType * CtorDtor::mutate( FunctionDecl *functionDecl ) {
|
---|
194 | // parameters should not be constructed and destructed, so don't mutate FunctionType
|
---|
195 | bool oldInFunc = inFunction;
|
---|
196 | mutateAll( functionDecl->get_oldDecls(), *this );
|
---|
197 | inFunction = true;
|
---|
198 | functionDecl->set_statements( maybeMutate( functionDecl->get_statements(), *this ) );
|
---|
199 | inFunction = oldInFunc;
|
---|
200 | return functionDecl;
|
---|
201 | }
|
---|
202 |
|
---|
203 | // should not traverse into any of these declarations to find objects
|
---|
204 | // that need to be constructed or destructed
|
---|
205 | Declaration* CtorDtor::mutate( StructDecl *aggregateDecl ) { return aggregateDecl; }
|
---|
206 | Declaration* CtorDtor::mutate( UnionDecl *aggregateDecl ) { return aggregateDecl; }
|
---|
207 | Declaration* CtorDtor::mutate( EnumDecl *aggregateDecl ) { return aggregateDecl; }
|
---|
208 | Declaration* CtorDtor::mutate( TraitDecl *aggregateDecl ) { return aggregateDecl; }
|
---|
209 | TypeDecl* CtorDtor::mutate( TypeDecl *typeDecl ) { return typeDecl; }
|
---|
210 | Declaration* CtorDtor::mutate( TypedefDecl *typeDecl ) { return typeDecl; }
|
---|
211 | Type* CtorDtor::mutate( FunctionType *funcType ) { return funcType; }
|
---|
212 |
|
---|
213 | } // namespace InitTweak
|
---|
214 |
|
---|
215 | // Local Variables: //
|
---|
216 | // tab-width: 4 //
|
---|
217 | // mode: c++ //
|
---|
218 | // compile-command: "make install" //
|
---|
219 | // End: //
|
---|