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 | // AlternativeFinder.cc -- |
---|
8 | // |
---|
9 | // Author : Richard C. Bilson |
---|
10 | // Created On : Sat May 16 23:52:08 2015 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Mon Jul 4 17:02:51 2016 |
---|
13 | // Update Count : 29 |
---|
14 | // |
---|
15 | |
---|
16 | #include <list> |
---|
17 | #include <iterator> |
---|
18 | #include <algorithm> |
---|
19 | #include <functional> |
---|
20 | #include <cassert> |
---|
21 | #include <unordered_map> |
---|
22 | #include <utility> |
---|
23 | #include <vector> |
---|
24 | |
---|
25 | #include "AlternativeFinder.h" |
---|
26 | #include "Alternative.h" |
---|
27 | #include "Cost.h" |
---|
28 | #include "typeops.h" |
---|
29 | #include "Unify.h" |
---|
30 | #include "RenameVars.h" |
---|
31 | #include "SynTree/Type.h" |
---|
32 | #include "SynTree/Declaration.h" |
---|
33 | #include "SynTree/Expression.h" |
---|
34 | #include "SynTree/Initializer.h" |
---|
35 | #include "SynTree/Visitor.h" |
---|
36 | #include "SymTab/Indexer.h" |
---|
37 | #include "SymTab/Mangler.h" |
---|
38 | #include "SynTree/TypeSubstitution.h" |
---|
39 | #include "SymTab/Validate.h" |
---|
40 | #include "Tuples/Tuples.h" |
---|
41 | #include "Common/utility.h" |
---|
42 | #include "InitTweak/InitTweak.h" |
---|
43 | #include "ResolveTypeof.h" |
---|
44 | |
---|
45 | extern bool resolvep; |
---|
46 | #define PRINT( text ) if ( resolvep ) { text } |
---|
47 | //#define DEBUG_COST |
---|
48 | |
---|
49 | namespace ResolvExpr { |
---|
50 | Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer, TypeEnvironment &env ) { |
---|
51 | CastExpr *castToVoid = new CastExpr( expr ); |
---|
52 | |
---|
53 | AlternativeFinder finder( indexer, env ); |
---|
54 | finder.findWithAdjustment( castToVoid ); |
---|
55 | |
---|
56 | // it's a property of the language that a cast expression has either 1 or 0 interpretations; if it has 0 |
---|
57 | // interpretations, an exception has already been thrown. |
---|
58 | assert( finder.get_alternatives().size() == 1 ); |
---|
59 | CastExpr *newExpr = dynamic_cast< CastExpr* >( finder.get_alternatives().front().expr ); |
---|
60 | assert( newExpr ); |
---|
61 | env = finder.get_alternatives().front().env; |
---|
62 | return newExpr->get_arg()->clone(); |
---|
63 | } |
---|
64 | |
---|
65 | Cost sumCost( const AltList &in ) { |
---|
66 | Cost total; |
---|
67 | for ( AltList::const_iterator i = in.begin(); i != in.end(); ++i ) { |
---|
68 | total += i->cost; |
---|
69 | } |
---|
70 | return total; |
---|
71 | } |
---|
72 | |
---|
73 | namespace { |
---|
74 | void printAlts( const AltList &list, std::ostream &os, int indent = 0 ) { |
---|
75 | for ( AltList::const_iterator i = list.begin(); i != list.end(); ++i ) { |
---|
76 | i->print( os, indent ); |
---|
77 | os << std::endl; |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | void makeExprList( const AltList &in, std::list< Expression* > &out ) { |
---|
82 | for ( AltList::const_iterator i = in.begin(); i != in.end(); ++i ) { |
---|
83 | out.push_back( i->expr->clone() ); |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | struct PruneStruct { |
---|
88 | bool isAmbiguous; |
---|
89 | AltList::iterator candidate; |
---|
90 | PruneStruct() {} |
---|
91 | PruneStruct( AltList::iterator candidate ): isAmbiguous( false ), candidate( candidate ) {} |
---|
92 | }; |
---|
93 | |
---|
94 | /// Prunes a list of alternatives down to those that have the minimum conversion cost for a given return type; skips ambiguous interpretations |
---|
95 | template< typename InputIterator, typename OutputIterator > |
---|
96 | void pruneAlternatives( InputIterator begin, InputIterator end, OutputIterator out, const SymTab::Indexer &indexer ) { |
---|
97 | // select the alternatives that have the minimum conversion cost for a particular set of result types |
---|
98 | std::map< std::string, PruneStruct > selected; |
---|
99 | for ( AltList::iterator candidate = begin; candidate != end; ++candidate ) { |
---|
100 | PruneStruct current( candidate ); |
---|
101 | std::string mangleName; |
---|
102 | { |
---|
103 | Type * newType = candidate->expr->get_result()->clone(); |
---|
104 | candidate->env.apply( newType ); |
---|
105 | mangleName = SymTab::Mangler::mangle( newType ); |
---|
106 | delete newType; |
---|
107 | } |
---|
108 | std::map< std::string, PruneStruct >::iterator mapPlace = selected.find( mangleName ); |
---|
109 | if ( mapPlace != selected.end() ) { |
---|
110 | if ( candidate->cost < mapPlace->second.candidate->cost ) { |
---|
111 | PRINT( |
---|
112 | std::cerr << "cost " << candidate->cost << " beats " << mapPlace->second.candidate->cost << std::endl; |
---|
113 | ) |
---|
114 | selected[ mangleName ] = current; |
---|
115 | } else if ( candidate->cost == mapPlace->second.candidate->cost ) { |
---|
116 | PRINT( |
---|
117 | std::cerr << "marking ambiguous" << std::endl; |
---|
118 | ) |
---|
119 | mapPlace->second.isAmbiguous = true; |
---|
120 | } |
---|
121 | } else { |
---|
122 | selected[ mangleName ] = current; |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | PRINT( |
---|
127 | std::cerr << "there are " << selected.size() << " alternatives before elimination" << std::endl; |
---|
128 | ) |
---|
129 | |
---|
130 | // accept the alternatives that were unambiguous |
---|
131 | for ( std::map< std::string, PruneStruct >::iterator target = selected.begin(); target != selected.end(); ++target ) { |
---|
132 | if ( ! target->second.isAmbiguous ) { |
---|
133 | Alternative &alt = *target->second.candidate; |
---|
134 | alt.env.applyFree( alt.expr->get_result() ); |
---|
135 | *out++ = alt; |
---|
136 | } |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | template< typename InputIterator > |
---|
141 | void simpleCombineEnvironments( InputIterator begin, InputIterator end, TypeEnvironment &result ) { |
---|
142 | while ( begin != end ) { |
---|
143 | result.simpleCombine( (*begin++).env ); |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | void renameTypes( Expression *expr ) { |
---|
148 | expr->get_result()->accept( global_renamer ); |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|
152 | template< typename InputIterator, typename OutputIterator > |
---|
153 | void AlternativeFinder::findSubExprs( InputIterator begin, InputIterator end, OutputIterator out ) { |
---|
154 | while ( begin != end ) { |
---|
155 | AlternativeFinder finder( indexer, env ); |
---|
156 | finder.findWithAdjustment( *begin ); |
---|
157 | // XXX either this |
---|
158 | //Designators::fixDesignations( finder, (*begin++)->get_argName() ); |
---|
159 | // or XXX this |
---|
160 | begin++; |
---|
161 | PRINT( |
---|
162 | std::cerr << "findSubExprs" << std::endl; |
---|
163 | printAlts( finder.alternatives, std::cerr ); |
---|
164 | ) |
---|
165 | *out++ = finder; |
---|
166 | } |
---|
167 | } |
---|
168 | |
---|
169 | AlternativeFinder::AlternativeFinder( const SymTab::Indexer &indexer, const TypeEnvironment &env ) |
---|
170 | : indexer( indexer ), env( env ) { |
---|
171 | } |
---|
172 | |
---|
173 | void AlternativeFinder::find( Expression *expr, bool adjust, bool prune ) { |
---|
174 | expr->accept( *this ); |
---|
175 | if ( alternatives.empty() ) { |
---|
176 | throw SemanticError( "No reasonable alternatives for expression ", expr ); |
---|
177 | } |
---|
178 | for ( AltList::iterator i = alternatives.begin(); i != alternatives.end(); ++i ) { |
---|
179 | if ( adjust ) { |
---|
180 | adjustExprType( i->expr->get_result(), i->env, indexer ); |
---|
181 | } |
---|
182 | } |
---|
183 | if ( prune ) { |
---|
184 | PRINT( |
---|
185 | std::cerr << "alternatives before prune:" << std::endl; |
---|
186 | printAlts( alternatives, std::cerr ); |
---|
187 | ) |
---|
188 | AltList::iterator oldBegin = alternatives.begin(); |
---|
189 | pruneAlternatives( alternatives.begin(), alternatives.end(), front_inserter( alternatives ), indexer ); |
---|
190 | if ( alternatives.begin() == oldBegin ) { |
---|
191 | std::ostringstream stream; |
---|
192 | stream << "Can't choose between alternatives for expression "; |
---|
193 | expr->print( stream ); |
---|
194 | stream << "Alternatives are:"; |
---|
195 | AltList winners; |
---|
196 | findMinCost( alternatives.begin(), alternatives.end(), back_inserter( winners ) ); |
---|
197 | printAlts( winners, stream, 8 ); |
---|
198 | throw SemanticError( stream.str() ); |
---|
199 | } |
---|
200 | alternatives.erase( oldBegin, alternatives.end() ); |
---|
201 | PRINT( |
---|
202 | std::cerr << "there are " << alternatives.size() << " alternatives after elimination" << std::endl; |
---|
203 | ) |
---|
204 | } |
---|
205 | |
---|
206 | // Central location to handle gcc extension keyword for all expression types. |
---|
207 | for ( Alternative &iter: alternatives ) { |
---|
208 | iter.expr->set_extension( expr->get_extension() ); |
---|
209 | } // for |
---|
210 | } |
---|
211 | |
---|
212 | void AlternativeFinder::findWithAdjustment( Expression *expr, bool prune ) { |
---|
213 | find( expr, true, prune ); |
---|
214 | } |
---|
215 | |
---|
216 | template< typename StructOrUnionType > |
---|
217 | void AlternativeFinder::addAggMembers( StructOrUnionType *aggInst, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member ) { |
---|
218 | |
---|
219 | // // member must be either a tuple expression or a name expr |
---|
220 | // if ( NameExpr * nameExpr = dynamic_cast< NameExpr * >( memberExpr->get_member() ) ) { |
---|
221 | // addAggMembers( structInst, agg->expr, agg->cost, nameExpr->get_name() ); |
---|
222 | // } else { |
---|
223 | // TupleExpr * tupleExpr = safe_dynamic_cast< TupleExpr * >( memberExpr->get_member() ); |
---|
224 | // // xxx - ... |
---|
225 | // assert( false ); |
---|
226 | // } |
---|
227 | // if ( TupleExpr * tupleExpr = dynamic_cast< TupleExpr * >( memberExpr->get_member() ) ) { |
---|
228 | |
---|
229 | // } |
---|
230 | NameExpr * nameExpr = safe_dynamic_cast< NameExpr * >( member ); |
---|
231 | const std::string & name = nameExpr->get_name(); |
---|
232 | std::list< Declaration* > members; |
---|
233 | aggInst->lookup( name, members ); |
---|
234 | for ( std::list< Declaration* >::const_iterator i = members.begin(); i != members.end(); ++i ) { |
---|
235 | if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType* >( *i ) ) { |
---|
236 | alternatives.push_back( Alternative( new MemberExpr( dwt, expr->clone() ), env, newCost ) ); |
---|
237 | renameTypes( alternatives.back().expr ); |
---|
238 | } else { |
---|
239 | assert( false ); |
---|
240 | } |
---|
241 | } |
---|
242 | } |
---|
243 | |
---|
244 | void AlternativeFinder::visit( ApplicationExpr *applicationExpr ) { |
---|
245 | alternatives.push_back( Alternative( applicationExpr->clone(), env, Cost::zero ) ); |
---|
246 | } |
---|
247 | |
---|
248 | Cost computeConversionCost( Alternative &alt, const SymTab::Indexer &indexer ) { |
---|
249 | ApplicationExpr *appExpr = safe_dynamic_cast< ApplicationExpr* >( alt.expr ); |
---|
250 | PointerType *pointer = safe_dynamic_cast< PointerType* >( appExpr->get_function()->get_result() ); |
---|
251 | FunctionType *function = safe_dynamic_cast< FunctionType* >( pointer->get_base() ); |
---|
252 | |
---|
253 | Cost convCost( 0, 0, 0 ); |
---|
254 | std::list< DeclarationWithType* >& formals = function->get_parameters(); |
---|
255 | std::list< DeclarationWithType* >::iterator formal = formals.begin(); |
---|
256 | std::list< Expression* >& actuals = appExpr->get_args(); |
---|
257 | |
---|
258 | std::list< Type * > formalTypes; |
---|
259 | std::list< Type * >::iterator formalType = formalTypes.end(); |
---|
260 | |
---|
261 | for ( std::list< Expression* >::iterator actualExpr = actuals.begin(); actualExpr != actuals.end(); ++actualExpr ) { |
---|
262 | |
---|
263 | PRINT( |
---|
264 | std::cerr << "actual expression:" << std::endl; |
---|
265 | (*actualExpr)->print( std::cerr, 8 ); |
---|
266 | std::cerr << "--- results are" << std::endl; |
---|
267 | (*actualExpr)->get_result()->print( std::cerr, 8 ); |
---|
268 | ) |
---|
269 | std::list< DeclarationWithType* >::iterator startFormal = formal; |
---|
270 | Cost actualCost; |
---|
271 | std::list< Type * > flatActualTypes; |
---|
272 | flatten( (*actualExpr)->get_result(), back_inserter( flatActualTypes ) ); |
---|
273 | for ( std::list< Type* >::iterator actualType = flatActualTypes.begin(); actualType != flatActualTypes.end(); ++actualType ) { |
---|
274 | |
---|
275 | |
---|
276 | // tuple handling code |
---|
277 | if ( formalType == formalTypes.end() ) { |
---|
278 | // the type of the formal parameter may be a tuple type. To make this easier to work with, |
---|
279 | // flatten the tuple type and traverse the resulting list of types, incrementing the formal |
---|
280 | // iterator once its types have been extracted. Once a particular formal parameter's type has |
---|
281 | // been exhausted load the next formal parameter's type. |
---|
282 | if ( formal == formals.end() ) { |
---|
283 | if ( function->get_isVarArgs() ) { |
---|
284 | convCost += Cost( 1, 0, 0 ); |
---|
285 | break; |
---|
286 | } else { |
---|
287 | return Cost::infinity; |
---|
288 | } |
---|
289 | } |
---|
290 | formalTypes.clear(); |
---|
291 | flatten( (*formal)->get_type(), back_inserter( formalTypes ) ); |
---|
292 | formalType = formalTypes.begin(); |
---|
293 | ++formal; |
---|
294 | } |
---|
295 | |
---|
296 | PRINT( |
---|
297 | std::cerr << std::endl << "converting "; |
---|
298 | (*actualType)->print( std::cerr, 8 ); |
---|
299 | std::cerr << std::endl << " to "; |
---|
300 | (*formal)->get_type()->print( std::cerr, 8 ); |
---|
301 | ) |
---|
302 | Cost newCost = conversionCost( *actualType, *formalType, indexer, alt.env ); |
---|
303 | PRINT( |
---|
304 | std::cerr << std::endl << "cost is" << newCost << std::endl; |
---|
305 | ) |
---|
306 | |
---|
307 | if ( newCost == Cost::infinity ) { |
---|
308 | return newCost; |
---|
309 | } |
---|
310 | convCost += newCost; |
---|
311 | actualCost += newCost; |
---|
312 | |
---|
313 | convCost += Cost( 0, polyCost( *formalType, alt.env, indexer ) + polyCost( *actualType, alt.env, indexer ), 0 ); |
---|
314 | |
---|
315 | formalType++; |
---|
316 | } |
---|
317 | if ( actualCost != Cost( 0, 0, 0 ) ) { |
---|
318 | std::list< DeclarationWithType* >::iterator startFormalPlusOne = startFormal; |
---|
319 | startFormalPlusOne++; |
---|
320 | if ( formal == startFormalPlusOne ) { |
---|
321 | // not a tuple type |
---|
322 | Type *newType = (*startFormal)->get_type()->clone(); |
---|
323 | alt.env.apply( newType ); |
---|
324 | *actualExpr = new CastExpr( *actualExpr, newType ); |
---|
325 | } else { |
---|
326 | TupleType *newType = new TupleType( Type::Qualifiers() ); |
---|
327 | for ( std::list< DeclarationWithType* >::iterator i = startFormal; i != formal; ++i ) { |
---|
328 | newType->get_types().push_back( (*i)->get_type()->clone() ); |
---|
329 | } |
---|
330 | alt.env.apply( newType ); |
---|
331 | *actualExpr = new CastExpr( *actualExpr, newType ); |
---|
332 | } |
---|
333 | } |
---|
334 | |
---|
335 | } |
---|
336 | if ( formal != formals.end() ) { |
---|
337 | return Cost::infinity; |
---|
338 | } |
---|
339 | |
---|
340 | for ( InferredParams::const_iterator assert = appExpr->get_inferParams().begin(); assert != appExpr->get_inferParams().end(); ++assert ) { |
---|
341 | PRINT( |
---|
342 | std::cerr << std::endl << "converting "; |
---|
343 | assert->second.actualType->print( std::cerr, 8 ); |
---|
344 | std::cerr << std::endl << " to "; |
---|
345 | assert->second.formalType->print( std::cerr, 8 ); |
---|
346 | ) |
---|
347 | Cost newCost = conversionCost( assert->second.actualType, assert->second.formalType, indexer, alt.env ); |
---|
348 | PRINT( |
---|
349 | std::cerr << std::endl << "cost of conversion is " << newCost << std::endl; |
---|
350 | ) |
---|
351 | if ( newCost == Cost::infinity ) { |
---|
352 | return newCost; |
---|
353 | } |
---|
354 | convCost += newCost; |
---|
355 | |
---|
356 | convCost += Cost( 0, polyCost( assert->second.formalType, alt.env, indexer ) + polyCost( assert->second.actualType, alt.env, indexer ), 0 ); |
---|
357 | } |
---|
358 | |
---|
359 | return convCost; |
---|
360 | } |
---|
361 | |
---|
362 | /// Adds type variables to the open variable set and marks their assertions |
---|
363 | void makeUnifiableVars( Type *type, OpenVarSet &unifiableVars, AssertionSet &needAssertions ) { |
---|
364 | for ( Type::ForallList::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) { |
---|
365 | unifiableVars[ (*tyvar)->get_name() ] = (*tyvar)->get_kind(); |
---|
366 | for ( std::list< DeclarationWithType* >::iterator assert = (*tyvar)->get_assertions().begin(); assert != (*tyvar)->get_assertions().end(); ++assert ) { |
---|
367 | needAssertions[ *assert ] = true; |
---|
368 | } |
---|
369 | /// needAssertions.insert( needAssertions.end(), (*tyvar)->get_assertions().begin(), (*tyvar)->get_assertions().end() ); |
---|
370 | } |
---|
371 | } |
---|
372 | |
---|
373 | bool AlternativeFinder::instantiateFunction( std::list< DeclarationWithType* >& formals, /*const*/ AltList &actuals, bool isVarArgs, OpenVarSet& openVars, TypeEnvironment &resultEnv, AssertionSet &resultNeed, AssertionSet &resultHave ) { |
---|
374 | simpleCombineEnvironments( actuals.begin(), actuals.end(), resultEnv ); |
---|
375 | // make sure we don't widen any existing bindings |
---|
376 | for ( TypeEnvironment::iterator i = resultEnv.begin(); i != resultEnv.end(); ++i ) { |
---|
377 | i->allowWidening = false; |
---|
378 | } |
---|
379 | resultEnv.extractOpenVars( openVars ); |
---|
380 | |
---|
381 | std::list< DeclarationWithType* >::iterator formal = formals.begin(); |
---|
382 | |
---|
383 | AltList newActuals; |
---|
384 | explode( actuals, back_inserter( newActuals ) ); |
---|
385 | |
---|
386 | std::list< Type * > formalTypes; |
---|
387 | std::list< Type * >::iterator formalType = formalTypes.end(); |
---|
388 | |
---|
389 | for ( AltList::const_iterator actualExpr = actuals.begin(); actualExpr != actuals.end(); ++actualExpr ) { |
---|
390 | std::list< Type * > flatActualTypes; |
---|
391 | flatten( actualExpr->expr->get_result(), back_inserter( flatActualTypes ) ); |
---|
392 | for ( std::list< Type* >::iterator actualType = flatActualTypes.begin(); actualType != flatActualTypes.end(); ++actualType, ++formalType ) { |
---|
393 | if ( formalType == formalTypes.end() ) { |
---|
394 | // the type of the formal parameter may be a tuple type. To make this easier to work with, |
---|
395 | // flatten the tuple type and traverse the resulting list of types, incrementing the formal |
---|
396 | // iterator once its types have been extracted. Once a particular formal parameter's type has |
---|
397 | // been exhausted load the next formal parameter's type. |
---|
398 | if ( formal == formals.end() ) { |
---|
399 | return isVarArgs; |
---|
400 | } |
---|
401 | formalTypes.clear(); |
---|
402 | flatten( (*formal)->get_type(), back_inserter( formalTypes ) ); |
---|
403 | formalType = formalTypes.begin(); |
---|
404 | ++formal; |
---|
405 | } |
---|
406 | PRINT( |
---|
407 | std::cerr << "formal type is "; |
---|
408 | (*formalType)->print( std::cerr ); |
---|
409 | std::cerr << std::endl << "actual type is "; |
---|
410 | (*actualType)->print( std::cerr ); |
---|
411 | std::cerr << std::endl; |
---|
412 | ) |
---|
413 | if ( ! unify( *formalType, *actualType, resultEnv, resultNeed, resultHave, openVars, indexer ) ) { |
---|
414 | return false; |
---|
415 | } |
---|
416 | } |
---|
417 | } |
---|
418 | |
---|
419 | // xxx - a tuple type was not completely matched |
---|
420 | // partially handle the tuple with default arguments?? |
---|
421 | if ( formalType != formalTypes.end() ) return false; |
---|
422 | |
---|
423 | // Handling of default values |
---|
424 | while ( formal != formals.end() ) { |
---|
425 | if ( ObjectDecl *od = dynamic_cast<ObjectDecl *>( *formal ) ) |
---|
426 | if ( SingleInit *si = dynamic_cast<SingleInit *>( od->get_init() )) |
---|
427 | // so far, only constant expressions are accepted as default values |
---|
428 | if ( ConstantExpr *cnstexpr = dynamic_cast<ConstantExpr *>( si->get_value()) ) |
---|
429 | if ( Constant *cnst = dynamic_cast<Constant *>( cnstexpr->get_constant() ) ) |
---|
430 | if ( unify( (*formal)->get_type(), cnst->get_type(), resultEnv, resultNeed, resultHave, openVars, indexer ) ) { |
---|
431 | // XXX Don't know if this is right |
---|
432 | actuals.push_back( Alternative( cnstexpr->clone(), env, Cost::zero ) ); |
---|
433 | formal++; |
---|
434 | if ( formal == formals.end()) break; |
---|
435 | } |
---|
436 | return false; |
---|
437 | } |
---|
438 | return true; |
---|
439 | } |
---|
440 | |
---|
441 | // /// Map of declaration uniqueIds (intended to be the assertions in an AssertionSet) to their parents and the number of times they've been included |
---|
442 | //typedef std::unordered_map< UniqueId, std::unordered_map< UniqueId, unsigned > > AssertionParentSet; |
---|
443 | |
---|
444 | static const int recursionLimit = /*10*/ 4; ///< Limit to depth of recursion satisfaction |
---|
445 | //static const unsigned recursionParentLimit = 1; ///< Limit to the number of times an assertion can recursively use itself |
---|
446 | |
---|
447 | void addToIndexer( AssertionSet &assertSet, SymTab::Indexer &indexer ) { |
---|
448 | for ( AssertionSet::iterator i = assertSet.begin(); i != assertSet.end(); ++i ) { |
---|
449 | if ( i->second == true ) { |
---|
450 | i->first->accept( indexer ); |
---|
451 | } |
---|
452 | } |
---|
453 | } |
---|
454 | |
---|
455 | template< typename ForwardIterator, typename OutputIterator > |
---|
456 | void inferRecursive( ForwardIterator begin, ForwardIterator end, const Alternative &newAlt, OpenVarSet &openVars, const SymTab::Indexer &decls, const AssertionSet &newNeed, /*const AssertionParentSet &needParents,*/ |
---|
457 | int level, const SymTab::Indexer &indexer, OutputIterator out ) { |
---|
458 | if ( begin == end ) { |
---|
459 | if ( newNeed.empty() ) { |
---|
460 | *out++ = newAlt; |
---|
461 | return; |
---|
462 | } else if ( level >= recursionLimit ) { |
---|
463 | throw SemanticError( "Too many recursive assertions" ); |
---|
464 | } else { |
---|
465 | AssertionSet newerNeed; |
---|
466 | PRINT( |
---|
467 | std::cerr << "recursing with new set:" << std::endl; |
---|
468 | printAssertionSet( newNeed, std::cerr, 8 ); |
---|
469 | ) |
---|
470 | inferRecursive( newNeed.begin(), newNeed.end(), newAlt, openVars, decls, newerNeed, /*needParents,*/ level+1, indexer, out ); |
---|
471 | return; |
---|
472 | } |
---|
473 | } |
---|
474 | |
---|
475 | ForwardIterator cur = begin++; |
---|
476 | if ( ! cur->second ) { |
---|
477 | inferRecursive( begin, end, newAlt, openVars, decls, newNeed, /*needParents,*/ level, indexer, out ); |
---|
478 | } |
---|
479 | DeclarationWithType *curDecl = cur->first; |
---|
480 | PRINT( |
---|
481 | std::cerr << "inferRecursive: assertion is "; |
---|
482 | curDecl->print( std::cerr ); |
---|
483 | std::cerr << std::endl; |
---|
484 | ) |
---|
485 | std::list< DeclarationWithType* > candidates; |
---|
486 | decls.lookupId( curDecl->get_name(), candidates ); |
---|
487 | /// if ( candidates.empty() ) { std::cerr << "no candidates!" << std::endl; } |
---|
488 | for ( std::list< DeclarationWithType* >::const_iterator candidate = candidates.begin(); candidate != candidates.end(); ++candidate ) { |
---|
489 | PRINT( |
---|
490 | std::cerr << "inferRecursive: candidate is "; |
---|
491 | (*candidate)->print( std::cerr ); |
---|
492 | std::cerr << std::endl; |
---|
493 | ) |
---|
494 | |
---|
495 | AssertionSet newHave, newerNeed( newNeed ); |
---|
496 | TypeEnvironment newEnv( newAlt.env ); |
---|
497 | OpenVarSet newOpenVars( openVars ); |
---|
498 | Type *adjType = (*candidate)->get_type()->clone(); |
---|
499 | adjustExprType( adjType, newEnv, indexer ); |
---|
500 | adjType->accept( global_renamer ); |
---|
501 | PRINT( |
---|
502 | std::cerr << "unifying "; |
---|
503 | curDecl->get_type()->print( std::cerr ); |
---|
504 | std::cerr << " with "; |
---|
505 | adjType->print( std::cerr ); |
---|
506 | std::cerr << std::endl; |
---|
507 | ) |
---|
508 | if ( unify( curDecl->get_type(), adjType, newEnv, newerNeed, newHave, newOpenVars, indexer ) ) { |
---|
509 | PRINT( |
---|
510 | std::cerr << "success!" << std::endl; |
---|
511 | ) |
---|
512 | SymTab::Indexer newDecls( decls ); |
---|
513 | addToIndexer( newHave, newDecls ); |
---|
514 | Alternative newerAlt( newAlt ); |
---|
515 | newerAlt.env = newEnv; |
---|
516 | assert( (*candidate)->get_uniqueId() ); |
---|
517 | DeclarationWithType *candDecl = static_cast< DeclarationWithType* >( Declaration::declFromId( (*candidate)->get_uniqueId() ) ); |
---|
518 | //AssertionParentSet newNeedParents( needParents ); |
---|
519 | // skip repeatingly-self-recursive assertion satisfaction |
---|
520 | // DOESN'T WORK: grandchild nodes conflict with their cousins |
---|
521 | //if ( newNeedParents[ curDecl->get_uniqueId() ][ candDecl->get_uniqueId() ]++ > recursionParentLimit ) continue; |
---|
522 | Expression *varExpr = new VariableExpr( candDecl ); |
---|
523 | delete varExpr->get_result(); |
---|
524 | varExpr->set_result( adjType->clone() ); |
---|
525 | PRINT( |
---|
526 | std::cerr << "satisfying assertion " << curDecl->get_uniqueId() << " "; |
---|
527 | curDecl->print( std::cerr ); |
---|
528 | std::cerr << " with declaration " << (*candidate)->get_uniqueId() << " "; |
---|
529 | (*candidate)->print( std::cerr ); |
---|
530 | std::cerr << std::endl; |
---|
531 | ) |
---|
532 | ApplicationExpr *appExpr = static_cast< ApplicationExpr* >( newerAlt.expr ); |
---|
533 | // XXX: this is a memory leak, but adjType can't be deleted because it might contain assertions |
---|
534 | appExpr->get_inferParams()[ curDecl->get_uniqueId() ] = ParamEntry( (*candidate)->get_uniqueId(), adjType->clone(), curDecl->get_type()->clone(), varExpr ); |
---|
535 | inferRecursive( begin, end, newerAlt, newOpenVars, newDecls, newerNeed, /*newNeedParents,*/ level, indexer, out ); |
---|
536 | } else { |
---|
537 | delete adjType; |
---|
538 | } |
---|
539 | } |
---|
540 | } |
---|
541 | |
---|
542 | template< typename OutputIterator > |
---|
543 | void AlternativeFinder::inferParameters( const AssertionSet &need, AssertionSet &have, const Alternative &newAlt, OpenVarSet &openVars, OutputIterator out ) { |
---|
544 | // PRINT( |
---|
545 | // std::cerr << "inferParameters: assertions needed are" << std::endl; |
---|
546 | // printAll( need, std::cerr, 8 ); |
---|
547 | // ) |
---|
548 | SymTab::Indexer decls( indexer ); |
---|
549 | PRINT( |
---|
550 | std::cerr << "============= original indexer" << std::endl; |
---|
551 | indexer.print( std::cerr ); |
---|
552 | std::cerr << "============= new indexer" << std::endl; |
---|
553 | decls.print( std::cerr ); |
---|
554 | ) |
---|
555 | addToIndexer( have, decls ); |
---|
556 | AssertionSet newNeed; |
---|
557 | //AssertionParentSet needParents; |
---|
558 | inferRecursive( need.begin(), need.end(), newAlt, openVars, decls, newNeed, /*needParents,*/ 0, indexer, out ); |
---|
559 | // PRINT( |
---|
560 | // std::cerr << "declaration 14 is "; |
---|
561 | // Declaration::declFromId |
---|
562 | // *out++ = newAlt; |
---|
563 | // ) |
---|
564 | } |
---|
565 | |
---|
566 | template< typename OutputIterator > |
---|
567 | void AlternativeFinder::makeFunctionAlternatives( const Alternative &func, FunctionType *funcType, AltList &actualAlt, OutputIterator out ) { |
---|
568 | OpenVarSet openVars; |
---|
569 | AssertionSet resultNeed, resultHave; |
---|
570 | TypeEnvironment resultEnv; |
---|
571 | makeUnifiableVars( funcType, openVars, resultNeed ); |
---|
572 | if ( instantiateFunction( funcType->get_parameters(), actualAlt, funcType->get_isVarArgs(), openVars, resultEnv, resultNeed, resultHave ) ) { |
---|
573 | ApplicationExpr *appExpr = new ApplicationExpr( func.expr->clone() ); |
---|
574 | Alternative newAlt( appExpr, resultEnv, sumCost( actualAlt ) ); |
---|
575 | makeExprList( actualAlt, appExpr->get_args() ); |
---|
576 | PRINT( |
---|
577 | std::cerr << "need assertions:" << std::endl; |
---|
578 | printAssertionSet( resultNeed, std::cerr, 8 ); |
---|
579 | ) |
---|
580 | inferParameters( resultNeed, resultHave, newAlt, openVars, out ); |
---|
581 | } |
---|
582 | } |
---|
583 | |
---|
584 | void AlternativeFinder::visit( UntypedExpr *untypedExpr ) { |
---|
585 | bool doneInit = false; |
---|
586 | AlternativeFinder funcOpFinder( indexer, env ); |
---|
587 | |
---|
588 | AlternativeFinder funcFinder( indexer, env ); |
---|
589 | |
---|
590 | { |
---|
591 | std::string fname = InitTweak::getFunctionName( untypedExpr ); |
---|
592 | if ( fname == "&&" ) { |
---|
593 | VoidType v = Type::Qualifiers(); // resolve to type void * |
---|
594 | PointerType pt( Type::Qualifiers(), v.clone() ); |
---|
595 | UntypedExpr *vexpr = untypedExpr->clone(); |
---|
596 | vexpr->set_result( pt.clone() ); |
---|
597 | alternatives.push_back( Alternative( vexpr, env, Cost()) ); |
---|
598 | return; |
---|
599 | } |
---|
600 | } |
---|
601 | |
---|
602 | funcFinder.findWithAdjustment( untypedExpr->get_function() ); |
---|
603 | std::list< AlternativeFinder > argAlternatives; |
---|
604 | findSubExprs( untypedExpr->begin_args(), untypedExpr->end_args(), back_inserter( argAlternatives ) ); |
---|
605 | |
---|
606 | std::list< AltList > possibilities; |
---|
607 | combos( argAlternatives.begin(), argAlternatives.end(), back_inserter( possibilities ) ); |
---|
608 | |
---|
609 | // take care of possible tuple assignments |
---|
610 | // if not tuple assignment, assignment is taken care of as a normal function call |
---|
611 | Tuples::handleTupleAssignment( *this, untypedExpr, possibilities ); |
---|
612 | |
---|
613 | AltList candidates; |
---|
614 | SemanticError errors; |
---|
615 | |
---|
616 | for ( AltList::const_iterator func = funcFinder.alternatives.begin(); func != funcFinder.alternatives.end(); ++func ) { |
---|
617 | try { |
---|
618 | PRINT( |
---|
619 | std::cerr << "working on alternative: " << std::endl; |
---|
620 | func->print( std::cerr, 8 ); |
---|
621 | ) |
---|
622 | // check if the type is pointer to function |
---|
623 | PointerType *pointer; |
---|
624 | if ( ( pointer = dynamic_cast< PointerType* >( func->expr->get_result() ) ) ) { |
---|
625 | if ( FunctionType *function = dynamic_cast< FunctionType* >( pointer->get_base() ) ) { |
---|
626 | for ( std::list< AltList >::iterator actualAlt = possibilities.begin(); actualAlt != possibilities.end(); ++actualAlt ) { |
---|
627 | // XXX |
---|
628 | //Designators::check_alternative( function, *actualAlt ); |
---|
629 | makeFunctionAlternatives( *func, function, *actualAlt, std::back_inserter( candidates ) ); |
---|
630 | } |
---|
631 | } else if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( pointer->get_base() ) ) { |
---|
632 | EqvClass eqvClass; |
---|
633 | if ( func->env.lookup( typeInst->get_name(), eqvClass ) && eqvClass.type ) { |
---|
634 | if ( FunctionType *function = dynamic_cast< FunctionType* >( eqvClass.type ) ) { |
---|
635 | for ( std::list< AltList >::iterator actualAlt = possibilities.begin(); actualAlt != possibilities.end(); ++actualAlt ) { |
---|
636 | makeFunctionAlternatives( *func, function, *actualAlt, std::back_inserter( candidates ) ); |
---|
637 | } // for |
---|
638 | } // if |
---|
639 | } // if |
---|
640 | } // if |
---|
641 | } else { |
---|
642 | // seek a function operator that's compatible |
---|
643 | if ( ! doneInit ) { |
---|
644 | doneInit = true; |
---|
645 | NameExpr *opExpr = new NameExpr( "?()" ); |
---|
646 | try { |
---|
647 | funcOpFinder.findWithAdjustment( opExpr ); |
---|
648 | } catch( SemanticError &e ) { |
---|
649 | // it's ok if there aren't any defined function ops |
---|
650 | } |
---|
651 | PRINT( |
---|
652 | std::cerr << "known function ops:" << std::endl; |
---|
653 | printAlts( funcOpFinder.alternatives, std::cerr, 8 ); |
---|
654 | ) |
---|
655 | } |
---|
656 | |
---|
657 | for ( AltList::const_iterator funcOp = funcOpFinder.alternatives.begin(); funcOp != funcOpFinder.alternatives.end(); ++funcOp ) { |
---|
658 | // check if the type is pointer to function |
---|
659 | PointerType *pointer; |
---|
660 | if ( ( pointer = dynamic_cast< PointerType* >( funcOp->expr->get_result() ) ) ) { |
---|
661 | if ( FunctionType *function = dynamic_cast< FunctionType* >( pointer->get_base() ) ) { |
---|
662 | for ( std::list< AltList >::iterator actualAlt = possibilities.begin(); actualAlt != possibilities.end(); ++actualAlt ) { |
---|
663 | AltList currentAlt; |
---|
664 | currentAlt.push_back( *func ); |
---|
665 | currentAlt.insert( currentAlt.end(), actualAlt->begin(), actualAlt->end() ); |
---|
666 | makeFunctionAlternatives( *funcOp, function, currentAlt, std::back_inserter( candidates ) ); |
---|
667 | } // for |
---|
668 | } // if |
---|
669 | } // if |
---|
670 | } // for |
---|
671 | } // if |
---|
672 | } catch ( SemanticError &e ) { |
---|
673 | errors.append( e ); |
---|
674 | } |
---|
675 | } // for |
---|
676 | |
---|
677 | // Implement SFINAE; resolution errors are only errors if there aren't any non-erroneous resolutions |
---|
678 | if ( candidates.empty() && ! errors.isEmpty() ) { throw errors; } |
---|
679 | |
---|
680 | for ( AltList::iterator withFunc = candidates.begin(); withFunc != candidates.end(); ++withFunc ) { |
---|
681 | Cost cvtCost = computeConversionCost( *withFunc, indexer ); |
---|
682 | |
---|
683 | PRINT( |
---|
684 | ApplicationExpr *appExpr = safe_dynamic_cast< ApplicationExpr* >( withFunc->expr ); |
---|
685 | PointerType *pointer = safe_dynamic_cast< PointerType* >( appExpr->get_function()->get_result() ); |
---|
686 | FunctionType *function = safe_dynamic_cast< FunctionType* >( pointer->get_base() ); |
---|
687 | std::cerr << "Case +++++++++++++" << std::endl; |
---|
688 | std::cerr << "formals are:" << std::endl; |
---|
689 | printAll( function->get_parameters(), std::cerr, 8 ); |
---|
690 | std::cerr << "actuals are:" << std::endl; |
---|
691 | printAll( appExpr->get_args(), std::cerr, 8 ); |
---|
692 | std::cerr << "bindings are:" << std::endl; |
---|
693 | withFunc->env.print( std::cerr, 8 ); |
---|
694 | std::cerr << "cost of conversion is:" << cvtCost << std::endl; |
---|
695 | ) |
---|
696 | if ( cvtCost != Cost::infinity ) { |
---|
697 | withFunc->cvtCost = cvtCost; |
---|
698 | alternatives.push_back( *withFunc ); |
---|
699 | } // if |
---|
700 | } // for |
---|
701 | candidates.clear(); |
---|
702 | candidates.splice( candidates.end(), alternatives ); |
---|
703 | |
---|
704 | findMinCost( candidates.begin(), candidates.end(), std::back_inserter( alternatives ) ); |
---|
705 | } |
---|
706 | |
---|
707 | bool isLvalue( Expression *expr ) { |
---|
708 | // xxx - recurse into tuples? |
---|
709 | return expr->has_result() && expr->get_result()->get_isLvalue(); |
---|
710 | } |
---|
711 | |
---|
712 | void AlternativeFinder::visit( AddressExpr *addressExpr ) { |
---|
713 | AlternativeFinder finder( indexer, env ); |
---|
714 | finder.find( addressExpr->get_arg() ); |
---|
715 | for ( std::list< Alternative >::iterator i = finder.alternatives.begin(); i != finder.alternatives.end(); ++i ) { |
---|
716 | if ( isLvalue( i->expr ) ) { |
---|
717 | alternatives.push_back( Alternative( new AddressExpr( i->expr->clone() ), i->env, i->cost ) ); |
---|
718 | } // if |
---|
719 | } // for |
---|
720 | } |
---|
721 | |
---|
722 | void AlternativeFinder::visit( CastExpr *castExpr ) { |
---|
723 | Type *& toType = castExpr->get_result(); |
---|
724 | toType = resolveTypeof( toType, indexer ); |
---|
725 | SymTab::validateType( toType, &indexer ); |
---|
726 | adjustExprType( toType, env, indexer ); |
---|
727 | |
---|
728 | AlternativeFinder finder( indexer, env ); |
---|
729 | finder.findWithAdjustment( castExpr->get_arg() ); |
---|
730 | |
---|
731 | AltList candidates; |
---|
732 | for ( std::list< Alternative >::iterator i = finder.alternatives.begin(); i != finder.alternatives.end(); ++i ) { |
---|
733 | AssertionSet needAssertions, haveAssertions; |
---|
734 | OpenVarSet openVars; |
---|
735 | |
---|
736 | // It's possible that a cast can throw away some values in a multiply-valued expression. (An example is a |
---|
737 | // cast-to-void, which casts from one value to zero.) Figure out the prefix of the subexpression results |
---|
738 | // that are cast directly. The candidate is invalid if it has fewer results than there are types to cast |
---|
739 | // to. |
---|
740 | int discardedValues = (*i).expr->get_result()->size() - castExpr->get_result()->size(); |
---|
741 | if ( discardedValues < 0 ) continue; |
---|
742 | // xxx - may need to go into tuple types and extract relavent types and use unifyList |
---|
743 | // unification run for side-effects |
---|
744 | unify( castExpr->get_result(), (*i).expr->get_result(), i->env, needAssertions, haveAssertions, openVars, indexer ); |
---|
745 | Cost thisCost = castCost( (*i).expr->get_result(), castExpr->get_result(), indexer, i->env ); |
---|
746 | if ( thisCost != Cost::infinity ) { |
---|
747 | // count one safe conversion for each value that is thrown away |
---|
748 | thisCost += Cost( 0, 0, discardedValues ); |
---|
749 | CastExpr *newExpr = castExpr->clone(); |
---|
750 | newExpr->set_arg( i->expr->clone() ); |
---|
751 | candidates.push_back( Alternative( newExpr, i->env, i->cost, thisCost ) ); |
---|
752 | } // if |
---|
753 | } // for |
---|
754 | |
---|
755 | // findMinCost selects the alternatives with the lowest "cost" members, but has the side effect of copying the |
---|
756 | // cvtCost member to the cost member (since the old cost is now irrelevant). Thus, calling findMinCost twice |
---|
757 | // selects first based on argument cost, then on conversion cost. |
---|
758 | AltList minArgCost; |
---|
759 | findMinCost( candidates.begin(), candidates.end(), std::back_inserter( minArgCost ) ); |
---|
760 | findMinCost( minArgCost.begin(), minArgCost.end(), std::back_inserter( alternatives ) ); |
---|
761 | } |
---|
762 | |
---|
763 | void AlternativeFinder::visit( UntypedMemberExpr *memberExpr ) { |
---|
764 | AlternativeFinder funcFinder( indexer, env ); |
---|
765 | funcFinder.findWithAdjustment( memberExpr->get_aggregate() ); |
---|
766 | |
---|
767 | for ( AltList::const_iterator agg = funcFinder.alternatives.begin(); agg != funcFinder.alternatives.end(); ++agg ) { |
---|
768 | if ( StructInstType *structInst = dynamic_cast< StructInstType* >( agg->expr->get_result() ) ) { |
---|
769 | addAggMembers( structInst, agg->expr, agg->cost, agg->env, memberExpr->get_member() ); |
---|
770 | } else if ( UnionInstType *unionInst = dynamic_cast< UnionInstType* >( agg->expr->get_result() ) ) { |
---|
771 | addAggMembers( unionInst, agg->expr, agg->cost, agg->env, memberExpr->get_member() ); |
---|
772 | } // if |
---|
773 | } // for |
---|
774 | } |
---|
775 | |
---|
776 | void AlternativeFinder::visit( MemberExpr *memberExpr ) { |
---|
777 | alternatives.push_back( Alternative( memberExpr->clone(), env, Cost::zero ) ); |
---|
778 | } |
---|
779 | |
---|
780 | void AlternativeFinder::visit( NameExpr *nameExpr ) { |
---|
781 | std::list< DeclarationWithType* > declList; |
---|
782 | indexer.lookupId( nameExpr->get_name(), declList ); |
---|
783 | PRINT( std::cerr << "nameExpr is " << nameExpr->get_name() << std::endl; ) |
---|
784 | for ( std::list< DeclarationWithType* >::iterator i = declList.begin(); i != declList.end(); ++i ) { |
---|
785 | VariableExpr newExpr( *i, nameExpr->get_argName() ); |
---|
786 | alternatives.push_back( Alternative( newExpr.clone(), env, Cost() ) ); |
---|
787 | PRINT( |
---|
788 | std::cerr << "decl is "; |
---|
789 | (*i)->print( std::cerr ); |
---|
790 | std::cerr << std::endl; |
---|
791 | std::cerr << "newExpr is "; |
---|
792 | newExpr.print( std::cerr ); |
---|
793 | std::cerr << std::endl; |
---|
794 | ) |
---|
795 | renameTypes( alternatives.back().expr ); |
---|
796 | if ( StructInstType *structInst = dynamic_cast< StructInstType* >( (*i)->get_type() ) ) { |
---|
797 | NameExpr nameExpr( "" ); |
---|
798 | addAggMembers( structInst, &newExpr, Cost( 0, 0, 1 ), env, &nameExpr ); |
---|
799 | } else if ( UnionInstType *unionInst = dynamic_cast< UnionInstType* >( (*i)->get_type() ) ) { |
---|
800 | NameExpr nameExpr( "" ); |
---|
801 | addAggMembers( unionInst, &newExpr, Cost( 0, 0, 1 ), env, &nameExpr ); |
---|
802 | } // if |
---|
803 | } // for |
---|
804 | } |
---|
805 | |
---|
806 | void AlternativeFinder::visit( VariableExpr *variableExpr ) { |
---|
807 | // not sufficient to clone here, because variable's type may have changed |
---|
808 | // since the VariableExpr was originally created. |
---|
809 | alternatives.push_back( Alternative( new VariableExpr( variableExpr->get_var() ), env, Cost::zero ) ); |
---|
810 | } |
---|
811 | |
---|
812 | void AlternativeFinder::visit( ConstantExpr *constantExpr ) { |
---|
813 | alternatives.push_back( Alternative( constantExpr->clone(), env, Cost::zero ) ); |
---|
814 | } |
---|
815 | |
---|
816 | void AlternativeFinder::visit( SizeofExpr *sizeofExpr ) { |
---|
817 | if ( sizeofExpr->get_isType() ) { |
---|
818 | // xxx - resolveTypeof? |
---|
819 | alternatives.push_back( Alternative( sizeofExpr->clone(), env, Cost::zero ) ); |
---|
820 | } else { |
---|
821 | // find all alternatives for the argument to sizeof |
---|
822 | AlternativeFinder finder( indexer, env ); |
---|
823 | finder.find( sizeofExpr->get_expr() ); |
---|
824 | // find the lowest cost alternative among the alternatives, otherwise ambiguous |
---|
825 | AltList winners; |
---|
826 | findMinCost( finder.alternatives.begin(), finder.alternatives.end(), back_inserter( winners ) ); |
---|
827 | if ( winners.size() != 1 ) { |
---|
828 | throw SemanticError( "Ambiguous expression in sizeof operand: ", sizeofExpr->get_expr() ); |
---|
829 | } // if |
---|
830 | // return the lowest cost alternative for the argument |
---|
831 | Alternative &choice = winners.front(); |
---|
832 | alternatives.push_back( Alternative( new SizeofExpr( choice.expr->clone() ), choice.env, Cost::zero ) ); |
---|
833 | } // if |
---|
834 | } |
---|
835 | |
---|
836 | void AlternativeFinder::visit( AlignofExpr *alignofExpr ) { |
---|
837 | if ( alignofExpr->get_isType() ) { |
---|
838 | // xxx - resolveTypeof? |
---|
839 | alternatives.push_back( Alternative( alignofExpr->clone(), env, Cost::zero ) ); |
---|
840 | } else { |
---|
841 | // find all alternatives for the argument to sizeof |
---|
842 | AlternativeFinder finder( indexer, env ); |
---|
843 | finder.find( alignofExpr->get_expr() ); |
---|
844 | // find the lowest cost alternative among the alternatives, otherwise ambiguous |
---|
845 | AltList winners; |
---|
846 | findMinCost( finder.alternatives.begin(), finder.alternatives.end(), back_inserter( winners ) ); |
---|
847 | if ( winners.size() != 1 ) { |
---|
848 | throw SemanticError( "Ambiguous expression in alignof operand: ", alignofExpr->get_expr() ); |
---|
849 | } // if |
---|
850 | // return the lowest cost alternative for the argument |
---|
851 | Alternative &choice = winners.front(); |
---|
852 | alternatives.push_back( Alternative( new AlignofExpr( choice.expr->clone() ), choice.env, Cost::zero ) ); |
---|
853 | } // if |
---|
854 | } |
---|
855 | |
---|
856 | template< typename StructOrUnionType > |
---|
857 | void AlternativeFinder::addOffsetof( StructOrUnionType *aggInst, const std::string &name ) { |
---|
858 | std::list< Declaration* > members; |
---|
859 | aggInst->lookup( name, members ); |
---|
860 | for ( std::list< Declaration* >::const_iterator i = members.begin(); i != members.end(); ++i ) { |
---|
861 | if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType* >( *i ) ) { |
---|
862 | alternatives.push_back( Alternative( new OffsetofExpr( aggInst->clone(), dwt ), env, Cost::zero ) ); |
---|
863 | renameTypes( alternatives.back().expr ); |
---|
864 | } else { |
---|
865 | assert( false ); |
---|
866 | } |
---|
867 | } |
---|
868 | } |
---|
869 | |
---|
870 | void AlternativeFinder::visit( UntypedOffsetofExpr *offsetofExpr ) { |
---|
871 | AlternativeFinder funcFinder( indexer, env ); |
---|
872 | // xxx - resolveTypeof? |
---|
873 | if ( StructInstType *structInst = dynamic_cast< StructInstType* >( offsetofExpr->get_type() ) ) { |
---|
874 | addOffsetof( structInst, offsetofExpr->get_member() ); |
---|
875 | } else if ( UnionInstType *unionInst = dynamic_cast< UnionInstType* >( offsetofExpr->get_type() ) ) { |
---|
876 | addOffsetof( unionInst, offsetofExpr->get_member() ); |
---|
877 | } |
---|
878 | } |
---|
879 | |
---|
880 | void AlternativeFinder::visit( OffsetofExpr *offsetofExpr ) { |
---|
881 | alternatives.push_back( Alternative( offsetofExpr->clone(), env, Cost::zero ) ); |
---|
882 | } |
---|
883 | |
---|
884 | void AlternativeFinder::visit( OffsetPackExpr *offsetPackExpr ) { |
---|
885 | alternatives.push_back( Alternative( offsetPackExpr->clone(), env, Cost::zero ) ); |
---|
886 | } |
---|
887 | |
---|
888 | void AlternativeFinder::resolveAttr( DeclarationWithType *funcDecl, FunctionType *function, Type *argType, const TypeEnvironment &env ) { |
---|
889 | // assume no polymorphism |
---|
890 | // assume no implicit conversions |
---|
891 | assert( function->get_parameters().size() == 1 ); |
---|
892 | PRINT( |
---|
893 | std::cerr << "resolvAttr: funcDecl is "; |
---|
894 | funcDecl->print( std::cerr ); |
---|
895 | std::cerr << " argType is "; |
---|
896 | argType->print( std::cerr ); |
---|
897 | std::cerr << std::endl; |
---|
898 | ) |
---|
899 | if ( typesCompatibleIgnoreQualifiers( argType, function->get_parameters().front()->get_type(), indexer, env ) ) { |
---|
900 | alternatives.push_back( Alternative( new AttrExpr( new VariableExpr( funcDecl ), argType->clone() ), env, Cost::zero ) ); |
---|
901 | for ( std::list< DeclarationWithType* >::iterator i = function->get_returnVals().begin(); i != function->get_returnVals().end(); ++i ) { |
---|
902 | alternatives.back().expr->set_result( (*i)->get_type()->clone() ); |
---|
903 | } // for |
---|
904 | } // if |
---|
905 | } |
---|
906 | |
---|
907 | void AlternativeFinder::visit( AttrExpr *attrExpr ) { |
---|
908 | // assume no 'pointer-to-attribute' |
---|
909 | NameExpr *nameExpr = dynamic_cast< NameExpr* >( attrExpr->get_attr() ); |
---|
910 | assert( nameExpr ); |
---|
911 | std::list< DeclarationWithType* > attrList; |
---|
912 | indexer.lookupId( nameExpr->get_name(), attrList ); |
---|
913 | if ( attrExpr->get_isType() || attrExpr->get_expr() ) { |
---|
914 | for ( std::list< DeclarationWithType* >::iterator i = attrList.begin(); i != attrList.end(); ++i ) { |
---|
915 | // check if the type is function |
---|
916 | if ( FunctionType *function = dynamic_cast< FunctionType* >( (*i)->get_type() ) ) { |
---|
917 | // assume exactly one parameter |
---|
918 | if ( function->get_parameters().size() == 1 ) { |
---|
919 | if ( attrExpr->get_isType() ) { |
---|
920 | resolveAttr( *i, function, attrExpr->get_type(), env ); |
---|
921 | } else { |
---|
922 | AlternativeFinder finder( indexer, env ); |
---|
923 | finder.find( attrExpr->get_expr() ); |
---|
924 | for ( AltList::iterator choice = finder.alternatives.begin(); choice != finder.alternatives.end(); ++choice ) { |
---|
925 | if ( choice->expr->get_result()->size() == 1 ) { |
---|
926 | resolveAttr(*i, function, choice->expr->get_result(), choice->env ); |
---|
927 | } // fi |
---|
928 | } // for |
---|
929 | } // if |
---|
930 | } // if |
---|
931 | } // if |
---|
932 | } // for |
---|
933 | } else { |
---|
934 | for ( std::list< DeclarationWithType* >::iterator i = attrList.begin(); i != attrList.end(); ++i ) { |
---|
935 | VariableExpr newExpr( *i ); |
---|
936 | alternatives.push_back( Alternative( newExpr.clone(), env, Cost() ) ); |
---|
937 | renameTypes( alternatives.back().expr ); |
---|
938 | } // for |
---|
939 | } // if |
---|
940 | } |
---|
941 | |
---|
942 | void AlternativeFinder::visit( LogicalExpr *logicalExpr ) { |
---|
943 | AlternativeFinder firstFinder( indexer, env ); |
---|
944 | firstFinder.findWithAdjustment( logicalExpr->get_arg1() ); |
---|
945 | for ( AltList::const_iterator first = firstFinder.alternatives.begin(); first != firstFinder.alternatives.end(); ++first ) { |
---|
946 | AlternativeFinder secondFinder( indexer, first->env ); |
---|
947 | secondFinder.findWithAdjustment( logicalExpr->get_arg2() ); |
---|
948 | for ( AltList::const_iterator second = secondFinder.alternatives.begin(); second != secondFinder.alternatives.end(); ++second ) { |
---|
949 | LogicalExpr *newExpr = new LogicalExpr( first->expr->clone(), second->expr->clone(), logicalExpr->get_isAnd() ); |
---|
950 | alternatives.push_back( Alternative( newExpr, second->env, first->cost + second->cost ) ); |
---|
951 | } |
---|
952 | } |
---|
953 | } |
---|
954 | |
---|
955 | void AlternativeFinder::visit( ConditionalExpr *conditionalExpr ) { |
---|
956 | AlternativeFinder firstFinder( indexer, env ); |
---|
957 | firstFinder.findWithAdjustment( conditionalExpr->get_arg1() ); |
---|
958 | for ( AltList::const_iterator first = firstFinder.alternatives.begin(); first != firstFinder.alternatives.end(); ++first ) { |
---|
959 | AlternativeFinder secondFinder( indexer, first->env ); |
---|
960 | secondFinder.findWithAdjustment( conditionalExpr->get_arg2() ); |
---|
961 | for ( AltList::const_iterator second = secondFinder.alternatives.begin(); second != secondFinder.alternatives.end(); ++second ) { |
---|
962 | AlternativeFinder thirdFinder( indexer, second->env ); |
---|
963 | thirdFinder.findWithAdjustment( conditionalExpr->get_arg3() ); |
---|
964 | for ( AltList::const_iterator third = thirdFinder.alternatives.begin(); third != thirdFinder.alternatives.end(); ++third ) { |
---|
965 | OpenVarSet openVars; |
---|
966 | AssertionSet needAssertions, haveAssertions; |
---|
967 | Alternative newAlt( 0, third->env, first->cost + second->cost + third->cost ); |
---|
968 | Type* commonType; |
---|
969 | if ( unify( second->expr->get_result(), third->expr->get_result(), newAlt.env, needAssertions, haveAssertions, openVars, indexer, commonType ) ) { |
---|
970 | ConditionalExpr *newExpr = new ConditionalExpr( first->expr->clone(), second->expr->clone(), third->expr->clone() ); |
---|
971 | newExpr->set_result( commonType ? commonType : second->expr->get_result()->clone() ); |
---|
972 | newAlt.expr = newExpr; |
---|
973 | inferParameters( needAssertions, haveAssertions, newAlt, openVars, back_inserter( alternatives ) ); |
---|
974 | } // if |
---|
975 | } // for |
---|
976 | } // for |
---|
977 | } // for |
---|
978 | } |
---|
979 | |
---|
980 | void AlternativeFinder::visit( CommaExpr *commaExpr ) { |
---|
981 | TypeEnvironment newEnv( env ); |
---|
982 | Expression *newFirstArg = resolveInVoidContext( commaExpr->get_arg1(), indexer, newEnv ); |
---|
983 | AlternativeFinder secondFinder( indexer, newEnv ); |
---|
984 | secondFinder.findWithAdjustment( commaExpr->get_arg2() ); |
---|
985 | for ( AltList::const_iterator alt = secondFinder.alternatives.begin(); alt != secondFinder.alternatives.end(); ++alt ) { |
---|
986 | alternatives.push_back( Alternative( new CommaExpr( newFirstArg->clone(), alt->expr->clone() ), alt->env, alt->cost ) ); |
---|
987 | } // for |
---|
988 | delete newFirstArg; |
---|
989 | } |
---|
990 | |
---|
991 | void AlternativeFinder::visit( TupleExpr *tupleExpr ) { |
---|
992 | std::list< AlternativeFinder > subExprAlternatives; |
---|
993 | findSubExprs( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end(), back_inserter( subExprAlternatives ) ); |
---|
994 | std::list< AltList > possibilities; |
---|
995 | combos( subExprAlternatives.begin(), subExprAlternatives.end(), back_inserter( possibilities ) ); |
---|
996 | for ( std::list< AltList >::const_iterator i = possibilities.begin(); i != possibilities.end(); ++i ) { |
---|
997 | TupleExpr *newExpr = new TupleExpr; |
---|
998 | makeExprList( *i, newExpr->get_exprs() ); |
---|
999 | newExpr->set_result( Tuples::makeTupleType( newExpr->get_exprs() ) ); |
---|
1000 | |
---|
1001 | TypeEnvironment compositeEnv; |
---|
1002 | simpleCombineEnvironments( i->begin(), i->end(), compositeEnv ); |
---|
1003 | alternatives.push_back( Alternative( newExpr, compositeEnv, sumCost( *i ) ) ); |
---|
1004 | } // for |
---|
1005 | } |
---|
1006 | |
---|
1007 | void AlternativeFinder::visit( ImplicitCopyCtorExpr * impCpCtorExpr ) { |
---|
1008 | alternatives.push_back( Alternative( impCpCtorExpr->clone(), env, Cost::zero ) ); |
---|
1009 | } |
---|
1010 | |
---|
1011 | void AlternativeFinder::visit( ConstructorExpr * ctorExpr ) { |
---|
1012 | AlternativeFinder finder( indexer, env ); |
---|
1013 | // don't prune here, since it's guaranteed all alternatives will have the same type |
---|
1014 | // (giving the alternatives different types is half of the point of ConstructorExpr nodes) |
---|
1015 | finder.findWithAdjustment( ctorExpr->get_callExpr(), false ); |
---|
1016 | for ( Alternative & alt : finder.alternatives ) { |
---|
1017 | alternatives.push_back( Alternative( new ConstructorExpr( alt.expr->clone() ), alt.env, alt.cost ) ); |
---|
1018 | } |
---|
1019 | } |
---|
1020 | |
---|
1021 | void AlternativeFinder::visit( TupleIndexExpr *tupleExpr ) { |
---|
1022 | alternatives.push_back( Alternative( tupleExpr->clone(), env, Cost::zero ) ); |
---|
1023 | } |
---|
1024 | |
---|
1025 | void AlternativeFinder::visit( TupleAssignExpr *tupleAssignExpr ) { |
---|
1026 | alternatives.push_back( Alternative( tupleAssignExpr->clone(), env, Cost::zero ) ); |
---|
1027 | } |
---|
1028 | } // namespace ResolvExpr |
---|
1029 | |
---|
1030 | // Local Variables: // |
---|
1031 | // tab-width: 4 // |
---|
1032 | // mode: c++ // |
---|
1033 | // compile-command: "make install" // |
---|
1034 | // End: // |
---|