source: src/ResolvExpr/CastCost.cc @ ea6332d

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 ea6332d was ea6332d, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Big header cleaning pass - commit 3

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