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 | // FixInit.h -- |
---|
8 | // |
---|
9 | // Author : Rob Schluntz |
---|
10 | // Created On : Wed Jan 13 16:29:30 2016 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Wed Jun 21 17:35:05 2017 |
---|
13 | // Update Count : 74 |
---|
14 | // |
---|
15 | #include "FixInit.h" |
---|
16 | |
---|
17 | #include <stddef.h> // for NULL |
---|
18 | #include <algorithm> // for set_difference, copy_if |
---|
19 | #include <cassert> // for assert, strict_dynamic_cast |
---|
20 | #include <iostream> // for operator<<, ostream, basic_ost... |
---|
21 | #include <iterator> // for insert_iterator, back_inserter |
---|
22 | #include <list> // for _List_iterator, list, list<>::... |
---|
23 | #include <map> // for _Rb_tree_iterator, _Rb_tree_co... |
---|
24 | #include <memory> // for allocator_traits<>::value_type |
---|
25 | #include <set> // for set, set<>::value_type |
---|
26 | #include <unordered_map> // for unordered_map, unordered_map<>... |
---|
27 | #include <unordered_set> // for unordered_set |
---|
28 | #include <utility> // for pair |
---|
29 | |
---|
30 | #include "CodeGen/GenType.h" // for genPrettyType |
---|
31 | #include "CodeGen/OperatorTable.h" |
---|
32 | #include "Common/PassVisitor.h" // for PassVisitor, WithStmtsToAdd |
---|
33 | #include "Common/SemanticError.h" // for SemanticError |
---|
34 | #include "Common/UniqueName.h" // for UniqueName |
---|
35 | #include "Common/utility.h" // for CodeLocation, ValueGuard, toSt... |
---|
36 | #include "FixGlobalInit.h" // for fixGlobalInit |
---|
37 | #include "GenInit.h" // for genCtorDtor |
---|
38 | #include "GenPoly/DeclMutator.h" // for DeclMutator |
---|
39 | #include "GenPoly/GenPoly.h" // for getFunctionType |
---|
40 | #include "GenPoly/PolyMutator.h" // for PolyMutator |
---|
41 | #include "InitTweak.h" // for getFunctionName, getCallArg |
---|
42 | #include "Parser/LinkageSpec.h" // for C, Spec, Cforall, isBuiltin |
---|
43 | #include "ResolvExpr/Resolver.h" // for findVoidExpression |
---|
44 | #include "ResolvExpr/typeops.h" // for typesCompatible |
---|
45 | #include "SymTab/Autogen.h" // for genImplicitCall |
---|
46 | #include "SymTab/Indexer.h" // for Indexer |
---|
47 | #include "SymTab/Mangler.h" // for Mangler |
---|
48 | #include "SynTree/AddStmtVisitor.h" // for AddStmtVisitor |
---|
49 | #include "SynTree/Attribute.h" // for Attribute |
---|
50 | #include "SynTree/Constant.h" // for Constant |
---|
51 | #include "SynTree/Declaration.h" // for ObjectDecl, FunctionDecl, Decl... |
---|
52 | #include "SynTree/Expression.h" // for UniqueExpr, VariableExpr, Unty... |
---|
53 | #include "SynTree/Initializer.h" // for ConstructorInit, SingleInit |
---|
54 | #include "SynTree/Label.h" // for Label, noLabels, operator< |
---|
55 | #include "SynTree/Mutator.h" // for mutateAll, Mutator, maybeMutate |
---|
56 | #include "SynTree/Statement.h" // for ExprStmt, CompoundStmt, Branch... |
---|
57 | #include "SynTree/Type.h" // for Type, Type::StorageClasses |
---|
58 | #include "SynTree/TypeSubstitution.h" // for TypeSubstitution, operator<< |
---|
59 | #include "SynTree/Visitor.h" // for acceptAll, maybeAccept |
---|
60 | |
---|
61 | bool ctordtorp = false; // print all debug |
---|
62 | bool ctorp = false; // print ctor debug |
---|
63 | bool cpctorp = false; // print copy ctor debug |
---|
64 | bool dtorp = false; // print dtor debug |
---|
65 | #define PRINT( text ) if ( ctordtorp ) { text } |
---|
66 | #define CP_CTOR_PRINT( text ) if ( ctordtorp || cpctorp ) { text } |
---|
67 | #define DTOR_PRINT( text ) if ( ctordtorp || dtorp ) { text } |
---|
68 | |
---|
69 | namespace InitTweak { |
---|
70 | namespace { |
---|
71 | typedef std::unordered_map< int, int > UnqCount; |
---|
72 | |
---|
73 | struct InsertImplicitCalls : public WithTypeSubstitution { |
---|
74 | /// wrap function application expressions as ImplicitCopyCtorExpr nodes so that it is easy to identify which |
---|
75 | /// function calls need their parameters to be copy constructed |
---|
76 | static void insert( std::list< Declaration * > & translationUnit ); |
---|
77 | |
---|
78 | Expression * postmutate( ApplicationExpr * appExpr ); |
---|
79 | }; |
---|
80 | |
---|
81 | struct ResolveCopyCtors final : public WithIndexer, public WithShortCircuiting, public WithTypeSubstitution { |
---|
82 | /// generate temporary ObjectDecls for each argument and return value of each ImplicitCopyCtorExpr, |
---|
83 | /// generate/resolve copy construction expressions for each, and generate/resolve destructors for both |
---|
84 | /// arguments and return value temporaries |
---|
85 | static void resolveImplicitCalls( std::list< Declaration * > & translationUnit, UnqCount & unqCount ); |
---|
86 | |
---|
87 | ResolveCopyCtors( UnqCount & unqCount ) : unqCount( unqCount ) {} |
---|
88 | |
---|
89 | void postvisit( ImplicitCopyCtorExpr * impCpCtorExpr ); |
---|
90 | void postvisit( StmtExpr * stmtExpr ); |
---|
91 | void previsit( UniqueExpr * unqExpr ); |
---|
92 | void postvisit( UniqueExpr * unqExpr ); |
---|
93 | |
---|
94 | /// create and resolve ctor/dtor expression: fname(var, [cpArg]) |
---|
95 | Expression * makeCtorDtor( const std::string & fname, ObjectDecl * var, Expression * cpArg = NULL ); |
---|
96 | /// true if type does not need to be copy constructed to ensure correctness |
---|
97 | bool skipCopyConstruct( Type * type ); |
---|
98 | void copyConstructArg( Expression *& arg, ImplicitCopyCtorExpr * impCpCtorExpr ); |
---|
99 | void destructRet( ObjectDecl * ret, ImplicitCopyCtorExpr * impCpCtorExpr ); |
---|
100 | |
---|
101 | UnqCount & unqCount; // count the number of times each unique expr ID appears |
---|
102 | std::unordered_set< int > vars; |
---|
103 | }; |
---|
104 | |
---|
105 | /// collects constructed object decls - used as a base class |
---|
106 | struct ObjDeclCollector : public WithGuards, public WithShortCircuiting { |
---|
107 | // use ordered data structure to maintain ordering for set_difference and for consistent error messages |
---|
108 | typedef std::list< ObjectDecl * > ObjectSet; |
---|
109 | void previsit( CompoundStmt *compoundStmt ); |
---|
110 | void previsit( DeclStmt *stmt ); |
---|
111 | |
---|
112 | // don't go into other functions |
---|
113 | void previsit( FunctionDecl * ) { visit_children = false; } |
---|
114 | |
---|
115 | protected: |
---|
116 | ObjectSet curVars; |
---|
117 | }; |
---|
118 | |
---|
119 | // debug |
---|
120 | template<typename ObjectSet> |
---|
121 | struct PrintSet { |
---|
122 | PrintSet( const ObjectSet & objs ) : objs( objs ) {} |
---|
123 | const ObjectSet & objs; |
---|
124 | }; |
---|
125 | template<typename ObjectSet> |
---|
126 | PrintSet<ObjectSet> printSet( const ObjectSet & objs ) { return PrintSet<ObjectSet>( objs ); } |
---|
127 | template<typename ObjectSet> |
---|
128 | std::ostream & operator<<( std::ostream & out, const PrintSet<ObjectSet> & set) { |
---|
129 | out << "{ "; |
---|
130 | for ( ObjectDecl * obj : set.objs ) { |
---|
131 | out << obj->get_name() << ", " ; |
---|
132 | } // for |
---|
133 | out << " }"; |
---|
134 | return out; |
---|
135 | } |
---|
136 | |
---|
137 | struct LabelFinder final : public ObjDeclCollector { |
---|
138 | typedef std::map< Label, ObjectSet > LabelMap; |
---|
139 | // map of Label -> live variables at that label |
---|
140 | LabelMap vars; |
---|
141 | |
---|
142 | typedef ObjDeclCollector Parent; |
---|
143 | using Parent::previsit; |
---|
144 | void previsit( Statement * stmt ); |
---|
145 | |
---|
146 | void previsit( CompoundStmt *compoundStmt ); |
---|
147 | void previsit( DeclStmt *stmt ); |
---|
148 | }; |
---|
149 | |
---|
150 | struct InsertDtors final : public ObjDeclCollector, public WithStmtsToAdd { |
---|
151 | /// insert destructor calls at the appropriate places. must happen before CtorInit nodes are removed |
---|
152 | /// (currently by FixInit) |
---|
153 | static void insert( std::list< Declaration * > & translationUnit ); |
---|
154 | |
---|
155 | typedef std::list< ObjectDecl * > OrderedDecls; |
---|
156 | typedef std::list< OrderedDecls > OrderedDeclsStack; |
---|
157 | |
---|
158 | InsertDtors( PassVisitor<LabelFinder> & finder ) : finder( finder ), labelVars( finder.pass.vars ) {} |
---|
159 | |
---|
160 | typedef ObjDeclCollector Parent; |
---|
161 | using Parent::previsit; |
---|
162 | |
---|
163 | void previsit( ObjectDecl * objDecl ); |
---|
164 | void previsit( FunctionDecl * funcDecl ); |
---|
165 | |
---|
166 | void previsit( CompoundStmt * compoundStmt ); |
---|
167 | void postvisit( CompoundStmt * compoundStmt ); |
---|
168 | void previsit( ReturnStmt * returnStmt ); |
---|
169 | void previsit( BranchStmt * stmt ); |
---|
170 | private: |
---|
171 | void handleGoto( BranchStmt * stmt ); |
---|
172 | |
---|
173 | PassVisitor<LabelFinder> & finder; |
---|
174 | LabelFinder::LabelMap & labelVars; |
---|
175 | OrderedDeclsStack reverseDeclOrder; |
---|
176 | }; |
---|
177 | |
---|
178 | class FixInit : public WithStmtsToAdd { |
---|
179 | public: |
---|
180 | /// expand each object declaration to use its constructor after it is declared. |
---|
181 | static void fixInitializers( std::list< Declaration * > &translationUnit ); |
---|
182 | |
---|
183 | DeclarationWithType * postmutate( ObjectDecl *objDecl ); |
---|
184 | |
---|
185 | std::list< Declaration * > staticDtorDecls; |
---|
186 | }; |
---|
187 | |
---|
188 | class FixCopyCtors final : public GenPoly::PolyMutator { |
---|
189 | public: |
---|
190 | FixCopyCtors( UnqCount & unqCount ) : unqCount( unqCount ){} |
---|
191 | /// expand ImplicitCopyCtorExpr nodes into the temporary declarations, copy constructors, call expression, |
---|
192 | /// and destructors |
---|
193 | static void fixCopyCtors( std::list< Declaration * > &translationUnit, UnqCount & unqCount ); |
---|
194 | |
---|
195 | typedef GenPoly::PolyMutator Parent; |
---|
196 | using Parent::mutate; |
---|
197 | virtual Expression * mutate( ImplicitCopyCtorExpr * impCpCtorExpr ) override; |
---|
198 | virtual Expression * mutate( UniqueExpr * unqExpr ) override; |
---|
199 | virtual Expression * mutate( StmtExpr * stmtExpr ) override; |
---|
200 | |
---|
201 | UnqCount & unqCount; |
---|
202 | }; |
---|
203 | |
---|
204 | struct GenStructMemberCalls final : public WithGuards, public WithShortCircuiting, public WithIndexer { |
---|
205 | /// generate default/copy ctor and dtor calls for user-defined struct ctor/dtors |
---|
206 | /// for any member that is missing a corresponding ctor/dtor call. |
---|
207 | /// error if a member is used before constructed |
---|
208 | static void generate( std::list< Declaration * > & translationUnit ); |
---|
209 | |
---|
210 | void previsit( FunctionDecl * funcDecl ); |
---|
211 | void postvisit( FunctionDecl * funcDecl ); |
---|
212 | |
---|
213 | void previsit( MemberExpr * memberExpr ); |
---|
214 | void previsit( ApplicationExpr * appExpr ); |
---|
215 | |
---|
216 | SemanticError errors; |
---|
217 | private: |
---|
218 | template< typename... Params > |
---|
219 | void emit( CodeLocation, const Params &... params ); |
---|
220 | |
---|
221 | FunctionDecl * function = 0; |
---|
222 | std::set< DeclarationWithType * > unhandled; |
---|
223 | std::map< DeclarationWithType *, CodeLocation > usedUninit; |
---|
224 | ObjectDecl * thisParam = 0; |
---|
225 | bool isCtor = false; // true if current function is a constructor |
---|
226 | StructDecl * structDecl = 0; |
---|
227 | }; |
---|
228 | |
---|
229 | // very simple resolver-like mutator class - used to |
---|
230 | // resolve UntypedExprs that are found within newly |
---|
231 | // generated constructor/destructor calls |
---|
232 | class MutatingResolver final : public Mutator { |
---|
233 | public: |
---|
234 | MutatingResolver( SymTab::Indexer & indexer ) : indexer( indexer ) {} |
---|
235 | |
---|
236 | using Mutator::mutate; |
---|
237 | virtual DeclarationWithType* mutate( ObjectDecl *objectDecl ) override; |
---|
238 | virtual Expression* mutate( UntypedExpr *untypedExpr ) override; |
---|
239 | |
---|
240 | private: |
---|
241 | SymTab::Indexer & indexer; |
---|
242 | }; |
---|
243 | |
---|
244 | class FixCtorExprs final : public GenPoly::DeclMutator { |
---|
245 | public: |
---|
246 | /// expands ConstructorExpr nodes into comma expressions, using a temporary for the first argument |
---|
247 | static void fix( std::list< Declaration * > & translationUnit ); |
---|
248 | |
---|
249 | using GenPoly::DeclMutator::mutate; |
---|
250 | virtual Expression * mutate( ConstructorExpr * ctorExpr ) override; |
---|
251 | }; |
---|
252 | } // namespace |
---|
253 | |
---|
254 | void fix( std::list< Declaration * > & translationUnit, const std::string & filename, bool inLibrary ) { |
---|
255 | // fixes ConstructorInit for global variables. should happen before fixInitializers. |
---|
256 | InitTweak::fixGlobalInit( translationUnit, filename, inLibrary ); |
---|
257 | |
---|
258 | UnqCount unqCount; |
---|
259 | |
---|
260 | InsertImplicitCalls::insert( translationUnit ); |
---|
261 | ResolveCopyCtors::resolveImplicitCalls( translationUnit, unqCount ); |
---|
262 | InsertDtors::insert( translationUnit ); |
---|
263 | FixInit::fixInitializers( translationUnit ); |
---|
264 | |
---|
265 | // FixCopyCtors must happen after FixInit, so that destructors are placed correctly |
---|
266 | FixCopyCtors::fixCopyCtors( translationUnit, unqCount ); |
---|
267 | |
---|
268 | GenStructMemberCalls::generate( translationUnit ); |
---|
269 | // xxx - ctor expansion currently has to be after FixCopyCtors, because there is currently a |
---|
270 | // hack in the way untyped assignments are generated, where the first argument cannot have |
---|
271 | // its address taken because of the way codegeneration handles UntypedExpr vs. ApplicationExpr. |
---|
272 | // Thus such assignment exprs must never pushed through expression resolution (and thus should |
---|
273 | // not go through the FixCopyCtors pass), otherwise they will fail -- guaranteed. |
---|
274 | // Also needs to happen after GenStructMemberCalls, since otherwise member constructors exprs |
---|
275 | // don't look right, and a member can be constructed more than once. |
---|
276 | FixCtorExprs::fix( translationUnit ); |
---|
277 | } |
---|
278 | |
---|
279 | namespace { |
---|
280 | void InsertImplicitCalls::insert( std::list< Declaration * > & translationUnit ) { |
---|
281 | PassVisitor<InsertImplicitCalls> inserter; |
---|
282 | mutateAll( translationUnit, inserter ); |
---|
283 | } |
---|
284 | |
---|
285 | void ResolveCopyCtors::resolveImplicitCalls( std::list< Declaration * > & translationUnit, UnqCount & unqCount ) { |
---|
286 | PassVisitor<ResolveCopyCtors> resolver( unqCount ); |
---|
287 | acceptAll( translationUnit, resolver ); |
---|
288 | } |
---|
289 | |
---|
290 | void FixInit::fixInitializers( std::list< Declaration * > & translationUnit ) { |
---|
291 | PassVisitor<FixInit> fixer; |
---|
292 | |
---|
293 | // can't use mutateAll, because need to insert declarations at top-level |
---|
294 | // can't use DeclMutator, because sometimes need to insert IfStmt, etc. |
---|
295 | SemanticError errors; |
---|
296 | for ( std::list< Declaration * >::iterator i = translationUnit.begin(); i != translationUnit.end(); ++i ) { |
---|
297 | try { |
---|
298 | *i = maybeMutate( *i, fixer ); |
---|
299 | translationUnit.splice( i, fixer.pass.staticDtorDecls ); |
---|
300 | } catch( SemanticError &e ) { |
---|
301 | e.set_location( (*i)->location ); |
---|
302 | errors.append( e ); |
---|
303 | } // try |
---|
304 | } // for |
---|
305 | if ( ! errors.isEmpty() ) { |
---|
306 | throw errors; |
---|
307 | } // if |
---|
308 | } |
---|
309 | |
---|
310 | void InsertDtors::insert( std::list< Declaration * > & translationUnit ) { |
---|
311 | PassVisitor<LabelFinder> finder; |
---|
312 | PassVisitor<InsertDtors> inserter( finder ); |
---|
313 | acceptAll( translationUnit, inserter ); |
---|
314 | } |
---|
315 | |
---|
316 | void FixCopyCtors::fixCopyCtors( std::list< Declaration * > & translationUnit, UnqCount & unqCount ) { |
---|
317 | FixCopyCtors fixer( unqCount ); |
---|
318 | mutateAll( translationUnit, fixer ); |
---|
319 | } |
---|
320 | |
---|
321 | void GenStructMemberCalls::generate( std::list< Declaration * > & translationUnit ) { |
---|
322 | PassVisitor<GenStructMemberCalls> warner; |
---|
323 | acceptAll( translationUnit, warner ); |
---|
324 | } |
---|
325 | |
---|
326 | void FixCtorExprs::fix( std::list< Declaration * > & translationUnit ) { |
---|
327 | FixCtorExprs fixer; |
---|
328 | fixer.mutateDeclarationList( translationUnit ); |
---|
329 | } |
---|
330 | |
---|
331 | Expression * InsertImplicitCalls::postmutate( ApplicationExpr * appExpr ) { |
---|
332 | assert( appExpr ); |
---|
333 | |
---|
334 | if ( VariableExpr * function = dynamic_cast< VariableExpr * > ( appExpr->get_function() ) ) { |
---|
335 | if ( LinkageSpec::isBuiltin( function->get_var()->get_linkage() ) ) { |
---|
336 | // optimization: don't need to copy construct in order to call intrinsic functions |
---|
337 | return appExpr; |
---|
338 | } else if ( DeclarationWithType * funcDecl = dynamic_cast< DeclarationWithType * > ( function->get_var() ) ) { |
---|
339 | FunctionType * ftype = dynamic_cast< FunctionType * >( GenPoly::getFunctionType( funcDecl->get_type() ) ); |
---|
340 | assertf( ftype, "Function call without function type: %s", toString( funcDecl ).c_str() ); |
---|
341 | if ( CodeGen::isConstructor( funcDecl->get_name() ) && ftype->get_parameters().size() == 2 ) { |
---|
342 | Type * t1 = getPointerBase( ftype->get_parameters().front()->get_type() ); |
---|
343 | Type * t2 = ftype->get_parameters().back()->get_type(); |
---|
344 | assert( t1 ); |
---|
345 | |
---|
346 | if ( ResolvExpr::typesCompatible( t1, t2, SymTab::Indexer() ) ) { |
---|
347 | // optimization: don't need to copy construct in order to call a copy constructor |
---|
348 | return appExpr; |
---|
349 | } // if |
---|
350 | } else if ( CodeGen::isDestructor( funcDecl->get_name() ) ) { |
---|
351 | // correctness: never copy construct arguments to a destructor |
---|
352 | return appExpr; |
---|
353 | } // if |
---|
354 | } // if |
---|
355 | } // if |
---|
356 | CP_CTOR_PRINT( std::cerr << "InsertImplicitCalls: adding a wrapper " << appExpr << std::endl; ) |
---|
357 | |
---|
358 | // wrap each function call so that it is easy to identify nodes that have to be copy constructed |
---|
359 | ImplicitCopyCtorExpr * expr = new ImplicitCopyCtorExpr( appExpr ); |
---|
360 | // Move the type substitution to the new top-level, if it is attached to the appExpr. |
---|
361 | // Ensure it is not deleted with the ImplicitCopyCtorExpr by removing it before deletion. |
---|
362 | // The substitution is needed to obtain the type of temporary variables so that copy constructor |
---|
363 | // calls can be resolved. |
---|
364 | assert( env ); |
---|
365 | std::swap( expr->env, appExpr->env ); |
---|
366 | return expr; |
---|
367 | } |
---|
368 | |
---|
369 | bool ResolveCopyCtors::skipCopyConstruct( Type * type ) { return ! isConstructable( type ); } |
---|
370 | |
---|
371 | Expression * ResolveCopyCtors::makeCtorDtor( const std::string & fname, ObjectDecl * var, Expression * cpArg ) { |
---|
372 | assert( var ); |
---|
373 | // arrays are not copy constructed, so this should always be an ExprStmt |
---|
374 | ImplicitCtorDtorStmt * stmt = genCtorDtor( fname, var, cpArg ); |
---|
375 | ExprStmt * exprStmt = strict_dynamic_cast< ExprStmt * >( stmt->get_callStmt() ); |
---|
376 | Expression * untyped = exprStmt->get_expr(); |
---|
377 | |
---|
378 | // resolve copy constructor |
---|
379 | // should only be one alternative for copy ctor and dtor expressions, since all arguments are fixed |
---|
380 | // (VariableExpr and already resolved expression) |
---|
381 | CP_CTOR_PRINT( std::cerr << "ResolvingCtorDtor " << untyped << std::endl; ) |
---|
382 | Expression * resolved = ResolvExpr::findVoidExpression( untyped, indexer ); |
---|
383 | assert( resolved ); |
---|
384 | if ( resolved->get_env() ) { |
---|
385 | // Extract useful information and discard new environments. Keeping them causes problems in PolyMutator passes. |
---|
386 | env->add( *resolved->get_env() ); |
---|
387 | delete resolved->get_env(); |
---|
388 | resolved->set_env( nullptr ); |
---|
389 | } // if |
---|
390 | |
---|
391 | delete stmt; |
---|
392 | return resolved; |
---|
393 | } |
---|
394 | |
---|
395 | void ResolveCopyCtors::copyConstructArg( Expression *& arg, ImplicitCopyCtorExpr * impCpCtorExpr ) { |
---|
396 | static UniqueName tempNamer("_tmp_cp"); |
---|
397 | assert( env ); |
---|
398 | CP_CTOR_PRINT( std::cerr << "Type Substitution: " << *env << std::endl; ) |
---|
399 | assert( arg->has_result() ); |
---|
400 | Type * result = arg->get_result(); |
---|
401 | if ( skipCopyConstruct( result ) ) return; // skip certain non-copyable types |
---|
402 | |
---|
403 | // type may involve type variables, so apply type substitution to get temporary variable's actual type |
---|
404 | result = result->clone(); |
---|
405 | env->apply( result ); |
---|
406 | ObjectDecl * tmp = ObjectDecl::newObject( "__tmp", result, nullptr ); |
---|
407 | tmp->get_type()->set_const( false ); |
---|
408 | |
---|
409 | // create and resolve copy constructor |
---|
410 | CP_CTOR_PRINT( std::cerr << "makeCtorDtor for an argument" << std::endl; ) |
---|
411 | Expression * cpCtor = makeCtorDtor( "?{}", tmp, arg ); |
---|
412 | |
---|
413 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( cpCtor ) ) { |
---|
414 | // if the chosen constructor is intrinsic, the copy is unnecessary, so |
---|
415 | // don't create the temporary and don't call the copy constructor |
---|
416 | VariableExpr * function = dynamic_cast< VariableExpr * >( appExpr->get_function() ); |
---|
417 | assert( function ); |
---|
418 | if ( function->get_var()->get_linkage() == LinkageSpec::Intrinsic ) return; |
---|
419 | } |
---|
420 | |
---|
421 | // set a unique name for the temporary once it's certain the call is necessary |
---|
422 | tmp->name = tempNamer.newName(); |
---|
423 | |
---|
424 | // replace argument to function call with temporary |
---|
425 | arg = new CommaExpr( cpCtor, new VariableExpr( tmp ) ); |
---|
426 | impCpCtorExpr->get_tempDecls().push_back( tmp ); |
---|
427 | impCpCtorExpr->get_dtors().push_front( makeCtorDtor( "^?{}", tmp ) ); |
---|
428 | } |
---|
429 | |
---|
430 | void ResolveCopyCtors::destructRet( ObjectDecl * ret, ImplicitCopyCtorExpr * impCpCtorExpr ) { |
---|
431 | impCpCtorExpr->get_dtors().push_front( makeCtorDtor( "^?{}", ret ) ); |
---|
432 | } |
---|
433 | |
---|
434 | void ResolveCopyCtors::postvisit( ImplicitCopyCtorExpr *impCpCtorExpr ) { |
---|
435 | CP_CTOR_PRINT( std::cerr << "ResolveCopyCtors: " << impCpCtorExpr << std::endl; ) |
---|
436 | |
---|
437 | ApplicationExpr * appExpr = impCpCtorExpr->get_callExpr(); |
---|
438 | |
---|
439 | // take each argument and attempt to copy construct it. |
---|
440 | for ( Expression * & arg : appExpr->get_args() ) { |
---|
441 | copyConstructArg( arg, impCpCtorExpr ); |
---|
442 | } // for |
---|
443 | |
---|
444 | // each return value from the call needs to be connected with an ObjectDecl at the call site, which is |
---|
445 | // initialized with the return value and is destructed later |
---|
446 | // xxx - handle named return values? |
---|
447 | Type * result = appExpr->get_result(); |
---|
448 | if ( ! result->isVoid() ) { |
---|
449 | static UniqueName retNamer("_tmp_cp_ret"); |
---|
450 | result = result->clone(); |
---|
451 | env->apply( result ); |
---|
452 | ObjectDecl * ret = ObjectDecl::newObject( retNamer.newName(), result, nullptr ); |
---|
453 | ret->get_type()->set_const( false ); |
---|
454 | impCpCtorExpr->get_returnDecls().push_back( ret ); |
---|
455 | CP_CTOR_PRINT( std::cerr << "makeCtorDtor for a return" << std::endl; ) |
---|
456 | if ( ! dynamic_cast< ReferenceType * >( result ) ) { |
---|
457 | // destructing reference returns is bad because it can cause multiple destructor calls to the same object - the returned object is not a temporary |
---|
458 | destructRet( ret, impCpCtorExpr ); |
---|
459 | } |
---|
460 | } // for |
---|
461 | CP_CTOR_PRINT( std::cerr << "after Resolving: " << impCpCtorExpr << std::endl; ) |
---|
462 | } |
---|
463 | |
---|
464 | void ResolveCopyCtors::postvisit( StmtExpr * stmtExpr ) { |
---|
465 | assert( env ); |
---|
466 | assert( stmtExpr->get_result() ); |
---|
467 | Type * result = stmtExpr->get_result(); |
---|
468 | if ( ! result->isVoid() ) { |
---|
469 | static UniqueName retNamer("_tmp_stmtexpr_ret"); |
---|
470 | |
---|
471 | // create variable that will hold the result of the stmt expr |
---|
472 | result = result->clone(); |
---|
473 | env->apply( result ); |
---|
474 | ObjectDecl * ret = ObjectDecl::newObject( retNamer.newName(), result, nullptr ); |
---|
475 | ret->get_type()->set_const( false ); |
---|
476 | stmtExpr->get_returnDecls().push_front( ret ); |
---|
477 | |
---|
478 | // must have a non-empty body, otherwise it wouldn't have a result |
---|
479 | CompoundStmt * body = stmtExpr->get_statements(); |
---|
480 | assert( ! body->get_kids().empty() ); |
---|
481 | // must be an ExprStmt, otherwise it wouldn't have a result |
---|
482 | ExprStmt * last = strict_dynamic_cast< ExprStmt * >( body->get_kids().back() ); |
---|
483 | last->set_expr( makeCtorDtor( "?{}", ret, last->get_expr() ) ); |
---|
484 | |
---|
485 | stmtExpr->get_dtors().push_front( makeCtorDtor( "^?{}", ret ) ); |
---|
486 | } // if |
---|
487 | } |
---|
488 | |
---|
489 | void ResolveCopyCtors::previsit( UniqueExpr * unqExpr ) { |
---|
490 | unqCount[ unqExpr->get_id() ]++; // count the number of unique expressions for each ID |
---|
491 | if ( vars.count( unqExpr->get_id() ) ) { |
---|
492 | // xxx - hack to prevent double-handling of unique exprs, otherwise too many temporary variables and destructors are generated |
---|
493 | visit_children = false; |
---|
494 | } |
---|
495 | } |
---|
496 | |
---|
497 | void ResolveCopyCtors::postvisit( UniqueExpr * unqExpr ) { |
---|
498 | if ( vars.count( unqExpr->get_id() ) ) { |
---|
499 | // xxx - hack to prevent double-handling of unique exprs, otherwise too many temporary variables and destructors are generated |
---|
500 | return; |
---|
501 | } |
---|
502 | |
---|
503 | // it should never be necessary to wrap a void-returning expression in a UniqueExpr - if this assumption changes, this needs to be rethought |
---|
504 | assert( unqExpr->get_result() ); |
---|
505 | if ( ImplicitCopyCtorExpr * impCpCtorExpr = dynamic_cast<ImplicitCopyCtorExpr*>( unqExpr->get_expr() ) ) { |
---|
506 | // note the variable used as the result from the call |
---|
507 | assert( impCpCtorExpr->get_result() && impCpCtorExpr->get_returnDecls().size() == 1 ); |
---|
508 | unqExpr->set_var( new VariableExpr( impCpCtorExpr->get_returnDecls().front() ) ); |
---|
509 | } else { |
---|
510 | // expr isn't a call expr, so create a new temporary variable to use to hold the value of the unique expression |
---|
511 | unqExpr->set_object( ObjectDecl::newObject( toString("_unq", unqExpr->get_id()), unqExpr->get_result()->clone(), nullptr ) ); |
---|
512 | unqExpr->set_var( new VariableExpr( unqExpr->get_object() ) ); |
---|
513 | } |
---|
514 | vars.insert( unqExpr->get_id() ); |
---|
515 | } |
---|
516 | |
---|
517 | Expression * FixCopyCtors::mutate( ImplicitCopyCtorExpr * impCpCtorExpr ) { |
---|
518 | CP_CTOR_PRINT( std::cerr << "FixCopyCtors: " << impCpCtorExpr << std::endl; ) |
---|
519 | |
---|
520 | impCpCtorExpr = strict_dynamic_cast< ImplicitCopyCtorExpr * >( Parent::mutate( impCpCtorExpr ) ); |
---|
521 | std::list< ObjectDecl * > & tempDecls = impCpCtorExpr->get_tempDecls(); |
---|
522 | std::list< ObjectDecl * > & returnDecls = impCpCtorExpr->get_returnDecls(); |
---|
523 | std::list< Expression * > & dtors = impCpCtorExpr->get_dtors(); |
---|
524 | |
---|
525 | // add all temporary declarations and their constructors |
---|
526 | for ( ObjectDecl * obj : tempDecls ) { |
---|
527 | stmtsToAdd.push_back( new DeclStmt( noLabels, obj ) ); |
---|
528 | } // for |
---|
529 | for ( ObjectDecl * obj : returnDecls ) { |
---|
530 | stmtsToAdd.push_back( new DeclStmt( noLabels, obj ) ); |
---|
531 | } // for |
---|
532 | |
---|
533 | // add destructors after current statement |
---|
534 | for ( Expression * dtor : dtors ) { |
---|
535 | stmtsToAddAfter.push_back( new ExprStmt( noLabels, dtor ) ); |
---|
536 | } // for |
---|
537 | |
---|
538 | // xxx - update to work with multiple return values |
---|
539 | ObjectDecl * returnDecl = returnDecls.empty() ? nullptr : returnDecls.front(); |
---|
540 | Expression * callExpr = impCpCtorExpr->get_callExpr(); |
---|
541 | |
---|
542 | CP_CTOR_PRINT( std::cerr << "Coming out the back..." << impCpCtorExpr << std::endl; ) |
---|
543 | |
---|
544 | // detach fields from wrapper node so that it can be deleted without deleting too much |
---|
545 | dtors.clear(); |
---|
546 | tempDecls.clear(); |
---|
547 | returnDecls.clear(); |
---|
548 | impCpCtorExpr->set_callExpr( nullptr ); |
---|
549 | std::swap( impCpCtorExpr->env, callExpr->env ); |
---|
550 | assert( impCpCtorExpr->env == nullptr ); |
---|
551 | delete impCpCtorExpr; |
---|
552 | |
---|
553 | if ( returnDecl ) { |
---|
554 | UntypedExpr * assign = new UntypedExpr( new NameExpr( "?=?" ) ); |
---|
555 | assign->get_args().push_back( new VariableExpr( returnDecl ) ); |
---|
556 | assign->get_args().push_back( callExpr ); |
---|
557 | // know the result type of the assignment is the type of the LHS (minus the pointer), so |
---|
558 | // add that onto the assignment expression so that later steps have the necessary information |
---|
559 | assign->set_result( returnDecl->get_type()->clone() ); |
---|
560 | |
---|
561 | Expression * retExpr = new CommaExpr( assign, new VariableExpr( returnDecl ) ); |
---|
562 | // move env from callExpr to retExpr |
---|
563 | retExpr->set_env( callExpr->get_env() ); |
---|
564 | callExpr->set_env( nullptr ); |
---|
565 | return retExpr; |
---|
566 | } else { |
---|
567 | return callExpr; |
---|
568 | } // if |
---|
569 | } |
---|
570 | |
---|
571 | Expression * FixCopyCtors::mutate( StmtExpr * stmtExpr ) { |
---|
572 | // function call temporaries should be placed at statement-level, rather than nested inside of a new statement expression, |
---|
573 | // since temporaries can be shared across sub-expressions, e.g. |
---|
574 | // [A, A] f(); |
---|
575 | // g([A] x, [A] y); |
---|
576 | // f(g()); |
---|
577 | // f is executed once, so the return temporary is shared across the tuple constructors for x and y. |
---|
578 | std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids(); |
---|
579 | for ( Statement *& stmt : stmts ) { |
---|
580 | stmt = stmt->acceptMutator( *this ); |
---|
581 | } // for |
---|
582 | // stmtExpr = strict_dynamic_cast< StmtExpr * >( Parent::mutate( stmtExpr ) ); |
---|
583 | assert( stmtExpr->get_result() ); |
---|
584 | Type * result = stmtExpr->get_result(); |
---|
585 | if ( ! result->isVoid() ) { |
---|
586 | for ( ObjectDecl * obj : stmtExpr->get_returnDecls() ) { |
---|
587 | stmtsToAdd.push_back( new DeclStmt( noLabels, obj ) ); |
---|
588 | } // for |
---|
589 | // add destructors after current statement |
---|
590 | for ( Expression * dtor : stmtExpr->get_dtors() ) { |
---|
591 | stmtsToAddAfter.push_back( new ExprStmt( noLabels, dtor ) ); |
---|
592 | } // for |
---|
593 | // must have a non-empty body, otherwise it wouldn't have a result |
---|
594 | CompoundStmt * body = stmtExpr->get_statements(); |
---|
595 | assert( ! body->get_kids().empty() ); |
---|
596 | assert( ! stmtExpr->get_returnDecls().empty() ); |
---|
597 | body->get_kids().push_back( new ExprStmt( noLabels, new VariableExpr( stmtExpr->get_returnDecls().front() ) ) ); |
---|
598 | stmtExpr->get_returnDecls().clear(); |
---|
599 | stmtExpr->get_dtors().clear(); |
---|
600 | } |
---|
601 | assert( stmtExpr->get_returnDecls().empty() ); |
---|
602 | assert( stmtExpr->get_dtors().empty() ); |
---|
603 | return stmtExpr; |
---|
604 | } |
---|
605 | |
---|
606 | Expression * FixCopyCtors::mutate( UniqueExpr * unqExpr ) { |
---|
607 | unqCount[ unqExpr->get_id() ]--; |
---|
608 | static std::unordered_map< int, std::list< Statement * > > dtors; |
---|
609 | static std::unordered_map< int, UniqueExpr * > unqMap; |
---|
610 | static std::unordered_set< int > addDeref; |
---|
611 | // has to be done to clean up ImplicitCopyCtorExpr nodes, even when this node was skipped in previous passes |
---|
612 | if ( unqMap.count( unqExpr->get_id() ) ) { |
---|
613 | // take data from other UniqueExpr to ensure consistency |
---|
614 | delete unqExpr->get_expr(); |
---|
615 | unqExpr->set_expr( unqMap[unqExpr->get_id()]->get_expr()->clone() ); |
---|
616 | delete unqExpr->get_result(); |
---|
617 | unqExpr->set_result( maybeClone( unqExpr->get_expr()->get_result() ) ); |
---|
618 | if ( unqCount[ unqExpr->get_id() ] == 0 ) { // insert destructor after the last use of the unique expression |
---|
619 | stmtsToAddAfter.splice( stmtsToAddAfter.end(), dtors[ unqExpr->get_id() ] ); |
---|
620 | } |
---|
621 | if ( addDeref.count( unqExpr->get_id() ) ) { |
---|
622 | // other UniqueExpr was dereferenced because it was an lvalue return, so this one should be too |
---|
623 | return UntypedExpr::createDeref( unqExpr ); |
---|
624 | } |
---|
625 | return unqExpr; |
---|
626 | } |
---|
627 | FixCopyCtors fixer( unqCount ); |
---|
628 | unqExpr->set_expr( unqExpr->get_expr()->acceptMutator( fixer ) ); // stmtexprs contained should not be separately fixed, so this must occur after the lookup |
---|
629 | stmtsToAdd.splice( stmtsToAdd.end(), fixer.stmtsToAdd ); |
---|
630 | unqMap[unqExpr->get_id()] = unqExpr; |
---|
631 | if ( unqCount[ unqExpr->get_id() ] == 0 ) { // insert destructor after the last use of the unique expression |
---|
632 | stmtsToAddAfter.splice( stmtsToAddAfter.end(), dtors[ unqExpr->get_id() ] ); |
---|
633 | } else { // remember dtors for last instance of unique expr |
---|
634 | dtors[ unqExpr->get_id() ] = fixer.stmtsToAddAfter; |
---|
635 | } |
---|
636 | if ( UntypedExpr * deref = dynamic_cast< UntypedExpr * >( unqExpr->get_expr() ) ) { |
---|
637 | // unique expression is now a dereference, because the inner expression is an lvalue returning function call. |
---|
638 | // Normalize the expression by dereferencing the unique expression, rather than the inner expression |
---|
639 | // (i.e. move the dereference out a level) |
---|
640 | assert( getFunctionName( deref ) == "*?" ); |
---|
641 | unqExpr->set_expr( getCallArg( deref, 0 ) ); |
---|
642 | getCallArg( deref, 0 ) = unqExpr; |
---|
643 | addDeref.insert( unqExpr->get_id() ); |
---|
644 | return deref; |
---|
645 | } |
---|
646 | return unqExpr; |
---|
647 | } |
---|
648 | |
---|
649 | DeclarationWithType *FixInit::postmutate( ObjectDecl *objDecl ) { |
---|
650 | // since this removes the init field from objDecl, it must occur after children are mutated (i.e. postmutate) |
---|
651 | if ( ConstructorInit * ctorInit = dynamic_cast< ConstructorInit * >( objDecl->get_init() ) ) { |
---|
652 | // a decision should have been made by the resolver, so ctor and init are not both non-NULL |
---|
653 | assert( ! ctorInit->get_ctor() || ! ctorInit->get_init() ); |
---|
654 | if ( Statement * ctor = ctorInit->get_ctor() ) { |
---|
655 | if ( objDecl->get_storageClasses().is_static ) { |
---|
656 | // originally wanted to take advantage of gcc nested functions, but |
---|
657 | // we get memory errors with this approach. To remedy this, the static |
---|
658 | // variable is hoisted when the destructor needs to be called. |
---|
659 | // |
---|
660 | // generate: |
---|
661 | // static T __objName_static_varN; |
---|
662 | // void __objName_dtor_atexitN() { |
---|
663 | // __dtor__...; |
---|
664 | // } |
---|
665 | // int f(...) { |
---|
666 | // ... |
---|
667 | // static bool __objName_uninitialized = true; |
---|
668 | // if (__objName_uninitialized) { |
---|
669 | // __ctor(__objName); |
---|
670 | // __objName_uninitialized = false; |
---|
671 | // atexit(__objName_dtor_atexitN); |
---|
672 | // } |
---|
673 | // ... |
---|
674 | // } |
---|
675 | |
---|
676 | static UniqueName dtorCallerNamer( "_dtor_atexit" ); |
---|
677 | |
---|
678 | // static bool __objName_uninitialized = true |
---|
679 | BasicType * boolType = new BasicType( Type::Qualifiers(), BasicType::Bool ); |
---|
680 | SingleInit * boolInitExpr = new SingleInit( new ConstantExpr( Constant::from_int( 1 ) ) ); |
---|
681 | ObjectDecl * isUninitializedVar = new ObjectDecl( objDecl->get_mangleName() + "_uninitialized", Type::StorageClasses( Type::Static ), LinkageSpec::Cforall, 0, boolType, boolInitExpr ); |
---|
682 | isUninitializedVar->fixUniqueId(); |
---|
683 | |
---|
684 | // __objName_uninitialized = false; |
---|
685 | UntypedExpr * setTrue = new UntypedExpr( new NameExpr( "?=?" ) ); |
---|
686 | setTrue->get_args().push_back( new VariableExpr( isUninitializedVar ) ); |
---|
687 | setTrue->get_args().push_back( new ConstantExpr( Constant::from_int( 0 ) ) ); |
---|
688 | |
---|
689 | // generate body of if |
---|
690 | CompoundStmt * initStmts = new CompoundStmt( noLabels ); |
---|
691 | std::list< Statement * > & body = initStmts->get_kids(); |
---|
692 | body.push_back( ctor ); |
---|
693 | body.push_back( new ExprStmt( noLabels, setTrue ) ); |
---|
694 | |
---|
695 | // put it all together |
---|
696 | IfStmt * ifStmt = new IfStmt( noLabels, new VariableExpr( isUninitializedVar ), initStmts, 0 ); |
---|
697 | stmtsToAddAfter.push_back( new DeclStmt( noLabels, isUninitializedVar ) ); |
---|
698 | stmtsToAddAfter.push_back( ifStmt ); |
---|
699 | |
---|
700 | Statement * dtor = ctorInit->get_dtor(); |
---|
701 | objDecl->set_init( nullptr ); |
---|
702 | ctorInit->set_ctor( nullptr ); |
---|
703 | ctorInit->set_dtor( nullptr ); |
---|
704 | if ( dtor ) { |
---|
705 | // if the object has a non-trivial destructor, have to |
---|
706 | // hoist it and the object into the global space and |
---|
707 | // call the destructor function with atexit. |
---|
708 | |
---|
709 | Statement * dtorStmt = dtor->clone(); |
---|
710 | |
---|
711 | // void __objName_dtor_atexitN(...) {...} |
---|
712 | FunctionDecl * dtorCaller = new FunctionDecl( objDecl->get_mangleName() + dtorCallerNamer.newName(), Type::StorageClasses( Type::Static ), LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ) ); |
---|
713 | dtorCaller->fixUniqueId(); |
---|
714 | dtorCaller->get_statements()->push_back( dtorStmt ); |
---|
715 | |
---|
716 | // atexit(dtor_atexit); |
---|
717 | UntypedExpr * callAtexit = new UntypedExpr( new NameExpr( "atexit" ) ); |
---|
718 | callAtexit->get_args().push_back( new VariableExpr( dtorCaller ) ); |
---|
719 | |
---|
720 | body.push_back( new ExprStmt( noLabels, callAtexit ) ); |
---|
721 | |
---|
722 | // hoist variable and dtor caller decls to list of decls that will be added into global scope |
---|
723 | staticDtorDecls.push_back( objDecl ); |
---|
724 | staticDtorDecls.push_back( dtorCaller ); |
---|
725 | |
---|
726 | // need to rename object uniquely since it now appears |
---|
727 | // at global scope and there could be multiple function-scoped |
---|
728 | // static variables with the same name in different functions. |
---|
729 | // Note: it isn't sufficient to modify only the mangleName, because |
---|
730 | // then subsequent Indexer passes can choke on seeing the object's name |
---|
731 | // if another object has the same name and type. An unfortunate side-effect |
---|
732 | // of renaming the object is that subsequent NameExprs may fail to resolve, |
---|
733 | // but there shouldn't be any remaining past this point. |
---|
734 | static UniqueName staticNamer( "_static_var" ); |
---|
735 | objDecl->set_name( objDecl->get_name() + staticNamer.newName() ); |
---|
736 | objDecl->set_mangleName( SymTab::Mangler::mangle( objDecl ) ); |
---|
737 | |
---|
738 | // xxx - temporary hack: need to return a declaration, but want to hoist the current object out of this scope |
---|
739 | // create a new object which is never used |
---|
740 | static UniqueName dummyNamer( "_dummy" ); |
---|
741 | ObjectDecl * dummy = new ObjectDecl( dummyNamer.newName(), Type::StorageClasses( Type::Static ), LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), new VoidType( Type::Qualifiers() ) ), 0, std::list< Attribute * >{ new Attribute("unused") } ); |
---|
742 | delete ctorInit; |
---|
743 | return dummy; |
---|
744 | } |
---|
745 | } else { |
---|
746 | ImplicitCtorDtorStmt * implicit = strict_dynamic_cast< ImplicitCtorDtorStmt * > ( ctor ); |
---|
747 | ExprStmt * ctorStmt = dynamic_cast< ExprStmt * >( implicit->get_callStmt() ); |
---|
748 | ApplicationExpr * ctorCall = nullptr; |
---|
749 | if ( ctorStmt && (ctorCall = isIntrinsicCallExpr( ctorStmt->get_expr() )) && ctorCall->get_args().size() == 2 ) { |
---|
750 | // clean up intrinsic copy constructor calls by making them into SingleInits |
---|
751 | objDecl->set_init( new SingleInit( ctorCall->get_args().back() ) ); |
---|
752 | ctorCall->get_args().pop_back(); |
---|
753 | } else { |
---|
754 | stmtsToAddAfter.push_back( ctor ); |
---|
755 | objDecl->set_init( nullptr ); |
---|
756 | ctorInit->set_ctor( nullptr ); |
---|
757 | } |
---|
758 | } // if |
---|
759 | } else if ( Initializer * init = ctorInit->get_init() ) { |
---|
760 | objDecl->set_init( init ); |
---|
761 | ctorInit->set_init( nullptr ); |
---|
762 | } else { |
---|
763 | // no constructor and no initializer, which is okay |
---|
764 | objDecl->set_init( nullptr ); |
---|
765 | } // if |
---|
766 | delete ctorInit; |
---|
767 | } // if |
---|
768 | return objDecl; |
---|
769 | } |
---|
770 | |
---|
771 | void ObjDeclCollector::previsit( CompoundStmt * ) { |
---|
772 | GuardValue( curVars ); |
---|
773 | } |
---|
774 | |
---|
775 | void ObjDeclCollector::previsit( DeclStmt * stmt ) { |
---|
776 | // keep track of all variables currently in scope |
---|
777 | if ( ObjectDecl * objDecl = dynamic_cast< ObjectDecl * > ( stmt->get_decl() ) ) { |
---|
778 | curVars.push_back( objDecl ); |
---|
779 | } // if |
---|
780 | } |
---|
781 | |
---|
782 | void LabelFinder::previsit( Statement * stmt ) { |
---|
783 | // for each label, remember the variables in scope at that label. |
---|
784 | for ( Label l : stmt->get_labels() ) { |
---|
785 | vars[l] = curVars; |
---|
786 | } // for |
---|
787 | } |
---|
788 | |
---|
789 | void LabelFinder::previsit( CompoundStmt * stmt ) { |
---|
790 | previsit( (Statement *)stmt ); |
---|
791 | Parent::previsit( stmt ); |
---|
792 | } |
---|
793 | |
---|
794 | void LabelFinder::previsit( DeclStmt * stmt ) { |
---|
795 | previsit( (Statement *)stmt ); |
---|
796 | Parent::previsit( stmt ); |
---|
797 | } |
---|
798 | |
---|
799 | |
---|
800 | template<typename Iterator, typename OutputIterator> |
---|
801 | void insertDtors( Iterator begin, Iterator end, OutputIterator out ) { |
---|
802 | for ( Iterator it = begin ; it != end ; ++it ) { |
---|
803 | // extract destructor statement from the object decl and insert it into the output. Note that this is |
---|
804 | // only called on lists of non-static objects with implicit non-intrinsic dtors, so if the user manually |
---|
805 | // calls an intrinsic dtor then the call must (and will) still be generated since the argument may |
---|
806 | // contain side effects. |
---|
807 | ObjectDecl * objDecl = *it; |
---|
808 | ConstructorInit * ctorInit = dynamic_cast< ConstructorInit * >( objDecl->get_init() ); |
---|
809 | assert( ctorInit && ctorInit->get_dtor() ); |
---|
810 | *out++ = ctorInit->get_dtor()->clone(); |
---|
811 | } // for |
---|
812 | } |
---|
813 | |
---|
814 | void InsertDtors::previsit( ObjectDecl * objDecl ) { |
---|
815 | // remember non-static destructed objects so that their destructors can be inserted later |
---|
816 | if ( ! objDecl->get_storageClasses().is_static ) { |
---|
817 | if ( ConstructorInit * ctorInit = dynamic_cast< ConstructorInit * >( objDecl->get_init() ) ) { |
---|
818 | // a decision should have been made by the resolver, so ctor and init are not both non-NULL |
---|
819 | assert( ! ctorInit->get_ctor() || ! ctorInit->get_init() ); |
---|
820 | Statement * dtor = ctorInit->get_dtor(); |
---|
821 | // don't need to call intrinsic dtor, because it does nothing, but |
---|
822 | // non-intrinsic dtors must be called |
---|
823 | if ( dtor && ! isIntrinsicSingleArgCallStmt( dtor ) ) { |
---|
824 | // set dtor location to the object's location for error messages |
---|
825 | ctorInit->dtor->location = objDecl->location; |
---|
826 | reverseDeclOrder.front().push_front( objDecl ); |
---|
827 | } // if |
---|
828 | } // if |
---|
829 | } // if |
---|
830 | } |
---|
831 | |
---|
832 | void InsertDtors::previsit( FunctionDecl * funcDecl ) { |
---|
833 | // each function needs to have its own set of labels |
---|
834 | GuardValue( labelVars ); |
---|
835 | labelVars.clear(); |
---|
836 | maybeAccept( funcDecl->type, finder ); |
---|
837 | maybeAccept( funcDecl->statements, finder ); |
---|
838 | |
---|
839 | // all labels for this function have been collected, insert destructors as appropriate via implicit recursion. |
---|
840 | } |
---|
841 | |
---|
842 | void InsertDtors::previsit( CompoundStmt * compoundStmt ) { |
---|
843 | // visit statements - this will also populate reverseDeclOrder list. don't want to dump all destructors |
---|
844 | // when block is left, just the destructors associated with variables defined in this block, so push a new |
---|
845 | // list to the top of the stack so that we can differentiate scopes |
---|
846 | reverseDeclOrder.push_front( OrderedDecls() ); |
---|
847 | Parent::previsit( compoundStmt ); |
---|
848 | } |
---|
849 | |
---|
850 | void InsertDtors::postvisit( CompoundStmt * compoundStmt ) { |
---|
851 | // add destructors for the current scope that we're exiting, unless the last statement is a return, which |
---|
852 | // causes unreachable code warnings |
---|
853 | std::list< Statement * > & statements = compoundStmt->get_kids(); |
---|
854 | if ( ! statements.empty() && ! dynamic_cast< ReturnStmt * >( statements.back() ) ) { |
---|
855 | insertDtors( reverseDeclOrder.front().begin(), reverseDeclOrder.front().end(), back_inserter( statements ) ); |
---|
856 | } |
---|
857 | reverseDeclOrder.pop_front(); |
---|
858 | } |
---|
859 | |
---|
860 | void InsertDtors::previsit( ReturnStmt * ) { |
---|
861 | // return exits all scopes, so dump destructors for all scopes |
---|
862 | for ( OrderedDecls & od : reverseDeclOrder ) { |
---|
863 | insertDtors( od.begin(), od.end(), back_inserter( stmtsToAddBefore ) ); |
---|
864 | } // for |
---|
865 | } |
---|
866 | |
---|
867 | // Handle break/continue/goto in the same manner as C++. Basic idea: any objects that are in scope at the |
---|
868 | // BranchStmt but not at the labelled (target) statement must be destructed. If there are any objects in scope |
---|
869 | // at the target location but not at the BranchStmt then those objects would be uninitialized so notify the user |
---|
870 | // of the error. See C++ Reference 6.6 Jump Statements for details. |
---|
871 | void InsertDtors::handleGoto( BranchStmt * stmt ) { |
---|
872 | // can't do anything for computed goto |
---|
873 | if ( stmt->computedTarget ) return; |
---|
874 | |
---|
875 | assertf( stmt->get_target() != "", "BranchStmt missing a label: %s", toString( stmt ).c_str() ); |
---|
876 | // S_L = lvars = set of objects in scope at label definition |
---|
877 | // S_G = curVars = set of objects in scope at goto statement |
---|
878 | ObjectSet & lvars = labelVars[ stmt->get_target() ]; |
---|
879 | |
---|
880 | DTOR_PRINT( |
---|
881 | std::cerr << "at goto label: " << stmt->get_target().get_name() << std::endl; |
---|
882 | std::cerr << "S_G = " << printSet( curVars ) << std::endl; |
---|
883 | std::cerr << "S_L = " << printSet( lvars ) << std::endl; |
---|
884 | ) |
---|
885 | |
---|
886 | ObjectSet diff; |
---|
887 | // S_L-S_G results in set of objects whose construction is skipped - it's an error if this set is non-empty |
---|
888 | std::set_difference( lvars.begin(), lvars.end(), curVars.begin(), curVars.end(), std::inserter( diff, diff.begin() ) ); |
---|
889 | DTOR_PRINT( |
---|
890 | std::cerr << "S_L-S_G = " << printSet( diff ) << std::endl; |
---|
891 | ) |
---|
892 | if ( ! diff.empty() ) { |
---|
893 | throw SemanticError( std::string("jump to label '") + stmt->get_target().get_name() + "' crosses initialization of " + (*diff.begin())->get_name() + " ", stmt ); |
---|
894 | } // if |
---|
895 | // S_G-S_L results in set of objects that must be destructed |
---|
896 | diff.clear(); |
---|
897 | std::set_difference( curVars.begin(), curVars.end(), lvars.begin(), lvars.end(), std::inserter( diff, diff.end() ) ); |
---|
898 | DTOR_PRINT( |
---|
899 | std::cerr << "S_G-S_L = " << printSet( diff ) << std::endl; |
---|
900 | ) |
---|
901 | if ( ! diff.empty() ) { |
---|
902 | // create an auxilliary set for fast lookup -- can't make diff a set, because diff ordering should be consistent for error messages. |
---|
903 | std::unordered_set<ObjectDecl *> needsDestructor( diff.begin(), diff.end() ); |
---|
904 | |
---|
905 | // go through decl ordered list of objectdecl. for each element that occurs in diff, output destructor |
---|
906 | OrderedDecls ordered; |
---|
907 | for ( OrderedDecls & rdo : reverseDeclOrder ) { |
---|
908 | // add elements from reverseDeclOrder into ordered if they occur in diff - it is key that this happens in reverse declaration order. |
---|
909 | copy_if( rdo.begin(), rdo.end(), back_inserter( ordered ), [&]( ObjectDecl * objDecl ) { return needsDestructor.count( objDecl ); } ); |
---|
910 | } // for |
---|
911 | insertDtors( ordered.begin(), ordered.end(), back_inserter( stmtsToAddBefore ) ); |
---|
912 | } // if |
---|
913 | } |
---|
914 | |
---|
915 | void InsertDtors::previsit( BranchStmt * stmt ) { |
---|
916 | switch( stmt->get_type() ) { |
---|
917 | case BranchStmt::Continue: |
---|
918 | case BranchStmt::Break: |
---|
919 | // could optimize the break/continue case, because the S_L-S_G check is unnecessary (this set should |
---|
920 | // always be empty), but it serves as a small sanity check. |
---|
921 | case BranchStmt::Goto: |
---|
922 | handleGoto( stmt ); |
---|
923 | break; |
---|
924 | default: |
---|
925 | assert( false ); |
---|
926 | } // switch |
---|
927 | } |
---|
928 | |
---|
929 | bool checkWarnings( FunctionDecl * funcDecl ) { |
---|
930 | // only check for warnings if the current function is a user-defined |
---|
931 | // constructor or destructor |
---|
932 | if ( ! funcDecl ) return false; |
---|
933 | if ( ! funcDecl->get_statements() ) return false; |
---|
934 | return CodeGen::isCtorDtor( funcDecl->get_name() ) && ! LinkageSpec::isOverridable( funcDecl->get_linkage() ); |
---|
935 | } |
---|
936 | |
---|
937 | void GenStructMemberCalls::previsit( FunctionDecl * funcDecl ) { |
---|
938 | GuardValue( funcDecl ); |
---|
939 | GuardValue( unhandled ); |
---|
940 | GuardValue( usedUninit ); |
---|
941 | GuardValue( thisParam ); |
---|
942 | GuardValue( isCtor ); |
---|
943 | GuardValue( structDecl ); |
---|
944 | errors = SemanticError(); // clear previous errors |
---|
945 | |
---|
946 | // need to start with fresh sets |
---|
947 | unhandled.clear(); |
---|
948 | usedUninit.clear(); |
---|
949 | |
---|
950 | function = funcDecl; |
---|
951 | isCtor = CodeGen::isConstructor( function->get_name() ); |
---|
952 | if ( checkWarnings( function ) ) { |
---|
953 | FunctionType * type = function->get_functionType(); |
---|
954 | assert( ! type->get_parameters().empty() ); |
---|
955 | thisParam = strict_dynamic_cast< ObjectDecl * >( type->get_parameters().front() ); |
---|
956 | Type * thisType = getPointerBase( thisParam->get_type() ); |
---|
957 | StructInstType * structType = dynamic_cast< StructInstType * >( thisType ); |
---|
958 | if ( structType ) { |
---|
959 | structDecl = structType->get_baseStruct(); |
---|
960 | for ( Declaration * member : structDecl->get_members() ) { |
---|
961 | if ( ObjectDecl * field = dynamic_cast< ObjectDecl * >( member ) ) { |
---|
962 | // record all of the struct type's members that need to be constructed or |
---|
963 | // destructed by the end of the function |
---|
964 | unhandled.insert( field ); |
---|
965 | } |
---|
966 | } |
---|
967 | } |
---|
968 | } |
---|
969 | } |
---|
970 | |
---|
971 | void addIds( SymTab::Indexer & indexer, const std::list< DeclarationWithType * > & decls ) { |
---|
972 | for ( auto d : decls ) { |
---|
973 | indexer.addId( d ); |
---|
974 | } |
---|
975 | } |
---|
976 | |
---|
977 | void addTypes( SymTab::Indexer & indexer, const std::list< TypeDecl * > & tds ) { |
---|
978 | for ( auto td : tds ) { |
---|
979 | indexer.addType( td ); |
---|
980 | addIds( indexer, td->assertions ); |
---|
981 | } |
---|
982 | } |
---|
983 | |
---|
984 | void GenStructMemberCalls::postvisit( FunctionDecl * funcDecl ) { |
---|
985 | // remove the unhandled objects from usedUninit, because a call is inserted |
---|
986 | // to handle them - only objects that are later constructed are used uninitialized. |
---|
987 | std::map< DeclarationWithType *, CodeLocation > diff; |
---|
988 | // need the comparator since usedUninit and unhandled have different types |
---|
989 | struct comp_t { |
---|
990 | typedef decltype(usedUninit)::value_type usedUninit_t; |
---|
991 | typedef decltype(unhandled)::value_type unhandled_t; |
---|
992 | bool operator()(usedUninit_t x, unhandled_t y) { return x.first < y; } |
---|
993 | bool operator()(unhandled_t x, usedUninit_t y) { return x < y.first; } |
---|
994 | } comp; |
---|
995 | std::set_difference( usedUninit.begin(), usedUninit.end(), unhandled.begin(), unhandled.end(), std::inserter( diff, diff.begin() ), comp ); |
---|
996 | for ( auto p : diff ) { |
---|
997 | DeclarationWithType * member = p.first; |
---|
998 | CodeLocation loc = p.second; |
---|
999 | // xxx - make error message better by also tracking the location that the object is constructed at? |
---|
1000 | emit( loc, "in ", CodeGen::genPrettyType( function->get_functionType(), function->get_name() ), ", field ", member->get_name(), " used before being constructed" ); |
---|
1001 | } |
---|
1002 | |
---|
1003 | if ( ! unhandled.empty() ) { |
---|
1004 | // need to explicitly re-add function parameters to the indexer in order to resolve copy constructors |
---|
1005 | auto guard = makeFuncGuard( [this]() { indexer.enterScope(); }, [this]() { indexer.leaveScope(); } ); |
---|
1006 | addTypes( indexer, function->type->forall ); |
---|
1007 | addIds( indexer, function->type->returnVals ); |
---|
1008 | addIds( indexer, function->type->parameters ); |
---|
1009 | |
---|
1010 | // need to iterate through members in reverse in order for |
---|
1011 | // ctor/dtor statements to come out in the right order |
---|
1012 | for ( Declaration * member : reverseIterate( structDecl->get_members() ) ) { |
---|
1013 | DeclarationWithType * field = dynamic_cast< DeclarationWithType * >( member ); |
---|
1014 | // skip non-DWT members |
---|
1015 | if ( ! field ) continue; |
---|
1016 | // skip non-constructable members |
---|
1017 | if ( ! tryConstruct( field ) ) continue; |
---|
1018 | // skip handled members |
---|
1019 | if ( ! unhandled.count( field ) ) continue; |
---|
1020 | |
---|
1021 | // insert and resolve default/copy constructor call for each field that's unhandled |
---|
1022 | std::list< Statement * > stmt; |
---|
1023 | Expression * arg2 = 0; |
---|
1024 | if ( isCopyConstructor( function ) ) { |
---|
1025 | // if copy ctor, need to pass second-param-of-this-function.field |
---|
1026 | std::list< DeclarationWithType * > & params = function->get_functionType()->get_parameters(); |
---|
1027 | assert( params.size() == 2 ); |
---|
1028 | arg2 = new MemberExpr( field, new VariableExpr( params.back() ) ); |
---|
1029 | } |
---|
1030 | InitExpander srcParam( arg2 ); |
---|
1031 | // cast away reference type and construct field. |
---|
1032 | Expression * thisExpr = new CastExpr( new VariableExpr( thisParam ), thisParam->get_type()->stripReferences()->clone() ); |
---|
1033 | Expression * memberDest = new MemberExpr( field, thisExpr ); |
---|
1034 | SymTab::genImplicitCall( srcParam, memberDest, function->get_name(), back_inserter( stmt ), field, isCtor ); |
---|
1035 | |
---|
1036 | assert( stmt.size() <= 1 ); |
---|
1037 | if ( stmt.size() == 1 ) { |
---|
1038 | Statement * callStmt = stmt.front(); |
---|
1039 | |
---|
1040 | MutatingResolver resolver( indexer ); |
---|
1041 | try { |
---|
1042 | callStmt->acceptMutator( resolver ); |
---|
1043 | if ( isCtor ) { |
---|
1044 | function->get_statements()->push_front( callStmt ); |
---|
1045 | } else { |
---|
1046 | // destructor statements should be added at the end |
---|
1047 | function->get_statements()->push_back( callStmt ); |
---|
1048 | } |
---|
1049 | } catch ( SemanticError & error ) { |
---|
1050 | emit( funcDecl->location, "in ", CodeGen::genPrettyType( function->get_functionType(), function->get_name() ), ", field ", field->get_name(), " not explicitly ", isCtor ? "constructed" : "destructed", " and no ", isCtor ? "default constructor" : "destructor", " found" ); |
---|
1051 | } |
---|
1052 | } |
---|
1053 | } |
---|
1054 | } |
---|
1055 | if (! errors.isEmpty()) { |
---|
1056 | throw errors; |
---|
1057 | } |
---|
1058 | } |
---|
1059 | |
---|
1060 | /// true if expr is effectively just the 'this' parameter |
---|
1061 | bool isThisExpression( Expression * expr, DeclarationWithType * thisParam ) { |
---|
1062 | // TODO: there are more complicated ways to pass 'this' to a constructor, e.g. &*, *&, etc. |
---|
1063 | if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( expr ) ) { |
---|
1064 | return varExpr->get_var() == thisParam; |
---|
1065 | } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * > ( expr ) ) { |
---|
1066 | return isThisExpression( castExpr->get_arg(), thisParam ); |
---|
1067 | } |
---|
1068 | return false; |
---|
1069 | } |
---|
1070 | |
---|
1071 | /// returns a MemberExpr if expr is effectively just member access on the 'this' parameter, else nullptr |
---|
1072 | MemberExpr * isThisMemberExpr( Expression * expr, DeclarationWithType * thisParam ) { |
---|
1073 | if ( MemberExpr * memberExpr = dynamic_cast< MemberExpr * >( expr ) ) { |
---|
1074 | if ( isThisExpression( memberExpr->get_aggregate(), thisParam ) ) { |
---|
1075 | return memberExpr; |
---|
1076 | } |
---|
1077 | } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) { |
---|
1078 | return isThisMemberExpr( castExpr->get_arg(), thisParam ); |
---|
1079 | } |
---|
1080 | return nullptr; |
---|
1081 | } |
---|
1082 | |
---|
1083 | void GenStructMemberCalls::previsit( ApplicationExpr * appExpr ) { |
---|
1084 | if ( ! checkWarnings( function ) ) { |
---|
1085 | visit_children = false; |
---|
1086 | return; |
---|
1087 | } |
---|
1088 | |
---|
1089 | std::string fname = getFunctionName( appExpr ); |
---|
1090 | if ( fname == function->get_name() ) { |
---|
1091 | // call to same kind of function |
---|
1092 | Expression * firstParam = appExpr->get_args().front(); |
---|
1093 | |
---|
1094 | if ( isThisExpression( firstParam, thisParam ) ) { |
---|
1095 | // if calling another constructor on thisParam, assume that function handles |
---|
1096 | // all members - if it doesn't a warning will appear in that function. |
---|
1097 | unhandled.clear(); |
---|
1098 | } else if ( MemberExpr * memberExpr = isThisMemberExpr( firstParam, thisParam ) ) { |
---|
1099 | // if first parameter is a member expression on the this parameter, |
---|
1100 | // then remove the member from unhandled set. |
---|
1101 | if ( isThisExpression( memberExpr->get_aggregate(), thisParam ) ) { |
---|
1102 | unhandled.erase( memberExpr->get_member() ); |
---|
1103 | } |
---|
1104 | } |
---|
1105 | } |
---|
1106 | } |
---|
1107 | |
---|
1108 | void GenStructMemberCalls::previsit( MemberExpr * memberExpr ) { |
---|
1109 | if ( ! checkWarnings( function ) || ! isCtor ) { |
---|
1110 | visit_children = false; |
---|
1111 | return; |
---|
1112 | } |
---|
1113 | |
---|
1114 | if ( isThisExpression( memberExpr->get_aggregate(), thisParam ) ) { |
---|
1115 | if ( unhandled.count( memberExpr->get_member() ) ) { |
---|
1116 | // emit a warning because a member was used before it was constructed |
---|
1117 | usedUninit.insert( { memberExpr->get_member(), memberExpr->location } ); |
---|
1118 | } |
---|
1119 | } |
---|
1120 | } |
---|
1121 | |
---|
1122 | template< typename Visitor, typename... Params > |
---|
1123 | void error( Visitor & v, CodeLocation loc, const Params &... params ) { |
---|
1124 | SemanticError err( toString( params... ) ); |
---|
1125 | err.set_location( loc ); |
---|
1126 | v.errors.append( err ); |
---|
1127 | } |
---|
1128 | |
---|
1129 | template< typename... Params > |
---|
1130 | void GenStructMemberCalls::emit( CodeLocation loc, const Params &... params ) { |
---|
1131 | // toggle warnings vs. errors here. |
---|
1132 | // warn( params... ); |
---|
1133 | error( *this, loc, params... ); |
---|
1134 | } |
---|
1135 | |
---|
1136 | DeclarationWithType * MutatingResolver::mutate( ObjectDecl *objectDecl ) { |
---|
1137 | // add object to the indexer assumes that there will be no name collisions |
---|
1138 | // in generated code. If this changes, add mutate methods for entities with |
---|
1139 | // scope and call {enter,leave}Scope explicitly. |
---|
1140 | indexer.addId( objectDecl ); |
---|
1141 | return objectDecl; |
---|
1142 | } |
---|
1143 | |
---|
1144 | Expression* MutatingResolver::mutate( UntypedExpr *untypedExpr ) { |
---|
1145 | return strict_dynamic_cast< ApplicationExpr * >( ResolvExpr::findVoidExpression( untypedExpr, indexer ) ); |
---|
1146 | } |
---|
1147 | |
---|
1148 | Expression * FixCtorExprs::mutate( ConstructorExpr * ctorExpr ) { |
---|
1149 | static UniqueName tempNamer( "_tmp_ctor_expr" ); |
---|
1150 | // xxx - is the size check necessary? |
---|
1151 | assert( ctorExpr->has_result() && ctorExpr->get_result()->size() == 1 ); |
---|
1152 | |
---|
1153 | // xxx - ideally we would reuse the temporary generated from the copy constructor passes from within firstArg if it exists and not generate a temporary if it's unnecessary. |
---|
1154 | ObjectDecl * tmp = ObjectDecl::newObject( tempNamer.newName(), ctorExpr->get_result()->clone(), nullptr ); |
---|
1155 | addDeclaration( tmp ); |
---|
1156 | |
---|
1157 | // xxx - this can be TupleAssignExpr now. Need to properly handle this case. |
---|
1158 | ApplicationExpr * callExpr = strict_dynamic_cast< ApplicationExpr * > ( ctorExpr->get_callExpr() ); |
---|
1159 | TypeSubstitution * env = ctorExpr->get_env(); |
---|
1160 | ctorExpr->set_callExpr( nullptr ); |
---|
1161 | ctorExpr->set_env( nullptr ); |
---|
1162 | delete ctorExpr; |
---|
1163 | |
---|
1164 | Expression *& firstArg = callExpr->get_args().front(); |
---|
1165 | |
---|
1166 | // xxx - hack in 'fake' assignment operator until resolver can easily be called in this pass. Once the resolver can be used in PassVisitor, this hack goes away. |
---|
1167 | |
---|
1168 | // generate the type of assignment operator using the type of tmp minus any reference types |
---|
1169 | Type * type = tmp->get_type()->stripReferences(); |
---|
1170 | FunctionType * ftype = SymTab::genAssignType( type ); |
---|
1171 | |
---|
1172 | // generate fake assignment decl and call it using &tmp and &firstArg |
---|
1173 | // since tmp is guaranteed to be a reference and we want to assign pointers |
---|
1174 | FunctionDecl * assignDecl = new FunctionDecl( "?=?", Type::StorageClasses(), LinkageSpec::Intrinsic, ftype, nullptr ); |
---|
1175 | ApplicationExpr * assign = new ApplicationExpr( VariableExpr::functionPointer( assignDecl ) ); |
---|
1176 | assign->get_args().push_back( new AddressExpr( new VariableExpr( tmp ) ) ); |
---|
1177 | Expression * addrArg = new AddressExpr( firstArg ); |
---|
1178 | // if firstArg has type T&&, then &firstArg has type T*&. |
---|
1179 | // Cast away the reference to a value type so that the argument |
---|
1180 | // matches the assignment's parameter types |
---|
1181 | if ( dynamic_cast<ReferenceType *>( addrArg->get_result() ) ) { |
---|
1182 | addrArg = new CastExpr( addrArg, addrArg->get_result()->stripReferences()->clone() ); |
---|
1183 | } |
---|
1184 | assign->get_args().push_back( addrArg ); |
---|
1185 | firstArg = new VariableExpr( tmp ); |
---|
1186 | |
---|
1187 | // for constructor expr: |
---|
1188 | // T x; |
---|
1189 | // x{}; |
---|
1190 | // results in: |
---|
1191 | // T x; |
---|
1192 | // T & tmp; |
---|
1193 | // &tmp = &x, ?{}(tmp), tmp |
---|
1194 | CommaExpr * commaExpr = new CommaExpr( assign, new CommaExpr( callExpr, new VariableExpr( tmp ) ) ); |
---|
1195 | commaExpr->set_env( env ); |
---|
1196 | return commaExpr; |
---|
1197 | } |
---|
1198 | } // namespace |
---|
1199 | } // namespace InitTweak |
---|
1200 | |
---|
1201 | // Local Variables: // |
---|
1202 | // tab-width: 4 // |
---|
1203 | // mode: c++ // |
---|
1204 | // compile-command: "make install" // |
---|
1205 | // End: // |
---|