// // 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.h -- // // Author : Richard C. Bilson // Created On : Sat May 16 23:56:12 2015 // Last Modified By : Andrew Beach // Last Modified On : Wed Jul 26 11:24:00 2017 // Update Count : 4 // #pragma once #include // for copy #include // for list #include // for string #include "Alternative.h" // for AltList, Alternative #include "ExplodedActual.h" // for ExplodedActual #include "ResolvExpr/Cost.h" // for Cost, Cost::infinity #include "ResolvExpr/TypeEnvironment.h" // for AssertionSet, OpenVarSet #include "SynTree/Visitor.h" // for Visitor #include "SynTree/SynTree.h" // for Visitor Nodes namespace SymTab { class Indexer; } // namespace SymTab namespace ResolvExpr { struct ArgPack; /// First index is which argument, second index is which alternative for that argument, /// third index is which exploded element of that alternative using ExplodedArgs = std::vector< std::vector< ExplodedActual > >; class AlternativeFinder : public Visitor { public: AlternativeFinder( const SymTab::Indexer &indexer, const TypeEnvironment &env ); AlternativeFinder( const AlternativeFinder& o ) : indexer(o.indexer), alternatives(o.alternatives), env(o.env), targetType(o.targetType) {} AlternativeFinder( AlternativeFinder&& o ) : indexer(o.indexer), alternatives(std::move(o.alternatives)), env(o.env), targetType(o.targetType) {} AlternativeFinder& operator= ( const AlternativeFinder& o ) { if (&o == this) return *this; // horrific nasty hack to rebind references... alternatives.~AltList(); new(this) AlternativeFinder(o); return *this; } AlternativeFinder& operator= ( AlternativeFinder&& o ) { if (&o == this) return *this; // horrific nasty hack to rebind references... alternatives.~AltList(); new(this) AlternativeFinder(std::move(o)); return *this; } void find( Expression *expr, bool adjust = false, bool prune = true, bool failFast = true ); /// Calls find with the adjust flag set; adjustment turns array and function types into equivalent pointer types void findWithAdjustment( Expression *expr ); /// Calls find with the adjust flag set and prune flag unset; pruning ensures there is at most one alternative per result type void findWithoutPrune( Expression *expr ); /// Calls find with the adjust and prune flags set, failFast flags unset; fail fast ensures that there is at least one resulting alternative void maybeFind( Expression *expr ); AltList &get_alternatives() { return alternatives; } // make this look like an STL container so that we can apply generic algorithms typedef Alternative value_type; typedef AltList::iterator iterator; typedef AltList::const_iterator const_iterator; AltList::iterator begin() { return alternatives.begin(); } AltList::iterator end() { return alternatives.end(); } AltList::const_iterator begin() const { return alternatives.begin(); } AltList::const_iterator end() const { return alternatives.end(); } const SymTab::Indexer &get_indexer() const { return indexer; } const TypeEnvironment &get_environ() const { return env; } /// Runs a new alternative finder on each element in [begin, end) /// and writes each alternative finder to out. template< typename InputIterator, typename OutputIterator > void findSubExprs( InputIterator begin, InputIterator end, OutputIterator out ); private: virtual void visit( ApplicationExpr *applicationExpr ); virtual void visit( UntypedExpr *untypedExpr ); virtual void visit( AddressExpr *addressExpr ); virtual void visit( LabelAddressExpr *labelExpr ); virtual void visit( CastExpr *castExpr ); virtual void visit( VirtualCastExpr *castExpr ); virtual void visit( UntypedMemberExpr *memberExpr ); virtual void visit( MemberExpr *memberExpr ); virtual void visit( NameExpr *variableExpr ); virtual void visit( VariableExpr *variableExpr ); virtual void visit( ConstantExpr *constantExpr ); virtual void visit( SizeofExpr *sizeofExpr ); virtual void visit( AlignofExpr *alignofExpr ); virtual void visit( UntypedOffsetofExpr *offsetofExpr ); virtual void visit( OffsetofExpr *offsetofExpr ); virtual void visit( OffsetPackExpr *offsetPackExpr ); virtual void visit( AttrExpr *attrExpr ); virtual void visit( LogicalExpr *logicalExpr ); virtual void visit( ConditionalExpr *conditionalExpr ); virtual void visit( CommaExpr *commaExpr ); virtual void visit( ImplicitCopyCtorExpr * impCpCtorExpr ); virtual void visit( ConstructorExpr * ctorExpr ); virtual void visit( RangeExpr * rangeExpr ); virtual void visit( UntypedTupleExpr *tupleExpr ); virtual void visit( TupleExpr *tupleExpr ); virtual void visit( TupleIndexExpr *tupleExpr ); virtual void visit( TupleAssignExpr *tupleExpr ); virtual void visit( UniqueExpr *unqExpr ); virtual void visit( StmtExpr *stmtExpr ); virtual void visit( UntypedInitExpr *initExpr ); /// Adds alternatives for anonymous members void addAnonConversions( const Alternative & alt ); /// Adds alternatives for member expressions, given the aggregate, conversion cost for that aggregate, and name of the member template< typename StructOrUnionType > void addAggMembers( StructOrUnionType *aggInst, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member ); /// Adds alternatives for member expressions where the left side has tuple type void addTupleMembers( TupleType * tupleType, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member ); /// Adds alternatives for offsetof expressions, given the base type and name of the member template< typename StructOrUnionType > void addOffsetof( StructOrUnionType *aggInst, const std::string &name ); /// Takes a final result and checks if its assertions can be satisfied template void validateFunctionAlternative( const Alternative &func, ArgPack& result, const std::vector& results, OutputIterator out ); /// Finds matching alternatives for a function, given a set of arguments template void makeFunctionAlternatives( const Alternative &func, FunctionType *funcType, const ExplodedArgs& args, OutputIterator out ); /// Checks if assertion parameters match for a new alternative template< typename OutputIterator > void inferParameters( const AssertionSet &need, AssertionSet &have, const Alternative &newAlt, OpenVarSet &openVars, OutputIterator out ); void resolveAttr( DeclarationWithType *funcDecl, FunctionType *function, Type *argType, const TypeEnvironment &env ); const SymTab::Indexer &indexer; AltList alternatives; const TypeEnvironment &env; Type * targetType = nullptr; }; // AlternativeFinder Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer, TypeEnvironment &env ); void referenceToRvalueConversion( Expression *& expr ); template< typename InputIterator, typename OutputIterator > void findMinCost( InputIterator begin, InputIterator end, OutputIterator out ) { AltList alternatives; // select the alternatives that have the minimum parameter cost Cost minCost = Cost::infinity; for ( InputIterator i = begin; i != end; ++i ) { if ( i->cost < minCost ) { minCost = i->cost; i->cost = i->cvtCost; alternatives.clear(); alternatives.push_back( *i ); } else if ( i->cost == minCost ) { i->cost = i->cvtCost; alternatives.push_back( *i ); } } std::copy( alternatives.begin(), alternatives.end(), out ); } Cost sumCost( const AltList &in ); void printAlts( const AltList &list, std::ostream &os, unsigned int indentAmt = 0 ); template< typename InputIterator > void simpleCombineEnvironments( InputIterator begin, InputIterator end, TypeEnvironment &result ) { while ( begin != end ) { result.simpleCombine( (*begin++).env ); } } } // namespace ResolvExpr // Local Variables: // // tab-width: 4 // // mode: c++ // // compile-command: "make install" // // End: //