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