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