source: src/ResolvExpr/CastCost.cc@ 834b892

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 834b892 was bd0b6b62, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Convert ConversionCost and CastCost to PassVisitor

  • Property mode set to 100644
File size: 4.7 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#if 0
27#define PRINT(x) x
28#else
29#define PRINT(x)
30#endif
31
32namespace ResolvExpr {
33 struct CastCost : public ConversionCost {
34 public:
35 CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env, CostFunction costFunc );
36
37 using ConversionCost::previsit;
38 using ConversionCost::postvisit;
39 void postvisit( BasicType * basicType );
40 void postvisit( PointerType * pointerType );
41 };
42
43 Cost castCost( Type *src, Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
44 if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) {
45 EqvClass eqvClass;
46 NamedTypeDecl *namedType;
47 if ( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) {
48 if ( eqvClass.type ) {
49 return castCost( src, eqvClass.type, indexer, env );
50 } else {
51 return Cost::infinity;
52 }
53 } else if ( ( namedType = indexer.lookupType( destAsTypeInst->get_name() ) ) ) {
54 // all typedefs should be gone by this point
55 TypeDecl *type = strict_dynamic_cast< TypeDecl* >( namedType );
56 if ( type->base ) {
57 return castCost( src, type->base, indexer, env ) + Cost::safe;
58 } // if
59 } // if
60 } // if
61
62 PRINT(
63 std::cerr << "castCost ::: src is ";
64 src->print( std::cerr );
65 std::cerr << std::endl << "dest is ";
66 dest->print( std::cerr );
67 std::cerr << std::endl << "env is" << std::endl;
68 env.print( std::cerr, 8 );
69 )
70
71 if ( typesCompatibleIgnoreQualifiers( src, dest, indexer, env ) ) {
72 PRINT( std::cerr << "compatible!" << std::endl; )
73 return Cost::zero;
74 } else if ( dynamic_cast< VoidType* >( dest ) ) {
75 return Cost::safe;
76 } else if ( ReferenceType * refType = dynamic_cast< ReferenceType * > ( dest ) ) {
77 PRINT( std::cerr << "conversionCost: dest is reference" << std::endl; )
78 return convertToReferenceCost( src, refType, indexer, env, [](Type * t1, Type * t2, const SymTab::Indexer & indexer, const TypeEnvironment & env ) {
79 return ptrsCastable( t1, t2, env, indexer );
80 });
81 } else {
82 PassVisitor<CastCost> converter( dest, indexer, env, castCost );
83 src->accept( converter );
84 if ( converter.pass.get_cost() == Cost::infinity ) {
85 return Cost::infinity;
86 } else {
87 // xxx - why are we adding cost 0 here?
88 return converter.pass.get_cost() + Cost::zero;
89 } // if
90 } // if
91 }
92
93 CastCost::CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env, CostFunction costFunc )
94 : ConversionCost( dest, indexer, env, costFunc ) {
95 }
96
97 void CastCost::postvisit( BasicType *basicType ) {
98 PointerType *destAsPointer = dynamic_cast< PointerType* >( dest );
99 if ( destAsPointer && basicType->isInteger() ) {
100 // necessary for, e.g. unsigned long => void*
101 cost = Cost::unsafe;
102 } else {
103 cost = conversionCost( basicType, dest, indexer, env );
104 } // if
105 }
106
107 void CastCost::postvisit( PointerType *pointerType ) {
108 if ( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) {
109 if ( pointerType->get_qualifiers() <= destAsPtr->get_qualifiers() && typesCompatibleIgnoreQualifiers( pointerType->base, destAsPtr->base, indexer, env ) ) {
110 cost = Cost::safe;
111 } else {
112 TypeEnvironment newEnv( env );
113 newEnv.add( pointerType->forall );
114 newEnv.add( pointerType->base->forall );
115 int castResult = ptrsCastable( pointerType->base, destAsPtr->base, newEnv, indexer );
116 if ( castResult > 0 ) {
117 cost = Cost::safe;
118 } else if ( castResult < 0 ) {
119 cost = Cost::infinity;
120 } // if
121 } // if
122 } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) {
123 if ( destAsBasic->isInteger() ) {
124 // necessary for, e.g. void* => unsigned long
125 cost = Cost::unsafe;
126 } // if
127 }
128 }
129} // namespace ResolvExpr
130
131// Local Variables: //
132// tab-width: 4 //
133// mode: c++ //
134// compile-command: "make install" //
135// End: //
Note: See TracBrowser for help on using the repository browser.