Changeset 906e24d for src/ResolvExpr
- Timestamp:
- Sep 15, 2016, 10:17:16 AM (9 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- aa8f9df
- Parents:
- 96a10cd
- git-author:
- Rob Schluntz <rschlunt@…> (09/14/16 22:32:34)
- git-committer:
- Rob Schluntz <rschlunt@…> (09/15/16 10:17:16)
- Location:
- src/ResolvExpr
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified src/ResolvExpr/Alternative.cc ¶
r96a10cd r906e24d 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // Alternative.cc -- 7 // Alternative.cc -- 8 8 // 9 9 // Author : Richard C. Bilson … … 12 12 // Last Modified On : Sat May 16 23:54:23 2015 13 13 // Update Count : 2 14 // 14 // 15 15 16 16 #include "Alternative.h" … … 54 54 expr->print( os, indent ); 55 55 os << "(types:" << std::endl; 56 printAll( expr->get_results(), os, indent + 4 ); 57 os << ")" << std::endl; 56 os << std::string( indent+4, ' ' ); 57 expr->get_result()->print( os, indent + 4 ); 58 os << std::endl << ")" << std::endl; 58 59 } else { 59 60 os << "Null expression!" << std::endl; -
TabularUnified src/ResolvExpr/AlternativeFinder.cc ¶
r96a10cd r906e24d 101 101 PruneStruct current( candidate ); 102 102 std::string mangleName; 103 for ( std::list< Type* >::const_iterator retType = candidate->expr->get_results().begin(); retType != candidate->expr->get_results().end(); ++retType ){104 Type * newType = (*retType)->clone();103 { 104 Type * newType = candidate->expr->get_result()->clone(); 105 105 candidate->env.apply( newType ); 106 mangleName += SymTab::Mangler::mangle( newType );106 mangleName = SymTab::Mangler::mangle( newType ); 107 107 delete newType; 108 108 } … … 133 133 if ( ! target->second.isAmbiguous ) { 134 134 Alternative &alt = *target->second.candidate; 135 for ( std::list< Type* >::iterator result = alt.expr->get_results().begin(); result != alt.expr->get_results().end(); ++result ) { 136 alt.env.applyFree( *result ); 137 } 135 alt.env.applyFree( alt.expr->get_result() ); 138 136 *out++ = alt; 139 137 } 140 138 } 141 142 139 } 143 140 … … 170 167 171 168 void renameTypes( Expression *expr ) { 172 for ( std::list< Type* >::iterator i = expr->get_results().begin(); i != expr->get_results().end(); ++i ) { 173 (*i)->accept( global_renamer ); 174 } 169 expr->get_result()->accept( global_renamer ); 175 170 } 176 171 } … … 204 199 for ( AltList::iterator i = alternatives.begin(); i != alternatives.end(); ++i ) { 205 200 if ( adjust ) { 206 adjustExprType List( i->expr->get_results().begin(), i->expr->get_results().end(), i->env, indexer );201 adjustExprType( i->expr->get_result(), i->env, indexer ); 207 202 } 208 203 } … … 259 254 260 255 Cost computeConversionCost( Alternative &alt, const SymTab::Indexer &indexer ) { 261 ApplicationExpr *appExpr = dynamic_cast< ApplicationExpr* >( alt.expr ); 262 assert( appExpr ); 263 PointerType *pointer = dynamic_cast< PointerType* >( appExpr->get_function()->get_results().front() ); 264 assert( pointer ); 265 FunctionType *function = dynamic_cast< FunctionType* >( pointer->get_base() ); 266 assert( function ); 256 ApplicationExpr *appExpr = safe_dynamic_cast< ApplicationExpr* >( alt.expr ); 257 PointerType *pointer = safe_dynamic_cast< PointerType* >( appExpr->get_function()->get_result() ); 258 FunctionType *function = safe_dynamic_cast< FunctionType* >( pointer->get_base() ); 267 259 268 260 Cost convCost( 0, 0, 0 ); … … 275 267 (*actualExpr)->print( std::cerr, 8 ); 276 268 std::cerr << "--- results are" << std::endl; 277 printAll( (*actualExpr)->get_results(),std::cerr, 8 );269 (*actualExpr)->get_result()->print( std::cerr, 8 ); 278 270 ) 279 271 std::list< DeclarationWithType* >::iterator startFormal = formal; 280 272 Cost actualCost; 281 for ( std::list< Type* >::iterator actual = (*actualExpr)->get_results().begin(); actual != (*actualExpr)->get_results().end(); ++actual ) { 273 // xxx - tuple type matching 274 std::list< Type * > flatActualTypes; 275 flatten( (*actualExpr)->get_result(), back_inserter( flatActualTypes ) ); 276 for ( std::list< Type* >::iterator actual = flatActualTypes.begin(); actual != flatActualTypes.end(); ++actual ) { 282 277 if ( formal == formals.end() ) { 283 278 if ( function->get_isVarArgs() ) { … … 383 378 std::list< DeclarationWithType* >::iterator formal = formals.begin(); 384 379 for ( AltList::const_iterator actualExpr = actuals.begin(); actualExpr != actuals.end(); ++actualExpr ) { 385 for ( std::list< Type* >::iterator actual = actualExpr->expr->get_results().begin(); actual != actualExpr->expr->get_results().end(); ++actual ) { 380 std::list< Type * > flatActualTypes; 381 flatten( actualExpr->expr->get_result(), back_inserter( flatActualTypes ) ); 382 for ( std::list< Type* >::iterator actual = flatActualTypes.begin(); actual != flatActualTypes.end(); ++actual ) { 386 383 if ( formal == formals.end() ) { 387 384 return isVarArgs; … … 500 497 //if ( newNeedParents[ curDecl->get_uniqueId() ][ candDecl->get_uniqueId() ]++ > recursionParentLimit ) continue; 501 498 Expression *varExpr = new VariableExpr( candDecl ); 502 deleteAll( varExpr->get_results() ); 503 varExpr->get_results().clear(); 504 varExpr->get_results().push_front( adjType->clone() ); 499 delete varExpr->get_result(); 500 varExpr->set_result( adjType->clone() ); 505 501 PRINT( 506 502 std::cerr << "satisfying assertion " << curDecl->get_uniqueId() << " "; … … 574 570 PointerType pt( Type::Qualifiers(), v.clone() ); 575 571 UntypedExpr *vexpr = untypedExpr->clone(); 576 vexpr-> get_results().push_front( pt.clone() );572 vexpr->set_result( pt.clone() ); 577 573 alternatives.push_back( Alternative( vexpr, env, Cost()) ); 578 574 return; … … 604 600 // check if the type is pointer to function 605 601 PointerType *pointer; 606 if ( func->expr->get_results().size() == 1 && ( pointer = dynamic_cast< PointerType* >( func->expr->get_results().front() ) ) ) {602 if ( ( pointer = dynamic_cast< PointerType* >( func->expr->get_result() ) ) ) { 607 603 if ( FunctionType *function = dynamic_cast< FunctionType* >( pointer->get_base() ) ) { 608 604 for ( std::list< AltList >::iterator actualAlt = possibilities.begin(); actualAlt != possibilities.end(); ++actualAlt ) { … … 640 636 // check if the type is pointer to function 641 637 PointerType *pointer; 642 if ( funcOp->expr->get_results().size() == 1 643 && ( pointer = dynamic_cast< PointerType* >( funcOp->expr->get_results().front() ) ) ) { 638 if ( ( pointer = dynamic_cast< PointerType* >( funcOp->expr->get_result() ) ) ) { 644 639 if ( FunctionType *function = dynamic_cast< FunctionType* >( pointer->get_base() ) ) { 645 640 for ( std::list< AltList >::iterator actualAlt = possibilities.begin(); actualAlt != possibilities.end(); ++actualAlt ) { … … 665 660 666 661 PRINT( 667 ApplicationExpr *appExpr = dynamic_cast< ApplicationExpr* >( withFunc->expr ); 668 assert( appExpr ); 669 PointerType *pointer = dynamic_cast< PointerType* >( appExpr->get_function()->get_results().front() ); 670 assert( pointer ); 671 FunctionType *function = dynamic_cast< FunctionType* >( pointer->get_base() ); 672 assert( function ); 662 ApplicationExpr *appExpr = safe_dynamic_cast< ApplicationExpr* >( withFunc->expr ); 663 PointerType *pointer = safe_dynamic_cast< PointerType* >( appExpr->get_function()->get_result() ); 664 FunctionType *function = safe_dynamic_cast< FunctionType* >( pointer->get_base() ); 673 665 std::cerr << "Case +++++++++++++" << std::endl; 674 666 std::cerr << "formals are:" << std::endl; … … 692 684 693 685 bool isLvalue( Expression *expr ) { 694 for ( std::list< Type* >::const_iterator i = expr->get_results().begin(); i != expr->get_results().end(); ++i ) { 695 if ( !(*i)->get_isLvalue() ) return false; 696 } // for 697 return true; 686 // xxx - recurse into tuples? 687 return expr->has_result() && expr->get_result()->get_isLvalue(); 698 688 } 699 689 … … 709 699 710 700 void AlternativeFinder::visit( CastExpr *castExpr ) { 711 for ( std::list< Type* >::iterator i = castExpr->get_results().begin(); i != castExpr->get_results().end(); ++i ) { 712 *i = resolveTypeof( *i, indexer ); 713 SymTab::validateType( *i, &indexer ); 714 adjustExprType( *i, env, indexer ); 715 } // for 701 Type *& toType = castExpr->get_result(); 702 toType = resolveTypeof( toType, indexer ); 703 SymTab::validateType( toType, &indexer ); 704 adjustExprType( toType, env, indexer ); 716 705 717 706 AlternativeFinder finder( indexer, env ); … … 727 716 // that are cast directly. The candidate is invalid if it has fewer results than there are types to cast 728 717 // to. 729 int discardedValues = (*i).expr->get_result s().size() - castExpr->get_results().size();718 int discardedValues = (*i).expr->get_result()->size() - castExpr->get_result()->size(); 730 719 if ( discardedValues < 0 ) continue; 731 std::list< Type* >::iterator candidate_end = (*i).expr->get_results().begin(); 732 std::advance( candidate_end, castExpr->get_results().size() ); 720 // xxx - may need to go into tuple types and extract relavent types and use unifyList 733 721 // unification run for side-effects 734 unifyList( castExpr->get_results().begin(), castExpr->get_results().end(), 735 (*i).expr->get_results().begin(), candidate_end, 736 i->env, needAssertions, haveAssertions, openVars, indexer ); 737 Cost thisCost = castCostList( (*i).expr->get_results().begin(), candidate_end, 738 castExpr->get_results().begin(), castExpr->get_results().end(), 739 indexer, i->env ); 722 unify( castExpr->get_result(), (*i).expr->get_result(), i->env, needAssertions, haveAssertions, openVars, indexer ); 723 Cost thisCost = castCost( (*i).expr->get_result(), castExpr->get_result(), indexer, i->env ); 740 724 if ( thisCost != Cost::infinity ) { 741 725 // count one safe conversion for each value that is thrown away … … 760 744 761 745 for ( AltList::const_iterator agg = funcFinder.alternatives.begin(); agg != funcFinder.alternatives.end(); ++agg ) { 762 if ( agg->expr->get_results().size() == 1 ) { 763 if ( StructInstType *structInst = dynamic_cast< StructInstType* >( agg->expr->get_results().front() ) ) { 764 addAggMembers( structInst, agg->expr, agg->cost, agg->env, memberExpr->get_member() ); 765 } else if ( UnionInstType *unionInst = dynamic_cast< UnionInstType* >( agg->expr->get_results().front() ) ) { 766 addAggMembers( unionInst, agg->expr, agg->cost, agg->env, memberExpr->get_member() ); 767 } // if 746 if ( StructInstType *structInst = dynamic_cast< StructInstType* >( agg->expr->get_result() ) ) { 747 addAggMembers( structInst, agg->expr, agg->cost, agg->env, memberExpr->get_member() ); 748 } else if ( UnionInstType *unionInst = dynamic_cast< UnionInstType* >( agg->expr->get_result() ) ) { 749 addAggMembers( unionInst, agg->expr, agg->cost, agg->env, memberExpr->get_member() ); 768 750 } // if 769 751 } // for … … 894 876 alternatives.push_back( Alternative( new AttrExpr( new VariableExpr( funcDecl ), argType->clone() ), env, Cost::zero ) ); 895 877 for ( std::list< DeclarationWithType* >::iterator i = function->get_returnVals().begin(); i != function->get_returnVals().end(); ++i ) { 896 alternatives.back().expr-> get_results().push_back( (*i)->get_type()->clone() );878 alternatives.back().expr->set_result( (*i)->get_type()->clone() ); 897 879 } // for 898 880 } // if … … 917 899 finder.find( attrExpr->get_expr() ); 918 900 for ( AltList::iterator choice = finder.alternatives.begin(); choice != finder.alternatives.end(); ++choice ) { 919 if ( choice->expr->get_result s().size() == 1 ) {920 resolveAttr(*i, function, choice->expr->get_result s().front(), choice->env );901 if ( choice->expr->get_result()->size() == 1 ) { 902 resolveAttr(*i, function, choice->expr->get_result(), choice->env ); 921 903 } // fi 922 904 } // for … … 960 942 AssertionSet needAssertions, haveAssertions; 961 943 Alternative newAlt( 0, third->env, first->cost + second->cost + third->cost ); 962 std::list< Type* > commonTypes;963 if ( unify List( second->expr->get_results().begin(), second->expr->get_results().end(), third->expr->get_results().begin(), third->expr->get_results().end(), newAlt.env, needAssertions, haveAssertions, openVars, indexer, commonTypes) ) {944 Type* commonType; 945 if ( unify( second->expr->get_result(), third->expr->get_result(), newAlt.env, needAssertions, haveAssertions, openVars, indexer, commonType ) ) { 964 946 ConditionalExpr *newExpr = new ConditionalExpr( first->expr->clone(), second->expr->clone(), third->expr->clone() ); 965 std::list< Type* >::const_iterator original = second->expr->get_results().begin(); 966 std::list< Type* >::const_iterator commonType = commonTypes.begin(); 967 for ( ; original != second->expr->get_results().end() && commonType != commonTypes.end(); ++original, ++commonType ) { 968 if ( *commonType ) { 969 newExpr->get_results().push_back( *commonType ); 970 } else { 971 newExpr->get_results().push_back( (*original)->clone() ); 972 } // if 973 } // for 947 newExpr->set_result( commonType ? commonType : second->expr->get_result()->clone() ); 974 948 newAlt.expr = newExpr; 975 949 inferParameters( needAssertions, haveAssertions, newAlt, openVars, back_inserter( alternatives ) ); … … 999 973 TupleExpr *newExpr = new TupleExpr; 1000 974 makeExprList( *i, newExpr->get_exprs() ); 1001 for ( std::list< Expression* >::const_iterator resultExpr = newExpr->get_exprs().begin(); resultExpr != newExpr->get_exprs().end(); ++resultExpr ) { 1002 for ( std::list< Type* >::const_iterator resultType = (*resultExpr)->get_results().begin(); resultType != (*resultExpr)->get_results().end(); ++resultType ) { 1003 newExpr->get_results().push_back( (*resultType)->clone() ); 1004 } // for 975 for ( Expression * resultExpr : newExpr->get_exprs() ) { 976 newExpr->set_result( resultExpr->get_result()->clone() ); 1005 977 } // for 1006 978 -
TabularUnified src/ResolvExpr/AlternativePrinter.cc ¶
r96a10cd r906e24d 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // AlternativePrinter.cc -- 7 // AlternativePrinter.cc -- 8 8 // 9 9 // Author : Richard C. Bilson … … 33 33 for ( AltList::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { 34 34 os << "Alternative " << count++ << " ==============" << std::endl; 35 printAll( i->expr->get_results(),os );35 i->expr->get_result()->print( os ); 36 36 // i->print( os ); 37 37 os << std::endl; -
TabularUnified src/ResolvExpr/ResolveTypeof.cc ¶
r96a10cd r906e24d 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // ResolveTypeof.cc -- 7 // ResolveTypeof.cc -- 8 8 // 9 9 // Author : Richard C. Bilson … … 58 58 if ( typeofType->get_expr() ) { 59 59 Expression *newExpr = resolveInVoidContext( typeofType->get_expr(), indexer ); 60 assert( newExpr->get_results().size() > 0 ); 61 Type *newType; 62 if ( newExpr->get_results().size() > 1 ) { 63 TupleType *tupleType = new TupleType( Type::Qualifiers() ); 64 cloneAll( newExpr->get_results(), tupleType->get_types() ); 65 newType = tupleType; 66 } else { 67 newType = newExpr->get_results().front()->clone(); 68 } // if 60 assert( newExpr->has_result() && ! newExpr->get_result()->isVoid() ); 61 Type *newType = newExpr->get_result(); 69 62 delete typeofType; 70 63 return newType; -
TabularUnified src/ResolvExpr/Resolver.cc ¶
r96a10cd r906e24d 19 19 #include "RenameVars.h" 20 20 #include "ResolveTypeof.h" 21 #include "typeops.h" 21 22 #include "SynTree/Statement.h" 22 23 #include "SynTree/Type.h" … … 67 68 void resolveSingleAggrInit( Declaration *, InitIterator &, InitIterator & ); 68 69 void fallbackInit( ConstructorInit * ctorInit ); 69 std::list< Type * >functionReturn;70 Type * functionReturn; 70 71 Type *initContext; 71 72 Type *switchType; … … 155 156 const TypeEnvironment *newEnv = 0; 156 157 for ( AltList::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { 157 if ( i->expr->get_result s().size() == 1 && isIntegralType( i->expr->get_results().front() ) ) {158 if ( i->expr->get_result()->size() == 1 && isIntegralType( i->expr->get_result() ) ) { 158 159 if ( newExpr ) { 159 160 throw SemanticError( "Too many interpretations for case control expression", untyped ); … … 232 233 Type *new_type = resolveTypeof( functionDecl->get_type(), *this ); 233 234 functionDecl->set_type( new_type ); 234 std::list< Type * > oldFunctionReturn = functionReturn; 235 functionReturn.clear(); 236 for ( std::list< DeclarationWithType * >::const_iterator i = functionDecl->get_functionType()->get_returnVals().begin(); i != functionDecl->get_functionType()->get_returnVals().end(); ++i ) { 237 functionReturn.push_back( (*i)->get_type() ); 238 } // for 235 ValueGuard< Type * > oldFunctionReturn( functionReturn ); 236 functionReturn = ResolvExpr::extractResultType( functionDecl->get_functionType() ); 239 237 SymTab::Indexer::visit( functionDecl ); 240 functionReturn = oldFunctionReturn;241 238 } 242 239 … … 336 333 void Resolver::visit( ReturnStmt *returnStmt ) { 337 334 if ( returnStmt->get_expr() ) { 338 CastExpr *castExpr = new CastExpr( returnStmt->get_expr() ); 339 cloneAll( functionReturn, castExpr->get_results() ); 335 CastExpr *castExpr = new CastExpr( returnStmt->get_expr(), functionReturn->clone() ); 340 336 Expression *newExpr = findSingleExpression( castExpr, *this ); 341 337 delete castExpr; … … 382 378 if ( isCharType( at->get_base() ) ) { 383 379 // check if the resolved type is char * 384 if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_result s().front() ) ) {380 if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_result() ) ) { 385 381 if ( isCharType( pt->get_base() ) ) { 386 382 // strip cast if we're initializing a char[] with a char *, e.g. char x[] = "hello"; -
TabularUnified src/ResolvExpr/Unify.cc ¶
r96a10cd r906e24d 588 588 } 589 589 590 // xxx - compute once and store in the FunctionType? 591 Type * extractResultType( FunctionType * function ) { 592 if ( function->get_returnVals().size() == 0 ) { 593 return new VoidType( Type::Qualifiers() ); 594 } else if ( function->get_returnVals().size() == 1 ) { 595 return function->get_returnVals().front()->get_type()->clone(); 596 } else { 597 TupleType * tupleType = new TupleType( Type::Qualifiers() ); 598 for ( DeclarationWithType * decl : function->get_returnVals() ) { 599 tupleType->get_types().push_back( decl->get_type()->clone() ); 600 } // for 601 return tupleType; 602 } 603 } 604 590 605 } // namespace ResolvExpr 591 606 -
TabularUnified src/ResolvExpr/typeops.h ¶
r96a10cd r906e24d 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // typeops.h -- 7 // typeops.h -- 8 8 // 9 9 // Author : Richard C. Bilson … … 30 30 typedef typename InputIterator::value_type SetType; 31 31 typedef typename std::list< typename SetType::value_type > ListType; 32 32 33 33 if ( begin == end ) { 34 34 *out++ = ListType(); 35 35 return; 36 36 } // if 37 37 38 38 InputIterator current = begin; 39 39 begin++; … … 41 41 std::list< ListType > recursiveResult; 42 42 combos( begin, end, back_inserter( recursiveResult ) ); 43 43 44 44 for ( typename std::list< ListType >::const_iterator i = recursiveResult.begin(); i != recursiveResult.end(); ++i ) { 45 45 for ( typename ListType::const_iterator j = current->begin(); j != current->end(); ++j ) { … … 52 52 } // for 53 53 } 54 54 55 55 // in AdjustExprType.cc 56 56 /// Replaces array types with the equivalent pointer, and function types with a pointer-to-function … … 144 144 } 145 145 146 /// creates the type represented by the list of returnVals in a FunctionType. The caller owns the return value. 147 Type * extractResultType( FunctionType * functionType ); 148 146 149 // in CommonType.cc 147 150 Type *commonType( Type *type1, Type *type2, bool widenFirst, bool widenSecond, const SymTab::Indexer &indexer, TypeEnvironment &env, const OpenVarSet &openVars ); … … 152 155 // in Occurs.cc 153 156 bool occurs( Type *type, std::string varName, const TypeEnvironment &env ); 157 158 // flatten tuple type into list of types 159 template< typename OutputIterator > 160 void flatten( Type * type, OutputIterator out ) { 161 if ( TupleType * tupleType = dynamic_cast< TupleType * >( type ) ) { 162 for ( Type * t : tupleType->get_types() ) { 163 flatten( t, out ); 164 } 165 } else { 166 *out++ = type; 167 } 168 } 154 169 } // namespace ResolvExpr 155 170
Note: See TracChangeset
for help on using the changeset viewer.