source: src/InitTweak/GenInit.cc@ f51aefb

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 with_gc
Last change on this file since f51aefb 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
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 "SymTab/Mangler.h"
28#include "GenPoly/PolyMutator.h"
29#include "GenPoly/DeclMutator.h"
30#include "GenPoly/ScopedSet.h"
31
32namespace InitTweak {
33 namespace {
34 const std::list<Label> noLabels;
35 const std::list<Expression *> noDesignators;
36 }
37
38 class ReturnFixer final : public GenPoly::PolyMutator {
39 public:
40 /// consistently allocates a temporary variable for the return value
41 /// of a function so that anything which the resolver decides can be constructed
42 /// into the return type of a function can be returned.
43 static void makeReturnTemp( std::list< Declaration * > &translationUnit );
44
45 ReturnFixer();
46
47 using GenPoly::PolyMutator::mutate;
48 virtual DeclarationWithType * mutate( FunctionDecl *functionDecl ) override;
49 virtual Statement * mutate( ReturnStmt * returnStmt ) override;
50
51 protected:
52 std::list<DeclarationWithType*> returnVals;
53 UniqueName tempNamer;
54 std::string funcName;
55 };
56
57 class CtorDtor final : public GenPoly::PolyMutator {
58 public:
59 typedef GenPoly::PolyMutator Parent;
60 using Parent::mutate;
61 /// create constructor and destructor statements for object declarations.
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.
65 static void generateCtorDtor( std::list< Declaration * > &translationUnit );
66
67 virtual DeclarationWithType * mutate( ObjectDecl * ) override;
68 virtual DeclarationWithType * mutate( FunctionDecl *functionDecl ) override;
69 // should not traverse into any of these declarations to find objects
70 // that need to be constructed or destructed
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; }
77
78 virtual Type * mutate( FunctionType *funcType ) override { return funcType; }
79
80 virtual CompoundStmt * mutate( CompoundStmt * compoundStmt ) override;
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;
91 };
92
93 class HoistArrayDimension final : public GenPoly::DeclMutator {
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:
103 using Parent::mutate;
104
105 virtual DeclarationWithType * mutate( ObjectDecl * objectDecl ) override;
106 virtual DeclarationWithType * mutate( FunctionDecl *functionDecl ) override;
107 // should not traverse into any of these declarations to find objects
108 // that need to be constructed or destructed
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; }
117
118 void hoist( Type * type );
119
120 DeclarationNode::StorageClass storageclass = DeclarationNode::NoStorageClass;
121 bool inFunction = false;
122 };
123
124 void genInit( std::list< Declaration * > & translationUnit ) {
125 ReturnFixer::makeReturnTemp( translationUnit );
126 HoistArrayDimension::hoistArrayDimension( translationUnit );
127 CtorDtor::generateCtorDtor( translationUnit );
128 }
129
130 void ReturnFixer::makeReturnTemp( std::list< Declaration * > & translationUnit ) {
131 ReturnFixer fixer;
132 mutateAll( translationUnit, fixer );
133 }
134
135 ReturnFixer::ReturnFixer() : tempNamer( "_retVal" ) {}
136
137 Statement *ReturnFixer::mutate( ReturnStmt *returnStmt ) {
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
142 if ( returnStmt->get_expr() && returnVals.size() == 1 && funcName != "?=?" && ! returnVals.front()->get_type()->get_isLvalue() ) {
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 ) );
146 stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) );
147
148 // and explicitly create the constructor expression separately
149 UntypedExpr *construct = new UntypedExpr( new NameExpr( "?{}" ) );
150 construct->get_args().push_back( new AddressExpr( new VariableExpr( newObj ) ) );
151 construct->get_args().push_back( returnStmt->get_expr() );
152 stmtsToAdd.push_back(new ExprStmt(noLabels, construct));
153
154 returnStmt->set_expr( new VariableExpr( newObj ) );
155 } // if
156 return returnStmt;
157 }
158
159 DeclarationWithType* ReturnFixer::mutate( FunctionDecl *functionDecl ) {
160 ValueGuard< std::list<DeclarationWithType*> > oldReturnVals( returnVals );
161 ValueGuard< std::string > oldFuncName( funcName );
162
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 }
169
170 // precompute array dimension expression, because constructor generation may duplicate it,
171 // which would be incorrect if it is a side-effecting computation.
172 void HoistArrayDimension::hoistArrayDimension( std::list< Declaration * > & translationUnit ) {
173 HoistArrayDimension hoister;
174 hoister.mutateDeclarationList( translationUnit );
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 ) {
186 // if in function, generate const size_t var
187 static UniqueName dimensionName( "_array_dim" );
188
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 ) ) {
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 }
211
212 DeclarationWithType * HoistArrayDimension::mutate( FunctionDecl *functionDecl ) {
213 ValueGuard< bool > oldInFunc( inFunction );
214 inFunction = true;
215 DeclarationWithType * decl = Parent::mutate( functionDecl );
216 return decl;
217 }
218
219 void CtorDtor::generateCtorDtor( std::list< Declaration * > & translationUnit ) {
220 CtorDtor ctordtor;
221 mutateAll( translationUnit, ctordtor );
222 }
223
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
242 DeclarationWithType * CtorDtor::mutate( ObjectDecl * objDecl ) {
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 );
249 // constructed objects should not have initializers nested too deeply
250 if ( ! checkInitDepth( objDecl ) ) throw SemanticError( "Managed object's initializer is too deep ", objDecl );
251
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 ) {
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
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() ) );
279 }
280 }
281 return Parent::mutate( objDecl );
282 }
283
284 DeclarationWithType * CtorDtor::mutate( FunctionDecl *functionDecl ) {
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 }
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 ) );
300
301 managedTypes.endScope();
302 return functionDecl;
303 }
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
326} // namespace InitTweak
327
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.