| [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 | // | 
|---|
|  | 7 | // CastCost.cc -- | 
|---|
|  | 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 |  | 
|---|
| [51b73452] | 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 ); | 
|---|
| [51b73452] | 28 |  | 
|---|
| [a32b204] | 29 | virtual void visit( BasicType *basicType ); | 
|---|
|  | 30 | virtual void visit( PointerType *pointerType ); | 
|---|
|  | 31 | }; | 
|---|
| [51b73452] | 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 ) ) { | 
|---|
|  | 38 | return castCost( src, eqvClass.type, indexer, env ); | 
|---|
|  | 39 | } else if ( ( namedType = indexer.lookupType( destAsTypeInst->get_name() ) ) ) { | 
|---|
|  | 40 | TypeDecl *type = dynamic_cast< TypeDecl* >( namedType ); | 
|---|
|  | 41 | // all typedefs should be gone by this point | 
|---|
|  | 42 | assert( type ); | 
|---|
|  | 43 | if ( type->get_base() ) { | 
|---|
|  | 44 | return castCost( src, type->get_base(), indexer, env ) + Cost( 0, 0, 1 ); | 
|---|
|  | 45 | } // if | 
|---|
|  | 46 | } // if | 
|---|
|  | 47 | } // if | 
|---|
|  | 48 | if ( typesCompatibleIgnoreQualifiers( src, dest, indexer, env ) ) { | 
|---|
|  | 49 | return Cost( 0, 0, 0 ); | 
|---|
|  | 50 | } else if ( dynamic_cast< VoidType* >( dest ) ) { | 
|---|
|  | 51 | return Cost( 0, 0, 1 ); | 
|---|
|  | 52 | } else { | 
|---|
|  | 53 | CastCost converter( dest, indexer, env ); | 
|---|
|  | 54 | src->accept( converter ); | 
|---|
|  | 55 | if ( converter.get_cost() == Cost::infinity ) { | 
|---|
|  | 56 | return Cost::infinity; | 
|---|
|  | 57 | } else { | 
|---|
| [1521de20] | 58 | // xxx - why are we adding cost 0 here? | 
|---|
| [a32b204] | 59 | return converter.get_cost() + Cost( 0, 0, 0 ); | 
|---|
|  | 60 | } // if | 
|---|
| [c11e31c] | 61 | } // if | 
|---|
| [a32b204] | 62 | } | 
|---|
| [51b73452] | 63 |  | 
|---|
| [a32b204] | 64 | CastCost::CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ) | 
|---|
|  | 65 | : ConversionCost( dest, indexer, env ) { | 
|---|
|  | 66 | } | 
|---|
| [51b73452] | 67 |  | 
|---|
| [a32b204] | 68 | void CastCost::visit( BasicType *basicType ) { | 
|---|
| [543159b] | 69 | PointerType *destAsPointer = dynamic_cast< PointerType* >( dest ); | 
|---|
|  | 70 | if ( destAsPointer && basicType->isInteger() ) { | 
|---|
| [52f85e0] | 71 | //cost = Cost( 1, 0, 0 ); | 
|---|
|  | 72 | cost = Cost::infinity; | 
|---|
| [a32b204] | 73 | } else { | 
|---|
|  | 74 | ConversionCost::visit( basicType ); | 
|---|
|  | 75 | } // if | 
|---|
|  | 76 | } | 
|---|
| [51b73452] | 77 |  | 
|---|
| [a32b204] | 78 | void CastCost::visit( PointerType *pointerType ) { | 
|---|
|  | 79 | if ( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) { | 
|---|
|  | 80 | if ( pointerType->get_qualifiers() <= destAsPtr->get_qualifiers() && typesCompatibleIgnoreQualifiers( pointerType->get_base(), destAsPtr->get_base(), indexer, env ) ) { | 
|---|
|  | 81 | cost = Cost( 0, 0, 1 ); | 
|---|
|  | 82 | } else { | 
|---|
|  | 83 | TypeEnvironment newEnv( env ); | 
|---|
|  | 84 | newEnv.add( pointerType->get_forall() ); | 
|---|
|  | 85 | newEnv.add( pointerType->get_base()->get_forall() ); | 
|---|
| [1521de20] | 86 | int castResult = ptrsCastable( pointerType->get_base(), destAsPtr->get_base(), newEnv, indexer ); | 
|---|
|  | 87 | if ( castResult > 0 ) { | 
|---|
| [a32b204] | 88 | cost = Cost( 0, 0, 1 ); | 
|---|
| [1521de20] | 89 | } else if ( castResult < 0 ) { | 
|---|
| [52f85e0] | 90 | cost = Cost::infinity; | 
|---|
|  | 91 | //cost = Cost( 1, 0, 0 ); | 
|---|
| [a32b204] | 92 | } // if | 
|---|
|  | 93 | } // if | 
|---|
| [543159b] | 94 | } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) { | 
|---|
|  | 95 | if ( destAsBasic->isInteger() ) { | 
|---|
| [52f85e0] | 96 | //cost = Cost( 1, 0, 0 ); | 
|---|
|  | 97 | cost = Cost::infinity; | 
|---|
| [543159b] | 98 | } // if | 
|---|
|  | 99 | } | 
|---|
| [a32b204] | 100 | } | 
|---|
| [51b73452] | 101 | } // namespace ResolvExpr | 
|---|
| [a32b204] | 102 |  | 
|---|
|  | 103 | // Local Variables: // | 
|---|
|  | 104 | // tab-width: 4 // | 
|---|
|  | 105 | // mode: c++ // | 
|---|
|  | 106 | // compile-command: "make install" // | 
|---|
|  | 107 | // End: // | 
|---|