source: src/ResolvExpr/CastCost.cc @ 2ae171d8

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 2ae171d8 was 8135d4c, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Merge branch 'master' into references

  • Property mode set to 100644
File size: 4.0 KB
RevLine 
[a32b204]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//
[0b150ec]7// CastCost.cc --
[a32b204]8//
9// Author           : Richard C. Bilson
10// Created On       : Sun May 17 06:57:43 2015
[52f85e0]11// Last Modified By : Peter A. Buhr
12// Last Modified On : Tue Feb  2 15:34:36 2016
13// Update Count     : 7
[a32b204]14//
15
[ea6332d]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
[51b7345]25
26
27namespace ResolvExpr {
[a32b204]28        class CastCost : public ConversionCost {
29          public:
30                CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env );
[0b150ec]31
[a32b204]32                virtual void visit( BasicType *basicType );
33                virtual void visit( PointerType *pointerType );
34        };
[51b7345]35
[a32b204]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 ) ) {
[0b150ec]41                                if ( eqvClass.type ) {
42                                        return castCost( src, eqvClass.type, indexer, env );
43                                } else {
44                                        return Cost::infinity;
45                                }
[a32b204]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() ) {
[89be1c68]51                                        return castCost( src, type->get_base(), indexer, env ) + Cost::safe;
[a32b204]52                                } // if
53                        } // if
54                } // if
55                if ( typesCompatibleIgnoreQualifiers( src, dest, indexer, env ) ) {
[89be1c68]56                        return Cost::zero;
[a32b204]57                } else if ( dynamic_cast< VoidType* >( dest ) ) {
[89be1c68]58                        return Cost::safe;
[2463d0e]59                } else if ( ReferenceType * refType = dynamic_cast< ReferenceType * > ( dest ) ) {
60                        return convertToReferenceCost( src, refType, indexer, env );
[a32b204]61                } else {
62                        CastCost converter( dest, indexer, env );
63                        src->accept( converter );
64                        if ( converter.get_cost() == Cost::infinity ) {
65                                return Cost::infinity;
66                        } else {
[1521de20]67                                // xxx - why are we adding cost 0 here?
[89be1c68]68                                return converter.get_cost() + Cost::zero;
[a32b204]69                        } // if
[c11e31c]70                } // if
[a32b204]71        }
[51b7345]72
[a32b204]73        CastCost::CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env )
74                : ConversionCost( dest, indexer, env ) {
75        }
[51b7345]76
[a32b204]77        void CastCost::visit( BasicType *basicType ) {
[543159b]78                PointerType *destAsPointer = dynamic_cast< PointerType* >( dest );
79                if ( destAsPointer && basicType->isInteger() ) {
[9fd97126]80                        // necessary for, e.g. unsigned long => void*
[89be1c68]81                        cost = Cost::unsafe;
[a32b204]82                } else {
[2463d0e]83                        cost = conversionCost( basicType, dest, indexer, env );
[a32b204]84                } // if
85        }
[51b7345]86
[a32b204]87        void CastCost::visit( PointerType *pointerType ) {
88                if ( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) {
89                        if ( pointerType->get_qualifiers() <= destAsPtr->get_qualifiers() && typesCompatibleIgnoreQualifiers( pointerType->get_base(), destAsPtr->get_base(), indexer, env ) ) {
[89be1c68]90                                cost = Cost::safe;
[a32b204]91                        } else {
92                                TypeEnvironment newEnv( env );
93                                newEnv.add( pointerType->get_forall() );
94                                newEnv.add( pointerType->get_base()->get_forall() );
[1521de20]95                                int castResult = ptrsCastable( pointerType->get_base(), destAsPtr->get_base(), newEnv, indexer );
96                                if ( castResult > 0 ) {
[89be1c68]97                                        cost = Cost::safe;
[1521de20]98                                } else if ( castResult < 0 ) {
[52f85e0]99                                        cost = Cost::infinity;
[a32b204]100                                } // if
101                        } // if
[543159b]102                } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) {
103                        if ( destAsBasic->isInteger() ) {
[9fd97126]104                                // necessary for, e.g. void* => unsigned long
[89be1c68]105                                cost = Cost::unsafe;
[543159b]106                        } // if
107                }
[a32b204]108        }
[51b7345]109} // namespace ResolvExpr
[a32b204]110
111// Local Variables: //
112// tab-width: 4 //
113// mode: c++ //
114// compile-command: "make install" //
115// End: //
Note: See TracBrowser for help on using the repository browser.