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 : Andrew Beach |
---|
12 | // Last Modified On : Tue Oct 4 15:00:00 2019 |
---|
13 | // Update Count : 9 |
---|
14 | // |
---|
15 | |
---|
16 | #include "CastCost.hpp" |
---|
17 | |
---|
18 | #include <cassert> // for assert |
---|
19 | |
---|
20 | #include "AST/Print.hpp" |
---|
21 | #include "AST/SymbolTable.hpp" |
---|
22 | #include "AST/Type.hpp" |
---|
23 | #include "AST/TypeEnvironment.hpp" |
---|
24 | #include "ConversionCost.h" // for ConversionCost |
---|
25 | #include "Cost.h" // for Cost, Cost::infinity |
---|
26 | #include "ResolvExpr/ConversionCost.h" // for conversionCost |
---|
27 | #include "ResolvExpr/PtrsCastable.hpp" // for ptrsCastable |
---|
28 | #include "ResolvExpr/TypeEnvironment.h" // for TypeEnvironment, EqvClass |
---|
29 | #include "ResolvExpr/typeops.h" // for ptrsCastable |
---|
30 | #include "ResolvExpr/Unify.h" // for typesCompatibleIgnoreQualifiers |
---|
31 | #include "SymTab/Indexer.h" // for Indexer |
---|
32 | #include "SynTree/Declaration.h" // for TypeDecl, NamedTypeDecl |
---|
33 | #include "SynTree/Type.h" // for PointerType, Type, TypeInstType |
---|
34 | |
---|
35 | #if 0 |
---|
36 | #define PRINT(x) x |
---|
37 | #else |
---|
38 | #define PRINT(x) |
---|
39 | #endif |
---|
40 | |
---|
41 | namespace ResolvExpr { |
---|
42 | struct CastCost_old : public ConversionCost { |
---|
43 | public: |
---|
44 | CastCost_old( const Type * dest, bool srcIsLvalue, |
---|
45 | const SymTab::Indexer &indexer, const TypeEnvironment &env, CostFunction costFunc ); |
---|
46 | |
---|
47 | using ConversionCost::previsit; |
---|
48 | using ConversionCost::postvisit; |
---|
49 | void postvisit( const BasicType * basicType ); |
---|
50 | void postvisit( const PointerType * pointerType ); |
---|
51 | }; |
---|
52 | |
---|
53 | Cost castCost( const Type * src, const Type * dest, bool srcIsLvalue, |
---|
54 | const SymTab::Indexer &indexer, const TypeEnvironment &env ) { |
---|
55 | if ( const TypeInstType * destAsTypeInst = dynamic_cast< const TypeInstType * >( dest ) ) { |
---|
56 | if ( const EqvClass * eqvClass = env.lookup( destAsTypeInst->name ) ) { |
---|
57 | if ( eqvClass->type ) { |
---|
58 | return castCost( src, eqvClass->type, srcIsLvalue, indexer, env ); |
---|
59 | } else { |
---|
60 | return Cost::infinity; |
---|
61 | } |
---|
62 | } else if ( const NamedTypeDecl * namedType = indexer.lookupType( destAsTypeInst->name ) ) { |
---|
63 | // all typedefs should be gone by this point |
---|
64 | const TypeDecl * type = strict_dynamic_cast< const TypeDecl * >( namedType ); |
---|
65 | if ( type->base ) { |
---|
66 | return castCost( src, type->base, srcIsLvalue, indexer, env ) + Cost::safe; |
---|
67 | } // if |
---|
68 | } // if |
---|
69 | } // if |
---|
70 | |
---|
71 | PRINT( |
---|
72 | std::cerr << "castCost ::: src is "; |
---|
73 | src->print( std::cerr ); |
---|
74 | std::cerr << std::endl << "dest is "; |
---|
75 | dest->print( std::cerr ); |
---|
76 | std::cerr << std::endl << "env is" << std::endl; |
---|
77 | env.print( std::cerr, 8 ); |
---|
78 | ) |
---|
79 | |
---|
80 | if ( typesCompatibleIgnoreQualifiers( src, dest, indexer, env ) ) { |
---|
81 | PRINT( std::cerr << "compatible!" << std::endl; ) |
---|
82 | return Cost::zero; |
---|
83 | } else if ( dynamic_cast< const VoidType * >( dest ) ) { |
---|
84 | return Cost::safe; |
---|
85 | } else if ( const ReferenceType * refType = dynamic_cast< const ReferenceType * > ( dest ) ) { |
---|
86 | PRINT( std::cerr << "conversionCost: dest is reference" << std::endl; ) |
---|
87 | return convertToReferenceCost( src, refType, srcIsLvalue, indexer, env, [](const Type * t1, const Type * t2, const SymTab::Indexer & indexer, const TypeEnvironment & env ) { |
---|
88 | return ptrsCastable( t1, t2, env, indexer ); |
---|
89 | }); |
---|
90 | } else { |
---|
91 | PassVisitor<CastCost_old> converter( |
---|
92 | dest, srcIsLvalue, indexer, env, |
---|
93 | (Cost (*)( const Type *, const Type *, bool, const SymTab::Indexer &, const TypeEnvironment & )) |
---|
94 | castCost ); |
---|
95 | src->accept( converter ); |
---|
96 | if ( converter.pass.get_cost() == Cost::infinity ) { |
---|
97 | return Cost::infinity; |
---|
98 | } else { |
---|
99 | // xxx - why are we adding cost 0 here? |
---|
100 | return converter.pass.get_cost() + Cost::zero; |
---|
101 | } // if |
---|
102 | } // if |
---|
103 | } |
---|
104 | |
---|
105 | CastCost_old::CastCost_old( const Type * dest, bool srcIsLvalue, |
---|
106 | const SymTab::Indexer &indexer, const TypeEnvironment &env, CostFunction costFunc ) |
---|
107 | : ConversionCost( dest, srcIsLvalue, indexer, env, costFunc ) { |
---|
108 | } |
---|
109 | |
---|
110 | void CastCost_old::postvisit( const BasicType * basicType ) { |
---|
111 | const PointerType * destAsPointer = dynamic_cast< const PointerType * >( dest ); |
---|
112 | if ( destAsPointer && basicType->isInteger() ) { |
---|
113 | // necessary for, e.g. unsigned long => void * |
---|
114 | cost = Cost::unsafe; |
---|
115 | } else { |
---|
116 | cost = conversionCost( basicType, dest, srcIsLvalue, indexer, env ); |
---|
117 | } // if |
---|
118 | } |
---|
119 | |
---|
120 | void CastCost_old::postvisit( const PointerType * pointerType ) { |
---|
121 | if ( const PointerType * destAsPtr = dynamic_cast< const PointerType * >( dest ) ) { |
---|
122 | if ( pointerType->tq <= destAsPtr->tq && typesCompatibleIgnoreQualifiers( pointerType->base, destAsPtr->base, indexer, env ) ) { |
---|
123 | cost = Cost::safe; |
---|
124 | } else { |
---|
125 | TypeEnvironment newEnv( env ); |
---|
126 | newEnv.add( pointerType->forall ); |
---|
127 | newEnv.add( pointerType->base->forall ); |
---|
128 | int castResult = ptrsCastable( pointerType->base, destAsPtr->base, newEnv, indexer ); |
---|
129 | if ( castResult > 0 ) { |
---|
130 | cost = Cost::safe; |
---|
131 | } else if ( castResult < 0 ) { |
---|
132 | cost = Cost::infinity; |
---|
133 | } // if |
---|
134 | } // if |
---|
135 | } else if ( const BasicType * destAsBasic = dynamic_cast< const BasicType * >( dest ) ) { |
---|
136 | if ( destAsBasic->isInteger() ) { |
---|
137 | // necessary for, e.g. void * => unsigned long |
---|
138 | cost = Cost::unsafe; |
---|
139 | } // if |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | namespace { |
---|
144 | struct CastCost_new : public ConversionCost_new { |
---|
145 | using ConversionCost_new::previsit; |
---|
146 | using ConversionCost_new::postvisit; |
---|
147 | |
---|
148 | CastCost_new( |
---|
149 | const ast::Type * dst, bool srcIsLvalue, const ast::SymbolTable & symtab, |
---|
150 | const ast::TypeEnvironment & env, CostCalculation costFunc ) |
---|
151 | : ConversionCost_new( dst, srcIsLvalue, symtab, env, costFunc ) {} |
---|
152 | |
---|
153 | void postvisit( const ast::BasicType * basicType ) { |
---|
154 | auto ptr = dynamic_cast< const ast::PointerType * >( dst ); |
---|
155 | if ( ptr && basicType->isInteger() ) { |
---|
156 | // needed for, e.g. unsigned long => void * |
---|
157 | cost = Cost::unsafe; |
---|
158 | } else { |
---|
159 | cost = conversionCost( basicType, dst, srcIsLvalue, symtab, env ); |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | void postvisit( const ast::PointerType * pointerType ) { |
---|
164 | if ( auto ptr = dynamic_cast< const ast::PointerType * >( dst ) ) { |
---|
165 | if ( |
---|
166 | pointerType->qualifiers <= ptr->qualifiers |
---|
167 | && typesCompatibleIgnoreQualifiers( pointerType->base, ptr->base, symtab, env ) |
---|
168 | ) { |
---|
169 | cost = Cost::safe; |
---|
170 | } else { |
---|
171 | ast::TypeEnvironment newEnv{ env }; |
---|
172 | if ( auto wParams = pointerType->base.as< ast::FunctionType >() ) { |
---|
173 | newEnv.add( wParams->forall ); |
---|
174 | } |
---|
175 | int castResult = ptrsCastable( pointerType->base, ptr->base, symtab, newEnv ); |
---|
176 | if ( castResult > 0 ) { |
---|
177 | cost = Cost::safe; |
---|
178 | } else if ( castResult < 0 ) { |
---|
179 | cost = Cost::infinity; |
---|
180 | } |
---|
181 | } |
---|
182 | } else if ( auto basic = dynamic_cast< const ast::BasicType * >( dst ) ) { |
---|
183 | if ( basic->isInteger() ) { |
---|
184 | // necessary for, e.g. void * => unsigned long |
---|
185 | cost = Cost::unsafe; |
---|
186 | } |
---|
187 | } |
---|
188 | } |
---|
189 | }; |
---|
190 | |
---|
191 | #warning For overload resolution between the two versions. |
---|
192 | int localPtrsCastable(const ast::Type * t1, const ast::Type * t2, |
---|
193 | const ast::SymbolTable & symtab, const ast::TypeEnvironment & env ) { |
---|
194 | return ptrsCastable( t1, t2, symtab, env ); |
---|
195 | } |
---|
196 | Cost localCastCost( |
---|
197 | const ast::Type * src, const ast::Type * dst, bool srcIsLvalue, |
---|
198 | const ast::SymbolTable & symtab, const ast::TypeEnvironment & env |
---|
199 | ) { return castCost( src, dst, srcIsLvalue, symtab, env ); } |
---|
200 | } // anonymous namespace |
---|
201 | |
---|
202 | |
---|
203 | |
---|
204 | Cost castCost( |
---|
205 | const ast::Type * src, const ast::Type * dst, bool srcIsLvalue, |
---|
206 | const ast::SymbolTable & symtab, const ast::TypeEnvironment & env |
---|
207 | ) { |
---|
208 | if ( auto typeInst = dynamic_cast< const ast::TypeInstType * >( dst ) ) { |
---|
209 | if ( const ast::EqvClass * eqvClass = env.lookup( *typeInst ) ) { |
---|
210 | // check cast cost against bound type, if present |
---|
211 | if ( eqvClass->bound ) { |
---|
212 | return castCost( src, eqvClass->bound, srcIsLvalue, symtab, env ); |
---|
213 | } else { |
---|
214 | return Cost::infinity; |
---|
215 | } |
---|
216 | } else if ( const ast::NamedTypeDecl * named = symtab.lookupType( typeInst->name ) ) { |
---|
217 | // all typedefs should be gone by now |
---|
218 | auto type = strict_dynamic_cast< const ast::TypeDecl * >( named ); |
---|
219 | if ( type->base ) { |
---|
220 | return castCost( src, type->base, srcIsLvalue, symtab, env ) + Cost::safe; |
---|
221 | } |
---|
222 | } |
---|
223 | } |
---|
224 | |
---|
225 | PRINT( |
---|
226 | std::cerr << "castCost ::: src is "; |
---|
227 | ast::print( std::cerr, src ); |
---|
228 | std::cerr << std::endl << "dest is "; |
---|
229 | ast::print( std::cerr, dst ); |
---|
230 | std::cerr << std::endl << "env is" << std::endl; |
---|
231 | ast::print( std::cerr, env, 2 ); |
---|
232 | ) |
---|
233 | |
---|
234 | if ( typesCompatibleIgnoreQualifiers( src, dst, symtab, env ) ) { |
---|
235 | PRINT( std::cerr << "compatible!" << std::endl; ) |
---|
236 | return Cost::zero; |
---|
237 | } else if ( dynamic_cast< const ast::VoidType * >( dst ) ) { |
---|
238 | return Cost::safe; |
---|
239 | } else if ( auto refType = dynamic_cast< const ast::ReferenceType * >( dst ) ) { |
---|
240 | PRINT( std::cerr << "conversionCost: dest is reference" << std::endl; ) |
---|
241 | #warning cast on ptrsCastable artifact of having two functions, remove when port done |
---|
242 | return convertToReferenceCost( |
---|
243 | src, refType, srcIsLvalue, symtab, env, localPtrsCastable ); |
---|
244 | } else { |
---|
245 | #warning cast on castCost artifact of having two functions, remove when port done |
---|
246 | ast::Pass< CastCost_new > converter( |
---|
247 | dst, srcIsLvalue, symtab, env, localCastCost ); |
---|
248 | src->accept( converter ); |
---|
249 | return converter.core.cost; |
---|
250 | } |
---|
251 | } |
---|
252 | |
---|
253 | } // namespace ResolvExpr |
---|
254 | |
---|
255 | // Local Variables: // |
---|
256 | // tab-width: 4 // |
---|
257 | // mode: c++ // |
---|
258 | // compile-command: "make install" // |
---|
259 | // End: // |
---|