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