Changeset 937e51d for src/ResolvExpr
- Timestamp:
- Jun 26, 2015, 4:00:26 PM (10 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
- Children:
- 0df292b, e0ff3e6
- Parents:
- eb50842 (diff), 1869adf (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:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
src/ResolvExpr/AlternativeFinder.cc
reb50842 r937e51d 10 10 // Created On : Sat May 16 23:52:08 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sat May 16 23:55:30201513 // Update Count : 312 // Last Modified On : Mon Jun 22 17:19:54 2015 13 // Update Count : 17 14 14 // 15 15 … … 108 108 PRINT( 109 109 std::cout << "cost " << candidate->cost << " beats " << mapPlace->second.candidate->cost << std::endl; 110 110 ) 111 111 selected[ mangleName ] = current; 112 112 } else if ( candidate->cost == mapPlace->second.candidate->cost ) { 113 113 PRINT( 114 114 std::cout << "marking ambiguous" << std::endl; 115 115 ) 116 116 mapPlace->second.isAmbiguous = true; 117 117 } … … 123 123 PRINT( 124 124 std::cout << "there are " << selected.size() << " alternatives before elimination" << std::endl; 125 125 ) 126 126 127 127 // accept the alternatives that were unambiguous … … 184 184 std::cout << "findSubExprs" << std::endl; 185 185 printAlts( finder.alternatives, std::cout ); 186 186 ) 187 187 *out++ = finder; 188 188 } … … 206 206 std::cout << "alternatives before prune:" << std::endl; 207 207 printAlts( alternatives, std::cout ); 208 208 ) 209 209 AltList::iterator oldBegin = alternatives.begin(); 210 210 pruneAlternatives( alternatives.begin(), alternatives.end(), front_inserter( alternatives ), indexer ); 211 211 if ( alternatives.begin() == oldBegin ) { 212 std::ostr stream stream;212 std::ostringstream stream; 213 213 stream << "Can't choose between alternatives for expression "; 214 214 expr->print( stream ); … … 217 217 findMinCost( alternatives.begin(), alternatives.end(), back_inserter( winners ) ); 218 218 printAlts( winners, stream, 8 ); 219 throw SemanticError( st d::string( stream.str(), stream.pcount()) );219 throw SemanticError( stream.str() ); 220 220 } 221 221 alternatives.erase( oldBegin, alternatives.end() ); 222 222 PRINT( 223 223 std::cout << "there are " << alternatives.size() << " alternatives after elimination" << std::endl; 224 224 ) 225 225 } 226 226 … … 265 265 std::cout << "--- results are" << std::endl; 266 266 printAll( (*actualExpr)->get_results(), std::cout, 8 ); 267 268 267 ) 268 std::list< DeclarationWithType* >::iterator startFormal = formal; 269 269 Cost actualCost; 270 270 for ( std::list< Type* >::iterator actual = (*actualExpr)->get_results().begin(); actual != (*actualExpr)->get_results().end(); ++actual ) { … … 282 282 std::cout << std::endl << " to "; 283 283 (*formal)->get_type()->print( std::cout, 8 ); 284 285 284 ) 285 Cost newCost = conversionCost( *actual, (*formal)->get_type(), indexer, alt.env ); 286 286 PRINT( 287 287 std::cout << std::endl << "cost is" << newCost << std::endl; 288 289 290 291 292 288 ) 289 290 if ( newCost == Cost::infinity ) { 291 return newCost; 292 } 293 293 convCost += newCost; 294 294 actualCost += newCost; … … 381 381 (*actual)->print( std::cerr ); 382 382 std::cerr << std::endl; 383 383 ) 384 384 if ( ! unify( (*formal)->get_type(), *actual, resultEnv, resultNeed, resultHave, openVars, indexer ) ) { 385 385 return false; … … 429 429 std::cerr << "recursing with new set:" << std::endl; 430 430 printAssertionSet( newNeed, std::cerr, 8 ); 431 431 ) 432 432 inferRecursive( newNeed.begin(), newNeed.end(), newAlt, openVars, decls, newerNeed, level+1, indexer, out ); 433 433 return; … … 444 444 curDecl->print( std::cerr ); 445 445 std::cerr << std::endl; 446 446 ) 447 447 std::list< DeclarationWithType* > candidates; 448 448 decls.lookupId( curDecl->get_name(), candidates ); … … 453 453 (*candidate)->print( std::cout ); 454 454 std::cout << std::endl; 455 455 ) 456 456 AssertionSet newHave, newerNeed( newNeed ); 457 457 TypeEnvironment newEnv( newAlt.env ); … … 466 466 adjType->print( std::cerr ); 467 467 std::cerr << std::endl; 468 468 ) 469 469 if ( unify( curDecl->get_type(), adjType, newEnv, newerNeed, newHave, newOpenVars, indexer ) ) { 470 470 PRINT( 471 471 std::cerr << "success!" << std::endl; 472 472 ) 473 473 SymTab::Indexer newDecls( decls ); 474 474 addToIndexer( newHave, newDecls ); … … 486 486 (*candidate)->print( std::cout ); 487 487 std::cout << std::endl; 488 488 ) 489 489 ApplicationExpr *appExpr = static_cast< ApplicationExpr* >( newerAlt.expr ); 490 490 // XXX: this is a memory leak, but adjType can't be deleted because it might contain assertions … … 509 509 std::cout << "============= new indexer" << std::endl; 510 510 decls.print( std::cout ); 511 511 ) 512 512 addToIndexer( have, decls ); 513 513 AssertionSet newNeed; … … 533 533 std::cout << "need assertions:" << std::endl; 534 534 printAssertionSet( resultNeed, std::cout, 8 ); 535 535 ) 536 536 inferParameters( resultNeed, resultHave, newAlt, openVars, out ); 537 537 } … … 542 542 AlternativeFinder funcOpFinder( indexer, env ); 543 543 544 AlternativeFinder funcFinder( indexer, env ); 545 546 { 544 AlternativeFinder funcFinder( indexer, env ); { 547 545 NameExpr *fname; 548 546 if ( ( fname = dynamic_cast<NameExpr *>( untypedExpr->get_function())) 549 && ( fname->get_name() == std::string(" LabAddress")) ) {550 alternatives.push_back( Alternative( untypedExpr , env, Cost()) );547 && ( fname->get_name() == std::string("&&")) ) { 548 alternatives.push_back( Alternative( untypedExpr->clone(), env, Cost()) ); 551 549 return; 552 550 } … … 562 560 Tuples::TupleAssignSpotter tassign( this ); 563 561 if ( tassign.isTupleAssignment( untypedExpr, possibilities ) ) { 564 // TODOtake care of possible tuple assignments, or discard expression562 // take care of possible tuple assignments, or discard expression 565 563 return; 566 564 } // else ... … … 572 570 std::cout << "working on alternative: " << std::endl; 573 571 func->print( std::cout, 8 ); 574 572 ) 575 573 // check if the type is pointer to function 576 574 PointerType *pointer; … … 605 603 std::cout << "known function ops:" << std::endl; 606 604 printAlts( funcOpFinder.alternatives, std::cout, 8 ); 607 605 ) 608 606 } 609 607 … … 644 642 withFunc->env.print( std::cout, 8 ); 645 643 std::cout << "cost of conversion is:" << cvtCost << std::endl; 646 647 648 649 650 644 ) 645 if ( cvtCost != Cost::infinity ) { 646 withFunc->cvtCost = cvtCost; 647 alternatives.push_back( *withFunc ); 648 } // if 651 649 } // for 652 650 candidates.clear(); … … 749 747 newExpr.print( std::cerr ); 750 748 std::cerr << std::endl; 751 749 ) 752 750 renameTypes( alternatives.back().expr ); 753 751 if ( StructInstType *structInst = dynamic_cast< StructInstType* >( (*i)->get_type() ) ) { … … 796 794 argType->print( std::cout ); 797 795 std::cout << std::endl; 798 799 800 801 802 803 804 796 ) 797 if ( typesCompatibleIgnoreQualifiers( argType, function->get_parameters().front()->get_type(), indexer, env ) ) { 798 alternatives.push_back( Alternative( new AttrExpr( new VariableExpr( funcDecl ), argType->clone() ), env, Cost::zero ) ); 799 for ( std::list< DeclarationWithType* >::iterator i = function->get_returnVals().begin(); i != function->get_returnVals().end(); ++i ) { 800 alternatives.back().expr->get_results().push_back( (*i)->get_type()->clone() ); 801 } // for 802 } // if 805 803 } 806 804 -
src/ResolvExpr/RenameVars.cc
reb50842 r937e51d 10 10 // Created On : Sun May 17 12:05:18 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sun May 17 12:07:59201513 // Update Count : 212 // Last Modified On : Mon Jun 8 14:51:35 2015 13 // Update Count : 4 14 14 // 15 15 16 #include <s trstream>16 #include <sstream> 17 17 18 18 #include "RenameVars.h" … … 122 122 // renames all "forall" type names to `_${level}_${name}' 123 123 for ( std::list< TypeDecl* >::iterator i = type->get_forall().begin(); i != type->get_forall().end(); ++i ) { 124 std::ostr stream output;124 std::ostringstream output; 125 125 output << "_" << level << "_" << (*i)->get_name(); 126 std::string newname( output.str() , output.pcount());126 std::string newname( output.str() ); 127 127 mapStack.front()[ (*i)->get_name() ] = newname; 128 128 (*i)->set_name( newname ); -
src/ResolvExpr/Resolver.cc
reb50842 r937e51d 9 9 // Author : Richard C. Bilson 10 10 // Created On : Sun May 17 12:17:01 2015 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Sun May 17 12:18:17201513 // Update Count : 211 // Last Modified By : Rob Schluntz 12 // Last Modified On : Wed Jun 24 16:20:35 2015 13 // Update Count : 156 14 14 // 15 15 … … 38 38 virtual void visit( TypeDecl *typeDecl ); 39 39 40 virtual void visit( ArrayType * at ); 41 40 42 virtual void visit( ExprStmt *exprStmt ); 41 43 virtual void visit( IfStmt *ifStmt ); … … 45 47 virtual void visit( ChooseStmt *switchStmt ); 46 48 virtual void visit( CaseStmt *caseStmt ); 49 virtual void visit( BranchStmt *branchStmt ); 47 50 virtual void visit( ReturnStmt *returnStmt ); 48 51 … … 50 53 virtual void visit( ListInit *listInit ); 51 54 private: 55 typedef std::list< Initializer * >::iterator InitIterator; 56 57 void resolveAggrInit( AggregateDecl *, InitIterator &, InitIterator & ); 58 void resolveSingleAggrInit( Declaration *, InitIterator &, InitIterator & ); 59 52 60 std::list< Type * > functionReturn; 53 61 Type *initContext; … … 158 166 SymTab::Indexer::visit( objectDecl ); 159 167 } 160 168 169 void Resolver::visit( ArrayType * at ) { 170 if ( at->get_dimension() ) { 171 BasicType arrayLenType = BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); 172 CastExpr *castExpr = new CastExpr( at->get_dimension(), arrayLenType.clone() ); 173 Expression *newExpr = findSingleExpression( castExpr, *this ); 174 delete at->get_dimension(); 175 at->set_dimension( newExpr ); 176 } 177 Visitor::visit( at ); 178 } 179 161 180 void Resolver::visit( TypeDecl *typeDecl ) { 162 181 if ( typeDecl->get_base() ) { … … 166 185 SymTab::Indexer::visit( typeDecl ); 167 186 } 168 187 169 188 void Resolver::visit( FunctionDecl *functionDecl ) { 170 189 #if 0 … … 252 271 } 253 272 273 void Resolver::visit( BranchStmt *branchStmt ) { 274 // must resolve the argument for a computed goto 275 if ( branchStmt->get_type() == BranchStmt::Goto ) { // check for computed goto statement 276 if ( NameExpr * arg = dynamic_cast< NameExpr * >( branchStmt->get_computedTarget() ) ) { 277 VoidType v = Type::Qualifiers(); // cast to void * for the alternative finder 278 PointerType pt( Type::Qualifiers(), v.clone() ); 279 CastExpr * castExpr = new CastExpr( arg, pt.clone() ); 280 Expression * newExpr = findSingleExpression( castExpr, *this ); // find best expression 281 branchStmt->set_target( newExpr ); 282 } // if 283 } // if 284 } 285 254 286 void Resolver::visit( ReturnStmt *returnStmt ) { 255 287 if ( returnStmt->get_expr() ) { … … 260 292 returnStmt->set_expr( newExpr ); 261 293 } // if 294 } 295 296 template< typename T > 297 bool isCharType( T t ) { 298 if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) { 299 return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar || 300 bt->get_kind() == BasicType::UnsignedChar; 301 } 302 return false; 262 303 } 263 304 … … 286 327 delete castExpr; 287 328 singleInit->set_value( newExpr ); 329 330 // check if initializing type is char[] 331 if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) { 332 if ( isCharType( at->get_base() ) ) { 333 // check if the resolved type is char * 334 if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_results().front() ) ) { 335 if ( isCharType( pt->get_base() ) ) { 336 // strip cast if we're initializing a char[] with a char *, e.g. 337 // char x[] = "hello"; 338 CastExpr *ce = dynamic_cast< CastExpr * >( newExpr ); 339 singleInit->set_value( ce->get_arg() ); 340 ce->set_arg( NULL ); 341 delete ce; 342 } 343 } 344 } 345 } 288 346 } // if 289 347 // singleInit->get_value()->accept( *this ); 290 348 } 291 349 292 void Resolver::visit( ListInit *listInit ) { 293 Visitor::visit(listInit); 350 void Resolver::resolveSingleAggrInit( Declaration * dcl, InitIterator & init, InitIterator & initEnd ) { 351 DeclarationWithType * dt = dynamic_cast< DeclarationWithType * >( dcl ); 352 assert( dt ); 353 initContext = dt->get_type(); 354 try { 355 if ( init == initEnd ) return; // stop when there are no more initializers 356 (*init)->accept( *this ); 357 ++init; // made it past an initializer 358 } catch( SemanticError & ) { 359 // need to delve deeper, if you can 360 if ( StructInstType * sit = dynamic_cast< StructInstType * >( dt->get_type() ) ) { 361 resolveAggrInit( sit->get_baseStruct(), init, initEnd ); 362 } else if ( UnionInstType * uit = dynamic_cast< UnionInstType * >( dt->get_type() ) ) { 363 resolveAggrInit( uit->get_baseUnion(), init, initEnd ); 364 } else { 365 // member is not an aggregate type, so can't go any deeper 366 367 // might need to rethink what is being thrown 368 throw; 369 } // if 370 } 371 } 372 373 void Resolver::resolveAggrInit( AggregateDecl * aggr, InitIterator & init, InitIterator & initEnd ) { 374 if ( StructDecl * st = dynamic_cast< StructDecl * >( aggr ) ) { 375 // want to resolve each initializer to the members of the struct, 376 // but if there are more initializers than members we should stop 377 list< Declaration * >::iterator it = st->get_members().begin(); 378 for ( ; it != st->get_members().end(); ++it) { 379 resolveSingleAggrInit( *it, init, initEnd ); 380 } 381 } else if ( UnionDecl * un = dynamic_cast< UnionDecl * >( aggr ) ) { 382 // only resolve to the first member of a union 383 resolveSingleAggrInit( *un->get_members().begin(), init, initEnd ); 384 } // if 385 } 386 387 void Resolver::visit( ListInit * listInit ) { 388 InitIterator iter = listInit->begin_initializers(); 389 InitIterator end = listInit->end_initializers(); 390 391 if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) { 392 // resolve each member to the base type of the array 393 for ( ; iter != end; ++iter ) { 394 initContext = at->get_base(); 395 (*iter)->accept( *this ); 396 } // for 397 } else if ( StructInstType * st = dynamic_cast< StructInstType * >( initContext ) ) { 398 resolveAggrInit( st->get_baseStruct(), iter, end ); 399 } else if ( UnionInstType *st = dynamic_cast< UnionInstType * >( initContext ) ) { 400 resolveAggrInit( st->get_baseUnion(), iter, end ); 401 } else { 402 // basic types are handled here 403 Visitor::visit( listInit ); 404 } 405 294 406 #if 0 295 407 if ( ArrayType *at = dynamic_cast<ArrayType*>(initContext) ) { -
src/ResolvExpr/module.mk
reb50842 r937e51d 1 ######################### -*- Mode: Makefile-Gmake -*- ######################## 2 ## 3 ## Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo 4 ## 5 ## The contents of this file are covered under the licence agreement in the 6 ## file "LICENCE" distributed with Cforall. 7 ## 8 ## module.mk -- 9 ## 10 ## Author : Richard C. Bilson 11 ## Created On : Mon Jun 1 17:49:17 2015 12 ## Last Modified By : Peter A. Buhr 13 ## Last Modified On : Mon Jun 1 17:53:28 2015 14 ## Update Count : 1 15 ############################################################################### 16 1 17 SRC += ResolvExpr/AlternativeFinder.cc \ 2 ResolvExpr/Alternative.cc \18 ResolvExpr/Alternative.cc \ 3 19 ResolvExpr/Unify.cc \ 4 20 ResolvExpr/PtrsAssignable.cc \ … … 16 32 ResolvExpr/Occurs.cc \ 17 33 ResolvExpr/TypeEnvironment.cc 18
Note: See TracChangeset
for help on using the changeset viewer.