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