[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 |
---|
[e563edf] | 28 | #include "ResolvExpr/Unify.h" // for typesCompatibleIgnoreQualifiers |
---|
[51b7345] | 29 | |
---|
[150ec33] | 30 | #if 0 |
---|
| 31 | #define PRINT(x) x |
---|
| 32 | #else |
---|
| 33 | #define PRINT(x) |
---|
| 34 | #endif |
---|
[51b7345] | 35 | |
---|
| 36 | namespace ResolvExpr { |
---|
[c8e4d2f8] | 37 | |
---|
[3c89751] | 38 | namespace { |
---|
[0bd3faf] | 39 | struct CastCost : public ConversionCost { |
---|
| 40 | using ConversionCost::previsit; |
---|
| 41 | using ConversionCost::postvisit; |
---|
[3c89751] | 42 | |
---|
[0bd3faf] | 43 | CastCost( |
---|
[cf32116] | 44 | const ast::Type * dst, bool srcIsLvalue, const ast::SymbolTable & symtab, |
---|
[3c89751] | 45 | const ast::TypeEnvironment & env, CostCalculation costFunc ) |
---|
[0bd3faf] | 46 | : ConversionCost( dst, srcIsLvalue, symtab, env, costFunc ) {} |
---|
[3c89751] | 47 | |
---|
| 48 | void postvisit( const ast::BasicType * basicType ) { |
---|
| 49 | auto ptr = dynamic_cast< const ast::PointerType * >( dst ); |
---|
| 50 | if ( ptr && basicType->isInteger() ) { |
---|
| 51 | // needed for, e.g. unsigned long => void * |
---|
| 52 | cost = Cost::unsafe; |
---|
| 53 | } else { |
---|
[cf32116] | 54 | cost = conversionCost( basicType, dst, srcIsLvalue, symtab, env ); |
---|
[eb7586e] | 55 | if ( Cost::unsafe < cost ) { |
---|
| 56 | if (auto enumInst = dynamic_cast<const ast::EnumInstType *>(dst)) { |
---|
| 57 | assert(enumInst->base->base); |
---|
| 58 | cost = Cost::unsafe; |
---|
| 59 | } |
---|
| 60 | } |
---|
| 61 | } |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | void postvisit( const ast::ZeroType * zero ) { |
---|
| 65 | // auto ptr = dynamic_cast< const ast::PointerType * >( dst ); |
---|
| 66 | // if ( ptr && basicType->isInteger() ) { |
---|
| 67 | // // needed for, e.g. unsigned long => void * |
---|
| 68 | // cost = Cost::unsafe; |
---|
| 69 | // } else { |
---|
| 70 | cost = conversionCost( zero, dst, srcIsLvalue, symtab, env ); |
---|
| 71 | if ( Cost::unsafe < cost ) { |
---|
| 72 | if (auto enumInst = dynamic_cast<const ast::EnumInstType *>(dst)) { |
---|
| 73 | assert(enumInst->base->base); |
---|
| 74 | cost = Cost::unsafe; |
---|
| 75 | } |
---|
[3c89751] | 76 | } |
---|
[eb7586e] | 77 | // } |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | void postvisit( const ast::OneType * one ) { |
---|
| 81 | // auto ptr = dynamic_cast< const ast::PointerType * >( dst ); |
---|
| 82 | // if ( ptr && basicType->isInteger() ) { |
---|
| 83 | // // needed for, e.g. unsigned long => void * |
---|
| 84 | // cost = Cost::unsafe; |
---|
| 85 | // } else { |
---|
| 86 | cost = conversionCost( one, dst, srcIsLvalue, symtab, env ); |
---|
| 87 | if ( Cost::unsafe < cost ) { |
---|
| 88 | if (auto enumInst = dynamic_cast<const ast::EnumInstType *>(dst)) { |
---|
| 89 | assert(enumInst->base->base); |
---|
| 90 | cost = Cost::unsafe; |
---|
| 91 | } |
---|
| 92 | } |
---|
| 93 | // } |
---|
[3c89751] | 94 | } |
---|
| 95 | |
---|
| 96 | void postvisit( const ast::PointerType * pointerType ) { |
---|
| 97 | if ( auto ptr = dynamic_cast< const ast::PointerType * >( dst ) ) { |
---|
| 98 | if ( |
---|
| 99 | pointerType->qualifiers <= ptr->qualifiers |
---|
[251ce80] | 100 | && typesCompatibleIgnoreQualifiers( pointerType->base, ptr->base, env ) |
---|
[3c89751] | 101 | ) { |
---|
| 102 | cost = Cost::safe; |
---|
| 103 | } else { |
---|
| 104 | ast::TypeEnvironment newEnv{ env }; |
---|
[361bf01] | 105 | if ( auto wParams = pointerType->base.as< ast::FunctionType >() ) { |
---|
[3c89751] | 106 | newEnv.add( wParams->forall ); |
---|
| 107 | } |
---|
| 108 | int castResult = ptrsCastable( pointerType->base, ptr->base, symtab, newEnv ); |
---|
| 109 | if ( castResult > 0 ) { |
---|
| 110 | cost = Cost::safe; |
---|
| 111 | } else if ( castResult < 0 ) { |
---|
| 112 | cost = Cost::infinity; |
---|
| 113 | } |
---|
| 114 | } |
---|
| 115 | } else if ( auto basic = dynamic_cast< const ast::BasicType * >( dst ) ) { |
---|
| 116 | if ( basic->isInteger() ) { |
---|
| 117 | // necessary for, e.g. void * => unsigned long |
---|
| 118 | cost = Cost::unsafe; |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | } |
---|
[eb7586e] | 122 | |
---|
| 123 | void postvist( const ast::EnumInstType * ) { |
---|
| 124 | if ( auto basic = dynamic_cast< const ast::BasicType * >(dst) ) { |
---|
| 125 | if ( basic->isInteger() ) cost = Cost::unsafe; |
---|
| 126 | } |
---|
| 127 | } |
---|
[3c89751] | 128 | }; |
---|
[cf32116] | 129 | |
---|
[3c89751] | 130 | } // anonymous namespace |
---|
| 131 | |
---|
[7870799] | 132 | Cost castCost( |
---|
[cf32116] | 133 | const ast::Type * src, const ast::Type * dst, bool srcIsLvalue, |
---|
| 134 | const ast::SymbolTable & symtab, const ast::TypeEnvironment & env |
---|
[3c89751] | 135 | ) { |
---|
| 136 | if ( auto typeInst = dynamic_cast< const ast::TypeInstType * >( dst ) ) { |
---|
[3e5dd913] | 137 | if ( const ast::EqvClass * eqvClass = env.lookup( *typeInst ) ) { |
---|
[3c89751] | 138 | // check cast cost against bound type, if present |
---|
| 139 | if ( eqvClass->bound ) { |
---|
[cf32116] | 140 | return castCost( src, eqvClass->bound, srcIsLvalue, symtab, env ); |
---|
[3c89751] | 141 | } else { |
---|
| 142 | return Cost::infinity; |
---|
| 143 | } |
---|
| 144 | } else if ( const ast::NamedTypeDecl * named = symtab.lookupType( typeInst->name ) ) { |
---|
| 145 | // all typedefs should be gone by now |
---|
| 146 | auto type = strict_dynamic_cast< const ast::TypeDecl * >( named ); |
---|
| 147 | if ( type->base ) { |
---|
[cf32116] | 148 | return castCost( src, type->base, srcIsLvalue, symtab, env ) + Cost::safe; |
---|
[3c89751] | 149 | } |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | PRINT( |
---|
| 154 | std::cerr << "castCost ::: src is "; |
---|
| 155 | ast::print( std::cerr, src ); |
---|
| 156 | std::cerr << std::endl << "dest is "; |
---|
| 157 | ast::print( std::cerr, dst ); |
---|
| 158 | std::cerr << std::endl << "env is" << std::endl; |
---|
| 159 | ast::print( std::cerr, env, 2 ); |
---|
| 160 | ) |
---|
| 161 | |
---|
[251ce80] | 162 | if ( typesCompatibleIgnoreQualifiers( src, dst, env ) ) { |
---|
[3c89751] | 163 | PRINT( std::cerr << "compatible!" << std::endl; ) |
---|
[46da46b] | 164 | if (dynamic_cast<const ast::ZeroType *>(dst) || dynamic_cast<const ast::OneType *>(dst)) { |
---|
| 165 | return Cost::spec; |
---|
| 166 | } |
---|
[c8e4d2f8] | 167 | return Cost::zero; |
---|
[3c89751] | 168 | } else if ( dynamic_cast< const ast::VoidType * >( dst ) ) { |
---|
| 169 | return Cost::safe; |
---|
| 170 | } else if ( auto refType = dynamic_cast< const ast::ReferenceType * >( dst ) ) { |
---|
| 171 | PRINT( std::cerr << "conversionCost: dest is reference" << std::endl; ) |
---|
[7870799] | 172 | return convertToReferenceCost( |
---|
[0bd3faf] | 173 | src, refType, srcIsLvalue, symtab, env, ptrsCastable ); |
---|
[3c89751] | 174 | } else { |
---|
[0bd3faf] | 175 | ast::Pass< CastCost > converter( |
---|
| 176 | dst, srcIsLvalue, symtab, env, castCost ); |
---|
[3c89751] | 177 | src->accept( converter ); |
---|
[7ff3e522] | 178 | return converter.core.cost; |
---|
[c8e4d2f8] | 179 | } |
---|
[3c89751] | 180 | } |
---|
| 181 | |
---|
[51b7345] | 182 | } // namespace ResolvExpr |
---|
[a32b204] | 183 | |
---|
| 184 | // Local Variables: // |
---|
| 185 | // tab-width: 4 // |
---|
| 186 | // mode: c++ // |
---|
| 187 | // compile-command: "make install" // |
---|
| 188 | // End: // |
---|