Changeset e01eb4a for src


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.

Location:
src
Files:
13 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Expr.cpp

    r0bd46fd re01eb4a  
    2222#include "Copy.hpp"                // for shallowCopy
    2323#include "GenericSubstitution.hpp"
     24#include "Inspect.hpp"
    2425#include "LinkageSpec.hpp"
    2526#include "Stmt.hpp"
     
    2930#include "Common/SemanticError.h"
    3031#include "GenPoly/Lvalue.h"        // for referencesPermissable
    31 #include "InitTweak/InitTweak.h"   // for getFunction, getPointerBase
    3232#include "ResolvExpr/typeops.h"    // for extractResultType
    3333#include "Tuples/Tuples.h"         // for makeTupleType
     
    5858
    5959bool ApplicationExpr::get_lvalue() const {
    60         if ( const DeclWithType * func = InitTweak::getFunction( this ) ) {
     60        if ( const DeclWithType * func = getFunction( this ) ) {
    6161                return func->linkage == Linkage::Intrinsic && lvalueFunctionNames.count( func->name );
    6262        }
     
    6767
    6868bool UntypedExpr::get_lvalue() const {
    69         std::string fname = InitTweak::getFunctionName( this );
     69        std::string fname = getFunctionName( this );
    7070        return lvalueFunctionNames.count( fname );
    7171}
     
    7676        UntypedExpr * ret = createCall( loc, "*?", { arg } );
    7777        if ( const Type * ty = arg->result ) {
    78                 const Type * base = InitTweak::getPointerBase( ty );
     78                const Type * base = getPointerBase( ty );
    7979                assertf( base, "expected pointer type in dereference (type was %s)", toString( ty ).c_str() );
    8080
     
    335335        // first argument
    336336        assert( callExpr );
    337         const Expr * arg = InitTweak::getCallArg( callExpr, 0 );
     337        const Expr * arg = getCallArg( callExpr, 0 );
    338338        assert( arg );
    339339        result = arg->result;
  • src/AST/Inspect.cpp

    r0bd46fd re01eb4a  
    1010// Created On       : Fri Jun 24 13:16:31 2022
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Mon Jun 27 15:35:00 2022
    13 // Update Count     : 1
     12// Last Modified On : Wed Sep 22 13:50:00 2022
     13// Update Count     : 2
    1414//
    1515
    16 #include "AST/Decl.hpp"
    17 #include "AST/Type.hpp"
     16#include "Inspect.hpp"
    1817
    1918#include <iostream>
    20 #include <AST/Print.hpp>
     19
     20#include "AST/Decl.hpp"
     21#include "AST/Expr.hpp"
     22#include "AST/Print.hpp"
     23#include "AST/Stmt.hpp"
     24#include "AST/Type.hpp"
     25#include "CodeGen/OperatorTable.h"
    2126
    2227namespace ast {
     28
     29const Type * getPointerBase( const Type * t ) {
     30        if ( const auto * p = dynamic_cast< const PointerType * >( t ) ) {
     31                return p->base;
     32        } else if ( const auto * a = dynamic_cast< const ArrayType * >( t ) ) {
     33                return a->base;
     34        } else if ( const auto * r = dynamic_cast< const ReferenceType * >( t ) ) {
     35                return r->base;
     36        } else {
     37                return nullptr;
     38        }
     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 );
     51}
     52
     53template<typename CallExpr, typename Ret>
     54static Ret throughDeref( const CallExpr * expr, Ret(*func)( const Expr * ) ) {
     55        // In `(*f)(x)` the function we want is `f`.
     56        std::string name = getFunctionName( expr );
     57        assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() );
     58        assertf( !expr->args.empty(), "Cannot get function name from dereference with no arguments" );
     59        return func( expr->args.front() );
     60}
     61
     62static const DeclWithType * getCalledFunction( const Expr * expr ) {
     63        assert( expr );
     64        if ( const ast::VariableExpr * varExpr = dynamic_cast< const ast::VariableExpr * >( expr ) ) {
     65                return varExpr->var;
     66        } else if ( const ast::MemberExpr * memberExpr = dynamic_cast< const ast::MemberExpr * >( expr ) ) {
     67                return memberExpr->member;
     68        } else if ( const ast::CastExpr * castExpr = dynamic_cast< const ast::CastExpr * >( expr ) ) {
     69                return getCalledFunction( castExpr->arg );
     70        } else if ( const ast::UntypedExpr * untypedExpr = dynamic_cast< const ast::UntypedExpr * >( expr ) ) {
     71                return throughDeref( untypedExpr, getCalledFunction );
     72        } else if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * > ( expr ) ) {
     73                return throughDeref( appExpr, getCalledFunction );
     74        } else if ( const ast::AddressExpr * addrExpr = dynamic_cast< const ast::AddressExpr * >( expr ) ) {
     75                return getCalledFunction( addrExpr->arg );
     76        } else if ( const ast::CommaExpr * commaExpr = dynamic_cast< const ast::CommaExpr * >( expr ) ) {
     77                return getCalledFunction( commaExpr->arg2 );
     78        }
     79        return nullptr;
     80}
     81
     82const DeclWithType * getFunction( const Expr * expr ) {
     83        if ( auto app = dynamic_cast< const ApplicationExpr * >( expr ) ) {
     84                return getCalledFunction( app->func );
     85        } else if ( auto untyped = dynamic_cast< const UntypedExpr * >( expr ) ) {
     86                return getCalledFunction( untyped->func );
     87        }
     88        assertf( false, "getFunction received unknown expression: %s", toString( expr ).c_str() );
     89}
     90
     91// There is a lot of overlap with getCalledFunction. Ideally it would use
     92// it as a helper function and return the name of the DeclWithType. But the
     93// NameExpr and UntypedMemberExpr only work on this version.
     94static std::string funcName( const Expr * func ) {
     95        assert( func );
     96        if ( const ast::NameExpr * nameExpr = dynamic_cast< const ast::NameExpr * >( func ) ) {
     97                return nameExpr->name;
     98        } else if ( const ast::VariableExpr * varExpr = dynamic_cast< const ast::VariableExpr * >( func ) ) {
     99                return varExpr->var->name;
     100        } else if ( const ast::CastExpr * castExpr = dynamic_cast< const ast::CastExpr * >( func ) ) {
     101                return funcName( castExpr->arg );
     102        } else if ( const ast::MemberExpr * memberExpr = dynamic_cast< const ast::MemberExpr * >( func ) ) {
     103                return memberExpr->member->name;
     104        } else if ( const ast::UntypedMemberExpr * memberExpr = dynamic_cast< const ast::UntypedMemberExpr * > ( func ) ) {
     105                return funcName( memberExpr->member );
     106        } else if ( const ast::UntypedExpr * untypedExpr = dynamic_cast< const ast::UntypedExpr * >( func ) ) {
     107                return throughDeref( untypedExpr, funcName );
     108        } else if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * >( func ) ) {
     109                return throughDeref( appExpr, funcName );
     110        } else if ( const ast::ConstructorExpr * ctorExpr = dynamic_cast< const ast::ConstructorExpr * >( func ) ) {
     111                return funcName( getCallArg( ctorExpr->callExpr, 0 ) );
     112        } else {
     113                assertf( false, "Unexpected expression type being called as a function in call expression: %s", toString( func ).c_str() );
     114        }
     115}
     116
     117std::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.
     122        if ( auto app = dynamic_cast< const ApplicationExpr * >( expr ) ) {
     123                return funcName( app->func );
     124        } else if ( auto untyped = dynamic_cast< const UntypedExpr * >( expr ) ) {
     125                return funcName( untyped->func );
     126        } else {
     127                assertf( false, "Unexpected expression type passed to getFunctionName: %s", toString( expr ).c_str() );
     128        }
     129}
     130
     131const Expr * getCallArg( const Expr * call, unsigned int pos ) {
     132        if ( auto app = dynamic_cast< const ApplicationExpr * >( call ) ) {
     133                return callArg( app, pos );
     134        } else if ( auto untyped = dynamic_cast< const UntypedExpr * >( call ) ) {
     135                return callArg( untyped, pos );
     136        } else if ( auto tupleAssn = dynamic_cast< const TupleAssignExpr * >( call ) ) {
     137                const std::list<ptr<Stmt>>& stmts = tupleAssn->stmtExpr->stmts->kids;
     138                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() );
     141                assertf( !tuple->exprs.empty(), "TupleAssignExpr has empty tuple expr." );
     142                return getCallArg( tuple->exprs.front(), pos );
     143        } else if ( auto ctor = dynamic_cast< const ImplicitCopyCtorExpr * >( call ) ) {
     144                return getCallArg( ctor->callExpr, pos );
     145        } else {
     146                assertf( false, "Unexpected expression type passed to getCallArg: %s",
     147                        toString( call ).c_str() );
     148        }
     149}
    23150
    24151bool structHasFlexibleArray( const ast::StructDecl * decl ) {
     
    33160}
    34161
     162const ApplicationExpr * isIntrinsicCallExpr( const Expr * expr ) {
     163        auto appExpr = dynamic_cast< const ApplicationExpr * >( expr );
     164        if ( !appExpr ) return nullptr;
     165
     166        const DeclWithType * func = getCalledFunction( appExpr->func );
     167        assertf( func,
     168                "getCalledFunction returned nullptr: %s", toString( appExpr->func ).c_str() );
     169
     170        return func->linkage == ast::Linkage::Intrinsic ? appExpr : nullptr;
     171}
     172
    35173} // namespace ast
  • src/AST/Inspect.hpp

    r0bd46fd re01eb4a  
    1010// Created On       : Fri Jun 24 13:16:31 2022
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Mon Jun 27 15:35:00 2022
    13 // Update Count     : 1
     12// Last Modified On : Thr Sep 22 13:44:00 2022
     13// Update Count     : 2
    1414//
    1515
     
    1818namespace ast {
    1919
    20 // Does the structure end in a flexable array declaration?
    21 bool structHasFlexibleArray( const ast::StructDecl * );
     20/// Returns the base type of an pointer/array/reference type,
     21/// if the argument is not one of those types, return null.
     22const Type * getPointerBase( const Type * );
     23
     24/// Get the declaration of the function called (ApplicationExpr or UntypedExpr).
     25const DeclWithType * getFunction( const Expr * expr );
     26
     27/// Get the name of the function being called.
     28std::string getFunctionName( const Expr * expr );
     29
     30/// Returns the argument to a call expression in position N, indexed from 0.
     31const Expr * getCallArg( const Expr * call, unsigned pos );
     32
     33/// Does the structure end in a flexable array declaration?
     34bool structHasFlexibleArray( const StructDecl * );
     35
     36/// If the expression is an application whose target function is an
     37/// intrinsic, then returns a pointer to that application.
     38const ApplicationExpr * isIntrinsicCallExpr( const Expr * expr );
    2239
    2340}
  • src/AST/SymbolTable.cpp

    r0bd46fd re01eb4a  
    2020#include "Decl.hpp"
    2121#include "Expr.hpp"
     22#include "Inspect.hpp"
    2223#include "Type.hpp"
    2324#include "CodeGen/OperatorTable.h"  // for isCtorDtorAssign
     
    466467                assert( ! params.empty() );
    467468                // use base type of pointer, so that qualifiers on the pointer type aren't considered.
    468                 const Type * base = InitTweak::getPointerBase( params.front() );
     469                const Type * base = ast::getPointerBase( params.front() );
    469470                assert( base );
    470471                if (stripParams) {
  • src/AST/Type.cpp

    r0bd46fd re01eb4a  
    2222#include "Decl.hpp"
    2323#include "Init.hpp"
     24#include "Inspect.hpp"
    2425#include "Common/utility.h"      // for copy, move
    25 #include "InitTweak/InitTweak.h" // for getPointerBase
    2626#include "Tuples/Tuples.h"       // for isTtype
    2727
     
    3636        const Type * t;
    3737        const Type * a;
    38         for ( t = this; (a = InitTweak::getPointerBase( t )); t = a );
     38        for ( t = this; (a = ast::getPointerBase( t )); t = a );
    3939        return t;
    4040}
  • src/Common/Eval.cc

    r0bd46fd re01eb4a  
    1616#include <utility> // for pair
    1717
     18#include "AST/Inspect.hpp"
    1819#include "Common/PassVisitor.h"
    1920#include "CodeGen/OperatorTable.h"                                              // access: OperatorInfo
     
    177178
    178179        void postvisit( const ast::ApplicationExpr * expr ) {
    179                 const ast::DeclWithType * function = InitTweak::getFunction(expr);
     180                const ast::DeclWithType * function = ast::getFunction(expr);
    180181                if ( ! function || function->linkage != ast::Linkage::Intrinsic ) { valid = false; return; }
    181182                const std::string & fname = function->name;
  • src/Concurrency/KeywordsNew.cpp

    r0bd46fd re01eb4a  
    2121#include "AST/Decl.hpp"
    2222#include "AST/Expr.hpp"
     23#include "AST/Inspect.hpp"
    2324#include "AST/Pass.hpp"
    2425#include "AST/Stmt.hpp"
     
    15281529        const ast::ptr<ast::DeclWithType> & param = decl->params.front();
    15291530        auto type = dynamic_cast<const ast::StructInstType *>(
    1530                 InitTweak::getPointerBase( param->get_type() ) );
     1531                ast::getPointerBase( param->get_type() ) );
    15311532        if ( nullptr == type ) return decl;
    15321533        if ( !type->base->is_thread() ) return decl;
  • src/GenPoly/InstantiateGenericNew.cpp

    r0bd46fd re01eb4a  
    2323#include "AST/Copy.hpp"                // for deepCopy
    2424#include "AST/Create.hpp"              // for asForward
     25#include "AST/Inspect.hpp"             // for getFunction
    2526#include "AST/Pass.hpp"                // for Pass, WithGuard, WithShortCi...
    2627#include "AST/TranslationUnit.hpp"     // for TranslationUnit
     
    3031#include "GenPoly/GenPoly.h"           // for isPolyType, typesPolyCompatible
    3132#include "GenPoly/ScrubTyVars.h"       // for scrubAll
    32 #include "InitTweak/InitTweak.h"       // for getFunction
    3333#include "ResolvExpr/typeops.h"        // for typesCompatible
    3434
     
    294294                ast::ApplicationExpr const * expr ) {
    295295        GuardValue( isLValueArg ) = false;
    296         ast::Decl const * function = InitTweak::getFunction( expr );
     296        ast::Decl const * function = ast::getFunction( expr );
    297297        if ( ast::Linkage::Intrinsic != function->linkage
    298298                        || !CodeGen::isAssignment( function->name ) ) {
  • src/GenPoly/SpecializeNew.cpp

    r0bd46fd re01eb4a  
    1616#include "Specialize.h"
    1717
    18 #include "AST/Pass.hpp"
     18#include "AST/Inspect.hpp"               // for isIntrinsicCallExpr
     19#include "AST/Pass.hpp"                  // for Pass
    1920#include "AST/TypeEnvironment.hpp"       // for OpenVarSet, AssertionSet
    2021#include "Common/UniqueName.h"           // for UniqueName
    2122#include "GenPoly/GenPoly.h"             // for getFunctionType
    22 #include "InitTweak/InitTweak.h"         // for isIntrinsicCallExpr
    2323#include "ResolvExpr/FindOpenVars.h"     // for findOpenVars
    2424#include "ResolvExpr/TypeEnvironment.h"  // for FirstOpen, FirstClosed
    25 
    26 #include "AST/Print.hpp"
    2725
    2826namespace GenPoly {
     
    425423const ast::Expr * SpecializeCore::postvisit(
    426424                const ast::ApplicationExpr * expr ) {
    427         if ( InitTweak::isIntrinsicCallExpr( expr ) ) {
     425        if ( ast::isIntrinsicCallExpr( expr ) ) {
    428426                return expr;
    429427        }
  • src/InitTweak/FixInitNew.cpp

    r0bd46fd re01eb4a  
    1414#include <utility>                     // for pair
    1515
     16#include "AST/Inspect.hpp"             // for getFunction, getPointerBase, g...
    1617#include "CodeGen/GenType.h"           // for genPrettyType
    1718#include "CodeGen/OperatorTable.h"
     
    2425#include "GenInit.h"                   // for genCtorDtor
    2526#include "GenPoly/GenPoly.h"           // for getFunctionType
    26 #include "InitTweak.h"                 // for getFunctionName, getCallArg
    2727#include "ResolvExpr/Resolver.h"       // for findVoidExpression
    2828#include "ResolvExpr/typeops.h"        // for typesCompatible
  • 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 ) {
  • src/InitTweak/InitTweak.h

    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:20:00 2021
    13 // Update Count     : 8
     12// Last Modified On : Wed Sep 22  9:21:00 2022
     13// Update Count     : 9
    1414//
    1515
     
    7474        DeclarationWithType * getFunction( Expression * expr );
    7575        const DeclarationWithType * getFunction( const Expression * expr );
    76         const ast::DeclWithType * getFunction( const ast::Expr * expr );
    7776
    7877        /// Non-Null if expr is a call expression whose target function is intrinsic
    7978        ApplicationExpr * isIntrinsicCallExpr( Expression * expr );
    80         const ast::ApplicationExpr * isIntrinsicCallExpr( const ast::Expr * expr);
    8179
    8280        /// True if stmt is a call statement where the function called is intrinsic and takes one parameter.
     
    9896        /// returns the name of the function being called
    9997        std::string getFunctionName( Expression * expr );
    100         std::string getFunctionName( const ast::Expr * expr );
    10198
    10299        /// returns the argument to a call expression in position N indexed from 0
    103100        Expression *& getCallArg( Expression * callExpr, unsigned int pos );
    104         const ast::Expr * getCallArg( const ast::Expr * call, unsigned pos );
    105101
    106102        /// returns the base type of a PointerType or ArrayType, else returns NULL
    107103        Type * getPointerBase( Type * );
    108         const ast::Type* getPointerBase( const ast::Type* );
    109104
    110105        /// returns the argument if it is a PointerType or ArrayType, else returns NULL
  • src/Tuples/Tuples.cc

    r0bd46fd re01eb4a  
    1717
    1818#include "AST/Pass.hpp"
     19#include "AST/Inspect.hpp"
    1920#include "AST/LinkageSpec.hpp"
    2021#include "Common/PassVisitor.h"
     
    8081
    8182                void previsit( ast::ApplicationExpr const * appExpr ) {
    82                         if ( ast::DeclWithType const * function = InitTweak::getFunction( appExpr ) ) {
     83                        if ( ast::DeclWithType const * function = ast::getFunction( appExpr ) ) {
    8384                                if ( function->linkage == ast::Linkage::Intrinsic
    8485                                                && ( function->name == "*?" || function->name == "?[?]" ) ) {
Note: See TracChangeset for help on using the changeset viewer.