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