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