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 | // ConversionCost.cc -- |
---|
8 | // |
---|
9 | // Author : Richard C. Bilson |
---|
10 | // Created On : Sun May 17 07:06:19 2015 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Mon Sep 25 15:43:34 2017 |
---|
13 | // Update Count : 10 |
---|
14 | // |
---|
15 | |
---|
16 | #include "ConversionCost.h" |
---|
17 | |
---|
18 | #include <cassert> // for assert |
---|
19 | #include <list> // for list, list<>::const_iterator |
---|
20 | #include <string> // for operator==, string |
---|
21 | |
---|
22 | #include "ResolvExpr/Cost.h" // for Cost |
---|
23 | #include "ResolvExpr/TypeEnvironment.h" // for EqvClass, TypeEnvironment |
---|
24 | #include "SymTab/Indexer.h" // for Indexer |
---|
25 | #include "SynTree/Declaration.h" // for TypeDecl, NamedTypeDecl |
---|
26 | #include "SynTree/Type.h" // for Type, BasicType, TypeInstType |
---|
27 | #include "typeops.h" // for typesCompatibleIgnoreQualifiers |
---|
28 | |
---|
29 | namespace ResolvExpr { |
---|
30 | const Cost Cost::zero = Cost( 0, 0, 0, 0 ); |
---|
31 | const Cost Cost::infinity = Cost( -1, -1, -1, -1 ); |
---|
32 | const Cost Cost::unsafe = Cost( 1, 0, 0, 0 ); |
---|
33 | const Cost Cost::poly = Cost( 0, 1, 0, 0 ); |
---|
34 | const Cost Cost::safe = Cost( 0, 0, 1, 0 ); |
---|
35 | const Cost Cost::reference = Cost( 0, 0, 0, 1 ); |
---|
36 | |
---|
37 | #if 0 |
---|
38 | #define PRINT(x) x |
---|
39 | #else |
---|
40 | #define PRINT(x) |
---|
41 | #endif |
---|
42 | Cost conversionCost( Type *src, Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ) { |
---|
43 | if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) { |
---|
44 | EqvClass eqvClass; |
---|
45 | NamedTypeDecl *namedType; |
---|
46 | PRINT( std::cerr << "type inst " << destAsTypeInst->get_name(); ) |
---|
47 | if ( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) { |
---|
48 | if ( eqvClass.type ) { |
---|
49 | return conversionCost( src, eqvClass.type, indexer, env ); |
---|
50 | } else { |
---|
51 | return Cost::infinity; |
---|
52 | } |
---|
53 | } else if ( ( namedType = indexer.lookupType( destAsTypeInst->get_name() ) ) ) { |
---|
54 | PRINT( std::cerr << " found" << std::endl; ) |
---|
55 | TypeDecl *type = dynamic_cast< TypeDecl* >( namedType ); |
---|
56 | // all typedefs should be gone by this point |
---|
57 | assert( type ); |
---|
58 | if ( type->get_base() ) { |
---|
59 | return conversionCost( src, type->get_base(), indexer, env ) + Cost::safe; |
---|
60 | } // if |
---|
61 | } // if |
---|
62 | PRINT( std::cerr << " not found" << std::endl; ) |
---|
63 | } // if |
---|
64 | PRINT( |
---|
65 | std::cerr << "src is "; |
---|
66 | src->print( std::cerr ); |
---|
67 | std::cerr << std::endl << "dest is "; |
---|
68 | dest->print( std::cerr ); |
---|
69 | std::cerr << std::endl << "env is" << std::endl; |
---|
70 | env.print( std::cerr, 8 ); |
---|
71 | ) |
---|
72 | if ( typesCompatibleIgnoreQualifiers( src, dest, indexer, env ) ) { |
---|
73 | PRINT( std::cerr << "compatible!" << std::endl; ) |
---|
74 | return Cost::zero; |
---|
75 | } else if ( dynamic_cast< VoidType* >( dest ) ) { |
---|
76 | return Cost::safe; |
---|
77 | } else if ( ReferenceType * refType = dynamic_cast< ReferenceType * > ( dest ) ) { |
---|
78 | PRINT( std::cerr << "conversionCost: dest is reference" << std::endl; ) |
---|
79 | return convertToReferenceCost( src, refType, indexer, env, [](Type * t1, Type * t2, const TypeEnvironment & env, const SymTab::Indexer &){ |
---|
80 | return ptrsAssignable( t1, t2, env ); |
---|
81 | }); |
---|
82 | } else { |
---|
83 | ConversionCost converter( dest, indexer, env ); |
---|
84 | src->accept( converter ); |
---|
85 | if ( converter.get_cost() == Cost::infinity ) { |
---|
86 | return Cost::infinity; |
---|
87 | } else { |
---|
88 | return converter.get_cost() + Cost::zero; |
---|
89 | } // if |
---|
90 | } // if |
---|
91 | } |
---|
92 | |
---|
93 | Cost convertToReferenceCost( Type * src, Type * dest, int diff, const SymTab::Indexer & indexer, const TypeEnvironment & env, PtrsFunction func ) { |
---|
94 | PRINT( std::cerr << "convert to reference cost... diff " << diff << std::endl; ) |
---|
95 | if ( diff > 0 ) { |
---|
96 | // TODO: document this |
---|
97 | Cost cost = convertToReferenceCost( strict_dynamic_cast< ReferenceType * >( src )->get_base(), dest, diff-1, indexer, env, func ); |
---|
98 | cost.incReference(); |
---|
99 | return cost; |
---|
100 | } else if ( diff < -1 ) { |
---|
101 | // TODO: document this |
---|
102 | Cost cost = convertToReferenceCost( src, strict_dynamic_cast< ReferenceType * >( dest )->get_base(), diff+1, indexer, env, func ); |
---|
103 | cost.incReference(); |
---|
104 | return cost; |
---|
105 | } else if ( diff == 0 ) { |
---|
106 | ReferenceType * srcAsRef = dynamic_cast< ReferenceType * >( src ); |
---|
107 | ReferenceType * destAsRef = dynamic_cast< ReferenceType * >( dest ); |
---|
108 | if ( srcAsRef && destAsRef ) { // pointer-like conversions between references |
---|
109 | PRINT( std::cerr << "converting between references" << std::endl; ) |
---|
110 | if ( srcAsRef->get_base()->get_qualifiers() <= destAsRef->get_base()->get_qualifiers() && typesCompatibleIgnoreQualifiers( srcAsRef->get_base(), destAsRef->get_base(), indexer, env ) ) { |
---|
111 | return Cost::safe; |
---|
112 | } else { // xxx - this discards reference qualifiers from consideration -- reducing qualifiers is a safe conversion; is this right? |
---|
113 | int assignResult = func( srcAsRef->get_base(), destAsRef->get_base(), env, indexer ); |
---|
114 | PRINT( std::cerr << "comparing references: " << assignResult << " " << srcAsRef << " " << destAsRef << std::endl; ) |
---|
115 | if ( assignResult < 0 ) { |
---|
116 | return Cost::safe; |
---|
117 | } else if ( assignResult > 0 ) { |
---|
118 | return Cost::unsafe; |
---|
119 | } // if |
---|
120 | } // if |
---|
121 | } else { |
---|
122 | PRINT( std::cerr << "reference to rvalue conversion" << std::endl; ) |
---|
123 | ConversionCost converter( dest, indexer, env ); |
---|
124 | src->accept( converter ); |
---|
125 | return converter.get_cost(); |
---|
126 | } // if |
---|
127 | } else { |
---|
128 | ReferenceType * destAsRef = dynamic_cast< ReferenceType * >( dest ); |
---|
129 | assert( diff == -1 && destAsRef ); |
---|
130 | PRINT( std::cerr << "dest is: " << dest << " / src is: " << src << std::endl; ) |
---|
131 | if ( typesCompatibleIgnoreQualifiers( src, destAsRef->get_base(), indexer, env ) ) { |
---|
132 | PRINT( std::cerr << "converting compatible base type" << std::endl; ) |
---|
133 | if ( src->get_lvalue() ) { |
---|
134 | PRINT( |
---|
135 | std::cerr << "lvalue to reference conversion" << std::endl; |
---|
136 | std::cerr << src << " => " << destAsRef << std::endl; |
---|
137 | ) |
---|
138 | // lvalue-to-reference conversion: cv lvalue T => cv T & |
---|
139 | if ( src->get_qualifiers() == destAsRef->get_base()->get_qualifiers() ) { |
---|
140 | return Cost::reference; // cost needs to be non-zero to add cast |
---|
141 | } if ( src->get_qualifiers() < destAsRef->get_base()->get_qualifiers() ) { |
---|
142 | return Cost::safe; // cost needs to be higher than previous cast to differentiate adding qualifiers vs. keeping same |
---|
143 | } else { |
---|
144 | return Cost::unsafe; |
---|
145 | } // if |
---|
146 | } else if ( destAsRef->get_base()->get_const() ) { |
---|
147 | PRINT( std::cerr << "rvalue to const ref conversion" << std::endl; ) |
---|
148 | // rvalue-to-const-reference conversion: T => const T & |
---|
149 | return Cost::safe; |
---|
150 | } else { |
---|
151 | PRINT( std::cerr << "rvalue to non-const reference conversion" << std::endl; ) |
---|
152 | // rvalue-to-reference conversion: T => T & |
---|
153 | return Cost::unsafe; |
---|
154 | } // if |
---|
155 | } // if |
---|
156 | PRINT( std::cerr << "attempting to convert from incompatible base type -- fail" << std::endl; ) |
---|
157 | } |
---|
158 | return Cost::infinity; |
---|
159 | } |
---|
160 | |
---|
161 | Cost convertToReferenceCost( Type * src, ReferenceType * dest, const SymTab::Indexer & indexer, const TypeEnvironment & env, PtrsFunction func ) { |
---|
162 | int sdepth = src->referenceDepth(), ddepth = dest->referenceDepth(); |
---|
163 | return convertToReferenceCost( src, dest, sdepth-ddepth, indexer, env, func ); |
---|
164 | } |
---|
165 | |
---|
166 | ConversionCost::ConversionCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ) |
---|
167 | : dest( dest ), indexer( indexer ), cost( Cost::infinity ), env( env ) { |
---|
168 | } |
---|
169 | |
---|
170 | /* |
---|
171 | Old |
---|
172 | === |
---|
173 | Double |
---|
174 | | |
---|
175 | Float |
---|
176 | | |
---|
177 | ULong |
---|
178 | / \ |
---|
179 | UInt Long |
---|
180 | \ / |
---|
181 | Int |
---|
182 | | |
---|
183 | Ushort |
---|
184 | | |
---|
185 | Short |
---|
186 | | |
---|
187 | Uchar |
---|
188 | / \ |
---|
189 | Schar Char |
---|
190 | |
---|
191 | New |
---|
192 | === |
---|
193 | +-----LongDoubleComplex--+ |
---|
194 | LongDouble--+ | +-LongDoubleImag |
---|
195 | | +---DoubleComplex---+ | |
---|
196 | Double------+ | +----DoubleImag |
---|
197 | | +-FloatComplex-+ | |
---|
198 | Float---------+ +-------FloatImag |
---|
199 | | |
---|
200 | ULongLong |
---|
201 | | |
---|
202 | LongLong |
---|
203 | | |
---|
204 | ULong |
---|
205 | / \ |
---|
206 | UInt Long |
---|
207 | \ / |
---|
208 | Int |
---|
209 | | |
---|
210 | Ushort |
---|
211 | | |
---|
212 | Short |
---|
213 | | |
---|
214 | Uchar |
---|
215 | / \ |
---|
216 | Schar Char |
---|
217 | \ / |
---|
218 | Bool |
---|
219 | */ |
---|
220 | |
---|
221 | static const int costMatrix[ BasicType::NUMBER_OF_BASIC_TYPES ][ BasicType::NUMBER_OF_BASIC_TYPES ] = { |
---|
222 | /* Src \ Dest: Bool Char SChar UChar Short UShort Int UInt Long ULong LLong ULLong Float Double LDbl FCplex DCplex LDCplex FImag DImag LDImag I128, U128 */ |
---|
223 | /* Bool */ { 0, 1, 1, 2, 3, 4, 5, 6, 6, 7, 8, 9, 12, 13, 14, 12, 13, 14, -1, -1, -1, 10, 11, }, |
---|
224 | /* Char */ { -1, 0, -1, 1, 2, 3, 4, 5, 5, 6, 7, 8, 11, 12, 13, 11, 12, 13, -1, -1, -1, 9, 10, }, |
---|
225 | /* SChar */ { -1, -1, 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 11, 12, 13, 11, 12, 13, -1, -1, -1, 9, 10, }, |
---|
226 | /* UChar */ { -1, -1, -1, 0, 1, 2, 3, 4, 4, 5, 6, 7, 10, 11, 12, 10, 11, 12, -1, -1, -1, 8, 9, }, |
---|
227 | /* Short */ { -1, -1, -1, -1, 0, 1, 2, 3, 3, 4, 5, 6, 9, 10, 11, 9, 10, 11, -1, -1, -1, 7, 8, }, |
---|
228 | /* UShort */{ -1, -1, -1, -1, -1, 0, 1, 2, 2, 3, 4, 5, 8, 9, 10, 8, 9, 10, -1, -1, -1, 6, 7, }, |
---|
229 | /* Int */ { -1, -1, -1, -1, -1, -1, 0, 1, 1, 2, 3, 4, 7, 8, 9, 7, 8, 9, -1, -1, -1, 5, 6, }, |
---|
230 | /* UInt */ { -1, -1, -1, -1, -1, -1, -1, 0, -1, 1, 2, 3, 6, 7, 8, 6, 7, 8, -1, -1, -1, 4, 5, }, |
---|
231 | /* Long */ { -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 6, 7, 8, 6, 7, 8, -1, -1, -1, 4, 5, }, |
---|
232 | /* ULong */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 5, 6, 7, 5, 6, 7, -1, -1, -1, 3, 4, }, |
---|
233 | /* LLong */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 4, 5, 6, 4, 5, 6, -1, -1, -1, 2, 3, }, |
---|
234 | /* ULLong */{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 3, 4, 5, 3, 4, 5, -1, -1, -1, 1, 2, }, |
---|
235 | |
---|
236 | /* Float */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 1, 2, 3, -1, -1, -1, -1, -1, }, |
---|
237 | /* Double */{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, -1, 1, 2, -1, -1, -1, -1, -1, }, |
---|
238 | /* LDbl */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, 1, -1, -1, -1, -1, -1, }, |
---|
239 | /* FCplex */{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, -1, -1, -1, -1, -1, }, |
---|
240 | /* DCplex */{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, -1, -1, -1, -1, -1, }, |
---|
241 | /* LDCplex */{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1, }, |
---|
242 | /* FImag */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 3, 0, 1, 2, -1, -1, }, |
---|
243 | /* DImag */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, -1, 0, 1, -1, -1, }, |
---|
244 | /* LDImag */{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 0, -1, -1, }, |
---|
245 | |
---|
246 | /* I128 */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, 3, 4, 3, 4, 5, -1, -1, -1, 0, 1, }, |
---|
247 | /* U128 */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 3, 2, 3, 4, -1, -1, -1, -1, 0, }, |
---|
248 | }; |
---|
249 | |
---|
250 | void ConversionCost::visit( __attribute((unused)) VoidType *voidType ) { |
---|
251 | cost = Cost::infinity; |
---|
252 | } |
---|
253 | |
---|
254 | void ConversionCost::visit(BasicType *basicType) { |
---|
255 | if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) { |
---|
256 | int tableResult = costMatrix[ basicType->get_kind() ][ destAsBasic->get_kind() ]; |
---|
257 | if ( tableResult == -1 ) { |
---|
258 | cost = Cost::unsafe; |
---|
259 | } else { |
---|
260 | cost = Cost::zero; |
---|
261 | cost.incSafe( tableResult ); |
---|
262 | } // if |
---|
263 | } else if ( dynamic_cast< EnumInstType *>( dest ) ) { |
---|
264 | // xxx - not positive this is correct, but appears to allow casting int => enum |
---|
265 | cost = Cost::unsafe; |
---|
266 | } else if ( dynamic_cast< ZeroType* >( dest ) != nullptr || dynamic_cast< OneType* >( dest ) != nullptr ) { |
---|
267 | cost = Cost::unsafe; |
---|
268 | } // if |
---|
269 | } |
---|
270 | |
---|
271 | void ConversionCost::visit(PointerType *pointerType) { |
---|
272 | if ( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) { |
---|
273 | PRINT( std::cerr << pointerType << " ===> " << destAsPtr; ) |
---|
274 | Type::Qualifiers tq1 = pointerType->get_base()->get_qualifiers(); |
---|
275 | Type::Qualifiers tq2 = destAsPtr->get_base()->get_qualifiers(); |
---|
276 | if ( tq1 <= tq2 && typesCompatibleIgnoreQualifiers( pointerType->get_base(), destAsPtr->get_base(), indexer, env ) ) { |
---|
277 | if ( tq1 == tq2 ) { |
---|
278 | // types are the same |
---|
279 | cost = Cost::zero; |
---|
280 | } else { |
---|
281 | // types are the same, except otherPointer has more qualifiers |
---|
282 | PRINT( std::cerr << " :: compatible and good qualifiers" << std::endl; ) |
---|
283 | cost = Cost::safe; |
---|
284 | } |
---|
285 | } else { // xxx - this discards qualifiers from consideration -- reducing qualifiers is a safe conversion; is this right? |
---|
286 | int assignResult = ptrsAssignable( pointerType->get_base(), destAsPtr->get_base(), env ); |
---|
287 | PRINT( std::cerr << " :: " << assignResult << std::endl; ) |
---|
288 | if ( assignResult < 0 && pointerType->get_base()->get_qualifiers() <= destAsPtr->get_qualifiers() ) { |
---|
289 | cost = Cost::safe; |
---|
290 | } else if ( assignResult > 0 ) { |
---|
291 | cost = Cost::unsafe; |
---|
292 | } // if |
---|
293 | // assignResult == 0 means Cost::Infinity |
---|
294 | } // if |
---|
295 | } else if ( dynamic_cast< ZeroType* >( dest ) != nullptr || dynamic_cast< OneType* >( dest ) != nullptr ) { |
---|
296 | cost = Cost::unsafe; |
---|
297 | } // if |
---|
298 | } |
---|
299 | |
---|
300 | void ConversionCost::visit(__attribute((unused)) ArrayType *arrayType) {} |
---|
301 | |
---|
302 | void ConversionCost::visit(ReferenceType *refType) { |
---|
303 | // Note: dest can never be a reference, since it would have been caught in an earlier check |
---|
304 | assert( ! dynamic_cast< ReferenceType * >( dest ) ); |
---|
305 | // convert reference to rvalue: cv T1 & => T2 |
---|
306 | // recursively compute conversion cost from T1 to T2. |
---|
307 | // cv can be safely dropped because of 'implicit dereference' behavior. |
---|
308 | refType->base->accept( *this ); |
---|
309 | if ( refType->base->get_qualifiers() == dest->get_qualifiers() ) { |
---|
310 | cost.incReference(); // prefer exact qualifiers |
---|
311 | } else if ( refType->base->get_qualifiers() < dest->get_qualifiers() ) { |
---|
312 | cost.incSafe(); // then gaining qualifiers |
---|
313 | } else { |
---|
314 | cost.incUnsafe(); // lose qualifiers as last resort |
---|
315 | } |
---|
316 | PRINT( std::cerr << refType << " ==> " << dest << " " << cost << std::endl; ) |
---|
317 | } |
---|
318 | |
---|
319 | void ConversionCost::visit(__attribute((unused)) FunctionType *functionType) {} |
---|
320 | |
---|
321 | void ConversionCost::visit(StructInstType *inst) { |
---|
322 | if ( StructInstType *destAsInst = dynamic_cast< StructInstType* >( dest ) ) { |
---|
323 | if ( inst->name == destAsInst->name ) { |
---|
324 | cost = Cost::zero; |
---|
325 | } // if |
---|
326 | } // if |
---|
327 | } |
---|
328 | |
---|
329 | void ConversionCost::visit(UnionInstType *inst) { |
---|
330 | if ( UnionInstType *destAsInst = dynamic_cast< UnionInstType* >( dest ) ) { |
---|
331 | if ( inst->name == destAsInst->name ) { |
---|
332 | cost = Cost::zero; |
---|
333 | } // if |
---|
334 | } // if |
---|
335 | } |
---|
336 | |
---|
337 | void ConversionCost::visit( __attribute((unused)) EnumInstType *inst ) { |
---|
338 | static Type::Qualifiers q; |
---|
339 | static BasicType integer( q, BasicType::SignedInt ); |
---|
340 | integer.accept( *this ); // safe if dest >= int |
---|
341 | if ( cost < Cost::unsafe ) { |
---|
342 | cost.incSafe(); |
---|
343 | } // if |
---|
344 | } |
---|
345 | |
---|
346 | void ConversionCost::visit( __attribute((unused)) TraitInstType *inst) { |
---|
347 | } |
---|
348 | |
---|
349 | void ConversionCost::visit(TypeInstType *inst) { |
---|
350 | EqvClass eqvClass; |
---|
351 | NamedTypeDecl *namedType; |
---|
352 | if ( env.lookup( inst->get_name(), eqvClass ) ) { |
---|
353 | cost = conversionCost( eqvClass.type, dest, indexer, env ); |
---|
354 | } else if ( TypeInstType *destAsInst = dynamic_cast< TypeInstType* >( dest ) ) { |
---|
355 | if ( inst->get_name() == destAsInst->get_name() ) { |
---|
356 | cost = Cost::zero; |
---|
357 | } |
---|
358 | } else if ( ( namedType = indexer.lookupType( inst->get_name() ) ) ) { |
---|
359 | TypeDecl *type = dynamic_cast< TypeDecl* >( namedType ); |
---|
360 | // all typedefs should be gone by this point |
---|
361 | assert( type ); |
---|
362 | if ( type->get_base() ) { |
---|
363 | cost = conversionCost( type->get_base(), dest, indexer, env ) + Cost::safe; |
---|
364 | } // if |
---|
365 | } // if |
---|
366 | } |
---|
367 | |
---|
368 | void ConversionCost::visit( __attribute((unused)) TupleType *tupleType) { |
---|
369 | Cost c = Cost::zero; |
---|
370 | if ( TupleType *destAsTuple = dynamic_cast< TupleType* >( dest ) ) { |
---|
371 | std::list< Type* >::const_iterator srcIt = tupleType->get_types().begin(); |
---|
372 | std::list< Type* >::const_iterator destIt = destAsTuple->get_types().begin(); |
---|
373 | while ( srcIt != tupleType->get_types().end() && destIt != destAsTuple->get_types().end() ) { |
---|
374 | Cost newCost = conversionCost( *srcIt++, *destIt++, indexer, env ); |
---|
375 | if ( newCost == Cost::infinity ) { |
---|
376 | return; |
---|
377 | } // if |
---|
378 | c += newCost; |
---|
379 | } // while |
---|
380 | if ( destIt != destAsTuple->get_types().end() ) { |
---|
381 | cost = Cost::infinity; |
---|
382 | } else { |
---|
383 | cost = c; |
---|
384 | } // if |
---|
385 | } // if |
---|
386 | } |
---|
387 | |
---|
388 | void ConversionCost::visit( __attribute((unused)) VarArgsType *varArgsType) { |
---|
389 | if ( dynamic_cast< VarArgsType* >( dest ) ) { |
---|
390 | cost = Cost::zero; |
---|
391 | } |
---|
392 | } |
---|
393 | |
---|
394 | void ConversionCost::visit( __attribute((unused)) ZeroType *zeroType) { |
---|
395 | if ( dynamic_cast< ZeroType* >( dest ) ) { |
---|
396 | cost = Cost::zero; |
---|
397 | } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) { |
---|
398 | // copied from visit(BasicType*) for signed int, but +1 for safe conversions |
---|
399 | int tableResult = costMatrix[ BasicType::SignedInt ][ destAsBasic->get_kind() ]; |
---|
400 | if ( tableResult == -1 ) { |
---|
401 | cost = Cost::unsafe; |
---|
402 | } else { |
---|
403 | cost = Cost::zero; |
---|
404 | cost.incSafe( tableResult + 1 ); |
---|
405 | } |
---|
406 | } else if ( dynamic_cast< PointerType* >( dest ) ) { |
---|
407 | cost = Cost::safe; |
---|
408 | } |
---|
409 | } |
---|
410 | |
---|
411 | void ConversionCost::visit( __attribute((unused)) OneType *oneType) { |
---|
412 | if ( dynamic_cast< OneType* >( dest ) ) { |
---|
413 | cost = Cost::zero; |
---|
414 | } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) { |
---|
415 | // copied from visit(BasicType*) for signed int, but +1 for safe conversions |
---|
416 | int tableResult = costMatrix[ BasicType::SignedInt ][ destAsBasic->get_kind() ]; |
---|
417 | if ( tableResult == -1 ) { |
---|
418 | cost = Cost::unsafe; |
---|
419 | } else { |
---|
420 | cost = Cost::zero; |
---|
421 | cost.incSafe( tableResult + 1 ); |
---|
422 | } |
---|
423 | } |
---|
424 | } |
---|
425 | } // namespace ResolvExpr |
---|
426 | |
---|
427 | // Local Variables: // |
---|
428 | // tab-width: 4 // |
---|
429 | // mode: c++ // |
---|
430 | // compile-command: "make install" // |
---|
431 | // End: // |
---|