Changes in / [9f10d1f2:88ef2af]
- Location:
- src
- Files:
-
- 2 added
- 13 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Makefile.in
r9f10d1f2 r88ef2af 210 210 ResolvExpr/driver_cfa_cpp-TypeEnvironment.$(OBJEXT) \ 211 211 ResolvExpr/driver_cfa_cpp-CurrentObject.$(OBJEXT) \ 212 ResolvExpr/driver_cfa_cpp-ExplodedActual.$(OBJEXT) \ 212 213 SymTab/driver_cfa_cpp-Indexer.$(OBJEXT) \ 213 214 SymTab/driver_cfa_cpp-Mangler.$(OBJEXT) \ … … 511 512 ResolvExpr/FindOpenVars.cc ResolvExpr/PolyCost.cc \ 512 513 ResolvExpr/Occurs.cc ResolvExpr/TypeEnvironment.cc \ 513 ResolvExpr/CurrentObject.cc SymTab/Indexer.cc \ 514 SymTab/Mangler.cc SymTab/Validate.cc SymTab/FixFunction.cc \ 515 SymTab/ImplementationType.cc SymTab/TypeEquality.cc \ 516 SymTab/Autogen.cc SynTree/Type.cc SynTree/VoidType.cc \ 517 SynTree/BasicType.cc SynTree/PointerType.cc \ 518 SynTree/ArrayType.cc SynTree/ReferenceType.cc \ 519 SynTree/FunctionType.cc SynTree/ReferenceToType.cc \ 520 SynTree/TupleType.cc SynTree/TypeofType.cc SynTree/AttrType.cc \ 514 ResolvExpr/CurrentObject.cc ResolvExpr/ExplodedActual.cc \ 515 SymTab/Indexer.cc SymTab/Mangler.cc SymTab/Validate.cc \ 516 SymTab/FixFunction.cc SymTab/ImplementationType.cc \ 517 SymTab/TypeEquality.cc SymTab/Autogen.cc SynTree/Type.cc \ 518 SynTree/VoidType.cc SynTree/BasicType.cc \ 519 SynTree/PointerType.cc SynTree/ArrayType.cc \ 520 SynTree/ReferenceType.cc SynTree/FunctionType.cc \ 521 SynTree/ReferenceToType.cc SynTree/TupleType.cc \ 522 SynTree/TypeofType.cc SynTree/AttrType.cc \ 521 523 SynTree/VarArgsType.cc SynTree/ZeroOneType.cc \ 522 524 SynTree/Constant.cc SynTree/Expression.cc SynTree/TupleExpr.cc \ … … 825 827 ResolvExpr/$(am__dirstamp) \ 826 828 ResolvExpr/$(DEPDIR)/$(am__dirstamp) 829 ResolvExpr/driver_cfa_cpp-ExplodedActual.$(OBJEXT): \ 830 ResolvExpr/$(am__dirstamp) \ 831 ResolvExpr/$(DEPDIR)/$(am__dirstamp) 827 832 SymTab/$(am__dirstamp): 828 833 @$(MKDIR_P) SymTab … … 1022 1027 @AMDEP_TRUE@@am__include@ @am__quote@ResolvExpr/$(DEPDIR)/driver_cfa_cpp-ConversionCost.Po@am__quote@ 1023 1028 @AMDEP_TRUE@@am__include@ @am__quote@ResolvExpr/$(DEPDIR)/driver_cfa_cpp-CurrentObject.Po@am__quote@ 1029 @AMDEP_TRUE@@am__include@ @am__quote@ResolvExpr/$(DEPDIR)/driver_cfa_cpp-ExplodedActual.Po@am__quote@ 1024 1030 @AMDEP_TRUE@@am__include@ @am__quote@ResolvExpr/$(DEPDIR)/driver_cfa_cpp-FindOpenVars.Po@am__quote@ 1025 1031 @AMDEP_TRUE@@am__include@ @am__quote@ResolvExpr/$(DEPDIR)/driver_cfa_cpp-Occurs.Po@am__quote@ … … 1964 1970 @am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(driver_cfa_cpp_CXXFLAGS) $(CXXFLAGS) -c -o ResolvExpr/driver_cfa_cpp-CurrentObject.obj `if test -f 'ResolvExpr/CurrentObject.cc'; then $(CYGPATH_W) 'ResolvExpr/CurrentObject.cc'; else $(CYGPATH_W) '$(srcdir)/ResolvExpr/CurrentObject.cc'; fi` 1965 1971 1972 ResolvExpr/driver_cfa_cpp-ExplodedActual.o: ResolvExpr/ExplodedActual.cc 1973 @am__fastdepCXX_TRUE@ $(AM_V_CXX)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(driver_cfa_cpp_CXXFLAGS) $(CXXFLAGS) -MT ResolvExpr/driver_cfa_cpp-ExplodedActual.o -MD -MP -MF ResolvExpr/$(DEPDIR)/driver_cfa_cpp-ExplodedActual.Tpo -c -o ResolvExpr/driver_cfa_cpp-ExplodedActual.o `test -f 'ResolvExpr/ExplodedActual.cc' || echo '$(srcdir)/'`ResolvExpr/ExplodedActual.cc 1974 @am__fastdepCXX_TRUE@ $(AM_V_at)$(am__mv) ResolvExpr/$(DEPDIR)/driver_cfa_cpp-ExplodedActual.Tpo ResolvExpr/$(DEPDIR)/driver_cfa_cpp-ExplodedActual.Po 1975 @AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(AM_V_CXX)source='ResolvExpr/ExplodedActual.cc' object='ResolvExpr/driver_cfa_cpp-ExplodedActual.o' libtool=no @AMDEPBACKSLASH@ 1976 @AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ 1977 @am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(driver_cfa_cpp_CXXFLAGS) $(CXXFLAGS) -c -o ResolvExpr/driver_cfa_cpp-ExplodedActual.o `test -f 'ResolvExpr/ExplodedActual.cc' || echo '$(srcdir)/'`ResolvExpr/ExplodedActual.cc 1978 1979 ResolvExpr/driver_cfa_cpp-ExplodedActual.obj: ResolvExpr/ExplodedActual.cc 1980 @am__fastdepCXX_TRUE@ $(AM_V_CXX)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(driver_cfa_cpp_CXXFLAGS) $(CXXFLAGS) -MT ResolvExpr/driver_cfa_cpp-ExplodedActual.obj -MD -MP -MF ResolvExpr/$(DEPDIR)/driver_cfa_cpp-ExplodedActual.Tpo -c -o ResolvExpr/driver_cfa_cpp-ExplodedActual.obj `if test -f 'ResolvExpr/ExplodedActual.cc'; then $(CYGPATH_W) 'ResolvExpr/ExplodedActual.cc'; else $(CYGPATH_W) '$(srcdir)/ResolvExpr/ExplodedActual.cc'; fi` 1981 @am__fastdepCXX_TRUE@ $(AM_V_at)$(am__mv) ResolvExpr/$(DEPDIR)/driver_cfa_cpp-ExplodedActual.Tpo ResolvExpr/$(DEPDIR)/driver_cfa_cpp-ExplodedActual.Po 1982 @AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(AM_V_CXX)source='ResolvExpr/ExplodedActual.cc' object='ResolvExpr/driver_cfa_cpp-ExplodedActual.obj' libtool=no @AMDEPBACKSLASH@ 1983 @AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ 1984 @am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(driver_cfa_cpp_CXXFLAGS) $(CXXFLAGS) -c -o ResolvExpr/driver_cfa_cpp-ExplodedActual.obj `if test -f 'ResolvExpr/ExplodedActual.cc'; then $(CYGPATH_W) 'ResolvExpr/ExplodedActual.cc'; else $(CYGPATH_W) '$(srcdir)/ResolvExpr/ExplodedActual.cc'; fi` 1985 1966 1986 SymTab/driver_cfa_cpp-Indexer.o: SymTab/Indexer.cc 1967 1987 @am__fastdepCXX_TRUE@ $(AM_V_CXX)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(driver_cfa_cpp_CXXFLAGS) $(CXXFLAGS) -MT SymTab/driver_cfa_cpp-Indexer.o -MD -MP -MF SymTab/$(DEPDIR)/driver_cfa_cpp-Indexer.Tpo -c -o SymTab/driver_cfa_cpp-Indexer.o `test -f 'SymTab/Indexer.cc' || echo '$(srcdir)/'`SymTab/Indexer.cc -
src/Parser/DeclarationNode.cc
r9f10d1f2 r88ef2af 10 10 // Created On : Sat May 16 12:34:05 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sat Sep 23 18:16:48201713 // Update Count : 10 2412 // Last Modified On : Mon Nov 20 09:21:52 2017 13 // Update Count : 1031 14 14 // 15 15 … … 509 509 510 510 DeclarationNode * DeclarationNode::addQualifiers( DeclarationNode * q ) { 511 if ( ! q ) { delete q; return this; } 511 if ( ! q ) { delete q; return this; } // empty qualifier 512 512 513 513 checkSpecifiers( q ); 514 514 copySpecifiers( q ); 515 515 516 if ( ! q->type ) { 517 delete q; 518 return this; 519 } // if 516 if ( ! q->type ) { delete q; return this; } 520 517 521 518 if ( ! type ) { 522 type = q->type; // reuse thisstructure519 type = q->type; // reuse structure 523 520 q->type = nullptr; 524 521 delete q; … … 526 523 } // if 527 524 528 if ( q->type->forall ) { 529 if ( type->forall ) { 530 type->forall->appendList( q->type->forall ); 525 if ( q->type->forall ) { // forall qualifier ? 526 if ( type->forall ) { // polymorphic routine ? 527 type->forall->appendList( q->type->forall ); // augment forall qualifier 531 528 } else { 532 if ( type->kind == TypeData::Aggregate ) { 533 type->aggregate.params = q->type->forall; 534 // change implicit typedef from TYPEDEFname to TYPEGENname 535 typedefTable.changeKind( *type->aggregate.name, TypedefTable::TG ); 536 } else { 537 type->forall = q->type->forall; 529 if ( type->kind == TypeData::Aggregate ) { // struct/union ? 530 if ( type->aggregate.params ) { // polymorphic ? 531 type->aggregate.params->appendList( q->type->forall ); // augment forall qualifier 532 } else { // not polymorphic 533 type->aggregate.params = q->type->forall; // make polymorphic type 534 // change implicit typedef from TYPEDEFname to TYPEGENname 535 typedefTable.changeKind( *type->aggregate.name, TypedefTable::TG ); 536 } // if 537 } else { // not polymorphic 538 type->forall = q->type->forall; // make polymorphic routine 538 539 } // if 539 540 } // if 540 q->type->forall = nullptr; 541 q->type->forall = nullptr; // forall qualifier moved 541 542 } // if 542 543 -
src/Parser/parser.yy
r9f10d1f2 r88ef2af 10 10 // Created On : Sat Sep 1 20:22:55 2001 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Oct 25 12:28:54201713 // Update Count : 2 89312 // Last Modified On : Mon Nov 20 09:45:36 2017 13 // Update Count : 2945 14 14 // 15 15 … … 114 114 } // for 115 115 } // distExt 116 117 // There is an ambiguity for inline generic-routine return-types and generic routines. 118 // forall( otype T ) struct S { int i; } bar( T ) {} 119 // Does the forall bind to the struct or the routine, and how would it be possible to explicitly specify the binding. 120 // forall( otype T ) struct S { int T; } forall( otype W ) bar( W ) {} 121 122 void rebindForall( DeclarationNode * declSpec, DeclarationNode * funcDecl ) { 123 if ( declSpec->type->kind == TypeData::Aggregate ) { // return is aggregate definition 124 funcDecl->type->forall = declSpec->type->aggregate.params; // move forall from aggregate to function type 125 declSpec->type->aggregate.params = nullptr; 126 } // if 127 } // rebindForall 116 128 117 129 bool forall = false; // aggregate have one or more forall qualifiers ? … … 348 360 349 361 350 // Handle single shift/reduce conflict for dangling else by shifting the ELSE token. For example, this string 351 // is ambiguous: 352 // .---------. matches IF '(' comma_expression ')' statement . (reduce) 353 // if ( C ) S1 else S2 354 // `-----------------' matches IF '(' comma_expression ')' statement . (shift) ELSE statement */ 362 // Handle shift/reduce conflict for dangling else by shifting the ELSE token. For example, this string is ambiguous: 363 // .---------. matches IF '(' comma_expression ')' statement . (reduce) 364 // if ( C ) S1 else S2 365 // `-----------------' matches IF '(' comma_expression ')' statement . (shift) ELSE statement */ 355 366 // Similar issues exit with the waitfor statement. 356 367 … … 361 372 %precedence TIMEOUT // token precedence for start of TIMEOUT in WAITFOR statement 362 373 %precedence ELSE // token precedence for start of else clause in IF/WAITFOR statement 374 375 // Handle shift/reduce conflict for generic type by shifting the '(' token. For example, this string is ambiguous: 376 // forall( otype T ) struct Foo { T v; }; 377 // .-----. matches pointer to function returning a generic (which is impossible without a type) 378 // Foo ( *fp )( int ); 379 // `---' matches start of TYPEGENname '(' 380 // Must be: 381 // Foo( int ) ( *fp )( int ); 382 383 // Order of these lines matters (low-to-high precedence). 384 %precedence TYPEGENname 385 %precedence '(' 363 386 364 387 %locations // support location tracking for error messages … … 1765 1788 1766 1789 typegen_name: // CFA 1767 TYPEGENname '(' ')' 1790 TYPEGENname 1791 { $$ = DeclarationNode::newFromTypeGen( $1, nullptr ); } 1792 | TYPEGENname '(' ')' 1768 1793 { $$ = DeclarationNode::newFromTypeGen( $1, nullptr ); } 1769 1794 | TYPEGENname '(' type_list ')' … … 1809 1834 } 1810 1835 | aggregate_key attribute_list_opt typegen_name // CFA 1811 { $$ = $3->addQualifiers( $2 ); } 1836 { 1837 // Create new generic declaration with same name as previous forward declaration, where the IDENTIFIER is 1838 // switched to a TYPEGENname. Link any generic arguments from typegen_name to new generic declaration and 1839 // delete newFromTypeGen. 1840 $$ = DeclarationNode::newAggregate( $1, $3->type->symbolic.name, $3->type->symbolic.actuals, nullptr, false )->addQualifiers( $2 ); 1841 $3->type->symbolic.name = nullptr; 1842 $3->type->symbolic.actuals = nullptr; 1843 delete $3; 1844 } 1812 1845 ; 1813 1846 … … 2380 2413 | declaration_specifier function_declarator with_clause_opt compound_statement 2381 2414 { 2415 rebindForall( $1, $2 ); 2382 2416 typedefTable.addToEnclosingScope( TypedefTable::ID ); 2383 2417 typedefTable.leaveScope(); … … 2406 2440 | declaration_specifier KR_function_declarator KR_declaration_list_opt with_clause_opt compound_statement 2407 2441 { 2442 rebindForall( $1, $2 ); 2408 2443 typedefTable.addToEnclosingScope( TypedefTable::ID ); 2409 2444 typedefTable.leaveScope(); -
src/ResolvExpr/Alternative.cc
r9f10d1f2 r88ef2af 18 18 #include <ostream> // for operator<<, ostream, basic_o... 19 19 #include <string> // for operator<<, char_traits, string 20 #include <utility> // for move 20 21 21 22 #include "Common/utility.h" // for maybeClone … … 81 82 os << std::endl; 82 83 } 84 85 void splice( AltList& dst, AltList& src ) { 86 dst.reserve( dst.size() + src.size() ); 87 for ( Alternative& alt : src ) { 88 dst.push_back( std::move(alt) ); 89 } 90 src.clear(); 91 } 92 93 void spliceBegin( AltList& dst, AltList& src ) { 94 splice( src, dst ); 95 dst.swap( src ); 96 } 97 83 98 } // namespace ResolvExpr 84 99 -
src/ResolvExpr/Alternative.h
r9f10d1f2 r88ef2af 17 17 18 18 #include <iosfwd> // for ostream 19 #include < list> // for list19 #include <vector> // for vector 20 20 21 21 #include "Cost.h" // for Cost … … 25 25 26 26 namespace ResolvExpr { 27 struct Alternative;28 29 typedef std::list< Alternative > AltList;30 31 27 struct Alternative { 32 28 Alternative(); … … 46 42 TypeEnvironment env; 47 43 }; 44 45 typedef std::vector< Alternative > AltList; 46 47 /// Moves all elements from src to the end of dst 48 void splice( AltList& dst, AltList& src ); 49 50 /// Moves all elements from src to the beginning of dst 51 void spliceBegin( AltList& dst, AltList& src ); 48 52 } // namespace ResolvExpr 49 53 -
src/ResolvExpr/AlternativeFinder.cc
r9f10d1f2 r88ef2af 16 16 #include <algorithm> // for copy 17 17 #include <cassert> // for strict_dynamic_cast, assert, assertf 18 #include <cstddef> // for size_t 18 19 #include <iostream> // for operator<<, cerr, ostream, endl 19 20 #include <iterator> // for back_insert_iterator, back_inserter 20 21 #include <list> // for _List_iterator, list, _List_const_... 21 22 #include <map> // for _Rb_tree_iterator, map, _Rb_tree_c... 22 #include <memory> // for allocator_traits<>::value_type 23 #include <memory> // for allocator_traits<>::value_type, unique_ptr 23 24 #include <utility> // for pair 24 25 #include <vector> // for vector … … 29 30 #include "Common/utility.h" // for deleteAll, printAll, CodeLocation 30 31 #include "Cost.h" // for Cost, Cost::zero, operator<<, Cost... 32 #include "ExplodedActual.h" // for ExplodedActual 31 33 #include "InitTweak/InitTweak.h" // for getFunctionName 32 34 #include "RenameVars.h" // for RenameVars, global_renamer … … 50 52 #define PRINT( text ) if ( resolvep ) { text } 51 53 //#define DEBUG_COST 54 55 using std::move; 56 57 /// copies any copyable type 58 template<typename T> 59 T copy(const T& x) { return x; } 52 60 53 61 namespace ResolvExpr { … … 187 195 printAlts( alternatives, std::cerr ); 188 196 ) 189 AltList ::iterator oldBegin = alternatives.begin();190 pruneAlternatives( alternatives.begin(), alternatives.end(), front_inserter( alternatives) );191 if ( failFast && alternatives.begin() == oldBegin) {197 AltList pruned; 198 pruneAlternatives( alternatives.begin(), alternatives.end(), back_inserter( pruned ) ); 199 if ( failFast && pruned.empty() ) { 192 200 std::ostringstream stream; 193 201 AltList winners; … … 199 207 throw SemanticError( stream.str() ); 200 208 } 201 alternatives .erase( oldBegin, alternatives.end());209 alternatives = move(pruned); 202 210 PRINT( 203 211 std::cerr << "there are " << oldsize << " alternatives before elimination" << std::endl; … … 571 579 /// State to iteratively build a match of parameter expressions to arguments 572 580 struct ArgPack { 573 AltList actuals; ///< Arguments included in this pack 574 TypeEnvironment env; ///< Environment for this pack 575 AssertionSet need; ///< Assertions outstanding for this pack 576 AssertionSet have; ///< Assertions found for this pack 577 OpenVarSet openVars; ///< Open variables for this pack 578 unsigned nextArg; ///< Index of next argument in arguments list 579 std::vector<Alternative> expls; ///< Exploded actuals left over from last match 580 unsigned nextExpl; ///< Index of next exploded alternative to use 581 std::vector<unsigned> tupleEls; /// Number of elements in current tuple element(s) 582 583 ArgPack(const TypeEnvironment& env, const AssertionSet& need, const AssertionSet& have, 581 std::size_t parent; ///< Index of parent pack 582 std::unique_ptr<Expression> expr; ///< The argument stored here 583 Cost cost; ///< The cost of this argument 584 TypeEnvironment env; ///< Environment for this pack 585 AssertionSet need; ///< Assertions outstanding for this pack 586 AssertionSet have; ///< Assertions found for this pack 587 OpenVarSet openVars; ///< Open variables for this pack 588 unsigned nextArg; ///< Index of next argument in arguments list 589 unsigned tupleStart; ///< Number of tuples that start at this index 590 unsigned nextExpl; ///< Index of next exploded element 591 unsigned explAlt; ///< Index of alternative for nextExpl > 0 592 593 ArgPack() 594 : parent(0), expr(), cost(Cost::zero), env(), need(), have(), openVars(), nextArg(0), 595 tupleStart(0), nextExpl(0), explAlt(0) {} 596 597 ArgPack(const TypeEnvironment& env, const AssertionSet& need, const AssertionSet& have, 584 598 const OpenVarSet& openVars) 585 : actuals(), env(env), need(need), have(have), openVars(openVars), nextArg(0), 586 expls(), nextExpl(0), tupleEls() {} 587 588 /// Starts a new tuple expression 589 void beginTuple() { 590 if ( ! tupleEls.empty() ) ++tupleEls.back(); 591 tupleEls.push_back(0); 592 } 593 599 : parent(0), expr(), cost(Cost::zero), env(env), need(need), have(have), 600 openVars(openVars), nextArg(0), tupleStart(0), nextExpl(0), explAlt(0) {} 601 602 ArgPack(std::size_t parent, Expression* expr, TypeEnvironment&& env, AssertionSet&& need, 603 AssertionSet&& have, OpenVarSet&& openVars, unsigned nextArg, 604 unsigned tupleStart = 0, Cost cost = Cost::zero, unsigned nextExpl = 0, 605 unsigned explAlt = 0 ) 606 : parent(parent), expr(expr->clone()), cost(cost), env(move(env)), need(move(need)), 607 have(move(have)), openVars(move(openVars)), nextArg(nextArg), tupleStart(tupleStart), 608 nextExpl(nextExpl), explAlt(explAlt) {} 609 610 ArgPack(const ArgPack& o, TypeEnvironment&& env, AssertionSet&& need, AssertionSet&& have, 611 OpenVarSet&& openVars, unsigned nextArg, Cost added ) 612 : parent(o.parent), expr(o.expr ? o.expr->clone() : nullptr), cost(o.cost + added), 613 env(move(env)), need(move(need)), have(move(have)), openVars(move(openVars)), 614 nextArg(nextArg), tupleStart(o.tupleStart), nextExpl(0), explAlt(0) {} 615 616 /// true iff this pack is in the middle of an exploded argument 617 bool hasExpl() const { return nextExpl > 0; } 618 619 /// Gets the list of exploded alternatives for this pack 620 const ExplodedActual& getExpl( const ExplodedArgs& args ) const { 621 return args[nextArg-1][explAlt]; 622 } 623 594 624 /// Ends a tuple expression, consolidating the appropriate actuals 595 void endTuple( ) {596 // set up new Tuple alternative625 void endTuple( const std::vector<ArgPack>& packs ) { 626 // add all expressions in tuple to list, summing cost 597 627 std::list<Expression*> exprs; 598 Cost cost = Cost::zero; 599 600 // transfer elements into alternative 601 for (unsigned i = 0; i < tupleEls.back(); ++i) { 602 exprs.push_front( actuals.back().expr ); 603 actuals.back().expr = nullptr; 604 cost += actuals.back().cost; 605 actuals.pop_back(); 606 } 607 tupleEls.pop_back(); 608 609 // build new alternative 610 actuals.emplace_back( new TupleExpr( exprs ), this->env, cost ); 611 } 612 613 /// Clones and adds an actual, returns this 614 ArgPack& withArg( Expression* expr, Cost cost = Cost::zero ) { 615 actuals.emplace_back( expr->clone(), this->env, cost ); 616 if ( ! tupleEls.empty() ) ++tupleEls.back(); 617 return *this; 628 const ArgPack* pack = this; 629 if ( expr ) { exprs.push_front( expr.release() ); } 630 while ( pack->tupleStart == 0 ) { 631 pack = &packs[pack->parent]; 632 exprs.push_front( pack->expr->clone() ); 633 cost += pack->cost; 634 } 635 // reset pack to appropriate tuple 636 expr.reset( new TupleExpr( exprs ) ); 637 tupleStart = pack->tupleStart - 1; 638 parent = pack->parent; 618 639 } 619 640 }; 620 641 621 642 /// Instantiates an argument to match a formal, returns false if no results left 622 bool instantiateArgument( Type* formalType, Initializer* initializer, 623 const std::vector< AlternativeFinder >& args, 624 std::vector<ArgPack>& results, std::vector<ArgPack>& nextResults, 625 const SymTab::Indexer& indexer ) { 643 bool instantiateArgument( Type* formalType, Initializer* initializer, 644 const ExplodedArgs& args, std::vector<ArgPack>& results, std::size_t& genStart, 645 const SymTab::Indexer& indexer, unsigned nTuples = 0 ) { 626 646 if ( TupleType* tupleType = dynamic_cast<TupleType*>( formalType ) ) { 627 647 // formalType is a TupleType - group actuals into a TupleExpr 628 for ( ArgPack& result : results ) { result.beginTuple(); }648 ++nTuples; 629 649 for ( Type* type : *tupleType ) { 630 650 // xxx - dropping initializer changes behaviour from previous, but seems correct 631 if ( ! instantiateArgument( type, nullptr, args, results, nextResults, indexer ) ) 651 if ( ! instantiateArgument( 652 type, nullptr, args, results, genStart, indexer, nTuples ) ) 632 653 return false; 633 } 634 for ( ArgPack& result : results ) { result.endTuple(); } 654 nTuples = 0; 655 } 656 // re-consititute tuples for final generation 657 for ( auto i = genStart; i < results.size(); ++i ) { 658 results[i].endTuple( results ); 659 } 635 660 return true; 636 661 } else if ( TypeInstType* ttype = Tuples::isTtype( formalType ) ) { 637 662 // formalType is a ttype, consumes all remaining arguments 638 663 // xxx - mixing default arguments with variadic?? 639 std::vector<ArgPack> finalResults{}; /// list of completed tuples 640 // start tuples 641 for ( ArgPack& result : results ) { 642 result.beginTuple(); 643 644 // use rest of exploded tuple if present 645 while ( result.nextExpl < result.expls.size() ) { 646 const Alternative& actual = result.expls[result.nextExpl]; 647 result.env.addActual( actual.env, result.openVars ); 648 result.withArg( actual.expr ); 649 ++result.nextExpl; 650 } 651 } 664 665 // completed tuples; will be spliced to end of results to finish 666 std::vector<ArgPack> finalResults{}; 667 652 668 // iterate until all results completed 653 while ( ! results.empty() ) { 669 std::size_t genEnd; 670 ++nTuples; 671 do { 672 genEnd = results.size(); 673 654 674 // add another argument to results 655 for ( ArgPack& result : results) {656 // finish result when out of arguments657 if ( result.nextArg >= args.size() ) { 658 Type* argType = result.actuals.back().expr->get_result();659 if ( result.tupleEls.back() == 1 && Tuples::isTtype( argType) ) {660 // the case where a ttype value is passed directly is special, e.g. for661 // argument forwarding purposes662 // xxx - what if passing multiple arguments, last of which is ttype?663 // xxx - what would happen if unify was changed so that unifying tuple664 // types flattened both before unifying lists? then pass in TupleType665 // (ttype) below. 666 result.tupleEls.pop_back();667 } else { 668 // collapse leftover arguments into tuple669 result.endTuple();670 argType = result.actuals.back().expr->get_result();675 for ( std::size_t i = genStart; i < genEnd; ++i ) { 676 auto nextArg = results[i].nextArg; 677 678 // use remainder of exploded tuple if present 679 if ( results[i].hasExpl() ) { 680 const ExplodedActual& expl = results[i].getExpl( args ); 681 const Alternative& actual = expl.alts[results[i].nextExpl]; 682 683 TypeEnvironment env = results[i].env; 684 OpenVarSet openVars = results[i].openVars; 685 686 env.addActual( actual.env, openVars ); 687 688 unsigned nextExpl = results[i].nextExpl + 1; 689 if ( nextExpl == expl.alts.size() ) { 690 nextExpl = 0; 671 691 } 672 // check unification for ttype before adding to final 673 if ( unify( ttype, argType, result.env, result.need, result.have, 674 result.openVars, indexer ) ) { 675 finalResults.push_back( std::move(result) ); 676 } 692 693 results.emplace_back( 694 i, actual.expr, move(env), copy(results[i].need), 695 copy(results[i].have), move(openVars), nextArg, nTuples, 696 Cost::zero, nextExpl, results[i].explAlt ); 697 677 698 continue; 678 699 } 700 701 // finish result when out of arguments 702 if ( nextArg >= args.size() ) { 703 ArgPack newResult{ 704 results[i].env, results[i].need, results[i].have, 705 results[i].openVars }; 706 newResult.nextArg = nextArg; 707 Type* argType; 708 709 if ( nTuples > 0 ) { 710 // first iteration, push empty tuple expression 711 newResult.parent = i; 712 std::list<Expression*> emptyList; 713 newResult.expr.reset( new TupleExpr( emptyList ) ); 714 argType = newResult.expr->get_result(); 715 } else { 716 // clone result to collect tuple 717 newResult.parent = results[i].parent; 718 newResult.cost = results[i].cost; 719 newResult.tupleStart = results[i].tupleStart; 720 newResult.expr.reset( results[i].expr->clone() ); 721 argType = newResult.expr->get_result(); 722 723 if ( results[i].tupleStart > 0 && Tuples::isTtype( argType ) ) { 724 // the case where a ttype value is passed directly is special, 725 // e.g. for argument forwarding purposes 726 // xxx - what if passing multiple arguments, last of which is 727 // ttype? 728 // xxx - what would happen if unify was changed so that unifying 729 // tuple 730 // types flattened both before unifying lists? then pass in 731 // TupleType (ttype) below. 732 --newResult.tupleStart; 733 } else { 734 // collapse leftover arguments into tuple 735 newResult.endTuple( results ); 736 argType = newResult.expr->get_result(); 737 } 738 } 739 740 // check unification for ttype before adding to final 741 if ( unify( ttype, argType, newResult.env, newResult.need, newResult.have, 742 newResult.openVars, indexer ) ) { 743 finalResults.push_back( move(newResult) ); 744 } 745 746 continue; 747 } 679 748 680 749 // add each possible next argument 681 for ( const Alternative& actual : args[result.nextArg] ) { 682 ArgPack aResult = result; // copy to clone everything 683 // add details of actual to result 684 aResult.env.addActual( actual.env, aResult.openVars ); 685 Cost cost = actual.cost; 686 687 // explode argument 688 std::vector<Alternative> exploded; 689 Tuples::explode( actual, indexer, back_inserter( exploded ) ); 690 691 // add exploded argument to tuple 692 for ( Alternative& aActual : exploded ) { 693 aResult.withArg( aActual.expr, cost ); 694 cost = Cost::zero; 750 for ( std::size_t j = 0; j < args[nextArg].size(); ++j ) { 751 const ExplodedActual& expl = args[nextArg][j]; 752 753 // fresh copies of parent parameters for this iteration 754 TypeEnvironment env = results[i].env; 755 OpenVarSet openVars = results[i].openVars; 756 757 env.addActual( expl.env, openVars ); 758 759 // skip empty tuple arguments by (near-)cloning parent into next gen 760 if ( expl.alts.empty() ) { 761 results.emplace_back( 762 results[i], move(env), copy(results[i].need), 763 copy(results[i].have), move(openVars), nextArg + 1, expl.cost ); 764 765 continue; 695 766 } 696 ++aResult.nextArg; 697 nextResults.push_back( std::move(aResult) ); 767 768 // add new result 769 results.emplace_back( 770 i, expl.alts.front().expr, move(env), copy(results[i].need), 771 copy(results[i].have), move(openVars), nextArg + 1, 772 nTuples, expl.cost, expl.alts.size() == 1 ? 0 : 1, j ); 698 773 } 699 774 } 700 775 701 776 // reset for next round 702 results.swap( nextResults ); 703 nextResults.clear(); 704 } 705 results.swap( finalResults ); 706 return ! results.empty(); 777 genStart = genEnd; 778 nTuples = 0; 779 } while ( genEnd != results.size() ); 780 781 // splice final results onto results 782 for ( std::size_t i = 0; i < finalResults.size(); ++i ) { 783 results.push_back( move(finalResults[i]) ); 784 } 785 return ! finalResults.empty(); 707 786 } 708 787 709 788 // iterate each current subresult 710 for ( unsigned iResult = 0; iResult < results.size(); ++iResult ) { 711 ArgPack& result = results[iResult]; 712 713 if ( result.nextExpl < result.expls.size() ) { 714 // use remainder of exploded tuple if present 715 const Alternative& actual = result.expls[result.nextExpl]; 716 result.env.addActual( actual.env, result.openVars ); 789 std::size_t genEnd = results.size(); 790 for ( std::size_t i = genStart; i < genEnd; ++i ) { 791 auto nextArg = results[i].nextArg; 792 793 // use remainder of exploded tuple if present 794 if ( results[i].hasExpl() ) { 795 const ExplodedActual& expl = results[i].getExpl( args ); 796 const Alternative& actual = expl.alts[results[i].nextExpl]; 797 798 TypeEnvironment env = results[i].env; 799 AssertionSet need = results[i].need, have = results[i].have; 800 OpenVarSet openVars = results[i].openVars; 801 802 env.addActual( actual.env, openVars ); 717 803 Type* actualType = actual.expr->get_result(); 718 804 … … 724 810 std::cerr << std::endl; 725 811 ) 726 727 if ( unify( formalType, actualType, result.env, result.need, result.have, 728 result.openVars, indexer ) ) { 729 ++result.nextExpl; 730 nextResults.push_back( std::move(result.withArg( actual.expr )) ); 812 813 if ( unify( formalType, actualType, env, need, have, openVars, indexer ) ) { 814 unsigned nextExpl = results[i].nextExpl + 1; 815 if ( nextExpl == expl.alts.size() ) { 816 nextExpl = 0; 817 } 818 819 results.emplace_back( 820 i, actual.expr, move(env), move(need), move(have), move(openVars), 821 nextArg, nTuples, Cost::zero, nextExpl, results[i].explAlt ); 731 822 } 732 823 733 824 continue; 734 } else if ( result.nextArg >= args.size() ) { 735 // use default initializers if out of arguments 825 } 826 827 // use default initializers if out of arguments 828 if ( nextArg >= args.size() ) { 736 829 if ( ConstantExpr* cnstExpr = getDefaultValue( initializer ) ) { 737 830 if ( Constant* cnst = dynamic_cast<Constant*>( cnstExpr->get_constant() ) ) { 738 if ( unify( formalType, cnst->get_type(), result.env, result.need, 739 result.have, result.openVars, indexer ) ) { 740 nextResults.push_back( std::move(result.withArg( cnstExpr )) ); 831 TypeEnvironment env = results[i].env; 832 AssertionSet need = results[i].need, have = results[i].have; 833 OpenVarSet openVars = results[i].openVars; 834 835 if ( unify( formalType, cnst->get_type(), env, need, have, openVars, 836 indexer ) ) { 837 results.emplace_back( 838 i, cnstExpr, move(env), move(need), move(have), 839 move(openVars), nextArg, nTuples ); 741 840 } 742 841 } 743 842 } 843 744 844 continue; 745 845 } 746 846 747 847 // Check each possible next argument 748 for ( const Alternative& actual : args[result.nextArg] ) { 749 ArgPack aResult = result; // copy to clone everything 750 // add details of actual to result 751 aResult.env.addActual( actual.env, aResult.openVars ); 752 753 // explode argument 754 std::vector<Alternative> exploded; 755 Tuples::explode( actual, indexer, back_inserter( exploded ) ); 756 if ( exploded.empty() ) { 757 // skip empty tuple arguments 758 ++aResult.nextArg; 759 results.push_back( std::move(aResult) ); 848 for ( std::size_t j = 0; j < args[nextArg].size(); ++j ) { 849 const ExplodedActual& expl = args[nextArg][j]; 850 851 // fresh copies of parent parameters for this iteration 852 TypeEnvironment env = results[i].env; 853 AssertionSet need = results[i].need, have = results[i].have; 854 OpenVarSet openVars = results[i].openVars; 855 856 env.addActual( expl.env, openVars ); 857 858 // skip empty tuple arguments by (near-)cloning parent into next gen 859 if ( expl.alts.empty() ) { 860 results.emplace_back( 861 results[i], move(env), move(need), move(have), move(openVars), 862 nextArg + 1, expl.cost ); 863 760 864 continue; 761 865 } 762 866 763 867 // consider only first exploded actual 764 const Alternative& a Actual = exploded.front();765 Type* actualType = a Actual.expr->get_result()->clone();868 const Alternative& actual = expl.alts.front(); 869 Type* actualType = actual.expr->get_result()->clone(); 766 870 767 871 PRINT( … … 774 878 775 879 // attempt to unify types 776 if ( unify( formalType, actualType, aResult.env, aResult.need, aResult.have, aResult.openVars, indexer ) ) { 777 // add argument 778 aResult.withArg( aActual.expr, actual.cost ); 779 ++aResult.nextArg; 780 if ( exploded.size() > 1 ) { 781 // other parts of tuple left over 782 aResult.expls = std::move( exploded ); 783 aResult.nextExpl = 1; 784 } 785 nextResults.push_back( std::move(aResult) ); 880 if ( unify( formalType, actualType, env, need, have, openVars, indexer ) ) { 881 // add new result 882 results.emplace_back( 883 i, actual.expr, move(env), move(need), move(have), move(openVars), 884 nextArg + 1, nTuples, expl.cost, expl.alts.size() == 1 ? 0 : 1, j ); 786 885 } 787 886 } … … 789 888 790 889 // reset for next parameter 791 results.swap( nextResults ); 792 nextResults.clear(); 793 794 return ! results.empty(); 890 genStart = genEnd; 891 892 return genEnd != results.size(); 893 } 894 895 template<typename OutputIterator> 896 void AlternativeFinder::validateFunctionAlternative( const Alternative &func, ArgPack& result, 897 const std::vector<ArgPack>& results, OutputIterator out ) { 898 ApplicationExpr *appExpr = new ApplicationExpr( func.expr->clone() ); 899 // sum cost and accumulate actuals 900 std::list<Expression*>& args = appExpr->get_args(); 901 Cost cost = Cost::zero; 902 const ArgPack* pack = &result; 903 while ( pack->expr ) { 904 args.push_front( pack->expr->clone() ); 905 cost += pack->cost; 906 pack = &results[pack->parent]; 907 } 908 // build and validate new alternative 909 Alternative newAlt( appExpr, result.env, cost ); 910 PRINT( 911 std::cerr << "instantiate function success: " << appExpr << std::endl; 912 std::cerr << "need assertions:" << std::endl; 913 printAssertionSet( result.need, std::cerr, 8 ); 914 ) 915 inferParameters( result.need, result.have, newAlt, result.openVars, out ); 795 916 } 796 917 797 918 template<typename OutputIterator> 798 919 void AlternativeFinder::makeFunctionAlternatives( const Alternative &func, 799 FunctionType *funcType, const std::vector< AlternativeFinder > &args, 800 OutputIterator out ) { 920 FunctionType *funcType, const ExplodedArgs &args, OutputIterator out ) { 801 921 OpenVarSet funcOpenVars; 802 922 AssertionSet funcNeed, funcHave; … … 818 938 819 939 // iteratively build matches, one parameter at a time 820 std::vector<ArgPack> results{ ArgPack{ funcEnv, funcNeed, funcHave, funcOpenVars } }; 821 std::vector<ArgPack> nextResults{}; 940 std::vector<ArgPack> results; 941 results.push_back( ArgPack{ funcEnv, funcNeed, funcHave, funcOpenVars } ); 942 std::size_t genStart = 0; 943 822 944 for ( DeclarationWithType* formal : funcType->get_parameters() ) { 823 945 ObjectDecl* obj = strict_dynamic_cast< ObjectDecl* >( formal ); 824 if ( ! instantiateArgument( 825 obj->get_type(), obj->get_init(), args, results, nextResults, indexer ) )946 if ( ! instantiateArgument( 947 obj->get_type(), obj->get_init(), args, results, genStart, indexer ) ) 826 948 return; 827 949 } 828 950 829 // filter out results that don't use all the arguments, and aren't variadic830 std::vector<ArgPack> finalResults{};831 951 if ( funcType->get_isVarArgs() ) { 832 for ( ArgPack& result : results ) { 833 // use rest of exploded tuple if present 834 while ( result.nextExpl < result.expls.size() ) { 835 const Alternative& actual = result.expls[result.nextExpl]; 836 result.env.addActual( actual.env, result.openVars ); 837 result.withArg( actual.expr ); 838 ++result.nextExpl; 839 } 840 } 841 842 while ( ! results.empty() ) { 843 // build combinations for all remaining arguments 844 for ( ArgPack& result : results ) { 845 // keep if used all arguments 846 if ( result.nextArg >= args.size() ) { 847 finalResults.push_back( std::move(result) ); 952 // append any unused arguments to vararg pack 953 std::size_t genEnd; 954 do { 955 genEnd = results.size(); 956 957 // iterate results 958 for ( std::size_t i = genStart; i < genEnd; ++i ) { 959 auto nextArg = results[i].nextArg; 960 961 // use remainder of exploded tuple if present 962 if ( results[i].hasExpl() ) { 963 const ExplodedActual& expl = results[i].getExpl( args ); 964 const Alternative& actual = expl.alts[results[i].nextExpl]; 965 966 TypeEnvironment env = results[i].env; 967 OpenVarSet openVars = results[i].openVars; 968 969 env.addActual( actual.env, openVars ); 970 971 unsigned nextExpl = results[i].nextExpl + 1; 972 if ( nextExpl == expl.alts.size() ) { 973 nextExpl = 0; 974 } 975 976 results.emplace_back( 977 i, actual.expr, move(env), copy(results[i].need), 978 copy(results[i].have), move(openVars), nextArg, 0, 979 Cost::zero, nextExpl, results[i].explAlt ); 980 848 981 continue; 849 982 } 850 983 984 // finish result when out of arguments 985 if ( nextArg >= args.size() ) { 986 validateFunctionAlternative( func, results[i], results, out ); 987 988 continue; 989 } 990 851 991 // add each possible next argument 852 for ( const Alternative& actual : args[result.nextArg] ) { 853 ArgPack aResult = result; // copy to clone everything 854 // add details of actual to result 855 aResult.env.addActual( actual.env, aResult.openVars ); 856 Cost cost = actual.cost; 857 858 // explode argument 859 std::vector<Alternative> exploded; 860 Tuples::explode( actual, indexer, back_inserter( exploded ) ); 861 862 // add exploded argument to arg list 863 for ( Alternative& aActual : exploded ) { 864 aResult.withArg( aActual.expr, cost ); 865 cost = Cost::zero; 992 for ( std::size_t j = 0; j < args[nextArg].size(); ++j ) { 993 const ExplodedActual& expl = args[nextArg][j]; 994 995 // fresh copies of parent parameters for this iteration 996 TypeEnvironment env = results[i].env; 997 OpenVarSet openVars = results[i].openVars; 998 999 env.addActual( expl.env, openVars ); 1000 1001 // skip empty tuple arguments by (near-)cloning parent into next gen 1002 if ( expl.alts.empty() ) { 1003 results.emplace_back( 1004 results[i], move(env), copy(results[i].need), 1005 copy(results[i].have), move(openVars), nextArg + 1, expl.cost ); 1006 1007 continue; 866 1008 } 867 ++aResult.nextArg; 868 nextResults.push_back( std::move(aResult) ); 1009 1010 // add new result 1011 results.emplace_back( 1012 i, expl.alts.front().expr, move(env), copy(results[i].need), 1013 copy(results[i].have), move(openVars), nextArg + 1, 0, 1014 expl.cost, expl.alts.size() == 1 ? 0 : 1, j ); 869 1015 } 870 1016 } 871 1017 872 // reset for next round 873 results.swap( nextResults ); 874 nextResults.clear(); 875 } 1018 genStart = genEnd; 1019 } while ( genEnd != results.size() ); 876 1020 } else { 877 1021 // filter out results that don't use all the arguments 878 for ( ArgPack& result : results ) { 879 if ( result.nextExpl >= result.expls.size() && result.nextArg >= args.size() ) { 880 finalResults.push_back( std::move(result) ); 1022 for ( std::size_t i = genStart; i < results.size(); ++i ) { 1023 ArgPack& result = results[i]; 1024 if ( ! result.hasExpl() && result.nextArg >= args.size() ) { 1025 validateFunctionAlternative( func, result, results, out ); 881 1026 } 882 1027 } 883 }884 885 // validate matching combos, add to final result list886 for ( ArgPack& result : finalResults ) {887 ApplicationExpr *appExpr = new ApplicationExpr( func.expr->clone() );888 Alternative newAlt( appExpr, result.env, sumCost( result.actuals ) );889 makeExprList( result.actuals, appExpr->get_args() );890 PRINT(891 std::cerr << "instantiate function success: " << appExpr << std::endl;892 std::cerr << "need assertions:" << std::endl;893 printAssertionSet( result.need, std::cerr, 8 );894 )895 inferParameters( result.need, result.have, newAlt, result.openVars, out );896 1028 } 897 1029 } … … 920 1052 printAlts( funcOpFinder.alternatives, std::cerr, 1 ); 921 1053 ) 1054 1055 // pre-explode arguments 1056 ExplodedArgs argExpansions; 1057 argExpansions.reserve( argAlternatives.size() ); 1058 1059 for ( const AlternativeFinder& arg : argAlternatives ) { 1060 argExpansions.emplace_back(); 1061 auto& argE = argExpansions.back(); 1062 argE.reserve( arg.alternatives.size() ); 1063 1064 for ( const Alternative& actual : arg ) { 1065 argE.emplace_back( actual, indexer ); 1066 } 1067 } 922 1068 923 1069 AltList candidates; … … 934 1080 Alternative newFunc( *func ); 935 1081 referenceToRvalueConversion( newFunc.expr ); 936 makeFunctionAlternatives( newFunc, function, arg Alternatives,1082 makeFunctionAlternatives( newFunc, function, argExpansions, 937 1083 std::back_inserter( candidates ) ); 938 1084 } … … 943 1089 Alternative newFunc( *func ); 944 1090 referenceToRvalueConversion( newFunc.expr ); 945 makeFunctionAlternatives( newFunc, function, arg Alternatives,1091 makeFunctionAlternatives( newFunc, function, argExpansions, 946 1092 std::back_inserter( candidates ) ); 947 1093 } // if … … 955 1101 // try each function operator ?() with each function alternative 956 1102 if ( ! funcOpFinder.alternatives.empty() ) { 957 // add function alternatives to front of argument list 958 argAlternatives.insert( argAlternatives.begin(), std::move(funcFinder) ); 1103 // add exploded function alternatives to front of argument list 1104 std::vector<ExplodedActual> funcE; 1105 funcE.reserve( funcFinder.alternatives.size() ); 1106 for ( const Alternative& actual : funcFinder ) { 1107 funcE.emplace_back( actual, indexer ); 1108 } 1109 argExpansions.insert( argExpansions.begin(), move(funcE) ); 959 1110 960 1111 for ( AltList::iterator funcOp = funcOpFinder.alternatives.begin(); … … 968 1119 Alternative newFunc( *funcOp ); 969 1120 referenceToRvalueConversion( newFunc.expr ); 970 makeFunctionAlternatives( newFunc, function, arg Alternatives,1121 makeFunctionAlternatives( newFunc, function, argExpansions, 971 1122 std::back_inserter( candidates ) ); 972 1123 } … … 982 1133 983 1134 // compute conversionsion costs 984 for ( Alt List::iterator withFunc = candidates.begin(); withFunc != candidates.end(); ++withFunc) {985 Cost cvtCost = computeApplicationConversionCost( *withFunc, indexer );1135 for ( Alternative& withFunc : candidates ) { 1136 Cost cvtCost = computeApplicationConversionCost( withFunc, indexer ); 986 1137 987 1138 PRINT( 988 ApplicationExpr *appExpr = strict_dynamic_cast< ApplicationExpr* >( withFunc ->expr );1139 ApplicationExpr *appExpr = strict_dynamic_cast< ApplicationExpr* >( withFunc.expr ); 989 1140 PointerType *pointer = strict_dynamic_cast< PointerType* >( appExpr->get_function()->get_result() ); 990 1141 FunctionType *function = strict_dynamic_cast< FunctionType* >( pointer->get_base() ); … … 995 1146 printAll( appExpr->get_args(), std::cerr, 8 ); 996 1147 std::cerr << "bindings are:" << std::endl; 997 withFunc ->env.print( std::cerr, 8 );1148 withFunc.env.print( std::cerr, 8 ); 998 1149 std::cerr << "cost of conversion is:" << cvtCost << std::endl; 999 1150 ) 1000 1151 if ( cvtCost != Cost::infinity ) { 1001 withFunc ->cvtCost = cvtCost;1002 alternatives.push_back( *withFunc );1152 withFunc.cvtCost = cvtCost; 1153 alternatives.push_back( withFunc ); 1003 1154 } // if 1004 1155 } // for 1005 1156 1006 candidates.clear(); 1007 candidates.splice( candidates.end(), alternatives ); 1157 candidates = move(alternatives); 1008 1158 1009 1159 // use a new list so that alternatives are not examined by addAnonConversions twice. … … 1011 1161 findMinCost( candidates.begin(), candidates.end(), std::back_inserter( winners ) ); 1012 1162 1013 // function may return struct or union value, in which case we need to add alternatives for implicit1014 // conversions to each of the anonymous members, must happen after findMinCost since anon conversions1015 // are never the cheapest expression1163 // function may return struct or union value, in which case we need to add alternatives 1164 // for implicitconversions to each of the anonymous members, must happen after findMinCost 1165 // since anon conversions are never the cheapest expression 1016 1166 for ( const Alternative & alt : winners ) { 1017 1167 addAnonConversions( alt ); 1018 1168 } 1019 alternatives.splice( alternatives.begin(), winners );1169 spliceBegin( alternatives, winners ); 1020 1170 1021 1171 if ( alternatives.empty() && targetType && ! targetType->isVoid() ) { … … 1041 1191 AlternativeFinder finder( indexer, env ); 1042 1192 finder.find( addressExpr->get_arg() ); 1043 for ( std::list< Alternative >::iterator i = finder.alternatives.begin(); i != finder.alternatives.end(); ++i ) { 1044 if ( isLvalue( i->expr ) ) { 1045 alternatives.push_back( Alternative( new AddressExpr( i->expr->clone() ), i->env, i->cost ) ); 1193 for ( Alternative& alt : finder.alternatives ) { 1194 if ( isLvalue( alt.expr ) ) { 1195 alternatives.push_back( 1196 Alternative{ new AddressExpr( alt.expr->clone() ), alt.env, alt.cost } ); 1046 1197 } // if 1047 1198 } // for … … 1049 1200 1050 1201 void AlternativeFinder::visit( LabelAddressExpr * expr ) { 1051 alternatives.push_back( Alternative ( expr->clone(), env, Cost::zero));1202 alternatives.push_back( Alternative{ expr->clone(), env, Cost::zero } ); 1052 1203 } 1053 1204 … … 1091 1242 1092 1243 AltList candidates; 1093 for ( std::list< Alternative >::iterator i = finder.alternatives.begin(); i != finder.alternatives.end(); ++i) {1244 for ( Alternative& alt : finder.alternatives ) { 1094 1245 AssertionSet needAssertions, haveAssertions; 1095 1246 OpenVarSet openVars; … … 1099 1250 // that are cast directly. The candidate is invalid if it has fewer results than there are types to cast 1100 1251 // to. 1101 int discardedValues = i->expr->get_result()->size() - castExpr->get_result()->size();1252 int discardedValues = alt.expr->get_result()->size() - castExpr->get_result()->size(); 1102 1253 if ( discardedValues < 0 ) continue; 1103 1254 // xxx - may need to go into tuple types and extract relevant types and use unifyList. Note that currently, this does not 1104 1255 // allow casting a tuple to an atomic type (e.g. (int)([1, 2, 3])) 1105 1256 // unification run for side-effects 1106 unify( castExpr->get_result(), i->expr->get_result(), i->env, needAssertions, haveAssertions, openVars, indexer ); 1107 Cost thisCost = castCost( i->expr->get_result(), castExpr->get_result(), indexer, i->env ); 1257 unify( castExpr->get_result(), alt.expr->get_result(), alt.env, needAssertions, 1258 haveAssertions, openVars, indexer ); 1259 Cost thisCost = castCost( alt.expr->get_result(), castExpr->get_result(), indexer, 1260 alt.env ); 1108 1261 if ( thisCost != Cost::infinity ) { 1109 1262 // count one safe conversion for each value that is thrown away 1110 1263 thisCost.incSafe( discardedValues ); 1111 Alternative newAlt( restructureCast( i->expr->clone(), toType ), i->env, i->cost, thisCost ); 1112 inferParameters( needAssertions, haveAssertions, newAlt, openVars, back_inserter( candidates ) ); 1264 Alternative newAlt( restructureCast( alt.expr->clone(), toType ), alt.env, 1265 alt.cost, thisCost ); 1266 inferParameters( needAssertions, haveAssertions, newAlt, openVars, 1267 back_inserter( candidates ) ); 1113 1268 } // if 1114 1269 } // for … … 1397 1552 1398 1553 void AlternativeFinder::visit( UntypedTupleExpr *tupleExpr ) { 1399 std::list< AlternativeFinder > subExprAlternatives; 1400 findSubExprs( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end(), back_inserter( subExprAlternatives ) ); 1401 std::list< AltList > possibilities; 1402 combos( subExprAlternatives.begin(), subExprAlternatives.end(), back_inserter( possibilities ) ); 1403 for ( std::list< AltList >::const_iterator i = possibilities.begin(); i != possibilities.end(); ++i ) { 1554 std::vector< AlternativeFinder > subExprAlternatives; 1555 findSubExprs( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end(), 1556 back_inserter( subExprAlternatives ) ); 1557 std::vector< AltList > possibilities; 1558 combos( subExprAlternatives.begin(), subExprAlternatives.end(), 1559 back_inserter( possibilities ) ); 1560 for ( const AltList& alts : possibilities ) { 1404 1561 std::list< Expression * > exprs; 1405 makeExprList( *i, exprs );1562 makeExprList( alts, exprs ); 1406 1563 1407 1564 TypeEnvironment compositeEnv; 1408 simpleCombineEnvironments( i->begin(), i->end(), compositeEnv ); 1409 alternatives.push_back( Alternative( new TupleExpr( exprs ) , compositeEnv, sumCost( *i ) ) ); 1565 simpleCombineEnvironments( alts.begin(), alts.end(), compositeEnv ); 1566 alternatives.push_back( 1567 Alternative{ new TupleExpr( exprs ), compositeEnv, sumCost( alts ) } ); 1410 1568 } // for 1411 1569 } -
src/ResolvExpr/AlternativeFinder.h
r9f10d1f2 r88ef2af 21 21 22 22 #include "Alternative.h" // for AltList, Alternative 23 #include "ExplodedActual.h" // for ExplodedActual 23 24 #include "ResolvExpr/Cost.h" // for Cost, Cost::infinity 24 25 #include "ResolvExpr/TypeEnvironment.h" // for AssertionSet, OpenVarSet … … 31 32 32 33 namespace ResolvExpr { 34 class ArgPack; 35 36 /// First index is which argument, second index is which alternative for that argument, 37 /// third index is which exploded element of that alternative 38 using ExplodedArgs = std::vector< std::vector< ExplodedActual > >; 39 33 40 class AlternativeFinder : public Visitor { 34 41 public: … … 126 133 /// Adds alternatives for offsetof expressions, given the base type and name of the member 127 134 template< typename StructOrUnionType > void addOffsetof( StructOrUnionType *aggInst, const std::string &name ); 135 /// Takes a final result and checks if its assertions can be satisfied 128 136 template<typename OutputIterator> 129 void makeFunctionAlternatives( const Alternative &func, FunctionType *funcType, const std::vector< AlternativeFinder >& args, OutputIterator out ); 137 void validateFunctionAlternative( const Alternative &func, ArgPack& result, const std::vector<ArgPack>& results, OutputIterator out ); 138 /// Finds matching alternatives for a function, given a set of arguments 139 template<typename OutputIterator> 140 void makeFunctionAlternatives( const Alternative &func, FunctionType *funcType, const ExplodedArgs& args, OutputIterator out ); 141 /// Checks if assertion parameters match for a new alternative 130 142 template< typename OutputIterator > 131 143 void inferParameters( const AssertionSet &need, AssertionSet &have, const Alternative &newAlt, OpenVarSet &openVars, OutputIterator out ); -
src/ResolvExpr/Resolver.cc
r9f10d1f2 r88ef2af 18 18 #include <memory> // for allocator, allocator_traits<... 19 19 #include <tuple> // for get 20 #include <vector> 20 21 21 22 #include "Alternative.h" // for Alternative, AltList … … 411 412 412 413 // Find all alternatives for all arguments in canonical form 413 std:: list< AlternativeFinder > argAlternatives;414 std::vector< AlternativeFinder > argAlternatives; 414 415 funcFinder.findSubExprs( clause.target.arguments.begin(), clause.target.arguments.end(), back_inserter( argAlternatives ) ); 415 416 416 417 // List all combinations of arguments 417 std:: list< AltList > possibilities;418 std::vector< AltList > possibilities; 418 419 combos( argAlternatives.begin(), argAlternatives.end(), back_inserter( possibilities ) ); 419 420 -
src/ResolvExpr/module.mk
r9f10d1f2 r88ef2af 32 32 ResolvExpr/Occurs.cc \ 33 33 ResolvExpr/TypeEnvironment.cc \ 34 ResolvExpr/CurrentObject.cc 34 ResolvExpr/CurrentObject.cc \ 35 ResolvExpr/ExplodedActual.cc -
src/ResolvExpr/typeops.h
r9f10d1f2 r88ef2af 16 16 #pragma once 17 17 18 #include <vector> 19 18 20 #include "SynTree/SynTree.h" 19 21 #include "SynTree/Type.h" … … 28 30 void combos( InputIterator begin, InputIterator end, OutputIterator out ) { 29 31 typedef typename InputIterator::value_type SetType; 30 typedef typename std:: list< typename SetType::value_type > ListType;32 typedef typename std::vector< typename SetType::value_type > ListType; 31 33 32 34 if ( begin == end ) { … … 38 40 begin++; 39 41 40 std:: list< ListType > recursiveResult;42 std::vector< ListType > recursiveResult; 41 43 combos( begin, end, back_inserter( recursiveResult ) ); 42 44 43 for ( typename std::list< ListType >::const_iterator i = recursiveResult.begin(); i != recursiveResult.end(); ++i ) { 44 for ( typename ListType::const_iterator j = current->begin(); j != current->end(); ++j ) { 45 ListType result; 46 std::back_insert_iterator< ListType > inserter = back_inserter( result ); 47 *inserter++ = *j; 48 std::copy( i->begin(), i->end(), inserter ); 49 *out++ = result; 50 } // for 51 } // for 45 for ( const auto& i : recursiveResult ) for ( const auto& j : *current ) { 46 ListType result; 47 std::back_insert_iterator< ListType > inserter = back_inserter( result ); 48 *inserter++ = j; 49 std::copy( i.begin(), i.end(), inserter ); 50 *out++ = result; 51 } 52 52 } 53 53 -
src/SymTab/Validate.cc
r9f10d1f2 r88ef2af 423 423 } 424 424 425 void checkGenericParameters( ReferenceToType * inst ) { 426 for ( Expression * param : inst->parameters ) { 427 if ( ! dynamic_cast< TypeExpr * >( param ) ) { 428 throw SemanticError( "Expression parameters for generic types are currently unsupported: ", inst ); 429 } 430 } 431 } 432 425 433 void LinkReferenceToTypes::postvisit( StructInstType *structInst ) { 426 434 StructDecl *st = local_indexer->lookupStruct( structInst->get_name() ); … … 434 442 forwardStructs[ structInst->get_name() ].push_back( structInst ); 435 443 } // if 444 checkGenericParameters( structInst ); 436 445 } 437 446 … … 446 455 forwardUnions[ unionInst->get_name() ].push_back( unionInst ); 447 456 } // if 457 checkGenericParameters( unionInst ); 448 458 } 449 459 … … 525 535 // need to carry over the 'sized' status of each decl in the instance 526 536 for ( auto p : group_iterate( traitDecl->get_parameters(), traitInst->get_parameters() ) ) { 527 TypeExpr * expr = strict_dynamic_cast< TypeExpr * >( std::get<1>(p) ); 537 TypeExpr * expr = dynamic_cast< TypeExpr * >( std::get<1>(p) ); 538 if ( ! expr ) { 539 throw SemanticError( "Expression parameters for trait instances are currently unsupported: ", std::get<1>(p) ); 540 } 528 541 if ( TypeInstType * inst = dynamic_cast< TypeInstType * >( expr->get_type() ) ) { 529 542 TypeDecl * formalDecl = std::get<0>(p); -
src/Tuples/TupleAssignment.cc
r9f10d1f2 r88ef2af 251 251 // combine assignment environments into combined expression environment 252 252 simpleCombineEnvironments( current.begin(), current.end(), matcher->compositeEnv ); 253 currentFinder.get_alternatives().push_front( ResolvExpr::Alternative( 253 // xxx -- was push_front 254 currentFinder.get_alternatives().push_back( ResolvExpr::Alternative( 254 255 new TupleAssignExpr(solved_assigns, matcher->tmpDecls), matcher->compositeEnv, 255 256 ResolvExpr::sumCost( current ) + matcher->baseCost ) ); -
src/tests/.expect/castError.txt
r9f10d1f2 r88ef2af 5 5 charAlternatives are: 6 6 Cost ( 1, 0, 0, 0 ): Cast of: 7 Variable Expression: f: signed int 7 Variable Expression: f: function 8 accepting unspecified arguments 9 ... returning nothing 10 8 11 ... to: 9 12 char … … 23 26 24 27 Cost ( 1, 0, 0, 0 ): Cast of: 25 Variable Expression: f: function 26 accepting unspecified arguments 27 ... returning nothing 28 28 Variable Expression: f: signed int 29 29 ... to: 30 30 char
Note: See TracChangeset
for help on using the changeset viewer.