[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 |
---|
[52f85e0] | 11 | // Last Modified By : Peter A. Buhr |
---|
| 12 | // Last Modified On : Tue Feb 2 15:34:36 2016 |
---|
| 13 | // Update Count : 7 |
---|
[a32b204] | 14 | // |
---|
| 15 | |
---|
[ea6332d] | 16 | #include <cassert> // for assert |
---|
| 17 | |
---|
| 18 | #include "ConversionCost.h" // for ConversionCost |
---|
| 19 | #include "Cost.h" // for Cost, Cost::infinity |
---|
| 20 | #include "ResolvExpr/TypeEnvironment.h" // for TypeEnvironment, EqvClass |
---|
| 21 | #include "SymTab/Indexer.h" // for Indexer |
---|
| 22 | #include "SynTree/Declaration.h" // for TypeDecl, NamedTypeDecl |
---|
| 23 | #include "SynTree/Type.h" // for PointerType, Type, TypeInstType |
---|
| 24 | #include "typeops.h" // for typesCompatibleIgnoreQualifiers |
---|
[51b7345] | 25 | |
---|
| 26 | |
---|
| 27 | namespace ResolvExpr { |
---|
[a32b204] | 28 | class CastCost : public ConversionCost { |
---|
| 29 | public: |
---|
| 30 | CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ); |
---|
[0b150ec] | 31 | |
---|
[a32b204] | 32 | virtual void visit( BasicType *basicType ); |
---|
| 33 | virtual void visit( PointerType *pointerType ); |
---|
| 34 | }; |
---|
[51b7345] | 35 | |
---|
[a32b204] | 36 | Cost castCost( Type *src, Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ) { |
---|
| 37 | if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) { |
---|
| 38 | EqvClass eqvClass; |
---|
| 39 | NamedTypeDecl *namedType; |
---|
| 40 | if ( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) { |
---|
[0b150ec] | 41 | if ( eqvClass.type ) { |
---|
| 42 | return castCost( src, eqvClass.type, indexer, env ); |
---|
| 43 | } else { |
---|
| 44 | return Cost::infinity; |
---|
| 45 | } |
---|
[a32b204] | 46 | } else if ( ( namedType = indexer.lookupType( destAsTypeInst->get_name() ) ) ) { |
---|
| 47 | TypeDecl *type = dynamic_cast< TypeDecl* >( namedType ); |
---|
| 48 | // all typedefs should be gone by this point |
---|
| 49 | assert( type ); |
---|
| 50 | if ( type->get_base() ) { |
---|
[89be1c68] | 51 | return castCost( src, type->get_base(), indexer, env ) + Cost::safe; |
---|
[a32b204] | 52 | } // if |
---|
| 53 | } // if |
---|
| 54 | } // if |
---|
| 55 | if ( typesCompatibleIgnoreQualifiers( src, dest, indexer, env ) ) { |
---|
[89be1c68] | 56 | return Cost::zero; |
---|
[a32b204] | 57 | } else if ( dynamic_cast< VoidType* >( dest ) ) { |
---|
[89be1c68] | 58 | return Cost::safe; |
---|
[2463d0e] | 59 | } else if ( ReferenceType * refType = dynamic_cast< ReferenceType * > ( dest ) ) { |
---|
[0c6596f] | 60 | return convertToReferenceCost( src, refType, indexer, env, [](Type * t1, Type * t2, const TypeEnvironment & env, const SymTab::Indexer & indexer) { |
---|
| 61 | return ptrsCastable( t1, t2, env, indexer ); |
---|
| 62 | }); |
---|
[a32b204] | 63 | } else { |
---|
| 64 | CastCost converter( dest, indexer, env ); |
---|
| 65 | src->accept( converter ); |
---|
| 66 | if ( converter.get_cost() == Cost::infinity ) { |
---|
| 67 | return Cost::infinity; |
---|
| 68 | } else { |
---|
[1521de20] | 69 | // xxx - why are we adding cost 0 here? |
---|
[89be1c68] | 70 | return converter.get_cost() + Cost::zero; |
---|
[a32b204] | 71 | } // if |
---|
[c11e31c] | 72 | } // if |
---|
[a32b204] | 73 | } |
---|
[51b7345] | 74 | |
---|
[a32b204] | 75 | CastCost::CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ) |
---|
| 76 | : ConversionCost( dest, indexer, env ) { |
---|
| 77 | } |
---|
[51b7345] | 78 | |
---|
[a32b204] | 79 | void CastCost::visit( BasicType *basicType ) { |
---|
[543159b] | 80 | PointerType *destAsPointer = dynamic_cast< PointerType* >( dest ); |
---|
| 81 | if ( destAsPointer && basicType->isInteger() ) { |
---|
[9fd97126] | 82 | // necessary for, e.g. unsigned long => void* |
---|
[89be1c68] | 83 | cost = Cost::unsafe; |
---|
[a32b204] | 84 | } else { |
---|
[2463d0e] | 85 | cost = conversionCost( basicType, dest, indexer, env ); |
---|
[a32b204] | 86 | } // if |
---|
| 87 | } |
---|
[51b7345] | 88 | |
---|
[a32b204] | 89 | void CastCost::visit( PointerType *pointerType ) { |
---|
| 90 | if ( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) { |
---|
| 91 | if ( pointerType->get_qualifiers() <= destAsPtr->get_qualifiers() && typesCompatibleIgnoreQualifiers( pointerType->get_base(), destAsPtr->get_base(), indexer, env ) ) { |
---|
[89be1c68] | 92 | cost = Cost::safe; |
---|
[a32b204] | 93 | } else { |
---|
| 94 | TypeEnvironment newEnv( env ); |
---|
| 95 | newEnv.add( pointerType->get_forall() ); |
---|
| 96 | newEnv.add( pointerType->get_base()->get_forall() ); |
---|
[1521de20] | 97 | int castResult = ptrsCastable( pointerType->get_base(), destAsPtr->get_base(), newEnv, indexer ); |
---|
| 98 | if ( castResult > 0 ) { |
---|
[89be1c68] | 99 | cost = Cost::safe; |
---|
[1521de20] | 100 | } else if ( castResult < 0 ) { |
---|
[52f85e0] | 101 | cost = Cost::infinity; |
---|
[a32b204] | 102 | } // if |
---|
| 103 | } // if |
---|
[543159b] | 104 | } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) { |
---|
| 105 | if ( destAsBasic->isInteger() ) { |
---|
[9fd97126] | 106 | // necessary for, e.g. void* => unsigned long |
---|
[89be1c68] | 107 | cost = Cost::unsafe; |
---|
[543159b] | 108 | } // if |
---|
| 109 | } |
---|
[a32b204] | 110 | } |
---|
[51b7345] | 111 | } // namespace ResolvExpr |
---|
[a32b204] | 112 | |
---|
| 113 | // Local Variables: // |
---|
| 114 | // tab-width: 4 // |
---|
| 115 | // mode: c++ // |
---|
| 116 | // compile-command: "make install" // |
---|
| 117 | // End: // |
---|