source: src/ResolvExpr/CastCost.cc @ 1395748

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 1395748 was 0c6596f, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Handle difference between reference casting and implicit conversion costs [fixes #39]

  • Property mode set to 100644
File size: 4.2 KB
Line 
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
27namespace 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, [](Type * t1, Type * t2, const TypeEnvironment & env, const SymTab::Indexer & indexer) {
61                                return ptrsCastable( t1, t2, env, indexer );
62                        });
63                } else {
64                        CastCost converter( dest, indexer, env );
65                        src->accept( converter );
66                        if ( converter.get_cost() == Cost::infinity ) {
67                                return Cost::infinity;
68                        } else {
69                                // xxx - why are we adding cost 0 here?
70                                return converter.get_cost() + Cost::zero;
71                        } // if
72                } // if
73        }
74
75        CastCost::CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env )
76                : ConversionCost( dest, indexer, env ) {
77        }
78
79        void CastCost::visit( BasicType *basicType ) {
80                PointerType *destAsPointer = dynamic_cast< PointerType* >( dest );
81                if ( destAsPointer && basicType->isInteger() ) {
82                        // necessary for, e.g. unsigned long => void*
83                        cost = Cost::unsafe;
84                } else {
85                        cost = conversionCost( basicType, dest, indexer, env );
86                } // if
87        }
88
89        void CastCost::visit( PointerType *pointerType ) {
90                if ( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) {
91                        if ( pointerType->get_qualifiers() <= destAsPtr->get_qualifiers() && typesCompatibleIgnoreQualifiers( pointerType->get_base(), destAsPtr->get_base(), indexer, env ) ) {
92                                cost = Cost::safe;
93                        } else {
94                                TypeEnvironment newEnv( env );
95                                newEnv.add( pointerType->get_forall() );
96                                newEnv.add( pointerType->get_base()->get_forall() );
97                                int castResult = ptrsCastable( pointerType->get_base(), destAsPtr->get_base(), newEnv, indexer );
98                                if ( castResult > 0 ) {
99                                        cost = Cost::safe;
100                                } else if ( castResult < 0 ) {
101                                        cost = Cost::infinity;
102                                } // if
103                        } // if
104                } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) {
105                        if ( destAsBasic->isInteger() ) {
106                                // necessary for, e.g. void* => unsigned long
107                                cost = Cost::unsafe;
108                        } // if
109                }
110        }
111} // namespace ResolvExpr
112
113// Local Variables: //
114// tab-width: 4 //
115// mode: c++ //
116// compile-command: "make install" //
117// End: //
Note: See TracBrowser for help on using the repository browser.