[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 |
---|
[4040425] | 12 | // Last Modified On : Wed Mar 2 17:35:46 2016 |
---|
| 13 | // Update Count : 6 |
---|
[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 { |
---|
[7ebaa56] | 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; |
---|
[5ccb10d] | 46 | PRINT( std::cerr << "type inst " << destAsTypeInst->get_name(); ) |
---|
[a32b204] | 47 | if ( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) { |
---|
[0b150ec] | 48 | if ( eqvClass.type ) { |
---|
| 49 | return conversionCost( src, eqvClass.type, indexer, env ); |
---|
| 50 | } else { |
---|
| 51 | return Cost::infinity; |
---|
| 52 | } |
---|
[a32b204] | 53 | } else if ( ( namedType = indexer.lookupType( destAsTypeInst->get_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 ); |
---|
| 58 | if ( type->get_base() ) { |
---|
[89be1c68] | 59 | return conversionCost( src, type->get_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; ) |
---|
[0c6596f] | 79 | return convertToReferenceCost( src, refType, indexer, env, [](Type * t1, Type * t2, const TypeEnvironment & env, const SymTab::Indexer &){ |
---|
| 80 | return ptrsAssignable( t1, t2, env ); |
---|
| 81 | }); |
---|
[a32b204] | 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 { |
---|
[89be1c68] | 88 | return converter.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 ) { |
---|
[5ccb10d] | 94 | PRINT( std::cerr << "convert to reference cost..." << std::endl; ) |
---|
[89be1c68] | 95 | if ( diff > 0 ) { |
---|
| 96 | // TODO: document this |
---|
[0c6596f] | 97 | Cost cost = convertToReferenceCost( safe_dynamic_cast< ReferenceType * >( src )->get_base(), dest, diff-1, indexer, env, func ); |
---|
[7ebaa56] | 98 | cost.incReference(); |
---|
[89be1c68] | 99 | return cost; |
---|
| 100 | } else if ( diff < -1 ) { |
---|
| 101 | // TODO: document this |
---|
[0c6596f] | 102 | Cost cost = convertToReferenceCost( src, safe_dynamic_cast< ReferenceType * >( dest )->get_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; ) |
---|
[89be1c68] | 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? |
---|
[0c6596f] | 113 | int assignResult = func( srcAsRef->get_base(), destAsRef->get_base(), env, indexer ); |
---|
[5ccb10d] | 114 | PRINT( std::cerr << "comparing references: " << assignResult << " " << srcAsRef << " " << destAsRef << std::endl; ) |
---|
[89be1c68] | 115 | if ( assignResult < 0 ) { |
---|
| 116 | return Cost::safe; |
---|
| 117 | } else if ( assignResult > 0 ) { |
---|
| 118 | return Cost::unsafe; |
---|
| 119 | } // if |
---|
[2463d0e] | 120 | } // if |
---|
| 121 | } else { |
---|
[5ccb10d] | 122 | PRINT( std::cerr << "reference to rvalue conversion" << std::endl; ) |
---|
[89be1c68] | 123 | ConversionCost converter( dest, indexer, env ); |
---|
| 124 | src->accept( converter ); |
---|
| 125 | return converter.get_cost(); |
---|
| 126 | } // if |
---|
[2463d0e] | 127 | } else { |
---|
[89be1c68] | 128 | ReferenceType * destAsRef = dynamic_cast< ReferenceType * >( dest ); |
---|
| 129 | assert( diff == -1 && destAsRef ); |
---|
| 130 | if ( typesCompatibleIgnoreQualifiers( src, destAsRef->get_base(), indexer, env ) ) { |
---|
[5ccb10d] | 131 | PRINT( std::cerr << "converting compatible base type" << std::endl; ) |
---|
[89be1c68] | 132 | if ( src->get_lvalue() ) { |
---|
[5ccb10d] | 133 | PRINT( |
---|
| 134 | std::cerr << "lvalue to reference conversion" << std::endl; |
---|
| 135 | std::cerr << src << " => " << destAsRef << std::endl; |
---|
| 136 | ) |
---|
[89be1c68] | 137 | // lvalue-to-reference conversion: cv lvalue T => cv T & |
---|
| 138 | if ( src->get_qualifiers() == destAsRef->get_base()->get_qualifiers() ) { |
---|
[7ebaa56] | 139 | return Cost::reference; // cost needs to be non-zero to add cast |
---|
[89be1c68] | 140 | } if ( src->get_qualifiers() < destAsRef->get_base()->get_qualifiers() ) { |
---|
[7ebaa56] | 141 | return Cost::safe; // cost needs to be higher than previous cast to differentiate adding qualifiers vs. keeping same |
---|
[89be1c68] | 142 | } else { |
---|
| 143 | return Cost::unsafe; |
---|
| 144 | } // if |
---|
| 145 | } else if ( destAsRef->get_base()->get_const() ) { |
---|
[5ccb10d] | 146 | PRINT( std::cerr << "rvalue to const ref conversion" << std::endl; ) |
---|
[89be1c68] | 147 | // rvalue-to-const-reference conversion: T => const T & |
---|
| 148 | return Cost::safe; |
---|
| 149 | } else { |
---|
[5ccb10d] | 150 | PRINT( std::cerr << "rvalue to non-const reference conversion" << std::endl; ) |
---|
[89be1c68] | 151 | // rvalue-to-reference conversion: T => T & |
---|
| 152 | return Cost::unsafe; |
---|
| 153 | } // if |
---|
| 154 | } // if |
---|
[5ccb10d] | 155 | PRINT( std::cerr << "attempting to convert from incompatible base type -- fail" << std::endl; ) |
---|
[2463d0e] | 156 | } |
---|
| 157 | return Cost::infinity; |
---|
| 158 | } |
---|
| 159 | |
---|
[0c6596f] | 160 | Cost convertToReferenceCost( Type * src, ReferenceType * dest, const SymTab::Indexer & indexer, const TypeEnvironment & env, PtrsFunction func ) { |
---|
[89be1c68] | 161 | int sdepth = src->referenceDepth(), ddepth = dest->referenceDepth(); |
---|
[0c6596f] | 162 | return convertToReferenceCost( src, dest, sdepth-ddepth, indexer, env, func ); |
---|
[89be1c68] | 163 | } |
---|
| 164 | |
---|
[a32b204] | 165 | ConversionCost::ConversionCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ) |
---|
| 166 | : dest( dest ), indexer( indexer ), cost( Cost::infinity ), env( env ) { |
---|
| 167 | } |
---|
[51b7345] | 168 | |
---|
| 169 | /* |
---|
[a32b204] | 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 |
---|
[51b7345] | 218 | */ |
---|
| 219 | |
---|
[a32b204] | 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 | |
---|
[7e003011] | 246 | void ConversionCost::visit( __attribute((unused)) VoidType *voidType ) { |
---|
[a32b204] | 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 ) { |
---|
[89be1c68] | 254 | cost = Cost::unsafe; |
---|
[a32b204] | 255 | } else { |
---|
[89be1c68] | 256 | cost = Cost::zero; |
---|
| 257 | cost.incSafe( tableResult ); |
---|
[a32b204] | 258 | } // if |
---|
[a436947] | 259 | } else if ( dynamic_cast< EnumInstType *>( dest ) ) { |
---|
| 260 | // xxx - not positive this is correct, but appears to allow casting int => enum |
---|
[89be1c68] | 261 | cost = Cost::unsafe; |
---|
[89e6ffc] | 262 | } else if ( dynamic_cast< ZeroType* >( dest ) != nullptr || dynamic_cast< OneType* >( dest ) != nullptr ) { |
---|
[89be1c68] | 263 | cost = Cost::unsafe; |
---|
[89e6ffc] | 264 | } // if |
---|
[a32b204] | 265 | } |
---|
| 266 | |
---|
| 267 | void ConversionCost::visit(PointerType *pointerType) { |
---|
| 268 | if ( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) { |
---|
[5ccb10d] | 269 | PRINT( std::cerr << pointerType << " ===> " << destAsPtr; ) |
---|
[cb43451] | 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 |
---|
[5ccb10d] | 278 | PRINT( std::cerr << " :: compatible and good qualifiers" << std::endl; ) |
---|
[cb43451] | 279 | cost = Cost::safe; |
---|
| 280 | } |
---|
| 281 | } else { // xxx - this discards qualifiers from consideration -- reducing qualifiers is a safe conversion; is this right? |
---|
[a32b204] | 282 | int assignResult = ptrsAssignable( pointerType->get_base(), destAsPtr->get_base(), env ); |
---|
[5ccb10d] | 283 | PRINT( std::cerr << " :: " << assignResult << std::endl; ) |
---|
[cb43451] | 284 | if ( assignResult < 0 && pointerType->get_base()->get_qualifiers() <= destAsPtr->get_qualifiers() ) { |
---|
[89be1c68] | 285 | cost = Cost::safe; |
---|
[a32b204] | 286 | } else if ( assignResult > 0 ) { |
---|
[89be1c68] | 287 | cost = Cost::unsafe; |
---|
[a32b204] | 288 | } // if |
---|
[cb43451] | 289 | // assignResult == 0 means Cost::Infinity |
---|
[a32b204] | 290 | } // if |
---|
[89e6ffc] | 291 | } else if ( dynamic_cast< ZeroType* >( dest ) != nullptr || dynamic_cast< OneType* >( dest ) != nullptr ) { |
---|
[89be1c68] | 292 | cost = Cost::unsafe; |
---|
[a32b204] | 293 | } // if |
---|
| 294 | } |
---|
| 295 | |
---|
[7e003011] | 296 | void ConversionCost::visit(__attribute((unused)) ArrayType *arrayType) {} |
---|
[2463d0e] | 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 ); |
---|
[cb43451] | 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 | } |
---|
[5ccb10d] | 312 | PRINT( std::cerr << refType << " ==> " << dest << " " << cost << std::endl; ) |
---|
[2463d0e] | 313 | } |
---|
| 314 | |
---|
[7e003011] | 315 | void ConversionCost::visit(__attribute((unused)) FunctionType *functionType) {} |
---|
[a32b204] | 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 | |
---|
[7e003011] | 333 | void ConversionCost::visit( __attribute((unused)) EnumInstType *inst ) { |
---|
[a32b204] | 334 | static Type::Qualifiers q; |
---|
| 335 | static BasicType integer( q, BasicType::SignedInt ); |
---|
[2463d0e] | 336 | integer.accept( *this ); // safe if dest >= int |
---|
[89be1c68] | 337 | if ( cost < Cost::unsafe ) { |
---|
[a32b204] | 338 | cost.incSafe(); |
---|
| 339 | } // if |
---|
| 340 | } |
---|
| 341 | |
---|
[7e003011] | 342 | void ConversionCost::visit( __attribute((unused)) TraitInstType *inst) { |
---|
[a32b204] | 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() ) { |
---|
[89be1c68] | 359 | cost = conversionCost( type->get_base(), dest, indexer, env ) + Cost::safe; |
---|
[a32b204] | 360 | } // if |
---|
| 361 | } // if |
---|
| 362 | } |
---|
| 363 | |
---|
[7e003011] | 364 | void ConversionCost::visit( __attribute((unused)) TupleType *tupleType) { |
---|
[89be1c68] | 365 | Cost c = Cost::zero; |
---|
[a32b204] | 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(); |
---|
[4ab9536] | 369 | while ( srcIt != tupleType->get_types().end() && destIt != destAsTuple->get_types().end() ) { |
---|
[a32b204] | 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 | } |
---|
[44b7088] | 383 | |
---|
[7e003011] | 384 | void ConversionCost::visit( __attribute((unused)) VarArgsType *varArgsType) { |
---|
[90c3b1c] | 385 | if ( dynamic_cast< VarArgsType* >( dest ) ) { |
---|
[44b7088] | 386 | cost = Cost::zero; |
---|
| 387 | } |
---|
| 388 | } |
---|
[89e6ffc] | 389 | |
---|
[7e003011] | 390 | void ConversionCost::visit( __attribute((unused)) ZeroType *zeroType) { |
---|
[89e6ffc] | 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 ) { |
---|
[89be1c68] | 397 | cost = Cost::unsafe; |
---|
[89e6ffc] | 398 | } else { |
---|
[89be1c68] | 399 | cost = Cost::zero; |
---|
| 400 | cost.incSafe( tableResult + 1 ); |
---|
[89e6ffc] | 401 | } |
---|
| 402 | } else if ( dynamic_cast< PointerType* >( dest ) ) { |
---|
[89be1c68] | 403 | cost = Cost::safe; |
---|
[89e6ffc] | 404 | } |
---|
| 405 | } |
---|
| 406 | |
---|
[7e003011] | 407 | void ConversionCost::visit( __attribute((unused)) OneType *oneType) { |
---|
[89e6ffc] | 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 ) { |
---|
[89be1c68] | 414 | cost = Cost::unsafe; |
---|
[89e6ffc] | 415 | } else { |
---|
[89be1c68] | 416 | cost = Cost::zero; |
---|
| 417 | cost.incSafe( tableResult + 1 ); |
---|
[89e6ffc] | 418 | } |
---|
| 419 | } |
---|
| 420 | } |
---|
[51b7345] | 421 | } // namespace ResolvExpr |
---|
[a32b204] | 422 | |
---|
| 423 | // Local Variables: // |
---|
| 424 | // tab-width: 4 // |
---|
| 425 | // mode: c++ // |
---|
| 426 | // compile-command: "make install" // |
---|
| 427 | // End: // |
---|