1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // CastCost.cc -- |
---|
8 | // |
---|
9 | // Author : Richard C. Bilson |
---|
10 | // Created On : Sun May 17 06:57:43 2015 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Tue Feb 2 15:34:36 2016 |
---|
13 | // Update Count : 7 |
---|
14 | // |
---|
15 | |
---|
16 | #include <cassert> // for assert |
---|
17 | |
---|
18 | #include "ConversionCost.h" // for ConversionCost |
---|
19 | #include "Cost.h" // for Cost, Cost::infinity |
---|
20 | #include "ResolvExpr/TypeEnvironment.h" // for TypeEnvironment, EqvClass |
---|
21 | #include "SymTab/Indexer.h" // for Indexer |
---|
22 | #include "SynTree/Declaration.h" // for TypeDecl, NamedTypeDecl |
---|
23 | #include "SynTree/Type.h" // for PointerType, Type, TypeInstType |
---|
24 | #include "typeops.h" // for typesCompatibleIgnoreQualifiers |
---|
25 | |
---|
26 | #if 0 |
---|
27 | #define PRINT(x) x |
---|
28 | #else |
---|
29 | #define PRINT(x) |
---|
30 | #endif |
---|
31 | |
---|
32 | namespace ResolvExpr { |
---|
33 | class CastCost : public ConversionCost { |
---|
34 | public: |
---|
35 | CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ); |
---|
36 | |
---|
37 | virtual void visit( BasicType *basicType ); |
---|
38 | virtual void visit( PointerType *pointerType ); |
---|
39 | }; |
---|
40 | |
---|
41 | Cost castCost( Type *src, Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ) { |
---|
42 | if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) { |
---|
43 | EqvClass eqvClass; |
---|
44 | NamedTypeDecl *namedType; |
---|
45 | if ( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) { |
---|
46 | if ( eqvClass.type ) { |
---|
47 | return castCost( src, eqvClass.type, indexer, env ); |
---|
48 | } else { |
---|
49 | return Cost::infinity; |
---|
50 | } |
---|
51 | } else if ( ( namedType = indexer.lookupType( destAsTypeInst->get_name() ) ) ) { |
---|
52 | // all typedefs should be gone by this point |
---|
53 | TypeDecl *type = strict_dynamic_cast< TypeDecl* >( namedType ); |
---|
54 | if ( type->get_base() ) { |
---|
55 | return castCost( src, type->get_base(), indexer, env ) + Cost::safe; |
---|
56 | } // if |
---|
57 | } // if |
---|
58 | } // if |
---|
59 | |
---|
60 | PRINT( |
---|
61 | std::cerr << "castCost ::: src is "; |
---|
62 | src->print( std::cerr ); |
---|
63 | std::cerr << std::endl << "dest is "; |
---|
64 | dest->print( std::cerr ); |
---|
65 | std::cerr << std::endl << "env is" << std::endl; |
---|
66 | env.print( std::cerr, 8 ); |
---|
67 | ) |
---|
68 | |
---|
69 | if ( typesCompatibleIgnoreQualifiers( src, dest, indexer, env ) ) { |
---|
70 | PRINT( std::cerr << "compatible!" << std::endl; ) |
---|
71 | return Cost::zero; |
---|
72 | } else if ( dynamic_cast< VoidType* >( dest ) ) { |
---|
73 | return Cost::safe; |
---|
74 | } else if ( ReferenceType * refType = dynamic_cast< ReferenceType * > ( dest ) ) { |
---|
75 | PRINT( std::cerr << "conversionCost: dest is reference" << std::endl; ) |
---|
76 | return convertToReferenceCost( src, refType, indexer, env, [](Type * t1, Type * t2, const TypeEnvironment & env, const SymTab::Indexer & indexer) { |
---|
77 | return ptrsCastable( t1, t2, env, indexer ); |
---|
78 | }); |
---|
79 | } else { |
---|
80 | CastCost converter( dest, indexer, env ); |
---|
81 | src->accept( converter ); |
---|
82 | if ( converter.get_cost() == Cost::infinity ) { |
---|
83 | return Cost::infinity; |
---|
84 | } else { |
---|
85 | // xxx - why are we adding cost 0 here? |
---|
86 | return converter.get_cost() + Cost::zero; |
---|
87 | } // if |
---|
88 | } // if |
---|
89 | } |
---|
90 | |
---|
91 | CastCost::CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ) |
---|
92 | : ConversionCost( dest, indexer, env ) { |
---|
93 | } |
---|
94 | |
---|
95 | void CastCost::visit( BasicType *basicType ) { |
---|
96 | PointerType *destAsPointer = dynamic_cast< PointerType* >( dest ); |
---|
97 | if ( destAsPointer && basicType->isInteger() ) { |
---|
98 | // necessary for, e.g. unsigned long => void* |
---|
99 | cost = Cost::unsafe; |
---|
100 | } else { |
---|
101 | cost = conversionCost( basicType, dest, indexer, env ); |
---|
102 | } // if |
---|
103 | } |
---|
104 | |
---|
105 | void CastCost::visit( PointerType *pointerType ) { |
---|
106 | if ( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) { |
---|
107 | if ( pointerType->get_qualifiers() <= destAsPtr->get_qualifiers() && typesCompatibleIgnoreQualifiers( pointerType->get_base(), destAsPtr->get_base(), indexer, env ) ) { |
---|
108 | cost = Cost::safe; |
---|
109 | } else { |
---|
110 | TypeEnvironment newEnv( env ); |
---|
111 | newEnv.add( pointerType->get_forall() ); |
---|
112 | newEnv.add( pointerType->get_base()->get_forall() ); |
---|
113 | int castResult = ptrsCastable( pointerType->get_base(), destAsPtr->get_base(), newEnv, indexer ); |
---|
114 | if ( castResult > 0 ) { |
---|
115 | cost = Cost::safe; |
---|
116 | } else if ( castResult < 0 ) { |
---|
117 | cost = Cost::infinity; |
---|
118 | } // if |
---|
119 | } // if |
---|
120 | } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) { |
---|
121 | if ( destAsBasic->isInteger() ) { |
---|
122 | // necessary for, e.g. void* => unsigned long |
---|
123 | cost = Cost::unsafe; |
---|
124 | } // if |
---|
125 | } |
---|
126 | } |
---|
127 | } // namespace ResolvExpr |
---|
128 | |
---|
129 | // Local Variables: // |
---|
130 | // tab-width: 4 // |
---|
131 | // mode: c++ // |
---|
132 | // compile-command: "make install" // |
---|
133 | // End: // |
---|