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