source: src/ResolvExpr/CastCost.cc@ f7cb0bc

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since f7cb0bc was 0b150ec, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

minor zero_t/one_t prelude changes, simplify prelude generation, memory error fixes

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