// // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo // // The contents of this file are covered under the licence agreement in the // file "LICENCE" distributed with Cforall. // // AlternativeFinder.cc -- // // Author : Richard C. Bilson // Created On : Sat May 16 23:52:08 2015 // Last Modified By : Peter A. Buhr // Last Modified On : Mon Aug 28 13:47:24 2017 // Update Count : 32 // #include // for copy #include // for strict_dynamic_cast, assert, assertf #include // for operator<<, cerr, ostream, endl #include // for back_insert_iterator, back_inserter #include // for _List_iterator, list, _List_const_... #include // for _Rb_tree_iterator, map, _Rb_tree_c... #include // for allocator_traits<>::value_type #include // for pair #include // for vector #include "Alternative.h" // for AltList, Alternative #include "AlternativeFinder.h" #include "Common/SemanticError.h" // for SemanticError #include "Common/utility.h" // for deleteAll, printAll, CodeLocation #include "Cost.h" // for Cost, Cost::zero, operator<<, Cost... #include "InitTweak/InitTweak.h" // for getFunctionName #include "RenameVars.h" // for RenameVars, global_renamer #include "ResolveTypeof.h" // for resolveTypeof #include "Resolver.h" // for resolveStmtExpr #include "SymTab/Indexer.h" // for Indexer #include "SymTab/Mangler.h" // for Mangler #include "SymTab/Validate.h" // for validateType #include "SynTree/Constant.h" // for Constant #include "SynTree/Declaration.h" // for DeclarationWithType, TypeDecl, Dec... #include "SynTree/Expression.h" // for Expression, CastExpr, NameExpr #include "SynTree/Initializer.h" // for SingleInit, operator<<, Designation #include "SynTree/SynTree.h" // for UniqueId #include "SynTree/Type.h" // for Type, FunctionType, PointerType #include "Tuples/Explode.h" // for explode #include "Tuples/Tuples.h" // for isTtype, handleTupleAssignment #include "Unify.h" // for unify #include "typeops.h" // for adjustExprType, polyCost, castCost extern bool resolvep; #define PRINT( text ) if ( resolvep ) { text } //#define DEBUG_COST namespace ResolvExpr { Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer, TypeEnvironment &env ) { CastExpr *castToVoid = new CastExpr( expr ); AlternativeFinder finder( indexer, env ); finder.findWithAdjustment( castToVoid ); // it's a property of the language that a cast expression has either 1 or 0 interpretations; if it has 0 // interpretations, an exception has already been thrown. assert( finder.get_alternatives().size() == 1 ); CastExpr *newExpr = dynamic_cast< CastExpr* >( finder.get_alternatives().front().expr ); assert( newExpr ); env = finder.get_alternatives().front().env; return newExpr->get_arg()->clone(); } Cost sumCost( const AltList &in ) { Cost total = Cost::zero; for ( AltList::const_iterator i = in.begin(); i != in.end(); ++i ) { total += i->cost; } return total; } namespace { void printAlts( const AltList &list, std::ostream &os, int indent = 0 ) { for ( AltList::const_iterator i = list.begin(); i != list.end(); ++i ) { i->print( os, indent ); os << std::endl; } } void makeExprList( const AltList &in, std::list< Expression* > &out ) { for ( AltList::const_iterator i = in.begin(); i != in.end(); ++i ) { out.push_back( i->expr->clone() ); } } struct PruneStruct { bool isAmbiguous; AltList::iterator candidate; PruneStruct() {} PruneStruct( AltList::iterator candidate ): isAmbiguous( false ), candidate( candidate ) {} }; /// Prunes a list of alternatives down to those that have the minimum conversion cost for a given return type; skips ambiguous interpretations template< typename InputIterator, typename OutputIterator > void pruneAlternatives( InputIterator begin, InputIterator end, OutputIterator out ) { // select the alternatives that have the minimum conversion cost for a particular set of result types std::map< std::string, PruneStruct > selected; for ( AltList::iterator candidate = begin; candidate != end; ++candidate ) { PruneStruct current( candidate ); std::string mangleName; { Type * newType = candidate->expr->get_result()->clone(); candidate->env.apply( newType ); mangleName = SymTab::Mangler::mangle( newType ); delete newType; } std::map< std::string, PruneStruct >::iterator mapPlace = selected.find( mangleName ); if ( mapPlace != selected.end() ) { if ( candidate->cost < mapPlace->second.candidate->cost ) { PRINT( std::cerr << "cost " << candidate->cost << " beats " << mapPlace->second.candidate->cost << std::endl; ) selected[ mangleName ] = current; } else if ( candidate->cost == mapPlace->second.candidate->cost ) { PRINT( std::cerr << "marking ambiguous" << std::endl; ) mapPlace->second.isAmbiguous = true; } } else { selected[ mangleName ] = current; } } PRINT( std::cerr << "there are " << selected.size() << " alternatives before elimination" << std::endl; ) // accept the alternatives that were unambiguous for ( std::map< std::string, PruneStruct >::iterator target = selected.begin(); target != selected.end(); ++target ) { if ( ! target->second.isAmbiguous ) { Alternative &alt = *target->second.candidate; alt.env.applyFree( alt.expr->get_result() ); *out++ = alt; } } } void renameTypes( Expression *expr ) { expr->get_result()->accept( global_renamer ); } void referenceToRvalueConversion( Expression *& expr ) { if ( dynamic_cast< ReferenceType * >( expr->get_result() ) ) { // cast away reference from expr expr = new CastExpr( expr, expr->get_result()->stripReferences()->clone() ); } } } // namespace template< typename InputIterator, typename OutputIterator > void AlternativeFinder::findSubExprs( InputIterator begin, InputIterator end, OutputIterator out ) { while ( begin != end ) { AlternativeFinder finder( indexer, env ); finder.findWithAdjustment( *begin ); // XXX either this //Designators::fixDesignations( finder, (*begin++)->get_argName() ); // or XXX this begin++; PRINT( std::cerr << "findSubExprs" << std::endl; printAlts( finder.alternatives, std::cerr ); ) *out++ = finder; } } AlternativeFinder::AlternativeFinder( const SymTab::Indexer &indexer, const TypeEnvironment &env ) : indexer( indexer ), env( env ) { } void AlternativeFinder::find( Expression *expr, bool adjust, bool prune ) { expr->accept( *this ); if ( alternatives.empty() ) { throw SemanticError( "No reasonable alternatives for expression ", expr ); } for ( AltList::iterator i = alternatives.begin(); i != alternatives.end(); ++i ) { if ( adjust ) { adjustExprType( i->expr->get_result(), i->env, indexer ); } } if ( prune ) { PRINT( std::cerr << "alternatives before prune:" << std::endl; printAlts( alternatives, std::cerr ); ) AltList::iterator oldBegin = alternatives.begin(); pruneAlternatives( alternatives.begin(), alternatives.end(), front_inserter( alternatives ) ); if ( alternatives.begin() == oldBegin ) { std::ostringstream stream; AltList winners; findMinCost( alternatives.begin(), alternatives.end(), back_inserter( winners ) ); stream << "Cannot choose between " << winners.size() << " alternatives for expression "; expr->print( stream ); stream << "Alternatives are:"; printAlts( winners, stream, 8 ); throw SemanticError( stream.str() ); } alternatives.erase( oldBegin, alternatives.end() ); PRINT( std::cerr << "there are " << alternatives.size() << " alternatives after elimination" << std::endl; ) } // Central location to handle gcc extension keyword, etc. for all expression types. for ( Alternative &iter: alternatives ) { iter.expr->set_extension( expr->get_extension() ); iter.expr->location = expr->location; } // for } void AlternativeFinder::findWithAdjustment( Expression *expr, bool prune ) { find( expr, true, prune ); } void AlternativeFinder::addAnonConversions( const Alternative & alt ) { // adds anonymous member interpretations whenever an aggregate value type is seen. // it's okay for the aggregate expression to have reference type -- cast it to the base type to treat the aggregate as the referenced value std::unique_ptr aggrExpr( alt.expr->clone() ); alt.env.apply( aggrExpr->get_result() ); Type * aggrType = aggrExpr->get_result(); if ( dynamic_cast< ReferenceType * >( aggrType ) ) { aggrType = aggrType->stripReferences(); aggrExpr.reset( new CastExpr( aggrExpr.release(), aggrType->clone() ) ); } if ( StructInstType *structInst = dynamic_cast< StructInstType* >( aggrExpr->get_result() ) ) { NameExpr nameExpr( "" ); addAggMembers( structInst, aggrExpr.get(), alt.cost+Cost::safe, alt.env, &nameExpr ); } else if ( UnionInstType *unionInst = dynamic_cast< UnionInstType* >( aggrExpr->get_result() ) ) { NameExpr nameExpr( "" ); addAggMembers( unionInst, aggrExpr.get(), alt.cost+Cost::safe, alt.env, &nameExpr ); } // if } template< typename StructOrUnionType > void AlternativeFinder::addAggMembers( StructOrUnionType *aggInst, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member ) { // by this point, member must be a name expr NameExpr * nameExpr = dynamic_cast< NameExpr * >( member ); if ( ! nameExpr ) return; const std::string & name = nameExpr->get_name(); std::list< Declaration* > members; aggInst->lookup( name, members ); for ( std::list< Declaration* >::const_iterator i = members.begin(); i != members.end(); ++i ) { if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType* >( *i ) ) { alternatives.push_back( Alternative( new MemberExpr( dwt, expr->clone() ), env, newCost ) ); renameTypes( alternatives.back().expr ); addAnonConversions( alternatives.back() ); // add anonymous member interpretations whenever an aggregate value type is seen as a member expression. } else { assert( false ); } } } void AlternativeFinder::addTupleMembers( TupleType * tupleType, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member ) { if ( ConstantExpr * constantExpr = dynamic_cast< ConstantExpr * >( member ) ) { // get the value of the constant expression as an int, must be between 0 and the length of the tuple type to have meaning // xxx - this should be improved by memoizing the value of constant exprs // during parsing and reusing that information here. std::stringstream ss( constantExpr->get_constant()->get_value() ); int val = 0; std::string tmp; if ( ss >> val && ! (ss >> tmp) ) { if ( val >= 0 && (unsigned int)val < tupleType->size() ) { alternatives.push_back( Alternative( new TupleIndexExpr( expr->clone(), val ), env, newCost ) ); } // if } // if } else if ( NameExpr * nameExpr = dynamic_cast< NameExpr * >( member ) ) { // xxx - temporary hack until 0/1 are int constants if ( nameExpr->get_name() == "0" || nameExpr->get_name() == "1" ) { std::stringstream ss( nameExpr->get_name() ); int val; ss >> val; alternatives.push_back( Alternative( new TupleIndexExpr( expr->clone(), val ), env, newCost ) ); } } // if } void AlternativeFinder::visit( ApplicationExpr *applicationExpr ) { alternatives.push_back( Alternative( applicationExpr->clone(), env, Cost::zero ) ); } Cost computeConversionCost( Type * actualType, Type * formalType, const SymTab::Indexer &indexer, const TypeEnvironment & env ) { PRINT( std::cerr << std::endl << "converting "; actualType->print( std::cerr, 8 ); std::cerr << std::endl << " to "; formalType->print( std::cerr, 8 ); std::cerr << std::endl << "environment is: "; env.print( std::cerr, 8 ); std::cerr << std::endl; ) Cost convCost = conversionCost( actualType, formalType, indexer, env ); PRINT( std::cerr << std::endl << "cost is" << convCost << std::endl; ) if ( convCost == Cost::infinity ) { return convCost; } convCost.incPoly( polyCost( formalType, env, indexer ) + polyCost( actualType, env, indexer ) ); return convCost; } Cost computeExpressionConversionCost( Expression *& actualExpr, Type * formalType, const SymTab::Indexer &indexer, const TypeEnvironment & env ) { Cost convCost = computeConversionCost( actualExpr->result, formalType, indexer, env ); // if ( convCost != Cost::zero ) { // xxx - temporary -- ignore poly cost, since this causes some polymorphic functions to be cast, which causes the specialize // pass to try to specialize them, which currently does not work. Once that is fixed, remove the next 3 lines and uncomment the // previous line. Cost tmpCost = convCost; tmpCost.incPoly( -tmpCost.get_polyCost() ); if ( tmpCost != Cost::zero ) { Type *newType = formalType->clone(); env.apply( newType ); actualExpr = new CastExpr( actualExpr, newType ); // xxx - SHOULD be able to resolve this cast, but at the moment pointers are not castable to zero_t, but are implicitly convertible. This is clearly // inconsistent, once this is fixed it should be possible to resolve the cast. // xxx - this isn't working, it appears because type1 (the formal type) is seen as widenable, but it shouldn't be, because this makes the conversion from DT* to DT* since commontype(zero_t, DT*) is DT*, rather than just nothing. // AlternativeFinder finder( indexer, env ); // finder.findWithAdjustment( actualExpr ); // assertf( finder.get_alternatives().size() > 0, "Somehow castable expression failed to find alternatives." ); // assertf( finder.get_alternatives().size() == 1, "Somehow got multiple alternatives for known cast expression." ); // Alternative & alt = finder.get_alternatives().front(); // delete actualExpr; // actualExpr = alt.expr->clone(); } return convCost; } Cost computeApplicationConversionCost( Alternative &alt, const SymTab::Indexer &indexer ) { ApplicationExpr *appExpr = strict_dynamic_cast< ApplicationExpr* >( alt.expr ); PointerType *pointer = strict_dynamic_cast< PointerType* >( appExpr->get_function()->get_result() ); FunctionType *function = strict_dynamic_cast< FunctionType* >( pointer->get_base() ); Cost convCost = Cost::zero; std::list< DeclarationWithType* >& formals = function->get_parameters(); std::list< DeclarationWithType* >::iterator formal = formals.begin(); std::list< Expression* >& actuals = appExpr->get_args(); for ( std::list< Expression* >::iterator actualExpr = actuals.begin(); actualExpr != actuals.end(); ++actualExpr ) { Type * actualType = (*actualExpr)->get_result(); PRINT( std::cerr << "actual expression:" << std::endl; (*actualExpr)->print( std::cerr, 8 ); std::cerr << "--- results are" << std::endl; actualType->print( std::cerr, 8 ); ) if ( formal == formals.end() ) { if ( function->get_isVarArgs() ) { convCost.incUnsafe(); // convert reference-typed expressions to value-typed expressions referenceToRvalueConversion( *actualExpr ); continue; } else { return Cost::infinity; } } Type * formalType = (*formal)->get_type(); PRINT( std::cerr << std::endl << "converting "; actualType->print( std::cerr, 8 ); std::cerr << std::endl << " to "; formalType->print( std::cerr, 8 ); std::cerr << std::endl << "environment is: "; alt.env.print( std::cerr, 8 ); std::cerr << std::endl; ) convCost += computeExpressionConversionCost( *actualExpr, formalType, indexer, alt.env ); ++formal; // can't be in for-loop update because of the continue } if ( formal != formals.end() ) { return Cost::infinity; } for ( InferredParams::const_iterator assert = appExpr->get_inferParams().begin(); assert != appExpr->get_inferParams().end(); ++assert ) { convCost += computeConversionCost( assert->second.actualType, assert->second.formalType, indexer, alt.env ); } return convCost; } /// Adds type variables to the open variable set and marks their assertions void makeUnifiableVars( Type *type, OpenVarSet &unifiableVars, AssertionSet &needAssertions ) { for ( Type::ForallList::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) { unifiableVars[ (*tyvar)->get_name() ] = TypeDecl::Data{ *tyvar }; for ( std::list< DeclarationWithType* >::iterator assert = (*tyvar)->get_assertions().begin(); assert != (*tyvar)->get_assertions().end(); ++assert ) { needAssertions[ *assert ].isUsed = true; } /// needAssertions.insert( needAssertions.end(), (*tyvar)->get_assertions().begin(), (*tyvar)->get_assertions().end() ); } } // /// Map of declaration uniqueIds (intended to be the assertions in an AssertionSet) to their parents and the number of times they've been included //typedef std::unordered_map< UniqueId, std::unordered_map< UniqueId, unsigned > > AssertionParentSet; static const int recursionLimit = /*10*/ 4; ///< Limit to depth of recursion satisfaction //static const unsigned recursionParentLimit = 1; ///< Limit to the number of times an assertion can recursively use itself void addToIndexer( AssertionSet &assertSet, SymTab::Indexer &indexer ) { for ( AssertionSet::iterator i = assertSet.begin(); i != assertSet.end(); ++i ) { if ( i->second.isUsed ) { indexer.addId( i->first ); } } } template< typename ForwardIterator, typename OutputIterator > void inferRecursive( ForwardIterator begin, ForwardIterator end, const Alternative &newAlt, OpenVarSet &openVars, const SymTab::Indexer &decls, const AssertionSet &newNeed, /*const AssertionParentSet &needParents,*/ int level, const SymTab::Indexer &indexer, OutputIterator out ) { if ( begin == end ) { if ( newNeed.empty() ) { PRINT( std::cerr << "all assertions satisfied, output alternative: "; newAlt.print( std::cerr ); std::cerr << std::endl; ); *out++ = newAlt; return; } else if ( level >= recursionLimit ) { throw SemanticError( "Too many recursive assertions" ); } else { AssertionSet newerNeed; PRINT( std::cerr << "recursing with new set:" << std::endl; printAssertionSet( newNeed, std::cerr, 8 ); ) inferRecursive( newNeed.begin(), newNeed.end(), newAlt, openVars, decls, newerNeed, /*needParents,*/ level+1, indexer, out ); return; } } ForwardIterator cur = begin++; if ( ! cur->second.isUsed ) { inferRecursive( begin, end, newAlt, openVars, decls, newNeed, /*needParents,*/ level, indexer, out ); return; // xxx - should this continue? previously this wasn't here, and it looks like it should be } DeclarationWithType *curDecl = cur->first; PRINT( std::cerr << "inferRecursive: assertion is "; curDecl->print( std::cerr ); std::cerr << std::endl; ) std::list< DeclarationWithType* > candidates; decls.lookupId( curDecl->get_name(), candidates ); /// if ( candidates.empty() ) { std::cerr << "no candidates!" << std::endl; } for ( std::list< DeclarationWithType* >::const_iterator candidate = candidates.begin(); candidate != candidates.end(); ++candidate ) { PRINT( std::cerr << "inferRecursive: candidate is "; (*candidate)->print( std::cerr ); std::cerr << std::endl; ) AssertionSet newHave, newerNeed( newNeed ); TypeEnvironment newEnv( newAlt.env ); OpenVarSet newOpenVars( openVars ); Type *adjType = (*candidate)->get_type()->clone(); adjustExprType( adjType, newEnv, indexer ); adjType->accept( global_renamer ); PRINT( std::cerr << "unifying "; curDecl->get_type()->print( std::cerr ); std::cerr << " with "; adjType->print( std::cerr ); std::cerr << std::endl; ) if ( unify( curDecl->get_type(), adjType, newEnv, newerNeed, newHave, newOpenVars, indexer ) ) { PRINT( std::cerr << "success!" << std::endl; ) SymTab::Indexer newDecls( decls ); addToIndexer( newHave, newDecls ); Alternative newerAlt( newAlt ); newerAlt.env = newEnv; assert( (*candidate)->get_uniqueId() ); DeclarationWithType *candDecl = static_cast< DeclarationWithType* >( Declaration::declFromId( (*candidate)->get_uniqueId() ) ); // everything with an empty idChain was pulled in by the current assertion. // add current assertion's idChain + current assertion's ID so that the correct inferParameters can be found. for ( auto & a : newerNeed ) { if ( a.second.idChain.empty() ) { a.second.idChain = cur->second.idChain; a.second.idChain.push_back( curDecl->get_uniqueId() ); } } //AssertionParentSet newNeedParents( needParents ); // skip repeatingly-self-recursive assertion satisfaction // DOESN'T WORK: grandchild nodes conflict with their cousins //if ( newNeedParents[ curDecl->get_uniqueId() ][ candDecl->get_uniqueId() ]++ > recursionParentLimit ) continue; Expression *varExpr = new VariableExpr( candDecl ); delete varExpr->get_result(); varExpr->set_result( adjType->clone() ); PRINT( std::cerr << "satisfying assertion " << curDecl->get_uniqueId() << " "; curDecl->print( std::cerr ); std::cerr << " with declaration " << (*candidate)->get_uniqueId() << " "; (*candidate)->print( std::cerr ); std::cerr << std::endl; ) ApplicationExpr *appExpr = static_cast< ApplicationExpr* >( newerAlt.expr ); // follow the current assertion's ID chain to find the correct set of inferred parameters to add the candidate to (i.e. the set of inferred parameters belonging to the entity which requested the assertion parameter). InferredParams * inferParameters = &appExpr->get_inferParams(); for ( UniqueId id : cur->second.idChain ) { inferParameters = (*inferParameters)[ id ].inferParams.get(); } // XXX: this is a memory leak, but adjType can't be deleted because it might contain assertions (*inferParameters)[ curDecl->get_uniqueId() ] = ParamEntry( (*candidate)->get_uniqueId(), adjType->clone(), curDecl->get_type()->clone(), varExpr ); inferRecursive( begin, end, newerAlt, newOpenVars, newDecls, newerNeed, /*newNeedParents,*/ level, indexer, out ); } else { delete adjType; } } } template< typename OutputIterator > void AlternativeFinder::inferParameters( const AssertionSet &need, AssertionSet &have, const Alternative &newAlt, OpenVarSet &openVars, OutputIterator out ) { // PRINT( // std::cerr << "inferParameters: assertions needed are" << std::endl; // printAll( need, std::cerr, 8 ); // ) SymTab::Indexer decls( indexer ); // PRINT( // std::cerr << "============= original indexer" << std::endl; // indexer.print( std::cerr ); // std::cerr << "============= new indexer" << std::endl; // decls.print( std::cerr ); // ) addToIndexer( have, decls ); AssertionSet newNeed; //AssertionParentSet needParents; PRINT( std::cerr << "env is: " << std::endl; newAlt.env.print( std::cerr, 0 ); std::cerr << std::endl; ) inferRecursive( need.begin(), need.end(), newAlt, openVars, decls, newNeed, /*needParents,*/ 0, indexer, out ); // PRINT( // std::cerr << "declaration 14 is "; // Declaration::declFromId // *out++ = newAlt; // ) } /// Gets a default value from an initializer, nullptr if not present ConstantExpr* getDefaultValue( Initializer* init ) { if ( SingleInit* si = dynamic_cast( init ) ) { if ( CastExpr* ce = dynamic_cast( si->get_value() ) ) { return dynamic_cast( ce->get_arg() ); } } return nullptr; } /// State to iteratively build a match of parameter expressions to arguments struct ArgPack { AltList actuals; ///< Arguments included in this pack TypeEnvironment env; ///< Environment for this pack AssertionSet need; ///< Assertions outstanding for this pack AssertionSet have; ///< Assertions found for this pack OpenVarSet openVars; ///< Open variables for this pack unsigned nextArg; ///< Index of next argument in arguments list /// Number of elements included in current tuple element (nested appropriately) std::vector tupleEls; ArgPack(const TypeEnvironment& env, const AssertionSet& need, const AssertionSet& have, const OpenVarSet& openVars) : actuals(), env(env), need(need), have(have), openVars(openVars), nextArg(0), tupleEls() {} ArgPack(const ArgPack& old, Expression* actual, TypeEnvironment&& env, OpenVarSet&& openVars, const Cost& cost = Cost::zero) : actuals(old.actuals), env(std::move(env)), need(old.need), have(old.have), openVars(std::move(openVars)), nextArg(old.nextArg + 1), tupleEls(old.tupleEls) { // add new actual actuals.emplace_back( actual, this->env, cost ); // count tuple elements, if applicable if ( ! tupleEls.empty() ) ++tupleEls.back(); } ArgPack(const ArgPack& old, Expression* actual, TypeEnvironment&& env, AssertionSet&& need, AssertionSet&& have, OpenVarSet&& openVars, const Cost& cost = Cost::zero) : actuals(old.actuals), env(std::move(env)), need(std::move(need)), have(std::move(have)), openVars(std::move(openVars)), nextArg(old.nextArg + 1), tupleEls(old.tupleEls) { // add new actual actuals.emplace_back( actual, this->env, cost ); // count tuple elements, if applicable if ( ! tupleEls.empty() ) ++tupleEls.back(); } /// Starts a new tuple expression void beginTuple() { if ( ! tupleEls.empty() ) ++tupleEls.back(); tupleEls.push_back(0); } /// Ends a tuple expression, consolidating the appropriate actuals void endTuple() { // set up new Tuple alternative std::list exprs; Cost cost = Cost::zero; // transfer elements into alternative for (unsigned i = 0; i < tupleEls.back(); ++i) { exprs.push_front( actuals.back().expr ); cost += actuals.back().cost; actuals.pop_back(); } tupleEls.pop_back(); // build new alternative actuals.emplace_back( new TupleExpr( exprs ), this->env, cost ); } }; /// Iterates a result, exploding actuals as needed. /// add is a function that takes the same parameters as this (with the exception of add) template void addExplodedActual( ArgPack& result, Expression* expr, Cost cost, std::vector& nextResults, F add ) { Type* res = expr->get_result()->stripReferences(); if ( TupleType* tupleType = dynamic_cast( res ) ) { if ( TupleExpr* tupleExpr = dynamic_cast( expr ) ) { // recursively explode tuple for ( Expression* sexpr : tupleExpr->get_exprs() ) { addExplodedActual( result, sexpr, cost, nextResults, add ); cost = Cost::zero; // reset cost so not duplicated } } else { // tuple type, but not tuple expr - recursively index into components. // if expr type is reference, convert to value type Expression* arg = expr->clone(); if ( Tuples::maybeImpureIgnoreUnique( arg ) ) { // expressions which may contain side effects require a single unique instance of the expression. arg = new UniqueExpr( arg ); } // cast reference to value type to facilitate further explosion if ( dynamic_cast( arg->get_result() ) ) { arg = new CastExpr( arg, tupleType->clone() ); } // explode tuple by index for ( unsigned i = 0; i < tupleType->size(); ++i ) { TupleIndexExpr* idx = new TupleIndexExpr( arg->clone(), i ); addExplodedActual( result, idx, cost, nextResults, add ); cost = Cost::zero; // reset cost so not duplicated delete idx; } delete arg; } } else { // add non-tuple results directly add( result, expr->clone(), cost, nextResults ); } } /// Instantiates an argument to match a formal, returns false if no results left bool instantiateArgument( Type* formalType, Initializer* initializer, const std::vector< AlternativeFinder >& args, std::vector& results, std::vector& nextResults, const SymTab::Indexer& indexer ) { if ( TupleType* tupleType = dynamic_cast( formalType ) ) { // formalType is a TupleType - group actuals into a TupleExpr for ( ArgPack& result : results ) { result.beginTuple(); } for ( Type* type : *tupleType ) { // xxx - dropping initializer changes behaviour from previous, but seems correct if ( ! instantiateArgument( type, nullptr, args, results, nextResults, indexer ) ) return false; } for ( ArgPack& result : results ) { result.endTuple(); } return true; } else if ( TypeInstType* ttype = Tuples::isTtype( formalType ) ) { // formalType is a ttype, consumes all remaining arguments // xxx - mixing default arguments with variadic?? std::vector finalResults{}; /// list of completed tuples // start tuples for ( ArgPack& result : results ) { result.beginTuple(); } // iterate until all results completed while ( ! results.empty() ) { // add another argument to results for ( ArgPack& result : results ) { // finish result when out of arguments if ( result.nextArg >= args.size() ) { Type* argType = result.actuals.back().expr->get_result(); if ( result.tupleEls.back() == 1 && Tuples::isTtype( argType ) ) { // the case where a ttype value is passed directly is special, e.g. for // argument forwarding purposes // xxx - what if passing multiple arguments, last of which is ttype? // xxx - what would happen if unify was changed so that unifying tuple // types flattened both before unifying lists? then pass in TupleType // (ttype) below. result.tupleEls.pop_back(); } else { // collapse leftover arguments into tuple result.endTuple(); argType = result.actuals.back().expr->get_result(); } // check unification for ttype before adding to final if ( unify( ttype, argType, result.env, result.need, result.have, result.openVars, indexer ) ) { finalResults.emplace_back( std::move(result) ); } continue; } // add each possible next argument for ( const Alternative& actual : args[result.nextArg] ) { addExplodedActual( result, actual.expr, actual.cost, nextResults, [&actual]( ArgPack& result, Expression* expr, Cost cost, std::vector& nextResults ) { TypeEnvironment env{ result.env }; OpenVarSet openVars{ result.openVars }; env.addActual( actual.env, openVars ); nextResults.emplace_back( result, expr, std::move(env), std::move(openVars), cost ); } ); } } // reset for next round results.swap( nextResults ); nextResults.clear(); } results.swap( finalResults ); return ! results.empty(); } // iterate each current subresult for ( ArgPack& result : results ) { if ( result.nextArg >= args.size() ) { // If run out of actuals, handle default values if ( ConstantExpr* cnstExpr = getDefaultValue( initializer ) ) { if ( Constant* cnst = dynamic_cast( cnstExpr->get_constant() ) ) { TypeEnvironment resultEnv = result.env; AssertionSet resultNeed = result.need, resultHave = result.have; if ( unify( formalType, cnst->get_type(), resultEnv, resultNeed, resultHave, result.openVars, indexer ) ) { nextResults.emplace_back( result, cnstExpr->clone(), std::move(resultEnv), std::move(resultNeed), std::move(resultHave), OpenVarSet{ result.openVars } ); } } } continue; } // Check each possible next argument for ( const Alternative& actual : args[result.nextArg] ) { addExplodedActual( result, actual.expr, actual.cost, nextResults, [formalType,&indexer,&actual]( ArgPack& result, Expression* expr, Cost cost, std::vector& nextResults ) { // attempt to unify actual with parameter TypeEnvironment resultEnv = result.env; AssertionSet resultNeed = result.need, resultHave = result.have; OpenVarSet resultOpenVars = result.openVars; resultEnv.addActual( actual.env, resultOpenVars ); Type* actualType = expr->get_result(); PRINT( std::cerr << "formal type is "; formalType->print( std::cerr ); std::cerr << std::endl << "actual type is "; actualType->print( std::cerr ); std::cerr << std::endl; ) if ( unify( formalType, actualType, resultEnv, resultNeed, resultHave, resultOpenVars, indexer ) ) { nextResults.emplace_back( result, expr->clone(), std::move(resultEnv), std::move(resultNeed), std::move(resultHave), std::move(resultOpenVars), cost ); } } ); } } // reset for next parameter results.swap( nextResults ); nextResults.clear(); return ! results.empty(); } template void AlternativeFinder::makeFunctionAlternatives( const Alternative& func, FunctionType* funcType, const std::vector< AlternativeFinder >& args, OutputIterator out ) { OpenVarSet funcOpenVars; AssertionSet funcNeed, funcHave; TypeEnvironment funcEnv; makeUnifiableVars( funcType, funcOpenVars, funcNeed ); // add all type variables as open variables now so that those not used in the parameter // list are still considered open. funcEnv.add( funcType->get_forall() ); if ( targetType && ! targetType->isVoid() && ! funcType->get_returnVals().empty() ) { // attempt to narrow based on expected target type Type* returnType = funcType->get_returnVals().front()->get_type(); if ( ! unify( returnType, targetType, funcEnv, funcNeed, funcHave, funcOpenVars, indexer ) ) { // unification failed, don't pursue this function alternative return; } } // iteratively build matches, one parameter at a time std::vector results{ ArgPack{ funcEnv, funcNeed, funcHave, funcOpenVars } }; std::vector nextResults{}; for ( DeclarationWithType* formal : funcType->get_parameters() ) { ObjectDecl* obj = strict_dynamic_cast< ObjectDecl* >( formal ); if ( ! instantiateArgument( obj->get_type(), obj->get_init(), args, results, nextResults, indexer ) ) return; } // filter out results that don't use all the arguments, and aren't variadic std::vector finalResults{}; if ( funcType->get_isVarArgs() ) { while ( ! results.empty() ) { // build combinations for all remaining arguments for ( ArgPack& result : results ) { // keep if used all arguments if ( result.nextArg >= args.size() ) { finalResults.emplace_back( std::move(result) ); continue; } // add each possible next argument for ( const Alternative& actual : args[result.nextArg] ) { TypeEnvironment env{ result.env }; OpenVarSet openVars{ result.openVars }; env.addActual( actual.env, openVars ); nextResults.emplace_back( result, actual.expr->clone(), std::move(env), std::move(openVars), actual.cost ); } } // reset for next round results.swap( nextResults ); nextResults.clear(); } } else { // filter out results that don't use all the arguments for ( ArgPack& result : results ) { if ( result.nextArg >= args.size() ) { finalResults.emplace_back( std::move(result) ); } } } // validate matching combos, add to final result list for ( ArgPack& result : finalResults ) { ApplicationExpr *appExpr = new ApplicationExpr( func.expr->clone() ); Alternative newAlt( appExpr, result.env, sumCost( result.actuals ) ); makeExprList( result.actuals, appExpr->get_args() ); PRINT( std::cerr << "instantiate function success: " << appExpr << std::endl; std::cerr << "need assertions:" << std::endl; printAssertionSet( result.need, std::cerr, 8 ); ) inferParameters( result.need, result.have, newAlt, result.openVars, out ); } } void AlternativeFinder::visit( UntypedExpr *untypedExpr ) { AlternativeFinder funcFinder( indexer, env ); funcFinder.findWithAdjustment( untypedExpr->get_function() ); // if there are no function alternatives, then proceeding is a waste of time. if ( funcFinder.alternatives.empty() ) return; std::vector< AlternativeFinder > argAlternatives; findSubExprs( untypedExpr->begin_args(), untypedExpr->end_args(), back_inserter( argAlternatives ) ); // take care of possible tuple assignments // if not tuple assignment, assignment is taken care of as a normal function call Tuples::handleTupleAssignment( *this, untypedExpr, argAlternatives ); // find function operators AlternativeFinder funcOpFinder( indexer, env ); NameExpr *opExpr = new NameExpr( "?()" ); try { funcOpFinder.findWithAdjustment( opExpr ); } catch( SemanticError &e ) { // it's ok if there aren't any defined function ops } PRINT( std::cerr << "known function ops:" << std::endl; printAlts( funcOpFinder.alternatives, std::cerr, 8 ); ) AltList candidates; SemanticError errors; for ( AltList::iterator func = funcFinder.alternatives.begin(); func != funcFinder.alternatives.end(); ++func ) { try { PRINT( std::cerr << "working on alternative: " << std::endl; func->print( std::cerr, 8 ); ) // check if the type is pointer to function if ( PointerType *pointer = dynamic_cast< PointerType* >( func->expr->get_result()->stripReferences() ) ) { if ( FunctionType *function = dynamic_cast< FunctionType* >( pointer->get_base() ) ) { Alternative newFunc( *func ); referenceToRvalueConversion( newFunc.expr ); makeFunctionAlternatives( newFunc, function, argAlternatives, std::back_inserter( candidates ) ); } } else if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( func->expr->get_result()->stripReferences() ) ) { // handle ftype (e.g. *? on function pointer) EqvClass eqvClass; if ( func->env.lookup( typeInst->get_name(), eqvClass ) && eqvClass.type ) { if ( FunctionType *function = dynamic_cast< FunctionType* >( eqvClass.type ) ) { Alternative newFunc( *func ); referenceToRvalueConversion( newFunc.expr ); makeFunctionAlternatives( newFunc, function, argAlternatives, std::back_inserter( candidates ) ); } // if } // if } } catch ( SemanticError &e ) { errors.append( e ); } } // for // try each function operator ?() with each function alternative if ( ! funcOpFinder.alternatives.empty() ) { // add function alternatives to front of argument list argAlternatives.insert( argAlternatives.begin(), std::move(funcFinder) ); for ( AltList::iterator funcOp = funcOpFinder.alternatives.begin(); funcOp != funcOpFinder.alternatives.end(); ++funcOp ) { try { // check if type is a pointer to function if ( PointerType* pointer = dynamic_cast( funcOp->expr->get_result()->stripReferences() ) ) { if ( FunctionType* function = dynamic_cast( pointer->get_base() ) ) { Alternative newFunc( *funcOp ); referenceToRvalueConversion( newFunc.expr ); makeFunctionAlternatives( newFunc, function, argAlternatives, std::back_inserter( candidates ) ); } } } catch ( SemanticError &e ) { errors.append( e ); } } } // Implement SFINAE; resolution errors are only errors if there aren't any non-erroneous resolutions if ( candidates.empty() && ! errors.isEmpty() ) { throw errors; } // compute conversionsion costs for ( AltList::iterator withFunc = candidates.begin(); withFunc != candidates.end(); ++withFunc ) { Cost cvtCost = computeApplicationConversionCost( *withFunc, indexer ); PRINT( ApplicationExpr *appExpr = strict_dynamic_cast< ApplicationExpr* >( withFunc->expr ); PointerType *pointer = strict_dynamic_cast< PointerType* >( appExpr->get_function()->get_result() ); FunctionType *function = strict_dynamic_cast< FunctionType* >( pointer->get_base() ); std::cerr << "Case +++++++++++++ " << appExpr->get_function() << std::endl; std::cerr << "formals are:" << std::endl; printAll( function->get_parameters(), std::cerr, 8 ); std::cerr << "actuals are:" << std::endl; printAll( appExpr->get_args(), std::cerr, 8 ); std::cerr << "bindings are:" << std::endl; withFunc->env.print( std::cerr, 8 ); std::cerr << "cost of conversion is:" << cvtCost << std::endl; ) if ( cvtCost != Cost::infinity ) { withFunc->cvtCost = cvtCost; alternatives.push_back( *withFunc ); } // if } // for candidates.clear(); candidates.splice( candidates.end(), alternatives ); findMinCost( candidates.begin(), candidates.end(), std::back_inserter( alternatives ) ); // function may return struct or union value, in which case we need to add alternatives for implicit // conversions to each of the anonymous members, must happen after findMinCost since anon conversions // are never the cheapest expression for ( const Alternative & alt : alternatives ) { addAnonConversions( alt ); } if ( alternatives.empty() && targetType && ! targetType->isVoid() ) { // xxx - this is a temporary hack. If resolution is unsuccessful with a target type, try again without a // target type, since it will sometimes succeed when it wouldn't easily with target type binding. For example, // forall( otype T ) lvalue T ?[?]( T *, ptrdiff_t ); // const char * x = "hello world"; // unsigned char ch = x[0]; // Fails with simple return type binding. First, T is bound to unsigned char, then (x: const char *) is unified // with unsigned char *, which fails because pointer base types must be unified exactly. The new resolver should // fix this issue in a more robust way. targetType = nullptr; visit( untypedExpr ); } } bool isLvalue( Expression *expr ) { // xxx - recurse into tuples? return expr->has_result() && ( expr->get_result()->get_lvalue() || dynamic_cast< ReferenceType * >( expr->get_result() ) ); } void AlternativeFinder::visit( AddressExpr *addressExpr ) { AlternativeFinder finder( indexer, env ); finder.find( addressExpr->get_arg() ); for ( std::list< Alternative >::iterator i = finder.alternatives.begin(); i != finder.alternatives.end(); ++i ) { if ( isLvalue( i->expr ) ) { alternatives.push_back( Alternative( new AddressExpr( i->expr->clone() ), i->env, i->cost ) ); } // if } // for } void AlternativeFinder::visit( LabelAddressExpr * expr ) { alternatives.push_back( Alternative( expr->clone(), env, Cost::zero) ); } Expression * restructureCast( Expression * argExpr, Type * toType ) { if ( argExpr->get_result()->size() > 1 && ! toType->isVoid() && ! dynamic_cast( toType ) ) { // Argument expression is a tuple and the target type is not void and not a reference type. // Cast each member of the tuple to its corresponding target type, producing the tuple of those // cast expressions. If there are more components of the tuple than components in the target type, // then excess components do not come out in the result expression (but UniqueExprs ensure that // side effects will still be done). if ( Tuples::maybeImpureIgnoreUnique( argExpr ) ) { // expressions which may contain side effects require a single unique instance of the expression. argExpr = new UniqueExpr( argExpr ); } std::list< Expression * > componentExprs; for ( unsigned int i = 0; i < toType->size(); i++ ) { // cast each component TupleIndexExpr * idx = new TupleIndexExpr( argExpr->clone(), i ); componentExprs.push_back( restructureCast( idx, toType->getComponent( i ) ) ); } delete argExpr; assert( componentExprs.size() > 0 ); // produce the tuple of casts return new TupleExpr( componentExprs ); } else { // handle normally return new CastExpr( argExpr, toType->clone() ); } } void AlternativeFinder::visit( CastExpr *castExpr ) { Type *& toType = castExpr->get_result(); assert( toType ); toType = resolveTypeof( toType, indexer ); SymTab::validateType( toType, &indexer ); adjustExprType( toType, env, indexer ); AlternativeFinder finder( indexer, env ); finder.targetType = toType; finder.findWithAdjustment( castExpr->get_arg() ); AltList candidates; for ( std::list< Alternative >::iterator i = finder.alternatives.begin(); i != finder.alternatives.end(); ++i ) { AssertionSet needAssertions, haveAssertions; OpenVarSet openVars; // It's possible that a cast can throw away some values in a multiply-valued expression. (An example is a // cast-to-void, which casts from one value to zero.) Figure out the prefix of the subexpression results // that are cast directly. The candidate is invalid if it has fewer results than there are types to cast // to. int discardedValues = i->expr->get_result()->size() - castExpr->get_result()->size(); if ( discardedValues < 0 ) continue; // xxx - may need to go into tuple types and extract relevant types and use unifyList. Note that currently, this does not // allow casting a tuple to an atomic type (e.g. (int)([1, 2, 3])) // unification run for side-effects unify( castExpr->get_result(), i->expr->get_result(), i->env, needAssertions, haveAssertions, openVars, indexer ); Cost thisCost = castCost( i->expr->get_result(), castExpr->get_result(), indexer, i->env ); if ( thisCost != Cost::infinity ) { // count one safe conversion for each value that is thrown away thisCost.incSafe( discardedValues ); Alternative newAlt( restructureCast( i->expr->clone(), toType ), i->env, i->cost, thisCost ); // xxx - this doesn't work at the moment, since inferParameters requires an ApplicationExpr as the alternative. // Once this works, it should be possible to infer parameters on a cast expression and specialize any function. // inferParameters( needAssertions, haveAssertions, newAlt, openVars, back_inserter( candidates ) ); candidates.emplace_back( std::move( newAlt ) ); } // if } // for // findMinCost selects the alternatives with the lowest "cost" members, but has the side effect of copying the // cvtCost member to the cost member (since the old cost is now irrelevant). Thus, calling findMinCost twice // selects first based on argument cost, then on conversion cost. AltList minArgCost; findMinCost( candidates.begin(), candidates.end(), std::back_inserter( minArgCost ) ); findMinCost( minArgCost.begin(), minArgCost.end(), std::back_inserter( alternatives ) ); } void AlternativeFinder::visit( VirtualCastExpr * castExpr ) { assertf( castExpr->get_result(), "Implicate virtual cast targets not yet supported." ); AlternativeFinder finder( indexer, env ); // don't prune here, since it's guaranteed all alternatives will have the same type // (giving the alternatives different types is half of the point of ConstructorExpr nodes) finder.findWithAdjustment( castExpr->get_arg(), false ); for ( Alternative & alt : finder.alternatives ) { alternatives.push_back( Alternative( new VirtualCastExpr( alt.expr->clone(), castExpr->get_result()->clone() ), alt.env, alt.cost ) ); } } void AlternativeFinder::visit( UntypedMemberExpr *memberExpr ) { AlternativeFinder funcFinder( indexer, env ); funcFinder.findWithAdjustment( memberExpr->get_aggregate() ); for ( AltList::const_iterator agg = funcFinder.alternatives.begin(); agg != funcFinder.alternatives.end(); ++agg ) { // it's okay for the aggregate expression to have reference type -- cast it to the base type to treat the aggregate as the referenced value std::unique_ptr aggrExpr( agg->expr->clone() ); Type * aggrType = aggrExpr->get_result(); if ( dynamic_cast< ReferenceType * >( aggrType ) ) { aggrType = aggrType->stripReferences(); aggrExpr.reset( new CastExpr( aggrExpr.release(), aggrType->clone() ) ); } // find member of the given type if ( StructInstType *structInst = dynamic_cast< StructInstType* >( aggrExpr->get_result() ) ) { addAggMembers( structInst, aggrExpr.get(), agg->cost, agg->env, memberExpr->get_member() ); } else if ( UnionInstType *unionInst = dynamic_cast< UnionInstType* >( aggrExpr->get_result() ) ) { addAggMembers( unionInst, aggrExpr.get(), agg->cost, agg->env, memberExpr->get_member() ); } else if ( TupleType * tupleType = dynamic_cast< TupleType * >( aggrExpr->get_result() ) ) { addTupleMembers( tupleType, aggrExpr.get(), agg->cost, agg->env, memberExpr->get_member() ); } // if } // for } void AlternativeFinder::visit( MemberExpr *memberExpr ) { alternatives.push_back( Alternative( memberExpr->clone(), env, Cost::zero ) ); } void AlternativeFinder::visit( NameExpr *nameExpr ) { std::list< DeclarationWithType* > declList; indexer.lookupId( nameExpr->get_name(), declList ); PRINT( std::cerr << "nameExpr is " << nameExpr->get_name() << std::endl; ) for ( std::list< DeclarationWithType* >::iterator i = declList.begin(); i != declList.end(); ++i ) { VariableExpr newExpr( *i, nameExpr->get_argName() ); alternatives.push_back( Alternative( newExpr.clone(), env, Cost::zero ) ); PRINT( std::cerr << "decl is "; (*i)->print( std::cerr ); std::cerr << std::endl; std::cerr << "newExpr is "; newExpr.print( std::cerr ); std::cerr << std::endl; ) renameTypes( alternatives.back().expr ); addAnonConversions( alternatives.back() ); // add anonymous member interpretations whenever an aggregate value type is seen as a name expression. } // for } void AlternativeFinder::visit( VariableExpr *variableExpr ) { // not sufficient to clone here, because variable's type may have changed // since the VariableExpr was originally created. alternatives.push_back( Alternative( new VariableExpr( variableExpr->get_var() ), env, Cost::zero ) ); } void AlternativeFinder::visit( ConstantExpr *constantExpr ) { alternatives.push_back( Alternative( constantExpr->clone(), env, Cost::zero ) ); } void AlternativeFinder::visit( SizeofExpr *sizeofExpr ) { if ( sizeofExpr->get_isType() ) { Type * newType = sizeofExpr->get_type()->clone(); alternatives.push_back( Alternative( new SizeofExpr( resolveTypeof( newType, indexer ) ), env, Cost::zero ) ); } else { // find all alternatives for the argument to sizeof AlternativeFinder finder( indexer, env ); finder.find( sizeofExpr->get_expr() ); // find the lowest cost alternative among the alternatives, otherwise ambiguous AltList winners; findMinCost( finder.alternatives.begin(), finder.alternatives.end(), back_inserter( winners ) ); if ( winners.size() != 1 ) { throw SemanticError( "Ambiguous expression in sizeof operand: ", sizeofExpr->get_expr() ); } // if // return the lowest cost alternative for the argument Alternative &choice = winners.front(); referenceToRvalueConversion( choice.expr ); alternatives.push_back( Alternative( new SizeofExpr( choice.expr->clone() ), choice.env, Cost::zero ) ); } // if } void AlternativeFinder::visit( AlignofExpr *alignofExpr ) { if ( alignofExpr->get_isType() ) { Type * newType = alignofExpr->get_type()->clone(); alternatives.push_back( Alternative( new AlignofExpr( resolveTypeof( newType, indexer ) ), env, Cost::zero ) ); } else { // find all alternatives for the argument to sizeof AlternativeFinder finder( indexer, env ); finder.find( alignofExpr->get_expr() ); // find the lowest cost alternative among the alternatives, otherwise ambiguous AltList winners; findMinCost( finder.alternatives.begin(), finder.alternatives.end(), back_inserter( winners ) ); if ( winners.size() != 1 ) { throw SemanticError( "Ambiguous expression in alignof operand: ", alignofExpr->get_expr() ); } // if // return the lowest cost alternative for the argument Alternative &choice = winners.front(); referenceToRvalueConversion( choice.expr ); alternatives.push_back( Alternative( new AlignofExpr( choice.expr->clone() ), choice.env, Cost::zero ) ); } // if } template< typename StructOrUnionType > void AlternativeFinder::addOffsetof( StructOrUnionType *aggInst, const std::string &name ) { std::list< Declaration* > members; aggInst->lookup( name, members ); for ( std::list< Declaration* >::const_iterator i = members.begin(); i != members.end(); ++i ) { if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType* >( *i ) ) { alternatives.push_back( Alternative( new OffsetofExpr( aggInst->clone(), dwt ), env, Cost::zero ) ); renameTypes( alternatives.back().expr ); } else { assert( false ); } } } void AlternativeFinder::visit( UntypedOffsetofExpr *offsetofExpr ) { AlternativeFinder funcFinder( indexer, env ); // xxx - resolveTypeof? if ( StructInstType *structInst = dynamic_cast< StructInstType* >( offsetofExpr->get_type() ) ) { addOffsetof( structInst, offsetofExpr->get_member() ); } else if ( UnionInstType *unionInst = dynamic_cast< UnionInstType* >( offsetofExpr->get_type() ) ) { addOffsetof( unionInst, offsetofExpr->get_member() ); } } void AlternativeFinder::visit( OffsetofExpr *offsetofExpr ) { alternatives.push_back( Alternative( offsetofExpr->clone(), env, Cost::zero ) ); } void AlternativeFinder::visit( OffsetPackExpr *offsetPackExpr ) { alternatives.push_back( Alternative( offsetPackExpr->clone(), env, Cost::zero ) ); } void AlternativeFinder::resolveAttr( DeclarationWithType *funcDecl, FunctionType *function, Type *argType, const TypeEnvironment &env ) { // assume no polymorphism // assume no implicit conversions assert( function->get_parameters().size() == 1 ); PRINT( std::cerr << "resolvAttr: funcDecl is "; funcDecl->print( std::cerr ); std::cerr << " argType is "; argType->print( std::cerr ); std::cerr << std::endl; ) if ( typesCompatibleIgnoreQualifiers( argType, function->get_parameters().front()->get_type(), indexer, env ) ) { alternatives.push_back( Alternative( new AttrExpr( new VariableExpr( funcDecl ), argType->clone() ), env, Cost::zero ) ); for ( std::list< DeclarationWithType* >::iterator i = function->get_returnVals().begin(); i != function->get_returnVals().end(); ++i ) { alternatives.back().expr->set_result( (*i)->get_type()->clone() ); } // for } // if } void AlternativeFinder::visit( AttrExpr *attrExpr ) { // assume no 'pointer-to-attribute' NameExpr *nameExpr = dynamic_cast< NameExpr* >( attrExpr->get_attr() ); assert( nameExpr ); std::list< DeclarationWithType* > attrList; indexer.lookupId( nameExpr->get_name(), attrList ); if ( attrExpr->get_isType() || attrExpr->get_expr() ) { for ( std::list< DeclarationWithType* >::iterator i = attrList.begin(); i != attrList.end(); ++i ) { // check if the type is function if ( FunctionType *function = dynamic_cast< FunctionType* >( (*i)->get_type() ) ) { // assume exactly one parameter if ( function->get_parameters().size() == 1 ) { if ( attrExpr->get_isType() ) { resolveAttr( *i, function, attrExpr->get_type(), env ); } else { AlternativeFinder finder( indexer, env ); finder.find( attrExpr->get_expr() ); for ( AltList::iterator choice = finder.alternatives.begin(); choice != finder.alternatives.end(); ++choice ) { if ( choice->expr->get_result()->size() == 1 ) { resolveAttr(*i, function, choice->expr->get_result(), choice->env ); } // fi } // for } // if } // if } // if } // for } else { for ( std::list< DeclarationWithType* >::iterator i = attrList.begin(); i != attrList.end(); ++i ) { VariableExpr newExpr( *i ); alternatives.push_back( Alternative( newExpr.clone(), env, Cost::zero ) ); renameTypes( alternatives.back().expr ); } // for } // if } void AlternativeFinder::visit( LogicalExpr *logicalExpr ) { AlternativeFinder firstFinder( indexer, env ); firstFinder.findWithAdjustment( logicalExpr->get_arg1() ); for ( AltList::const_iterator first = firstFinder.alternatives.begin(); first != firstFinder.alternatives.end(); ++first ) { AlternativeFinder secondFinder( indexer, first->env ); secondFinder.findWithAdjustment( logicalExpr->get_arg2() ); for ( AltList::const_iterator second = secondFinder.alternatives.begin(); second != secondFinder.alternatives.end(); ++second ) { LogicalExpr *newExpr = new LogicalExpr( first->expr->clone(), second->expr->clone(), logicalExpr->get_isAnd() ); alternatives.push_back( Alternative( newExpr, second->env, first->cost + second->cost ) ); } } } void AlternativeFinder::visit( ConditionalExpr *conditionalExpr ) { // find alternatives for condition AlternativeFinder firstFinder( indexer, env ); firstFinder.findWithAdjustment( conditionalExpr->get_arg1() ); for ( AltList::const_iterator first = firstFinder.alternatives.begin(); first != firstFinder.alternatives.end(); ++first ) { // find alternatives for true expression AlternativeFinder secondFinder( indexer, first->env ); secondFinder.findWithAdjustment( conditionalExpr->get_arg2() ); for ( AltList::const_iterator second = secondFinder.alternatives.begin(); second != secondFinder.alternatives.end(); ++second ) { // find alterantives for false expression AlternativeFinder thirdFinder( indexer, second->env ); thirdFinder.findWithAdjustment( conditionalExpr->get_arg3() ); for ( AltList::const_iterator third = thirdFinder.alternatives.begin(); third != thirdFinder.alternatives.end(); ++third ) { // unify true and false types, then infer parameters to produce new alternatives OpenVarSet openVars; AssertionSet needAssertions, haveAssertions; Alternative newAlt( 0, third->env, first->cost + second->cost + third->cost ); Type* commonType = nullptr; if ( unify( second->expr->get_result(), third->expr->get_result(), newAlt.env, needAssertions, haveAssertions, openVars, indexer, commonType ) ) { ConditionalExpr *newExpr = new ConditionalExpr( first->expr->clone(), second->expr->clone(), third->expr->clone() ); newExpr->set_result( commonType ? commonType : second->expr->get_result()->clone() ); // convert both options to the conditional result type newAlt.cost += computeExpressionConversionCost( newExpr->arg2, newExpr->result, indexer, newAlt.env ); newAlt.cost += computeExpressionConversionCost( newExpr->arg3, newExpr->result, indexer, newAlt.env ); newAlt.expr = newExpr; inferParameters( needAssertions, haveAssertions, newAlt, openVars, back_inserter( alternatives ) ); } // if } // for } // for } // for } void AlternativeFinder::visit( CommaExpr *commaExpr ) { TypeEnvironment newEnv( env ); Expression *newFirstArg = resolveInVoidContext( commaExpr->get_arg1(), indexer, newEnv ); AlternativeFinder secondFinder( indexer, newEnv ); secondFinder.findWithAdjustment( commaExpr->get_arg2() ); for ( AltList::const_iterator alt = secondFinder.alternatives.begin(); alt != secondFinder.alternatives.end(); ++alt ) { alternatives.push_back( Alternative( new CommaExpr( newFirstArg->clone(), alt->expr->clone() ), alt->env, alt->cost ) ); } // for delete newFirstArg; } void AlternativeFinder::visit( RangeExpr * rangeExpr ) { // resolve low and high, accept alternatives whose low and high types unify AlternativeFinder firstFinder( indexer, env ); firstFinder.findWithAdjustment( rangeExpr->get_low() ); for ( AltList::const_iterator first = firstFinder.alternatives.begin(); first != firstFinder.alternatives.end(); ++first ) { AlternativeFinder secondFinder( indexer, first->env ); secondFinder.findWithAdjustment( rangeExpr->get_high() ); for ( AltList::const_iterator second = secondFinder.alternatives.begin(); second != secondFinder.alternatives.end(); ++second ) { OpenVarSet openVars; AssertionSet needAssertions, haveAssertions; Alternative newAlt( 0, second->env, first->cost + second->cost ); Type* commonType = nullptr; if ( unify( first->expr->get_result(), second->expr->get_result(), newAlt.env, needAssertions, haveAssertions, openVars, indexer, commonType ) ) { RangeExpr *newExpr = new RangeExpr( first->expr->clone(), second->expr->clone() ); newExpr->set_result( commonType ? commonType : first->expr->get_result()->clone() ); newAlt.expr = newExpr; inferParameters( needAssertions, haveAssertions, newAlt, openVars, back_inserter( alternatives ) ); } // if } // for } // for } void AlternativeFinder::visit( UntypedTupleExpr *tupleExpr ) { std::list< AlternativeFinder > subExprAlternatives; findSubExprs( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end(), back_inserter( subExprAlternatives ) ); std::list< AltList > possibilities; // TODO re-write to use iterative method combos( subExprAlternatives.begin(), subExprAlternatives.end(), back_inserter( possibilities ) ); for ( std::list< AltList >::const_iterator i = possibilities.begin(); i != possibilities.end(); ++i ) { std::list< Expression * > exprs; makeExprList( *i, exprs ); TypeEnvironment compositeEnv; simpleCombineEnvironments( i->begin(), i->end(), compositeEnv ); alternatives.push_back( Alternative( new TupleExpr( exprs ) , compositeEnv, sumCost( *i ) ) ); } // for } void AlternativeFinder::visit( TupleExpr *tupleExpr ) { alternatives.push_back( Alternative( tupleExpr->clone(), env, Cost::zero ) ); } void AlternativeFinder::visit( ImplicitCopyCtorExpr * impCpCtorExpr ) { alternatives.push_back( Alternative( impCpCtorExpr->clone(), env, Cost::zero ) ); } void AlternativeFinder::visit( ConstructorExpr * ctorExpr ) { AlternativeFinder finder( indexer, env ); // don't prune here, since it's guaranteed all alternatives will have the same type // (giving the alternatives different types is half of the point of ConstructorExpr nodes) finder.findWithAdjustment( ctorExpr->get_callExpr(), false ); for ( Alternative & alt : finder.alternatives ) { alternatives.push_back( Alternative( new ConstructorExpr( alt.expr->clone() ), alt.env, alt.cost ) ); } } void AlternativeFinder::visit( TupleIndexExpr *tupleExpr ) { alternatives.push_back( Alternative( tupleExpr->clone(), env, Cost::zero ) ); } void AlternativeFinder::visit( TupleAssignExpr *tupleAssignExpr ) { alternatives.push_back( Alternative( tupleAssignExpr->clone(), env, Cost::zero ) ); } void AlternativeFinder::visit( UniqueExpr *unqExpr ) { AlternativeFinder finder( indexer, env ); finder.findWithAdjustment( unqExpr->get_expr() ); for ( Alternative & alt : finder.alternatives ) { // ensure that the id is passed on to the UniqueExpr alternative so that the expressions are "linked" UniqueExpr * newUnqExpr = new UniqueExpr( alt.expr->clone(), unqExpr->get_id() ); alternatives.push_back( Alternative( newUnqExpr, alt.env, alt.cost ) ); } } void AlternativeFinder::visit( StmtExpr *stmtExpr ) { StmtExpr * newStmtExpr = stmtExpr->clone(); ResolvExpr::resolveStmtExpr( newStmtExpr, indexer ); // xxx - this env is almost certainly wrong, and needs to somehow contain the combined environments from all of the statements in the stmtExpr... alternatives.push_back( Alternative( newStmtExpr, env, Cost::zero ) ); } void AlternativeFinder::visit( UntypedInitExpr *initExpr ) { // handle each option like a cast AltList candidates; PRINT( std::cerr << "untyped init expr: " << initExpr << std::endl; ) // O(N^2) checks of d-types with e-types for ( InitAlternative & initAlt : initExpr->get_initAlts() ) { Type * toType = resolveTypeof( initAlt.type, indexer ); SymTab::validateType( toType, &indexer ); adjustExprType( toType, env, indexer ); // Ideally the call to findWithAdjustment could be moved out of the loop, but unfortunately it currently has to occur inside or else // polymorphic return types are not properly bound to the initialization type, since return type variables are only open for the duration of resolving // the UntypedExpr. This is only actually an issue in initialization contexts that allow more than one possible initialization type, but it is still suboptimal. AlternativeFinder finder( indexer, env ); finder.targetType = toType; finder.findWithAdjustment( initExpr->get_expr() ); for ( Alternative & alt : finder.get_alternatives() ) { TypeEnvironment newEnv( alt.env ); AssertionSet needAssertions, haveAssertions; OpenVarSet openVars; // find things in env that don't have a "representative type" and claim those are open vars? PRINT( std::cerr << " @ " << toType << " " << initAlt.designation << std::endl; ) // It's possible that a cast can throw away some values in a multiply-valued expression. (An example is a // cast-to-void, which casts from one value to zero.) Figure out the prefix of the subexpression results // that are cast directly. The candidate is invalid if it has fewer results than there are types to cast // to. int discardedValues = alt.expr->get_result()->size() - toType->size(); if ( discardedValues < 0 ) continue; // xxx - may need to go into tuple types and extract relevant types and use unifyList. Note that currently, this does not // allow casting a tuple to an atomic type (e.g. (int)([1, 2, 3])) // unification run for side-effects unify( toType, alt.expr->get_result(), newEnv, needAssertions, haveAssertions, openVars, indexer ); // xxx - do some inspecting on this line... why isn't result bound to initAlt.type?? Cost thisCost = castCost( alt.expr->get_result(), toType, indexer, newEnv ); if ( thisCost != Cost::infinity ) { // count one safe conversion for each value that is thrown away thisCost.incSafe( discardedValues ); candidates.push_back( Alternative( new InitExpr( restructureCast( alt.expr->clone(), toType ), initAlt.designation->clone() ), newEnv, alt.cost, thisCost ) ); } } } // findMinCost selects the alternatives with the lowest "cost" members, but has the side effect of copying the // cvtCost member to the cost member (since the old cost is now irrelevant). Thus, calling findMinCost twice // selects first based on argument cost, then on conversion cost. AltList minArgCost; findMinCost( candidates.begin(), candidates.end(), std::back_inserter( minArgCost ) ); findMinCost( minArgCost.begin(), minArgCost.end(), std::back_inserter( alternatives ) ); } } // namespace ResolvExpr // Local Variables: // // tab-width: 4 // // mode: c++ // // compile-command: "make install" // // End: //