- File:
-
- 1 edited
-
src/ResolvExpr/AlternativeFinder.cc (modified) (28 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/ResolvExpr/AlternativeFinder.cc
r5af7306 r43bd69d 21 21 #include <list> // for _List_iterator, list, _List_const_... 22 22 #include <map> // for _Rb_tree_iterator, map, _Rb_tree_c... 23 #include <memory> // for allocator_traits<>::value_type 23 #include <memory> // for allocator_traits<>::value_type, unique_ptr 24 24 #include <utility> // for pair 25 25 #include <vector> // for vector … … 35 35 #include "ResolveTypeof.h" // for resolveTypeof 36 36 #include "Resolver.h" // for resolveStmtExpr 37 #include "Common/GC.h" // for new_static_root38 37 #include "SymTab/Indexer.h" // for Indexer 39 38 #include "SymTab/Mangler.h" // for Mangler … … 102 101 void addAnonConversions( const Alternative & alt ); 103 102 /// Adds alternatives for member expressions, given the aggregate, conversion cost for that aggregate, and name of the member 104 template< typename StructOrUnionType > void addAggMembers( StructOrUnionType *aggInst, Expression *expr, const Cost &newCost, const TypeEnvironment & env, const std::string & name);103 template< typename StructOrUnionType > void addAggMembers( StructOrUnionType *aggInst, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member ); 105 104 /// Adds alternatives for member expressions where the left side has tuple type 106 105 void addTupleMembers( TupleType * tupleType, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member ); … … 166 165 candidate->env.apply( newType ); 167 166 mangleName = SymTab::Mangler::mangle( newType ); 167 delete newType; 168 168 } 169 169 std::map< std::string, PruneStruct >::iterator mapPlace = selected.find( mangleName ); … … 297 297 // adds anonymous member interpretations whenever an aggregate value type is seen. 298 298 // 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 299 Expression* aggrExpr = alt.expr->clone();299 std::unique_ptr<Expression> aggrExpr( alt.expr->clone() ); 300 300 alt.env.apply( aggrExpr->get_result() ); 301 301 Type * aggrType = aggrExpr->get_result(); 302 302 if ( dynamic_cast< ReferenceType * >( aggrType ) ) { 303 303 aggrType = aggrType->stripReferences(); 304 aggrExpr = new CastExpr{ aggrExpr, aggrType->clone() };304 aggrExpr.reset( new CastExpr( aggrExpr.release(), aggrType->clone() ) ); 305 305 } 306 306 307 307 if ( StructInstType *structInst = dynamic_cast< StructInstType* >( aggrExpr->get_result() ) ) { 308 addAggMembers( structInst, aggrExpr, alt.cost+Cost::safe, alt.env, "" ); 308 NameExpr nameExpr( "" ); 309 addAggMembers( structInst, aggrExpr.get(), alt.cost+Cost::safe, alt.env, &nameExpr ); 309 310 } else if ( UnionInstType *unionInst = dynamic_cast< UnionInstType* >( aggrExpr->get_result() ) ) { 310 addAggMembers( unionInst, aggrExpr, alt.cost+Cost::safe, alt.env, "" ); 311 NameExpr nameExpr( "" ); 312 addAggMembers( unionInst, aggrExpr.get(), alt.cost+Cost::safe, alt.env, &nameExpr ); 311 313 } // if 312 314 } 313 315 314 316 template< typename StructOrUnionType > 315 void AlternativeFinder::Finder::addAggMembers( StructOrUnionType *aggInst, Expression *expr, const Cost &newCost, const TypeEnvironment & env, const std::string & name ) { 317 void AlternativeFinder::Finder::addAggMembers( StructOrUnionType *aggInst, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member ) { 318 // by this point, member must be a name expr 319 NameExpr * nameExpr = dynamic_cast< NameExpr * >( member ); 320 if ( ! nameExpr ) return; 321 const std::string & name = nameExpr->get_name(); 316 322 std::list< Declaration* > members; 317 323 aggInst->lookup( name, members ); … … 453 459 /// Adds type variables to the open variable set and marks their assertions 454 460 void makeUnifiableVars( Type *type, OpenVarSet &unifiableVars, AssertionSet &needAssertions ) { 455 for ( Type::ForallList::const_iterator tyvar = type-> get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) {461 for ( Type::ForallList::const_iterator tyvar = type->forall.begin(); tyvar != type->forall.end(); ++tyvar ) { 456 462 unifiableVars[ (*tyvar)->get_name() ] = TypeDecl::Data{ *tyvar }; 457 for ( std::list< DeclarationWithType* >::iterator assert = (*tyvar)-> get_assertions().begin(); assert != (*tyvar)->get_assertions().end(); ++assert ) {463 for ( std::list< DeclarationWithType* >::iterator assert = (*tyvar)->assertions.begin(); assert != (*tyvar)->assertions.end(); ++assert ) { 458 464 needAssertions[ *assert ].isUsed = true; 459 465 } … … 562 568 563 569 Expression *varExpr = data.combine( newerAlt.cvtCost ); 570 delete varExpr->get_result(); 564 571 varExpr->set_result( adjType->clone() ); 565 572 PRINT( … … 578 585 (*inferParameters)[ curDecl->get_uniqueId() ] = ParamEntry( candidate->get_uniqueId(), adjType->clone(), curDecl->get_type()->clone(), varExpr ); 579 586 inferRecursive( begin, end, newerAlt, newOpenVars, newDecls, newerNeed, /*newNeedParents,*/ level, indexer, out ); 587 } else { 588 delete adjType; 580 589 } 581 590 } … … 625 634 struct ArgPack { 626 635 std::size_t parent; ///< Index of parent pack 627 Expression* expr;///< The argument stored here636 std::unique_ptr<Expression> expr; ///< The argument stored here 628 637 Cost cost; ///< The cost of this argument 629 638 TypeEnvironment env; ///< Environment for this pack … … 672 681 std::list<Expression*> exprs; 673 682 const ArgPack* pack = this; 674 if ( expr ) { exprs.push_front( expr ); }683 if ( expr ) { exprs.push_front( expr.release() ); } 675 684 while ( pack->tupleStart == 0 ) { 676 685 pack = &packs[pack->parent]; … … 679 688 } 680 689 // reset pack to appropriate tuple 681 expr = new TupleExpr{ exprs };690 expr.reset( new TupleExpr( exprs ) ); 682 691 tupleStart = pack->tupleStart - 1; 683 692 parent = pack->parent; … … 731 740 732 741 results.emplace_back( 733 i, expl.exprs[results[i].nextExpl] , copy(results[i].env),742 i, expl.exprs[results[i].nextExpl].get(), copy(results[i].env), 734 743 copy(results[i].need), copy(results[i].have), 735 744 copy(results[i].openVars), nextArg, nTuples, Cost::zero, nextExpl, … … 752 761 newResult.parent = i; 753 762 std::list<Expression*> emptyList; 754 newResult.expr = new TupleExpr{ emptyList };763 newResult.expr.reset( new TupleExpr( emptyList ) ); 755 764 argType = newResult.expr->get_result(); 756 765 } else { … … 759 768 newResult.cost = results[i].cost; 760 769 newResult.tupleStart = results[i].tupleStart; 761 newResult.expr = results[i].expr->clone();770 newResult.expr.reset( results[i].expr->clone() ); 762 771 argType = newResult.expr->get_result(); 763 772 … … 809 818 // add new result 810 819 results.emplace_back( 811 i, expl.exprs.front() , move(env), copy(results[i].need),820 i, expl.exprs.front().get(), move(env), copy(results[i].need), 812 821 copy(results[i].have), move(openVars), nextArg + 1, 813 822 nTuples, expl.cost, expl.exprs.size() == 1 ? 0 : 1, j ); … … 835 844 if ( results[i].hasExpl() ) { 836 845 const ExplodedActual& expl = results[i].getExpl( args ); 837 Expression* expr = expl.exprs[results[i].nextExpl] ;846 Expression* expr = expl.exprs[results[i].nextExpl].get(); 838 847 839 848 TypeEnvironment env = results[i].env; … … 906 915 907 916 // consider only first exploded actual 908 Expression* expr = expl.exprs.front() ;917 Expression* expr = expl.exprs.front().get(); 909 918 Type* actualType = expr->get_result()->clone(); 910 919 … … 1009 1018 1010 1019 results.emplace_back( 1011 i, expl.exprs[results[i].nextExpl] , copy(results[i].env),1020 i, expl.exprs[results[i].nextExpl].get(), copy(results[i].env), 1012 1021 copy(results[i].need), copy(results[i].have), 1013 1022 copy(results[i].openVars), nextArg, 0, Cost::zero, nextExpl, … … 1045 1054 // add new result 1046 1055 results.emplace_back( 1047 i, expl.exprs.front() , move(env), copy(results[i].need),1056 i, expl.exprs.front().get(), move(env), copy(results[i].need), 1048 1057 copy(results[i].have), move(openVars), nextArg + 1, 0, 1049 1058 expl.cost, expl.exprs.size() == 1 ? 0 : 1, j ); … … 1079 1088 1080 1089 // find function operators 1081 static auto *opExpr = new_static_root<NameExpr>( "?()" );1090 static NameExpr *opExpr = new NameExpr( "?()" ); 1082 1091 AlternativeFinder funcOpFinder( indexer, env ); 1083 1092 // it's ok if there aren't any defined function ops 1084 funcOpFinder.maybeFind( opExpr );1093 funcOpFinder.maybeFind( opExpr); 1085 1094 PRINT( 1086 1095 std::cerr << "known function ops:" << std::endl; … … 1238 1247 } 1239 1248 1240 Expression * restructureCast( Expression * argExpr, Type * toType ) {1249 Expression * restructureCast( Expression * argExpr, Type * toType, bool isGenerated ) { 1241 1250 if ( argExpr->get_result()->size() > 1 && ! toType->isVoid() && ! dynamic_cast<ReferenceType *>( toType ) ) { 1242 1251 // Argument expression is a tuple and the target type is not void and not a reference type. … … 1253 1262 // cast each component 1254 1263 TupleIndexExpr * idx = new TupleIndexExpr( argExpr->clone(), i ); 1255 componentExprs.push_back( restructureCast( idx, toType->getComponent( i ) ) ); 1256 } 1264 componentExprs.push_back( restructureCast( idx, toType->getComponent( i ), isGenerated ) ); 1265 } 1266 delete argExpr; 1257 1267 assert( componentExprs.size() > 0 ); 1258 1268 // produce the tuple of casts … … 1260 1270 } else { 1261 1271 // handle normally 1262 return new CastExpr( argExpr, toType->clone() ); 1272 CastExpr * ret = new CastExpr( argExpr, toType->clone() ); 1273 ret->isGenerated = isGenerated; 1274 return ret; 1263 1275 } 1264 1276 } … … 1304 1316 // count one safe conversion for each value that is thrown away 1305 1317 thisCost.incSafe( discardedValues ); 1306 Alternative newAlt( restructureCast( alt.expr->clone(), toType ), alt.env,1318 Alternative newAlt( restructureCast( alt.expr->clone(), toType, castExpr->isGenerated ), alt.env, 1307 1319 alt.cost, thisCost ); 1308 1320 inferParameters( needAssertions, haveAssertions, newAlt, openVars, … … 1331 1343 } 1332 1344 1333 namespace {1334 /// Gets name from untyped member expression (member must be NameExpr)1335 const std::string& get_member_name( UntypedMemberExpr *memberExpr ) {1336 NameExpr * nameExpr = dynamic_cast< NameExpr * >( memberExpr->get_member() );1337 assert( nameExpr );1338 return nameExpr->get_name();1339 }1340 }1341 1342 1345 void AlternativeFinder::Finder::postvisit( UntypedMemberExpr *memberExpr ) { 1343 1346 AlternativeFinder funcFinder( indexer, env ); … … 1348 1351 Expression * aggrExpr = agg->expr->clone(); 1349 1352 referenceToRvalueConversion( aggrExpr, cost ); 1353 std::unique_ptr<Expression> guard( aggrExpr ); 1350 1354 1351 1355 // find member of the given type 1352 1356 if ( StructInstType *structInst = dynamic_cast< StructInstType* >( aggrExpr->get_result() ) ) { 1353 addAggMembers( structInst, aggrExpr, cost, agg->env, get_member_name(memberExpr) );1357 addAggMembers( structInst, aggrExpr, cost, agg->env, memberExpr->get_member() ); 1354 1358 } else if ( UnionInstType *unionInst = dynamic_cast< UnionInstType* >( aggrExpr->get_result() ) ) { 1355 addAggMembers( unionInst, aggrExpr, cost, agg->env, get_member_name(memberExpr) );1359 addAggMembers( unionInst, aggrExpr, cost, agg->env, memberExpr->get_member() ); 1356 1360 } else if ( TupleType * tupleType = dynamic_cast< TupleType * >( aggrExpr->get_result() ) ) { 1357 1361 addTupleMembers( tupleType, aggrExpr, cost, agg->env, memberExpr->get_member() ); … … 1598 1602 alternatives.push_back( Alternative( new CommaExpr( newFirstArg->clone(), alt.expr->clone() ), alt.env, alt.cost ) ); 1599 1603 } // for 1604 delete newFirstArg; 1600 1605 } 1601 1606 … … 1727 1732 // count one safe conversion for each value that is thrown away 1728 1733 thisCost.incSafe( discardedValues ); 1729 Alternative newAlt( new InitExpr( restructureCast( alt.expr->clone(), toType ), initAlt.designation->clone() ), newEnv, alt.cost, thisCost );1734 Alternative newAlt( new InitExpr( restructureCast( alt.expr->clone(), toType, true ), initAlt.designation->clone() ), newEnv, alt.cost, thisCost ); 1730 1735 inferParameters( needAssertions, haveAssertions, newAlt, openVars, back_inserter( candidates ) ); 1731 1736 }
Note:
See TracChangeset
for help on using the changeset viewer.