[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 |
|
---|
[3c89751] | 18 | #include "AST/Print.hpp"
|
---|
| 19 | #include "AST/SymbolTable.hpp"
|
---|
| 20 | #include "AST/Type.hpp"
|
---|
| 21 | #include "AST/TypeEnvironment.hpp"
|
---|
[ea6332d] | 22 | #include "ConversionCost.h" // for ConversionCost
|
---|
| 23 | #include "Cost.h" // for Cost, Cost::infinity
|
---|
| 24 | #include "ResolvExpr/TypeEnvironment.h" // for TypeEnvironment, EqvClass
|
---|
| 25 | #include "SymTab/Indexer.h" // for Indexer
|
---|
| 26 | #include "SynTree/Declaration.h" // for TypeDecl, NamedTypeDecl
|
---|
| 27 | #include "SynTree/Type.h" // for PointerType, Type, TypeInstType
|
---|
| 28 | #include "typeops.h" // for typesCompatibleIgnoreQualifiers
|
---|
[51b73452] | 29 |
|
---|
[150ec33] | 30 | #if 0
|
---|
| 31 | #define PRINT(x) x
|
---|
| 32 | #else
|
---|
| 33 | #define PRINT(x)
|
---|
| 34 | #endif
|
---|
[51b73452] | 35 |
|
---|
| 36 | namespace ResolvExpr {
|
---|
[3c89751] | 37 | struct CastCost_old : public ConversionCost {
|
---|
[a32b204] | 38 | public:
|
---|
[ef5b828] | 39 | CastCost_old( const Type * dest, const SymTab::Indexer &indexer, const TypeEnvironment &env, CostFunction costFunc );
|
---|
[0b150ec] | 40 |
|
---|
[bd0b6b62] | 41 | using ConversionCost::previsit;
|
---|
| 42 | using ConversionCost::postvisit;
|
---|
[7870799] | 43 | void postvisit( const BasicType * basicType );
|
---|
| 44 | void postvisit( const PointerType * pointerType );
|
---|
[a32b204] | 45 | };
|
---|
[51b73452] | 46 |
|
---|
[ef5b828] | 47 | Cost castCost( const Type * src, const Type * dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
|
---|
| 48 | if ( const TypeInstType * destAsTypeInst = dynamic_cast< const TypeInstType * >( dest ) ) {
|
---|
| 49 | if ( const EqvClass * eqvClass = env.lookup( destAsTypeInst->name ) ) {
|
---|
[00ac42e] | 50 | if ( eqvClass->type ) {
|
---|
| 51 | return castCost( src, eqvClass->type, indexer, env );
|
---|
[0b150ec] | 52 | } else {
|
---|
| 53 | return Cost::infinity;
|
---|
| 54 | }
|
---|
[ef5b828] | 55 | } else if ( const NamedTypeDecl * namedType = indexer.lookupType( destAsTypeInst->name ) ) {
|
---|
[a32b204] | 56 | // all typedefs should be gone by this point
|
---|
[ef5b828] | 57 | const TypeDecl * type = strict_dynamic_cast< const TypeDecl * >( namedType );
|
---|
[eb0aedb] | 58 | if ( type->base ) {
|
---|
| 59 | return castCost( src, type->base, indexer, env ) + Cost::safe;
|
---|
[a32b204] | 60 | } // if
|
---|
| 61 | } // if
|
---|
| 62 | } // if
|
---|
[150ec33] | 63 |
|
---|
| 64 | PRINT(
|
---|
| 65 | std::cerr << "castCost ::: src is ";
|
---|
| 66 | src->print( std::cerr );
|
---|
| 67 | std::cerr << std::endl << "dest is ";
|
---|
| 68 | dest->print( std::cerr );
|
---|
| 69 | std::cerr << std::endl << "env is" << std::endl;
|
---|
| 70 | env.print( std::cerr, 8 );
|
---|
| 71 | )
|
---|
| 72 |
|
---|
[a32b204] | 73 | if ( typesCompatibleIgnoreQualifiers( src, dest, indexer, env ) ) {
|
---|
[150ec33] | 74 | PRINT( std::cerr << "compatible!" << std::endl; )
|
---|
[89be1c68] | 75 | return Cost::zero;
|
---|
[ef5b828] | 76 | } else if ( dynamic_cast< const VoidType * >( dest ) ) {
|
---|
[89be1c68] | 77 | return Cost::safe;
|
---|
[7870799] | 78 | } else if ( const ReferenceType * refType = dynamic_cast< const ReferenceType * > ( dest ) ) {
|
---|
[150ec33] | 79 | PRINT( std::cerr << "conversionCost: dest is reference" << std::endl; )
|
---|
[7870799] | 80 | return convertToReferenceCost( src, refType, indexer, env, [](const Type * t1, const Type * t2, const SymTab::Indexer & indexer, const TypeEnvironment & env ) {
|
---|
[0c6596f] | 81 | return ptrsCastable( t1, t2, env, indexer );
|
---|
| 82 | });
|
---|
[a32b204] | 83 | } else {
|
---|
[7870799] | 84 | PassVisitor<CastCost_old> converter(
|
---|
| 85 | dest, indexer, env,
|
---|
| 86 | (Cost (*)( const Type *, const Type *, const SymTab::Indexer &, const TypeEnvironment & ))
|
---|
[c8e4d2f8] | 87 | castCost );
|
---|
[a32b204] | 88 | src->accept( converter );
|
---|
[bd0b6b62] | 89 | if ( converter.pass.get_cost() == Cost::infinity ) {
|
---|
[a32b204] | 90 | return Cost::infinity;
|
---|
| 91 | } else {
|
---|
[1521de20] | 92 | // xxx - why are we adding cost 0 here?
|
---|
[bd0b6b62] | 93 | return converter.pass.get_cost() + Cost::zero;
|
---|
[a32b204] | 94 | } // if
|
---|
[c11e31c] | 95 | } // if
|
---|
[a32b204] | 96 | }
|
---|
[51b73452] | 97 |
|
---|
[ef5b828] | 98 | CastCost_old::CastCost_old( const Type * dest, const SymTab::Indexer &indexer, const TypeEnvironment &env, CostFunction costFunc )
|
---|
[721cd19f] | 99 | : ConversionCost( dest, indexer, env, costFunc ) {
|
---|
[a32b204] | 100 | }
|
---|
[51b73452] | 101 |
|
---|
[ef5b828] | 102 | void CastCost_old::postvisit( const BasicType * basicType ) {
|
---|
| 103 | const PointerType * destAsPointer = dynamic_cast< const PointerType * >( dest );
|
---|
[543159b] | 104 | if ( destAsPointer && basicType->isInteger() ) {
|
---|
[ef5b828] | 105 | // necessary for, e.g. unsigned long => void *
|
---|
[89be1c68] | 106 | cost = Cost::unsafe;
|
---|
[a32b204] | 107 | } else {
|
---|
[2463d0e] | 108 | cost = conversionCost( basicType, dest, indexer, env );
|
---|
[a32b204] | 109 | } // if
|
---|
| 110 | }
|
---|
[51b73452] | 111 |
|
---|
[ef5b828] | 112 | void CastCost_old::postvisit( const PointerType * pointerType ) {
|
---|
| 113 | if ( const PointerType * destAsPtr = dynamic_cast< const PointerType * >( dest ) ) {
|
---|
[7870799] | 114 | if ( pointerType->tq <= destAsPtr->tq && typesCompatibleIgnoreQualifiers( pointerType->base, destAsPtr->base, indexer, env ) ) {
|
---|
[89be1c68] | 115 | cost = Cost::safe;
|
---|
[a32b204] | 116 | } else {
|
---|
| 117 | TypeEnvironment newEnv( env );
|
---|
[eb0aedb] | 118 | newEnv.add( pointerType->forall );
|
---|
| 119 | newEnv.add( pointerType->base->forall );
|
---|
| 120 | int castResult = ptrsCastable( pointerType->base, destAsPtr->base, newEnv, indexer );
|
---|
[1521de20] | 121 | if ( castResult > 0 ) {
|
---|
[89be1c68] | 122 | cost = Cost::safe;
|
---|
[1521de20] | 123 | } else if ( castResult < 0 ) {
|
---|
[52f85e0] | 124 | cost = Cost::infinity;
|
---|
[a32b204] | 125 | } // if
|
---|
| 126 | } // if
|
---|
[7870799] | 127 | } else if ( const BasicType * destAsBasic = dynamic_cast< const BasicType * >( dest ) ) {
|
---|
[543159b] | 128 | if ( destAsBasic->isInteger() ) {
|
---|
[ef5b828] | 129 | // necessary for, e.g. void * => unsigned long
|
---|
[89be1c68] | 130 | cost = Cost::unsafe;
|
---|
[543159b] | 131 | } // if
|
---|
| 132 | }
|
---|
[a32b204] | 133 | }
|
---|
[c8e4d2f8] | 134 |
|
---|
[3c89751] | 135 | namespace {
|
---|
| 136 | struct CastCost_new : public ConversionCost_new {
|
---|
| 137 | using ConversionCost_new::previsit;
|
---|
| 138 | using ConversionCost_new::postvisit;
|
---|
| 139 |
|
---|
[7870799] | 140 | CastCost_new(
|
---|
| 141 | const ast::Type * dst, const ast::SymbolTable & symtab,
|
---|
[3c89751] | 142 | const ast::TypeEnvironment & env, CostCalculation costFunc )
|
---|
| 143 | : ConversionCost_new( dst, symtab, env, costFunc ) {}
|
---|
| 144 |
|
---|
| 145 | void postvisit( const ast::BasicType * basicType ) {
|
---|
| 146 | auto ptr = dynamic_cast< const ast::PointerType * >( dst );
|
---|
| 147 | if ( ptr && basicType->isInteger() ) {
|
---|
| 148 | // needed for, e.g. unsigned long => void *
|
---|
| 149 | cost = Cost::unsafe;
|
---|
| 150 | } else {
|
---|
| 151 | cost = conversionCost( basicType, dst, symtab, env );
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | void postvisit( const ast::PointerType * pointerType ) {
|
---|
| 156 | if ( auto ptr = dynamic_cast< const ast::PointerType * >( dst ) ) {
|
---|
| 157 | if (
|
---|
| 158 | pointerType->qualifiers <= ptr->qualifiers
|
---|
| 159 | && typesCompatibleIgnoreQualifiers( pointerType->base, ptr->base, symtab, env )
|
---|
| 160 | ) {
|
---|
| 161 | cost = Cost::safe;
|
---|
| 162 | } else {
|
---|
| 163 | ast::TypeEnvironment newEnv{ env };
|
---|
| 164 | if ( auto wParams = pointerType->base.as< ast::ParameterizedType >() ) {
|
---|
| 165 | newEnv.add( wParams->forall );
|
---|
| 166 | }
|
---|
| 167 | int castResult = ptrsCastable( pointerType->base, ptr->base, symtab, newEnv );
|
---|
| 168 | if ( castResult > 0 ) {
|
---|
| 169 | cost = Cost::safe;
|
---|
| 170 | } else if ( castResult < 0 ) {
|
---|
| 171 | cost = Cost::infinity;
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 | } else if ( auto basic = dynamic_cast< const ast::BasicType * >( dst ) ) {
|
---|
| 175 | if ( basic->isInteger() ) {
|
---|
| 176 | // necessary for, e.g. void * => unsigned long
|
---|
| 177 | cost = Cost::unsafe;
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
| 181 | };
|
---|
| 182 | } // anonymous namespace
|
---|
| 183 |
|
---|
[7870799] | 184 | Cost castCost(
|
---|
| 185 | const ast::Type * src, const ast::Type * dst, const ast::SymbolTable & symtab,
|
---|
| 186 | const ast::TypeEnvironment & env
|
---|
[3c89751] | 187 | ) {
|
---|
| 188 | if ( auto typeInst = dynamic_cast< const ast::TypeInstType * >( dst ) ) {
|
---|
| 189 | if ( const ast::EqvClass * eqvClass = env.lookup( typeInst->name ) ) {
|
---|
| 190 | // check cast cost against bound type, if present
|
---|
| 191 | if ( eqvClass->bound ) {
|
---|
| 192 | return castCost( src, eqvClass->bound, symtab, env );
|
---|
| 193 | } else {
|
---|
| 194 | return Cost::infinity;
|
---|
| 195 | }
|
---|
| 196 | } else if ( const ast::NamedTypeDecl * named = symtab.lookupType( typeInst->name ) ) {
|
---|
| 197 | // all typedefs should be gone by now
|
---|
| 198 | auto type = strict_dynamic_cast< const ast::TypeDecl * >( named );
|
---|
| 199 | if ( type->base ) {
|
---|
| 200 | return castCost( src, type->base, symtab, env ) + Cost::safe;
|
---|
| 201 | }
|
---|
| 202 | }
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | PRINT(
|
---|
| 206 | std::cerr << "castCost ::: src is ";
|
---|
| 207 | ast::print( std::cerr, src );
|
---|
| 208 | std::cerr << std::endl << "dest is ";
|
---|
| 209 | ast::print( std::cerr, dst );
|
---|
| 210 | std::cerr << std::endl << "env is" << std::endl;
|
---|
| 211 | ast::print( std::cerr, env, 2 );
|
---|
| 212 | )
|
---|
| 213 |
|
---|
| 214 | if ( typesCompatibleIgnoreQualifiers( src, dst, symtab, env ) ) {
|
---|
| 215 | PRINT( std::cerr << "compatible!" << std::endl; )
|
---|
[c8e4d2f8] | 216 | return Cost::zero;
|
---|
[3c89751] | 217 | } else if ( dynamic_cast< const ast::VoidType * >( dst ) ) {
|
---|
| 218 | return Cost::safe;
|
---|
| 219 | } else if ( auto refType = dynamic_cast< const ast::ReferenceType * >( dst ) ) {
|
---|
| 220 | PRINT( std::cerr << "conversionCost: dest is reference" << std::endl; )
|
---|
| 221 | #warning cast on ptrsCastable artifact of having two functions, remove when port done
|
---|
[7870799] | 222 | return convertToReferenceCost(
|
---|
| 223 | src, refType, symtab, env,
|
---|
| 224 | ( int (*)(
|
---|
| 225 | const ast::Type *, const ast::Type *, const ast::SymbolTable &,
|
---|
[3c89751] | 226 | const ast::TypeEnvironment & )
|
---|
| 227 | ) ptrsCastable );
|
---|
| 228 | } else {
|
---|
| 229 | #warning cast on castCost artifact of having two functions, remove when port done
|
---|
| 230 | ast::Pass< CastCost_new > converter{
|
---|
[7870799] | 231 | dst, symtab, env,
|
---|
| 232 | ( Cost (*)(
|
---|
| 233 | const ast::Type *, const ast::Type *, const ast::SymbolTable &,
|
---|
[3c89751] | 234 | const ast::TypeEnvironment & )
|
---|
| 235 | ) castCost };
|
---|
| 236 | src->accept( converter );
|
---|
| 237 | return converter.pass.cost;
|
---|
[c8e4d2f8] | 238 | }
|
---|
[3c89751] | 239 | }
|
---|
| 240 |
|
---|
[51b73452] | 241 | } // namespace ResolvExpr
|
---|
[a32b204] | 242 |
|
---|
| 243 | // Local Variables: //
|
---|
| 244 | // tab-width: 4 //
|
---|
| 245 | // mode: c++ //
|
---|
| 246 | // compile-command: "make install" //
|
---|
| 247 | // End: //
|
---|