[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 | |
---|
[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 ); |
---|
[51b7345] | 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 ) ) { |
---|
| 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 | } |
---|
[51b7345] | 63 | |
---|
[a32b204] | 64 | CastCost::CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ) |
---|
| 65 | : ConversionCost( dest, indexer, env ) { |
---|
| 66 | } |
---|
[51b7345] | 67 | |
---|
[a32b204] | 68 | void CastCost::visit( BasicType *basicType ) { |
---|
[543159b] | 69 | PointerType *destAsPointer = dynamic_cast< PointerType* >( dest ); |
---|
| 70 | if ( destAsPointer && basicType->isInteger() ) { |
---|
[9fd97126] | 71 | // necessary for, e.g. unsigned long => void* |
---|
| 72 | cost = Cost( 1, 0, 0 ); |
---|
[a32b204] | 73 | } else { |
---|
| 74 | ConversionCost::visit( basicType ); |
---|
| 75 | } // if |
---|
| 76 | } |
---|
[51b7345] | 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; |
---|
[a32b204] | 91 | } // if |
---|
| 92 | } // if |
---|
[543159b] | 93 | } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) { |
---|
| 94 | if ( destAsBasic->isInteger() ) { |
---|
[9fd97126] | 95 | // necessary for, e.g. void* => unsigned long |
---|
| 96 | cost = Cost( 1, 0, 0 ); |
---|
[543159b] | 97 | } // if |
---|
| 98 | } |
---|
[a32b204] | 99 | } |
---|
[51b7345] | 100 | } // namespace ResolvExpr |
---|
[a32b204] | 101 | |
---|
| 102 | // Local Variables: // |
---|
| 103 | // tab-width: 4 // |
---|
| 104 | // mode: c++ // |
---|
| 105 | // compile-command: "make install" // |
---|
| 106 | // End: // |
---|