[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() ) { |
---|
| 48 | return castCost( src, type->get_base(), indexer, env ) + Cost( 0, 0, 1 ); |
---|
| 49 | } // if |
---|
| 50 | } // if |
---|
| 51 | } // if |
---|
| 52 | if ( typesCompatibleIgnoreQualifiers( src, dest, indexer, env ) ) { |
---|
| 53 | return Cost( 0, 0, 0 ); |
---|
| 54 | } else if ( dynamic_cast< VoidType* >( dest ) ) { |
---|
| 55 | return Cost( 0, 0, 1 ); |
---|
| 56 | } else { |
---|
| 57 | CastCost converter( dest, indexer, env ); |
---|
| 58 | src->accept( converter ); |
---|
| 59 | if ( converter.get_cost() == Cost::infinity ) { |
---|
| 60 | return Cost::infinity; |
---|
| 61 | } else { |
---|
[1521de20] | 62 | // xxx - why are we adding cost 0 here? |
---|
[a32b204] | 63 | return converter.get_cost() + Cost( 0, 0, 0 ); |
---|
| 64 | } // if |
---|
[c11e31c] | 65 | } // if |
---|
[a32b204] | 66 | } |
---|
[51b7345] | 67 | |
---|
[a32b204] | 68 | CastCost::CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ) |
---|
| 69 | : ConversionCost( dest, indexer, env ) { |
---|
| 70 | } |
---|
[51b7345] | 71 | |
---|
[a32b204] | 72 | void CastCost::visit( BasicType *basicType ) { |
---|
[543159b] | 73 | PointerType *destAsPointer = dynamic_cast< PointerType* >( dest ); |
---|
| 74 | if ( destAsPointer && basicType->isInteger() ) { |
---|
[9fd97126] | 75 | // necessary for, e.g. unsigned long => void* |
---|
| 76 | cost = Cost( 1, 0, 0 ); |
---|
[a32b204] | 77 | } else { |
---|
| 78 | ConversionCost::visit( basicType ); |
---|
| 79 | } // if |
---|
| 80 | } |
---|
[51b7345] | 81 | |
---|
[a32b204] | 82 | void CastCost::visit( PointerType *pointerType ) { |
---|
| 83 | if ( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) { |
---|
| 84 | if ( pointerType->get_qualifiers() <= destAsPtr->get_qualifiers() && typesCompatibleIgnoreQualifiers( pointerType->get_base(), destAsPtr->get_base(), indexer, env ) ) { |
---|
| 85 | cost = Cost( 0, 0, 1 ); |
---|
| 86 | } else { |
---|
| 87 | TypeEnvironment newEnv( env ); |
---|
| 88 | newEnv.add( pointerType->get_forall() ); |
---|
| 89 | newEnv.add( pointerType->get_base()->get_forall() ); |
---|
[1521de20] | 90 | int castResult = ptrsCastable( pointerType->get_base(), destAsPtr->get_base(), newEnv, indexer ); |
---|
| 91 | if ( castResult > 0 ) { |
---|
[a32b204] | 92 | cost = Cost( 0, 0, 1 ); |
---|
[1521de20] | 93 | } else if ( castResult < 0 ) { |
---|
[52f85e0] | 94 | cost = Cost::infinity; |
---|
[a32b204] | 95 | } // if |
---|
| 96 | } // if |
---|
[543159b] | 97 | } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) { |
---|
| 98 | if ( destAsBasic->isInteger() ) { |
---|
[9fd97126] | 99 | // necessary for, e.g. void* => unsigned long |
---|
| 100 | cost = Cost( 1, 0, 0 ); |
---|
[543159b] | 101 | } // if |
---|
| 102 | } |
---|
[a32b204] | 103 | } |
---|
[51b7345] | 104 | } // namespace ResolvExpr |
---|
[a32b204] | 105 | |
---|
| 106 | // Local Variables: // |
---|
| 107 | // tab-width: 4 // |
---|
| 108 | // mode: c++ // |
---|
| 109 | // compile-command: "make install" // |
---|
| 110 | // End: // |
---|