[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 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
| 12 | // Last Modified On : Sun May 17 06:59:10 2015 |
---|
| 13 | // Update Count : 2 |
---|
| 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 { |
---|
| 58 | return converter.get_cost() + Cost( 0, 0, 0 ); |
---|
| 59 | } // if |
---|
[c11e31c] | 60 | } // if |
---|
[a32b204] | 61 | } |
---|
[51b7345] | 62 | |
---|
[a32b204] | 63 | CastCost::CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ) |
---|
| 64 | : ConversionCost( dest, indexer, env ) { |
---|
| 65 | } |
---|
[51b7345] | 66 | |
---|
[a32b204] | 67 | void CastCost::visit( BasicType *basicType ) { |
---|
| 68 | if ( dynamic_cast< PointerType* >( dest ) ) { |
---|
| 69 | cost = Cost( 1, 0, 0 ); |
---|
| 70 | } else { |
---|
| 71 | ConversionCost::visit( basicType ); |
---|
| 72 | } // if |
---|
| 73 | } |
---|
[51b7345] | 74 | |
---|
[a32b204] | 75 | void CastCost::visit( PointerType *pointerType ) { |
---|
| 76 | if ( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) { |
---|
| 77 | if ( pointerType->get_qualifiers() <= destAsPtr->get_qualifiers() && typesCompatibleIgnoreQualifiers( pointerType->get_base(), destAsPtr->get_base(), indexer, env ) ) { |
---|
| 78 | cost = Cost( 0, 0, 1 ); |
---|
| 79 | } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) { |
---|
| 80 | if ( destAsBasic->isInteger() ) { |
---|
| 81 | cost = Cost( 1, 0, 0 ); |
---|
| 82 | } // if |
---|
| 83 | } else { |
---|
| 84 | TypeEnvironment newEnv( env ); |
---|
| 85 | newEnv.add( pointerType->get_forall() ); |
---|
| 86 | newEnv.add( pointerType->get_base()->get_forall() ); |
---|
| 87 | int assignResult = ptrsCastable( pointerType->get_base(), destAsPtr->get_base(), newEnv, indexer ); |
---|
| 88 | if ( assignResult > 0 ) { |
---|
| 89 | cost = Cost( 0, 0, 1 ); |
---|
| 90 | } else if ( assignResult < 0 ) { |
---|
| 91 | cost = Cost( 1, 0, 0 ); |
---|
| 92 | } // if |
---|
| 93 | } // if |
---|
[c11e31c] | 94 | } // if |
---|
[a32b204] | 95 | } |
---|
[51b7345] | 96 | } // namespace ResolvExpr |
---|
[a32b204] | 97 | |
---|
| 98 | // Local Variables: // |
---|
| 99 | // tab-width: 4 // |
---|
| 100 | // mode: c++ // |
---|
| 101 | // compile-command: "make install" // |
---|
| 102 | // End: // |
---|