source: src/ResolvExpr/CastCost.cc@ 10dc6908

ADT arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 10dc6908 was 150ec33, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Fix bug in computing union conversion costs

  • Property mode set to 100644
File size: 4.6 KB
RevLine 
[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
32namespace ResolvExpr {
[a32b204]33 class CastCost : public ConversionCost {
34 public:
35 CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env );
[0b150ec]36
[a32b204]37 virtual void visit( BasicType *basicType );
38 virtual void visit( PointerType *pointerType );
39 };
[51b73452]40
[a32b204]41 Cost castCost( Type *src, Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
42 if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) {
43 EqvClass eqvClass;
44 NamedTypeDecl *namedType;
45 if ( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) {
[0b150ec]46 if ( eqvClass.type ) {
47 return castCost( src, eqvClass.type, indexer, env );
48 } else {
49 return Cost::infinity;
50 }
[a32b204]51 } else if ( ( namedType = indexer.lookupType( destAsTypeInst->get_name() ) ) ) {
52 // all typedefs should be gone by this point
[982832e]53 TypeDecl *type = strict_dynamic_cast< TypeDecl* >( namedType );
[a32b204]54 if ( type->get_base() ) {
[89be1c68]55 return castCost( src, type->get_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; )
[0c6596f]76 return convertToReferenceCost( src, refType, indexer, env, [](Type * t1, Type * t2, const TypeEnvironment & env, const SymTab::Indexer & indexer) {
77 return ptrsCastable( t1, t2, env, indexer );
78 });
[a32b204]79 } else {
80 CastCost converter( dest, indexer, env );
81 src->accept( converter );
82 if ( converter.get_cost() == Cost::infinity ) {
83 return Cost::infinity;
84 } else {
[1521de20]85 // xxx - why are we adding cost 0 here?
[89be1c68]86 return converter.get_cost() + Cost::zero;
[a32b204]87 } // if
[c11e31c]88 } // if
[a32b204]89 }
[51b73452]90
[a32b204]91 CastCost::CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env )
92 : ConversionCost( dest, indexer, env ) {
93 }
[51b73452]94
[a32b204]95 void CastCost::visit( 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
[a32b204]105 void CastCost::visit( PointerType *pointerType ) {
106 if ( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) {
107 if ( pointerType->get_qualifiers() <= destAsPtr->get_qualifiers() && typesCompatibleIgnoreQualifiers( pointerType->get_base(), destAsPtr->get_base(), indexer, env ) ) {
[89be1c68]108 cost = Cost::safe;
[a32b204]109 } else {
110 TypeEnvironment newEnv( env );
111 newEnv.add( pointerType->get_forall() );
112 newEnv.add( pointerType->get_base()->get_forall() );
[1521de20]113 int castResult = ptrsCastable( pointerType->get_base(), destAsPtr->get_base(), newEnv, indexer );
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: //
Note: See TracBrowser for help on using the repository browser.