Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • translator/ResolvExpr/CastCost.cc

    rc11e31c r51b73452  
     1/*
     2 * This file is part of the Cforall project
     3 *
     4 * $Id: CastCost.cc,v 1.11 2005/08/29 20:14:15 rcbilson Exp $
     5 *
     6 */
     7
    18#include "typeops.h"
    29#include "Cost.h"
     
    815
    916namespace ResolvExpr {
    10     class CastCost : public ConversionCost {
    11       public:
    12         CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env );
     17
     18class CastCost : public ConversionCost
     19{
     20public:
     21  CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env );
    1322 
    14         virtual void visit( BasicType *basicType );
    15         virtual void visit( PointerType *pointerType );
    16     };
     23  virtual void visit(BasicType *basicType);
     24  virtual void visit(PointerType *pointerType);
     25};
    1726
    18     Cost castCost( Type *src, Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
    19         if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) {
    20             EqvClass eqvClass;
    21             NamedTypeDecl *namedType;
    22             if ( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) {
    23                 return castCost( src, eqvClass.type, indexer, env );
    24             } else if ( ( namedType = indexer.lookupType( destAsTypeInst->get_name() ) ) ) {
    25                 TypeDecl *type = dynamic_cast< TypeDecl* >( namedType );
    26                 // all typedefs should be gone by this point
    27                 assert( type );
    28                 if ( type->get_base() ) {
    29                     return castCost( src, type->get_base(), indexer, env ) + Cost( 0, 0, 1 );
    30                 } // if
    31             } // if
    32         } // if
    33         if ( typesCompatibleIgnoreQualifiers( src, dest, indexer, env ) ) {
    34             return Cost( 0, 0, 0 );
    35         } else if ( dynamic_cast< VoidType* >( dest ) ) {
    36             return Cost( 0, 0, 1 );
    37         } else {
    38             CastCost converter( dest, indexer, env );
    39             src->accept( converter );
    40             if ( converter.get_cost() == Cost::infinity ) {
    41                 return Cost::infinity;
    42             } else {
    43                 return converter.get_cost() + Cost( 0, 0, 0 );
    44             } // if
    45         } // if
     27Cost
     28castCost( Type *src, Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env )
     29{
     30//  std::cout << "casting" << std::endl;
     31//  src->print( std::cout, 8 );
     32//  std::cout << std::endl << "to" << std::endl;
     33//  dest->print( std::cout, 8 );
     34  if( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) {
     35    EqvClass eqvClass;
     36    NamedTypeDecl *namedType;
     37    if( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) {
     38      return castCost( src, eqvClass.type, indexer, env );
     39    } else if( ( namedType = indexer.lookupType( destAsTypeInst->get_name() ) ) ) {
     40      TypeDecl *type = dynamic_cast< TypeDecl* >( namedType );
     41      // all typedefs should be gone by this point
     42      assert( type );
     43      if( type->get_base() ) {
     44        return castCost( src, type->get_base(), indexer, env ) + Cost( 0, 0, 1 );
     45      }
    4646    }
     47  }
     48  if( typesCompatibleIgnoreQualifiers( src, dest, indexer, env ) ) {
     49//    std::cout << "types are compatible" << std::endl;
     50    return Cost( 0, 0, 0 );
     51  } else if( dynamic_cast< VoidType* >( dest ) ) {
     52//    std::cout << "destination is void" << std::endl;
     53    return Cost( 0, 0, 1 );
     54  } else {
     55    CastCost converter( dest, indexer, env );
     56    src->accept( converter );
     57//    std::cout << "cost is " << converter.get_cost() << std::endl;
     58    if( converter.get_cost() == Cost::infinity ) {
     59      return Cost::infinity;
     60    } else {
     61      return converter.get_cost() + Cost( 0, 0, 0 );
     62    }
     63  }
     64}
    4765
    48     CastCost::CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env )
    49         : ConversionCost( dest, indexer, env ) {
     66CastCost::CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env )
     67  : ConversionCost( dest, indexer, env )
     68{
     69}
     70
     71void
     72CastCost::visit(BasicType *basicType)
     73{
     74  if( dynamic_cast< PointerType* >( dest ) ) {
     75    cost = Cost( 1, 0, 0 );
     76  } else {
     77    ConversionCost::visit( basicType );
     78  }
     79}
     80
     81void
     82CastCost::visit(PointerType *pointerType)
     83{
     84  if( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) {
     85    if( pointerType->get_qualifiers() <= destAsPtr->get_qualifiers() && typesCompatibleIgnoreQualifiers( pointerType->get_base(), destAsPtr->get_base(), indexer, env ) ) {
     86      cost = Cost( 0, 0, 1 );
     87    } else if( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) {
     88      if( destAsBasic->isInteger() ) {
     89        cost = Cost( 1, 0, 0 );
     90      }
     91    } else {
     92      TypeEnvironment newEnv( env );
     93      newEnv.add( pointerType->get_forall() );
     94      newEnv.add( pointerType->get_base()->get_forall() );
     95      int assignResult = ptrsCastable( pointerType->get_base(), destAsPtr->get_base(), newEnv, indexer );
     96      if( assignResult > 0 ) {
     97        cost = Cost( 0, 0, 1 );
     98      } else if( assignResult < 0 ) {
     99        cost = Cost( 1, 0, 0 );
     100      }
    50101    }
     102  }
     103}
    51104
    52     void CastCost::visit( BasicType *basicType ) {
    53         if ( dynamic_cast< PointerType* >( dest ) ) {
    54             cost = Cost( 1, 0, 0 );
    55         } else {
    56             ConversionCost::visit( basicType );
    57         } // if
    58     }
    59 
    60     void CastCost::visit( PointerType *pointerType ) {
    61         if ( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) {
    62             if ( pointerType->get_qualifiers() <= destAsPtr->get_qualifiers() && typesCompatibleIgnoreQualifiers( pointerType->get_base(), destAsPtr->get_base(), indexer, env ) ) {
    63                 cost = Cost( 0, 0, 1 );
    64             } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) {
    65                 if ( destAsBasic->isInteger() ) {
    66                     cost = Cost( 1, 0, 0 );
    67                 }
    68             } else {
    69                 TypeEnvironment newEnv( env );
    70                 newEnv.add( pointerType->get_forall() );
    71                 newEnv.add( pointerType->get_base()->get_forall() );
    72                 int assignResult = ptrsCastable( pointerType->get_base(), destAsPtr->get_base(), newEnv, indexer );
    73                 if ( assignResult > 0 ) {
    74                     cost = Cost( 0, 0, 1 );
    75                 } else if ( assignResult < 0 ) {
    76                     cost = Cost( 1, 0, 0 );
    77                 } // if
    78             } // if
    79         } // if
    80     }
    81105} // namespace ResolvExpr
Note: See TracChangeset for help on using the changeset viewer.