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