source: src/ResolvExpr/CastCost.cc @ 7dc09294

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 7dc09294 was 108f3cdb, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Fix generation of assignment and autogenerate constructors and destructors for opaque type declarations

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