Changeset c02cef1


Ignore:
Timestamp:
Oct 3, 2022, 11:13:43 AM (19 months ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
ADT, ast-experimental, master
Children:
4b8b2a4
Parents:
f92e7b9
Message:

Clean-up in AST/Inspect.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Inspect.cpp

    rf92e7b9 rc02cef1  
    1010// Created On       : Fri Jun 24 13:16:31 2022
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Wed Sep 22 13:50:00 2022
    13 // Update Count     : 2
     12// Last Modified On : Mon Oct  3 11:04:00 2022
     13// Update Count     : 3
    1414//
    1515
     
    1717
    1818#include <iostream>
     19#include <iterator>
    1920
    2021#include "AST/Decl.hpp"
     
    2728namespace ast {
    2829
    29 const Type * getPointerBase( const Type * t ) {
    30         if ( const auto * p = dynamic_cast< const PointerType * >( t ) ) {
     30const Type * getPointerBase( const Type * type ) {
     31        if ( const auto * p = dynamic_cast< const PointerType * >( type ) ) {
    3132                return p->base;
    32         } else if ( const auto * a = dynamic_cast< const ArrayType * >( t ) ) {
     33        } else if ( auto a = dynamic_cast< const ArrayType * >( type ) ) {
    3334                return a->base;
    34         } else if ( const auto * r = dynamic_cast< const ReferenceType * >( t ) ) {
     35        } else if ( auto r = dynamic_cast< const ReferenceType * >( type ) ) {
    3536                return r->base;
    3637        } else {
    3738                return nullptr;
    3839        }
    39 }
    40 
    41 template<typename CallExpr>
    42 static 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 );
    5140}
    5241
     
    5645        std::string name = getFunctionName( expr );
    5746        assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() );
    58         assertf( !expr->args.empty(), "Cannot get function name from dereference with no arguments" );
     47        assertf( !expr->args.empty(), "Cannot pass through dereference with no arguments." );
    5948        return func( expr->args.front() );
    6049}
     
    6251static const DeclWithType * getCalledFunction( const Expr * expr ) {
    6352        assert( expr );
    64         if ( const ast::VariableExpr * varExpr = dynamic_cast< const ast::VariableExpr * >( expr ) ) {
     53        if ( const auto * varExpr = dynamic_cast< const VariableExpr * >( expr ) ) {
    6554                return varExpr->var;
    66         } else if ( const ast::MemberExpr * memberExpr = dynamic_cast< const ast::MemberExpr * >( expr ) ) {
     55        } else if ( auto memberExpr = dynamic_cast< const MemberExpr * >( expr ) ) {
    6756                return memberExpr->member;
    68         } else if ( const ast::CastExpr * castExpr = dynamic_cast< const ast::CastExpr * >( expr ) ) {
     57        } else if ( auto castExpr = dynamic_cast< const CastExpr * >( expr ) ) {
    6958                return getCalledFunction( castExpr->arg );
    70         } else if ( const ast::UntypedExpr * untypedExpr = dynamic_cast< const ast::UntypedExpr * >( expr ) ) {
     59        } else if ( auto untypedExpr = dynamic_cast< const UntypedExpr * >( expr ) ) {
    7160                return throughDeref( untypedExpr, getCalledFunction );
    72         } else if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * > ( expr ) ) {
     61        } else if ( auto appExpr = dynamic_cast< const ApplicationExpr * > ( expr ) ) {
    7362                return throughDeref( appExpr, getCalledFunction );
    74         } else if ( const ast::AddressExpr * addrExpr = dynamic_cast< const ast::AddressExpr * >( expr ) ) {
     63        } else if ( auto addrExpr = dynamic_cast< const AddressExpr * >( expr ) ) {
    7564                return getCalledFunction( addrExpr->arg );
    76         } else if ( const ast::CommaExpr * commaExpr = dynamic_cast< const ast::CommaExpr * >( expr ) ) {
     65        } else if ( auto commaExpr = dynamic_cast< const CommaExpr * >( expr ) ) {
    7766                return getCalledFunction( commaExpr->arg2 );
     67        } else {
     68                return nullptr;
    7869        }
    79         return nullptr;
    8070}
    8171
     
    8575        } else if ( auto untyped = dynamic_cast< const UntypedExpr * >( expr ) ) {
    8676                return getCalledFunction( untyped->func );
     77        } else {
     78                assertf( false, "getFunction received unknown expression: %s", toString( expr ).c_str() );
    8779        }
    88         assertf( false, "getFunction received unknown expression: %s", toString( expr ).c_str() );
    8980}
    9081
     
    9485static std::string funcName( const Expr * func ) {
    9586        assert( func );
    96         if ( const ast::NameExpr * nameExpr = dynamic_cast< const ast::NameExpr * >( func ) ) {
     87        if ( const auto * nameExpr = dynamic_cast< const NameExpr * >( func ) ) {
    9788                return nameExpr->name;
    98         } else if ( const ast::VariableExpr * varExpr = dynamic_cast< const ast::VariableExpr * >( func ) ) {
     89        } else if ( auto varExpr = dynamic_cast< const VariableExpr * >( func ) ) {
    9990                return varExpr->var->name;
    100         } else if ( const ast::CastExpr * castExpr = dynamic_cast< const ast::CastExpr * >( func ) ) {
     91        } else if ( auto castExpr = dynamic_cast< const CastExpr * >( func ) ) {
    10192                return funcName( castExpr->arg );
    102         } else if ( const ast::MemberExpr * memberExpr = dynamic_cast< const ast::MemberExpr * >( func ) ) {
     93        } else if ( auto memberExpr = dynamic_cast< const MemberExpr * >( func ) ) {
    10394                return memberExpr->member->name;
    104         } else if ( const ast::UntypedMemberExpr * memberExpr = dynamic_cast< const ast::UntypedMemberExpr * > ( func ) ) {
     95        } else if ( auto memberExpr = dynamic_cast< const UntypedMemberExpr * >( func ) ) {
    10596                return funcName( memberExpr->member );
    106         } else if ( const ast::UntypedExpr * untypedExpr = dynamic_cast< const ast::UntypedExpr * >( func ) ) {
     97        } else if ( auto untypedExpr = dynamic_cast< const UntypedExpr * >( func ) ) {
    10798                return throughDeref( untypedExpr, funcName );
    108         } else if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * >( func ) ) {
     99        } else if ( auto appExpr = dynamic_cast< const ApplicationExpr * >( func ) ) {
    109100                return throughDeref( appExpr, funcName );
    110         } else if ( const ast::ConstructorExpr * ctorExpr = dynamic_cast< const ast::ConstructorExpr * >( func ) ) {
     101        } else if ( auto ctorExpr = dynamic_cast< const ConstructorExpr * >( func ) ) {
    111102                return funcName( getCallArg( ctorExpr->callExpr, 0 ) );
    112103        } else {
     
    116107
    117108std::string getFunctionName( const Expr * expr ) {
    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.
     109        // There's some unforunate overlap here with getFunction. See above.
    122110        if ( auto app = dynamic_cast< const ApplicationExpr * >( expr ) ) {
    123111                return funcName( app->func );
     
    125113                return funcName( untyped->func );
    126114        } else {
    127                 assertf( false, "Unexpected expression type passed to getFunctionName: %s", toString( expr ).c_str() );
     115                assertf( false, "getFunctionName received unknown expression: %s", toString( expr ).c_str() );
    128116        }
     117}
     118
     119template<typename CallExpr>
     120static 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;
    129127}
    130128
     
    137135                const std::list<ptr<Stmt>>& stmts = tupleAssn->stmtExpr->stmts->kids;
    138136                assertf( !stmts.empty(), "TupleAssignExpr missing statements." );
    139                 auto stmt  = strict_dynamic_cast< const ExprStmt * >( stmts.back().get() );
    140                 auto tuple = strict_dynamic_cast< const TupleExpr * >( stmt->expr.get() );
     137                auto stmt  = stmts.back().strict_as< ExprStmt >();
     138                auto tuple = stmt->expr.strict_as< TupleExpr >();
    141139                assertf( !tuple->exprs.empty(), "TupleAssignExpr has empty tuple expr." );
    142140                return getCallArg( tuple->exprs.front(), pos );
     
    144142                return getCallArg( ctor->callExpr, pos );
    145143        } else {
    146                 assertf( false, "Unexpected expression type passed to getCallArg: %s",
    147                         toString( call ).c_str() );
     144                assertf( false, "Unexpected expression type passed to getCallArg: %s", toString( call ).c_str() );
    148145        }
    149146}
     
    165162
    166163        const DeclWithType * func = getCalledFunction( appExpr->func );
    167         assertf( func,
    168                 "getCalledFunction returned nullptr: %s", toString( appExpr->func ).c_str() );
     164        assertf( func, "getCalledFunction returned nullptr: %s",
     165                toString( appExpr->func ).c_str() );
    169166
    170         return func->linkage == ast::Linkage::Intrinsic ? appExpr : nullptr;
     167        return func->linkage == Linkage::Intrinsic ? appExpr : nullptr;
    171168}
    172169
Note: See TracChangeset for help on using the changeset viewer.