Changeset d908563 for src/InitTweak
- Timestamp:
- May 27, 2019, 11:08:54 AM (5 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- f88a252
- Parents:
- 933f32f (diff), 21a44ca (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/InitTweak
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/InitTweak/InitTweak.cc
r933f32f rd908563 346 346 namespace { 347 347 DeclarationWithType * getCalledFunction( Expression * expr ); 348 const ast::DeclWithType * getCalledFunction( const ast::Expr * expr ); 348 349 349 350 template<typename CallExpr> … … 355 356 return getCalledFunction( expr->get_args().front() ); 356 357 } 358 359 template<typename CallExpr> 360 const ast::DeclWithType * handleDerefCalledFunction( const CallExpr * expr ) { 361 // (*f)(x) => should get "f" 362 std::string name = getFunctionName( expr ); 363 assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() ); 364 assertf( ! expr->args.empty(), "Cannot get called function from dereference with no arguments" ); 365 return getCalledFunction( expr->args.front() ); 366 } 367 357 368 358 369 DeclarationWithType * getCalledFunction( Expression * expr ) { … … 375 386 return nullptr; 376 387 } 388 389 const ast::DeclWithType * getCalledFunction( const ast::Expr * expr ) { 390 assert( expr ); 391 if ( const ast::VariableExpr * varExpr = dynamic_cast< const ast::VariableExpr * >( expr ) ) { 392 return varExpr->var; 393 } else if ( const ast::MemberExpr * memberExpr = dynamic_cast< const ast::MemberExpr * >( expr ) ) { 394 return memberExpr->member; 395 } else if ( const ast::CastExpr * castExpr = dynamic_cast< const ast::CastExpr * >( expr ) ) { 396 return getCalledFunction( castExpr->arg ); 397 } else if ( const ast::UntypedExpr * untypedExpr = dynamic_cast< const ast::UntypedExpr * >( expr ) ) { 398 return handleDerefCalledFunction( untypedExpr ); 399 } else if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * > ( expr ) ) { 400 return handleDerefCalledFunction( appExpr ); 401 } else if ( const ast::AddressExpr * addrExpr = dynamic_cast< const ast::AddressExpr * >( expr ) ) { 402 return getCalledFunction( addrExpr->arg ); 403 } else if ( const ast::CommaExpr * commaExpr = dynamic_cast< const ast::CommaExpr * >( expr ) ) { 404 return getCalledFunction( commaExpr->arg2 ); 405 } 406 return nullptr; 407 } 377 408 } 378 409 … … 382 413 } else if ( UntypedExpr * untyped = dynamic_cast< UntypedExpr * > ( expr ) ) { 383 414 return getCalledFunction( untyped->get_function() ); 415 } 416 assertf( false, "getFunction received unknown expression: %s", toString( expr ).c_str() ); 417 } 418 419 const ast::DeclWithType * getFunction( const ast::Expr * expr ) { 420 if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * >( expr ) ) { 421 return getCalledFunction( appExpr->func ); 422 } else if ( const ast::UntypedExpr * untyped = dynamic_cast< const ast::UntypedExpr * > ( expr ) ) { 423 return getCalledFunction( untyped->func ); 384 424 } 385 425 assertf( false, "getFunction received unknown expression: %s", toString( expr ).c_str() ); … … 434 474 } 435 475 436 //template<typename CallExpr>437 //const ast::Expr * callArg( const CallExpr * call, unsigned int pos ) {438 //if( pos >= call->args.size() ) {439 //assertf( false, "getCallArg for argument that doesn't exist: (%u); %s.",440 //pos, toString( call ).c_str() );441 //}442 //for ( const ast::Expr * arg : call->args ) {443 //if ( pos == 0 ) return arg;444 //--pos;445 //}446 //assert( false );447 //}476 template<typename CallExpr> 477 const ast::Expr * callArg( const CallExpr * call, unsigned int pos ) { 478 if( pos >= call->args.size() ) { 479 assertf( false, "getCallArg for argument that doesn't exist: (%u); %s.", 480 pos, toString( call ).c_str() ); 481 } 482 for ( const ast::Expr * arg : call->args ) { 483 if ( pos == 0 ) return arg; 484 --pos; 485 } 486 assert( false ); 487 } 448 488 } 449 489 … … 466 506 } 467 507 } 508 468 509 const ast::Expr * getCallArg( const ast::Expr * call, unsigned pos ) { 469 (void)call; 470 (void)pos; 471 #warning unimplemented; needs to build AST/Expr.cpp 472 assertf(false, "unimplemented; needs to build AST/Expr.cpp"); 473 // if ( auto app = dynamic_cast< const ast::ApplicationExpr * >( call ) ) { 474 // return callArg( app, pos ); 475 // } else if ( auto untyped = dynamic_cast< const ast::UntypedExpr * >( call ) ) { 476 // return callArg( untyped, pos ); 477 // } else if ( auto tupleAssn = dynamic_cast< const ast::TupleAssignExpr * >( call ) ) { 478 // const std::list<ast::ptr<ast::Stmt>>& stmts = tupleAssn->stmtExpr->stmts->kids; 479 // assertf( ! stmts.empty(), "TupleAssignExpr missing statements." ); 480 // const ExprStmt * stmt = strict_dynamic_cast< const ast::ExprStmt * >( stmts.back() ); 481 // const TupleExpr * tuple = strict_dynamic_cast< const ast::TupleExpr * >( stmt->expr ); 482 // assertf( ! tuple->exprs.empty(), "TupleAssignExpr has empty tuple expr."); 483 // return getCallArg( tuple->exprs.front(), pos ); 484 // } else if ( auto ctor = dynamic_cast< const ast::ImplicitCopyCtorExpr * >( call ) ) { 485 // return getCallArg( ctor->callExpr, pos ); 486 // } else { 487 // assertf( false, "Unexpected expression type passed to getCallArg: %s", 488 // toString( call ).c_str() ); 489 // } 510 if ( auto app = dynamic_cast< const ast::ApplicationExpr * >( call ) ) { 511 return callArg( app, pos ); 512 } else if ( auto untyped = dynamic_cast< const ast::UntypedExpr * >( call ) ) { 513 return callArg( untyped, pos ); 514 } else if ( auto tupleAssn = dynamic_cast< const ast::TupleAssignExpr * >( call ) ) { 515 const std::list<ast::ptr<ast::Stmt>>& stmts = tupleAssn->stmtExpr->stmts->kids; 516 assertf( ! stmts.empty(), "TupleAssignExpr missing statements." ); 517 auto stmt = strict_dynamic_cast< const ast::ExprStmt * >( stmts.back().get() ); 518 auto tuple = strict_dynamic_cast< const ast::TupleExpr * >( stmt->expr.get() ); 519 assertf( ! tuple->exprs.empty(), "TupleAssignExpr has empty tuple expr."); 520 return getCallArg( tuple->exprs.front(), pos ); 521 } else if ( auto ctor = dynamic_cast< const ast::ImplicitCopyCtorExpr * >( call ) ) { 522 return getCallArg( ctor->callExpr, pos ); 523 } else { 524 assertf( false, "Unexpected expression type passed to getCallArg: %s", 525 toString( call ).c_str() ); 526 } 490 527 } 491 528 492 529 namespace { 493 530 std::string funcName( Expression * func ); 531 std::string funcName( const ast::Expr * func ); 494 532 495 533 template<typename CallExpr> … … 500 538 assertf( ! expr->get_args().empty(), "Cannot get function name from dereference with no arguments" ); 501 539 return funcName( expr->get_args().front() ); 540 } 541 542 template<typename CallExpr> 543 std::string handleDerefName( const CallExpr * expr ) { 544 // (*f)(x) => should get name "f" 545 std::string name = getFunctionName( expr ); 546 assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() ); 547 assertf( ! expr->args.empty(), "Cannot get function name from dereference with no arguments" ); 548 return funcName( expr->args.front() ); 502 549 } 503 550 … … 523 570 } 524 571 } 572 573 std::string funcName( const ast::Expr * func ) { 574 if ( const ast::NameExpr * nameExpr = dynamic_cast< const ast::NameExpr * >( func ) ) { 575 return nameExpr->name; 576 } else if ( const ast::VariableExpr * varExpr = dynamic_cast< const ast::VariableExpr * >( func ) ) { 577 return varExpr->var->name; 578 } else if ( const ast::CastExpr * castExpr = dynamic_cast< const ast::CastExpr * >( func ) ) { 579 return funcName( castExpr->arg ); 580 } else if ( const ast::MemberExpr * memberExpr = dynamic_cast< const ast::MemberExpr * >( func ) ) { 581 return memberExpr->member->name; 582 } else if ( const ast::UntypedMemberExpr * memberExpr = dynamic_cast< const ast::UntypedMemberExpr * > ( func ) ) { 583 return funcName( memberExpr->member ); 584 } else if ( const ast::UntypedExpr * untypedExpr = dynamic_cast< const ast::UntypedExpr * >( func ) ) { 585 return handleDerefName( untypedExpr ); 586 } else if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * >( func ) ) { 587 return handleDerefName( appExpr ); 588 } else if ( const ast::ConstructorExpr * ctorExpr = dynamic_cast< const ast::ConstructorExpr * >( func ) ) { 589 return funcName( getCallArg( ctorExpr->callExpr, 0 ) ); 590 } else { 591 assertf( false, "Unexpected expression type being called as a function in call expression: %s", toString( func ).c_str() ); 592 } 593 } 525 594 } 526 595 … … 539 608 } 540 609 610 std::string getFunctionName( const ast::Expr * expr ) { 611 // there's some unforunate overlap here with getCalledFunction. Ideally this would be able to use getCalledFunction and 612 // return the name of the DeclarationWithType, but this needs to work for NameExpr and UntypedMemberExpr, where getCalledFunction 613 // can't possibly do anything reasonable. 614 if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * >( expr ) ) { 615 return funcName( appExpr->func ); 616 } else if ( const ast::UntypedExpr * untypedExpr = dynamic_cast< const ast::UntypedExpr * > ( expr ) ) { 617 return funcName( untypedExpr->func ); 618 } else { 619 std::cerr << expr << std::endl; 620 assertf( false, "Unexpected expression type passed to getFunctionName" ); 621 } 622 } 623 541 624 Type * getPointerBase( Type * type ) { 542 625 if ( PointerType * ptrType = dynamic_cast< PointerType * >( type ) ) { … … 551 634 } 552 635 const ast::Type* getPointerBase( const ast::Type* t ) { 553 (void)t; 554 #warning needs to build Type.cpp before inclusion 555 assertf(false, "needs to build Type.cpp before inclusion"); 556 // if ( const auto * p = dynamic_cast< const ast::PointerType * >( t ) ) { 557 // return p->base; 558 // } else if ( const auto * a = dynamic_cast< const ast::ArrayType * >( t ) ) { 559 // return a->base; 560 // } else if ( const auto * r = dynamic_cast< const ast::ReferenceType * >( t ) ) { 561 // return r->base; 562 // } else return nullptr; 636 if ( const auto * p = dynamic_cast< const ast::PointerType * >( t ) ) { 637 return p->base; 638 } else if ( const auto * a = dynamic_cast< const ast::ArrayType * >( t ) ) { 639 return a->base; 640 } else if ( const auto * r = dynamic_cast< const ast::ReferenceType * >( t ) ) { 641 return r->base; 642 } else return nullptr; 563 643 } 564 644 -
src/InitTweak/InitTweak.h
r933f32f rd908563 58 58 /// returns the declaration of the function called by the expr (must be ApplicationExpr or UntypedExpr) 59 59 DeclarationWithType * getFunction( Expression * expr ); 60 const ast::DeclWithType * getFunction( const ast::Expr * expr ); 60 61 61 62 /// Non-Null if expr is a call expression whose target function is intrinsic … … 78 79 /// returns the name of the function being called 79 80 std::string getFunctionName( Expression * expr ); 81 std::string getFunctionName( const ast::Expr * expr ); 80 82 81 83 /// returns the argument to a call expression in position N indexed from 0
Note: See TracChangeset
for help on using the changeset viewer.