Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Inspect.cpp

    rc02cef1 re01eb4a  
    1010// Created On       : Fri Jun 24 13:16:31 2022
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Mon Oct  3 11:04:00 2022
    13 // Update Count     : 3
     12// Last Modified On : Wed Sep 22 13:50:00 2022
     13// Update Count     : 2
    1414//
    1515
     
    1717
    1818#include <iostream>
    19 #include <iterator>
    2019
    2120#include "AST/Decl.hpp"
     
    2827namespace ast {
    2928
    30 const Type * getPointerBase( const Type * type ) {
    31         if ( const auto * p = dynamic_cast< const PointerType * >( type ) ) {
     29const Type * getPointerBase( const Type * t ) {
     30        if ( const auto * p = dynamic_cast< const PointerType * >( t ) ) {
    3231                return p->base;
    33         } else if ( auto a = dynamic_cast< const ArrayType * >( type ) ) {
     32        } else if ( const auto * a = dynamic_cast< const ArrayType * >( t ) ) {
    3433                return a->base;
    35         } else if ( auto r = dynamic_cast< const ReferenceType * >( type ) ) {
     34        } else if ( const auto * r = dynamic_cast< const ReferenceType * >( t ) ) {
    3635                return r->base;
    3736        } else {
    3837                return nullptr;
    3938        }
     39}
     40
     41template<typename CallExpr>
     42static const Expr * callArg( const CallExpr * call, unsigned int pos ) {
     43        assertf( pos < call->args.size(),
     44                "getCallArg for argument that doesn't exist: (%u); %s.",
     45                pos, toString( call ).c_str() );
     46        for ( const Expr * arg : call->args ) {
     47                if ( 0 == pos ) return arg;
     48                --pos;
     49        }
     50        assert( false );
    4051}
    4152
     
    4556        std::string name = getFunctionName( expr );
    4657        assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() );
    47         assertf( !expr->args.empty(), "Cannot pass through dereference with no arguments." );
     58        assertf( !expr->args.empty(), "Cannot get function name from dereference with no arguments" );
    4859        return func( expr->args.front() );
    4960}
     
    5162static const DeclWithType * getCalledFunction( const Expr * expr ) {
    5263        assert( expr );
    53         if ( const auto * varExpr = dynamic_cast< const VariableExpr * >( expr ) ) {
     64        if ( const ast::VariableExpr * varExpr = dynamic_cast< const ast::VariableExpr * >( expr ) ) {
    5465                return varExpr->var;
    55         } else if ( auto memberExpr = dynamic_cast< const MemberExpr * >( expr ) ) {
     66        } else if ( const ast::MemberExpr * memberExpr = dynamic_cast< const ast::MemberExpr * >( expr ) ) {
    5667                return memberExpr->member;
    57         } else if ( auto castExpr = dynamic_cast< const CastExpr * >( expr ) ) {
     68        } else if ( const ast::CastExpr * castExpr = dynamic_cast< const ast::CastExpr * >( expr ) ) {
    5869                return getCalledFunction( castExpr->arg );
    59         } else if ( auto untypedExpr = dynamic_cast< const UntypedExpr * >( expr ) ) {
     70        } else if ( const ast::UntypedExpr * untypedExpr = dynamic_cast< const ast::UntypedExpr * >( expr ) ) {
    6071                return throughDeref( untypedExpr, getCalledFunction );
    61         } else if ( auto appExpr = dynamic_cast< const ApplicationExpr * > ( expr ) ) {
     72        } else if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * > ( expr ) ) {
    6273                return throughDeref( appExpr, getCalledFunction );
    63         } else if ( auto addrExpr = dynamic_cast< const AddressExpr * >( expr ) ) {
     74        } else if ( const ast::AddressExpr * addrExpr = dynamic_cast< const ast::AddressExpr * >( expr ) ) {
    6475                return getCalledFunction( addrExpr->arg );
    65         } else if ( auto commaExpr = dynamic_cast< const CommaExpr * >( expr ) ) {
     76        } else if ( const ast::CommaExpr * commaExpr = dynamic_cast< const ast::CommaExpr * >( expr ) ) {
    6677                return getCalledFunction( commaExpr->arg2 );
    67         } else {
    68                 return nullptr;
    6978        }
     79        return nullptr;
    7080}
    7181
     
    7585        } else if ( auto untyped = dynamic_cast< const UntypedExpr * >( expr ) ) {
    7686                return getCalledFunction( untyped->func );
    77         } else {
    78                 assertf( false, "getFunction received unknown expression: %s", toString( expr ).c_str() );
    7987        }
     88        assertf( false, "getFunction received unknown expression: %s", toString( expr ).c_str() );
    8089}
    8190
     
    8594static std::string funcName( const Expr * func ) {
    8695        assert( func );
    87         if ( const auto * nameExpr = dynamic_cast< const NameExpr * >( func ) ) {
     96        if ( const ast::NameExpr * nameExpr = dynamic_cast< const ast::NameExpr * >( func ) ) {
    8897                return nameExpr->name;
    89         } else if ( auto varExpr = dynamic_cast< const VariableExpr * >( func ) ) {
     98        } else if ( const ast::VariableExpr * varExpr = dynamic_cast< const ast::VariableExpr * >( func ) ) {
    9099                return varExpr->var->name;
    91         } else if ( auto castExpr = dynamic_cast< const CastExpr * >( func ) ) {
     100        } else if ( const ast::CastExpr * castExpr = dynamic_cast< const ast::CastExpr * >( func ) ) {
    92101                return funcName( castExpr->arg );
    93         } else if ( auto memberExpr = dynamic_cast< const MemberExpr * >( func ) ) {
     102        } else if ( const ast::MemberExpr * memberExpr = dynamic_cast< const ast::MemberExpr * >( func ) ) {
    94103                return memberExpr->member->name;
    95         } else if ( auto memberExpr = dynamic_cast< const UntypedMemberExpr * >( func ) ) {
     104        } else if ( const ast::UntypedMemberExpr * memberExpr = dynamic_cast< const ast::UntypedMemberExpr * > ( func ) ) {
    96105                return funcName( memberExpr->member );
    97         } else if ( auto untypedExpr = dynamic_cast< const UntypedExpr * >( func ) ) {
     106        } else if ( const ast::UntypedExpr * untypedExpr = dynamic_cast< const ast::UntypedExpr * >( func ) ) {
    98107                return throughDeref( untypedExpr, funcName );
    99         } else if ( auto appExpr = dynamic_cast< const ApplicationExpr * >( func ) ) {
     108        } else if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * >( func ) ) {
    100109                return throughDeref( appExpr, funcName );
    101         } else if ( auto ctorExpr = dynamic_cast< const ConstructorExpr * >( func ) ) {
     110        } else if ( const ast::ConstructorExpr * ctorExpr = dynamic_cast< const ast::ConstructorExpr * >( func ) ) {
    102111                return funcName( getCallArg( ctorExpr->callExpr, 0 ) );
    103112        } else {
     
    107116
    108117std::string getFunctionName( const Expr * expr ) {
    109         // There's some unforunate overlap here with getFunction. See above.
     118        // There's some unforunate overlap here with getCalledFunction. Ideally
     119        // this would be able to use getCalledFunction and return the name of the
     120        // DeclWithType, but this needs to work for NameExpr and UntypedMemberExpr,
     121        // where getCalledFunction can't possibly do anything reasonable.
    110122        if ( auto app = dynamic_cast< const ApplicationExpr * >( expr ) ) {
    111123                return funcName( app->func );
     
    113125                return funcName( untyped->func );
    114126        } else {
    115                 assertf( false, "getFunctionName received unknown expression: %s", toString( expr ).c_str() );
     127                assertf( false, "Unexpected expression type passed to getFunctionName: %s", toString( expr ).c_str() );
    116128        }
    117 }
    118 
    119 template<typename CallExpr>
    120 static const Expr * callArg( const CallExpr * call, unsigned int pos ) {
    121         assertf( pos < call->args.size(),
    122                 "callArg for argument that doesn't exist: (%u); %s.",
    123                 pos, toString( call ).c_str() );
    124         auto it = call->args.begin();
    125         std::advance( it, pos );
    126         return *it;
    127129}
    128130
     
    135137                const std::list<ptr<Stmt>>& stmts = tupleAssn->stmtExpr->stmts->kids;
    136138                assertf( !stmts.empty(), "TupleAssignExpr missing statements." );
    137                 auto stmt  = stmts.back().strict_as< ExprStmt >();
    138                 auto tuple = stmt->expr.strict_as< TupleExpr >();
     139                auto stmt  = strict_dynamic_cast< const ExprStmt * >( stmts.back().get() );
     140                auto tuple = strict_dynamic_cast< const TupleExpr * >( stmt->expr.get() );
    139141                assertf( !tuple->exprs.empty(), "TupleAssignExpr has empty tuple expr." );
    140142                return getCallArg( tuple->exprs.front(), pos );
     
    142144                return getCallArg( ctor->callExpr, pos );
    143145        } else {
    144                 assertf( false, "Unexpected expression type passed to getCallArg: %s", toString( call ).c_str() );
     146                assertf( false, "Unexpected expression type passed to getCallArg: %s",
     147                        toString( call ).c_str() );
    145148        }
    146149}
     
    162165
    163166        const DeclWithType * func = getCalledFunction( appExpr->func );
    164         assertf( func, "getCalledFunction returned nullptr: %s",
    165                 toString( appExpr->func ).c_str() );
     167        assertf( func,
     168                "getCalledFunction returned nullptr: %s", toString( appExpr->func ).c_str() );
    166169
    167         return func->linkage == Linkage::Intrinsic ? appExpr : nullptr;
     170        return func->linkage == ast::Linkage::Intrinsic ? appExpr : nullptr;
    168171}
    169172
Note: See TracChangeset for help on using the changeset viewer.