source: src/InitTweak/GenInit.cc@ 32b018e

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox 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 32b018e was 62e5546, checked in by Thierry Delisle <tdelisle@…>, 9 years ago

Removed warnings when compiling with clang

  • Property mode set to 100644
File size: 14.0 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"
[1ba88a0]27#include "SymTab/Mangler.h"
[1e9d87b]28#include "GenPoly/PolyMutator.h"
[5f98ce5]29#include "GenPoly/DeclMutator.h"
[1ba88a0]30#include "GenPoly/ScopedSet.h"
[42e2ad7]31
32namespace InitTweak {
[a08ba92]33 namespace {
34 const std::list<Label> noLabels;
[5b40f30]35 const std::list<Expression *> noDesignators;
[a08ba92]36 }
[1e9d87b]37
[62e5546]38 class ReturnFixer final : public GenPoly::PolyMutator {
[cf16f94]39 public:
[a0fdbd5]40 /// consistently allocates a temporary variable for the return value
41 /// of a function so that anything which the resolver decides can be constructed
[02c7d04]42 /// into the return type of a function can be returned.
[a0fdbd5]43 static void makeReturnTemp( std::list< Declaration * > &translationUnit );
[02c7d04]44
[a0fdbd5]45 ReturnFixer();
[db4ecc5]46
[62e5546]47 using GenPoly::PolyMutator::mutate;
48 virtual DeclarationWithType * mutate( FunctionDecl *functionDecl ) override;
49 virtual Statement * mutate( ReturnStmt * returnStmt ) override;
[1e9d87b]50
51 protected:
[cf16f94]52 std::list<DeclarationWithType*> returnVals;
53 UniqueName tempNamer;
54 std::string funcName;
55 };
[42e2ad7]56
[62e5546]57 class CtorDtor final : public GenPoly::PolyMutator {
[974906e2]58 public:
[1ba88a0]59 typedef GenPoly::PolyMutator Parent;
60 using Parent::mutate;
[02c7d04]61 /// create constructor and destructor statements for object declarations.
[5f98ce5]62 /// the actual call statements will be added in after the resolver has run
63 /// so that the initializer expression is only removed if a constructor is found
64 /// and the same destructor call is inserted in all of the appropriate locations.
[02c7d04]65 static void generateCtorDtor( std::list< Declaration * > &translationUnit );
[974906e2]66
[62e5546]67 virtual DeclarationWithType * mutate( ObjectDecl * ) override;
68 virtual DeclarationWithType * mutate( FunctionDecl *functionDecl ) override;
[5f98ce5]69 // should not traverse into any of these declarations to find objects
70 // that need to be constructed or destructed
[62e5546]71 virtual Declaration* mutate( StructDecl *aggregateDecl ) override;
72 virtual Declaration* mutate( UnionDecl *aggregateDecl ) override { return aggregateDecl; }
73 virtual Declaration* mutate( EnumDecl *aggregateDecl ) override { return aggregateDecl; }
74 virtual Declaration* mutate( TraitDecl *aggregateDecl ) override { return aggregateDecl; }
75 virtual TypeDecl* mutate( TypeDecl *typeDecl ) override { return typeDecl; }
76 virtual Declaration* mutate( TypedefDecl *typeDecl ) override { return typeDecl; }
[974906e2]77
[62e5546]78 virtual Type * mutate( FunctionType *funcType ) override { return funcType; }
[db4ecc5]79
[62e5546]80 virtual CompoundStmt * mutate( CompoundStmt * compoundStmt ) override;
[1ba88a0]81
82 private:
83 // set of mangled type names for which a constructor or destructor exists in the current scope.
84 // these types require a ConstructorInit node to be generated, anything else is a POD type and thus
85 // should not have a ConstructorInit generated.
86
87 bool isManaged( ObjectDecl * objDecl ) const ; // determine if object is managed
88 void handleDWT( DeclarationWithType * dwt ); // add type to managed if ctor/dtor
89 GenPoly::ScopedSet< std::string > managedTypes;
90 bool inFunction = false;
[974906e2]91 };
92
[62e5546]93 class HoistArrayDimension final : public GenPoly::DeclMutator {
[5f98ce5]94 public:
95 typedef GenPoly::DeclMutator Parent;
96
97 /// hoist dimension from array types in object declaration so that it uses a single
98 /// const variable of type size_t, so that side effecting array dimensions are only
99 /// computed once.
100 static void hoistArrayDimension( std::list< Declaration * > & translationUnit );
101
102 private:
[62e5546]103 using Parent::mutate;
104
105 virtual DeclarationWithType * mutate( ObjectDecl * objectDecl ) override;
106 virtual DeclarationWithType * mutate( FunctionDecl *functionDecl ) override;
[5f98ce5]107 // should not traverse into any of these declarations to find objects
108 // that need to be constructed or destructed
[62e5546]109 virtual Declaration* mutate( StructDecl *aggregateDecl ) override { return aggregateDecl; }
110 virtual Declaration* mutate( UnionDecl *aggregateDecl ) override { return aggregateDecl; }
111 virtual Declaration* mutate( EnumDecl *aggregateDecl ) override { return aggregateDecl; }
112 virtual Declaration* mutate( TraitDecl *aggregateDecl ) override { return aggregateDecl; }
113 virtual TypeDecl* mutate( TypeDecl *typeDecl ) override { return typeDecl; }
114 virtual Declaration* mutate( TypedefDecl *typeDecl ) override { return typeDecl; }
115
116 virtual Type* mutate( FunctionType *funcType ) override { return funcType; }
[5f98ce5]117
118 void hoist( Type * type );
119
120 DeclarationNode::StorageClass storageclass = DeclarationNode::NoStorageClass;
[40e636a]121 bool inFunction = false;
[5f98ce5]122 };
123
[a0fdbd5]124 void genInit( std::list< Declaration * > & translationUnit ) {
125 ReturnFixer::makeReturnTemp( translationUnit );
[5f98ce5]126 HoistArrayDimension::hoistArrayDimension( translationUnit );
[02c7d04]127 CtorDtor::generateCtorDtor( translationUnit );
128 }
129
[a0fdbd5]130 void ReturnFixer::makeReturnTemp( std::list< Declaration * > & translationUnit ) {
131 ReturnFixer fixer;
132 mutateAll( translationUnit, fixer );
[a08ba92]133 }
[42e2ad7]134
[a0fdbd5]135 ReturnFixer::ReturnFixer() : tempNamer( "_retVal" ) {}
[cf16f94]136
[a0fdbd5]137 Statement *ReturnFixer::mutate( ReturnStmt *returnStmt ) {
[cf16f94]138 // update for multiple return values
139 assert( returnVals.size() == 0 || returnVals.size() == 1 );
140 // hands off if the function returns an lvalue - we don't want to allocate a temporary if a variable's address
141 // is being returned
[7eabc25]142 if ( returnStmt->get_expr() && returnVals.size() == 1 && funcName != "?=?" && ! returnVals.front()->get_type()->get_isLvalue() ) {
[5b40f30]143 // ensure return value is not destructed by explicitly creating
144 // an empty SingleInit node wherein maybeConstruct is false
145 ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, returnVals.front()->get_type()->clone(), new ListInit( std::list<Initializer*>(), noDesignators, false ) );
[1e9d87b]146 stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) );
147
[5b40f30]148 // and explicitly create the constructor expression separately
149 UntypedExpr *construct = new UntypedExpr( new NameExpr( "?{}" ) );
[7eabc25]150 construct->get_args().push_back( new AddressExpr( new VariableExpr( newObj ) ) );
[5b40f30]151 construct->get_args().push_back( returnStmt->get_expr() );
152 stmtsToAdd.push_back(new ExprStmt(noLabels, construct));
[cf16f94]153
154 returnStmt->set_expr( new VariableExpr( newObj ) );
155 } // if
156 return returnStmt;
157 }
158
[a0fdbd5]159 DeclarationWithType* ReturnFixer::mutate( FunctionDecl *functionDecl ) {
[1ba88a0]160 ValueGuard< std::list<DeclarationWithType*> > oldReturnVals( returnVals );
161 ValueGuard< std::string > oldFuncName( funcName );
[1e9d87b]162
[cf16f94]163 FunctionType * type = functionDecl->get_functionType();
164 returnVals = type->get_returnVals();
165 funcName = functionDecl->get_name();
166 DeclarationWithType * decl = Mutator::mutate( functionDecl );
167 return decl;
168 }
[974906e2]169
[4d2434a]170 // precompute array dimension expression, because constructor generation may duplicate it,
171 // which would be incorrect if it is a side-effecting computation.
[5f98ce5]172 void HoistArrayDimension::hoistArrayDimension( std::list< Declaration * > & translationUnit ) {
173 HoistArrayDimension hoister;
[40e636a]174 hoister.mutateDeclarationList( translationUnit );
[5f98ce5]175 }
176
177 DeclarationWithType * HoistArrayDimension::mutate( ObjectDecl * objectDecl ) {
178 storageclass = objectDecl->get_storageClass();
179 DeclarationWithType * temp = Parent::mutate( objectDecl );
180 hoist( objectDecl->get_type() );
181 storageclass = DeclarationNode::NoStorageClass;
182 return temp;
183 }
184
185 void HoistArrayDimension::hoist( Type * type ) {
[40e636a]186 // if in function, generate const size_t var
[5f98ce5]187 static UniqueName dimensionName( "_array_dim" );
[40e636a]188
[f9cebb5]189 // C doesn't allow variable sized arrays at global scope or for static variables,
190 // so don't hoist dimension.
191 if ( ! inFunction ) return;
192 if ( storageclass == DeclarationNode::Static ) return;
193
194 if ( ArrayType * arrayType = dynamic_cast< ArrayType * >( type ) ) {
[5f98ce5]195 if ( ! arrayType->get_dimension() ) return; // xxx - recursive call to hoist?
196
197 // don't need to hoist dimension if it's a constexpr - only need to if there's potential
198 // for side effects.
199 if ( isConstExpr( arrayType->get_dimension() ) ) return;
200
201 ObjectDecl * arrayDimension = new ObjectDecl( dimensionName.newName(), storageclass, LinkageSpec::C, 0, SymTab::SizeType->clone(), new SingleInit( arrayType->get_dimension() ) );
202 arrayDimension->get_type()->set_isConst( true );
203
204 arrayType->set_dimension( new VariableExpr( arrayDimension ) );
205 addDeclaration( arrayDimension );
206
207 hoist( arrayType->get_base() );
208 return;
209 }
210 }
[974906e2]211
[40e636a]212 DeclarationWithType * HoistArrayDimension::mutate( FunctionDecl *functionDecl ) {
[1ba88a0]213 ValueGuard< bool > oldInFunc( inFunction );
[40e636a]214 inFunction = true;
215 DeclarationWithType * decl = Parent::mutate( functionDecl );
216 return decl;
217 }
218
[02c7d04]219 void CtorDtor::generateCtorDtor( std::list< Declaration * > & translationUnit ) {
220 CtorDtor ctordtor;
221 mutateAll( translationUnit, ctordtor );
[974906e2]222 }
223
[1ba88a0]224 bool CtorDtor::isManaged( ObjectDecl * objDecl ) const {
225 Type * type = objDecl->get_type();
226 while ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) {
227 type = at->get_base();
228 }
229 return managedTypes.find( SymTab::Mangler::mangle( type ) ) != managedTypes.end();
230 }
231
232 void CtorDtor::handleDWT( DeclarationWithType * dwt ) {
233 // if this function is a user-defined constructor or destructor, mark down the type as "managed"
234 if ( ! LinkageSpec::isOverridable( dwt->get_linkage() ) && isCtorDtor( dwt->get_name() ) ) {
235 std::list< DeclarationWithType * > & params = GenPoly::getFunctionType( dwt->get_type() )->get_parameters();
236 assert( ! params.empty() );
237 PointerType * type = safe_dynamic_cast< PointerType * >( params.front()->get_type() );
238 managedTypes.insert( SymTab::Mangler::mangle( type->get_base() ) );
239 }
240 }
241
[db4ecc5]242 DeclarationWithType * CtorDtor::mutate( ObjectDecl * objDecl ) {
[1ba88a0]243 handleDWT( objDecl );
244 // hands off if @=, extern, builtin, etc.
245 // if global but initializer is not constexpr, always try to construct, since this is not legal C
246 if ( ( tryConstruct( objDecl ) && isManaged( objDecl ) ) || (! inFunction && ! isConstExpr( objDecl->get_init() ) ) ) {
247 // constructed objects cannot be designated
248 if ( isDesignated( objDecl->get_init() ) ) throw SemanticError( "Cannot include designations in the initializer for a managed Object. If this is really what you want, then initialize with @=.", objDecl );
[dcd73d1]249 // constructed objects should not have initializers nested too deeply
250 if ( ! checkInitDepth( objDecl ) ) throw SemanticError( "Managed object's initializer is too deep ", objDecl );
[1ba88a0]251
[4d2434a]252 // call into genImplicitCall from Autogen.h to generate calls to ctor/dtor
253 // for each constructable object
254 std::list< Statement * > ctor;
255 std::list< Statement * > dtor;
256
257 InitExpander srcParam( objDecl->get_init() );
258 InitExpander nullParam( (Initializer *)NULL );
259 SymTab::genImplicitCall( srcParam, new VariableExpr( objDecl ), "?{}", back_inserter( ctor ), objDecl );
260 SymTab::genImplicitCall( nullParam, new VariableExpr( objDecl ), "^?{}", front_inserter( dtor ), objDecl, false );
261
262 // Currently genImplicitCall produces a single Statement - a CompoundStmt
263 // which wraps everything that needs to happen. As such, it's technically
264 // possible to use a Statement ** in the above calls, but this is inherently
265 // unsafe, so instead we take the slightly less efficient route, but will be
266 // immediately informed if somehow the above assumption is broken. In this case,
267 // we could always wrap the list of statements at this point with a CompoundStmt,
268 // but it seems reasonable at the moment for this to be done by genImplicitCall
269 // itself. It is possible that genImplicitCall produces no statements (e.g. if
270 // an array type does not have a dimension). In this case, it's fine to ignore
271 // the object for the purposes of construction.
272 assert( ctor.size() == dtor.size() && ctor.size() <= 1 );
273 if ( ctor.size() == 1 ) {
[6cf27a07]274 // need to remember init expression, in case no ctors exist
275 // if ctor does exist, want to use ctor expression instead of init
276 // push this decision to the resolver
[4d2434a]277 assert( dynamic_cast< ImplicitCtorDtorStmt * > ( ctor.front() ) && dynamic_cast< ImplicitCtorDtorStmt * > ( dtor.front() ) );
278 objDecl->set_init( new ConstructorInit( ctor.front(), dtor.front(), objDecl->get_init() ) );
[f1e012b]279 }
[974906e2]280 }
[1ba88a0]281 return Parent::mutate( objDecl );
[974906e2]282 }
283
[02c7d04]284 DeclarationWithType * CtorDtor::mutate( FunctionDecl *functionDecl ) {
[1ba88a0]285 ValueGuard< bool > oldInFunc = inFunction;
286 inFunction = true;
287
288 handleDWT( functionDecl );
289
290 managedTypes.beginScope();
291 // go through assertions and recursively add seen ctor/dtors
292 for ( TypeDecl * tyDecl : functionDecl->get_functionType()->get_forall() ) {
293 for ( DeclarationWithType *& assertion : tyDecl->get_assertions() ) {
294 assertion = assertion->acceptMutator( *this );
295 }
296 }
[02c7d04]297 // parameters should not be constructed and destructed, so don't mutate FunctionType
298 mutateAll( functionDecl->get_oldDecls(), *this );
299 functionDecl->set_statements( maybeMutate( functionDecl->get_statements(), *this ) );
[1ba88a0]300
301 managedTypes.endScope();
[02c7d04]302 return functionDecl;
303 }
[1ba88a0]304
305 Declaration* CtorDtor::mutate( StructDecl *aggregateDecl ) {
306 // don't construct members, but need to take note if there is a managed member,
307 // because that means that this type is also managed
308 for ( Declaration * member : aggregateDecl->get_members() ) {
309 if ( ObjectDecl * field = dynamic_cast< ObjectDecl * >( member ) ) {
310 if ( isManaged( field ) ) {
311 managedTypes.insert( SymTab::Mangler::mangle( aggregateDecl ) );
312 break;
313 }
314 }
315 }
316 return aggregateDecl;
317 }
318
319 CompoundStmt * CtorDtor::mutate( CompoundStmt * compoundStmt ) {
320 managedTypes.beginScope();
321 CompoundStmt * stmt = Parent::mutate( compoundStmt );
322 managedTypes.endScope();
323 return stmt;
324 }
325
[42e2ad7]326} // namespace InitTweak
327
[51587aa]328// Local Variables: //
329// tab-width: 4 //
330// mode: c++ //
331// compile-command: "make install" //
332// End: //
Note: See TracBrowser for help on using the repository browser.