Changeset d7aa12c for src/InitTweak
- Timestamp:
- May 23, 2019, 11:55:32 AM (6 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:
- b5fed34
- Parents:
- 9d6e7fa9
- Location:
- src/InitTweak
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/InitTweak/InitTweak.cc
r9d6e7fa9 rd7aa12c 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() ); … … 492 532 namespace { 493 533 std::string funcName( Expression * func ); 534 std::string funcName( const ast::Expr * func ); 494 535 495 536 template<typename CallExpr> … … 500 541 assertf( ! expr->get_args().empty(), "Cannot get function name from dereference with no arguments" ); 501 542 return funcName( expr->get_args().front() ); 543 } 544 545 template<typename CallExpr> 546 std::string handleDerefName( const CallExpr * expr ) { 547 // (*f)(x) => should get name "f" 548 std::string name = getFunctionName( expr ); 549 assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() ); 550 assertf( ! expr->args.empty(), "Cannot get function name from dereference with no arguments" ); 551 return funcName( expr->args.front() ); 502 552 } 503 553 … … 523 573 } 524 574 } 575 576 std::string funcName( const ast::Expr * func ) { 577 if ( const ast::NameExpr * nameExpr = dynamic_cast< const ast::NameExpr * >( func ) ) { 578 return nameExpr->name; 579 } else if ( const ast::VariableExpr * varExpr = dynamic_cast< const ast::VariableExpr * >( func ) ) { 580 return varExpr->var->name; 581 } else if ( const ast::CastExpr * castExpr = dynamic_cast< const ast::CastExpr * >( func ) ) { 582 return funcName( castExpr->arg ); 583 } else if ( const ast::MemberExpr * memberExpr = dynamic_cast< const ast::MemberExpr * >( func ) ) { 584 return memberExpr->member->name; 585 } else if ( const ast::UntypedMemberExpr * memberExpr = dynamic_cast< const ast::UntypedMemberExpr * > ( func ) ) { 586 return funcName( memberExpr->member ); 587 } else if ( const ast::UntypedExpr * untypedExpr = dynamic_cast< const ast::UntypedExpr * >( func ) ) { 588 return handleDerefName( untypedExpr ); 589 } else if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * >( func ) ) { 590 return handleDerefName( appExpr ); 591 } else if ( const ast::ConstructorExpr * ctorExpr = dynamic_cast< const ast::ConstructorExpr * >( func ) ) { 592 return funcName( getCallArg( ctorExpr->callExpr, 0 ) ); 593 } else { 594 assertf( false, "Unexpected expression type being called as a function in call expression: %s", toString( func ).c_str() ); 595 } 596 } 525 597 } 526 598 … … 533 605 } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * > ( expr ) ) { 534 606 return funcName( untypedExpr->get_function() ); 607 } else { 608 std::cerr << expr << std::endl; 609 assertf( false, "Unexpected expression type passed to getFunctionName" ); 610 } 611 } 612 613 std::string getFunctionName( const ast::Expr * expr ) { 614 // there's some unforunate overlap here with getCalledFunction. Ideally this would be able to use getCalledFunction and 615 // return the name of the DeclarationWithType, but this needs to work for NameExpr and UntypedMemberExpr, where getCalledFunction 616 // can't possibly do anything reasonable. 617 if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * >( expr ) ) { 618 return funcName( appExpr->func ); 619 } else if ( const ast::UntypedExpr * untypedExpr = dynamic_cast< const ast::UntypedExpr * > ( expr ) ) { 620 return funcName( untypedExpr->func ); 535 621 } else { 536 622 std::cerr << expr << std::endl; -
src/InitTweak/InitTweak.h
r9d6e7fa9 rd7aa12c 79 79 /// returns the name of the function being called 80 80 std::string getFunctionName( Expression * expr ); 81 std::string getFunctionName( const ast::Expr * expr ); 81 82 82 83 /// returns the argument to a call expression in position N indexed from 0
Note: See TracChangeset
for help on using the changeset viewer.