| 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 : Tue Feb  2 15:34:36 2016 | 
|---|
| 13 | // Update Count     : 7 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 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 | 
|---|
| 25 |  | 
|---|
| 26 |  | 
|---|
| 27 | namespace ResolvExpr { | 
|---|
| 28 | class CastCost : public ConversionCost { | 
|---|
| 29 | public: | 
|---|
| 30 | CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ); | 
|---|
| 31 |  | 
|---|
| 32 | virtual void visit( BasicType *basicType ); | 
|---|
| 33 | virtual void visit( PointerType *pointerType ); | 
|---|
| 34 | }; | 
|---|
| 35 |  | 
|---|
| 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 ) ) { | 
|---|
| 41 | if ( eqvClass.type ) { | 
|---|
| 42 | return castCost( src, eqvClass.type, indexer, env ); | 
|---|
| 43 | } else { | 
|---|
| 44 | return Cost::infinity; | 
|---|
| 45 | } | 
|---|
| 46 | } else if ( ( namedType = indexer.lookupType( destAsTypeInst->get_name() ) ) ) { | 
|---|
| 47 | TypeDecl *type = dynamic_cast< TypeDecl* >( namedType ); | 
|---|
| 48 | // all typedefs should be gone by this point | 
|---|
| 49 | assert( type ); | 
|---|
| 50 | if ( type->get_base() ) { | 
|---|
| 51 | return castCost( src, type->get_base(), indexer, env ) + Cost::safe; | 
|---|
| 52 | } // if | 
|---|
| 53 | } // if | 
|---|
| 54 | } // if | 
|---|
| 55 | if ( typesCompatibleIgnoreQualifiers( src, dest, indexer, env ) ) { | 
|---|
| 56 | return Cost::zero; | 
|---|
| 57 | } else if ( dynamic_cast< VoidType* >( dest ) ) { | 
|---|
| 58 | return Cost::safe; | 
|---|
| 59 | } else if ( ReferenceType * refType = dynamic_cast< ReferenceType * > ( dest ) ) { | 
|---|
| 60 | return convertToReferenceCost( src, refType, indexer, env ); | 
|---|
| 61 | } else { | 
|---|
| 62 | CastCost converter( dest, indexer, env ); | 
|---|
| 63 | src->accept( converter ); | 
|---|
| 64 | if ( converter.get_cost() == Cost::infinity ) { | 
|---|
| 65 | return Cost::infinity; | 
|---|
| 66 | } else { | 
|---|
| 67 | // xxx - why are we adding cost 0 here? | 
|---|
| 68 | return converter.get_cost() + Cost::zero; | 
|---|
| 69 | } // if | 
|---|
| 70 | } // if | 
|---|
| 71 | } | 
|---|
| 72 |  | 
|---|
| 73 | CastCost::CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ) | 
|---|
| 74 | : ConversionCost( dest, indexer, env ) { | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 | void CastCost::visit( BasicType *basicType ) { | 
|---|
| 78 | PointerType *destAsPointer = dynamic_cast< PointerType* >( dest ); | 
|---|
| 79 | if ( destAsPointer && basicType->isInteger() ) { | 
|---|
| 80 | // necessary for, e.g. unsigned long => void* | 
|---|
| 81 | cost = Cost::unsafe; | 
|---|
| 82 | } else { | 
|---|
| 83 | cost = conversionCost( basicType, dest, indexer, env ); | 
|---|
| 84 | } // if | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | void CastCost::visit( PointerType *pointerType ) { | 
|---|
| 88 | if ( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) { | 
|---|
| 89 | if ( pointerType->get_qualifiers() <= destAsPtr->get_qualifiers() && typesCompatibleIgnoreQualifiers( pointerType->get_base(), destAsPtr->get_base(), indexer, env ) ) { | 
|---|
| 90 | cost = Cost::safe; | 
|---|
| 91 | } else { | 
|---|
| 92 | TypeEnvironment newEnv( env ); | 
|---|
| 93 | newEnv.add( pointerType->get_forall() ); | 
|---|
| 94 | newEnv.add( pointerType->get_base()->get_forall() ); | 
|---|
| 95 | int castResult = ptrsCastable( pointerType->get_base(), destAsPtr->get_base(), newEnv, indexer ); | 
|---|
| 96 | if ( castResult > 0 ) { | 
|---|
| 97 | cost = Cost::safe; | 
|---|
| 98 | } else if ( castResult < 0 ) { | 
|---|
| 99 | cost = Cost::infinity; | 
|---|
| 100 | } // if | 
|---|
| 101 | } // if | 
|---|
| 102 | } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) { | 
|---|
| 103 | if ( destAsBasic->isInteger() ) { | 
|---|
| 104 | // necessary for, e.g. void* => unsigned long | 
|---|
| 105 | cost = Cost::unsafe; | 
|---|
| 106 | } // if | 
|---|
| 107 | } | 
|---|
| 108 | } | 
|---|
| 109 | } // namespace ResolvExpr | 
|---|
| 110 |  | 
|---|
| 111 | // Local Variables: // | 
|---|
| 112 | // tab-width: 4 // | 
|---|
| 113 | // mode: c++ // | 
|---|
| 114 | // compile-command: "make install" // | 
|---|
| 115 | // End: // | 
|---|