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 | // Resolver.cc -- |
---|
8 | // |
---|
9 | // Author : Aaron B. Moss |
---|
10 | // Created On : Sun May 17 12:17:01 2015 |
---|
11 | // Last Modified By : Andrew Beach |
---|
12 | // Last Modified On : Fri Mar 27 11:58:00 2020 |
---|
13 | // Update Count : 242 |
---|
14 | // |
---|
15 | |
---|
16 | #include <cassert> // for strict_dynamic_cast, assert |
---|
17 | #include <memory> // for allocator, allocator_traits<... |
---|
18 | #include <tuple> // for get |
---|
19 | #include <vector> // for vector |
---|
20 | |
---|
21 | #include "Alternative.h" // for Alternative, AltList |
---|
22 | #include "AlternativeFinder.h" // for AlternativeFinder, resolveIn... |
---|
23 | #include "Candidate.hpp" |
---|
24 | #include "CandidateFinder.hpp" |
---|
25 | #include "CurrentObject.h" // for CurrentObject |
---|
26 | #include "RenameVars.h" // for RenameVars, global_renamer |
---|
27 | #include "Resolver.h" |
---|
28 | #include "ResolvMode.h" // for ResolvMode |
---|
29 | #include "typeops.h" // for extractResultType |
---|
30 | #include "Unify.h" // for unify |
---|
31 | #include "AST/Chain.hpp" |
---|
32 | #include "AST/Decl.hpp" |
---|
33 | #include "AST/Init.hpp" |
---|
34 | #include "AST/Pass.hpp" |
---|
35 | #include "AST/Print.hpp" |
---|
36 | #include "AST/SymbolTable.hpp" |
---|
37 | #include "AST/Type.hpp" |
---|
38 | #include "Common/PassVisitor.h" // for PassVisitor |
---|
39 | #include "Common/SemanticError.h" // for SemanticError |
---|
40 | #include "Common/utility.h" // for ValueGuard, group_iterate |
---|
41 | #include "InitTweak/GenInit.h" |
---|
42 | #include "InitTweak/InitTweak.h" // for isIntrinsicSingleArgCallStmt |
---|
43 | #include "ResolvExpr/TypeEnvironment.h" // for TypeEnvironment |
---|
44 | #include "SymTab/Autogen.h" // for SizeType |
---|
45 | #include "SymTab/Indexer.h" // for Indexer |
---|
46 | #include "SynTree/Declaration.h" // for ObjectDecl, TypeDecl, Declar... |
---|
47 | #include "SynTree/Expression.h" // for Expression, CastExpr, InitExpr |
---|
48 | #include "SynTree/Initializer.h" // for ConstructorInit, SingleInit |
---|
49 | #include "SynTree/Statement.h" // for ForStmt, Statement, BranchStmt |
---|
50 | #include "SynTree/Type.h" // for Type, BasicType, PointerType |
---|
51 | #include "SynTree/TypeSubstitution.h" // for TypeSubstitution |
---|
52 | #include "SynTree/Visitor.h" // for acceptAll, maybeAccept |
---|
53 | #include "Tuples/Tuples.h" |
---|
54 | #include "Validate/FindSpecialDecls.h" // for SizeType |
---|
55 | |
---|
56 | using namespace std; |
---|
57 | |
---|
58 | namespace ResolvExpr { |
---|
59 | struct Resolver_old final : public WithIndexer, public WithGuards, public WithVisitorRef<Resolver_old>, public WithShortCircuiting, public WithStmtsToAdd { |
---|
60 | Resolver_old() {} |
---|
61 | Resolver_old( const SymTab::Indexer & other ) { |
---|
62 | indexer = other; |
---|
63 | } |
---|
64 | |
---|
65 | void previsit( FunctionDecl * functionDecl ); |
---|
66 | void postvisit( FunctionDecl * functionDecl ); |
---|
67 | void previsit( ObjectDecl * objectDecll ); |
---|
68 | void previsit( EnumDecl * enumDecl ); |
---|
69 | void previsit( StaticAssertDecl * assertDecl ); |
---|
70 | |
---|
71 | void previsit( ArrayType * at ); |
---|
72 | void previsit( PointerType * at ); |
---|
73 | |
---|
74 | void previsit( ExprStmt * exprStmt ); |
---|
75 | void previsit( AsmExpr * asmExpr ); |
---|
76 | void previsit( AsmStmt * asmStmt ); |
---|
77 | void previsit( IfStmt * ifStmt ); |
---|
78 | void previsit( WhileStmt * whileStmt ); |
---|
79 | void previsit( ForStmt * forStmt ); |
---|
80 | void previsit( SwitchStmt * switchStmt ); |
---|
81 | void previsit( CaseStmt * caseStmt ); |
---|
82 | void previsit( BranchStmt * branchStmt ); |
---|
83 | void previsit( ReturnStmt * returnStmt ); |
---|
84 | void previsit( ThrowStmt * throwStmt ); |
---|
85 | void previsit( CatchStmt * catchStmt ); |
---|
86 | void postvisit( CatchStmt * catchStmt ); |
---|
87 | void previsit( WaitForStmt * stmt ); |
---|
88 | |
---|
89 | void previsit( SingleInit * singleInit ); |
---|
90 | void previsit( ListInit * listInit ); |
---|
91 | void previsit( ConstructorInit * ctorInit ); |
---|
92 | private: |
---|
93 | typedef std::list< Initializer * >::iterator InitIterator; |
---|
94 | |
---|
95 | template< typename PtrType > |
---|
96 | void handlePtrType( PtrType * type ); |
---|
97 | |
---|
98 | void fallbackInit( ConstructorInit * ctorInit ); |
---|
99 | |
---|
100 | Type * functionReturn = nullptr; |
---|
101 | CurrentObject currentObject = nullptr; |
---|
102 | bool inEnumDecl = false; |
---|
103 | }; |
---|
104 | |
---|
105 | struct ResolveWithExprs : public WithIndexer, public WithGuards, public WithVisitorRef<ResolveWithExprs>, public WithShortCircuiting, public WithStmtsToAdd { |
---|
106 | void previsit( FunctionDecl * ); |
---|
107 | void previsit( WithStmt * ); |
---|
108 | |
---|
109 | void resolveWithExprs( std::list< Expression * > & withExprs, std::list< Statement * > & newStmts ); |
---|
110 | }; |
---|
111 | |
---|
112 | void resolve( std::list< Declaration * > translationUnit ) { |
---|
113 | PassVisitor<Resolver_old> resolver; |
---|
114 | acceptAll( translationUnit, resolver ); |
---|
115 | } |
---|
116 | |
---|
117 | void resolveDecl( Declaration * decl, const SymTab::Indexer & indexer ) { |
---|
118 | PassVisitor<Resolver_old> resolver( indexer ); |
---|
119 | maybeAccept( decl, resolver ); |
---|
120 | } |
---|
121 | |
---|
122 | namespace { |
---|
123 | struct DeleteFinder_old : public WithShortCircuiting { |
---|
124 | DeletedExpr * delExpr = nullptr; |
---|
125 | void previsit( DeletedExpr * expr ) { |
---|
126 | if ( delExpr ) visit_children = false; |
---|
127 | else delExpr = expr; |
---|
128 | } |
---|
129 | |
---|
130 | void previsit( Expression * ) { |
---|
131 | if ( delExpr ) visit_children = false; |
---|
132 | } |
---|
133 | }; |
---|
134 | } |
---|
135 | |
---|
136 | DeletedExpr * findDeletedExpr( Expression * expr ) { |
---|
137 | PassVisitor<DeleteFinder_old> finder; |
---|
138 | expr->accept( finder ); |
---|
139 | return finder.pass.delExpr; |
---|
140 | } |
---|
141 | |
---|
142 | namespace { |
---|
143 | struct StripCasts_old { |
---|
144 | Expression * postmutate( CastExpr * castExpr ) { |
---|
145 | if ( castExpr->isGenerated && ResolvExpr::typesCompatible( castExpr->arg->result, castExpr->result, SymTab::Indexer() ) ) { |
---|
146 | // generated cast is to the same type as its argument, so it's unnecessary -- remove it |
---|
147 | Expression * expr = castExpr->arg; |
---|
148 | castExpr->arg = nullptr; |
---|
149 | std::swap( expr->env, castExpr->env ); |
---|
150 | return expr; |
---|
151 | } |
---|
152 | return castExpr; |
---|
153 | } |
---|
154 | |
---|
155 | static void strip( Expression *& expr ) { |
---|
156 | PassVisitor<StripCasts_old> stripper; |
---|
157 | expr = expr->acceptMutator( stripper ); |
---|
158 | } |
---|
159 | }; |
---|
160 | |
---|
161 | void finishExpr( Expression *& expr, const TypeEnvironment & env, TypeSubstitution * oldenv = nullptr ) { |
---|
162 | expr->env = oldenv ? oldenv->clone() : new TypeSubstitution; |
---|
163 | env.makeSubstitution( *expr->env ); |
---|
164 | StripCasts_old::strip( expr ); // remove unnecessary casts that may be buried in an expression |
---|
165 | } |
---|
166 | |
---|
167 | void removeExtraneousCast( Expression *& expr, const SymTab::Indexer & indexer ) { |
---|
168 | if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) { |
---|
169 | if ( typesCompatible( castExpr->arg->result, castExpr->result, indexer ) ) { |
---|
170 | // cast is to the same type as its argument, so it's unnecessary -- remove it |
---|
171 | expr = castExpr->arg; |
---|
172 | castExpr->arg = nullptr; |
---|
173 | std::swap( expr->env, castExpr->env ); |
---|
174 | delete castExpr; |
---|
175 | } |
---|
176 | } |
---|
177 | } |
---|
178 | } // namespace |
---|
179 | |
---|
180 | namespace { |
---|
181 | void findUnfinishedKindExpression(Expression * untyped, Alternative & alt, const SymTab::Indexer & indexer, const std::string & kindStr, std::function<bool(const Alternative &)> pred, ResolvMode mode = ResolvMode{} ) { |
---|
182 | assertf( untyped, "expected a non-null expression." ); |
---|
183 | |
---|
184 | // xxx - this isn't thread-safe, but should work until we parallelize the resolver |
---|
185 | static unsigned recursion_level = 0; |
---|
186 | |
---|
187 | ++recursion_level; |
---|
188 | TypeEnvironment env; |
---|
189 | AlternativeFinder finder( indexer, env ); |
---|
190 | finder.find( untyped, recursion_level == 1 ? mode.atTopLevel() : mode ); |
---|
191 | --recursion_level; |
---|
192 | |
---|
193 | #if 0 |
---|
194 | if ( finder.get_alternatives().size() != 1 ) { |
---|
195 | std::cerr << "untyped expr is "; |
---|
196 | untyped->print( std::cerr ); |
---|
197 | std::cerr << std::endl << "alternatives are:"; |
---|
198 | for ( const Alternative & alt : finder.get_alternatives() ) { |
---|
199 | alt.print( std::cerr ); |
---|
200 | } // for |
---|
201 | } // if |
---|
202 | #endif |
---|
203 | |
---|
204 | // produce filtered list of alternatives |
---|
205 | AltList candidates; |
---|
206 | for ( Alternative & alt : finder.get_alternatives() ) { |
---|
207 | if ( pred( alt ) ) { |
---|
208 | candidates.push_back( std::move( alt ) ); |
---|
209 | } |
---|
210 | } |
---|
211 | |
---|
212 | // produce invalid error if no candidates |
---|
213 | if ( candidates.empty() ) { |
---|
214 | SemanticError( untyped, toString( "No reasonable alternatives for ", kindStr, (kindStr != "" ? " " : ""), "expression: ") ); |
---|
215 | } |
---|
216 | |
---|
217 | // search for cheapest candidate |
---|
218 | AltList winners; |
---|
219 | bool seen_undeleted = false; |
---|
220 | for ( unsigned i = 0; i < candidates.size(); ++i ) { |
---|
221 | int c = winners.empty() ? -1 : candidates[i].cost.compare( winners.front().cost ); |
---|
222 | |
---|
223 | if ( c > 0 ) continue; // skip more expensive than winner |
---|
224 | |
---|
225 | if ( c < 0 ) { |
---|
226 | // reset on new cheapest |
---|
227 | seen_undeleted = ! findDeletedExpr( candidates[i].expr ); |
---|
228 | winners.clear(); |
---|
229 | } else /* if ( c == 0 ) */ { |
---|
230 | if ( findDeletedExpr( candidates[i].expr ) ) { |
---|
231 | // skip deleted expression if already seen one equivalent-cost not |
---|
232 | if ( seen_undeleted ) continue; |
---|
233 | } else if ( ! seen_undeleted ) { |
---|
234 | // replace list of equivalent-cost deleted expressions with one non-deleted |
---|
235 | winners.clear(); |
---|
236 | seen_undeleted = true; |
---|
237 | } |
---|
238 | } |
---|
239 | |
---|
240 | winners.emplace_back( std::move( candidates[i] ) ); |
---|
241 | } |
---|
242 | |
---|
243 | // promote alternative.cvtCost to .cost |
---|
244 | // xxx - I don't know why this is done, but I'm keeping the behaviour from findMinCost |
---|
245 | for ( Alternative& winner : winners ) { |
---|
246 | winner.cost = winner.cvtCost; |
---|
247 | } |
---|
248 | |
---|
249 | // produce ambiguous errors, if applicable |
---|
250 | if ( winners.size() != 1 ) { |
---|
251 | std::ostringstream stream; |
---|
252 | stream << "Cannot choose between " << winners.size() << " alternatives for " << kindStr << (kindStr != "" ? " " : "") << "expression\n"; |
---|
253 | untyped->print( stream ); |
---|
254 | stream << " Alternatives are:\n"; |
---|
255 | printAlts( winners, stream, 1 ); |
---|
256 | SemanticError( untyped->location, stream.str() ); |
---|
257 | } |
---|
258 | |
---|
259 | // single selected choice |
---|
260 | Alternative& choice = winners.front(); |
---|
261 | |
---|
262 | // fail on only expression deleted |
---|
263 | if ( ! seen_undeleted ) { |
---|
264 | SemanticError( untyped->location, choice.expr, "Unique best alternative includes deleted identifier in " ); |
---|
265 | } |
---|
266 | |
---|
267 | // xxx - check for ambiguous expressions |
---|
268 | |
---|
269 | // output selected choice |
---|
270 | alt = std::move( choice ); |
---|
271 | } |
---|
272 | |
---|
273 | /// resolve `untyped` to the expression whose alternative satisfies `pred` with the lowest cost; kindStr is used for providing better error messages |
---|
274 | void findKindExpression(Expression *& untyped, const SymTab::Indexer & indexer, const std::string & kindStr, std::function<bool(const Alternative &)> pred, ResolvMode mode = ResolvMode{}) { |
---|
275 | if ( ! untyped ) return; |
---|
276 | Alternative choice; |
---|
277 | findUnfinishedKindExpression( untyped, choice, indexer, kindStr, pred, mode ); |
---|
278 | finishExpr( choice.expr, choice.env, untyped->env ); |
---|
279 | delete untyped; |
---|
280 | untyped = choice.expr; |
---|
281 | choice.expr = nullptr; |
---|
282 | } |
---|
283 | |
---|
284 | bool standardAlternativeFilter( const Alternative & ) { |
---|
285 | // currently don't need to filter, under normal circumstances. |
---|
286 | // in the future, this may be useful for removing deleted expressions |
---|
287 | return true; |
---|
288 | } |
---|
289 | } // namespace |
---|
290 | |
---|
291 | // used in resolveTypeof |
---|
292 | Expression * resolveInVoidContext( Expression * expr, const SymTab::Indexer & indexer ) { |
---|
293 | TypeEnvironment env; |
---|
294 | return resolveInVoidContext( expr, indexer, env ); |
---|
295 | } |
---|
296 | |
---|
297 | Expression * resolveInVoidContext( Expression * expr, const SymTab::Indexer & indexer, TypeEnvironment & env ) { |
---|
298 | // it's a property of the language that a cast expression has either 1 or 0 interpretations; if it has 0 |
---|
299 | // interpretations, an exception has already been thrown. |
---|
300 | assertf( expr, "expected a non-null expression." ); |
---|
301 | |
---|
302 | CastExpr * untyped = new CastExpr( expr ); // cast to void |
---|
303 | untyped->location = expr->location; |
---|
304 | |
---|
305 | // set up and resolve expression cast to void |
---|
306 | Alternative choice; |
---|
307 | findUnfinishedKindExpression( untyped, choice, indexer, "", standardAlternativeFilter, ResolvMode::withAdjustment() ); |
---|
308 | CastExpr * castExpr = strict_dynamic_cast< CastExpr * >( choice.expr ); |
---|
309 | assert( castExpr ); |
---|
310 | env = std::move( choice.env ); |
---|
311 | |
---|
312 | // clean up resolved expression |
---|
313 | Expression * ret = castExpr->arg; |
---|
314 | castExpr->arg = nullptr; |
---|
315 | |
---|
316 | // unlink the arg so that it isn't deleted twice at the end of the program |
---|
317 | untyped->arg = nullptr; |
---|
318 | return ret; |
---|
319 | } |
---|
320 | |
---|
321 | void findVoidExpression( Expression *& untyped, const SymTab::Indexer & indexer ) { |
---|
322 | resetTyVarRenaming(); |
---|
323 | TypeEnvironment env; |
---|
324 | Expression * newExpr = resolveInVoidContext( untyped, indexer, env ); |
---|
325 | finishExpr( newExpr, env, untyped->env ); |
---|
326 | delete untyped; |
---|
327 | untyped = newExpr; |
---|
328 | } |
---|
329 | |
---|
330 | void findSingleExpression( Expression *& untyped, const SymTab::Indexer & indexer ) { |
---|
331 | findKindExpression( untyped, indexer, "", standardAlternativeFilter ); |
---|
332 | } |
---|
333 | |
---|
334 | void findSingleExpression( Expression *& untyped, Type * type, const SymTab::Indexer & indexer ) { |
---|
335 | assert( untyped && type ); |
---|
336 | // transfer location to generated cast for error purposes |
---|
337 | CodeLocation location = untyped->location; |
---|
338 | untyped = new CastExpr( untyped, type ); |
---|
339 | untyped->location = location; |
---|
340 | findSingleExpression( untyped, indexer ); |
---|
341 | removeExtraneousCast( untyped, indexer ); |
---|
342 | } |
---|
343 | |
---|
344 | namespace { |
---|
345 | bool isIntegralType( const Alternative & alt ) { |
---|
346 | Type * type = alt.expr->result; |
---|
347 | if ( dynamic_cast< EnumInstType * >( type ) ) { |
---|
348 | return true; |
---|
349 | } else if ( BasicType * bt = dynamic_cast< BasicType * >( type ) ) { |
---|
350 | return bt->isInteger(); |
---|
351 | } else if ( dynamic_cast< ZeroType* >( type ) != nullptr || dynamic_cast< OneType* >( type ) != nullptr ) { |
---|
352 | return true; |
---|
353 | } else { |
---|
354 | return false; |
---|
355 | } // if |
---|
356 | } |
---|
357 | |
---|
358 | void findIntegralExpression( Expression *& untyped, const SymTab::Indexer & indexer ) { |
---|
359 | findKindExpression( untyped, indexer, "condition", isIntegralType ); |
---|
360 | } |
---|
361 | } |
---|
362 | |
---|
363 | |
---|
364 | bool isStructOrUnion( const Alternative & alt ) { |
---|
365 | Type * t = alt.expr->result->stripReferences(); |
---|
366 | return dynamic_cast< StructInstType * >( t ) || dynamic_cast< UnionInstType * >( t ); |
---|
367 | } |
---|
368 | |
---|
369 | void resolveWithExprs( std::list< Declaration * > & translationUnit ) { |
---|
370 | PassVisitor<ResolveWithExprs> resolver; |
---|
371 | acceptAll( translationUnit, resolver ); |
---|
372 | } |
---|
373 | |
---|
374 | void ResolveWithExprs::resolveWithExprs( std::list< Expression * > & withExprs, std::list< Statement * > & newStmts ) { |
---|
375 | for ( Expression *& expr : withExprs ) { |
---|
376 | // only struct- and union-typed expressions are viable candidates |
---|
377 | findKindExpression( expr, indexer, "with statement", isStructOrUnion ); |
---|
378 | |
---|
379 | // if with expression might be impure, create a temporary so that it is evaluated once |
---|
380 | if ( Tuples::maybeImpure( expr ) ) { |
---|
381 | static UniqueName tmpNamer( "_with_tmp_" ); |
---|
382 | ObjectDecl * tmp = ObjectDecl::newObject( tmpNamer.newName(), expr->result->clone(), new SingleInit( expr ) ); |
---|
383 | expr = new VariableExpr( tmp ); |
---|
384 | newStmts.push_back( new DeclStmt( tmp ) ); |
---|
385 | if ( InitTweak::isConstructable( tmp->type ) ) { |
---|
386 | // generate ctor/dtor and resolve them |
---|
387 | tmp->init = InitTweak::genCtorInit( tmp ); |
---|
388 | tmp->accept( *visitor ); |
---|
389 | } |
---|
390 | } |
---|
391 | } |
---|
392 | } |
---|
393 | |
---|
394 | void ResolveWithExprs::previsit( WithStmt * withStmt ) { |
---|
395 | resolveWithExprs( withStmt->exprs, stmtsToAddBefore ); |
---|
396 | } |
---|
397 | |
---|
398 | void ResolveWithExprs::previsit( FunctionDecl * functionDecl ) { |
---|
399 | { |
---|
400 | // resolve with-exprs with parameters in scope and add any newly generated declarations to the |
---|
401 | // front of the function body. |
---|
402 | auto guard = makeFuncGuard( [this]() { indexer.enterScope(); }, [this](){ indexer.leaveScope(); } ); |
---|
403 | indexer.addFunctionType( functionDecl->type ); |
---|
404 | std::list< Statement * > newStmts; |
---|
405 | resolveWithExprs( functionDecl->withExprs, newStmts ); |
---|
406 | if ( functionDecl->statements ) { |
---|
407 | functionDecl->statements->kids.splice( functionDecl->statements->kids.begin(), newStmts ); |
---|
408 | } else { |
---|
409 | assertf( functionDecl->withExprs.empty() && newStmts.empty(), "Function %s without a body has with-clause and/or generated with declarations.", functionDecl->name.c_str() ); |
---|
410 | } |
---|
411 | } |
---|
412 | } |
---|
413 | |
---|
414 | void Resolver_old::previsit( ObjectDecl * objectDecl ) { |
---|
415 | // To handle initialization of routine pointers, e.g., int (*fp)(int) = foo(), means that |
---|
416 | // class-variable initContext is changed multiple time because the LHS is analysed twice. |
---|
417 | // The second analysis changes initContext because of a function type can contain object |
---|
418 | // declarations in the return and parameter types. So each value of initContext is |
---|
419 | // retained, so the type on the first analysis is preserved and used for selecting the RHS. |
---|
420 | GuardValue( currentObject ); |
---|
421 | currentObject = CurrentObject( objectDecl->get_type() ); |
---|
422 | if ( inEnumDecl && dynamic_cast< EnumInstType * >( objectDecl->get_type() ) ) { |
---|
423 | // enumerator initializers should not use the enum type to initialize, since |
---|
424 | // the enum type is still incomplete at this point. Use signed int instead. |
---|
425 | currentObject = CurrentObject( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) ); |
---|
426 | } |
---|
427 | } |
---|
428 | |
---|
429 | template< typename PtrType > |
---|
430 | void Resolver_old::handlePtrType( PtrType * type ) { |
---|
431 | if ( type->get_dimension() ) { |
---|
432 | findSingleExpression( type->dimension, Validate::SizeType->clone(), indexer ); |
---|
433 | } |
---|
434 | } |
---|
435 | |
---|
436 | void Resolver_old::previsit( ArrayType * at ) { |
---|
437 | handlePtrType( at ); |
---|
438 | } |
---|
439 | |
---|
440 | void Resolver_old::previsit( PointerType * pt ) { |
---|
441 | handlePtrType( pt ); |
---|
442 | } |
---|
443 | |
---|
444 | void Resolver_old::previsit( FunctionDecl * functionDecl ) { |
---|
445 | #if 0 |
---|
446 | std::cerr << "resolver visiting functiondecl "; |
---|
447 | functionDecl->print( std::cerr ); |
---|
448 | std::cerr << std::endl; |
---|
449 | #endif |
---|
450 | GuardValue( functionReturn ); |
---|
451 | functionReturn = ResolvExpr::extractResultType( functionDecl->type ); |
---|
452 | } |
---|
453 | |
---|
454 | void Resolver_old::postvisit( FunctionDecl * functionDecl ) { |
---|
455 | // default value expressions have an environment which shouldn't be there and trips up |
---|
456 | // later passes. |
---|
457 | // xxx - it might be necessary to somehow keep the information from this environment, but I |
---|
458 | // can't currently see how it's useful. |
---|
459 | for ( Declaration * d : functionDecl->type->parameters ) { |
---|
460 | if ( ObjectDecl * obj = dynamic_cast< ObjectDecl * >( d ) ) { |
---|
461 | if ( SingleInit * init = dynamic_cast< SingleInit * >( obj->init ) ) { |
---|
462 | delete init->value->env; |
---|
463 | init->value->env = nullptr; |
---|
464 | } |
---|
465 | } |
---|
466 | } |
---|
467 | } |
---|
468 | |
---|
469 | void Resolver_old::previsit( EnumDecl * ) { |
---|
470 | // in case we decide to allow nested enums |
---|
471 | GuardValue( inEnumDecl ); |
---|
472 | inEnumDecl = true; |
---|
473 | } |
---|
474 | |
---|
475 | void Resolver_old::previsit( StaticAssertDecl * assertDecl ) { |
---|
476 | findIntegralExpression( assertDecl->condition, indexer ); |
---|
477 | } |
---|
478 | |
---|
479 | void Resolver_old::previsit( ExprStmt * exprStmt ) { |
---|
480 | visit_children = false; |
---|
481 | assertf( exprStmt->expr, "ExprStmt has null Expression in resolver" ); |
---|
482 | findVoidExpression( exprStmt->expr, indexer ); |
---|
483 | } |
---|
484 | |
---|
485 | void Resolver_old::previsit( AsmExpr * asmExpr ) { |
---|
486 | visit_children = false; |
---|
487 | findVoidExpression( asmExpr->operand, indexer ); |
---|
488 | } |
---|
489 | |
---|
490 | void Resolver_old::previsit( AsmStmt * asmStmt ) { |
---|
491 | visit_children = false; |
---|
492 | acceptAll( asmStmt->get_input(), *visitor ); |
---|
493 | acceptAll( asmStmt->get_output(), *visitor ); |
---|
494 | } |
---|
495 | |
---|
496 | void Resolver_old::previsit( IfStmt * ifStmt ) { |
---|
497 | findIntegralExpression( ifStmt->condition, indexer ); |
---|
498 | } |
---|
499 | |
---|
500 | void Resolver_old::previsit( WhileStmt * whileStmt ) { |
---|
501 | findIntegralExpression( whileStmt->condition, indexer ); |
---|
502 | } |
---|
503 | |
---|
504 | void Resolver_old::previsit( ForStmt * forStmt ) { |
---|
505 | if ( forStmt->condition ) { |
---|
506 | findIntegralExpression( forStmt->condition, indexer ); |
---|
507 | } // if |
---|
508 | |
---|
509 | if ( forStmt->increment ) { |
---|
510 | findVoidExpression( forStmt->increment, indexer ); |
---|
511 | } // if |
---|
512 | } |
---|
513 | |
---|
514 | void Resolver_old::previsit( SwitchStmt * switchStmt ) { |
---|
515 | GuardValue( currentObject ); |
---|
516 | findIntegralExpression( switchStmt->condition, indexer ); |
---|
517 | |
---|
518 | currentObject = CurrentObject( switchStmt->condition->result ); |
---|
519 | } |
---|
520 | |
---|
521 | void Resolver_old::previsit( CaseStmt * caseStmt ) { |
---|
522 | if ( caseStmt->condition ) { |
---|
523 | std::list< InitAlternative > initAlts = currentObject.getOptions(); |
---|
524 | assertf( initAlts.size() == 1, "SwitchStmt did not correctly resolve an integral expression." ); |
---|
525 | // must remove cast from case statement because RangeExpr cannot be cast. |
---|
526 | Expression * newExpr = new CastExpr( caseStmt->condition, initAlts.front().type->clone() ); |
---|
527 | findSingleExpression( newExpr, indexer ); |
---|
528 | // case condition cannot have a cast in C, so it must be removed, regardless of whether it performs a conversion. |
---|
529 | // Ideally we would perform the conversion internally here. |
---|
530 | if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( newExpr ) ) { |
---|
531 | newExpr = castExpr->arg; |
---|
532 | castExpr->arg = nullptr; |
---|
533 | std::swap( newExpr->env, castExpr->env ); |
---|
534 | delete castExpr; |
---|
535 | } |
---|
536 | caseStmt->condition = newExpr; |
---|
537 | } |
---|
538 | } |
---|
539 | |
---|
540 | void Resolver_old::previsit( BranchStmt * branchStmt ) { |
---|
541 | visit_children = false; |
---|
542 | // must resolve the argument for a computed goto |
---|
543 | if ( branchStmt->get_type() == BranchStmt::Goto ) { // check for computed goto statement |
---|
544 | if ( branchStmt->computedTarget ) { |
---|
545 | // computed goto argument is void * |
---|
546 | findSingleExpression( branchStmt->computedTarget, new PointerType( Type::Qualifiers(), new VoidType( Type::Qualifiers() ) ), indexer ); |
---|
547 | } // if |
---|
548 | } // if |
---|
549 | } |
---|
550 | |
---|
551 | void Resolver_old::previsit( ReturnStmt * returnStmt ) { |
---|
552 | visit_children = false; |
---|
553 | if ( returnStmt->expr ) { |
---|
554 | findSingleExpression( returnStmt->expr, functionReturn->clone(), indexer ); |
---|
555 | } // if |
---|
556 | } |
---|
557 | |
---|
558 | void Resolver_old::previsit( ThrowStmt * throwStmt ) { |
---|
559 | visit_children = false; |
---|
560 | // TODO: Replace *exception type with &exception type. |
---|
561 | if ( throwStmt->get_expr() ) { |
---|
562 | const StructDecl * exception_decl = indexer.lookupStruct( "__cfaehm_base_exception_t" ); |
---|
563 | assert( exception_decl ); |
---|
564 | Type * exceptType = new PointerType( noQualifiers, new StructInstType( noQualifiers, const_cast<StructDecl *>(exception_decl) ) ); |
---|
565 | findSingleExpression( throwStmt->expr, exceptType, indexer ); |
---|
566 | } |
---|
567 | } |
---|
568 | |
---|
569 | void Resolver_old::previsit( CatchStmt * catchStmt ) { |
---|
570 | // Until we are very sure this invarent (ifs that move between passes have thenPart) |
---|
571 | // holds, check it. This allows a check for when to decode the mangling. |
---|
572 | if ( IfStmt * ifStmt = dynamic_cast<IfStmt *>( catchStmt->body ) ) { |
---|
573 | assert( ifStmt->thenPart ); |
---|
574 | } |
---|
575 | // Encode the catchStmt so the condition can see the declaration. |
---|
576 | if ( catchStmt->cond ) { |
---|
577 | IfStmt * ifStmt = new IfStmt( catchStmt->cond, nullptr, catchStmt->body ); |
---|
578 | catchStmt->cond = nullptr; |
---|
579 | catchStmt->body = ifStmt; |
---|
580 | } |
---|
581 | } |
---|
582 | |
---|
583 | void Resolver_old::postvisit( CatchStmt * catchStmt ) { |
---|
584 | // Decode the catchStmt so everything is stored properly. |
---|
585 | IfStmt * ifStmt = dynamic_cast<IfStmt *>( catchStmt->body ); |
---|
586 | if ( nullptr != ifStmt && nullptr == ifStmt->thenPart ) { |
---|
587 | assert( ifStmt->condition ); |
---|
588 | assert( ifStmt->elsePart ); |
---|
589 | catchStmt->cond = ifStmt->condition; |
---|
590 | catchStmt->body = ifStmt->elsePart; |
---|
591 | ifStmt->condition = nullptr; |
---|
592 | ifStmt->elsePart = nullptr; |
---|
593 | delete ifStmt; |
---|
594 | } |
---|
595 | } |
---|
596 | |
---|
597 | template< typename iterator_t > |
---|
598 | inline bool advance_to_mutex( iterator_t & it, const iterator_t & end ) { |
---|
599 | while( it != end && !(*it)->get_type()->get_mutex() ) { |
---|
600 | it++; |
---|
601 | } |
---|
602 | |
---|
603 | return it != end; |
---|
604 | } |
---|
605 | |
---|
606 | void Resolver_old::previsit( WaitForStmt * stmt ) { |
---|
607 | visit_children = false; |
---|
608 | |
---|
609 | // Resolve all clauses first |
---|
610 | for( auto& clause : stmt->clauses ) { |
---|
611 | |
---|
612 | TypeEnvironment env; |
---|
613 | AlternativeFinder funcFinder( indexer, env ); |
---|
614 | |
---|
615 | // Find all alternatives for a function in canonical form |
---|
616 | funcFinder.findWithAdjustment( clause.target.function ); |
---|
617 | |
---|
618 | if ( funcFinder.get_alternatives().empty() ) { |
---|
619 | stringstream ss; |
---|
620 | ss << "Use of undeclared indentifier '"; |
---|
621 | ss << strict_dynamic_cast<NameExpr*>( clause.target.function )->name; |
---|
622 | ss << "' in call to waitfor"; |
---|
623 | SemanticError( stmt->location, ss.str() ); |
---|
624 | } |
---|
625 | |
---|
626 | if(clause.target.arguments.empty()) { |
---|
627 | SemanticError( stmt->location, "Waitfor clause must have at least one mutex parameter"); |
---|
628 | } |
---|
629 | |
---|
630 | // Find all alternatives for all arguments in canonical form |
---|
631 | std::vector< AlternativeFinder > argAlternatives; |
---|
632 | funcFinder.findSubExprs( clause.target.arguments.begin(), clause.target.arguments.end(), back_inserter( argAlternatives ) ); |
---|
633 | |
---|
634 | // List all combinations of arguments |
---|
635 | std::vector< AltList > possibilities; |
---|
636 | combos( argAlternatives.begin(), argAlternatives.end(), back_inserter( possibilities ) ); |
---|
637 | |
---|
638 | AltList func_candidates; |
---|
639 | std::vector< AltList > args_candidates; |
---|
640 | |
---|
641 | // For every possible function : |
---|
642 | // try matching the arguments to the parameters |
---|
643 | // not the other way around because we have more arguments than parameters |
---|
644 | SemanticErrorException errors; |
---|
645 | for ( Alternative & func : funcFinder.get_alternatives() ) { |
---|
646 | try { |
---|
647 | PointerType * pointer = dynamic_cast< PointerType* >( func.expr->get_result()->stripReferences() ); |
---|
648 | if( !pointer ) { |
---|
649 | SemanticError( func.expr->get_result(), "candidate not viable: not a pointer type\n" ); |
---|
650 | } |
---|
651 | |
---|
652 | FunctionType * function = dynamic_cast< FunctionType* >( pointer->get_base() ); |
---|
653 | if( !function ) { |
---|
654 | SemanticError( pointer->get_base(), "candidate not viable: not a function type\n" ); |
---|
655 | } |
---|
656 | |
---|
657 | |
---|
658 | { |
---|
659 | auto param = function->parameters.begin(); |
---|
660 | auto param_end = function->parameters.end(); |
---|
661 | |
---|
662 | if( !advance_to_mutex( param, param_end ) ) { |
---|
663 | SemanticError(function, "candidate function not viable: no mutex parameters\n"); |
---|
664 | } |
---|
665 | } |
---|
666 | |
---|
667 | Alternative newFunc( func ); |
---|
668 | // Strip reference from function |
---|
669 | referenceToRvalueConversion( newFunc.expr, newFunc.cost ); |
---|
670 | |
---|
671 | // For all the set of arguments we have try to match it with the parameter of the current function alternative |
---|
672 | for ( auto & argsList : possibilities ) { |
---|
673 | |
---|
674 | try { |
---|
675 | // Declare data structures need for resolution |
---|
676 | OpenVarSet openVars; |
---|
677 | AssertionSet resultNeed, resultHave; |
---|
678 | TypeEnvironment resultEnv( func.env ); |
---|
679 | makeUnifiableVars( function, openVars, resultNeed ); |
---|
680 | // add all type variables as open variables now so that those not used in the parameter |
---|
681 | // list are still considered open. |
---|
682 | resultEnv.add( function->forall ); |
---|
683 | |
---|
684 | // Load type variables from arguemnts into one shared space |
---|
685 | simpleCombineEnvironments( argsList.begin(), argsList.end(), resultEnv ); |
---|
686 | |
---|
687 | // Make sure we don't widen any existing bindings |
---|
688 | resultEnv.forbidWidening(); |
---|
689 | |
---|
690 | // Find any unbound type variables |
---|
691 | resultEnv.extractOpenVars( openVars ); |
---|
692 | |
---|
693 | auto param = function->parameters.begin(); |
---|
694 | auto param_end = function->parameters.end(); |
---|
695 | |
---|
696 | int n_mutex_param = 0; |
---|
697 | |
---|
698 | // For every arguments of its set, check if it matches one of the parameter |
---|
699 | // The order is important |
---|
700 | for( auto & arg : argsList ) { |
---|
701 | |
---|
702 | // Ignore non-mutex arguments |
---|
703 | if( !advance_to_mutex( param, param_end ) ) { |
---|
704 | // We ran out of parameters but still have arguments |
---|
705 | // this function doesn't match |
---|
706 | SemanticError( function, toString("candidate function not viable: too many mutex arguments, expected ", n_mutex_param, "\n" )); |
---|
707 | } |
---|
708 | |
---|
709 | n_mutex_param++; |
---|
710 | |
---|
711 | // Check if the argument matches the parameter type in the current scope |
---|
712 | if( ! unify( arg.expr->get_result(), (*param)->get_type(), resultEnv, resultNeed, resultHave, openVars, this->indexer ) ) { |
---|
713 | // Type doesn't match |
---|
714 | stringstream ss; |
---|
715 | ss << "candidate function not viable: no known convertion from '"; |
---|
716 | (*param)->get_type()->print( ss ); |
---|
717 | ss << "' to '"; |
---|
718 | arg.expr->get_result()->print( ss ); |
---|
719 | ss << "' with env '"; |
---|
720 | resultEnv.print(ss); |
---|
721 | ss << "'\n"; |
---|
722 | SemanticError( function, ss.str() ); |
---|
723 | } |
---|
724 | |
---|
725 | param++; |
---|
726 | } |
---|
727 | |
---|
728 | // All arguments match ! |
---|
729 | |
---|
730 | // Check if parameters are missing |
---|
731 | if( advance_to_mutex( param, param_end ) ) { |
---|
732 | do { |
---|
733 | n_mutex_param++; |
---|
734 | param++; |
---|
735 | } while( advance_to_mutex( param, param_end ) ); |
---|
736 | |
---|
737 | // We ran out of arguments but still have parameters left |
---|
738 | // this function doesn't match |
---|
739 | SemanticError( function, toString("candidate function not viable: too few mutex arguments, expected ", n_mutex_param, "\n" )); |
---|
740 | } |
---|
741 | |
---|
742 | // All parameters match ! |
---|
743 | |
---|
744 | // Finish the expressions to tie in the proper environments |
---|
745 | finishExpr( newFunc.expr, resultEnv ); |
---|
746 | for( Alternative & alt : argsList ) { |
---|
747 | finishExpr( alt.expr, resultEnv ); |
---|
748 | } |
---|
749 | |
---|
750 | // This is a match store it and save it for later |
---|
751 | func_candidates.push_back( newFunc ); |
---|
752 | args_candidates.push_back( argsList ); |
---|
753 | |
---|
754 | } |
---|
755 | catch( SemanticErrorException & e ) { |
---|
756 | errors.append( e ); |
---|
757 | } |
---|
758 | } |
---|
759 | } |
---|
760 | catch( SemanticErrorException & e ) { |
---|
761 | errors.append( e ); |
---|
762 | } |
---|
763 | } |
---|
764 | |
---|
765 | // Make sure we got the right number of arguments |
---|
766 | if( func_candidates.empty() ) { SemanticErrorException top( stmt->location, "No alternatives for function in call to waitfor" ); top.append( errors ); throw top; } |
---|
767 | if( args_candidates.empty() ) { SemanticErrorException top( stmt->location, "No alternatives for arguments in call to waitfor" ); top.append( errors ); throw top; } |
---|
768 | if( func_candidates.size() > 1 ) { SemanticErrorException top( stmt->location, "Ambiguous function in call to waitfor" ); top.append( errors ); throw top; } |
---|
769 | if( args_candidates.size() > 1 ) { SemanticErrorException top( stmt->location, "Ambiguous arguments in call to waitfor" ); top.append( errors ); throw top; } |
---|
770 | // TODO: need to use findDeletedExpr to ensure no deleted identifiers are used. |
---|
771 | |
---|
772 | // Swap the results from the alternative with the unresolved values. |
---|
773 | // Alternatives will handle deletion on destruction |
---|
774 | std::swap( clause.target.function, func_candidates.front().expr ); |
---|
775 | for( auto arg_pair : group_iterate( clause.target.arguments, args_candidates.front() ) ) { |
---|
776 | std::swap ( std::get<0>( arg_pair), std::get<1>( arg_pair).expr ); |
---|
777 | } |
---|
778 | |
---|
779 | // Resolve the conditions as if it were an IfStmt |
---|
780 | // Resolve the statments normally |
---|
781 | findSingleExpression( clause.condition, this->indexer ); |
---|
782 | clause.statement->accept( *visitor ); |
---|
783 | } |
---|
784 | |
---|
785 | |
---|
786 | if( stmt->timeout.statement ) { |
---|
787 | // Resolve the timeout as an size_t for now |
---|
788 | // Resolve the conditions as if it were an IfStmt |
---|
789 | // Resolve the statments normally |
---|
790 | findSingleExpression( stmt->timeout.time, new BasicType( noQualifiers, BasicType::LongLongUnsignedInt ), this->indexer ); |
---|
791 | findSingleExpression( stmt->timeout.condition, this->indexer ); |
---|
792 | stmt->timeout.statement->accept( *visitor ); |
---|
793 | } |
---|
794 | |
---|
795 | if( stmt->orelse.statement ) { |
---|
796 | // Resolve the conditions as if it were an IfStmt |
---|
797 | // Resolve the statments normally |
---|
798 | findSingleExpression( stmt->orelse.condition, this->indexer ); |
---|
799 | stmt->orelse.statement->accept( *visitor ); |
---|
800 | } |
---|
801 | } |
---|
802 | |
---|
803 | bool isCharType( Type * t ) { |
---|
804 | if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) { |
---|
805 | return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar || |
---|
806 | bt->get_kind() == BasicType::UnsignedChar; |
---|
807 | } |
---|
808 | return false; |
---|
809 | } |
---|
810 | |
---|
811 | void Resolver_old::previsit( SingleInit * singleInit ) { |
---|
812 | visit_children = false; |
---|
813 | // resolve initialization using the possibilities as determined by the currentObject cursor |
---|
814 | Expression * newExpr = new UntypedInitExpr( singleInit->value, currentObject.getOptions() ); |
---|
815 | findSingleExpression( newExpr, indexer ); |
---|
816 | InitExpr * initExpr = strict_dynamic_cast< InitExpr * >( newExpr ); |
---|
817 | |
---|
818 | // move cursor to the object that is actually initialized |
---|
819 | currentObject.setNext( initExpr->get_designation() ); |
---|
820 | |
---|
821 | // discard InitExpr wrapper and retain relevant pieces |
---|
822 | newExpr = initExpr->expr; |
---|
823 | initExpr->expr = nullptr; |
---|
824 | std::swap( initExpr->env, newExpr->env ); |
---|
825 | // InitExpr may have inferParams in the case where the expression specializes a function |
---|
826 | // pointer, and newExpr may already have inferParams of its own, so a simple swap is not |
---|
827 | // sufficient. |
---|
828 | newExpr->spliceInferParams( initExpr ); |
---|
829 | delete initExpr; |
---|
830 | |
---|
831 | // get the actual object's type (may not exactly match what comes back from the resolver |
---|
832 | // due to conversions) |
---|
833 | Type * initContext = currentObject.getCurrentType(); |
---|
834 | |
---|
835 | removeExtraneousCast( newExpr, indexer ); |
---|
836 | |
---|
837 | // check if actual object's type is char[] |
---|
838 | if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) { |
---|
839 | if ( isCharType( at->get_base() ) ) { |
---|
840 | // check if the resolved type is char * |
---|
841 | if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_result() ) ) { |
---|
842 | if ( isCharType( pt->get_base() ) ) { |
---|
843 | if ( CastExpr * ce = dynamic_cast< CastExpr * >( newExpr ) ) { |
---|
844 | // strip cast if we're initializing a char[] with a char *, |
---|
845 | // e.g. char x[] = "hello"; |
---|
846 | newExpr = ce->get_arg(); |
---|
847 | ce->set_arg( nullptr ); |
---|
848 | std::swap( ce->env, newExpr->env ); |
---|
849 | delete ce; |
---|
850 | } |
---|
851 | } |
---|
852 | } |
---|
853 | } |
---|
854 | } |
---|
855 | |
---|
856 | // set initializer expr to resolved express |
---|
857 | singleInit->value = newExpr; |
---|
858 | |
---|
859 | // move cursor to next object in preparation for next initializer |
---|
860 | currentObject.increment(); |
---|
861 | } |
---|
862 | |
---|
863 | void Resolver_old::previsit( ListInit * listInit ) { |
---|
864 | visit_children = false; |
---|
865 | // move cursor into brace-enclosed initializer-list |
---|
866 | currentObject.enterListInit(); |
---|
867 | // xxx - fix this so that the list isn't copied, iterator should be used to change current |
---|
868 | // element |
---|
869 | std::list<Designation *> newDesignations; |
---|
870 | for ( auto p : group_iterate(listInit->get_designations(), listInit->get_initializers()) ) { |
---|
871 | // iterate designations and initializers in pairs, moving the cursor to the current |
---|
872 | // designated object and resolving the initializer against that object. |
---|
873 | Designation * des = std::get<0>(p); |
---|
874 | Initializer * init = std::get<1>(p); |
---|
875 | newDesignations.push_back( currentObject.findNext( des ) ); |
---|
876 | init->accept( *visitor ); |
---|
877 | } |
---|
878 | // set the set of 'resolved' designations and leave the brace-enclosed initializer-list |
---|
879 | listInit->get_designations() = newDesignations; // xxx - memory management |
---|
880 | currentObject.exitListInit(); |
---|
881 | |
---|
882 | // xxx - this part has not be folded into CurrentObject yet |
---|
883 | // } else if ( TypeInstType * tt = dynamic_cast< TypeInstType * >( initContext ) ) { |
---|
884 | // Type * base = tt->get_baseType()->get_base(); |
---|
885 | // if ( base ) { |
---|
886 | // // know the implementation type, so try using that as the initContext |
---|
887 | // ObjectDecl tmpObj( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, base->clone(), nullptr ); |
---|
888 | // currentObject = &tmpObj; |
---|
889 | // visit( listInit ); |
---|
890 | // } else { |
---|
891 | // // missing implementation type -- might be an unknown type variable, so try proceeding with the current init context |
---|
892 | // Parent::visit( listInit ); |
---|
893 | // } |
---|
894 | // } else { |
---|
895 | } |
---|
896 | |
---|
897 | // ConstructorInit - fall back on C-style initializer |
---|
898 | void Resolver_old::fallbackInit( ConstructorInit * ctorInit ) { |
---|
899 | // could not find valid constructor, or found an intrinsic constructor |
---|
900 | // fall back on C-style initializer |
---|
901 | delete ctorInit->get_ctor(); |
---|
902 | ctorInit->set_ctor( nullptr ); |
---|
903 | delete ctorInit->get_dtor(); |
---|
904 | ctorInit->set_dtor( nullptr ); |
---|
905 | maybeAccept( ctorInit->get_init(), *visitor ); |
---|
906 | } |
---|
907 | |
---|
908 | // needs to be callable from outside the resolver, so this is a standalone function |
---|
909 | void resolveCtorInit( ConstructorInit * ctorInit, const SymTab::Indexer & indexer ) { |
---|
910 | assert( ctorInit ); |
---|
911 | PassVisitor<Resolver_old> resolver( indexer ); |
---|
912 | ctorInit->accept( resolver ); |
---|
913 | } |
---|
914 | |
---|
915 | void resolveStmtExpr( StmtExpr * stmtExpr, const SymTab::Indexer & indexer ) { |
---|
916 | assert( stmtExpr ); |
---|
917 | PassVisitor<Resolver_old> resolver( indexer ); |
---|
918 | stmtExpr->accept( resolver ); |
---|
919 | stmtExpr->computeResult(); |
---|
920 | // xxx - aggregate the environments from all statements? Possibly in AlternativeFinder instead? |
---|
921 | } |
---|
922 | |
---|
923 | void Resolver_old::previsit( ConstructorInit * ctorInit ) { |
---|
924 | visit_children = false; |
---|
925 | // xxx - fallback init has been removed => remove fallbackInit function and remove complexity from FixInit and remove C-init from ConstructorInit |
---|
926 | maybeAccept( ctorInit->ctor, *visitor ); |
---|
927 | maybeAccept( ctorInit->dtor, *visitor ); |
---|
928 | |
---|
929 | // found a constructor - can get rid of C-style initializer |
---|
930 | delete ctorInit->init; |
---|
931 | ctorInit->init = nullptr; |
---|
932 | |
---|
933 | // intrinsic single parameter constructors and destructors do nothing. Since this was |
---|
934 | // implicitly generated, there's no way for it to have side effects, so get rid of it |
---|
935 | // to clean up generated code. |
---|
936 | if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->ctor ) ) { |
---|
937 | delete ctorInit->ctor; |
---|
938 | ctorInit->ctor = nullptr; |
---|
939 | } |
---|
940 | |
---|
941 | if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->dtor ) ) { |
---|
942 | delete ctorInit->dtor; |
---|
943 | ctorInit->dtor = nullptr; |
---|
944 | } |
---|
945 | |
---|
946 | // xxx - todo -- what about arrays? |
---|
947 | // if ( dtor == nullptr && InitTweak::isIntrinsicCallStmt( ctorInit->get_ctor() ) ) { |
---|
948 | // // can reduce the constructor down to a SingleInit using the |
---|
949 | // // second argument from the ctor call, since |
---|
950 | // delete ctorInit->get_ctor(); |
---|
951 | // ctorInit->set_ctor( nullptr ); |
---|
952 | |
---|
953 | // Expression * arg = |
---|
954 | // ctorInit->set_init( new SingleInit( arg ) ); |
---|
955 | // } |
---|
956 | } |
---|
957 | |
---|
958 | /////////////////////////////////////////////////////////////////////////// |
---|
959 | // |
---|
960 | // *** NEW RESOLVER *** |
---|
961 | // |
---|
962 | /////////////////////////////////////////////////////////////////////////// |
---|
963 | |
---|
964 | namespace { |
---|
965 | /// Finds deleted expressions in an expression tree |
---|
966 | struct DeleteFinder_new final : public ast::WithShortCircuiting { |
---|
967 | const ast::DeletedExpr * delExpr = nullptr; |
---|
968 | |
---|
969 | void previsit( const ast::DeletedExpr * expr ) { |
---|
970 | if ( delExpr ) { visit_children = false; } |
---|
971 | else { delExpr = expr; } |
---|
972 | } |
---|
973 | |
---|
974 | void previsit( const ast::Expr * ) { |
---|
975 | if ( delExpr ) { visit_children = false; } |
---|
976 | } |
---|
977 | }; |
---|
978 | } // anonymous namespace |
---|
979 | |
---|
980 | /// Check if this expression is or includes a deleted expression |
---|
981 | const ast::DeletedExpr * findDeletedExpr( const ast::Expr * expr ) { |
---|
982 | ast::Pass<DeleteFinder_new> finder; |
---|
983 | expr->accept( finder ); |
---|
984 | return finder.pass.delExpr; |
---|
985 | } |
---|
986 | |
---|
987 | namespace { |
---|
988 | /// always-accept candidate filter |
---|
989 | bool anyCandidate( const Candidate & ) { return true; } |
---|
990 | |
---|
991 | /// Calls the CandidateFinder and finds the single best candidate |
---|
992 | CandidateRef findUnfinishedKindExpression( |
---|
993 | const ast::Expr * untyped, const ast::SymbolTable & symtab, const std::string & kind, |
---|
994 | std::function<bool(const Candidate &)> pred = anyCandidate, ResolvMode mode = {} |
---|
995 | ) { |
---|
996 | if ( ! untyped ) return nullptr; |
---|
997 | |
---|
998 | // xxx - this isn't thread-safe, but should work until we parallelize the resolver |
---|
999 | static unsigned recursion_level = 0; |
---|
1000 | |
---|
1001 | ++recursion_level; |
---|
1002 | ast::TypeEnvironment env; |
---|
1003 | CandidateFinder finder{ symtab, env }; |
---|
1004 | finder.find( untyped, recursion_level == 1 ? mode.atTopLevel() : mode ); |
---|
1005 | --recursion_level; |
---|
1006 | |
---|
1007 | // produce a filtered list of candidates |
---|
1008 | CandidateList candidates; |
---|
1009 | for ( auto & cand : finder.candidates ) { |
---|
1010 | if ( pred( *cand ) ) { candidates.emplace_back( cand ); } |
---|
1011 | } |
---|
1012 | |
---|
1013 | // produce invalid error if no candidates |
---|
1014 | if ( candidates.empty() ) { |
---|
1015 | SemanticError( untyped, |
---|
1016 | toString( "No reasonable alternatives for ", kind, (kind != "" ? " " : ""), |
---|
1017 | "expression: ") ); |
---|
1018 | } |
---|
1019 | |
---|
1020 | // search for cheapest candidate |
---|
1021 | CandidateList winners; |
---|
1022 | bool seen_undeleted = false; |
---|
1023 | for ( CandidateRef & cand : candidates ) { |
---|
1024 | int c = winners.empty() ? -1 : cand->cost.compare( winners.front()->cost ); |
---|
1025 | |
---|
1026 | if ( c > 0 ) continue; // skip more expensive than winner |
---|
1027 | |
---|
1028 | if ( c < 0 ) { |
---|
1029 | // reset on new cheapest |
---|
1030 | seen_undeleted = ! findDeletedExpr( cand->expr ); |
---|
1031 | winners.clear(); |
---|
1032 | } else /* if ( c == 0 ) */ { |
---|
1033 | if ( findDeletedExpr( cand->expr ) ) { |
---|
1034 | // skip deleted expression if already seen one equivalent-cost not |
---|
1035 | if ( seen_undeleted ) continue; |
---|
1036 | } else if ( ! seen_undeleted ) { |
---|
1037 | // replace list of equivalent-cost deleted expressions with one non-deleted |
---|
1038 | winners.clear(); |
---|
1039 | seen_undeleted = true; |
---|
1040 | } |
---|
1041 | } |
---|
1042 | |
---|
1043 | winners.emplace_back( std::move( cand ) ); |
---|
1044 | } |
---|
1045 | |
---|
1046 | // promote candidate.cvtCost to .cost |
---|
1047 | promoteCvtCost( winners ); |
---|
1048 | |
---|
1049 | // produce ambiguous errors, if applicable |
---|
1050 | if ( winners.size() != 1 ) { |
---|
1051 | std::ostringstream stream; |
---|
1052 | stream << "Cannot choose between " << winners.size() << " alternatives for " |
---|
1053 | << kind << (kind != "" ? " " : "") << "expression\n"; |
---|
1054 | ast::print( stream, untyped ); |
---|
1055 | stream << " Alternatives are:\n"; |
---|
1056 | print( stream, winners, 1 ); |
---|
1057 | SemanticError( untyped->location, stream.str() ); |
---|
1058 | } |
---|
1059 | |
---|
1060 | // single selected choice |
---|
1061 | CandidateRef & choice = winners.front(); |
---|
1062 | |
---|
1063 | // fail on only expression deleted |
---|
1064 | if ( ! seen_undeleted ) { |
---|
1065 | SemanticError( untyped->location, choice->expr.get(), "Unique best alternative " |
---|
1066 | "includes deleted identifier in " ); |
---|
1067 | } |
---|
1068 | |
---|
1069 | return std::move( choice ); |
---|
1070 | } |
---|
1071 | |
---|
1072 | /// Strips extraneous casts out of an expression |
---|
1073 | struct StripCasts_new final { |
---|
1074 | const ast::Expr * postvisit( const ast::CastExpr * castExpr ) { |
---|
1075 | if ( |
---|
1076 | castExpr->isGenerated == ast::GeneratedCast |
---|
1077 | && typesCompatible( castExpr->arg->result, castExpr->result ) |
---|
1078 | ) { |
---|
1079 | // generated cast is the same type as its argument, remove it after keeping env |
---|
1080 | return ast::mutate_field( |
---|
1081 | castExpr->arg.get(), &ast::Expr::env, castExpr->env ); |
---|
1082 | } |
---|
1083 | return castExpr; |
---|
1084 | } |
---|
1085 | |
---|
1086 | static void strip( ast::ptr< ast::Expr > & expr ) { |
---|
1087 | ast::Pass< StripCasts_new > stripper; |
---|
1088 | expr = expr->accept( stripper ); |
---|
1089 | } |
---|
1090 | }; |
---|
1091 | |
---|
1092 | /// Swaps argument into expression pointer, saving original environment |
---|
1093 | void swap_and_save_env( ast::ptr< ast::Expr > & expr, const ast::Expr * newExpr ) { |
---|
1094 | ast::ptr< ast::TypeSubstitution > env = expr->env; |
---|
1095 | expr.set_and_mutate( newExpr )->env = env; |
---|
1096 | } |
---|
1097 | |
---|
1098 | /// Removes cast to type of argument (unlike StripCasts, also handles non-generated casts) |
---|
1099 | void removeExtraneousCast( ast::ptr<ast::Expr> & expr, const ast::SymbolTable & symtab ) { |
---|
1100 | if ( const ast::CastExpr * castExpr = expr.as< ast::CastExpr >() ) { |
---|
1101 | if ( typesCompatible( castExpr->arg->result, castExpr->result, symtab ) ) { |
---|
1102 | // cast is to the same type as its argument, remove it |
---|
1103 | swap_and_save_env( expr, castExpr->arg ); |
---|
1104 | } |
---|
1105 | } |
---|
1106 | } |
---|
1107 | |
---|
1108 | /// Establish post-resolver invariants for expressions |
---|
1109 | void finishExpr( |
---|
1110 | ast::ptr< ast::Expr > & expr, const ast::TypeEnvironment & env, |
---|
1111 | const ast::TypeSubstitution * oldenv = nullptr |
---|
1112 | ) { |
---|
1113 | // set up new type substitution for expression |
---|
1114 | ast::ptr< ast::TypeSubstitution > newenv = |
---|
1115 | oldenv ? oldenv : new ast::TypeSubstitution{}; |
---|
1116 | env.writeToSubstitution( *newenv.get_and_mutate() ); |
---|
1117 | expr.get_and_mutate()->env = std::move( newenv ); |
---|
1118 | // remove unncecessary casts |
---|
1119 | StripCasts_new::strip( expr ); |
---|
1120 | } |
---|
1121 | } // anonymous namespace |
---|
1122 | |
---|
1123 | |
---|
1124 | ast::ptr< ast::Expr > resolveInVoidContext( |
---|
1125 | const ast::Expr * expr, const ast::SymbolTable & symtab, ast::TypeEnvironment & env |
---|
1126 | ) { |
---|
1127 | assertf( expr, "expected a non-null expression" ); |
---|
1128 | |
---|
1129 | // set up and resolve expression cast to void |
---|
1130 | ast::ptr< ast::CastExpr > untyped = new ast::CastExpr{ expr }; |
---|
1131 | CandidateRef choice = findUnfinishedKindExpression( |
---|
1132 | untyped, symtab, "", anyCandidate, ResolvMode::withAdjustment() ); |
---|
1133 | |
---|
1134 | // a cast expression has either 0 or 1 interpretations (by language rules); |
---|
1135 | // if 0, an exception has already been thrown, and this code will not run |
---|
1136 | const ast::CastExpr * castExpr = choice->expr.strict_as< ast::CastExpr >(); |
---|
1137 | env = std::move( choice->env ); |
---|
1138 | |
---|
1139 | return castExpr->arg; |
---|
1140 | } |
---|
1141 | |
---|
1142 | namespace { |
---|
1143 | /// Resolve `untyped` to the expression whose candidate is the best match for a `void` |
---|
1144 | /// context. |
---|
1145 | ast::ptr< ast::Expr > findVoidExpression( |
---|
1146 | const ast::Expr * untyped, const ast::SymbolTable & symtab |
---|
1147 | ) { |
---|
1148 | resetTyVarRenaming(); |
---|
1149 | ast::TypeEnvironment env; |
---|
1150 | ast::ptr< ast::Expr > newExpr = resolveInVoidContext( untyped, symtab, env ); |
---|
1151 | finishExpr( newExpr, env, untyped->env ); |
---|
1152 | return newExpr; |
---|
1153 | } |
---|
1154 | |
---|
1155 | /// resolve `untyped` to the expression whose candidate satisfies `pred` with the |
---|
1156 | /// lowest cost, returning the resolved version |
---|
1157 | ast::ptr< ast::Expr > findKindExpression( |
---|
1158 | const ast::Expr * untyped, const ast::SymbolTable & symtab, |
---|
1159 | std::function<bool(const Candidate &)> pred = anyCandidate, |
---|
1160 | const std::string & kind = "", ResolvMode mode = {} |
---|
1161 | ) { |
---|
1162 | if ( ! untyped ) return {}; |
---|
1163 | CandidateRef choice = |
---|
1164 | findUnfinishedKindExpression( untyped, symtab, kind, pred, mode ); |
---|
1165 | finishExpr( choice->expr, choice->env, untyped->env ); |
---|
1166 | return std::move( choice->expr ); |
---|
1167 | } |
---|
1168 | |
---|
1169 | /// Resolve `untyped` to the single expression whose candidate is the best match |
---|
1170 | ast::ptr< ast::Expr > findSingleExpression( |
---|
1171 | const ast::Expr * untyped, const ast::SymbolTable & symtab |
---|
1172 | ) { |
---|
1173 | return findKindExpression( untyped, symtab ); |
---|
1174 | } |
---|
1175 | } // anonymous namespace |
---|
1176 | |
---|
1177 | ast::ptr< ast::Expr > findSingleExpression( |
---|
1178 | const ast::Expr * untyped, const ast::Type * type, const ast::SymbolTable & symtab |
---|
1179 | ) { |
---|
1180 | assert( untyped && type ); |
---|
1181 | ast::ptr< ast::Expr > castExpr = new ast::CastExpr{ untyped, type }; |
---|
1182 | ast::ptr< ast::Expr > newExpr = findSingleExpression( castExpr, symtab ); |
---|
1183 | removeExtraneousCast( newExpr, symtab ); |
---|
1184 | return newExpr; |
---|
1185 | } |
---|
1186 | |
---|
1187 | namespace { |
---|
1188 | /// Predicate for "Candidate has integral type" |
---|
1189 | bool hasIntegralType( const Candidate & i ) { |
---|
1190 | const ast::Type * type = i.expr->result; |
---|
1191 | |
---|
1192 | if ( auto bt = dynamic_cast< const ast::BasicType * >( type ) ) { |
---|
1193 | return bt->isInteger(); |
---|
1194 | } else if ( |
---|
1195 | dynamic_cast< const ast::EnumInstType * >( type ) |
---|
1196 | || dynamic_cast< const ast::ZeroType * >( type ) |
---|
1197 | || dynamic_cast< const ast::OneType * >( type ) |
---|
1198 | ) { |
---|
1199 | return true; |
---|
1200 | } else return false; |
---|
1201 | } |
---|
1202 | |
---|
1203 | /// Resolve `untyped` as an integral expression, returning the resolved version |
---|
1204 | ast::ptr< ast::Expr > findIntegralExpression( |
---|
1205 | const ast::Expr * untyped, const ast::SymbolTable & symtab |
---|
1206 | ) { |
---|
1207 | return findKindExpression( untyped, symtab, hasIntegralType, "condition" ); |
---|
1208 | } |
---|
1209 | |
---|
1210 | /// check if a type is a character type |
---|
1211 | bool isCharType( const ast::Type * t ) { |
---|
1212 | if ( auto bt = dynamic_cast< const ast::BasicType * >( t ) ) { |
---|
1213 | return bt->kind == ast::BasicType::Char |
---|
1214 | || bt->kind == ast::BasicType::SignedChar |
---|
1215 | || bt->kind == ast::BasicType::UnsignedChar; |
---|
1216 | } |
---|
1217 | return false; |
---|
1218 | } |
---|
1219 | |
---|
1220 | /// Advance a type itertor to the next mutex parameter |
---|
1221 | template<typename Iter> |
---|
1222 | inline bool nextMutex( Iter & it, const Iter & end ) { |
---|
1223 | while ( it != end && ! (*it)->get_type()->is_mutex() ) { ++it; } |
---|
1224 | return it != end; |
---|
1225 | } |
---|
1226 | } |
---|
1227 | |
---|
1228 | class Resolver_new final |
---|
1229 | : public ast::WithSymbolTable, public ast::WithGuards, |
---|
1230 | public ast::WithVisitorRef<Resolver_new>, public ast::WithShortCircuiting, |
---|
1231 | public ast::WithStmtsToAdd<> { |
---|
1232 | |
---|
1233 | ast::ptr< ast::Type > functionReturn = nullptr; |
---|
1234 | ast::CurrentObject currentObject; |
---|
1235 | bool inEnumDecl = false; |
---|
1236 | |
---|
1237 | public: |
---|
1238 | static size_t traceId; |
---|
1239 | Resolver_new() = default; |
---|
1240 | Resolver_new( const ast::SymbolTable & syms ) { symtab = syms; } |
---|
1241 | |
---|
1242 | void previsit( const ast::FunctionDecl * ); |
---|
1243 | const ast::FunctionDecl * postvisit( const ast::FunctionDecl * ); |
---|
1244 | void previsit( const ast::ObjectDecl * ); |
---|
1245 | void previsit( const ast::EnumDecl * ); |
---|
1246 | const ast::StaticAssertDecl * previsit( const ast::StaticAssertDecl * ); |
---|
1247 | |
---|
1248 | const ast::ArrayType * previsit( const ast::ArrayType * ); |
---|
1249 | const ast::PointerType * previsit( const ast::PointerType * ); |
---|
1250 | |
---|
1251 | const ast::ExprStmt * previsit( const ast::ExprStmt * ); |
---|
1252 | const ast::AsmExpr * previsit( const ast::AsmExpr * ); |
---|
1253 | const ast::AsmStmt * previsit( const ast::AsmStmt * ); |
---|
1254 | const ast::IfStmt * previsit( const ast::IfStmt * ); |
---|
1255 | const ast::WhileStmt * previsit( const ast::WhileStmt * ); |
---|
1256 | const ast::ForStmt * previsit( const ast::ForStmt * ); |
---|
1257 | const ast::SwitchStmt * previsit( const ast::SwitchStmt * ); |
---|
1258 | const ast::CaseStmt * previsit( const ast::CaseStmt * ); |
---|
1259 | const ast::BranchStmt * previsit( const ast::BranchStmt * ); |
---|
1260 | const ast::ReturnStmt * previsit( const ast::ReturnStmt * ); |
---|
1261 | const ast::ThrowStmt * previsit( const ast::ThrowStmt * ); |
---|
1262 | const ast::CatchStmt * previsit( const ast::CatchStmt * ); |
---|
1263 | const ast::WaitForStmt * previsit( const ast::WaitForStmt * ); |
---|
1264 | |
---|
1265 | const ast::SingleInit * previsit( const ast::SingleInit * ); |
---|
1266 | const ast::ListInit * previsit( const ast::ListInit * ); |
---|
1267 | const ast::ConstructorInit * previsit( const ast::ConstructorInit * ); |
---|
1268 | }; |
---|
1269 | size_t Resolver_new::traceId = Stats::Heap::new_stacktrace_id("Resolver"); |
---|
1270 | |
---|
1271 | void resolve( std::list< ast::ptr< ast::Decl > >& translationUnit ) { |
---|
1272 | ast::Pass< Resolver_new >::run( translationUnit ); |
---|
1273 | } |
---|
1274 | |
---|
1275 | ast::ptr< ast::Init > resolveCtorInit( |
---|
1276 | const ast::ConstructorInit * ctorInit, const ast::SymbolTable & symtab |
---|
1277 | ) { |
---|
1278 | assert( ctorInit ); |
---|
1279 | ast::Pass< Resolver_new > resolver{ symtab }; |
---|
1280 | return ctorInit->accept( resolver ); |
---|
1281 | } |
---|
1282 | |
---|
1283 | ast::ptr< ast::Expr > resolveStmtExpr( |
---|
1284 | const ast::StmtExpr * stmtExpr, const ast::SymbolTable & symtab |
---|
1285 | ) { |
---|
1286 | assert( stmtExpr ); |
---|
1287 | ast::Pass< Resolver_new > resolver{ symtab }; |
---|
1288 | ast::ptr< ast::Expr > ret = stmtExpr; |
---|
1289 | ret = ret->accept( resolver ); |
---|
1290 | strict_dynamic_cast< ast::StmtExpr * >( ret.get_and_mutate() )->computeResult(); |
---|
1291 | return ret; |
---|
1292 | } |
---|
1293 | |
---|
1294 | void Resolver_new::previsit( const ast::FunctionDecl * functionDecl ) { |
---|
1295 | GuardValue( functionReturn ); |
---|
1296 | functionReturn = extractResultType( functionDecl->type ); |
---|
1297 | } |
---|
1298 | |
---|
1299 | const ast::FunctionDecl * Resolver_new::postvisit( const ast::FunctionDecl * functionDecl ) { |
---|
1300 | // default value expressions have an environment which shouldn't be there and trips up |
---|
1301 | // later passes. |
---|
1302 | assert( functionDecl->unique() ); |
---|
1303 | ast::FunctionType * mutType = mutate( functionDecl->type.get() ); |
---|
1304 | |
---|
1305 | for ( unsigned i = 0 ; i < mutType->params.size() ; ++i ) { |
---|
1306 | if ( const ast::ObjectDecl * obj = mutType->params[i].as< ast::ObjectDecl >() ) { |
---|
1307 | if ( const ast::SingleInit * init = obj->init.as< ast::SingleInit >() ) { |
---|
1308 | if ( init->value->env == nullptr ) continue; |
---|
1309 | // clone initializer minus the initializer environment |
---|
1310 | auto mutParam = mutate( mutType->params[i].strict_as< ast::ObjectDecl >() ); |
---|
1311 | auto mutInit = mutate( mutParam->init.strict_as< ast::SingleInit >() ); |
---|
1312 | auto mutValue = mutate( mutInit->value.get() ); |
---|
1313 | |
---|
1314 | mutValue->env = nullptr; |
---|
1315 | mutInit->value = mutValue; |
---|
1316 | mutParam->init = mutInit; |
---|
1317 | mutType->params[i] = mutParam; |
---|
1318 | |
---|
1319 | assert( ! mutType->params[i].strict_as< ast::ObjectDecl >()->init.strict_as< ast::SingleInit >()->value->env); |
---|
1320 | } |
---|
1321 | } |
---|
1322 | } |
---|
1323 | mutate_field(functionDecl, &ast::FunctionDecl::type, mutType); |
---|
1324 | return functionDecl; |
---|
1325 | } |
---|
1326 | |
---|
1327 | void Resolver_new::previsit( const ast::ObjectDecl * objectDecl ) { |
---|
1328 | // To handle initialization of routine pointers [e.g. int (*fp)(int) = foo()], |
---|
1329 | // class-variable `initContext` is changed multiple times because the LHS is analyzed |
---|
1330 | // twice. The second analysis changes `initContext` because a function type can contain |
---|
1331 | // object declarations in the return and parameter types. Therefore each value of |
---|
1332 | // `initContext` is retained so the type on the first analysis is preserved and used for |
---|
1333 | // selecting the RHS. |
---|
1334 | GuardValue( currentObject ); |
---|
1335 | currentObject = ast::CurrentObject{ objectDecl->location, objectDecl->get_type() }; |
---|
1336 | if ( inEnumDecl && dynamic_cast< const ast::EnumInstType * >( objectDecl->get_type() ) ) { |
---|
1337 | // enumerator initializers should not use the enum type to initialize, since the |
---|
1338 | // enum type is still incomplete at this point. Use `int` instead. |
---|
1339 | currentObject = ast::CurrentObject{ |
---|
1340 | objectDecl->location, new ast::BasicType{ ast::BasicType::SignedInt } }; |
---|
1341 | } |
---|
1342 | } |
---|
1343 | |
---|
1344 | void Resolver_new::previsit( const ast::EnumDecl * ) { |
---|
1345 | // in case we decide to allow nested enums |
---|
1346 | GuardValue( inEnumDecl ); |
---|
1347 | inEnumDecl = true; |
---|
1348 | } |
---|
1349 | |
---|
1350 | const ast::StaticAssertDecl * Resolver_new::previsit( |
---|
1351 | const ast::StaticAssertDecl * assertDecl |
---|
1352 | ) { |
---|
1353 | return ast::mutate_field( |
---|
1354 | assertDecl, &ast::StaticAssertDecl::cond, |
---|
1355 | findIntegralExpression( assertDecl->cond, symtab ) ); |
---|
1356 | } |
---|
1357 | |
---|
1358 | template< typename PtrType > |
---|
1359 | const PtrType * handlePtrType( const PtrType * type, const ast::SymbolTable & symtab ) { |
---|
1360 | if ( type->dimension ) { |
---|
1361 | #warning should use new equivalent to Validate::SizeType rather than sizeType here |
---|
1362 | ast::ptr< ast::Type > sizeType = new ast::BasicType{ ast::BasicType::LongUnsignedInt }; |
---|
1363 | ast::mutate_field( |
---|
1364 | type, &PtrType::dimension, |
---|
1365 | findSingleExpression( type->dimension, sizeType, symtab ) ); |
---|
1366 | } |
---|
1367 | return type; |
---|
1368 | } |
---|
1369 | |
---|
1370 | const ast::ArrayType * Resolver_new::previsit( const ast::ArrayType * at ) { |
---|
1371 | return handlePtrType( at, symtab ); |
---|
1372 | } |
---|
1373 | |
---|
1374 | const ast::PointerType * Resolver_new::previsit( const ast::PointerType * pt ) { |
---|
1375 | return handlePtrType( pt, symtab ); |
---|
1376 | } |
---|
1377 | |
---|
1378 | const ast::ExprStmt * Resolver_new::previsit( const ast::ExprStmt * exprStmt ) { |
---|
1379 | visit_children = false; |
---|
1380 | assertf( exprStmt->expr, "ExprStmt has null expression in resolver" ); |
---|
1381 | |
---|
1382 | return ast::mutate_field( |
---|
1383 | exprStmt, &ast::ExprStmt::expr, findVoidExpression( exprStmt->expr, symtab ) ); |
---|
1384 | } |
---|
1385 | |
---|
1386 | const ast::AsmExpr * Resolver_new::previsit( const ast::AsmExpr * asmExpr ) { |
---|
1387 | visit_children = false; |
---|
1388 | |
---|
1389 | asmExpr = ast::mutate_field( |
---|
1390 | asmExpr, &ast::AsmExpr::operand, findVoidExpression( asmExpr->operand, symtab ) ); |
---|
1391 | |
---|
1392 | return asmExpr; |
---|
1393 | } |
---|
1394 | |
---|
1395 | const ast::AsmStmt * Resolver_new::previsit( const ast::AsmStmt * asmStmt ) { |
---|
1396 | visitor->maybe_accept( asmStmt, &ast::AsmStmt::input ); |
---|
1397 | visitor->maybe_accept( asmStmt, &ast::AsmStmt::output ); |
---|
1398 | visit_children = false; |
---|
1399 | return asmStmt; |
---|
1400 | } |
---|
1401 | |
---|
1402 | const ast::IfStmt * Resolver_new::previsit( const ast::IfStmt * ifStmt ) { |
---|
1403 | return ast::mutate_field( |
---|
1404 | ifStmt, &ast::IfStmt::cond, findIntegralExpression( ifStmt->cond, symtab ) ); |
---|
1405 | } |
---|
1406 | |
---|
1407 | const ast::WhileStmt * Resolver_new::previsit( const ast::WhileStmt * whileStmt ) { |
---|
1408 | return ast::mutate_field( |
---|
1409 | whileStmt, &ast::WhileStmt::cond, findIntegralExpression( whileStmt->cond, symtab ) ); |
---|
1410 | } |
---|
1411 | |
---|
1412 | const ast::ForStmt * Resolver_new::previsit( const ast::ForStmt * forStmt ) { |
---|
1413 | if ( forStmt->cond ) { |
---|
1414 | forStmt = ast::mutate_field( |
---|
1415 | forStmt, &ast::ForStmt::cond, findIntegralExpression( forStmt->cond, symtab ) ); |
---|
1416 | } |
---|
1417 | |
---|
1418 | if ( forStmt->inc ) { |
---|
1419 | forStmt = ast::mutate_field( |
---|
1420 | forStmt, &ast::ForStmt::inc, findVoidExpression( forStmt->inc, symtab ) ); |
---|
1421 | } |
---|
1422 | |
---|
1423 | return forStmt; |
---|
1424 | } |
---|
1425 | |
---|
1426 | const ast::SwitchStmt * Resolver_new::previsit( const ast::SwitchStmt * switchStmt ) { |
---|
1427 | GuardValue( currentObject ); |
---|
1428 | switchStmt = ast::mutate_field( |
---|
1429 | switchStmt, &ast::SwitchStmt::cond, |
---|
1430 | findIntegralExpression( switchStmt->cond, symtab ) ); |
---|
1431 | currentObject = ast::CurrentObject{ switchStmt->location, switchStmt->cond->result }; |
---|
1432 | return switchStmt; |
---|
1433 | } |
---|
1434 | |
---|
1435 | const ast::CaseStmt * Resolver_new::previsit( const ast::CaseStmt * caseStmt ) { |
---|
1436 | if ( caseStmt->cond ) { |
---|
1437 | std::deque< ast::InitAlternative > initAlts = currentObject.getOptions(); |
---|
1438 | assertf( initAlts.size() == 1, "SwitchStmt did not correctly resolve an integral " |
---|
1439 | "expression." ); |
---|
1440 | |
---|
1441 | ast::ptr< ast::Expr > untyped = |
---|
1442 | new ast::CastExpr{ caseStmt->location, caseStmt->cond, initAlts.front().type }; |
---|
1443 | ast::ptr< ast::Expr > newExpr = findSingleExpression( untyped, symtab ); |
---|
1444 | |
---|
1445 | // case condition cannot have a cast in C, so it must be removed here, regardless of |
---|
1446 | // whether it would perform a conversion. |
---|
1447 | if ( const ast::CastExpr * castExpr = newExpr.as< ast::CastExpr >() ) { |
---|
1448 | swap_and_save_env( newExpr, castExpr->arg ); |
---|
1449 | } |
---|
1450 | |
---|
1451 | caseStmt = ast::mutate_field( caseStmt, &ast::CaseStmt::cond, newExpr ); |
---|
1452 | } |
---|
1453 | return caseStmt; |
---|
1454 | } |
---|
1455 | |
---|
1456 | const ast::BranchStmt * Resolver_new::previsit( const ast::BranchStmt * branchStmt ) { |
---|
1457 | visit_children = false; |
---|
1458 | // must resolve the argument of a computed goto |
---|
1459 | if ( branchStmt->kind == ast::BranchStmt::Goto && branchStmt->computedTarget ) { |
---|
1460 | // computed goto argument is void* |
---|
1461 | ast::ptr< ast::Type > target = new ast::PointerType{ new ast::VoidType{} }; |
---|
1462 | branchStmt = ast::mutate_field( |
---|
1463 | branchStmt, &ast::BranchStmt::computedTarget, |
---|
1464 | findSingleExpression( branchStmt->computedTarget, target, symtab ) ); |
---|
1465 | } |
---|
1466 | return branchStmt; |
---|
1467 | } |
---|
1468 | |
---|
1469 | const ast::ReturnStmt * Resolver_new::previsit( const ast::ReturnStmt * returnStmt ) { |
---|
1470 | visit_children = false; |
---|
1471 | if ( returnStmt->expr ) { |
---|
1472 | returnStmt = ast::mutate_field( |
---|
1473 | returnStmt, &ast::ReturnStmt::expr, |
---|
1474 | findSingleExpression( returnStmt->expr, functionReturn, symtab ) ); |
---|
1475 | } |
---|
1476 | return returnStmt; |
---|
1477 | } |
---|
1478 | |
---|
1479 | const ast::ThrowStmt * Resolver_new::previsit( const ast::ThrowStmt * throwStmt ) { |
---|
1480 | visit_children = false; |
---|
1481 | if ( throwStmt->expr ) { |
---|
1482 | const ast::StructDecl * exceptionDecl = |
---|
1483 | symtab.lookupStruct( "__cfaehm_base_exception_t" ); |
---|
1484 | assert( exceptionDecl ); |
---|
1485 | ast::ptr< ast::Type > exceptType = |
---|
1486 | new ast::PointerType{ new ast::StructInstType{ exceptionDecl } }; |
---|
1487 | throwStmt = ast::mutate_field( |
---|
1488 | throwStmt, &ast::ThrowStmt::expr, |
---|
1489 | findSingleExpression( throwStmt->expr, exceptType, symtab ) ); |
---|
1490 | } |
---|
1491 | return throwStmt; |
---|
1492 | } |
---|
1493 | |
---|
1494 | const ast::CatchStmt * Resolver_new::previsit( const ast::CatchStmt * catchStmt ) { |
---|
1495 | // TODO: This will need a fix for the decl/cond scoping problem. |
---|
1496 | if ( catchStmt->cond ) { |
---|
1497 | ast::ptr< ast::Type > boolType = new ast::BasicType{ ast::BasicType::Bool }; |
---|
1498 | catchStmt = ast::mutate_field( |
---|
1499 | catchStmt, &ast::CatchStmt::cond, |
---|
1500 | findSingleExpression( catchStmt->cond, boolType, symtab ) ); |
---|
1501 | } |
---|
1502 | return catchStmt; |
---|
1503 | } |
---|
1504 | |
---|
1505 | const ast::WaitForStmt * Resolver_new::previsit( const ast::WaitForStmt * stmt ) { |
---|
1506 | visit_children = false; |
---|
1507 | |
---|
1508 | // Resolve all clauses first |
---|
1509 | for ( unsigned i = 0; i < stmt->clauses.size(); ++i ) { |
---|
1510 | const ast::WaitForStmt::Clause & clause = stmt->clauses[i]; |
---|
1511 | |
---|
1512 | ast::TypeEnvironment env; |
---|
1513 | CandidateFinder funcFinder{ symtab, env }; |
---|
1514 | |
---|
1515 | // Find all candidates for a function in canonical form |
---|
1516 | funcFinder.find( clause.target.func, ResolvMode::withAdjustment() ); |
---|
1517 | |
---|
1518 | if ( funcFinder.candidates.empty() ) { |
---|
1519 | stringstream ss; |
---|
1520 | ss << "Use of undeclared indentifier '"; |
---|
1521 | ss << clause.target.func.strict_as< ast::NameExpr >()->name; |
---|
1522 | ss << "' in call to waitfor"; |
---|
1523 | SemanticError( stmt->location, ss.str() ); |
---|
1524 | } |
---|
1525 | |
---|
1526 | if ( clause.target.args.empty() ) { |
---|
1527 | SemanticError( stmt->location, |
---|
1528 | "Waitfor clause must have at least one mutex parameter"); |
---|
1529 | } |
---|
1530 | |
---|
1531 | // Find all alternatives for all arguments in canonical form |
---|
1532 | std::vector< CandidateFinder > argFinders = |
---|
1533 | funcFinder.findSubExprs( clause.target.args ); |
---|
1534 | |
---|
1535 | // List all combinations of arguments |
---|
1536 | std::vector< CandidateList > possibilities; |
---|
1537 | combos( argFinders.begin(), argFinders.end(), back_inserter( possibilities ) ); |
---|
1538 | |
---|
1539 | // For every possible function: |
---|
1540 | // * try matching the arguments to the parameters, not the other way around because |
---|
1541 | // more arguments than parameters |
---|
1542 | CandidateList funcCandidates; |
---|
1543 | std::vector< CandidateList > argsCandidates; |
---|
1544 | SemanticErrorException errors; |
---|
1545 | for ( CandidateRef & func : funcFinder.candidates ) { |
---|
1546 | try { |
---|
1547 | auto pointerType = dynamic_cast< const ast::PointerType * >( |
---|
1548 | func->expr->result->stripReferences() ); |
---|
1549 | if ( ! pointerType ) { |
---|
1550 | SemanticError( stmt->location, func->expr->result.get(), |
---|
1551 | "candidate not viable: not a pointer type\n" ); |
---|
1552 | } |
---|
1553 | |
---|
1554 | auto funcType = pointerType->base.as< ast::FunctionType >(); |
---|
1555 | if ( ! funcType ) { |
---|
1556 | SemanticError( stmt->location, func->expr->result.get(), |
---|
1557 | "candidate not viable: not a function type\n" ); |
---|
1558 | } |
---|
1559 | |
---|
1560 | { |
---|
1561 | auto param = funcType->params.begin(); |
---|
1562 | auto paramEnd = funcType->params.end(); |
---|
1563 | |
---|
1564 | if( ! nextMutex( param, paramEnd ) ) { |
---|
1565 | SemanticError( stmt->location, funcType, |
---|
1566 | "candidate function not viable: no mutex parameters\n"); |
---|
1567 | } |
---|
1568 | } |
---|
1569 | |
---|
1570 | CandidateRef func2{ new Candidate{ *func } }; |
---|
1571 | // strip reference from function |
---|
1572 | func2->expr = referenceToRvalueConversion( func->expr, func2->cost ); |
---|
1573 | |
---|
1574 | // Each argument must be matched with a parameter of the current candidate |
---|
1575 | for ( auto & argsList : possibilities ) { |
---|
1576 | try { |
---|
1577 | // Declare data structures needed for resolution |
---|
1578 | ast::OpenVarSet open; |
---|
1579 | ast::AssertionSet need, have; |
---|
1580 | ast::TypeEnvironment resultEnv{ func->env }; |
---|
1581 | // Add all type variables as open so that those not used in the |
---|
1582 | // parameter list are still considered open |
---|
1583 | resultEnv.add( funcType->forall ); |
---|
1584 | |
---|
1585 | // load type variables from arguments into one shared space |
---|
1586 | for ( auto & arg : argsList ) { |
---|
1587 | resultEnv.simpleCombine( arg->env ); |
---|
1588 | } |
---|
1589 | |
---|
1590 | // Make sure we don't widen any existing bindings |
---|
1591 | resultEnv.forbidWidening(); |
---|
1592 | |
---|
1593 | // Find any unbound type variables |
---|
1594 | resultEnv.extractOpenVars( open ); |
---|
1595 | |
---|
1596 | auto param = funcType->params.begin(); |
---|
1597 | auto paramEnd = funcType->params.end(); |
---|
1598 | |
---|
1599 | unsigned n_mutex_param = 0; |
---|
1600 | |
---|
1601 | // For every argument of its set, check if it matches one of the |
---|
1602 | // parameters. The order is important |
---|
1603 | for ( auto & arg : argsList ) { |
---|
1604 | // Ignore non-mutex arguments |
---|
1605 | if ( ! nextMutex( param, paramEnd ) ) { |
---|
1606 | // We ran out of parameters but still have arguments. |
---|
1607 | // This function doesn't match |
---|
1608 | SemanticError( stmt->location, funcType, |
---|
1609 | toString("candidate function not viable: too many mutex " |
---|
1610 | "arguments, expected ", n_mutex_param, "\n" ) ); |
---|
1611 | } |
---|
1612 | |
---|
1613 | ++n_mutex_param; |
---|
1614 | |
---|
1615 | // Check if the argument matches the parameter type in the current |
---|
1616 | // scope |
---|
1617 | ast::ptr< ast::Type > paramType = (*param)->get_type(); |
---|
1618 | if ( |
---|
1619 | ! unify( |
---|
1620 | arg->expr->result, paramType, resultEnv, need, have, open, |
---|
1621 | symtab ) |
---|
1622 | ) { |
---|
1623 | // Type doesn't match |
---|
1624 | stringstream ss; |
---|
1625 | ss << "candidate function not viable: no known conversion " |
---|
1626 | "from '"; |
---|
1627 | ast::print( ss, (*param)->get_type() ); |
---|
1628 | ss << "' to '"; |
---|
1629 | ast::print( ss, arg->expr->result ); |
---|
1630 | ss << "' with env '"; |
---|
1631 | ast::print( ss, resultEnv ); |
---|
1632 | ss << "'\n"; |
---|
1633 | SemanticError( stmt->location, funcType, ss.str() ); |
---|
1634 | } |
---|
1635 | |
---|
1636 | ++param; |
---|
1637 | } |
---|
1638 | |
---|
1639 | // All arguments match! |
---|
1640 | |
---|
1641 | // Check if parameters are missing |
---|
1642 | if ( nextMutex( param, paramEnd ) ) { |
---|
1643 | do { |
---|
1644 | ++n_mutex_param; |
---|
1645 | ++param; |
---|
1646 | } while ( nextMutex( param, paramEnd ) ); |
---|
1647 | |
---|
1648 | // We ran out of arguments but still have parameters left; this |
---|
1649 | // function doesn't match |
---|
1650 | SemanticError( stmt->location, funcType, |
---|
1651 | toString( "candidate function not viable: too few mutex " |
---|
1652 | "arguments, expected ", n_mutex_param, "\n" ) ); |
---|
1653 | } |
---|
1654 | |
---|
1655 | // All parameters match! |
---|
1656 | |
---|
1657 | // Finish the expressions to tie in proper environments |
---|
1658 | finishExpr( func2->expr, resultEnv ); |
---|
1659 | for ( CandidateRef & arg : argsList ) { |
---|
1660 | finishExpr( arg->expr, resultEnv ); |
---|
1661 | } |
---|
1662 | |
---|
1663 | // This is a match, store it and save it for later |
---|
1664 | funcCandidates.emplace_back( std::move( func2 ) ); |
---|
1665 | argsCandidates.emplace_back( std::move( argsList ) ); |
---|
1666 | |
---|
1667 | } catch ( SemanticErrorException & e ) { |
---|
1668 | errors.append( e ); |
---|
1669 | } |
---|
1670 | } |
---|
1671 | } catch ( SemanticErrorException & e ) { |
---|
1672 | errors.append( e ); |
---|
1673 | } |
---|
1674 | } |
---|
1675 | |
---|
1676 | // Make sure correct number of arguments |
---|
1677 | if( funcCandidates.empty() ) { |
---|
1678 | SemanticErrorException top( stmt->location, |
---|
1679 | "No alternatives for function in call to waitfor" ); |
---|
1680 | top.append( errors ); |
---|
1681 | throw top; |
---|
1682 | } |
---|
1683 | |
---|
1684 | if( argsCandidates.empty() ) { |
---|
1685 | SemanticErrorException top( stmt->location, |
---|
1686 | "No alternatives for arguments in call to waitfor" ); |
---|
1687 | top.append( errors ); |
---|
1688 | throw top; |
---|
1689 | } |
---|
1690 | |
---|
1691 | if( funcCandidates.size() > 1 ) { |
---|
1692 | SemanticErrorException top( stmt->location, |
---|
1693 | "Ambiguous function in call to waitfor" ); |
---|
1694 | top.append( errors ); |
---|
1695 | throw top; |
---|
1696 | } |
---|
1697 | if( argsCandidates.size() > 1 ) { |
---|
1698 | SemanticErrorException top( stmt->location, |
---|
1699 | "Ambiguous arguments in call to waitfor" ); |
---|
1700 | top.append( errors ); |
---|
1701 | throw top; |
---|
1702 | } |
---|
1703 | // TODO: need to use findDeletedExpr to ensure no deleted identifiers are used. |
---|
1704 | |
---|
1705 | // build new clause |
---|
1706 | ast::WaitForStmt::Clause clause2; |
---|
1707 | |
---|
1708 | clause2.target.func = funcCandidates.front()->expr; |
---|
1709 | |
---|
1710 | clause2.target.args.reserve( clause.target.args.size() ); |
---|
1711 | for ( auto arg : argsCandidates.front() ) { |
---|
1712 | clause2.target.args.emplace_back( std::move( arg->expr ) ); |
---|
1713 | } |
---|
1714 | |
---|
1715 | // Resolve the conditions as if it were an IfStmt, statements normally |
---|
1716 | clause2.cond = findSingleExpression( clause.cond, symtab ); |
---|
1717 | clause2.stmt = clause.stmt->accept( *visitor ); |
---|
1718 | |
---|
1719 | // set results into stmt |
---|
1720 | auto n = mutate( stmt ); |
---|
1721 | n->clauses[i] = std::move( clause2 ); |
---|
1722 | stmt = n; |
---|
1723 | } |
---|
1724 | |
---|
1725 | if ( stmt->timeout.stmt ) { |
---|
1726 | // resolve the timeout as a size_t, the conditions like IfStmt, and stmts normally |
---|
1727 | ast::WaitForStmt::Timeout timeout2; |
---|
1728 | |
---|
1729 | ast::ptr< ast::Type > target = |
---|
1730 | new ast::BasicType{ ast::BasicType::LongLongUnsignedInt }; |
---|
1731 | timeout2.time = findSingleExpression( stmt->timeout.time, target, symtab ); |
---|
1732 | timeout2.cond = findSingleExpression( stmt->timeout.cond, symtab ); |
---|
1733 | timeout2.stmt = stmt->timeout.stmt->accept( *visitor ); |
---|
1734 | |
---|
1735 | // set results into stmt |
---|
1736 | auto n = mutate( stmt ); |
---|
1737 | n->timeout = std::move( timeout2 ); |
---|
1738 | stmt = n; |
---|
1739 | } |
---|
1740 | |
---|
1741 | if ( stmt->orElse.stmt ) { |
---|
1742 | // resolve the condition like IfStmt, stmts normally |
---|
1743 | ast::WaitForStmt::OrElse orElse2; |
---|
1744 | |
---|
1745 | orElse2.cond = findSingleExpression( stmt->orElse.cond, symtab ); |
---|
1746 | orElse2.stmt = stmt->orElse.stmt->accept( *visitor ); |
---|
1747 | |
---|
1748 | // set results into stmt |
---|
1749 | auto n = mutate( stmt ); |
---|
1750 | n->orElse = std::move( orElse2 ); |
---|
1751 | stmt = n; |
---|
1752 | } |
---|
1753 | |
---|
1754 | return stmt; |
---|
1755 | } |
---|
1756 | |
---|
1757 | |
---|
1758 | |
---|
1759 | const ast::SingleInit * Resolver_new::previsit( const ast::SingleInit * singleInit ) { |
---|
1760 | visit_children = false; |
---|
1761 | // resolve initialization using the possibilities as determined by the `currentObject` |
---|
1762 | // cursor. |
---|
1763 | ast::ptr< ast::Expr > untyped = new ast::UntypedInitExpr{ |
---|
1764 | singleInit->location, singleInit->value, currentObject.getOptions() }; |
---|
1765 | ast::ptr<ast::Expr> newExpr = findSingleExpression( untyped, symtab ); |
---|
1766 | const ast::InitExpr * initExpr = newExpr.strict_as< ast::InitExpr >(); |
---|
1767 | |
---|
1768 | // move cursor to the object that is actually initialized |
---|
1769 | currentObject.setNext( initExpr->designation ); |
---|
1770 | |
---|
1771 | // discard InitExpr wrapper and retain relevant pieces. |
---|
1772 | // `initExpr` may have inferred params in the case where the expression specialized a |
---|
1773 | // function pointer, and newExpr may already have inferParams of its own, so a simple |
---|
1774 | // swap is not sufficient |
---|
1775 | ast::Expr::InferUnion inferred = initExpr->inferred; |
---|
1776 | swap_and_save_env( newExpr, initExpr->expr ); |
---|
1777 | newExpr.get_and_mutate()->inferred.splice( std::move(inferred) ); |
---|
1778 | |
---|
1779 | // get the actual object's type (may not exactly match what comes back from the resolver |
---|
1780 | // due to conversions) |
---|
1781 | const ast::Type * initContext = currentObject.getCurrentType(); |
---|
1782 | |
---|
1783 | removeExtraneousCast( newExpr, symtab ); |
---|
1784 | |
---|
1785 | // check if actual object's type is char[] |
---|
1786 | if ( auto at = dynamic_cast< const ast::ArrayType * >( initContext ) ) { |
---|
1787 | if ( isCharType( at->base ) ) { |
---|
1788 | // check if the resolved type is char* |
---|
1789 | if ( auto pt = newExpr->result.as< ast::PointerType >() ) { |
---|
1790 | if ( isCharType( pt->base ) ) { |
---|
1791 | // strip cast if we're initializing a char[] with a char* |
---|
1792 | // e.g. char x[] = "hello" |
---|
1793 | if ( auto ce = newExpr.as< ast::CastExpr >() ) { |
---|
1794 | swap_and_save_env( newExpr, ce->arg ); |
---|
1795 | } |
---|
1796 | } |
---|
1797 | } |
---|
1798 | } |
---|
1799 | } |
---|
1800 | |
---|
1801 | // move cursor to next object in preparation for next initializer |
---|
1802 | currentObject.increment(); |
---|
1803 | |
---|
1804 | // set initializer expression to resolved expression |
---|
1805 | return ast::mutate_field( singleInit, &ast::SingleInit::value, std::move(newExpr) ); |
---|
1806 | } |
---|
1807 | |
---|
1808 | const ast::ListInit * Resolver_new::previsit( const ast::ListInit * listInit ) { |
---|
1809 | // move cursor into brace-enclosed initializer-list |
---|
1810 | currentObject.enterListInit( listInit->location ); |
---|
1811 | |
---|
1812 | assert( listInit->designations.size() == listInit->initializers.size() ); |
---|
1813 | for ( unsigned i = 0; i < listInit->designations.size(); ++i ) { |
---|
1814 | // iterate designations and initializers in pairs, moving the cursor to the current |
---|
1815 | // designated object and resolving the initializer against that object |
---|
1816 | listInit = ast::mutate_field_index( |
---|
1817 | listInit, &ast::ListInit::designations, i, |
---|
1818 | currentObject.findNext( listInit->designations[i] ) ); |
---|
1819 | listInit = ast::mutate_field_index( |
---|
1820 | listInit, &ast::ListInit::initializers, i, |
---|
1821 | listInit->initializers[i]->accept( *visitor ) ); |
---|
1822 | } |
---|
1823 | |
---|
1824 | // move cursor out of brace-enclosed initializer-list |
---|
1825 | currentObject.exitListInit(); |
---|
1826 | |
---|
1827 | visit_children = false; |
---|
1828 | return listInit; |
---|
1829 | } |
---|
1830 | |
---|
1831 | const ast::ConstructorInit * Resolver_new::previsit( const ast::ConstructorInit * ctorInit ) { |
---|
1832 | visitor->maybe_accept( ctorInit, &ast::ConstructorInit::ctor ); |
---|
1833 | visitor->maybe_accept( ctorInit, &ast::ConstructorInit::dtor ); |
---|
1834 | |
---|
1835 | // found a constructor - can get rid of C-style initializer |
---|
1836 | // xxx - Rob suggests this field is dead code |
---|
1837 | ctorInit = ast::mutate_field( ctorInit, &ast::ConstructorInit::init, nullptr ); |
---|
1838 | |
---|
1839 | // intrinsic single-parameter constructors and destructors do nothing. Since this was |
---|
1840 | // implicitly generated, there's no way for it to have side effects, so get rid of it to |
---|
1841 | // clean up generated code |
---|
1842 | if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->ctor ) ) { |
---|
1843 | ctorInit = ast::mutate_field( ctorInit, &ast::ConstructorInit::ctor, nullptr ); |
---|
1844 | } |
---|
1845 | if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->dtor ) ) { |
---|
1846 | ctorInit = ast::mutate_field( ctorInit, &ast::ConstructorInit::dtor, nullptr ); |
---|
1847 | } |
---|
1848 | |
---|
1849 | return ctorInit; |
---|
1850 | } |
---|
1851 | |
---|
1852 | } // namespace ResolvExpr |
---|
1853 | |
---|
1854 | // Local Variables: // |
---|
1855 | // tab-width: 4 // |
---|
1856 | // mode: c++ // |
---|
1857 | // compile-command: "make install" // |
---|
1858 | // End: // |
---|