source: src/ResolvExpr/CastCost.cc @ 5170d95

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since 5170d95 was 00ac42e, checked in by Aaron Moss <a3moss@…>, 6 years ago

stop eagerly copying EqvClass? on lookup

  • Property mode set to 100644
File size: 4.7 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#if 0
27#define PRINT(x) x
28#else
29#define PRINT(x)
30#endif
31
32namespace ResolvExpr {
33        struct CastCost : public ConversionCost {
34          public:
35                CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env, CostFunction costFunc );
36
37                using ConversionCost::previsit;
38                using ConversionCost::postvisit;
39                void postvisit( BasicType * basicType );
40                void postvisit( PointerType * pointerType );
41        };
42
43        Cost castCost( Type *src, Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
44                if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) {
45                        if ( const EqvClass* eqvClass = env.lookup( destAsTypeInst->get_name() ) ) {
46                                if ( eqvClass->type ) {
47                                        return castCost( src, eqvClass->type, indexer, env );
48                                } else {
49                                        return Cost::infinity;
50                                }
51                        } else if ( NamedTypeDecl *namedType = indexer.lookupType( destAsTypeInst->get_name() ) ) {
52                                // all typedefs should be gone by this point
53                                TypeDecl *type = strict_dynamic_cast< TypeDecl* >( namedType );
54                                if ( type->base ) {
55                                        return castCost( src, type->base, indexer, env ) + Cost::safe;
56                                } // if
57                        } // if
58                } // if
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
69                if ( typesCompatibleIgnoreQualifiers( src, dest, indexer, env ) ) {
70                        PRINT( std::cerr << "compatible!" << std::endl; )
71                        return Cost::zero;
72                } else if ( dynamic_cast< VoidType* >( dest ) ) {
73                        return Cost::safe;
74                } else if ( ReferenceType * refType = dynamic_cast< ReferenceType * > ( dest ) ) {
75                        PRINT( std::cerr << "conversionCost: dest is reference" << std::endl; )
76                        return convertToReferenceCost( src, refType, indexer, env, [](Type * t1, Type * t2, const SymTab::Indexer & indexer, const TypeEnvironment & env ) {
77                                return ptrsCastable( t1, t2, env, indexer );
78                        });
79                } else {
80                        PassVisitor<CastCost> converter( dest, indexer, env, castCost );
81                        src->accept( converter );
82                        if ( converter.pass.get_cost() == Cost::infinity ) {
83                                return Cost::infinity;
84                        } else {
85                                // xxx - why are we adding cost 0 here?
86                                return converter.pass.get_cost() + Cost::zero;
87                        } // if
88                } // if
89        }
90
91        CastCost::CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env, CostFunction costFunc )
92                : ConversionCost( dest, indexer, env, costFunc ) {
93        }
94
95        void CastCost::postvisit( BasicType *basicType ) {
96                PointerType *destAsPointer = dynamic_cast< PointerType* >( dest );
97                if ( destAsPointer && basicType->isInteger() ) {
98                        // necessary for, e.g. unsigned long => void*
99                        cost = Cost::unsafe;
100                } else {
101                        cost = conversionCost( basicType, dest, indexer, env );
102                } // if
103        }
104
105        void CastCost::postvisit( PointerType *pointerType ) {
106                if ( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) {
107                        if ( pointerType->get_qualifiers() <= destAsPtr->get_qualifiers() && typesCompatibleIgnoreQualifiers( pointerType->base, destAsPtr->base, indexer, env ) ) {
108                                cost = Cost::safe;
109                        } else {
110                                TypeEnvironment newEnv( env );
111                                newEnv.add( pointerType->forall );
112                                newEnv.add( pointerType->base->forall );
113                                int castResult = ptrsCastable( pointerType->base, destAsPtr->base, newEnv, indexer );
114                                if ( castResult > 0 ) {
115                                        cost = Cost::safe;
116                                } else if ( castResult < 0 ) {
117                                        cost = Cost::infinity;
118                                } // if
119                        } // if
120                } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) {
121                        if ( destAsBasic->isInteger() ) {
122                                // necessary for, e.g. void* => unsigned long
123                                cost = Cost::unsafe;
124                        } // if
125                }
126        }
127} // namespace ResolvExpr
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.