| [a32b204] | 1 | // | 
|---|
|  | 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo | 
|---|
|  | 3 | // | 
|---|
|  | 4 | // The contents of this file are covered under the licence agreement in the | 
|---|
|  | 5 | // file "LICENCE" distributed with Cforall. | 
|---|
|  | 6 | // | 
|---|
| [a436947] | 7 | // ConversionCost.cc -- | 
|---|
| [a32b204] | 8 | // | 
|---|
|  | 9 | // Author           : Richard C. Bilson | 
|---|
|  | 10 | // Created On       : Sun May 17 07:06:19 2015 | 
|---|
|  | 11 | // Last Modified By : Peter A. Buhr | 
|---|
| [4040425] | 12 | // Last Modified On : Wed Mar  2 17:35:46 2016 | 
|---|
|  | 13 | // Update Count     : 6 | 
|---|
| [a32b204] | 14 | // | 
|---|
| [51b73452] | 15 |  | 
|---|
|  | 16 | #include "ConversionCost.h" | 
|---|
|  | 17 | #include "typeops.h" | 
|---|
|  | 18 | #include "SynTree/Type.h" | 
|---|
|  | 19 | #include "SynTree/Visitor.h" | 
|---|
|  | 20 | #include "SymTab/Indexer.h" | 
|---|
|  | 21 |  | 
|---|
|  | 22 | namespace ResolvExpr { | 
|---|
| [a32b204] | 23 | const Cost Cost::zero = Cost( 0, 0, 0 ); | 
|---|
|  | 24 | const Cost Cost::infinity = Cost( -1, -1, -1 ); | 
|---|
| [51b73452] | 25 |  | 
|---|
| [a32b204] | 26 | Cost conversionCost( Type *src, Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ) { | 
|---|
|  | 27 | if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) { | 
|---|
|  | 28 | EqvClass eqvClass; | 
|---|
|  | 29 | NamedTypeDecl *namedType; | 
|---|
| [51b73452] | 30 | ///     std::cout << "type inst " << destAsTypeInst->get_name(); | 
|---|
| [a32b204] | 31 | if ( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) { | 
|---|
| [0b150ec] | 32 | if ( eqvClass.type ) { | 
|---|
|  | 33 | return conversionCost( src, eqvClass.type, indexer, env ); | 
|---|
|  | 34 | } else { | 
|---|
|  | 35 | return Cost::infinity; | 
|---|
|  | 36 | } | 
|---|
| [a32b204] | 37 | } else if ( ( namedType = indexer.lookupType( destAsTypeInst->get_name() ) ) ) { | 
|---|
| [51b73452] | 38 | ///       std::cout << " found" << std::endl; | 
|---|
| [a32b204] | 39 | TypeDecl *type = dynamic_cast< TypeDecl* >( namedType ); | 
|---|
|  | 40 | // all typedefs should be gone by this point | 
|---|
|  | 41 | assert( type ); | 
|---|
|  | 42 | if ( type->get_base() ) { | 
|---|
|  | 43 | return conversionCost( src, type->get_base(), indexer, env ) + Cost( 0, 0, 1 ); | 
|---|
|  | 44 | } // if | 
|---|
|  | 45 | } // if | 
|---|
| [51b73452] | 46 | ///     std::cout << " not found" << std::endl; | 
|---|
| [a32b204] | 47 | } // if | 
|---|
| [51b73452] | 48 | ///   std::cout << "src is "; | 
|---|
|  | 49 | ///   src->print( std::cout ); | 
|---|
|  | 50 | ///   std::cout << std::endl << "dest is "; | 
|---|
|  | 51 | ///   dest->print( std::cout ); | 
|---|
|  | 52 | ///   std::cout << std::endl << "env is" << std::endl; | 
|---|
|  | 53 | ///   env.print( std::cout, 8 ); | 
|---|
| [a32b204] | 54 | if ( typesCompatibleIgnoreQualifiers( src, dest, indexer, env ) ) { | 
|---|
| [51b73452] | 55 | ///     std::cout << "compatible!" << std::endl; | 
|---|
| [a32b204] | 56 | return Cost( 0, 0, 0 ); | 
|---|
|  | 57 | } else if ( dynamic_cast< VoidType* >( dest ) ) { | 
|---|
|  | 58 | return Cost( 0, 0, 1 ); | 
|---|
|  | 59 | } else { | 
|---|
|  | 60 | ConversionCost converter( dest, indexer, env ); | 
|---|
|  | 61 | src->accept( converter ); | 
|---|
|  | 62 | if ( converter.get_cost() == Cost::infinity ) { | 
|---|
|  | 63 | return Cost::infinity; | 
|---|
|  | 64 | } else { | 
|---|
|  | 65 | return converter.get_cost() + Cost( 0, 0, 0 ); | 
|---|
|  | 66 | } // if | 
|---|
|  | 67 | } // if | 
|---|
|  | 68 | } | 
|---|
|  | 69 |  | 
|---|
|  | 70 | ConversionCost::ConversionCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ) | 
|---|
|  | 71 | : dest( dest ), indexer( indexer ), cost( Cost::infinity ), env( env ) { | 
|---|
|  | 72 | } | 
|---|
| [51b73452] | 73 |  | 
|---|
|  | 74 | /* | 
|---|
| [a32b204] | 75 | Old | 
|---|
|  | 76 | === | 
|---|
|  | 77 | Double | 
|---|
|  | 78 | | | 
|---|
|  | 79 | Float | 
|---|
|  | 80 | | | 
|---|
|  | 81 | ULong | 
|---|
|  | 82 | /   \ | 
|---|
|  | 83 | UInt    Long | 
|---|
|  | 84 | \   / | 
|---|
|  | 85 | Int | 
|---|
|  | 86 | | | 
|---|
|  | 87 | Ushort | 
|---|
|  | 88 | | | 
|---|
|  | 89 | Short | 
|---|
|  | 90 | | | 
|---|
|  | 91 | Uchar | 
|---|
|  | 92 | /   \ | 
|---|
|  | 93 | Schar   Char | 
|---|
|  | 94 |  | 
|---|
|  | 95 | New | 
|---|
|  | 96 | === | 
|---|
|  | 97 | +-----LongDoubleComplex--+ | 
|---|
|  | 98 | LongDouble--+          |             +-LongDoubleImag | 
|---|
|  | 99 | |         +---DoubleComplex---+         | | 
|---|
|  | 100 | Double------+        |          +----DoubleImag | 
|---|
|  | 101 | |           +-FloatComplex-+            | | 
|---|
|  | 102 | Float---------+              +-------FloatImag | 
|---|
|  | 103 | | | 
|---|
|  | 104 | ULongLong | 
|---|
|  | 105 | | | 
|---|
|  | 106 | LongLong | 
|---|
|  | 107 | | | 
|---|
|  | 108 | ULong | 
|---|
|  | 109 | /   \ | 
|---|
|  | 110 | UInt    Long | 
|---|
|  | 111 | \   / | 
|---|
|  | 112 | Int | 
|---|
|  | 113 | | | 
|---|
|  | 114 | Ushort | 
|---|
|  | 115 | | | 
|---|
|  | 116 | Short | 
|---|
|  | 117 | | | 
|---|
|  | 118 | Uchar | 
|---|
|  | 119 | /   \ | 
|---|
|  | 120 | Schar   Char | 
|---|
|  | 121 | \   / | 
|---|
|  | 122 | Bool | 
|---|
| [51b73452] | 123 | */ | 
|---|
|  | 124 |  | 
|---|
| [a32b204] | 125 | static const int costMatrix[ BasicType::NUMBER_OF_BASIC_TYPES ][ BasicType::NUMBER_OF_BASIC_TYPES ] = | 
|---|
|  | 126 | { | 
|---|
|  | 127 | /* Src \ Dest:  Bool    Char    SChar   UChar   Short   UShort  Int     UInt    Long    ULong   LLong   ULLong  Float   Double  LDbl    FCplex  DCplex  LDCplex FImag   DImag   LDImag */ | 
|---|
|  | 128 | /* Bool */      { 0,    1,              1,              2,              3,              4,              5,              6,              6,              7,              8,              9,              10,             11,             12,             11,             12,             13,             -1,             -1,             -1 }, | 
|---|
|  | 129 | /* Char */      { -1,   0,              -1,             1,              2,              3,              4,              5,              5,              6,              7,              8,              9,              10,             11,             10,             11,             12,             -1,             -1,             -1 }, | 
|---|
|  | 130 | /* SChar */ { -1,       -1,             0,              1,              2,              3,              4,              5,              5,              6,              7,              8,              9,              10,             11,             10,             11,             12,             -1,             -1,             -1 }, | 
|---|
|  | 131 | /* UChar */ { -1,       -1,             -1,             0,              1,              2,              3,              4,              4,              5,              6,              7,              8,              9,              10,             9,              10,             11,             -1,             -1,             -1 }, | 
|---|
|  | 132 | /* Short */ { -1,       -1,             -1,             -1,             0,              1,              2,              3,              3,              4,              5,              6,              7,              8,              9,              8,              9,              10,             -1,             -1,             -1 }, | 
|---|
|  | 133 | /* UShort */{ -1,       -1,             -1,             -1,             -1,             0,              1,              2,              2,              3,              4,              5,              6,              7,              8,              7,              8,              9,              -1,             -1,             -1 }, | 
|---|
|  | 134 | /* Int */       { -1,   -1,             -1,             -1,             -1,             -1,             0,              1,              1,              2,              3,              4,              5,              6,              7,              6,              7,              8,              -1,             -1,             -1 }, | 
|---|
|  | 135 | /* UInt */      { -1,   -1,             -1,             -1,             -1,             -1,             -1,             0,              -1,             1,              2,              3,              4,              5,              6,              5,              6,              7,              -1,             -1,             -1 }, | 
|---|
|  | 136 | /* Long */      { -1,   -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              1,              2,              3,              4,              5,              6,              5,              6,              7,              -1,             -1,             -1 }, | 
|---|
|  | 137 | /* ULong */ { -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              1,              2,              3,              4,              5,              4,              5,              6,              -1,             -1,             -1 }, | 
|---|
|  | 138 | /* LLong */ { -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              1,              2,              3,              4,              3,              4,              5,              -1,             -1,             -1 }, | 
|---|
|  | 139 | /* ULLong */{ -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              1,              2,              3,              2,              3,              4,              -1,             -1,             -1 }, | 
|---|
|  | 140 | /* Float */ { -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              1,              2,              1,              2,              3,              -1,             -1,             -1 }, | 
|---|
|  | 141 | /* Double */{ -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              1,              -1,             1,              2,              -1,             -1,             -1 }, | 
|---|
|  | 142 | /* LDbl */      { -1,   -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              -1,             -1,             1,              -1,             -1,             -1 }, | 
|---|
|  | 143 | /* FCplex */{ -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              1,              2,              -1,             -1,             -1 }, | 
|---|
|  | 144 | /* DCplex */{ -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              1,              -1,             -1,             -1 }, | 
|---|
|  | 145 | /* LDCplex */{ -1,      -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              -1,             -1,             -1 }, | 
|---|
|  | 146 | /* FImag */ { -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             1,              2,              3,              0,              1,              2 }, | 
|---|
|  | 147 | /* DImag */ { -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             1,              2,              -1,             0,              1 }, | 
|---|
|  | 148 | /* LDImag */{ -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             1,              -1,             -1,             0 } | 
|---|
|  | 149 | }; | 
|---|
|  | 150 |  | 
|---|
| [7e003011] | 151 | void ConversionCost::visit( __attribute((unused)) VoidType *voidType ) { | 
|---|
| [a32b204] | 152 | cost = Cost::infinity; | 
|---|
|  | 153 | } | 
|---|
|  | 154 |  | 
|---|
|  | 155 | void ConversionCost::visit(BasicType *basicType) { | 
|---|
|  | 156 | if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) { | 
|---|
|  | 157 | int tableResult = costMatrix[ basicType->get_kind() ][ destAsBasic->get_kind() ]; | 
|---|
|  | 158 | if ( tableResult == -1 ) { | 
|---|
|  | 159 | cost = Cost( 1, 0, 0 ); | 
|---|
|  | 160 | } else { | 
|---|
|  | 161 | cost = Cost( 0, 0, tableResult ); | 
|---|
|  | 162 | } // if | 
|---|
| [a436947] | 163 | } else if ( dynamic_cast< EnumInstType *>( dest ) ) { | 
|---|
|  | 164 | // xxx - not positive this is correct, but appears to allow casting int => enum | 
|---|
|  | 165 | cost = Cost( 1, 0, 0 ); | 
|---|
| [89e6ffc] | 166 | } else if ( dynamic_cast< ZeroType* >( dest ) != nullptr || dynamic_cast< OneType* >( dest ) != nullptr ) { | 
|---|
|  | 167 | cost = Cost( 1, 0, 0 ); | 
|---|
|  | 168 | } // if | 
|---|
| [a32b204] | 169 | } | 
|---|
|  | 170 |  | 
|---|
|  | 171 | void ConversionCost::visit(PointerType *pointerType) { | 
|---|
|  | 172 | if ( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) { | 
|---|
|  | 173 | if ( pointerType->get_base()->get_qualifiers() <= destAsPtr->get_base()->get_qualifiers() && typesCompatibleIgnoreQualifiers( pointerType->get_base(), destAsPtr->get_base(), indexer, env ) ) { | 
|---|
|  | 174 | cost = Cost( 0, 0, 1 ); | 
|---|
|  | 175 | } else { | 
|---|
|  | 176 | int assignResult = ptrsAssignable( pointerType->get_base(), destAsPtr->get_base(), env ); | 
|---|
|  | 177 | if ( assignResult < 0 ) { | 
|---|
|  | 178 | cost = Cost( 0, 0, 1 ); | 
|---|
|  | 179 | } else if ( assignResult > 0 ) { | 
|---|
|  | 180 | cost = Cost( 1, 0, 0 ); | 
|---|
|  | 181 | } // if | 
|---|
|  | 182 | } // if | 
|---|
| [89e6ffc] | 183 | } else if ( dynamic_cast< ZeroType* >( dest ) != nullptr || dynamic_cast< OneType* >( dest ) != nullptr ) { | 
|---|
|  | 184 | cost = Cost( 1, 0, 0 ); | 
|---|
| [a32b204] | 185 | } // if | 
|---|
|  | 186 | } | 
|---|
|  | 187 |  | 
|---|
| [7e003011] | 188 | void ConversionCost::visit(__attribute((unused)) ArrayType *arrayType) {} | 
|---|
|  | 189 | void ConversionCost::visit(__attribute((unused)) FunctionType *functionType) {} | 
|---|
| [a32b204] | 190 |  | 
|---|
|  | 191 | void ConversionCost::visit(StructInstType *inst) { | 
|---|
|  | 192 | if ( StructInstType *destAsInst = dynamic_cast< StructInstType* >( dest ) ) { | 
|---|
|  | 193 | if ( inst->get_name() == destAsInst->get_name() ) { | 
|---|
|  | 194 | cost = Cost::zero; | 
|---|
|  | 195 | } // if | 
|---|
|  | 196 | } // if | 
|---|
|  | 197 | } | 
|---|
|  | 198 |  | 
|---|
|  | 199 | void ConversionCost::visit(UnionInstType *inst) { | 
|---|
|  | 200 | if ( StructInstType *destAsInst = dynamic_cast< StructInstType* >( dest ) ) { | 
|---|
|  | 201 | if ( inst->get_name() == destAsInst->get_name() ) { | 
|---|
|  | 202 | cost = Cost::zero; | 
|---|
|  | 203 | } // if | 
|---|
|  | 204 | } // if | 
|---|
|  | 205 | } | 
|---|
|  | 206 |  | 
|---|
| [7e003011] | 207 | void ConversionCost::visit( __attribute((unused)) EnumInstType *inst ) { | 
|---|
| [a32b204] | 208 | static Type::Qualifiers q; | 
|---|
|  | 209 | static BasicType integer( q, BasicType::SignedInt ); | 
|---|
|  | 210 | integer.accept( *this ); | 
|---|
|  | 211 | if ( cost < Cost( 1, 0, 0 ) ) { | 
|---|
|  | 212 | cost.incSafe(); | 
|---|
|  | 213 | } // if | 
|---|
|  | 214 | } | 
|---|
|  | 215 |  | 
|---|
| [7e003011] | 216 | void ConversionCost::visit( __attribute((unused)) TraitInstType *inst) { | 
|---|
| [a32b204] | 217 | } | 
|---|
|  | 218 |  | 
|---|
|  | 219 | void ConversionCost::visit(TypeInstType *inst) { | 
|---|
|  | 220 | EqvClass eqvClass; | 
|---|
|  | 221 | NamedTypeDecl *namedType; | 
|---|
|  | 222 | if ( env.lookup( inst->get_name(), eqvClass ) ) { | 
|---|
|  | 223 | cost = conversionCost( eqvClass.type, dest, indexer, env ); | 
|---|
|  | 224 | } else if ( TypeInstType *destAsInst = dynamic_cast< TypeInstType* >( dest ) ) { | 
|---|
|  | 225 | if ( inst->get_name() == destAsInst->get_name() ) { | 
|---|
|  | 226 | cost = Cost::zero; | 
|---|
|  | 227 | } | 
|---|
|  | 228 | } else if ( ( namedType = indexer.lookupType( inst->get_name() ) ) ) { | 
|---|
|  | 229 | TypeDecl *type = dynamic_cast< TypeDecl* >( namedType ); | 
|---|
|  | 230 | // all typedefs should be gone by this point | 
|---|
|  | 231 | assert( type ); | 
|---|
|  | 232 | if ( type->get_base() ) { | 
|---|
|  | 233 | cost = conversionCost( type->get_base(), dest, indexer, env ) + Cost( 0, 0, 1 ); | 
|---|
|  | 234 | } // if | 
|---|
|  | 235 | } // if | 
|---|
|  | 236 | } | 
|---|
|  | 237 |  | 
|---|
| [7e003011] | 238 | void ConversionCost::visit( __attribute((unused)) TupleType *tupleType) { | 
|---|
| [a32b204] | 239 | Cost c; | 
|---|
|  | 240 | if ( TupleType *destAsTuple = dynamic_cast< TupleType* >( dest ) ) { | 
|---|
|  | 241 | std::list< Type* >::const_iterator srcIt = tupleType->get_types().begin(); | 
|---|
|  | 242 | std::list< Type* >::const_iterator destIt = destAsTuple->get_types().begin(); | 
|---|
| [4ab9536] | 243 | while ( srcIt != tupleType->get_types().end() && destIt != destAsTuple->get_types().end() ) { | 
|---|
| [a32b204] | 244 | Cost newCost = conversionCost( *srcIt++, *destIt++, indexer, env ); | 
|---|
|  | 245 | if ( newCost == Cost::infinity ) { | 
|---|
|  | 246 | return; | 
|---|
|  | 247 | } // if | 
|---|
|  | 248 | c += newCost; | 
|---|
|  | 249 | } // while | 
|---|
|  | 250 | if ( destIt != destAsTuple->get_types().end() ) { | 
|---|
|  | 251 | cost = Cost::infinity; | 
|---|
|  | 252 | } else { | 
|---|
|  | 253 | cost = c; | 
|---|
|  | 254 | } // if | 
|---|
|  | 255 | } // if | 
|---|
|  | 256 | } | 
|---|
| [44b7088] | 257 |  | 
|---|
| [7e003011] | 258 | void ConversionCost::visit( __attribute((unused)) VarArgsType *varArgsType) { | 
|---|
| [90c3b1c] | 259 | if ( dynamic_cast< VarArgsType* >( dest ) ) { | 
|---|
| [44b7088] | 260 | cost = Cost::zero; | 
|---|
|  | 261 | } | 
|---|
|  | 262 | } | 
|---|
| [89e6ffc] | 263 |  | 
|---|
| [7e003011] | 264 | void ConversionCost::visit( __attribute((unused)) ZeroType *zeroType) { | 
|---|
| [89e6ffc] | 265 | if ( dynamic_cast< ZeroType* >( dest ) ) { | 
|---|
|  | 266 | cost = Cost::zero; | 
|---|
|  | 267 | } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) { | 
|---|
|  | 268 | // copied from visit(BasicType*) for signed int, but +1 for safe conversions | 
|---|
|  | 269 | int tableResult = costMatrix[ BasicType::SignedInt ][ destAsBasic->get_kind() ]; | 
|---|
|  | 270 | if ( tableResult == -1 ) { | 
|---|
|  | 271 | cost = Cost( 1, 0, 0 ); | 
|---|
|  | 272 | } else { | 
|---|
|  | 273 | cost = Cost( 0, 0, tableResult + 1 ); | 
|---|
|  | 274 | } | 
|---|
|  | 275 | } else if ( dynamic_cast< PointerType* >( dest ) ) { | 
|---|
|  | 276 | cost = Cost( 0, 0, 1 ); | 
|---|
|  | 277 | } | 
|---|
|  | 278 | } | 
|---|
|  | 279 |  | 
|---|
| [7e003011] | 280 | void ConversionCost::visit( __attribute((unused)) OneType *oneType) { | 
|---|
| [89e6ffc] | 281 | if ( dynamic_cast< OneType* >( dest ) ) { | 
|---|
|  | 282 | cost = Cost::zero; | 
|---|
|  | 283 | } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) { | 
|---|
|  | 284 | // copied from visit(BasicType*) for signed int, but +1 for safe conversions | 
|---|
|  | 285 | int tableResult = costMatrix[ BasicType::SignedInt ][ destAsBasic->get_kind() ]; | 
|---|
|  | 286 | if ( tableResult == -1 ) { | 
|---|
|  | 287 | cost = Cost( 1, 0, 0 ); | 
|---|
|  | 288 | } else { | 
|---|
|  | 289 | cost = Cost( 0, 0, tableResult + 1 ); | 
|---|
|  | 290 | } | 
|---|
|  | 291 | } | 
|---|
|  | 292 | } | 
|---|
| [51b73452] | 293 | } // namespace ResolvExpr | 
|---|
| [a32b204] | 294 |  | 
|---|
|  | 295 | // Local Variables: // | 
|---|
|  | 296 | // tab-width: 4 // | 
|---|
|  | 297 | // mode: c++ // | 
|---|
|  | 298 | // compile-command: "make install" // | 
|---|
|  | 299 | // End: // | 
|---|