[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 | // |
---|
[0b150ec] | 7 | // CastCost.cc -- |
---|
[a32b204] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Sun May 17 06:57:43 2015 |
---|
[7d01cf44] | 11 | // Last Modified By : Andrew Beach |
---|
[cf32116] | 12 | // Last Modified On : Tue Oct 4 15:00:00 2019 |
---|
| 13 | // Update Count : 9 |
---|
[a32b204] | 14 | // |
---|
| 15 | |
---|
[5bf3976] | 16 | #include "CastCost.hpp" |
---|
| 17 | |
---|
[ea6332d] | 18 | #include <cassert> // for assert |
---|
| 19 | |
---|
[3c89751] | 20 | #include "AST/Print.hpp" |
---|
| 21 | #include "AST/SymbolTable.hpp" |
---|
| 22 | #include "AST/Type.hpp" |
---|
| 23 | #include "AST/TypeEnvironment.hpp" |
---|
[ea6332d] | 24 | #include "ConversionCost.h" // for ConversionCost |
---|
| 25 | #include "Cost.h" // for Cost, Cost::infinity |
---|
[5bf3976] | 26 | #include "ResolvExpr/ConversionCost.h" // for conversionCost |
---|
| 27 | #include "ResolvExpr/PtrsCastable.hpp" // for ptrsCastable |
---|
[ea6332d] | 28 | #include "ResolvExpr/TypeEnvironment.h" // for TypeEnvironment, EqvClass |
---|
[e563edf] | 29 | #include "ResolvExpr/typeops.h" // for ptrsCastable |
---|
| 30 | #include "ResolvExpr/Unify.h" // for typesCompatibleIgnoreQualifiers |
---|
[ea6332d] | 31 | #include "SymTab/Indexer.h" // for Indexer |
---|
| 32 | #include "SynTree/Declaration.h" // for TypeDecl, NamedTypeDecl |
---|
| 33 | #include "SynTree/Type.h" // for PointerType, Type, TypeInstType |
---|
[51b7345] | 34 | |
---|
[150ec33] | 35 | #if 0 |
---|
| 36 | #define PRINT(x) x |
---|
| 37 | #else |
---|
| 38 | #define PRINT(x) |
---|
| 39 | #endif |
---|
[51b7345] | 40 | |
---|
| 41 | namespace ResolvExpr { |
---|
[3c89751] | 42 | struct CastCost_old : public ConversionCost { |
---|
[a32b204] | 43 | public: |
---|
[7d01cf44] | 44 | CastCost_old( const Type * dest, bool srcIsLvalue, |
---|
| 45 | const SymTab::Indexer &indexer, const TypeEnvironment &env, CostFunction costFunc ); |
---|
[0b150ec] | 46 | |
---|
[bd0b6b62] | 47 | using ConversionCost::previsit; |
---|
| 48 | using ConversionCost::postvisit; |
---|
[7870799] | 49 | void postvisit( const BasicType * basicType ); |
---|
| 50 | void postvisit( const PointerType * pointerType ); |
---|
[a32b204] | 51 | }; |
---|
[51b7345] | 52 | |
---|
[7d01cf44] | 53 | Cost castCost( const Type * src, const Type * dest, bool srcIsLvalue, |
---|
| 54 | const SymTab::Indexer &indexer, const TypeEnvironment &env ) { |
---|
[ef5b828] | 55 | if ( const TypeInstType * destAsTypeInst = dynamic_cast< const TypeInstType * >( dest ) ) { |
---|
| 56 | if ( const EqvClass * eqvClass = env.lookup( destAsTypeInst->name ) ) { |
---|
[00ac42e] | 57 | if ( eqvClass->type ) { |
---|
[7d01cf44] | 58 | return castCost( src, eqvClass->type, srcIsLvalue, indexer, env ); |
---|
[0b150ec] | 59 | } else { |
---|
| 60 | return Cost::infinity; |
---|
| 61 | } |
---|
[ef5b828] | 62 | } else if ( const NamedTypeDecl * namedType = indexer.lookupType( destAsTypeInst->name ) ) { |
---|
[a32b204] | 63 | // all typedefs should be gone by this point |
---|
[ef5b828] | 64 | const TypeDecl * type = strict_dynamic_cast< const TypeDecl * >( namedType ); |
---|
[eb0aedb] | 65 | if ( type->base ) { |
---|
[7d01cf44] | 66 | return castCost( src, type->base, srcIsLvalue, indexer, env ) + Cost::safe; |
---|
[a32b204] | 67 | } // if |
---|
| 68 | } // if |
---|
| 69 | } // if |
---|
[150ec33] | 70 | |
---|
| 71 | PRINT( |
---|
| 72 | std::cerr << "castCost ::: src is "; |
---|
| 73 | src->print( std::cerr ); |
---|
| 74 | std::cerr << std::endl << "dest is "; |
---|
| 75 | dest->print( std::cerr ); |
---|
| 76 | std::cerr << std::endl << "env is" << std::endl; |
---|
| 77 | env.print( std::cerr, 8 ); |
---|
| 78 | ) |
---|
| 79 | |
---|
[a32b204] | 80 | if ( typesCompatibleIgnoreQualifiers( src, dest, indexer, env ) ) { |
---|
[150ec33] | 81 | PRINT( std::cerr << "compatible!" << std::endl; ) |
---|
[89be1c68] | 82 | return Cost::zero; |
---|
[ef5b828] | 83 | } else if ( dynamic_cast< const VoidType * >( dest ) ) { |
---|
[89be1c68] | 84 | return Cost::safe; |
---|
[7870799] | 85 | } else if ( const ReferenceType * refType = dynamic_cast< const ReferenceType * > ( dest ) ) { |
---|
[150ec33] | 86 | PRINT( std::cerr << "conversionCost: dest is reference" << std::endl; ) |
---|
[7d01cf44] | 87 | return convertToReferenceCost( src, refType, srcIsLvalue, indexer, env, [](const Type * t1, const Type * t2, const SymTab::Indexer & indexer, const TypeEnvironment & env ) { |
---|
[0c6596f] | 88 | return ptrsCastable( t1, t2, env, indexer ); |
---|
| 89 | }); |
---|
[a32b204] | 90 | } else { |
---|
[7870799] | 91 | PassVisitor<CastCost_old> converter( |
---|
[7d01cf44] | 92 | dest, srcIsLvalue, indexer, env, |
---|
| 93 | (Cost (*)( const Type *, const Type *, bool, const SymTab::Indexer &, const TypeEnvironment & )) |
---|
[c8e4d2f8] | 94 | castCost ); |
---|
[a32b204] | 95 | src->accept( converter ); |
---|
[bd0b6b62] | 96 | if ( converter.pass.get_cost() == Cost::infinity ) { |
---|
[a32b204] | 97 | return Cost::infinity; |
---|
| 98 | } else { |
---|
[1521de20] | 99 | // xxx - why are we adding cost 0 here? |
---|
[bd0b6b62] | 100 | return converter.pass.get_cost() + Cost::zero; |
---|
[a32b204] | 101 | } // if |
---|
[c11e31c] | 102 | } // if |
---|
[a32b204] | 103 | } |
---|
[51b7345] | 104 | |
---|
[7d01cf44] | 105 | CastCost_old::CastCost_old( const Type * dest, bool srcIsLvalue, |
---|
| 106 | const SymTab::Indexer &indexer, const TypeEnvironment &env, CostFunction costFunc ) |
---|
| 107 | : ConversionCost( dest, srcIsLvalue, indexer, env, costFunc ) { |
---|
[a32b204] | 108 | } |
---|
[51b7345] | 109 | |
---|
[ef5b828] | 110 | void CastCost_old::postvisit( const BasicType * basicType ) { |
---|
| 111 | const PointerType * destAsPointer = dynamic_cast< const PointerType * >( dest ); |
---|
[543159b] | 112 | if ( destAsPointer && basicType->isInteger() ) { |
---|
[ef5b828] | 113 | // necessary for, e.g. unsigned long => void * |
---|
[89be1c68] | 114 | cost = Cost::unsafe; |
---|
[a32b204] | 115 | } else { |
---|
[7d01cf44] | 116 | cost = conversionCost( basicType, dest, srcIsLvalue, indexer, env ); |
---|
[a32b204] | 117 | } // if |
---|
| 118 | } |
---|
[51b7345] | 119 | |
---|
[ef5b828] | 120 | void CastCost_old::postvisit( const PointerType * pointerType ) { |
---|
| 121 | if ( const PointerType * destAsPtr = dynamic_cast< const PointerType * >( dest ) ) { |
---|
[7870799] | 122 | if ( pointerType->tq <= destAsPtr->tq && typesCompatibleIgnoreQualifiers( pointerType->base, destAsPtr->base, indexer, env ) ) { |
---|
[89be1c68] | 123 | cost = Cost::safe; |
---|
[a32b204] | 124 | } else { |
---|
| 125 | TypeEnvironment newEnv( env ); |
---|
[eb0aedb] | 126 | newEnv.add( pointerType->forall ); |
---|
| 127 | newEnv.add( pointerType->base->forall ); |
---|
| 128 | int castResult = ptrsCastable( pointerType->base, destAsPtr->base, newEnv, indexer ); |
---|
[1521de20] | 129 | if ( castResult > 0 ) { |
---|
[89be1c68] | 130 | cost = Cost::safe; |
---|
[1521de20] | 131 | } else if ( castResult < 0 ) { |
---|
[52f85e0] | 132 | cost = Cost::infinity; |
---|
[a32b204] | 133 | } // if |
---|
| 134 | } // if |
---|
[7870799] | 135 | } else if ( const BasicType * destAsBasic = dynamic_cast< const BasicType * >( dest ) ) { |
---|
[543159b] | 136 | if ( destAsBasic->isInteger() ) { |
---|
[ef5b828] | 137 | // necessary for, e.g. void * => unsigned long |
---|
[89be1c68] | 138 | cost = Cost::unsafe; |
---|
[543159b] | 139 | } // if |
---|
| 140 | } |
---|
[a32b204] | 141 | } |
---|
[c8e4d2f8] | 142 | |
---|
[3c89751] | 143 | namespace { |
---|
| 144 | struct CastCost_new : public ConversionCost_new { |
---|
| 145 | using ConversionCost_new::previsit; |
---|
| 146 | using ConversionCost_new::postvisit; |
---|
| 147 | |
---|
[7870799] | 148 | CastCost_new( |
---|
[cf32116] | 149 | const ast::Type * dst, bool srcIsLvalue, const ast::SymbolTable & symtab, |
---|
[3c89751] | 150 | const ast::TypeEnvironment & env, CostCalculation costFunc ) |
---|
[cf32116] | 151 | : ConversionCost_new( dst, srcIsLvalue, symtab, env, costFunc ) {} |
---|
[3c89751] | 152 | |
---|
| 153 | void postvisit( const ast::BasicType * basicType ) { |
---|
| 154 | auto ptr = dynamic_cast< const ast::PointerType * >( dst ); |
---|
| 155 | if ( ptr && basicType->isInteger() ) { |
---|
| 156 | // needed for, e.g. unsigned long => void * |
---|
| 157 | cost = Cost::unsafe; |
---|
| 158 | } else { |
---|
[cf32116] | 159 | cost = conversionCost( basicType, dst, srcIsLvalue, symtab, env ); |
---|
[3c89751] | 160 | } |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | void postvisit( const ast::PointerType * pointerType ) { |
---|
| 164 | if ( auto ptr = dynamic_cast< const ast::PointerType * >( dst ) ) { |
---|
| 165 | if ( |
---|
| 166 | pointerType->qualifiers <= ptr->qualifiers |
---|
[251ce80] | 167 | && typesCompatibleIgnoreQualifiers( pointerType->base, ptr->base, env ) |
---|
[3c89751] | 168 | ) { |
---|
| 169 | cost = Cost::safe; |
---|
| 170 | } else { |
---|
| 171 | ast::TypeEnvironment newEnv{ env }; |
---|
[361bf01] | 172 | if ( auto wParams = pointerType->base.as< ast::FunctionType >() ) { |
---|
[3c89751] | 173 | newEnv.add( wParams->forall ); |
---|
| 174 | } |
---|
| 175 | int castResult = ptrsCastable( pointerType->base, ptr->base, symtab, newEnv ); |
---|
| 176 | if ( castResult > 0 ) { |
---|
| 177 | cost = Cost::safe; |
---|
| 178 | } else if ( castResult < 0 ) { |
---|
| 179 | cost = Cost::infinity; |
---|
| 180 | } |
---|
| 181 | } |
---|
| 182 | } else if ( auto basic = dynamic_cast< const ast::BasicType * >( dst ) ) { |
---|
| 183 | if ( basic->isInteger() ) { |
---|
| 184 | // necessary for, e.g. void * => unsigned long |
---|
| 185 | cost = Cost::unsafe; |
---|
| 186 | } |
---|
| 187 | } |
---|
| 188 | } |
---|
| 189 | }; |
---|
[cf32116] | 190 | |
---|
| 191 | #warning For overload resolution between the two versions. |
---|
| 192 | int localPtrsCastable(const ast::Type * t1, const ast::Type * t2, |
---|
| 193 | const ast::SymbolTable & symtab, const ast::TypeEnvironment & env ) { |
---|
| 194 | return ptrsCastable( t1, t2, symtab, env ); |
---|
| 195 | } |
---|
| 196 | Cost localCastCost( |
---|
| 197 | const ast::Type * src, const ast::Type * dst, bool srcIsLvalue, |
---|
| 198 | const ast::SymbolTable & symtab, const ast::TypeEnvironment & env |
---|
| 199 | ) { return castCost( src, dst, srcIsLvalue, symtab, env ); } |
---|
[3c89751] | 200 | } // anonymous namespace |
---|
| 201 | |
---|
[cf32116] | 202 | |
---|
| 203 | |
---|
[7870799] | 204 | Cost castCost( |
---|
[cf32116] | 205 | const ast::Type * src, const ast::Type * dst, bool srcIsLvalue, |
---|
| 206 | const ast::SymbolTable & symtab, const ast::TypeEnvironment & env |
---|
[3c89751] | 207 | ) { |
---|
| 208 | if ( auto typeInst = dynamic_cast< const ast::TypeInstType * >( dst ) ) { |
---|
[3e5dd913] | 209 | if ( const ast::EqvClass * eqvClass = env.lookup( *typeInst ) ) { |
---|
[3c89751] | 210 | // check cast cost against bound type, if present |
---|
| 211 | if ( eqvClass->bound ) { |
---|
[cf32116] | 212 | return castCost( src, eqvClass->bound, srcIsLvalue, symtab, env ); |
---|
[3c89751] | 213 | } else { |
---|
| 214 | return Cost::infinity; |
---|
| 215 | } |
---|
| 216 | } else if ( const ast::NamedTypeDecl * named = symtab.lookupType( typeInst->name ) ) { |
---|
| 217 | // all typedefs should be gone by now |
---|
| 218 | auto type = strict_dynamic_cast< const ast::TypeDecl * >( named ); |
---|
| 219 | if ( type->base ) { |
---|
[cf32116] | 220 | return castCost( src, type->base, srcIsLvalue, symtab, env ) + Cost::safe; |
---|
[3c89751] | 221 | } |
---|
| 222 | } |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | PRINT( |
---|
| 226 | std::cerr << "castCost ::: src is "; |
---|
| 227 | ast::print( std::cerr, src ); |
---|
| 228 | std::cerr << std::endl << "dest is "; |
---|
| 229 | ast::print( std::cerr, dst ); |
---|
| 230 | std::cerr << std::endl << "env is" << std::endl; |
---|
| 231 | ast::print( std::cerr, env, 2 ); |
---|
| 232 | ) |
---|
| 233 | |
---|
[251ce80] | 234 | if ( typesCompatibleIgnoreQualifiers( src, dst, env ) ) { |
---|
[3c89751] | 235 | PRINT( std::cerr << "compatible!" << std::endl; ) |
---|
[46da46b] | 236 | if (dynamic_cast<const ast::ZeroType *>(dst) || dynamic_cast<const ast::OneType *>(dst)) { |
---|
| 237 | return Cost::spec; |
---|
| 238 | } |
---|
[c8e4d2f8] | 239 | return Cost::zero; |
---|
[3c89751] | 240 | } else if ( dynamic_cast< const ast::VoidType * >( dst ) ) { |
---|
| 241 | return Cost::safe; |
---|
| 242 | } else if ( auto refType = dynamic_cast< const ast::ReferenceType * >( dst ) ) { |
---|
| 243 | PRINT( std::cerr << "conversionCost: dest is reference" << std::endl; ) |
---|
| 244 | #warning cast on ptrsCastable artifact of having two functions, remove when port done |
---|
[7870799] | 245 | return convertToReferenceCost( |
---|
[cf32116] | 246 | src, refType, srcIsLvalue, symtab, env, localPtrsCastable ); |
---|
[3c89751] | 247 | } else { |
---|
| 248 | #warning cast on castCost artifact of having two functions, remove when port done |
---|
[cf32116] | 249 | ast::Pass< CastCost_new > converter( |
---|
| 250 | dst, srcIsLvalue, symtab, env, localCastCost ); |
---|
[3c89751] | 251 | src->accept( converter ); |
---|
[7ff3e522] | 252 | return converter.core.cost; |
---|
[c8e4d2f8] | 253 | } |
---|
[3c89751] | 254 | } |
---|
| 255 | |
---|
[51b7345] | 256 | } // namespace ResolvExpr |
---|
[a32b204] | 257 | |
---|
| 258 | // Local Variables: // |
---|
| 259 | // tab-width: 4 // |
---|
| 260 | // mode: c++ // |
---|
| 261 | // compile-command: "make install" // |
---|
| 262 | // End: // |
---|