Changeset 599b386


Ignore:
Timestamp:
Dec 9, 2016, 3:20:17 PM (5 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
aaron-thesis, arm-eh, cleanup-dtors, deferred_resn, demangler, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, resolv-new, with_gc
Children:
bd66967
Parents:
9a063c8
Message:

handle the case where a function pointer is dereferenced to get the called function's name

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/InitTweak/InitTweak.cc

    r9a063c8 r599b386  
    304304
    305305        namespace {
     306                DeclarationWithType * getCalledFunction( Expression * expr );
     307
     308                template<typename CallExpr>
     309                DeclarationWithType * handleDerefCalledFunction( CallExpr * expr ) {
     310                        // (*f)(x) => should get "f"
     311                        std::string name = getFunctionName( expr );
     312                        assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() );
     313                        assertf( ! expr->get_args().empty(), "Can't get called function from dereference with no arguments" );
     314                        return getCalledFunction( expr->get_args().front() );
     315                }
     316
    306317                DeclarationWithType * getCalledFunction( Expression * expr ) {
    307318                        assert( expr );
     
    312323                        } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) {
    313324                                return getCalledFunction( castExpr->get_arg() );
     325                        } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( expr ) ) {
     326                                return handleDerefCalledFunction( untypedExpr );
     327                        } else if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * > ( expr ) ) {
     328                                return handleDerefCalledFunction( appExpr );
    314329                        }
    315330                        return nullptr;
     
    377392
    378393        namespace {
     394                std::string funcName( Expression * func );
     395
     396                template<typename CallExpr>
     397                std::string handleDerefName( CallExpr * expr ) {
     398                        // (*f)(x) => should get name "f"
     399                        std::string name = getFunctionName( expr );
     400                        assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() );
     401                        assertf( ! expr->get_args().empty(), "Can't get function name from dereference with no arguments" );
     402                        return funcName( expr->get_args().front() );
     403                }
     404
    379405                std::string funcName( Expression * func ) {
    380406                        if ( NameExpr * nameExpr = dynamic_cast< NameExpr * >( func ) ) {
     
    388414                        } else if ( UntypedMemberExpr * memberExpr = dynamic_cast< UntypedMemberExpr * > ( func ) ) {
    389415                                return funcName( memberExpr->get_member() );
     416                        } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( func ) ) {
     417                                return handleDerefName( untypedExpr );
     418                        } else if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( func ) ) {
     419                                return handleDerefName( appExpr );
    390420                        } else {
    391421                                assertf( false, "Unexpected expression type being called as a function in call expression" );
     
    395425
    396426        std::string getFunctionName( Expression * expr ) {
     427                // there's some unforunate overlap here with getCalledFunction. Ideally this would be able to use getCalledFunction and
     428                // return the name of the DeclarationWithType, but this needs to work for NameExpr and UntypedMemberExpr, where getCalledFunction
     429                // can't possibly do anything reasonable.
    397430                if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ) ) {
    398431                        return funcName( appExpr->get_function() );
Note: See TracChangeset for help on using the changeset viewer.