Ignore:
Timestamp:
Sep 22, 2022, 2:23:18 PM (21 months ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
ADT, ast-experimental, master
Children:
5d8dae7
Parents:
0bd46fd
Message:

Moved some functions from InitTweak? to Inspect.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/InitTweak/InitTweak.cc

    r0bd46fd re01eb4a  
    1010// Created On       : Fri May 13 11:26:36 2016
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Mon Dec  6 13:21:00 2021
    13 // Update Count     : 20
     12// Last Modified On : Wed Sep 22  9:50:00 2022
     13// Update Count     : 21
    1414//
    1515
     
    2323#include "AST/Expr.hpp"
    2424#include "AST/Init.hpp"
     25#include "AST/Inspect.hpp"
    2526#include "AST/Node.hpp"
    2627#include "AST/Pass.hpp"
     
    654655        namespace {
    655656                DeclarationWithType * getCalledFunction( Expression * expr );
    656                 const ast::DeclWithType * getCalledFunction( const ast::Expr * expr );
    657657
    658658                template<typename CallExpr>
     
    664664                        return getCalledFunction( expr->get_args().front() );
    665665                }
    666 
    667                 template<typename CallExpr>
    668                 const ast::DeclWithType * handleDerefCalledFunction( const CallExpr * expr ) {
    669                         // (*f)(x) => should get "f"
    670                         std::string name = getFunctionName( expr );
    671                         assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() );
    672                         assertf( ! expr->args.empty(), "Cannot get called function from dereference with no arguments" );
    673                         return getCalledFunction( expr->args.front() );
    674                 }
    675 
    676666
    677667                DeclarationWithType * getCalledFunction( Expression * expr ) {
     
    695685                }
    696686
    697                 const ast::DeclWithType * getCalledFunction( const ast::Expr * expr ) {
    698                         assert( expr );
    699                         if ( const ast::VariableExpr * varExpr = dynamic_cast< const ast::VariableExpr * >( expr ) ) {
    700                                 return varExpr->var;
    701                         } else if ( const ast::MemberExpr * memberExpr = dynamic_cast< const ast::MemberExpr * >( expr ) ) {
    702                                 return memberExpr->member;
    703                         } else if ( const ast::CastExpr * castExpr = dynamic_cast< const ast::CastExpr * >( expr ) ) {
    704                                 return getCalledFunction( castExpr->arg );
    705                         } else if ( const ast::UntypedExpr * untypedExpr = dynamic_cast< const ast::UntypedExpr * >( expr ) ) {
    706                                 return handleDerefCalledFunction( untypedExpr );
    707                         } else if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * > ( expr ) ) {
    708                                 return handleDerefCalledFunction( appExpr );
    709                         } else if ( const ast::AddressExpr * addrExpr = dynamic_cast< const ast::AddressExpr * >( expr ) ) {
    710                                 return getCalledFunction( addrExpr->arg );
    711                         } else if ( const ast::CommaExpr * commaExpr = dynamic_cast< const ast::CommaExpr * >( expr ) ) {
    712                                 return getCalledFunction( commaExpr->arg2 );
    713                         }
    714                         return nullptr;
    715                 }
    716 
    717687                DeclarationWithType * getFunctionCore( const Expression * expr ) {
    718688                        if ( const auto * appExpr = dynamic_cast< const ApplicationExpr * >( expr ) ) {
     
    731701        const DeclarationWithType * getFunction( const Expression * expr ) {
    732702                return getFunctionCore( expr );
    733         }
    734 
    735         const ast::DeclWithType * getFunction( const ast::Expr * expr ) {
    736                 if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * >( expr ) ) {
    737                         return getCalledFunction( appExpr->func );
    738                 } else if ( const ast::UntypedExpr * untyped = dynamic_cast< const ast::UntypedExpr * > ( expr ) ) {
    739                         return getCalledFunction( untyped->func );
    740                 }
    741                 assertf( false, "getFunction received unknown expression: %s", toString( expr ).c_str() );
    742703        }
    743704
     
    752713        }
    753714
    754         const ast::ApplicationExpr * isIntrinsicCallExpr( const ast::Expr * expr ) {
    755                 auto appExpr = dynamic_cast< const ast::ApplicationExpr * >( expr );
    756                 if ( ! appExpr ) return nullptr;
    757 
    758                 const ast::DeclWithType * func = getCalledFunction( appExpr->func );
    759                 assertf( func,
    760                         "getCalledFunction returned nullptr: %s", toString( appExpr->func ).c_str() );
    761 
    762                 // check for Intrinsic only -- don't want to remove all overridable ctor/dtor because
    763                 // autogenerated ctor/dtor will call all member dtors, and some members may have a
    764                 // user-defined dtor
    765                 return func->linkage == ast::Linkage::Intrinsic ? appExpr : nullptr;
    766         }
    767 
    768715        namespace {
    769716                template <typename Predicate>
     
    817764                                if ( pos == 0 ) return arg;
    818765                                pos--;
    819                         }
    820                         assert( false );
    821                 }
    822 
    823                 template<typename CallExpr>
    824                 const ast::Expr * callArg( const CallExpr * call, unsigned int pos ) {
    825                         if( pos >= call->args.size() ) {
    826                                 assertf( false, "getCallArg for argument that doesn't exist: (%u); %s.",
    827                                         pos, toString( call ).c_str() );
    828                         }
    829                         for ( const ast::Expr * arg : call->args ) {
    830                                 if ( pos == 0 ) return arg;
    831                                 --pos;
    832766                        }
    833767                        assert( false );
     
    854788        }
    855789
    856         const ast::Expr * getCallArg( const ast::Expr * call, unsigned pos ) {
    857                 if ( auto app = dynamic_cast< const ast::ApplicationExpr * >( call ) ) {
    858                         return callArg( app, pos );
    859                 } else if ( auto untyped = dynamic_cast< const ast::UntypedExpr * >( call ) ) {
    860                         return callArg( untyped, pos );
    861                 } else if ( auto tupleAssn = dynamic_cast< const ast::TupleAssignExpr * >( call ) ) {
    862                         const std::list<ast::ptr<ast::Stmt>>& stmts = tupleAssn->stmtExpr->stmts->kids;
    863                         assertf( ! stmts.empty(), "TupleAssignExpr missing statements." );
    864                         auto stmt  = strict_dynamic_cast< const ast::ExprStmt * >( stmts.back().get() );
    865                         auto tuple = strict_dynamic_cast< const ast::TupleExpr * >( stmt->expr.get() );
    866                         assertf( ! tuple->exprs.empty(), "TupleAssignExpr has empty tuple expr.");
    867                         return getCallArg( tuple->exprs.front(), pos );
    868                 } else if ( auto ctor = dynamic_cast< const ast::ImplicitCopyCtorExpr * >( call ) ) {
    869                         return getCallArg( ctor->callExpr, pos );
    870                 } else {
    871                         assertf( false, "Unexpected expression type passed to getCallArg: %s",
    872                                 toString( call ).c_str() );
    873                 }
    874         }
    875 
    876790        namespace {
    877791                std::string funcName( Expression * func );
    878                 std::string funcName( const ast::Expr * func );
    879792
    880793                template<typename CallExpr>
     
    885798                        assertf( ! expr->get_args().empty(), "Cannot get function name from dereference with no arguments" );
    886799                        return funcName( expr->get_args().front() );
    887                 }
    888 
    889                 template<typename CallExpr>
    890                 std::string handleDerefName( const CallExpr * expr ) {
    891                         // (*f)(x) => should get name "f"
    892                         std::string name = getFunctionName( expr );
    893                         assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() );
    894                         assertf( ! expr->args.empty(), "Cannot get function name from dereference with no arguments" );
    895                         return funcName( expr->args.front() );
    896800                }
    897801
     
    917821                        }
    918822                }
    919 
    920                 std::string funcName( const ast::Expr * func ) {
    921                         if ( const ast::NameExpr * nameExpr = dynamic_cast< const ast::NameExpr * >( func ) ) {
    922                                 return nameExpr->name;
    923                         } else if ( const ast::VariableExpr * varExpr = dynamic_cast< const ast::VariableExpr * >( func ) ) {
    924                                 return varExpr->var->name;
    925                         } else if ( const ast::CastExpr * castExpr = dynamic_cast< const ast::CastExpr * >( func ) ) {
    926                                 return funcName( castExpr->arg );
    927                         } else if ( const ast::MemberExpr * memberExpr = dynamic_cast< const ast::MemberExpr * >( func ) ) {
    928                                 return memberExpr->member->name;
    929                         } else if ( const ast::UntypedMemberExpr * memberExpr = dynamic_cast< const ast::UntypedMemberExpr * > ( func ) ) {
    930                                 return funcName( memberExpr->member );
    931                         } else if ( const ast::UntypedExpr * untypedExpr = dynamic_cast< const ast::UntypedExpr * >( func ) ) {
    932                                 return handleDerefName( untypedExpr );
    933                         } else if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * >( func ) ) {
    934                                 return handleDerefName( appExpr );
    935                         } else if ( const ast::ConstructorExpr * ctorExpr = dynamic_cast< const ast::ConstructorExpr * >( func ) ) {
    936                                 return funcName( getCallArg( ctorExpr->callExpr, 0 ) );
    937                         } else {
    938                                 assertf( false, "Unexpected expression type being called as a function in call expression: %s", toString( func ).c_str() );
    939                         }
    940                 }
    941823        }
    942824
     
    955837        }
    956838
    957         std::string getFunctionName( const ast::Expr * expr ) {
    958                 // there's some unforunate overlap here with getCalledFunction. Ideally this would be able to use getCalledFunction and
    959                 // return the name of the DeclarationWithType, but this needs to work for NameExpr and UntypedMemberExpr, where getCalledFunction
    960                 // can't possibly do anything reasonable.
    961                 if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * >( expr ) ) {
    962                         return funcName( appExpr->func );
    963                 } else if ( const ast::UntypedExpr * untypedExpr = dynamic_cast< const ast::UntypedExpr * > ( expr ) ) {
    964                         return funcName( untypedExpr->func );
    965                 } else {
    966                         std::cerr << expr << std::endl;
    967                         assertf( false, "Unexpected expression type passed to getFunctionName" );
    968                 }
    969         }
    970 
    971839        Type * getPointerBase( Type * type ) {
    972840                if ( PointerType * ptrType = dynamic_cast< PointerType * >( type ) ) {
     
    979847                        return nullptr;
    980848                }
    981         }
    982         const ast::Type* getPointerBase( const ast::Type* t ) {
    983                 if ( const auto * p = dynamic_cast< const ast::PointerType * >( t ) ) {
    984                         return p->base;
    985                 } else if ( const auto * a = dynamic_cast< const ast::ArrayType * >( t ) ) {
    986                         return a->base;
    987                 } else if ( const auto * r = dynamic_cast< const ast::ReferenceType * >( t ) ) {
    988                         return r->base;
    989                 } else return nullptr;
    990849        }
    991850
     
    12031062        if ( ftype->params.size() != 2 ) return false;
    12041063
    1205         const ast::Type * t1 = getPointerBase( ftype->params.front() );
     1064        const ast::Type * t1 = ast::getPointerBase( ftype->params.front() );
    12061065        if ( ! t1 ) return false;
    12071066        const ast::Type * t2 = ftype->params.back();
    12081067
    1209         return ResolvExpr::typesCompatibleIgnoreQualifiers( t1, t2, ast::SymbolTable{} );
     1068        return ResolvExpr::typesCompatibleIgnoreQualifiers( t1, t2, ast::SymbolTable() );
    12101069}
     1070
    12111071
    12121072        const FunctionDecl * isAssignment( const Declaration * decl ) {
Note: See TracChangeset for help on using the changeset viewer.