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 : Richard C. Bilson |
---|
10 | // Created On : Sun May 17 12:17:01 2015 |
---|
11 | // Last Modified By : Andrew Beach |
---|
12 | // Last Modified On : Tus Aug 8 16:06:00 2017 |
---|
13 | // Update Count : 212 |
---|
14 | // |
---|
15 | |
---|
16 | #include <stddef.h> // for NULL |
---|
17 | #include <cassert> // for strict_dynamic_cast, assert |
---|
18 | #include <memory> // for allocator, allocator_traits<... |
---|
19 | #include <tuple> // for get |
---|
20 | #include <vector> |
---|
21 | |
---|
22 | #include "Alternative.h" // for Alternative, AltList |
---|
23 | #include "AlternativeFinder.h" // for AlternativeFinder, resolveIn... |
---|
24 | #include "Common/PassVisitor.h" // for PassVisitor |
---|
25 | #include "Common/SemanticError.h" // for SemanticError |
---|
26 | #include "Common/utility.h" // for ValueGuard, group_iterate |
---|
27 | #include "CurrentObject.h" // for CurrentObject |
---|
28 | #include "InitTweak/GenInit.h" |
---|
29 | #include "InitTweak/InitTweak.h" // for isIntrinsicSingleArgCallStmt |
---|
30 | #include "RenameVars.h" // for RenameVars, global_renamer |
---|
31 | #include "ResolvExpr/TypeEnvironment.h" // for TypeEnvironment |
---|
32 | #include "ResolveTypeof.h" // for resolveTypeof |
---|
33 | #include "Resolver.h" |
---|
34 | #include "SymTab/Autogen.h" // for SizeType |
---|
35 | #include "SymTab/Indexer.h" // for Indexer |
---|
36 | #include "SynTree/Declaration.h" // for ObjectDecl, TypeDecl, Declar... |
---|
37 | #include "SynTree/Expression.h" // for Expression, CastExpr, InitExpr |
---|
38 | #include "SynTree/Initializer.h" // for ConstructorInit, SingleInit |
---|
39 | #include "SynTree/Statement.h" // for ForStmt, Statement, BranchStmt |
---|
40 | #include "SynTree/Type.h" // for Type, BasicType, PointerType |
---|
41 | #include "SynTree/TypeSubstitution.h" // for TypeSubstitution |
---|
42 | #include "SynTree/Visitor.h" // for acceptAll, maybeAccept |
---|
43 | #include "Tuples/Tuples.h" |
---|
44 | #include "typeops.h" // for extractResultType |
---|
45 | #include "Unify.h" // for unify |
---|
46 | |
---|
47 | using namespace std; |
---|
48 | |
---|
49 | namespace ResolvExpr { |
---|
50 | struct Resolver final : public WithIndexer, public WithGuards, public WithVisitorRef<Resolver>, public WithShortCircuiting, public WithStmtsToAdd { |
---|
51 | Resolver() {} |
---|
52 | Resolver( const SymTab::Indexer & other ) { |
---|
53 | indexer = other; |
---|
54 | } |
---|
55 | |
---|
56 | void previsit( FunctionDecl *functionDecl ); |
---|
57 | void postvisit( FunctionDecl *functionDecl ); |
---|
58 | void previsit( ObjectDecl *objectDecll ); |
---|
59 | void previsit( TypeDecl *typeDecl ); |
---|
60 | void previsit( EnumDecl * enumDecl ); |
---|
61 | |
---|
62 | void previsit( ArrayType * at ); |
---|
63 | void previsit( PointerType * at ); |
---|
64 | |
---|
65 | void previsit( ExprStmt *exprStmt ); |
---|
66 | void previsit( AsmExpr *asmExpr ); |
---|
67 | void previsit( AsmStmt *asmStmt ); |
---|
68 | void previsit( IfStmt *ifStmt ); |
---|
69 | void previsit( WhileStmt *whileStmt ); |
---|
70 | void previsit( ForStmt *forStmt ); |
---|
71 | void previsit( SwitchStmt *switchStmt ); |
---|
72 | void previsit( CaseStmt *caseStmt ); |
---|
73 | void previsit( BranchStmt *branchStmt ); |
---|
74 | void previsit( ReturnStmt *returnStmt ); |
---|
75 | void previsit( ThrowStmt *throwStmt ); |
---|
76 | void previsit( CatchStmt *catchStmt ); |
---|
77 | void previsit( WaitForStmt * stmt ); |
---|
78 | void previsit( WithStmt * withStmt ); |
---|
79 | |
---|
80 | void previsit( SingleInit *singleInit ); |
---|
81 | void previsit( ListInit *listInit ); |
---|
82 | void previsit( ConstructorInit *ctorInit ); |
---|
83 | private: |
---|
84 | typedef std::list< Initializer * >::iterator InitIterator; |
---|
85 | |
---|
86 | template< typename PtrType > |
---|
87 | void handlePtrType( PtrType * type ); |
---|
88 | |
---|
89 | void resolveWithExprs( std::list< Expression * > & withExprs, std::list< Statement * > & newStmts ); |
---|
90 | void fallbackInit( ConstructorInit * ctorInit ); |
---|
91 | |
---|
92 | Type * functionReturn = nullptr; |
---|
93 | CurrentObject currentObject = nullptr; |
---|
94 | bool inEnumDecl = false; |
---|
95 | }; |
---|
96 | |
---|
97 | void resolve( std::list< Declaration * > translationUnit ) { |
---|
98 | PassVisitor<Resolver> resolver; |
---|
99 | acceptAll( translationUnit, resolver ); |
---|
100 | } |
---|
101 | |
---|
102 | void resolveDecl( Declaration * decl, const SymTab::Indexer &indexer ) { |
---|
103 | PassVisitor<Resolver> resolver( indexer ); |
---|
104 | maybeAccept( decl, resolver ); |
---|
105 | } |
---|
106 | |
---|
107 | // used in resolveTypeof |
---|
108 | Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer ) { |
---|
109 | TypeEnvironment env; |
---|
110 | return resolveInVoidContext( expr, indexer, env ); |
---|
111 | } |
---|
112 | |
---|
113 | namespace { |
---|
114 | void finishExpr( Expression *expr, const TypeEnvironment &env, TypeSubstitution * oldenv = nullptr ) { |
---|
115 | expr->env = oldenv ? oldenv->clone() : new TypeSubstitution; |
---|
116 | env.makeSubstitution( *expr->get_env() ); |
---|
117 | } |
---|
118 | |
---|
119 | void removeExtraneousCast( Expression *& expr, const SymTab::Indexer & indexer ) { |
---|
120 | if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) { |
---|
121 | if ( ResolvExpr::typesCompatible( castExpr->arg->result, castExpr->result, indexer ) ) { |
---|
122 | // cast is to the same type as its argument, so it's unnecessary -- remove it |
---|
123 | expr = castExpr->arg; |
---|
124 | castExpr->arg = nullptr; |
---|
125 | std::swap( expr->env, castExpr->env ); |
---|
126 | delete castExpr; |
---|
127 | } |
---|
128 | } |
---|
129 | } |
---|
130 | } // namespace |
---|
131 | |
---|
132 | void findVoidExpression( Expression *& untyped, const SymTab::Indexer &indexer ) { |
---|
133 | resetTyVarRenaming(); |
---|
134 | TypeEnvironment env; |
---|
135 | Expression *newExpr = resolveInVoidContext( untyped, indexer, env ); |
---|
136 | finishExpr( newExpr, env, untyped->env ); |
---|
137 | delete untyped; |
---|
138 | untyped = newExpr; |
---|
139 | } |
---|
140 | |
---|
141 | void findSingleExpression( Expression *&untyped, const SymTab::Indexer &indexer ) { |
---|
142 | if ( ! untyped ) return; |
---|
143 | TypeEnvironment env; |
---|
144 | AlternativeFinder finder( indexer, env ); |
---|
145 | finder.find( untyped ); |
---|
146 | #if 0 |
---|
147 | if ( finder.get_alternatives().size() != 1 ) { |
---|
148 | std::cerr << "untyped expr is "; |
---|
149 | untyped->print( std::cerr ); |
---|
150 | std::cerr << std::endl << "alternatives are:"; |
---|
151 | for ( const Alternative & alt : finder.get_alternatives() ) { |
---|
152 | alt.print( std::cerr ); |
---|
153 | } // for |
---|
154 | } // if |
---|
155 | #endif |
---|
156 | assertf( finder.get_alternatives().size() == 1, "findSingleExpression: must have exactly one alternative at the end: (%zd) %s", finder.get_alternatives().size(), toString( untyped ).c_str() ); |
---|
157 | Alternative &choice = finder.get_alternatives().front(); |
---|
158 | Expression *newExpr = choice.expr->clone(); |
---|
159 | finishExpr( newExpr, choice.env, untyped->env ); |
---|
160 | delete untyped; |
---|
161 | untyped = newExpr; |
---|
162 | } |
---|
163 | |
---|
164 | void findSingleExpression( Expression *& untyped, Type * type, const SymTab::Indexer & indexer ) { |
---|
165 | assert( untyped && type ); |
---|
166 | untyped = new CastExpr( untyped, type ); |
---|
167 | findSingleExpression( untyped, indexer ); |
---|
168 | removeExtraneousCast( untyped, indexer ); |
---|
169 | } |
---|
170 | |
---|
171 | namespace { |
---|
172 | /// resolve `untyped` to the expression whose type satisfies `pred` with the lowest cost; kindStr is used for providing better error messages |
---|
173 | template<typename Pred> |
---|
174 | void findKindExpression(Expression *& untyped, const SymTab::Indexer & indexer, const std::string & kindStr, Pred pred) { |
---|
175 | TypeEnvironment env; |
---|
176 | AlternativeFinder finder( indexer, env ); |
---|
177 | finder.findWithAdjustment( untyped ); |
---|
178 | |
---|
179 | AltList candidates; |
---|
180 | for ( Alternative & alt : finder.get_alternatives() ) { |
---|
181 | if ( pred( alt.expr->result ) ) { |
---|
182 | candidates.push_back( std::move( alt ) ); |
---|
183 | } |
---|
184 | } |
---|
185 | |
---|
186 | // choose the lowest cost expression among the candidates |
---|
187 | AltList winners; |
---|
188 | findMinCost( candidates.begin(), candidates.end(), back_inserter( winners ) ); |
---|
189 | if ( winners.size() == 0 ) { |
---|
190 | throw SemanticError( "No reasonable alternatives for " + kindStr + " expression: ", untyped ); |
---|
191 | } else if ( winners.size() != 1 ) { |
---|
192 | std::ostringstream stream; |
---|
193 | stream << "Cannot choose between " << winners.size() << " alternatives for " + kindStr + " expression\n"; |
---|
194 | untyped->print( stream ); |
---|
195 | stream << "Alternatives are:\n"; |
---|
196 | printAlts( winners, stream, 1 ); |
---|
197 | throw SemanticError( stream.str() ); |
---|
198 | } |
---|
199 | |
---|
200 | // there is one unambiguous interpretation - move the expression into the with statement |
---|
201 | Alternative & alt = winners.front(); |
---|
202 | finishExpr( alt.expr, alt.env, untyped->env ); |
---|
203 | delete untyped; |
---|
204 | untyped = alt.expr; |
---|
205 | alt.expr = nullptr; |
---|
206 | } |
---|
207 | |
---|
208 | bool isIntegralType( Type *type ) { |
---|
209 | if ( dynamic_cast< EnumInstType * >( type ) ) { |
---|
210 | return true; |
---|
211 | } else if ( BasicType *bt = dynamic_cast< BasicType * >( type ) ) { |
---|
212 | return bt->isInteger(); |
---|
213 | } else if ( dynamic_cast< ZeroType* >( type ) != nullptr || dynamic_cast< OneType* >( type ) != nullptr ) { |
---|
214 | return true; |
---|
215 | } else { |
---|
216 | return false; |
---|
217 | } // if |
---|
218 | } |
---|
219 | |
---|
220 | void findIntegralExpression( Expression *& untyped, const SymTab::Indexer &indexer ) { |
---|
221 | findKindExpression( untyped, indexer, "condition", isIntegralType ); |
---|
222 | } |
---|
223 | } |
---|
224 | |
---|
225 | void Resolver::previsit( ObjectDecl *objectDecl ) { |
---|
226 | Type *new_type = resolveTypeof( objectDecl->get_type(), indexer ); |
---|
227 | objectDecl->set_type( new_type ); |
---|
228 | // To handle initialization of routine pointers, e.g., int (*fp)(int) = foo(), means that class-variable |
---|
229 | // initContext is changed multiple time because the LHS is analysed twice. The second analysis changes |
---|
230 | // initContext because of a function type can contain object declarations in the return and parameter types. So |
---|
231 | // each value of initContext is retained, so the type on the first analysis is preserved and used for selecting |
---|
232 | // the RHS. |
---|
233 | GuardValue( currentObject ); |
---|
234 | currentObject = CurrentObject( objectDecl->get_type() ); |
---|
235 | if ( inEnumDecl && dynamic_cast< EnumInstType * >( objectDecl->get_type() ) ) { |
---|
236 | // enumerator initializers should not use the enum type to initialize, since |
---|
237 | // the enum type is still incomplete at this point. Use signed int instead. |
---|
238 | currentObject = CurrentObject( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) ); |
---|
239 | } |
---|
240 | } |
---|
241 | |
---|
242 | template< typename PtrType > |
---|
243 | void Resolver::handlePtrType( PtrType * type ) { |
---|
244 | if ( type->get_dimension() ) { |
---|
245 | findSingleExpression( type->dimension, SymTab::SizeType->clone(), indexer ); |
---|
246 | } |
---|
247 | } |
---|
248 | |
---|
249 | void Resolver::previsit( ArrayType * at ) { |
---|
250 | handlePtrType( at ); |
---|
251 | } |
---|
252 | |
---|
253 | void Resolver::previsit( PointerType * pt ) { |
---|
254 | handlePtrType( pt ); |
---|
255 | } |
---|
256 | |
---|
257 | void Resolver::previsit( TypeDecl *typeDecl ) { |
---|
258 | if ( typeDecl->get_base() ) { |
---|
259 | Type *new_type = resolveTypeof( typeDecl->get_base(), indexer ); |
---|
260 | typeDecl->set_base( new_type ); |
---|
261 | } // if |
---|
262 | } |
---|
263 | |
---|
264 | void Resolver::previsit( FunctionDecl *functionDecl ) { |
---|
265 | #if 0 |
---|
266 | std::cerr << "resolver visiting functiondecl "; |
---|
267 | functionDecl->print( std::cerr ); |
---|
268 | std::cerr << std::endl; |
---|
269 | #endif |
---|
270 | Type *new_type = resolveTypeof( functionDecl->type, indexer ); |
---|
271 | functionDecl->set_type( new_type ); |
---|
272 | GuardValue( functionReturn ); |
---|
273 | functionReturn = ResolvExpr::extractResultType( functionDecl->type ); |
---|
274 | |
---|
275 | { |
---|
276 | // resolve with-exprs with parameters in scope and add any newly generated declarations to the |
---|
277 | // front of the function body. |
---|
278 | auto guard = makeFuncGuard( [this]() { indexer.enterScope(); }, [this](){ indexer.leaveScope(); } ); |
---|
279 | indexer.addFunctionType( functionDecl->type ); |
---|
280 | std::list< Statement * > newStmts; |
---|
281 | resolveWithExprs( functionDecl->withExprs, newStmts ); |
---|
282 | if ( functionDecl->statements ) { |
---|
283 | functionDecl->statements->kids.splice( functionDecl->statements->kids.begin(), newStmts ); |
---|
284 | } else { |
---|
285 | assertf( functionDecl->withExprs.empty() && newStmts.empty(), "Function %s without a body has with-clause and/or generated with declarations.", functionDecl->name.c_str() ); |
---|
286 | } |
---|
287 | } |
---|
288 | } |
---|
289 | |
---|
290 | void Resolver::postvisit( FunctionDecl *functionDecl ) { |
---|
291 | // default value expressions have an environment which shouldn't be there and trips up later passes. |
---|
292 | // xxx - it might be necessary to somehow keep the information from this environment, but I can't currently |
---|
293 | // see how it's useful. |
---|
294 | for ( Declaration * d : functionDecl->type->parameters ) { |
---|
295 | if ( ObjectDecl * obj = dynamic_cast< ObjectDecl * >( d ) ) { |
---|
296 | if ( SingleInit * init = dynamic_cast< SingleInit * >( obj->init ) ) { |
---|
297 | delete init->value->env; |
---|
298 | init->value->env = nullptr; |
---|
299 | } |
---|
300 | } |
---|
301 | } |
---|
302 | } |
---|
303 | |
---|
304 | void Resolver::previsit( EnumDecl * ) { |
---|
305 | // in case we decide to allow nested enums |
---|
306 | GuardValue( inEnumDecl ); |
---|
307 | inEnumDecl = true; |
---|
308 | } |
---|
309 | |
---|
310 | void Resolver::previsit( ExprStmt *exprStmt ) { |
---|
311 | visit_children = false; |
---|
312 | assertf( exprStmt->expr, "ExprStmt has null Expression in resolver" ); |
---|
313 | findVoidExpression( exprStmt->expr, indexer ); |
---|
314 | } |
---|
315 | |
---|
316 | void Resolver::previsit( AsmExpr *asmExpr ) { |
---|
317 | visit_children = false; |
---|
318 | findVoidExpression( asmExpr->operand, indexer ); |
---|
319 | if ( asmExpr->get_inout() ) { |
---|
320 | findVoidExpression( asmExpr->inout, indexer ); |
---|
321 | } // if |
---|
322 | } |
---|
323 | |
---|
324 | void Resolver::previsit( AsmStmt *asmStmt ) { |
---|
325 | visit_children = false; |
---|
326 | acceptAll( asmStmt->get_input(), *visitor ); |
---|
327 | acceptAll( asmStmt->get_output(), *visitor ); |
---|
328 | } |
---|
329 | |
---|
330 | void Resolver::previsit( IfStmt *ifStmt ) { |
---|
331 | findIntegralExpression( ifStmt->condition, indexer ); |
---|
332 | } |
---|
333 | |
---|
334 | void Resolver::previsit( WhileStmt *whileStmt ) { |
---|
335 | findIntegralExpression( whileStmt->condition, indexer ); |
---|
336 | } |
---|
337 | |
---|
338 | void Resolver::previsit( ForStmt *forStmt ) { |
---|
339 | if ( forStmt->condition ) { |
---|
340 | findIntegralExpression( forStmt->condition, indexer ); |
---|
341 | } // if |
---|
342 | |
---|
343 | if ( forStmt->increment ) { |
---|
344 | findVoidExpression( forStmt->increment, indexer ); |
---|
345 | } // if |
---|
346 | } |
---|
347 | |
---|
348 | void Resolver::previsit( SwitchStmt *switchStmt ) { |
---|
349 | GuardValue( currentObject ); |
---|
350 | findIntegralExpression( switchStmt->condition, indexer ); |
---|
351 | |
---|
352 | currentObject = CurrentObject( switchStmt->condition->result ); |
---|
353 | } |
---|
354 | |
---|
355 | void Resolver::previsit( CaseStmt *caseStmt ) { |
---|
356 | if ( caseStmt->get_condition() ) { |
---|
357 | std::list< InitAlternative > initAlts = currentObject.getOptions(); |
---|
358 | assertf( initAlts.size() == 1, "SwitchStmt did not correctly resolve an integral expression." ); |
---|
359 | // must remove cast from case statement because RangeExpr cannot be cast. |
---|
360 | Expression * newExpr = new CastExpr( caseStmt->condition, initAlts.front().type->clone() ); |
---|
361 | findSingleExpression( newExpr, indexer ); |
---|
362 | CastExpr * castExpr = strict_dynamic_cast< CastExpr * >( newExpr ); |
---|
363 | caseStmt->condition = castExpr->arg; |
---|
364 | castExpr->arg = nullptr; |
---|
365 | delete castExpr; |
---|
366 | } |
---|
367 | } |
---|
368 | |
---|
369 | void Resolver::previsit( BranchStmt *branchStmt ) { |
---|
370 | visit_children = false; |
---|
371 | // must resolve the argument for a computed goto |
---|
372 | if ( branchStmt->get_type() == BranchStmt::Goto ) { // check for computed goto statement |
---|
373 | if ( branchStmt->computedTarget ) { |
---|
374 | // computed goto argument is void * |
---|
375 | findSingleExpression( branchStmt->computedTarget, new PointerType( Type::Qualifiers(), new VoidType( Type::Qualifiers() ) ), indexer ); |
---|
376 | } // if |
---|
377 | } // if |
---|
378 | } |
---|
379 | |
---|
380 | void Resolver::previsit( ReturnStmt *returnStmt ) { |
---|
381 | visit_children = false; |
---|
382 | if ( returnStmt->expr ) { |
---|
383 | findSingleExpression( returnStmt->expr, functionReturn->clone(), indexer ); |
---|
384 | } // if |
---|
385 | } |
---|
386 | |
---|
387 | void Resolver::previsit( ThrowStmt *throwStmt ) { |
---|
388 | visit_children = false; |
---|
389 | // TODO: Replace *exception type with &exception type. |
---|
390 | if ( throwStmt->get_expr() ) { |
---|
391 | StructDecl * exception_decl = |
---|
392 | indexer.lookupStruct( "__cfaabi_ehm__base_exception_t" ); |
---|
393 | assert( exception_decl ); |
---|
394 | Type * exceptType = new PointerType( noQualifiers, new StructInstType( noQualifiers, exception_decl ) ); |
---|
395 | findSingleExpression( throwStmt->expr, exceptType, indexer ); |
---|
396 | } |
---|
397 | } |
---|
398 | |
---|
399 | void Resolver::previsit( CatchStmt *catchStmt ) { |
---|
400 | if ( catchStmt->cond ) { |
---|
401 | findSingleExpression( catchStmt->cond, new BasicType( noQualifiers, BasicType::Bool ), indexer ); |
---|
402 | } |
---|
403 | } |
---|
404 | |
---|
405 | template< typename iterator_t > |
---|
406 | inline bool advance_to_mutex( iterator_t & it, const iterator_t & end ) { |
---|
407 | while( it != end && !(*it)->get_type()->get_mutex() ) { |
---|
408 | it++; |
---|
409 | } |
---|
410 | |
---|
411 | return it != end; |
---|
412 | } |
---|
413 | |
---|
414 | void Resolver::previsit( WaitForStmt * stmt ) { |
---|
415 | visit_children = false; |
---|
416 | |
---|
417 | // Resolve all clauses first |
---|
418 | for( auto& clause : stmt->clauses ) { |
---|
419 | |
---|
420 | TypeEnvironment env; |
---|
421 | AlternativeFinder funcFinder( indexer, env ); |
---|
422 | |
---|
423 | // Find all alternatives for a function in canonical form |
---|
424 | funcFinder.findWithAdjustment( clause.target.function ); |
---|
425 | |
---|
426 | if ( funcFinder.get_alternatives().empty() ) { |
---|
427 | stringstream ss; |
---|
428 | ss << "Use of undeclared indentifier '"; |
---|
429 | ss << strict_dynamic_cast<NameExpr*>( clause.target.function )->name; |
---|
430 | ss << "' in call to waitfor"; |
---|
431 | throw SemanticError( ss.str() ); |
---|
432 | } |
---|
433 | |
---|
434 | // Find all alternatives for all arguments in canonical form |
---|
435 | std::vector< AlternativeFinder > argAlternatives; |
---|
436 | funcFinder.findSubExprs( clause.target.arguments.begin(), clause.target.arguments.end(), back_inserter( argAlternatives ) ); |
---|
437 | |
---|
438 | // List all combinations of arguments |
---|
439 | std::vector< AltList > possibilities; |
---|
440 | combos( argAlternatives.begin(), argAlternatives.end(), back_inserter( possibilities ) ); |
---|
441 | |
---|
442 | AltList func_candidates; |
---|
443 | std::vector< AltList > args_candidates; |
---|
444 | |
---|
445 | // For every possible function : |
---|
446 | // try matching the arguments to the parameters |
---|
447 | // not the other way around because we have more arguments than parameters |
---|
448 | SemanticError errors; |
---|
449 | for ( Alternative & func : funcFinder.get_alternatives() ) { |
---|
450 | try { |
---|
451 | PointerType * pointer = dynamic_cast< PointerType* >( func.expr->get_result()->stripReferences() ); |
---|
452 | if( !pointer ) { |
---|
453 | throw SemanticError( "candidate not viable: not a pointer type\n", func.expr->get_result() ); |
---|
454 | } |
---|
455 | |
---|
456 | FunctionType * function = dynamic_cast< FunctionType* >( pointer->get_base() ); |
---|
457 | if( !function ) { |
---|
458 | throw SemanticError( "candidate not viable: not a function type\n", pointer->get_base() ); |
---|
459 | } |
---|
460 | |
---|
461 | |
---|
462 | { |
---|
463 | auto param = function->parameters.begin(); |
---|
464 | auto param_end = function->parameters.end(); |
---|
465 | |
---|
466 | if( !advance_to_mutex( param, param_end ) ) { |
---|
467 | throw SemanticError("candidate function not viable: no mutex parameters\n", function); |
---|
468 | } |
---|
469 | } |
---|
470 | |
---|
471 | Alternative newFunc( func ); |
---|
472 | // Strip reference from function |
---|
473 | referenceToRvalueConversion( newFunc.expr ); |
---|
474 | |
---|
475 | // For all the set of arguments we have try to match it with the parameter of the current function alternative |
---|
476 | for ( auto & argsList : possibilities ) { |
---|
477 | |
---|
478 | try { |
---|
479 | // Declare data structures need for resolution |
---|
480 | OpenVarSet openVars; |
---|
481 | AssertionSet resultNeed, resultHave; |
---|
482 | TypeEnvironment resultEnv; |
---|
483 | |
---|
484 | // Load type variables from arguemnts into one shared space |
---|
485 | simpleCombineEnvironments( argsList.begin(), argsList.end(), resultEnv ); |
---|
486 | |
---|
487 | // Make sure we don't widen any existing bindings |
---|
488 | for ( auto & i : resultEnv ) { |
---|
489 | i.allowWidening = false; |
---|
490 | } |
---|
491 | |
---|
492 | // Find any unbound type variables |
---|
493 | resultEnv.extractOpenVars( openVars ); |
---|
494 | |
---|
495 | auto param = function->parameters.begin(); |
---|
496 | auto param_end = function->parameters.end(); |
---|
497 | |
---|
498 | // For every arguments of its set, check if it matches one of the parameter |
---|
499 | // The order is important |
---|
500 | for( auto & arg : argsList ) { |
---|
501 | |
---|
502 | // Ignore non-mutex arguments |
---|
503 | if( !advance_to_mutex( param, param_end ) ) { |
---|
504 | // We ran out of parameters but still have arguments |
---|
505 | // this function doesn't match |
---|
506 | throw SemanticError("candidate function not viable: too many mutex arguments\n", function); |
---|
507 | } |
---|
508 | |
---|
509 | // Check if the argument matches the parameter type in the current scope |
---|
510 | if( ! unify( (*param)->get_type(), arg.expr->get_result(), resultEnv, resultNeed, resultHave, openVars, this->indexer ) ) { |
---|
511 | // Type doesn't match |
---|
512 | stringstream ss; |
---|
513 | ss << "candidate function not viable: no known convertion from '"; |
---|
514 | arg.expr->get_result()->print( ss ); |
---|
515 | ss << "' to '"; |
---|
516 | (*param)->get_type()->print( ss ); |
---|
517 | ss << "'\n"; |
---|
518 | throw SemanticError(ss.str(), function); |
---|
519 | } |
---|
520 | |
---|
521 | param++; |
---|
522 | } |
---|
523 | |
---|
524 | // All arguments match ! |
---|
525 | |
---|
526 | // Check if parameters are missing |
---|
527 | if( advance_to_mutex( param, param_end ) ) { |
---|
528 | // We ran out of arguments but still have parameters left |
---|
529 | // this function doesn't match |
---|
530 | throw SemanticError("candidate function not viable: too few mutex arguments\n", function); |
---|
531 | } |
---|
532 | |
---|
533 | // All parameters match ! |
---|
534 | |
---|
535 | // Finish the expressions to tie in the proper environments |
---|
536 | finishExpr( newFunc.expr, resultEnv ); |
---|
537 | for( Alternative & alt : argsList ) { |
---|
538 | finishExpr( alt.expr, resultEnv ); |
---|
539 | } |
---|
540 | |
---|
541 | // This is a match store it and save it for later |
---|
542 | func_candidates.push_back( newFunc ); |
---|
543 | args_candidates.push_back( argsList ); |
---|
544 | |
---|
545 | } |
---|
546 | catch( SemanticError &e ) { |
---|
547 | errors.append( e ); |
---|
548 | } |
---|
549 | } |
---|
550 | } |
---|
551 | catch( SemanticError &e ) { |
---|
552 | errors.append( e ); |
---|
553 | } |
---|
554 | } |
---|
555 | |
---|
556 | // Make sure we got the right number of arguments |
---|
557 | if( func_candidates.empty() ) { SemanticError top( "No alternatives for function in call to waitfor" ); top.append( errors ); throw top; } |
---|
558 | if( args_candidates.empty() ) { SemanticError top( "No alternatives for arguments in call to waitfor" ); top.append( errors ); throw top; } |
---|
559 | if( func_candidates.size() > 1 ) { SemanticError top( "Ambiguous function in call to waitfor" ); top.append( errors ); throw top; } |
---|
560 | if( args_candidates.size() > 1 ) { SemanticError top( "Ambiguous arguments in call to waitfor" ); top.append( errors ); throw top; } |
---|
561 | |
---|
562 | |
---|
563 | // Swap the results from the alternative with the unresolved values. |
---|
564 | // Alternatives will handle deletion on destruction |
---|
565 | std::swap( clause.target.function, func_candidates.front().expr ); |
---|
566 | for( auto arg_pair : group_iterate( clause.target.arguments, args_candidates.front() ) ) { |
---|
567 | std::swap ( std::get<0>( arg_pair), std::get<1>( arg_pair).expr ); |
---|
568 | } |
---|
569 | |
---|
570 | // Resolve the conditions as if it were an IfStmt |
---|
571 | // Resolve the statments normally |
---|
572 | findSingleExpression( clause.condition, this->indexer ); |
---|
573 | clause.statement->accept( *visitor ); |
---|
574 | } |
---|
575 | |
---|
576 | |
---|
577 | if( stmt->timeout.statement ) { |
---|
578 | // Resolve the timeout as an size_t for now |
---|
579 | // Resolve the conditions as if it were an IfStmt |
---|
580 | // Resolve the statments normally |
---|
581 | findSingleExpression( stmt->timeout.time, new BasicType( noQualifiers, BasicType::LongLongUnsignedInt ), this->indexer ); |
---|
582 | findSingleExpression( stmt->timeout.condition, this->indexer ); |
---|
583 | stmt->timeout.statement->accept( *visitor ); |
---|
584 | } |
---|
585 | |
---|
586 | if( stmt->orelse.statement ) { |
---|
587 | // Resolve the conditions as if it were an IfStmt |
---|
588 | // Resolve the statments normally |
---|
589 | findSingleExpression( stmt->orelse.condition, this->indexer ); |
---|
590 | stmt->orelse.statement->accept( *visitor ); |
---|
591 | } |
---|
592 | } |
---|
593 | |
---|
594 | bool isStructOrUnion( Type * t ) { |
---|
595 | t = t->stripReferences(); |
---|
596 | return dynamic_cast< StructInstType * >( t ) || dynamic_cast< UnionInstType * >( t ); |
---|
597 | } |
---|
598 | |
---|
599 | void Resolver::resolveWithExprs( std::list< Expression * > & withExprs, std::list< Statement * > & newStmts ) { |
---|
600 | for ( Expression *& expr : withExprs ) { |
---|
601 | // only struct- and union-typed expressions are viable candidates |
---|
602 | findKindExpression( expr, indexer, "with statement", isStructOrUnion ); |
---|
603 | |
---|
604 | // if with expression might be impure, create a temporary so that it is evaluated once |
---|
605 | if ( Tuples::maybeImpure( expr ) ) { |
---|
606 | static UniqueName tmpNamer( "_with_tmp_" ); |
---|
607 | ObjectDecl * tmp = ObjectDecl::newObject( tmpNamer.newName(), expr->result->clone(), new SingleInit( expr ) ); |
---|
608 | expr = new VariableExpr( tmp ); |
---|
609 | newStmts.push_back( new DeclStmt( tmp ) ); |
---|
610 | if ( InitTweak::isConstructable( tmp->type ) ) { |
---|
611 | // generate ctor/dtor and resolve them |
---|
612 | tmp->init = InitTweak::genCtorInit( tmp ); |
---|
613 | tmp->accept( *visitor ); |
---|
614 | } |
---|
615 | } |
---|
616 | } |
---|
617 | } |
---|
618 | |
---|
619 | void Resolver::previsit( WithStmt * withStmt ) { |
---|
620 | resolveWithExprs( withStmt->exprs, stmtsToAddBefore ); |
---|
621 | } |
---|
622 | |
---|
623 | template< typename T > |
---|
624 | bool isCharType( T t ) { |
---|
625 | if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) { |
---|
626 | return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar || |
---|
627 | bt->get_kind() == BasicType::UnsignedChar; |
---|
628 | } |
---|
629 | return false; |
---|
630 | } |
---|
631 | |
---|
632 | void Resolver::previsit( SingleInit *singleInit ) { |
---|
633 | visit_children = false; |
---|
634 | // resolve initialization using the possibilities as determined by the currentObject cursor |
---|
635 | Expression * newExpr = new UntypedInitExpr( singleInit->value, currentObject.getOptions() ); |
---|
636 | findSingleExpression( newExpr, indexer ); |
---|
637 | InitExpr * initExpr = strict_dynamic_cast< InitExpr * >( newExpr ); |
---|
638 | |
---|
639 | // move cursor to the object that is actually initialized |
---|
640 | currentObject.setNext( initExpr->get_designation() ); |
---|
641 | |
---|
642 | // discard InitExpr wrapper and retain relevant pieces |
---|
643 | newExpr = initExpr->expr; |
---|
644 | initExpr->expr = nullptr; |
---|
645 | std::swap( initExpr->env, newExpr->env ); |
---|
646 | std::swap( initExpr->inferParams, newExpr->inferParams ) ; |
---|
647 | delete initExpr; |
---|
648 | |
---|
649 | // get the actual object's type (may not exactly match what comes back from the resolver due to conversions) |
---|
650 | Type * initContext = currentObject.getCurrentType(); |
---|
651 | |
---|
652 | removeExtraneousCast( newExpr, indexer ); |
---|
653 | |
---|
654 | // check if actual object's type is char[] |
---|
655 | if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) { |
---|
656 | if ( isCharType( at->get_base() ) ) { |
---|
657 | // check if the resolved type is char * |
---|
658 | if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_result() ) ) { |
---|
659 | if ( isCharType( pt->get_base() ) ) { |
---|
660 | if ( CastExpr *ce = dynamic_cast< CastExpr * >( newExpr ) ) { |
---|
661 | // strip cast if we're initializing a char[] with a char *, e.g. char x[] = "hello"; |
---|
662 | newExpr = ce->get_arg(); |
---|
663 | ce->set_arg( nullptr ); |
---|
664 | std::swap( ce->env, newExpr->env ); |
---|
665 | delete ce; |
---|
666 | } |
---|
667 | } |
---|
668 | } |
---|
669 | } |
---|
670 | } |
---|
671 | |
---|
672 | // set initializer expr to resolved express |
---|
673 | singleInit->value = newExpr; |
---|
674 | |
---|
675 | // move cursor to next object in preparation for next initializer |
---|
676 | currentObject.increment(); |
---|
677 | } |
---|
678 | |
---|
679 | void Resolver::previsit( ListInit * listInit ) { |
---|
680 | visit_children = false; |
---|
681 | // move cursor into brace-enclosed initializer-list |
---|
682 | currentObject.enterListInit(); |
---|
683 | // xxx - fix this so that the list isn't copied, iterator should be used to change current element |
---|
684 | std::list<Designation *> newDesignations; |
---|
685 | for ( auto p : group_iterate(listInit->get_designations(), listInit->get_initializers()) ) { |
---|
686 | // iterate designations and initializers in pairs, moving the cursor to the current designated object and resolving |
---|
687 | // the initializer against that object. |
---|
688 | Designation * des = std::get<0>(p); |
---|
689 | Initializer * init = std::get<1>(p); |
---|
690 | newDesignations.push_back( currentObject.findNext( des ) ); |
---|
691 | init->accept( *visitor ); |
---|
692 | } |
---|
693 | // set the set of 'resolved' designations and leave the brace-enclosed initializer-list |
---|
694 | listInit->get_designations() = newDesignations; // xxx - memory management |
---|
695 | currentObject.exitListInit(); |
---|
696 | |
---|
697 | // xxx - this part has not be folded into CurrentObject yet |
---|
698 | // } else if ( TypeInstType * tt = dynamic_cast< TypeInstType * >( initContext ) ) { |
---|
699 | // Type * base = tt->get_baseType()->get_base(); |
---|
700 | // if ( base ) { |
---|
701 | // // know the implementation type, so try using that as the initContext |
---|
702 | // ObjectDecl tmpObj( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, base->clone(), nullptr ); |
---|
703 | // currentObject = &tmpObj; |
---|
704 | // visit( listInit ); |
---|
705 | // } else { |
---|
706 | // // missing implementation type -- might be an unknown type variable, so try proceeding with the current init context |
---|
707 | // Parent::visit( listInit ); |
---|
708 | // } |
---|
709 | // } else { |
---|
710 | } |
---|
711 | |
---|
712 | // ConstructorInit - fall back on C-style initializer |
---|
713 | void Resolver::fallbackInit( ConstructorInit * ctorInit ) { |
---|
714 | // could not find valid constructor, or found an intrinsic constructor |
---|
715 | // fall back on C-style initializer |
---|
716 | delete ctorInit->get_ctor(); |
---|
717 | ctorInit->set_ctor( NULL ); |
---|
718 | delete ctorInit->get_dtor(); |
---|
719 | ctorInit->set_dtor( NULL ); |
---|
720 | maybeAccept( ctorInit->get_init(), *visitor ); |
---|
721 | } |
---|
722 | |
---|
723 | // needs to be callable from outside the resolver, so this is a standalone function |
---|
724 | void resolveCtorInit( ConstructorInit * ctorInit, const SymTab::Indexer & indexer ) { |
---|
725 | assert( ctorInit ); |
---|
726 | PassVisitor<Resolver> resolver( indexer ); |
---|
727 | ctorInit->accept( resolver ); |
---|
728 | } |
---|
729 | |
---|
730 | void resolveStmtExpr( StmtExpr * stmtExpr, const SymTab::Indexer & indexer ) { |
---|
731 | assert( stmtExpr ); |
---|
732 | PassVisitor<Resolver> resolver( indexer ); |
---|
733 | stmtExpr->accept( resolver ); |
---|
734 | stmtExpr->computeResult(); |
---|
735 | } |
---|
736 | |
---|
737 | void Resolver::previsit( ConstructorInit *ctorInit ) { |
---|
738 | visit_children = false; |
---|
739 | // xxx - fallback init has been removed => remove fallbackInit function and remove complexity from FixInit and remove C-init from ConstructorInit |
---|
740 | maybeAccept( ctorInit->get_ctor(), *visitor ); |
---|
741 | maybeAccept( ctorInit->get_dtor(), *visitor ); |
---|
742 | |
---|
743 | // found a constructor - can get rid of C-style initializer |
---|
744 | delete ctorInit->get_init(); |
---|
745 | ctorInit->set_init( NULL ); |
---|
746 | |
---|
747 | // intrinsic single parameter constructors and destructors do nothing. Since this was |
---|
748 | // implicitly generated, there's no way for it to have side effects, so get rid of it |
---|
749 | // to clean up generated code. |
---|
750 | if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->get_ctor() ) ) { |
---|
751 | delete ctorInit->get_ctor(); |
---|
752 | ctorInit->set_ctor( NULL ); |
---|
753 | } |
---|
754 | |
---|
755 | if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->get_dtor() ) ) { |
---|
756 | delete ctorInit->get_dtor(); |
---|
757 | ctorInit->set_dtor( NULL ); |
---|
758 | } |
---|
759 | |
---|
760 | // xxx - todo -- what about arrays? |
---|
761 | // if ( dtor == NULL && InitTweak::isIntrinsicCallStmt( ctorInit->get_ctor() ) ) { |
---|
762 | // // can reduce the constructor down to a SingleInit using the |
---|
763 | // // second argument from the ctor call, since |
---|
764 | // delete ctorInit->get_ctor(); |
---|
765 | // ctorInit->set_ctor( NULL ); |
---|
766 | |
---|
767 | // Expression * arg = |
---|
768 | // ctorInit->set_init( new SingleInit( arg ) ); |
---|
769 | // } |
---|
770 | } |
---|
771 | } // namespace ResolvExpr |
---|
772 | |
---|
773 | // Local Variables: // |
---|
774 | // tab-width: 4 // |
---|
775 | // mode: c++ // |
---|
776 | // compile-command: "make install" // |
---|
777 | // End: // |
---|