[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 |
---|
[ada4575] | 12 | // Last Modified On : Thu Feb 14 17:04:31 2019 |
---|
| 13 | // Update Count : 23 |
---|
[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 { |
---|
[cdcddfe1] | 30 | const Cost Cost::zero = Cost{ 0, 0, 0, 0, 0, 0, 0 }; |
---|
| 31 | const Cost Cost::infinity = Cost{ -1, -1, -1, -1, -1, 1, -1 }; |
---|
| 32 | const Cost Cost::unsafe = Cost{ 1, 0, 0, 0, 0, 0, 0 }; |
---|
| 33 | const Cost Cost::poly = Cost{ 0, 1, 0, 0, 0, 0, 0 }; |
---|
| 34 | const Cost Cost::safe = Cost{ 0, 0, 1, 0, 0, 0, 0 }; |
---|
| 35 | const Cost Cost::sign = Cost{ 0, 0, 0, 1, 0, 0, 0 }; |
---|
| 36 | const Cost Cost::var = Cost{ 0, 0, 0, 0, 1, 0, 0 }; |
---|
| 37 | const Cost Cost::spec = Cost{ 0, 0, 0, 0, 0, -1, 0 }; |
---|
| 38 | const Cost Cost::reference = Cost{ 0, 0, 0, 0, 0, 0, 1 }; |
---|
[b46e3bd] | 39 | |
---|
[5ccb10d] | 40 | #if 0 |
---|
| 41 | #define PRINT(x) x |
---|
| 42 | #else |
---|
| 43 | #define PRINT(x) |
---|
| 44 | #endif |
---|
[ada4575] | 45 | |
---|
[a32b204] | 46 | Cost conversionCost( Type *src, Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ) { |
---|
| 47 | if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) { |
---|
[eb0aedb] | 48 | PRINT( std::cerr << "type inst " << destAsTypeInst->name; ) |
---|
[00ac42e] | 49 | if ( const EqvClass* eqvClass = env.lookup( destAsTypeInst->name ) ) { |
---|
| 50 | if ( eqvClass->type ) { |
---|
| 51 | return conversionCost( src, eqvClass->type, indexer, env ); |
---|
[0b150ec] | 52 | } else { |
---|
| 53 | return Cost::infinity; |
---|
| 54 | } |
---|
[00ac42e] | 55 | } else if ( NamedTypeDecl *namedType = indexer.lookupType( destAsTypeInst->name ) ) { |
---|
[5ccb10d] | 56 | PRINT( std::cerr << " found" << std::endl; ) |
---|
[a32b204] | 57 | TypeDecl *type = dynamic_cast< TypeDecl* >( namedType ); |
---|
| 58 | // all typedefs should be gone by this point |
---|
| 59 | assert( type ); |
---|
[eb0aedb] | 60 | if ( type->base ) { |
---|
| 61 | return conversionCost( src, type->base, indexer, env ) + Cost::safe; |
---|
[a32b204] | 62 | } // if |
---|
| 63 | } // if |
---|
[5ccb10d] | 64 | PRINT( std::cerr << " not found" << std::endl; ) |
---|
[a32b204] | 65 | } // if |
---|
[5ccb10d] | 66 | PRINT( |
---|
| 67 | std::cerr << "src is "; |
---|
| 68 | src->print( std::cerr ); |
---|
| 69 | std::cerr << std::endl << "dest is "; |
---|
| 70 | dest->print( std::cerr ); |
---|
| 71 | std::cerr << std::endl << "env is" << std::endl; |
---|
| 72 | env.print( std::cerr, 8 ); |
---|
| 73 | ) |
---|
[a32b204] | 74 | if ( typesCompatibleIgnoreQualifiers( src, dest, indexer, env ) ) { |
---|
[5ccb10d] | 75 | PRINT( std::cerr << "compatible!" << std::endl; ) |
---|
[89be1c68] | 76 | return Cost::zero; |
---|
[a32b204] | 77 | } else if ( dynamic_cast< VoidType* >( dest ) ) { |
---|
[89be1c68] | 78 | return Cost::safe; |
---|
[2463d0e] | 79 | } else if ( ReferenceType * refType = dynamic_cast< ReferenceType * > ( dest ) ) { |
---|
[5ccb10d] | 80 | PRINT( std::cerr << "conversionCost: dest is reference" << std::endl; ) |
---|
[721cd19f] | 81 | return convertToReferenceCost( src, refType, indexer, env, [](Type * t1, Type * t2, const SymTab::Indexer &, const TypeEnvironment & env ){ |
---|
[0c6596f] | 82 | return ptrsAssignable( t1, t2, env ); |
---|
| 83 | }); |
---|
[a32b204] | 84 | } else { |
---|
[bd0b6b62] | 85 | PassVisitor<ConversionCost> converter( dest, indexer, env, conversionCost ); |
---|
[a32b204] | 86 | src->accept( converter ); |
---|
[bd0b6b62] | 87 | if ( converter.pass.get_cost() == Cost::infinity ) { |
---|
[a32b204] | 88 | return Cost::infinity; |
---|
| 89 | } else { |
---|
[bd0b6b62] | 90 | return converter.pass.get_cost() + Cost::zero; |
---|
[a32b204] | 91 | } // if |
---|
| 92 | } // if |
---|
| 93 | } |
---|
| 94 | |
---|
[0c6596f] | 95 | Cost convertToReferenceCost( Type * src, Type * dest, int diff, const SymTab::Indexer & indexer, const TypeEnvironment & env, PtrsFunction func ) { |
---|
[6e027d6] | 96 | PRINT( std::cerr << "convert to reference cost... diff " << diff << " " << src << " / " << dest << std::endl; ) |
---|
[89be1c68] | 97 | if ( diff > 0 ) { |
---|
| 98 | // TODO: document this |
---|
[eb0aedb] | 99 | Cost cost = convertToReferenceCost( strict_dynamic_cast< ReferenceType * >( src )->base, dest, diff-1, indexer, env, func ); |
---|
[7ebaa56] | 100 | cost.incReference(); |
---|
[89be1c68] | 101 | return cost; |
---|
| 102 | } else if ( diff < -1 ) { |
---|
| 103 | // TODO: document this |
---|
[eb0aedb] | 104 | Cost cost = convertToReferenceCost( src, strict_dynamic_cast< ReferenceType * >( dest )->base, diff+1, indexer, env, func ); |
---|
[7ebaa56] | 105 | cost.incReference(); |
---|
[89be1c68] | 106 | return cost; |
---|
| 107 | } else if ( diff == 0 ) { |
---|
| 108 | ReferenceType * srcAsRef = dynamic_cast< ReferenceType * >( src ); |
---|
| 109 | ReferenceType * destAsRef = dynamic_cast< ReferenceType * >( dest ); |
---|
| 110 | if ( srcAsRef && destAsRef ) { // pointer-like conversions between references |
---|
[5ccb10d] | 111 | PRINT( std::cerr << "converting between references" << std::endl; ) |
---|
[eb0aedb] | 112 | Type::Qualifiers tq1 = srcAsRef->base->get_qualifiers(); |
---|
| 113 | Type::Qualifiers tq2 = destAsRef->base->get_qualifiers(); |
---|
| 114 | if ( tq1 <= tq2 && typesCompatibleIgnoreQualifiers( srcAsRef->base, destAsRef->base, indexer, env ) ) { |
---|
[6e027d6] | 115 | PRINT( std::cerr << " :: compatible and good qualifiers" << std::endl; ) |
---|
| 116 | if ( tq1 == tq2 ) { |
---|
| 117 | // types are the same |
---|
| 118 | return Cost::zero; |
---|
| 119 | } else { |
---|
| 120 | // types are the same, except otherPointer has more qualifiers |
---|
| 121 | return Cost::safe; |
---|
| 122 | } |
---|
[89be1c68] | 123 | } else { // xxx - this discards reference qualifiers from consideration -- reducing qualifiers is a safe conversion; is this right? |
---|
[721cd19f] | 124 | int assignResult = func( srcAsRef->base, destAsRef->base, indexer, env ); |
---|
[5ccb10d] | 125 | PRINT( std::cerr << "comparing references: " << assignResult << " " << srcAsRef << " " << destAsRef << std::endl; ) |
---|
[b0837e4] | 126 | if ( assignResult > 0 ) { |
---|
[89be1c68] | 127 | return Cost::safe; |
---|
[b0837e4] | 128 | } else if ( assignResult < 0 ) { |
---|
[89be1c68] | 129 | return Cost::unsafe; |
---|
| 130 | } // if |
---|
[2463d0e] | 131 | } // if |
---|
| 132 | } else { |
---|
[5ccb10d] | 133 | PRINT( std::cerr << "reference to rvalue conversion" << std::endl; ) |
---|
[bd0b6b62] | 134 | PassVisitor<ConversionCost> converter( dest, indexer, env, conversionCost ); |
---|
[89be1c68] | 135 | src->accept( converter ); |
---|
[bd0b6b62] | 136 | return converter.pass.get_cost(); |
---|
[89be1c68] | 137 | } // if |
---|
[2463d0e] | 138 | } else { |
---|
[89be1c68] | 139 | ReferenceType * destAsRef = dynamic_cast< ReferenceType * >( dest ); |
---|
| 140 | assert( diff == -1 && destAsRef ); |
---|
[108f3cdb] | 141 | PRINT( std::cerr << "dest is: " << dest << " / src is: " << src << std::endl; ) |
---|
[eb0aedb] | 142 | if ( typesCompatibleIgnoreQualifiers( src, destAsRef->base, indexer, env ) ) { |
---|
[5ccb10d] | 143 | PRINT( std::cerr << "converting compatible base type" << std::endl; ) |
---|
[89be1c68] | 144 | if ( src->get_lvalue() ) { |
---|
[5ccb10d] | 145 | PRINT( |
---|
| 146 | std::cerr << "lvalue to reference conversion" << std::endl; |
---|
| 147 | std::cerr << src << " => " << destAsRef << std::endl; |
---|
| 148 | ) |
---|
[89be1c68] | 149 | // lvalue-to-reference conversion: cv lvalue T => cv T & |
---|
[eb0aedb] | 150 | if ( src->get_qualifiers() == destAsRef->base->get_qualifiers() ) { |
---|
[7ebaa56] | 151 | return Cost::reference; // cost needs to be non-zero to add cast |
---|
[eb0aedb] | 152 | } if ( src->get_qualifiers() < destAsRef->base->get_qualifiers() ) { |
---|
[7ebaa56] | 153 | return Cost::safe; // cost needs to be higher than previous cast to differentiate adding qualifiers vs. keeping same |
---|
[89be1c68] | 154 | } else { |
---|
| 155 | return Cost::unsafe; |
---|
| 156 | } // if |
---|
[eb0aedb] | 157 | } else if ( destAsRef->base->get_const() ) { |
---|
[5ccb10d] | 158 | PRINT( std::cerr << "rvalue to const ref conversion" << std::endl; ) |
---|
[89be1c68] | 159 | // rvalue-to-const-reference conversion: T => const T & |
---|
| 160 | return Cost::safe; |
---|
| 161 | } else { |
---|
[5ccb10d] | 162 | PRINT( std::cerr << "rvalue to non-const reference conversion" << std::endl; ) |
---|
[89be1c68] | 163 | // rvalue-to-reference conversion: T => T & |
---|
| 164 | return Cost::unsafe; |
---|
| 165 | } // if |
---|
| 166 | } // if |
---|
[5ccb10d] | 167 | PRINT( std::cerr << "attempting to convert from incompatible base type -- fail" << std::endl; ) |
---|
[2463d0e] | 168 | } |
---|
| 169 | return Cost::infinity; |
---|
| 170 | } |
---|
| 171 | |
---|
[0c6596f] | 172 | Cost convertToReferenceCost( Type * src, ReferenceType * dest, const SymTab::Indexer & indexer, const TypeEnvironment & env, PtrsFunction func ) { |
---|
[89be1c68] | 173 | int sdepth = src->referenceDepth(), ddepth = dest->referenceDepth(); |
---|
[eddb399] | 174 | Cost cost = convertToReferenceCost( src, dest, sdepth-ddepth, indexer, env, func ); |
---|
| 175 | PRINT( std::cerr << "convertToReferenceCost result: " << cost << std::endl; ) |
---|
| 176 | return cost; |
---|
[89be1c68] | 177 | } |
---|
| 178 | |
---|
[721cd19f] | 179 | ConversionCost::ConversionCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env, CostFunction costFunc ) |
---|
| 180 | : dest( dest ), indexer( indexer ), cost( Cost::infinity ), env( env ), costFunc( costFunc ) { |
---|
[a32b204] | 181 | } |
---|
[4ee3b0c1] | 182 | |
---|
[e15853c] | 183 | // GENERATED START, DO NOT EDIT |
---|
[6fd1955] | 184 | // GENERATED BY BasicTypes-gen.cc |
---|
[cdcddfe1] | 185 | /* EXTENDED INTEGRAL RANK HIERARCHY (root to leaves) |
---|
[e15853c] | 186 | _Bool |
---|
| 187 | char signed char unsigned char |
---|
| 188 | signed short int unsigned short int |
---|
| 189 | signed int unsigned int |
---|
| 190 | signed long int unsigned long int |
---|
| 191 | signed long long int unsigned long long int |
---|
| 192 | __int128 unsigned __int128 |
---|
| 193 | _Float16 _Float16 _Complex |
---|
| 194 | _Float32 _Float32 _Complex |
---|
| 195 | float float _Complex |
---|
| 196 | _Float32x _Float32x _Complex |
---|
| 197 | _Float64 _Float64 _Complex |
---|
| 198 | double double _Complex |
---|
| 199 | _Float64x _Float64x _Complex |
---|
| 200 | __float80 |
---|
| 201 | _Float128 _Float128 _Complex |
---|
| 202 | __float128 |
---|
| 203 | long double long double _Complex |
---|
| 204 | _Float128x _Float128x _Complex |
---|
[cdcddfe1] | 205 | */ |
---|
[e15853c] | 206 | // GENERATED END |
---|
[cdcddfe1] | 207 | |
---|
[e15853c] | 208 | // GENERATED START, DO NOT EDIT |
---|
[6fd1955] | 209 | // GENERATED BY BasicTypes-gen.cc |
---|
[cdcddfe1] | 210 | static const int costMatrix[BasicType::NUMBER_OF_BASIC_TYPES][BasicType::NUMBER_OF_BASIC_TYPES] = { // path length from root to node |
---|
[fd9ae1d] | 211 | /* B C SC UC SI SUI I UI LI LUI LLI LLUI IB UIB _FH _FH _F _FC F FC _FX _FXC FD _FDC D DC F80X_FDXC F80 _FB_FLDC FB LD LDC _FBX_FLDXC */ |
---|
| 212 | /* B*/ { 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 17, 16, 18, 17, }, |
---|
| 213 | /* C*/ { -1, 0, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 16, 15, 17, 16, }, |
---|
| 214 | /* SC*/ { -1, -1, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 16, 15, 17, 16, }, |
---|
| 215 | /* UC*/ { -1, -1, -1, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 16, 15, 17, 16, }, |
---|
| 216 | /* SI*/ { -1, -1, -1, -1, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 15, 14, 16, 15, }, |
---|
| 217 | /* SUI*/ { -1, -1, -1, -1, -1, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 15, 14, 16, 15, }, |
---|
| 218 | /* I*/ { -1, -1, -1, -1, -1, -1, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 14, 13, 15, 14, }, |
---|
| 219 | /* UI*/ { -1, -1, -1, -1, -1, -1, -1, 0, 1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 14, 13, 15, 14, }, |
---|
| 220 | /* LI*/ { -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 13, 12, 14, 13, }, |
---|
| 221 | /* LUI*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 1, 2, 2, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 13, 12, 14, 13, }, |
---|
| 222 | /* LLI*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 12, 11, 13, 12, }, |
---|
| 223 | /* LLUI*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 1, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 12, 11, 13, 12, }, |
---|
| 224 | /* IB*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 11, 10, 12, 11, }, |
---|
| 225 | /* UIB*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 11, 10, 12, 11, }, |
---|
| 226 | /* _FH*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 10, 9, 11, 10, }, |
---|
| 227 | /* _FH*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, 1, -1, 2, -1, 3, -1, 4, -1, 5, -1, 6, -1, -1, 7, -1, -1, 8, -1, 9, }, |
---|
| 228 | /* _F*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 9, 8, 10, 9, }, |
---|
| 229 | /* _FC*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, 1, -1, 2, -1, 3, -1, 4, -1, 5, -1, -1, 6, -1, -1, 7, -1, 8, }, |
---|
| 230 | /* F*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8, 7, 9, 8, }, |
---|
| 231 | /* FC*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, 1, -1, 2, -1, 3, -1, 4, -1, -1, 5, -1, -1, 6, -1, 7, }, |
---|
| 232 | /* _FX*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 7, 6, 8, 7, }, |
---|
| 233 | /* _FXC*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, 1, -1, 2, -1, 3, -1, -1, 4, -1, -1, 5, -1, 6, }, |
---|
| 234 | /* FD*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 6, 5, 7, 6, }, |
---|
| 235 | /* _FDC*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, 1, -1, 2, -1, -1, 3, -1, -1, 4, -1, 5, }, |
---|
| 236 | /* D*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 1, 2, 2, 3, 3, 4, 5, 4, 6, 5, }, |
---|
| 237 | /* DC*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, 1, -1, -1, 2, -1, -1, 3, -1, 4, }, |
---|
| 238 | /* F80X*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 1, 2, 2, 3, 4, 3, 5, 4, }, |
---|
| 239 | /* _FDXC*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, 1, -1, -1, 2, -1, 3, }, |
---|
| 240 | /* F80*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 0, 1, 2, 2, 3, 3, 4, 4, }, |
---|
| 241 | /* _FB*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 1, 2, 2, 3, 3, }, |
---|
| 242 | /* _FLDC*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, 1, -1, 2, }, |
---|
| 243 | /* FB*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 0, 1, 2, 2, 3, }, |
---|
| 244 | /* LD*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 1, 2, }, |
---|
| 245 | /* LDC*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, 1, }, |
---|
| 246 | /* _FBX*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, }, |
---|
| 247 | /*_FLDXC*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, }, |
---|
[e15853c] | 248 | }; // costMatrix |
---|
| 249 | // GENERATED END |
---|
[cdcddfe1] | 250 | static_assert( |
---|
| 251 | sizeof(costMatrix)/sizeof(costMatrix[0][0]) == BasicType::NUMBER_OF_BASIC_TYPES*BasicType::NUMBER_OF_BASIC_TYPES, |
---|
| 252 | "Missing row in the cost matrix" |
---|
| 253 | ); |
---|
| 254 | |
---|
[e15853c] | 255 | // GENERATED START, DO NOT EDIT |
---|
[6fd1955] | 256 | // GENERATED BY BasicTypes-gen.cc |
---|
[cdcddfe1] | 257 | static const int signMatrix[BasicType::NUMBER_OF_BASIC_TYPES][BasicType::NUMBER_OF_BASIC_TYPES] = { // number of sign changes in safe conversion |
---|
[fd9ae1d] | 258 | /* B C SC UC SI SUI I UI LI LUI LLI LLUI IB UIB _FH _FH _F _FC F FC _FX _FXC FD _FDC D DC F80X_FDXC F80 _FB_FLDC FB LD LDC _FBX_FLDXC */ |
---|
| 259 | /* B*/ { 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, |
---|
| 260 | /* C*/ { -1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, |
---|
| 261 | /* SC*/ { -1, -1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, |
---|
| 262 | /* UC*/ { -1, -1, -1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, |
---|
| 263 | /* SI*/ { -1, -1, -1, -1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, |
---|
| 264 | /* SUI*/ { -1, -1, -1, -1, -1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, |
---|
| 265 | /* I*/ { -1, -1, -1, -1, -1, -1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, |
---|
| 266 | /* UI*/ { -1, -1, -1, -1, -1, -1, -1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, |
---|
| 267 | /* LI*/ { -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, |
---|
| 268 | /* LUI*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, |
---|
| 269 | /* LLI*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, |
---|
| 270 | /* LLUI*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, |
---|
| 271 | /* IB*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, |
---|
| 272 | /* UIB*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, |
---|
| 273 | /* _FH*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, |
---|
| 274 | /* _FH*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, -1, 0, -1, -1, 0, -1, 0, }, |
---|
| 275 | /* _F*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, |
---|
| 276 | /* _FC*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, -1, 0, -1, -1, 0, -1, 0, }, |
---|
| 277 | /* F*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, |
---|
| 278 | /* FC*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, -1, 0, -1, -1, 0, -1, 0, }, |
---|
| 279 | /* _FX*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, |
---|
| 280 | /* _FXC*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, 0, -1, 0, -1, 0, -1, -1, 0, -1, -1, 0, -1, 0, }, |
---|
| 281 | /* FD*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, |
---|
| 282 | /* _FDC*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, 0, -1, 0, -1, -1, 0, -1, -1, 0, -1, 0, }, |
---|
| 283 | /* D*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, |
---|
| 284 | /* DC*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, 0, -1, -1, 0, -1, -1, 0, -1, 0, }, |
---|
| 285 | /* F80X*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, |
---|
| 286 | /* _FDXC*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, 0, -1, -1, 0, -1, 0, }, |
---|
| 287 | /* F80*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, |
---|
| 288 | /* _FB*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, }, |
---|
| 289 | /* _FLDC*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, 0, -1, 0, }, |
---|
| 290 | /* FB*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, }, |
---|
| 291 | /* LD*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, }, |
---|
| 292 | /* LDC*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, 0, }, |
---|
| 293 | /* _FBX*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, }, |
---|
| 294 | /*_FLDXC*/ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, }, |
---|
[e15853c] | 295 | }; // signMatrix |
---|
| 296 | // GENERATED END |
---|
[cdcddfe1] | 297 | static_assert( |
---|
| 298 | sizeof(signMatrix)/sizeof(signMatrix[0][0]) == BasicType::NUMBER_OF_BASIC_TYPES*BasicType::NUMBER_OF_BASIC_TYPES, |
---|
| 299 | "Missing row in the sign matrix" |
---|
| 300 | ); |
---|
[a32b204] | 301 | |
---|
[bd0b6b62] | 302 | void ConversionCost::postvisit( VoidType * ) { |
---|
[a32b204] | 303 | cost = Cost::infinity; |
---|
| 304 | } |
---|
| 305 | |
---|
[bd0b6b62] | 306 | void ConversionCost::postvisit(BasicType *basicType) { |
---|
[a32b204] | 307 | if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) { |
---|
| 308 | int tableResult = costMatrix[ basicType->get_kind() ][ destAsBasic->get_kind() ]; |
---|
| 309 | if ( tableResult == -1 ) { |
---|
[89be1c68] | 310 | cost = Cost::unsafe; |
---|
[a32b204] | 311 | } else { |
---|
[89be1c68] | 312 | cost = Cost::zero; |
---|
| 313 | cost.incSafe( tableResult ); |
---|
[cdcddfe1] | 314 | cost.incSign( signMatrix[ basicType->get_kind() ][ destAsBasic->get_kind() ] ); |
---|
[a32b204] | 315 | } // if |
---|
[a436947] | 316 | } else if ( dynamic_cast< EnumInstType *>( dest ) ) { |
---|
| 317 | // xxx - not positive this is correct, but appears to allow casting int => enum |
---|
[89be1c68] | 318 | cost = Cost::unsafe; |
---|
[89e6ffc] | 319 | } // if |
---|
[98278b3a] | 320 | // no cases for zero_t/one_t because it should not be possible to convert int, etc. to zero_t/one_t. |
---|
[a32b204] | 321 | } |
---|
| 322 | |
---|
[bd0b6b62] | 323 | void ConversionCost::postvisit( PointerType * pointerType ) { |
---|
[a32b204] | 324 | if ( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) { |
---|
[6e027d6] | 325 | PRINT( std::cerr << pointerType << " ===> " << destAsPtr << std::endl; ) |
---|
[eb0aedb] | 326 | Type::Qualifiers tq1 = pointerType->base->get_qualifiers(); |
---|
| 327 | Type::Qualifiers tq2 = destAsPtr->base->get_qualifiers(); |
---|
| 328 | if ( tq1 <= tq2 && typesCompatibleIgnoreQualifiers( pointerType->base, destAsPtr->base, indexer, env ) ) { |
---|
[6e027d6] | 329 | PRINT( std::cerr << " :: compatible and good qualifiers" << std::endl; ) |
---|
[cb43451] | 330 | if ( tq1 == tq2 ) { |
---|
| 331 | // types are the same |
---|
| 332 | cost = Cost::zero; |
---|
| 333 | } else { |
---|
| 334 | // types are the same, except otherPointer has more qualifiers |
---|
| 335 | cost = Cost::safe; |
---|
[cdcddfe1] | 336 | } // if |
---|
[b8b075cd] | 337 | } else { |
---|
[b0837e4] | 338 | int assignResult = ptrsAssignable( pointerType->base, destAsPtr->base, env ); |
---|
[5ccb10d] | 339 | PRINT( std::cerr << " :: " << assignResult << std::endl; ) |
---|
[b8b075cd] | 340 | if ( assignResult > 0 && tq1 <= tq2 ) { |
---|
| 341 | // 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? |
---|
| 342 | if ( tq1 == tq2 ) { |
---|
| 343 | cost = Cost::safe; |
---|
| 344 | } else if ( tq1 < tq2 ) { |
---|
| 345 | cost = Cost::safe+Cost::safe; |
---|
| 346 | } |
---|
[b0837e4] | 347 | } else if ( assignResult < 0 ) { |
---|
[89be1c68] | 348 | cost = Cost::unsafe; |
---|
[a32b204] | 349 | } // if |
---|
[cb43451] | 350 | // assignResult == 0 means Cost::Infinity |
---|
[a32b204] | 351 | } // if |
---|
[98278b3a] | 352 | // case case for zero_t because it should not be possible to convert pointers to zero_t. |
---|
[a32b204] | 353 | } // if |
---|
| 354 | } |
---|
| 355 | |
---|
[bd0b6b62] | 356 | void ConversionCost::postvisit( ArrayType * ) {} |
---|
[2463d0e] | 357 | |
---|
[bd0b6b62] | 358 | void ConversionCost::postvisit( ReferenceType * refType ) { |
---|
[2463d0e] | 359 | // Note: dest can never be a reference, since it would have been caught in an earlier check |
---|
| 360 | assert( ! dynamic_cast< ReferenceType * >( dest ) ); |
---|
| 361 | // convert reference to rvalue: cv T1 & => T2 |
---|
| 362 | // recursively compute conversion cost from T1 to T2. |
---|
| 363 | // cv can be safely dropped because of 'implicit dereference' behavior. |
---|
[721cd19f] | 364 | cost = costFunc( refType->base, dest, indexer, env ); |
---|
[150ec33] | 365 | if ( refType->base->get_qualifiers() == dest->get_qualifiers() ) { |
---|
[cb43451] | 366 | cost.incReference(); // prefer exact qualifiers |
---|
[150ec33] | 367 | } else if ( refType->base->get_qualifiers() < dest->get_qualifiers() ) { |
---|
[cb43451] | 368 | cost.incSafe(); // then gaining qualifiers |
---|
| 369 | } else { |
---|
| 370 | cost.incUnsafe(); // lose qualifiers as last resort |
---|
| 371 | } |
---|
[5ccb10d] | 372 | PRINT( std::cerr << refType << " ==> " << dest << " " << cost << std::endl; ) |
---|
[2463d0e] | 373 | } |
---|
| 374 | |
---|
[bd0b6b62] | 375 | void ConversionCost::postvisit( FunctionType * ) {} |
---|
[a32b204] | 376 | |
---|
[bd0b6b62] | 377 | void ConversionCost::postvisit( StructInstType * inst ) { |
---|
[a32b204] | 378 | if ( StructInstType *destAsInst = dynamic_cast< StructInstType* >( dest ) ) { |
---|
[150ec33] | 379 | if ( inst->name == destAsInst->name ) { |
---|
[a32b204] | 380 | cost = Cost::zero; |
---|
| 381 | } // if |
---|
| 382 | } // if |
---|
| 383 | } |
---|
| 384 | |
---|
[bd0b6b62] | 385 | void ConversionCost::postvisit( UnionInstType * inst ) { |
---|
[150ec33] | 386 | if ( UnionInstType *destAsInst = dynamic_cast< UnionInstType* >( dest ) ) { |
---|
| 387 | if ( inst->name == destAsInst->name ) { |
---|
[a32b204] | 388 | cost = Cost::zero; |
---|
| 389 | } // if |
---|
| 390 | } // if |
---|
| 391 | } |
---|
| 392 | |
---|
[bd0b6b62] | 393 | void ConversionCost::postvisit( EnumInstType * ) { |
---|
[a32b204] | 394 | static Type::Qualifiers q; |
---|
| 395 | static BasicType integer( q, BasicType::SignedInt ); |
---|
[721cd19f] | 396 | cost = costFunc( &integer, dest, indexer, env ); // safe if dest >= int |
---|
[89be1c68] | 397 | if ( cost < Cost::unsafe ) { |
---|
[a32b204] | 398 | cost.incSafe(); |
---|
| 399 | } // if |
---|
| 400 | } |
---|
| 401 | |
---|
[bd0b6b62] | 402 | void ConversionCost::postvisit( TraitInstType * ) {} |
---|
[a32b204] | 403 | |
---|
[bd0b6b62] | 404 | void ConversionCost::postvisit( TypeInstType *inst ) { |
---|
[00ac42e] | 405 | if ( const EqvClass *eqvClass = env.lookup( inst->name ) ) { |
---|
| 406 | cost = costFunc( eqvClass->type, dest, indexer, env ); |
---|
[a32b204] | 407 | } else if ( TypeInstType *destAsInst = dynamic_cast< TypeInstType* >( dest ) ) { |
---|
[eb0aedb] | 408 | if ( inst->name == destAsInst->name ) { |
---|
[a32b204] | 409 | cost = Cost::zero; |
---|
| 410 | } |
---|
[00ac42e] | 411 | } else if ( NamedTypeDecl *namedType = indexer.lookupType( inst->name ) ) { |
---|
[a32b204] | 412 | TypeDecl *type = dynamic_cast< TypeDecl* >( namedType ); |
---|
| 413 | // all typedefs should be gone by this point |
---|
| 414 | assert( type ); |
---|
[eb0aedb] | 415 | if ( type->base ) { |
---|
[721cd19f] | 416 | cost = costFunc( type->base, dest, indexer, env ) + Cost::safe; |
---|
[a32b204] | 417 | } // if |
---|
| 418 | } // if |
---|
| 419 | } |
---|
| 420 | |
---|
[bd0b6b62] | 421 | void ConversionCost::postvisit( TupleType * tupleType ) { |
---|
[89be1c68] | 422 | Cost c = Cost::zero; |
---|
[b0837e4] | 423 | if ( TupleType * destAsTuple = dynamic_cast< TupleType * >( dest ) ) { |
---|
[eb0aedb] | 424 | std::list< Type * >::const_iterator srcIt = tupleType->types.begin(); |
---|
| 425 | std::list< Type * >::const_iterator destIt = destAsTuple->types.begin(); |
---|
| 426 | while ( srcIt != tupleType->types.end() && destIt != destAsTuple->types.end() ) { |
---|
[721cd19f] | 427 | Cost newCost = costFunc( *srcIt++, *destIt++, indexer, env ); |
---|
[a32b204] | 428 | if ( newCost == Cost::infinity ) { |
---|
| 429 | return; |
---|
| 430 | } // if |
---|
| 431 | c += newCost; |
---|
| 432 | } // while |
---|
[eb0aedb] | 433 | if ( destIt != destAsTuple->types.end() ) { |
---|
[a32b204] | 434 | cost = Cost::infinity; |
---|
| 435 | } else { |
---|
| 436 | cost = c; |
---|
| 437 | } // if |
---|
| 438 | } // if |
---|
| 439 | } |
---|
[44b7088] | 440 | |
---|
[bd0b6b62] | 441 | void ConversionCost::postvisit( VarArgsType * ) { |
---|
[90c3b1c] | 442 | if ( dynamic_cast< VarArgsType* >( dest ) ) { |
---|
[44b7088] | 443 | cost = Cost::zero; |
---|
| 444 | } |
---|
| 445 | } |
---|
[89e6ffc] | 446 | |
---|
[bd0b6b62] | 447 | void ConversionCost::postvisit( ZeroType * ) { |
---|
[b0837e4] | 448 | if ( dynamic_cast< ZeroType * >( dest ) ) { |
---|
[89e6ffc] | 449 | cost = Cost::zero; |
---|
| 450 | } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) { |
---|
| 451 | // copied from visit(BasicType*) for signed int, but +1 for safe conversions |
---|
| 452 | int tableResult = costMatrix[ BasicType::SignedInt ][ destAsBasic->get_kind() ]; |
---|
| 453 | if ( tableResult == -1 ) { |
---|
[89be1c68] | 454 | cost = Cost::unsafe; |
---|
[89e6ffc] | 455 | } else { |
---|
[89be1c68] | 456 | cost = Cost::zero; |
---|
| 457 | cost.incSafe( tableResult + 1 ); |
---|
[cdcddfe1] | 458 | cost.incSign( signMatrix[ BasicType::SignedInt ][ destAsBasic->get_kind() ] ); |
---|
| 459 | } // if |
---|
[89e6ffc] | 460 | } else if ( dynamic_cast< PointerType* >( dest ) ) { |
---|
[89be1c68] | 461 | cost = Cost::safe; |
---|
[cdcddfe1] | 462 | } // if |
---|
[89e6ffc] | 463 | } |
---|
| 464 | |
---|
[bd0b6b62] | 465 | void ConversionCost::postvisit( OneType * ) { |
---|
[b0837e4] | 466 | if ( dynamic_cast< OneType * >( dest ) ) { |
---|
[89e6ffc] | 467 | cost = Cost::zero; |
---|
| 468 | } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) { |
---|
| 469 | // copied from visit(BasicType*) for signed int, but +1 for safe conversions |
---|
| 470 | int tableResult = costMatrix[ BasicType::SignedInt ][ destAsBasic->get_kind() ]; |
---|
| 471 | if ( tableResult == -1 ) { |
---|
[89be1c68] | 472 | cost = Cost::unsafe; |
---|
[89e6ffc] | 473 | } else { |
---|
[89be1c68] | 474 | cost = Cost::zero; |
---|
| 475 | cost.incSafe( tableResult + 1 ); |
---|
[cdcddfe1] | 476 | cost.incSign( signMatrix[ BasicType::SignedInt ][ destAsBasic->get_kind() ] ); |
---|
| 477 | } // if |
---|
| 478 | } // if |
---|
[89e6ffc] | 479 | } |
---|
[51b7345] | 480 | } // namespace ResolvExpr |
---|
[a32b204] | 481 | |
---|
| 482 | // Local Variables: // |
---|
| 483 | // tab-width: 4 // |
---|
| 484 | // mode: c++ // |
---|
| 485 | // compile-command: "make install" // |
---|
| 486 | // End: // |
---|