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