source: src/ResolvExpr/CastCost.cc @ 1521de2

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 1521de2 was 1521de2, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

fix segmentation fault in PtrsAssignable?, fix logic error with return value in PtrsCastable?

  • Property mode set to 100644
File size: 3.3 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 : Rob Schluntz
12// Last Modified On : Mon Oct 05 14:48:45 2015
13// Update Count     : 5
14//
15
16#include "typeops.h"
17#include "Cost.h"
18#include "ConversionCost.h"
19#include "SynTree/Type.h"
20#include "SynTree/Visitor.h"
21#include "SymTab/Indexer.h"
22
23
24namespace ResolvExpr {
25        class CastCost : public ConversionCost {
26          public:
27                CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env );
28 
29                virtual void visit( BasicType *basicType );
30                virtual void visit( PointerType *pointerType );
31        };
32
33        Cost castCost( Type *src, Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
34                if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) {
35                        EqvClass eqvClass;
36                        NamedTypeDecl *namedType;
37                        if ( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) {
38                                return castCost( src, eqvClass.type, indexer, env );
39                        } else if ( ( namedType = indexer.lookupType( destAsTypeInst->get_name() ) ) ) {
40                                TypeDecl *type = dynamic_cast< TypeDecl* >( namedType );
41                                // all typedefs should be gone by this point
42                                assert( type );
43                                if ( type->get_base() ) {
44                                        return castCost( src, type->get_base(), indexer, env ) + Cost( 0, 0, 1 );
45                                } // if
46                        } // if
47                } // if
48                if ( typesCompatibleIgnoreQualifiers( src, dest, indexer, env ) ) {
49                        return Cost( 0, 0, 0 );
50                } else if ( dynamic_cast< VoidType* >( dest ) ) {
51                        return Cost( 0, 0, 1 );
52                } else {
53                        CastCost converter( dest, indexer, env );
54                        src->accept( converter );
55                        if ( converter.get_cost() == Cost::infinity ) {
56                                return Cost::infinity;
57                        } else {
58                                // xxx - why are we adding cost 0 here?
59                                return converter.get_cost() + Cost( 0, 0, 0 );
60                        } // if
61                } // if
62        }
63
64        CastCost::CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env )
65                : ConversionCost( dest, indexer, env ) {
66        }
67
68        void CastCost::visit( BasicType *basicType ) {
69                PointerType *destAsPointer = dynamic_cast< PointerType* >( dest );
70                if ( destAsPointer && basicType->isInteger() ) {
71                        cost = Cost( 1, 0, 0 );
72                } else {
73                        ConversionCost::visit( basicType );
74                } // if
75        }
76
77        void CastCost::visit( PointerType *pointerType ) {
78                if ( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) {
79                        if ( pointerType->get_qualifiers() <= destAsPtr->get_qualifiers() && typesCompatibleIgnoreQualifiers( pointerType->get_base(), destAsPtr->get_base(), indexer, env ) ) {
80                                cost = Cost( 0, 0, 1 );
81                        } else {
82                                TypeEnvironment newEnv( env );
83                                newEnv.add( pointerType->get_forall() );
84                                newEnv.add( pointerType->get_base()->get_forall() );
85                                int castResult = ptrsCastable( pointerType->get_base(), destAsPtr->get_base(), newEnv, indexer );
86                                if ( castResult > 0 ) {
87                                        cost = Cost( 0, 0, 1 );
88                                } else if ( castResult < 0 ) {
89                                        cost = Cost( 1, 0, 0 );
90                                } // if
91                        } // if
92                } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) {
93                        if ( destAsBasic->isInteger() ) {
94                                cost = Cost( 1, 0, 0 );
95                        } // if
96                }
97        }
98} // namespace ResolvExpr
99
100// Local Variables: //
101// tab-width: 4 //
102// mode: c++ //
103// compile-command: "make install" //
104// End: //
Note: See TracBrowser for help on using the repository browser.