source: src/ResolvExpr/ConversionCost.cc@ 78a0b88

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 78a0b88 was 0c6596f, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Handle difference between reference casting and implicit conversion costs [fixes #39]

  • Property mode set to 100644
File size: 17.4 KB
Line 
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 : Wed Mar 2 17:35:46 2016
13// Update Count : 6
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
29namespace 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..." << std::endl; )
95 if ( diff > 0 ) {
96 // TODO: document this
97 Cost cost = convertToReferenceCost( safe_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, safe_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 if ( typesCompatibleIgnoreQualifiers( src, destAsRef->get_base(), indexer, env ) ) {
131 PRINT( std::cerr << "converting compatible base type" << std::endl; )
132 if ( src->get_lvalue() ) {
133 PRINT(
134 std::cerr << "lvalue to reference conversion" << std::endl;
135 std::cerr << src << " => " << destAsRef << std::endl;
136 )
137 // lvalue-to-reference conversion: cv lvalue T => cv T &
138 if ( src->get_qualifiers() == destAsRef->get_base()->get_qualifiers() ) {
139 return Cost::reference; // cost needs to be non-zero to add cast
140 } if ( src->get_qualifiers() < destAsRef->get_base()->get_qualifiers() ) {
141 return Cost::safe; // cost needs to be higher than previous cast to differentiate adding qualifiers vs. keeping same
142 } else {
143 return Cost::unsafe;
144 } // if
145 } else if ( destAsRef->get_base()->get_const() ) {
146 PRINT( std::cerr << "rvalue to const ref conversion" << std::endl; )
147 // rvalue-to-const-reference conversion: T => const T &
148 return Cost::safe;
149 } else {
150 PRINT( std::cerr << "rvalue to non-const reference conversion" << std::endl; )
151 // rvalue-to-reference conversion: T => T &
152 return Cost::unsafe;
153 } // if
154 } // if
155 PRINT( std::cerr << "attempting to convert from incompatible base type -- fail" << std::endl; )
156 }
157 return Cost::infinity;
158 }
159
160 Cost convertToReferenceCost( Type * src, ReferenceType * dest, const SymTab::Indexer & indexer, const TypeEnvironment & env, PtrsFunction func ) {
161 int sdepth = src->referenceDepth(), ddepth = dest->referenceDepth();
162 return convertToReferenceCost( src, dest, sdepth-ddepth, indexer, env, func );
163 }
164
165 ConversionCost::ConversionCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env )
166 : dest( dest ), indexer( indexer ), cost( Cost::infinity ), env( env ) {
167 }
168
169/*
170 Old
171 ===
172 Double
173 |
174 Float
175 |
176 ULong
177 / \
178 UInt Long
179 \ /
180 Int
181 |
182 Ushort
183 |
184 Short
185 |
186 Uchar
187 / \
188 Schar Char
189
190 New
191 ===
192 +-----LongDoubleComplex--+
193 LongDouble--+ | +-LongDoubleImag
194 | +---DoubleComplex---+ |
195 Double------+ | +----DoubleImag
196 | +-FloatComplex-+ |
197 Float---------+ +-------FloatImag
198 |
199 ULongLong
200 |
201 LongLong
202 |
203 ULong
204 / \
205 UInt Long
206 \ /
207 Int
208 |
209 Ushort
210 |
211 Short
212 |
213 Uchar
214 / \
215 Schar Char
216 \ /
217 Bool
218*/
219
220 static const int costMatrix[ BasicType::NUMBER_OF_BASIC_TYPES ][ BasicType::NUMBER_OF_BASIC_TYPES ] =
221 {
222 /* Src \ Dest: Bool Char SChar UChar Short UShort Int UInt Long ULong LLong ULLong Float Double LDbl FCplex DCplex LDCplex FImag DImag LDImag */
223 /* Bool */ { 0, 1, 1, 2, 3, 4, 5, 6, 6, 7, 8, 9, 10, 11, 12, 11, 12, 13, -1, -1, -1 },
224 /* Char */ { -1, 0, -1, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10, 11, 10, 11, 12, -1, -1, -1 },
225 /* SChar */ { -1, -1, 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10, 11, 10, 11, 12, -1, -1, -1 },
226 /* UChar */ { -1, -1, -1, 0, 1, 2, 3, 4, 4, 5, 6, 7, 8, 9, 10, 9, 10, 11, -1, -1, -1 },
227 /* Short */ { -1, -1, -1, -1, 0, 1, 2, 3, 3, 4, 5, 6, 7, 8, 9, 8, 9, 10, -1, -1, -1 },
228 /* UShort */{ -1, -1, -1, -1, -1, 0, 1, 2, 2, 3, 4, 5, 6, 7, 8, 7, 8, 9, -1, -1, -1 },
229 /* Int */ { -1, -1, -1, -1, -1, -1, 0, 1, 1, 2, 3, 4, 5, 6, 7, 6, 7, 8, -1, -1, -1 },
230 /* UInt */ { -1, -1, -1, -1, -1, -1, -1, 0, -1, 1, 2, 3, 4, 5, 6, 5, 6, 7, -1, -1, -1 },
231 /* Long */ { -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 5, 6, 7, -1, -1, -1 },
232 /* ULong */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 4, 5, 6, -1, -1, -1 },
233 /* LLong */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 3, 4, 5, -1, -1, -1 },
234 /* ULLong */{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 2, 3, 4, -1, -1, -1 },
235 /* Float */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 1, 2, 3, -1, -1, -1 },
236 /* Double */{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, -1, 1, 2, -1, -1, -1 },
237 /* LDbl */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, 1, -1, -1, -1 },
238 /* FCplex */{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, -1, -1, -1 },
239 /* DCplex */{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, -1, -1, -1 },
240 /* LDCplex */{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1 },
241 /* FImag */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 3, 0, 1, 2 },
242 /* DImag */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, -1, 0, 1 },
243 /* LDImag */{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 0 }
244 };
245
246 void ConversionCost::visit( __attribute((unused)) VoidType *voidType ) {
247 cost = Cost::infinity;
248 }
249
250 void ConversionCost::visit(BasicType *basicType) {
251 if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) {
252 int tableResult = costMatrix[ basicType->get_kind() ][ destAsBasic->get_kind() ];
253 if ( tableResult == -1 ) {
254 cost = Cost::unsafe;
255 } else {
256 cost = Cost::zero;
257 cost.incSafe( tableResult );
258 } // if
259 } else if ( dynamic_cast< EnumInstType *>( dest ) ) {
260 // xxx - not positive this is correct, but appears to allow casting int => enum
261 cost = Cost::unsafe;
262 } else if ( dynamic_cast< ZeroType* >( dest ) != nullptr || dynamic_cast< OneType* >( dest ) != nullptr ) {
263 cost = Cost::unsafe;
264 } // if
265 }
266
267 void ConversionCost::visit(PointerType *pointerType) {
268 if ( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) {
269 PRINT( std::cerr << pointerType << " ===> " << destAsPtr; )
270 Type::Qualifiers tq1 = pointerType->get_base()->get_qualifiers();
271 Type::Qualifiers tq2 = destAsPtr->get_base()->get_qualifiers();
272 if ( tq1 <= tq2 && typesCompatibleIgnoreQualifiers( pointerType->get_base(), destAsPtr->get_base(), indexer, env ) ) {
273 if ( tq1 == tq2 ) {
274 // types are the same
275 cost = Cost::zero;
276 } else {
277 // types are the same, except otherPointer has more qualifiers
278 PRINT( std::cerr << " :: compatible and good qualifiers" << std::endl; )
279 cost = Cost::safe;
280 }
281 } else { // xxx - this discards qualifiers from consideration -- reducing qualifiers is a safe conversion; is this right?
282 int assignResult = ptrsAssignable( pointerType->get_base(), destAsPtr->get_base(), env );
283 PRINT( std::cerr << " :: " << assignResult << std::endl; )
284 if ( assignResult < 0 && pointerType->get_base()->get_qualifiers() <= destAsPtr->get_qualifiers() ) {
285 cost = Cost::safe;
286 } else if ( assignResult > 0 ) {
287 cost = Cost::unsafe;
288 } // if
289 // assignResult == 0 means Cost::Infinity
290 } // if
291 } else if ( dynamic_cast< ZeroType* >( dest ) != nullptr || dynamic_cast< OneType* >( dest ) != nullptr ) {
292 cost = Cost::unsafe;
293 } // if
294 }
295
296 void ConversionCost::visit(__attribute((unused)) ArrayType *arrayType) {}
297
298 void ConversionCost::visit(ReferenceType *refType) {
299 // Note: dest can never be a reference, since it would have been caught in an earlier check
300 assert( ! dynamic_cast< ReferenceType * >( dest ) );
301 // convert reference to rvalue: cv T1 & => T2
302 // recursively compute conversion cost from T1 to T2.
303 // cv can be safely dropped because of 'implicit dereference' behavior.
304 refType->get_base()->accept( *this );
305 if ( refType->get_base()->get_qualifiers() == dest->get_qualifiers() ) {
306 cost.incReference(); // prefer exact qualifiers
307 } else if ( refType->get_base()->get_qualifiers() < dest->get_qualifiers() ) {
308 cost.incSafe(); // then gaining qualifiers
309 } else {
310 cost.incUnsafe(); // lose qualifiers as last resort
311 }
312 PRINT( std::cerr << refType << " ==> " << dest << " " << cost << std::endl; )
313 }
314
315 void ConversionCost::visit(__attribute((unused)) FunctionType *functionType) {}
316
317 void ConversionCost::visit(StructInstType *inst) {
318 if ( StructInstType *destAsInst = dynamic_cast< StructInstType* >( dest ) ) {
319 if ( inst->get_name() == destAsInst->get_name() ) {
320 cost = Cost::zero;
321 } // if
322 } // if
323 }
324
325 void ConversionCost::visit(UnionInstType *inst) {
326 if ( StructInstType *destAsInst = dynamic_cast< StructInstType* >( dest ) ) {
327 if ( inst->get_name() == destAsInst->get_name() ) {
328 cost = Cost::zero;
329 } // if
330 } // if
331 }
332
333 void ConversionCost::visit( __attribute((unused)) EnumInstType *inst ) {
334 static Type::Qualifiers q;
335 static BasicType integer( q, BasicType::SignedInt );
336 integer.accept( *this ); // safe if dest >= int
337 if ( cost < Cost::unsafe ) {
338 cost.incSafe();
339 } // if
340 }
341
342 void ConversionCost::visit( __attribute((unused)) TraitInstType *inst) {
343 }
344
345 void ConversionCost::visit(TypeInstType *inst) {
346 EqvClass eqvClass;
347 NamedTypeDecl *namedType;
348 if ( env.lookup( inst->get_name(), eqvClass ) ) {
349 cost = conversionCost( eqvClass.type, dest, indexer, env );
350 } else if ( TypeInstType *destAsInst = dynamic_cast< TypeInstType* >( dest ) ) {
351 if ( inst->get_name() == destAsInst->get_name() ) {
352 cost = Cost::zero;
353 }
354 } else if ( ( namedType = indexer.lookupType( inst->get_name() ) ) ) {
355 TypeDecl *type = dynamic_cast< TypeDecl* >( namedType );
356 // all typedefs should be gone by this point
357 assert( type );
358 if ( type->get_base() ) {
359 cost = conversionCost( type->get_base(), dest, indexer, env ) + Cost::safe;
360 } // if
361 } // if
362 }
363
364 void ConversionCost::visit( __attribute((unused)) TupleType *tupleType) {
365 Cost c = Cost::zero;
366 if ( TupleType *destAsTuple = dynamic_cast< TupleType* >( dest ) ) {
367 std::list< Type* >::const_iterator srcIt = tupleType->get_types().begin();
368 std::list< Type* >::const_iterator destIt = destAsTuple->get_types().begin();
369 while ( srcIt != tupleType->get_types().end() && destIt != destAsTuple->get_types().end() ) {
370 Cost newCost = conversionCost( *srcIt++, *destIt++, indexer, env );
371 if ( newCost == Cost::infinity ) {
372 return;
373 } // if
374 c += newCost;
375 } // while
376 if ( destIt != destAsTuple->get_types().end() ) {
377 cost = Cost::infinity;
378 } else {
379 cost = c;
380 } // if
381 } // if
382 }
383
384 void ConversionCost::visit( __attribute((unused)) VarArgsType *varArgsType) {
385 if ( dynamic_cast< VarArgsType* >( dest ) ) {
386 cost = Cost::zero;
387 }
388 }
389
390 void ConversionCost::visit( __attribute((unused)) ZeroType *zeroType) {
391 if ( dynamic_cast< ZeroType* >( dest ) ) {
392 cost = Cost::zero;
393 } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) {
394 // copied from visit(BasicType*) for signed int, but +1 for safe conversions
395 int tableResult = costMatrix[ BasicType::SignedInt ][ destAsBasic->get_kind() ];
396 if ( tableResult == -1 ) {
397 cost = Cost::unsafe;
398 } else {
399 cost = Cost::zero;
400 cost.incSafe( tableResult + 1 );
401 }
402 } else if ( dynamic_cast< PointerType* >( dest ) ) {
403 cost = Cost::safe;
404 }
405 }
406
407 void ConversionCost::visit( __attribute((unused)) OneType *oneType) {
408 if ( dynamic_cast< OneType* >( dest ) ) {
409 cost = Cost::zero;
410 } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) {
411 // copied from visit(BasicType*) for signed int, but +1 for safe conversions
412 int tableResult = costMatrix[ BasicType::SignedInt ][ destAsBasic->get_kind() ];
413 if ( tableResult == -1 ) {
414 cost = Cost::unsafe;
415 } else {
416 cost = Cost::zero;
417 cost.incSafe( tableResult + 1 );
418 }
419 }
420 }
421} // namespace ResolvExpr
422
423// Local Variables: //
424// tab-width: 4 //
425// mode: c++ //
426// compile-command: "make install" //
427// End: //
Note: See TracBrowser for help on using the repository browser.