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 | |
---|
16 | namespace ResolvExpr { |
---|
17 | |
---|
18 | class CastCost : public ConversionCost |
---|
19 | { |
---|
20 | public: |
---|
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 | |
---|
27 | Cost |
---|
28 | castCost( 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 | |
---|
66 | CastCost::CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ) |
---|
67 | : ConversionCost( dest, indexer, env ) |
---|
68 | { |
---|
69 | } |
---|
70 | |
---|
71 | void |
---|
72 | CastCost::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 | |
---|
81 | void |
---|
82 | CastCost::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 |
---|