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