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