source: src/ResolvExpr/CastCost.cc @ 37218fc

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 37218fc was 52f85e0, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

syslib updates and examples, formatting, make double 0 work

  • Property mode set to 100644
File size: 3.4 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 "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                        cost = Cost::infinity;
73                } else {
74                        ConversionCost::visit( basicType );
75                } // if
76        }
77
78        void CastCost::visit( PointerType *pointerType ) {
79                if ( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) {
80                        if ( pointerType->get_qualifiers() <= destAsPtr->get_qualifiers() && typesCompatibleIgnoreQualifiers( pointerType->get_base(), destAsPtr->get_base(), indexer, env ) ) {
81                                cost = Cost( 0, 0, 1 );
82                        } else {
83                                TypeEnvironment newEnv( env );
84                                newEnv.add( pointerType->get_forall() );
85                                newEnv.add( pointerType->get_base()->get_forall() );
86                                int castResult = ptrsCastable( pointerType->get_base(), destAsPtr->get_base(), newEnv, indexer );
87                                if ( castResult > 0 ) {
88                                        cost = Cost( 0, 0, 1 );
89                                } else if ( castResult < 0 ) {
90                                        cost = Cost::infinity;
91                                        //cost = Cost( 1, 0, 0 );
92                                } // if
93                        } // if
94                } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) {
95                        if ( destAsBasic->isInteger() ) {
96                                //cost = Cost( 1, 0, 0 );
97                                cost = Cost::infinity;
98                        } // if
99                }
100        }
101} // namespace ResolvExpr
102
103// Local Variables: //
104// tab-width: 4 //
105// mode: c++ //
106// compile-command: "make install" //
107// End: //
Note: See TracBrowser for help on using the repository browser.