source: src/InitTweak/GenInit.cc@ 4d2434a

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new stuck-waitfor-destruct with_gc
Last change on this file since 4d2434a was 4d2434a, checked in by Rob Schluntz <rschlunt@…>, 10 years ago

major reorganization of constructor generation from initializer list so that it now works with multi-dimensional arrays

  • Property mode set to 100644
File size: 10.8 KB
RevLine 
[51587aa]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//
[a0fdbd5]7// GenInit.cc --
[51587aa]8//
[cf16f94]9// Author : Rob Schluntz
[51587aa]10// Created On : Mon May 18 07:44:20 2015
[1e9d87b]11// Last Modified By : Rob Schluntz
[7b3f66b]12// Last Modified On : Fri May 13 11:37:48 2016
[d63eeb0]13// Update Count : 166
[51587aa]14//
[a08ba92]15
[974906e2]16#include <stack>
17#include <list>
[a0fdbd5]18#include "GenInit.h"
[7b3f66b]19#include "InitTweak.h"
[42e2ad7]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"
[620cb95]26#include "SymTab/Autogen.h"
[1e9d87b]27#include "GenPoly/PolyMutator.h"
[5f98ce5]28#include "GenPoly/DeclMutator.h"
[42e2ad7]29
30namespace InitTweak {
[a08ba92]31 namespace {
32 const std::list<Label> noLabels;
[5b40f30]33 const std::list<Expression *> noDesignators;
[a08ba92]34 }
[1e9d87b]35
[a0fdbd5]36 class ReturnFixer : public GenPoly::PolyMutator {
[cf16f94]37 public:
[a0fdbd5]38 /// consistently allocates a temporary variable for the return value
39 /// of a function so that anything which the resolver decides can be constructed
[02c7d04]40 /// into the return type of a function can be returned.
[a0fdbd5]41 static void makeReturnTemp( std::list< Declaration * > &translationUnit );
[02c7d04]42
[a0fdbd5]43 ReturnFixer();
[db4ecc5]44
[cf16f94]45 virtual DeclarationWithType * mutate( FunctionDecl *functionDecl );
46
47 virtual Statement * mutate( ReturnStmt * returnStmt );
[1e9d87b]48
49 protected:
[cf16f94]50 std::list<DeclarationWithType*> returnVals;
51 UniqueName tempNamer;
52 std::string funcName;
53 };
[42e2ad7]54
[974906e2]55 class CtorDtor : public GenPoly::PolyMutator {
56 public:
[02c7d04]57 /// create constructor and destructor statements for object declarations.
[5f98ce5]58 /// the actual call statements will be added in after the resolver has run
59 /// so that the initializer expression is only removed if a constructor is found
60 /// and the same destructor call is inserted in all of the appropriate locations.
[02c7d04]61 static void generateCtorDtor( std::list< Declaration * > &translationUnit );
[974906e2]62
[db4ecc5]63 virtual DeclarationWithType * mutate( ObjectDecl * );
[02c7d04]64 virtual DeclarationWithType * mutate( FunctionDecl *functionDecl );
[5f98ce5]65 // should not traverse into any of these declarations to find objects
66 // that need to be constructed or destructed
67 virtual Declaration* mutate( StructDecl *aggregateDecl ) { return aggregateDecl; }
68 virtual Declaration* mutate( UnionDecl *aggregateDecl ) { return aggregateDecl; }
69 virtual Declaration* mutate( EnumDecl *aggregateDecl ) { return aggregateDecl; }
70 virtual Declaration* mutate( TraitDecl *aggregateDecl ) { return aggregateDecl; }
71 virtual TypeDecl* mutate( TypeDecl *typeDecl ) { return typeDecl; }
72 virtual Declaration* mutate( TypedefDecl *typeDecl ) { return typeDecl; }
[974906e2]73
[5f98ce5]74 virtual Type * mutate( FunctionType *funcType ) { return funcType; }
[db4ecc5]75
[974906e2]76 protected:
77 };
78
[5f98ce5]79 class HoistArrayDimension : public GenPoly::DeclMutator {
80 public:
81 typedef GenPoly::DeclMutator Parent;
82
83 /// hoist dimension from array types in object declaration so that it uses a single
84 /// const variable of type size_t, so that side effecting array dimensions are only
85 /// computed once.
86 static void hoistArrayDimension( std::list< Declaration * > & translationUnit );
87
88 private:
[40e636a]89 virtual DeclarationWithType * mutate( ObjectDecl * objectDecl );
90 virtual DeclarationWithType * mutate( FunctionDecl *functionDecl );
[5f98ce5]91 // should not traverse into any of these declarations to find objects
92 // that need to be constructed or destructed
93 virtual Declaration* mutate( StructDecl *aggregateDecl ) { return aggregateDecl; }
94 virtual Declaration* mutate( UnionDecl *aggregateDecl ) { return aggregateDecl; }
95 virtual Declaration* mutate( EnumDecl *aggregateDecl ) { return aggregateDecl; }
96 virtual Declaration* mutate( TraitDecl *aggregateDecl ) { return aggregateDecl; }
97 virtual TypeDecl* mutate( TypeDecl *typeDecl ) { return typeDecl; }
98 virtual Declaration* mutate( TypedefDecl *typeDecl ) { return typeDecl; }
99
100 virtual Type* mutate( FunctionType *funcType ) { return funcType; }
101
102 void hoist( Type * type );
103
104 DeclarationNode::StorageClass storageclass = DeclarationNode::NoStorageClass;
[40e636a]105 bool inFunction = false;
[5f98ce5]106 };
107
[a0fdbd5]108 void genInit( std::list< Declaration * > & translationUnit ) {
109 ReturnFixer::makeReturnTemp( translationUnit );
[5f98ce5]110 HoistArrayDimension::hoistArrayDimension( translationUnit );
[02c7d04]111 CtorDtor::generateCtorDtor( translationUnit );
112 }
113
[a0fdbd5]114 void ReturnFixer::makeReturnTemp( std::list< Declaration * > & translationUnit ) {
115 ReturnFixer fixer;
116 mutateAll( translationUnit, fixer );
[a08ba92]117 }
[42e2ad7]118
[a0fdbd5]119 ReturnFixer::ReturnFixer() : tempNamer( "_retVal" ) {}
[cf16f94]120
[a0fdbd5]121 Statement *ReturnFixer::mutate( ReturnStmt *returnStmt ) {
[cf16f94]122 // update for multiple return values
123 assert( returnVals.size() == 0 || returnVals.size() == 1 );
124 // hands off if the function returns an lvalue - we don't want to allocate a temporary if a variable's address
125 // is being returned
[7eabc25]126 if ( returnStmt->get_expr() && returnVals.size() == 1 && funcName != "?=?" && ! returnVals.front()->get_type()->get_isLvalue() ) {
[5b40f30]127 // ensure return value is not destructed by explicitly creating
128 // an empty SingleInit node wherein maybeConstruct is false
129 ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, returnVals.front()->get_type()->clone(), new ListInit( std::list<Initializer*>(), noDesignators, false ) );
[1e9d87b]130 stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) );
131
[5b40f30]132 // and explicitly create the constructor expression separately
133 UntypedExpr *construct = new UntypedExpr( new NameExpr( "?{}" ) );
[7eabc25]134 construct->get_args().push_back( new AddressExpr( new VariableExpr( newObj ) ) );
[5b40f30]135 construct->get_args().push_back( returnStmt->get_expr() );
136 stmtsToAdd.push_back(new ExprStmt(noLabels, construct));
[cf16f94]137
138 returnStmt->set_expr( new VariableExpr( newObj ) );
139 } // if
140 return returnStmt;
141 }
142
[a0fdbd5]143 DeclarationWithType* ReturnFixer::mutate( FunctionDecl *functionDecl ) {
[cf16f94]144 std::list<DeclarationWithType*> oldReturnVals = returnVals;
145 std::string oldFuncName = funcName;
[1e9d87b]146
[cf16f94]147 FunctionType * type = functionDecl->get_functionType();
148 returnVals = type->get_returnVals();
149 funcName = functionDecl->get_name();
150 DeclarationWithType * decl = Mutator::mutate( functionDecl );
151 returnVals = oldReturnVals;
152 funcName = oldFuncName;
153 return decl;
154 }
[974906e2]155
[4d2434a]156 // precompute array dimension expression, because constructor generation may duplicate it,
157 // which would be incorrect if it is a side-effecting computation.
[5f98ce5]158 void HoistArrayDimension::hoistArrayDimension( std::list< Declaration * > & translationUnit ) {
159 HoistArrayDimension hoister;
[40e636a]160 hoister.mutateDeclarationList( translationUnit );
[5f98ce5]161 }
162
163 DeclarationWithType * HoistArrayDimension::mutate( ObjectDecl * objectDecl ) {
164 storageclass = objectDecl->get_storageClass();
165 DeclarationWithType * temp = Parent::mutate( objectDecl );
166 hoist( objectDecl->get_type() );
167 storageclass = DeclarationNode::NoStorageClass;
168 return temp;
169 }
170
171 void HoistArrayDimension::hoist( Type * type ) {
[40e636a]172 // if in function, generate const size_t var
[5f98ce5]173 static UniqueName dimensionName( "_array_dim" );
174 if ( ArrayType * arrayType = dynamic_cast< ArrayType * >( type ) ) {
[40e636a]175 if ( ! inFunction ) return;
176
[5f98ce5]177 if ( ! arrayType->get_dimension() ) return; // xxx - recursive call to hoist?
178
179 // don't need to hoist dimension if it's a constexpr - only need to if there's potential
180 // for side effects.
181 if ( isConstExpr( arrayType->get_dimension() ) ) return;
182
183 ObjectDecl * arrayDimension = new ObjectDecl( dimensionName.newName(), storageclass, LinkageSpec::C, 0, SymTab::SizeType->clone(), new SingleInit( arrayType->get_dimension() ) );
184 arrayDimension->get_type()->set_isConst( true );
185
186 arrayType->set_dimension( new VariableExpr( arrayDimension ) );
187 addDeclaration( arrayDimension );
188
189 hoist( arrayType->get_base() );
190 return;
191 }
192 }
[974906e2]193
[40e636a]194 DeclarationWithType * HoistArrayDimension::mutate( FunctionDecl *functionDecl ) {
195 bool oldInFunc = inFunction;
196 inFunction = true;
197 DeclarationWithType * decl = Parent::mutate( functionDecl );
198 inFunction = oldInFunc;
199 return decl;
200 }
201
[02c7d04]202 void CtorDtor::generateCtorDtor( std::list< Declaration * > & translationUnit ) {
203 CtorDtor ctordtor;
204 mutateAll( translationUnit, ctordtor );
[974906e2]205 }
206
[02c7d04]207 namespace {
[71f4e4f]208 Expression * makeCtorDtorExpr( std::string name, ObjectDecl * objDecl, std::list< Expression * > args ) {
[02c7d04]209 UntypedExpr * expr = new UntypedExpr( new NameExpr( name ) );
210 expr->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) );
211 expr->get_args().splice( expr->get_args().end(), args );
[71f4e4f]212 return expr;
[02c7d04]213 }
[974906e2]214 }
215
[db4ecc5]216 DeclarationWithType * CtorDtor::mutate( ObjectDecl * objDecl ) {
[6cf27a07]217 // hands off if designated, if @=, or if extern
[974906e2]218 if ( tryConstruct( objDecl ) ) {
[4d2434a]219 // call into genImplicitCall from Autogen.h to generate calls to ctor/dtor
220 // for each constructable object
221 std::list< Statement * > ctor;
222 std::list< Statement * > dtor;
223
224 InitExpander srcParam( objDecl->get_init() );
225 InitExpander nullParam( (Initializer *)NULL );
226 SymTab::genImplicitCall( srcParam, new VariableExpr( objDecl ), "?{}", back_inserter( ctor ), objDecl );
227 SymTab::genImplicitCall( nullParam, new VariableExpr( objDecl ), "^?{}", front_inserter( dtor ), objDecl, false );
228
229 // Currently genImplicitCall produces a single Statement - a CompoundStmt
230 // which wraps everything that needs to happen. As such, it's technically
231 // possible to use a Statement ** in the above calls, but this is inherently
232 // unsafe, so instead we take the slightly less efficient route, but will be
233 // immediately informed if somehow the above assumption is broken. In this case,
234 // we could always wrap the list of statements at this point with a CompoundStmt,
235 // but it seems reasonable at the moment for this to be done by genImplicitCall
236 // itself. It is possible that genImplicitCall produces no statements (e.g. if
237 // an array type does not have a dimension). In this case, it's fine to ignore
238 // the object for the purposes of construction.
239 assert( ctor.size() == dtor.size() && ctor.size() <= 1 );
240 if ( ctor.size() == 1 ) {
[6cf27a07]241 // need to remember init expression, in case no ctors exist
242 // if ctor does exist, want to use ctor expression instead of init
243 // push this decision to the resolver
[4d2434a]244 assert( dynamic_cast< ImplicitCtorDtorStmt * > ( ctor.front() ) && dynamic_cast< ImplicitCtorDtorStmt * > ( dtor.front() ) );
245 objDecl->set_init( new ConstructorInit( ctor.front(), dtor.front(), objDecl->get_init() ) );
[f1e012b]246 }
[974906e2]247 }
[db4ecc5]248 return Mutator::mutate( objDecl );
[974906e2]249 }
250
[02c7d04]251 DeclarationWithType * CtorDtor::mutate( FunctionDecl *functionDecl ) {
252 // parameters should not be constructed and destructed, so don't mutate FunctionType
253 mutateAll( functionDecl->get_oldDecls(), *this );
254 functionDecl->set_statements( maybeMutate( functionDecl->get_statements(), *this ) );
255 return functionDecl;
256 }
[42e2ad7]257} // namespace InitTweak
258
[51587aa]259// Local Variables: //
260// tab-width: 4 //
261// mode: c++ //
262// compile-command: "make install" //
263// End: //
Note: See TracBrowser for help on using the repository browser.