[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 | // |
---|
| 7 | // ConversionCost.cc -- |
---|
| 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Sun May 17 07:06:19 2015 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
| 12 | // Last Modified On : Sun May 17 07:22:19 2015 |
---|
| 13 | // Update Count : 4 |
---|
| 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 |
---|
| 159 | } // if |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | void ConversionCost::visit(PointerType *pointerType) { |
---|
| 163 | if ( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) { |
---|
| 164 | if ( pointerType->get_base()->get_qualifiers() <= destAsPtr->get_base()->get_qualifiers() && typesCompatibleIgnoreQualifiers( pointerType->get_base(), destAsPtr->get_base(), indexer, env ) ) { |
---|
| 165 | cost = Cost( 0, 0, 1 ); |
---|
| 166 | } else { |
---|
| 167 | int assignResult = ptrsAssignable( pointerType->get_base(), destAsPtr->get_base(), env ); |
---|
| 168 | if ( assignResult < 0 ) { |
---|
| 169 | cost = Cost( 0, 0, 1 ); |
---|
| 170 | } else if ( assignResult > 0 ) { |
---|
| 171 | cost = Cost( 1, 0, 0 ); |
---|
| 172 | } // if |
---|
| 173 | } // if |
---|
| 174 | } // if |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | void ConversionCost::visit(ArrayType *arrayType) { |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | void ConversionCost::visit(FunctionType *functionType) { |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | void ConversionCost::visit(StructInstType *inst) { |
---|
| 184 | if ( StructInstType *destAsInst = dynamic_cast< StructInstType* >( dest ) ) { |
---|
| 185 | if ( inst->get_name() == destAsInst->get_name() ) { |
---|
| 186 | cost = Cost::zero; |
---|
| 187 | } // if |
---|
| 188 | } // if |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | void ConversionCost::visit(UnionInstType *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(EnumInstType *inst) { |
---|
| 200 | static Type::Qualifiers q; |
---|
| 201 | static BasicType integer( q, BasicType::SignedInt ); |
---|
| 202 | integer.accept( *this ); |
---|
| 203 | if ( cost < Cost( 1, 0, 0 ) ) { |
---|
| 204 | cost.incSafe(); |
---|
| 205 | } // if |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | void ConversionCost::visit(ContextInstType *inst) { |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | void ConversionCost::visit(TypeInstType *inst) { |
---|
| 212 | EqvClass eqvClass; |
---|
| 213 | NamedTypeDecl *namedType; |
---|
| 214 | if ( env.lookup( inst->get_name(), eqvClass ) ) { |
---|
| 215 | cost = conversionCost( eqvClass.type, dest, indexer, env ); |
---|
| 216 | } else if ( TypeInstType *destAsInst = dynamic_cast< TypeInstType* >( dest ) ) { |
---|
| 217 | if ( inst->get_name() == destAsInst->get_name() ) { |
---|
| 218 | cost = Cost::zero; |
---|
| 219 | } |
---|
| 220 | } else if ( ( namedType = indexer.lookupType( inst->get_name() ) ) ) { |
---|
| 221 | TypeDecl *type = dynamic_cast< TypeDecl* >( namedType ); |
---|
| 222 | // all typedefs should be gone by this point |
---|
| 223 | assert( type ); |
---|
| 224 | if ( type->get_base() ) { |
---|
| 225 | cost = conversionCost( type->get_base(), dest, indexer, env ) + Cost( 0, 0, 1 ); |
---|
| 226 | } // if |
---|
| 227 | } // if |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | void ConversionCost::visit(TupleType *tupleType) { |
---|
| 231 | Cost c; |
---|
| 232 | if ( TupleType *destAsTuple = dynamic_cast< TupleType* >( dest ) ) { |
---|
| 233 | std::list< Type* >::const_iterator srcIt = tupleType->get_types().begin(); |
---|
| 234 | std::list< Type* >::const_iterator destIt = destAsTuple->get_types().begin(); |
---|
| 235 | while ( srcIt != tupleType->get_types().end() ) { |
---|
| 236 | Cost newCost = conversionCost( *srcIt++, *destIt++, indexer, env ); |
---|
| 237 | if ( newCost == Cost::infinity ) { |
---|
| 238 | return; |
---|
| 239 | } // if |
---|
| 240 | c += newCost; |
---|
| 241 | } // while |
---|
| 242 | if ( destIt != destAsTuple->get_types().end() ) { |
---|
| 243 | cost = Cost::infinity; |
---|
| 244 | } else { |
---|
| 245 | cost = c; |
---|
| 246 | } // if |
---|
| 247 | } // if |
---|
| 248 | } |
---|
[44b7088] | 249 | |
---|
| 250 | void ConversionCost::visit(VarArgsType *varArgsType) { |
---|
| 251 | if ( VarArgsType *destAsVarArgs = dynamic_cast< VarArgsType* >( dest ) ) { |
---|
| 252 | cost = Cost::zero; |
---|
| 253 | } |
---|
| 254 | } |
---|
[51b7345] | 255 | } // namespace ResolvExpr |
---|
[a32b204] | 256 | |
---|
| 257 | // Local Variables: // |
---|
| 258 | // tab-width: 4 // |
---|
| 259 | // mode: c++ // |
---|
| 260 | // compile-command: "make install" // |
---|
| 261 | // End: // |
---|