[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 |
|
---|
[150ec33] | 26 | #if 0
|
---|
| 27 | #define PRINT(x) x
|
---|
| 28 | #else
|
---|
| 29 | #define PRINT(x)
|
---|
| 30 | #endif
|
---|
[51b73452] | 31 |
|
---|
| 32 | namespace ResolvExpr {
|
---|
[bd0b6b62] | 33 | struct CastCost : public ConversionCost {
|
---|
[a32b204] | 34 | public:
|
---|
[721cd19f] | 35 | CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env, CostFunction costFunc );
|
---|
[0b150ec] | 36 |
|
---|
[bd0b6b62] | 37 | using ConversionCost::previsit;
|
---|
| 38 | using ConversionCost::postvisit;
|
---|
| 39 | void postvisit( BasicType * basicType );
|
---|
| 40 | void postvisit( PointerType * pointerType );
|
---|
[a32b204] | 41 | };
|
---|
[51b73452] | 42 |
|
---|
[a32b204] | 43 | Cost castCost( Type *src, Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
|
---|
| 44 | if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) {
|
---|
[00ac42e] | 45 | if ( const EqvClass* eqvClass = env.lookup( destAsTypeInst->get_name() ) ) {
|
---|
| 46 | if ( eqvClass->type ) {
|
---|
| 47 | return castCost( src, eqvClass->type, indexer, env );
|
---|
[0b150ec] | 48 | } else {
|
---|
| 49 | return Cost::infinity;
|
---|
| 50 | }
|
---|
[00ac42e] | 51 | } else if ( NamedTypeDecl *namedType = indexer.lookupType( destAsTypeInst->get_name() ) ) {
|
---|
[a32b204] | 52 | // all typedefs should be gone by this point
|
---|
[982832e] | 53 | TypeDecl *type = strict_dynamic_cast< TypeDecl* >( namedType );
|
---|
[eb0aedb] | 54 | if ( type->base ) {
|
---|
| 55 | return castCost( src, type->base, indexer, env ) + Cost::safe;
|
---|
[a32b204] | 56 | } // if
|
---|
| 57 | } // if
|
---|
| 58 | } // if
|
---|
[150ec33] | 59 |
|
---|
| 60 | PRINT(
|
---|
| 61 | std::cerr << "castCost ::: src is ";
|
---|
| 62 | src->print( std::cerr );
|
---|
| 63 | std::cerr << std::endl << "dest is ";
|
---|
| 64 | dest->print( std::cerr );
|
---|
| 65 | std::cerr << std::endl << "env is" << std::endl;
|
---|
| 66 | env.print( std::cerr, 8 );
|
---|
| 67 | )
|
---|
| 68 |
|
---|
[a32b204] | 69 | if ( typesCompatibleIgnoreQualifiers( src, dest, indexer, env ) ) {
|
---|
[150ec33] | 70 | PRINT( std::cerr << "compatible!" << std::endl; )
|
---|
[89be1c68] | 71 | return Cost::zero;
|
---|
[a32b204] | 72 | } else if ( dynamic_cast< VoidType* >( dest ) ) {
|
---|
[89be1c68] | 73 | return Cost::safe;
|
---|
[2463d0e] | 74 | } else if ( ReferenceType * refType = dynamic_cast< ReferenceType * > ( dest ) ) {
|
---|
[150ec33] | 75 | PRINT( std::cerr << "conversionCost: dest is reference" << std::endl; )
|
---|
[721cd19f] | 76 | return convertToReferenceCost( src, refType, indexer, env, [](Type * t1, Type * t2, const SymTab::Indexer & indexer, const TypeEnvironment & env ) {
|
---|
[0c6596f] | 77 | return ptrsCastable( t1, t2, env, indexer );
|
---|
| 78 | });
|
---|
[a32b204] | 79 | } else {
|
---|
[bd0b6b62] | 80 | PassVisitor<CastCost> converter( dest, indexer, env, castCost );
|
---|
[a32b204] | 81 | src->accept( converter );
|
---|
[bd0b6b62] | 82 | if ( converter.pass.get_cost() == Cost::infinity ) {
|
---|
[a32b204] | 83 | return Cost::infinity;
|
---|
| 84 | } else {
|
---|
[1521de20] | 85 | // xxx - why are we adding cost 0 here?
|
---|
[bd0b6b62] | 86 | return converter.pass.get_cost() + Cost::zero;
|
---|
[a32b204] | 87 | } // if
|
---|
[c11e31c] | 88 | } // if
|
---|
[a32b204] | 89 | }
|
---|
[51b73452] | 90 |
|
---|
[721cd19f] | 91 | CastCost::CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env, CostFunction costFunc )
|
---|
| 92 | : ConversionCost( dest, indexer, env, costFunc ) {
|
---|
[a32b204] | 93 | }
|
---|
[51b73452] | 94 |
|
---|
[bd0b6b62] | 95 | void CastCost::postvisit( BasicType *basicType ) {
|
---|
[543159b] | 96 | PointerType *destAsPointer = dynamic_cast< PointerType* >( dest );
|
---|
| 97 | if ( destAsPointer && basicType->isInteger() ) {
|
---|
[9fd97126] | 98 | // necessary for, e.g. unsigned long => void*
|
---|
[89be1c68] | 99 | cost = Cost::unsafe;
|
---|
[a32b204] | 100 | } else {
|
---|
[2463d0e] | 101 | cost = conversionCost( basicType, dest, indexer, env );
|
---|
[a32b204] | 102 | } // if
|
---|
| 103 | }
|
---|
[51b73452] | 104 |
|
---|
[bd0b6b62] | 105 | void CastCost::postvisit( PointerType *pointerType ) {
|
---|
[a32b204] | 106 | if ( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) {
|
---|
[eb0aedb] | 107 | if ( pointerType->get_qualifiers() <= destAsPtr->get_qualifiers() && typesCompatibleIgnoreQualifiers( pointerType->base, destAsPtr->base, indexer, env ) ) {
|
---|
[89be1c68] | 108 | cost = Cost::safe;
|
---|
[a32b204] | 109 | } else {
|
---|
| 110 | TypeEnvironment newEnv( env );
|
---|
[eb0aedb] | 111 | newEnv.add( pointerType->forall );
|
---|
| 112 | newEnv.add( pointerType->base->forall );
|
---|
| 113 | int castResult = ptrsCastable( pointerType->base, destAsPtr->base, newEnv, indexer );
|
---|
[1521de20] | 114 | if ( castResult > 0 ) {
|
---|
[89be1c68] | 115 | cost = Cost::safe;
|
---|
[1521de20] | 116 | } else if ( castResult < 0 ) {
|
---|
[52f85e0] | 117 | cost = Cost::infinity;
|
---|
[a32b204] | 118 | } // if
|
---|
| 119 | } // if
|
---|
[543159b] | 120 | } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) {
|
---|
| 121 | if ( destAsBasic->isInteger() ) {
|
---|
[9fd97126] | 122 | // necessary for, e.g. void* => unsigned long
|
---|
[89be1c68] | 123 | cost = Cost::unsafe;
|
---|
[543159b] | 124 | } // if
|
---|
| 125 | }
|
---|
[a32b204] | 126 | }
|
---|
[51b73452] | 127 | } // namespace ResolvExpr
|
---|
[a32b204] | 128 |
|
---|
| 129 | // Local Variables: //
|
---|
| 130 | // tab-width: 4 //
|
---|
| 131 | // mode: c++ //
|
---|
| 132 | // compile-command: "make install" //
|
---|
| 133 | // End: //
|
---|