Changeset aa8f9df for src/ResolvExpr
- Timestamp:
- Sep 15, 2016, 3:22:50 PM (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:
- 4ab9536
- Parents:
- fd782b2 (diff), 906e24d (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. - Location:
- src/ResolvExpr
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified src/ResolvExpr/Alternative.cc ¶
rfd782b2 raa8f9df 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 ¶
rfd782b2 raa8f9df 100 100 PruneStruct current( candidate ); 101 101 std::string mangleName; 102 for ( std::list< Type* >::const_iterator retType = candidate->expr->get_results().begin(); retType != candidate->expr->get_results().end(); ++retType ){103 Type * newType = (*retType)->clone();102 { 103 Type * newType = candidate->expr->get_result()->clone(); 104 104 candidate->env.apply( newType ); 105 mangleName += SymTab::Mangler::mangle( newType );105 mangleName = SymTab::Mangler::mangle( newType ); 106 106 delete newType; 107 107 } … … 132 132 if ( ! target->second.isAmbiguous ) { 133 133 Alternative &alt = *target->second.candidate; 134 for ( std::list< Type* >::iterator result = alt.expr->get_results().begin(); result != alt.expr->get_results().end(); ++result ) { 135 alt.env.applyFree( *result ); 136 } 134 alt.env.applyFree( alt.expr->get_result() ); 137 135 *out++ = alt; 138 136 } 139 137 } 140 141 138 } 142 139 … … 149 146 150 147 void renameTypes( Expression *expr ) { 151 for ( std::list< Type* >::iterator i = expr->get_results().begin(); i != expr->get_results().end(); ++i ) { 152 (*i)->accept( global_renamer ); 153 } 154 } 155 156 // flatten tuple type into list of types 157 template< typename OutputIterator > 158 void flatten( Type * type, OutputIterator out ) { 159 if ( TupleType * tupleType = dynamic_cast< TupleType * >( type ) ) { 160 for ( Type * t : *tupleType ) { 161 flatten( t, out ); 162 } 163 } else { 164 *out++ = type; 165 } 148 expr->get_result()->accept( global_renamer ); 166 149 } 167 150 } … … 195 178 for ( AltList::iterator i = alternatives.begin(); i != alternatives.end(); ++i ) { 196 179 if ( adjust ) { 197 adjustExprType List( i->expr->get_results().begin(), i->expr->get_results().end(), i->env, indexer );180 adjustExprType( i->expr->get_result(), i->env, indexer ); 198 181 } 199 182 } … … 265 248 Cost computeConversionCost( Alternative &alt, const SymTab::Indexer &indexer ) { 266 249 ApplicationExpr *appExpr = safe_dynamic_cast< ApplicationExpr* >( alt.expr ); 267 PointerType *pointer = safe_dynamic_cast< PointerType* >( appExpr->get_function()->get_result s().front() );250 PointerType *pointer = safe_dynamic_cast< PointerType* >( appExpr->get_function()->get_result() ); 268 251 FunctionType *function = safe_dynamic_cast< FunctionType* >( pointer->get_base() ); 269 252 … … 282 265 (*actualExpr)->print( std::cerr, 8 ); 283 266 std::cerr << "--- results are" << std::endl; 284 printAll( (*actualExpr)->get_results(),std::cerr, 8 );267 (*actualExpr)->get_result()->print( std::cerr, 8 ); 285 268 ) 286 269 std::list< DeclarationWithType* >::iterator startFormal = formal; 287 270 Cost actualCost; 288 for ( std::list< Type* >::iterator actualType = (*actualExpr)->get_results().begin(); actualType != (*actualExpr)->get_results().end(); ++actualType ) { 271 std::list< Type * > flatActualTypes; 272 flatten( (*actualExpr)->get_result(), back_inserter( flatActualTypes ) ); 273 for ( std::list< Type* >::iterator actualType = flatActualTypes.begin(); actualType != flatActualTypes.end(); ++actualType ) { 274 289 275 290 276 // tuple handling code … … 399 385 400 386 for ( AltList::const_iterator actualExpr = actuals.begin(); actualExpr != actuals.end(); ++actualExpr ) { 401 std::list< Type* > & actualTypes = actualExpr->expr->get_results(); 402 for ( std::list< Type* >::iterator actualType = actualTypes.begin(); actualType != actualTypes.end(); ++actualType ) { 387 std::list< Type * > flatActualTypes; 388 flatten( actualExpr->expr->get_result(), back_inserter( flatActualTypes ) ); 389 for ( std::list< Type* >::iterator actualType = flatActualTypes.begin(); actualType != flatActualTypes.end(); ++actualType, ++formalType ) { 403 390 if ( formalType == formalTypes.end() ) { 404 391 // the type of the formal parameter may be a tuple type. To make this easier to work with, … … 416 403 PRINT( 417 404 std::cerr << "formal type is "; 418 (*formal )->get_type()->print( std::cerr );405 (*formalType)->print( std::cerr ); 419 406 std::cerr << std::endl << "actual type is "; 420 407 (*actualType)->print( std::cerr ); … … 424 411 return false; 425 412 } 426 ++formalType;427 413 } 428 414 } … … 532 518 //if ( newNeedParents[ curDecl->get_uniqueId() ][ candDecl->get_uniqueId() ]++ > recursionParentLimit ) continue; 533 519 Expression *varExpr = new VariableExpr( candDecl ); 534 deleteAll( varExpr->get_results() ); 535 varExpr->get_results().clear(); 536 varExpr->get_results().push_front( adjType->clone() ); 520 delete varExpr->get_result(); 521 varExpr->set_result( adjType->clone() ); 537 522 PRINT( 538 523 std::cerr << "satisfying assertion " << curDecl->get_uniqueId() << " "; … … 606 591 PointerType pt( Type::Qualifiers(), v.clone() ); 607 592 UntypedExpr *vexpr = untypedExpr->clone(); 608 vexpr-> get_results().push_front( pt.clone() );593 vexpr->set_result( pt.clone() ); 609 594 alternatives.push_back( Alternative( vexpr, env, Cost()) ); 610 595 return; … … 634 619 // check if the type is pointer to function 635 620 PointerType *pointer; 636 if ( func->expr->get_results().size() == 1 && ( pointer = dynamic_cast< PointerType* >( func->expr->get_results().front() ) ) ) {621 if ( ( pointer = dynamic_cast< PointerType* >( func->expr->get_result() ) ) ) { 637 622 if ( FunctionType *function = dynamic_cast< FunctionType* >( pointer->get_base() ) ) { 638 623 for ( std::list< AltList >::iterator actualAlt = possibilities.begin(); actualAlt != possibilities.end(); ++actualAlt ) { … … 670 655 // check if the type is pointer to function 671 656 PointerType *pointer; 672 if ( funcOp->expr->get_results().size() == 1 673 && ( pointer = dynamic_cast< PointerType* >( funcOp->expr->get_results().front() ) ) ) { 657 if ( ( pointer = dynamic_cast< PointerType* >( funcOp->expr->get_result() ) ) ) { 674 658 if ( FunctionType *function = dynamic_cast< FunctionType* >( pointer->get_base() ) ) { 675 659 for ( std::list< AltList >::iterator actualAlt = possibilities.begin(); actualAlt != possibilities.end(); ++actualAlt ) { … … 696 680 PRINT( 697 681 ApplicationExpr *appExpr = safe_dynamic_cast< ApplicationExpr* >( withFunc->expr ); 698 PointerType *pointer = safe_dynamic_cast< PointerType* >( appExpr->get_function()->get_result s().front() );682 PointerType *pointer = safe_dynamic_cast< PointerType* >( appExpr->get_function()->get_result() ); 699 683 FunctionType *function = safe_dynamic_cast< FunctionType* >( pointer->get_base() ); 700 684 std::cerr << "Case +++++++++++++" << std::endl; … … 719 703 720 704 bool isLvalue( Expression *expr ) { 721 for ( std::list< Type* >::const_iterator i = expr->get_results().begin(); i != expr->get_results().end(); ++i ) { 722 if ( !(*i)->get_isLvalue() ) return false; 723 } // for 724 return true; 705 // xxx - recurse into tuples? 706 return expr->has_result() && expr->get_result()->get_isLvalue(); 725 707 } 726 708 … … 736 718 737 719 void AlternativeFinder::visit( CastExpr *castExpr ) { 738 for ( std::list< Type* >::iterator i = castExpr->get_results().begin(); i != castExpr->get_results().end(); ++i ) { 739 *i = resolveTypeof( *i, indexer ); 740 SymTab::validateType( *i, &indexer ); 741 adjustExprType( *i, env, indexer ); 742 } // for 720 Type *& toType = castExpr->get_result(); 721 toType = resolveTypeof( toType, indexer ); 722 SymTab::validateType( toType, &indexer ); 723 adjustExprType( toType, env, indexer ); 743 724 744 725 AlternativeFinder finder( indexer, env ); … … 754 735 // that are cast directly. The candidate is invalid if it has fewer results than there are types to cast 755 736 // to. 756 int discardedValues = (*i).expr->get_result s().size() - castExpr->get_results().size();737 int discardedValues = (*i).expr->get_result()->size() - castExpr->get_result()->size(); 757 738 if ( discardedValues < 0 ) continue; 758 std::list< Type* >::iterator candidate_end = (*i).expr->get_results().begin(); 759 std::advance( candidate_end, castExpr->get_results().size() ); 739 // xxx - may need to go into tuple types and extract relavent types and use unifyList 760 740 // unification run for side-effects 761 unifyList( castExpr->get_results().begin(), castExpr->get_results().end(), 762 (*i).expr->get_results().begin(), candidate_end, 763 i->env, needAssertions, haveAssertions, openVars, indexer ); 764 Cost thisCost = castCostList( (*i).expr->get_results().begin(), candidate_end, 765 castExpr->get_results().begin(), castExpr->get_results().end(), 766 indexer, i->env ); 741 unify( castExpr->get_result(), (*i).expr->get_result(), i->env, needAssertions, haveAssertions, openVars, indexer ); 742 Cost thisCost = castCost( (*i).expr->get_result(), castExpr->get_result(), indexer, i->env ); 767 743 if ( thisCost != Cost::infinity ) { 768 744 // count one safe conversion for each value that is thrown away … … 787 763 788 764 for ( AltList::const_iterator agg = funcFinder.alternatives.begin(); agg != funcFinder.alternatives.end(); ++agg ) { 789 if ( agg->expr->get_results().size() == 1 ) { 790 if ( StructInstType *structInst = dynamic_cast< StructInstType* >( agg->expr->get_results().front() ) ) { 791 addAggMembers( structInst, agg->expr, agg->cost, agg->env, memberExpr->get_member() ); 792 } else if ( UnionInstType *unionInst = dynamic_cast< UnionInstType* >( agg->expr->get_results().front() ) ) { 793 addAggMembers( unionInst, agg->expr, agg->cost, agg->env, memberExpr->get_member() ); 794 } // if 765 if ( StructInstType *structInst = dynamic_cast< StructInstType* >( agg->expr->get_result() ) ) { 766 addAggMembers( structInst, agg->expr, agg->cost, agg->env, memberExpr->get_member() ); 767 } else if ( UnionInstType *unionInst = dynamic_cast< UnionInstType* >( agg->expr->get_result() ) ) { 768 addAggMembers( unionInst, agg->expr, agg->cost, agg->env, memberExpr->get_member() ); 795 769 } // if 796 770 } // for … … 923 897 alternatives.push_back( Alternative( new AttrExpr( new VariableExpr( funcDecl ), argType->clone() ), env, Cost::zero ) ); 924 898 for ( std::list< DeclarationWithType* >::iterator i = function->get_returnVals().begin(); i != function->get_returnVals().end(); ++i ) { 925 alternatives.back().expr-> get_results().push_back( (*i)->get_type()->clone() );899 alternatives.back().expr->set_result( (*i)->get_type()->clone() ); 926 900 } // for 927 901 } // if … … 946 920 finder.find( attrExpr->get_expr() ); 947 921 for ( AltList::iterator choice = finder.alternatives.begin(); choice != finder.alternatives.end(); ++choice ) { 948 if ( choice->expr->get_result s().size() == 1 ) {949 resolveAttr(*i, function, choice->expr->get_result s().front(), choice->env );922 if ( choice->expr->get_result()->size() == 1 ) { 923 resolveAttr(*i, function, choice->expr->get_result(), choice->env ); 950 924 } // fi 951 925 } // for … … 989 963 AssertionSet needAssertions, haveAssertions; 990 964 Alternative newAlt( 0, third->env, first->cost + second->cost + third->cost ); 991 std::list< Type* > commonTypes;992 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) ) {965 Type* commonType; 966 if ( unify( second->expr->get_result(), third->expr->get_result(), newAlt.env, needAssertions, haveAssertions, openVars, indexer, commonType ) ) { 993 967 ConditionalExpr *newExpr = new ConditionalExpr( first->expr->clone(), second->expr->clone(), third->expr->clone() ); 994 std::list< Type* >::const_iterator original = second->expr->get_results().begin(); 995 std::list< Type* >::const_iterator commonType = commonTypes.begin(); 996 for ( ; original != second->expr->get_results().end() && commonType != commonTypes.end(); ++original, ++commonType ) { 997 if ( *commonType ) { 998 newExpr->get_results().push_back( *commonType ); 999 } else { 1000 newExpr->get_results().push_back( (*original)->clone() ); 1001 } // if 1002 } // for 968 newExpr->set_result( commonType ? commonType : second->expr->get_result()->clone() ); 1003 969 newAlt.expr = newExpr; 1004 970 inferParameters( needAssertions, haveAssertions, newAlt, openVars, back_inserter( alternatives ) ); … … 1028 994 TupleExpr *newExpr = new TupleExpr; 1029 995 makeExprList( *i, newExpr->get_exprs() ); 1030 for ( std::list< Expression* >::const_iterator resultExpr = newExpr->get_exprs().begin(); resultExpr != newExpr->get_exprs().end(); ++resultExpr ) { 1031 for ( std::list< Type* >::const_iterator resultType = (*resultExpr)->get_results().begin(); resultType != (*resultExpr)->get_results().end(); ++resultType ) { 1032 newExpr->get_results().push_back( (*resultType)->clone() ); 1033 } // for 996 TupleType *tupleType = new TupleType( Type::Qualifiers(true, true, true, true, true, true) ); 997 Type::Qualifiers &qualifiers = tupleType->get_qualifiers(); 998 for ( Expression * resultExpr : newExpr->get_exprs() ) { 999 Type * type = resultExpr->get_result()->clone(); 1000 tupleType->get_types().push_back( type ); 1001 qualifiers &= type->get_qualifiers(); 1034 1002 } // for 1003 newExpr->set_result( tupleType ); 1035 1004 1036 1005 TypeEnvironment compositeEnv; … … 1057 1026 alternatives.push_back( Alternative( tupleExpr->clone(), env, Cost::zero ) ); 1058 1027 } 1028 1029 void AlternativeFinder::visit( TupleAssignExpr *tupleAssignExpr ) { 1030 alternatives.push_back( Alternative( tupleAssignExpr->clone(), env, Cost::zero ) ); 1031 } 1059 1032 } // namespace ResolvExpr 1060 1033 -
TabularUnified src/ResolvExpr/AlternativeFinder.h ¶
rfd782b2 raa8f9df 68 68 virtual void visit( ConstructorExpr * ctorExpr ); 69 69 virtual void visit( TupleIndexExpr *tupleExpr ); 70 virtual void visit( TupleAssignExpr *tupleExpr ); 70 71 /// Runs a new alternative finder on each element in [begin, end) 71 72 /// and writes each alternative finder to out. -
TabularUnified src/ResolvExpr/AlternativePrinter.cc ¶
rfd782b2 raa8f9df 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 ¶
rfd782b2 raa8f9df 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 ¶
rfd782b2 raa8f9df 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 ¶
rfd782b2 raa8f9df 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 ¶
rfd782b2 raa8f9df 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.