Changeset b21c77a for src/ResolvExpr/AlternativeFinder.cc
- Timestamp:
- Jun 29, 2018, 4:14:15 PM (7 years ago)
- Branches:
- new-env
- Children:
- 184557e
- Parents:
- 97397a26 (diff), 28f3a19 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/ResolvExpr/AlternativeFinder.cc
r97397a26 rb21c77a 100 100 void postvisit( InitExpr * initExpr ); 101 101 void postvisit( DeletedExpr * delExpr ); 102 void postvisit( GenericExpr * genExpr ); 102 103 103 104 /// Adds alternatives for anonymous members … … 177 178 selected[ mangleName ] = current; 178 179 } else if ( candidate->cost == mapPlace->second.candidate->cost ) { 179 PRINT( 180 std::cerr << "marking ambiguous" << std::endl; 181 ) 182 mapPlace->second.isAmbiguous = true; 180 // if one of the candidates contains a deleted identifier, can pick the other, since 181 // deleted expressions should not be ambiguous if there is another option that is at least as good 182 if ( findDeletedExpr( candidate->expr ) ) { 183 // do nothing 184 PRINT( std::cerr << "candidate is deleted" << std::endl; ) 185 } else if ( findDeletedExpr( mapPlace->second.candidate->expr ) ) { 186 PRINT( std::cerr << "current is deleted" << std::endl; ) 187 selected[ mangleName ] = current; 188 } else { 189 PRINT( 190 std::cerr << "marking ambiguous" << std::endl; 191 ) 192 mapPlace->second.isAmbiguous = true; 193 } 183 194 } else { 184 195 PRINT( … … 300 311 // 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 301 312 Expression* aggrExpr = alt.expr->clone(); 302 alt.env.apply( aggrExpr-> get_result());303 Type * aggrType = aggrExpr-> get_result();313 alt.env.apply( aggrExpr->result ); 314 Type * aggrType = aggrExpr->result; 304 315 if ( dynamic_cast< ReferenceType * >( aggrType ) ) { 305 316 aggrType = aggrType->stripReferences(); … … 307 318 } 308 319 309 if ( StructInstType *structInst = dynamic_cast< StructInstType* >( aggrExpr-> get_result()) ) {320 if ( StructInstType *structInst = dynamic_cast< StructInstType* >( aggrExpr->result ) ) { 310 321 addAggMembers( structInst, aggrExpr, alt.cost+Cost::safe, alt.env, "" ); 311 } else if ( UnionInstType *unionInst = dynamic_cast< UnionInstType* >( aggrExpr-> get_result()) ) {322 } else if ( UnionInstType *unionInst = dynamic_cast< UnionInstType* >( aggrExpr->result ) ) { 312 323 addAggMembers( unionInst, aggrExpr, alt.cost+Cost::safe, alt.env, "" ); 313 324 } // if … … 319 330 aggInst->lookup( name, members ); 320 331 321 for ( std::list< Declaration* >::const_iterator i = members.begin(); i != members.end(); ++i ) { 322 if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType* >( *i ) ) { 323 alternatives.push_back( Alternative( new MemberExpr( dwt, expr->clone() ), env, newCost ) ); 324 renameTypes( alternatives.back().expr ); 325 addAnonConversions( alternatives.back() ); // add anonymous member interpretations whenever an aggregate value type is seen as a member expression. 332 for ( Declaration * decl : members ) { 333 if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType* >( decl ) ) { 334 // addAnonAlternatives uses vector::push_back, which invalidates references to existing elements, so 335 // can't construct in place and use vector::back 336 Alternative newAlt( new MemberExpr( dwt, expr->clone() ), env, newCost ); 337 renameTypes( newAlt.expr ); 338 addAnonConversions( newAlt ); // add anonymous member interpretations whenever an aggregate value type is seen as a member expression. 339 alternatives.push_back( std::move(newAlt) ); 326 340 } else { 327 341 assert( false ); … … 333 347 if ( ConstantExpr * constantExpr = dynamic_cast< ConstantExpr * >( member ) ) { 334 348 // get the value of the constant expression as an int, must be between 0 and the length of the tuple type to have meaning 335 // xxx - this should be improved by memoizing the value of constant exprs 336 // during parsing and reusing that information here. 337 std::stringstream ss( constantExpr->get_constant()->get_value() ); 338 int val = 0; 349 auto val = constantExpr->intValue(); 339 350 std::string tmp; 340 if ( ss >> val && ! (ss >> tmp) ) { 341 if ( val >= 0 && (unsigned int)val < tupleType->size() ) { 342 alternatives.push_back( Alternative( new TupleIndexExpr( expr, val ), env, newCost ) ); 343 } // if 351 if ( val >= 0 && (unsigned long long)val < tupleType->size() ) { 352 alternatives.push_back( Alternative( new TupleIndexExpr( expr->clone(), val ), env, newCost ) ); 344 353 } // if 345 } else if ( NameExpr * nameExpr = dynamic_cast< NameExpr * >( member ) ) {346 // xxx - temporary hack until 0/1 are int constants347 if ( nameExpr->get_name() == "0" || nameExpr->get_name() == "1" ) {348 std::stringstream ss( nameExpr->get_name() );349 int val;350 ss >> val;351 alternatives.push_back( Alternative( new TupleIndexExpr( expr, val ), env, newCost ) );352 }353 354 } // if 354 355 } … … 437 438 return Cost::infinity; 438 439 } 440 } 441 if ( DefaultArgExpr * def = dynamic_cast< DefaultArgExpr * >( *actualExpr ) ) { 442 // default arguments should be free - don't include conversion cost. 443 // Unwrap them here because they are not relevant to the rest of the system. 444 *actualExpr = def->expr; 445 ++formal; 446 continue; 439 447 } 440 448 Type * formalType = (*formal)->get_type(); … … 764 772 ConstantExpr* getDefaultValue( Initializer* init ) { 765 773 if ( SingleInit* si = dynamic_cast<SingleInit*>( init ) ) { 766 if ( CastExpr* ce = dynamic_cast<CastExpr*>( si->get_value() ) ) { 767 return dynamic_cast<ConstantExpr*>( ce->get_arg() ); 774 if ( CastExpr* ce = dynamic_cast<CastExpr*>( si->value ) ) { 775 return dynamic_cast<ConstantExpr*>( ce->arg ); 776 } else { 777 return dynamic_cast<ConstantExpr*>( si->value ); 768 778 } 769 779 } … … 1026 1036 indexer ) ) { 1027 1037 results.emplace_back( 1028 i, cnstExpr, move(env), move(need), move(have),1038 i, new DefaultArgExpr( cnstExpr ), move(env), move(need), move(have), 1029 1039 move(openVars), nextArg, nTuples ); 1030 1040 } … … 1359 1369 funcFinder.findWithAdjustment( untypedExpr->function ); 1360 1370 // if there are no function alternatives, then proceeding is a waste of time. 1371 // xxx - findWithAdjustment throws, so this check and others like it shouldn't be necessary. 1361 1372 if ( funcFinder.alternatives.empty() ) return; 1362 1373 … … 1386 1397 argExpansions.emplace_back(); 1387 1398 auto& argE = argExpansions.back(); 1388 argE.reserve( arg.alternatives.size() );1399 // argE.reserve( arg.alternatives.size() ); 1389 1400 1390 1401 for ( const Alternative& actual : arg ) { … … 1409 1420 std::back_inserter( candidates ) ); 1410 1421 } 1411 } else if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( func->expr-> get_result()->stripReferences() ) ) { // handle ftype (e.g. *? on function pointer)1412 if ( ClassRef eqvClass = func->env.lookup( typeInst-> get_name()) ) {1422 } else if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( func->expr->result->stripReferences() ) ) { // handle ftype (e.g. *? on function pointer) 1423 if ( ClassRef eqvClass = func->env.lookup( typeInst->name ) ) { 1413 1424 if ( FunctionType *function = dynamic_cast< FunctionType* >( eqvClass.get_bound().type ) ) { 1414 1425 Alternative newFunc( *func ); … … 1665 1676 Cost cost = Cost::zero; 1666 1677 Expression * newExpr = data.combine( cost ); 1667 alternatives.push_back( Alternative( newExpr, env, Cost::zero, cost ) ); 1678 1679 // addAnonAlternatives uses vector::push_back, which invalidates references to existing elements, so 1680 // can't construct in place and use vector::back 1681 Alternative newAlt( newExpr, env, Cost::zero, cost ); 1668 1682 PRINT( 1669 1683 std::cerr << "decl is "; … … 1674 1688 std::cerr << std::endl; 1675 1689 ) 1676 renameTypes( alternatives.back().expr ); 1677 addAnonConversions( alternatives.back() ); // add anonymous member interpretations whenever an aggregate value type is seen as a name expression. 1690 renameTypes( newAlt.expr ); 1691 addAnonConversions( newAlt ); // add anonymous member interpretations whenever an aggregate value type is seen as a name expression. 1692 alternatives.push_back( std::move(newAlt) ); 1678 1693 } // for 1679 1694 } … … 2042 2057 assertf( false, "AlternativeFinder should never see a DeletedExpr." ); 2043 2058 } 2059 2060 void AlternativeFinder::Finder::postvisit( GenericExpr * ) { 2061 assertf( false, "_Generic is not yet supported." ); 2062 } 2044 2063 } // namespace ResolvExpr 2045 2064
Note:
See TracChangeset
for help on using the changeset viewer.