source: translator/ResolvExpr/CastCost.cc @ a0d9f94

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since a0d9f94 was 51b7345, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

initial commit

  • Property mode set to 100644
File size: 3.2 KB
Line 
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
8#include "typeops.h"
9#include "Cost.h"
10#include "ConversionCost.h"
11#include "SynTree/Type.h"
12#include "SynTree/Visitor.h"
13#include "SymTab/Indexer.h"
14
15
16namespace ResolvExpr {
17
18class CastCost : public ConversionCost
19{
20public:
21  CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env );
22 
23  virtual void visit(BasicType *basicType);
24  virtual void visit(PointerType *pointerType);
25};
26
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      }
46    }
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}
65
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      }
101    }
102  }
103}
104
105} // namespace ResolvExpr
Note: See TracBrowser for help on using the repository browser.