Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/InitTweak/InitTweak.cc

    r2b46a13 r10a7775  
    6060  }
    6161
    62   bool isInstrinsicSingleArgCallStmt( Statement * stmt ) {
    63     if ( stmt == NULL ) return false;
     62  Expression * getCtorDtorCall( Statement * stmt ) {
     63    if ( stmt == NULL ) return NULL;
    6464    if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( stmt ) ) {
    65       ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( exprStmt->get_expr() );
    66       assert( appExpr );
    67       VariableExpr * function = dynamic_cast< VariableExpr * >( appExpr->get_function() );
    68       assert( function );
    69       // check for Intrinsic only - don't want to remove all overridable ctor/dtors because autogenerated ctor/dtor
    70       // will call all member dtors, and some members may have a user defined dtor.
    71       FunctionType * funcType = GenPoly::getFunctionType( function->get_var()->get_type() );
    72       assert( funcType );
    73       return function->get_var()->get_linkage() == LinkageSpec::Intrinsic && funcType->get_parameters().size() == 1;
     65      return exprStmt->get_expr();
    7466    } else if ( CompoundStmt * compoundStmt = dynamic_cast< CompoundStmt * >( stmt ) ) {
    7567      // could also be a compound statement with a loop, in the case of an array
     
    7769      ForStmt * forStmt = dynamic_cast< ForStmt * >( compoundStmt->get_kids().back() );
    7870      assert( forStmt && forStmt->get_body() );
    79       return isInstrinsicSingleArgCallStmt( forStmt->get_body() );
     71      return getCtorDtorCall( forStmt->get_body() );
     72    } if ( ImplicitCtorDtorStmt * impCtorDtorStmt = dynamic_cast< ImplicitCtorDtorStmt * > ( stmt ) ) {
     73      return getCtorDtorCall( impCtorDtorStmt->get_callStmt() );
    8074    } else {
    8175      // should never get here
     
    8377    }
    8478  }
     79
     80  bool isInstrinsicSingleArgCallStmt( Statement * stmt ) {
     81    Expression * callExpr = getCtorDtorCall( stmt );
     82    if ( ! callExpr ) return false;
     83    ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( callExpr );
     84    assert( appExpr );
     85    VariableExpr * function = dynamic_cast< VariableExpr * >( appExpr->get_function() );
     86    assert( function );
     87    // check for Intrinsic only - don't want to remove all overridable ctor/dtors because autogenerated ctor/dtor
     88    // will call all member dtors, and some members may have a user defined dtor.
     89    FunctionType * funcType = GenPoly::getFunctionType( function->get_var()->get_type() );
     90    assert( funcType );
     91    return function->get_var()->get_linkage() == LinkageSpec::Intrinsic && funcType->get_parameters().size() == 1;
     92  }
     93
     94  namespace {
     95    template<typename CallExpr>
     96    Expression * callArg( CallExpr * callExpr, unsigned int pos ) {
     97      if ( pos >= callExpr->get_args().size() ) assert( false && "asking for argument that doesn't exist. Return NULL/throw exception?" );
     98      for ( Expression * arg : callExpr->get_args() ) {
     99        if ( pos == 0 ) return arg;
     100        pos--;
     101      }
     102      assert( false );
     103    }
     104  }
     105
     106  Expression * getCallArg( Expression * callExpr, unsigned int pos ) {
     107    if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( callExpr ) ) {
     108      return callArg( appExpr, pos );
     109    } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( callExpr ) ) {
     110      return callArg( untypedExpr, pos );
     111    } else {
     112      assert( false && "Unexpected expression type passed to getCallArg" );
     113    }
     114  }
     115
     116  namespace {
     117    template<typename CallExpr>
     118    std::string funcName( CallExpr * expr ) {
     119      Expression * func = expr->get_function();
     120      if ( NameExpr * nameExpr = dynamic_cast< NameExpr * >( func ) ) {
     121        return nameExpr->get_name();
     122      } else if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( func ) ) {
     123        return varExpr->get_var()->get_name();
     124      } else {
     125        assert( false && "Unexpected expression type being called as a function in call expression" );
     126      }
     127    }
     128  }
     129
     130  std::string getFunctionName( Expression * expr ) {
     131    if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ) ) {
     132      return funcName( appExpr );
     133    } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * > ( expr ) ) {
     134      return funcName( untypedExpr );
     135    } else {
     136      assert( false && "Unexpected expression type passed to getFunctionName" );
     137    }
     138  }
     139
     140  Type * getPointerBase( Type * type ) {
     141    if ( PointerType * ptrType = dynamic_cast< PointerType * >( type ) ) {
     142      return ptrType->get_base();
     143    } else if ( ArrayType * arrayType = dynamic_cast< ArrayType * >( type ) ) {
     144      return arrayType->get_base();
     145    } else {
     146      return NULL;
     147    }
     148  }
     149
    85150}
Note: See TracChangeset for help on using the changeset viewer.