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