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