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.cpp -- |
---|
8 | // |
---|
9 | // Author : Richard C. Bilson |
---|
10 | // Created On : Sun May 17 06:57:43 2015 |
---|
11 | // Last Modified By : Andrew Beach |
---|
12 | // Last Modified On : Tue Oct 4 15:00:00 2019 |
---|
13 | // Update Count : 9 |
---|
14 | // |
---|
15 | |
---|
16 | #include "CastCost.hpp" |
---|
17 | |
---|
18 | #include <cassert> // for assert |
---|
19 | |
---|
20 | #include "AST/Print.hpp" |
---|
21 | #include "AST/SymbolTable.hpp" |
---|
22 | #include "AST/Type.hpp" |
---|
23 | #include "AST/TypeEnvironment.hpp" |
---|
24 | #include "ConversionCost.hpp" // for ConversionCost |
---|
25 | #include "Cost.hpp" // for Cost, Cost::infinity |
---|
26 | #include "ResolvExpr/ConversionCost.hpp" // for conversionCost |
---|
27 | #include "ResolvExpr/PtrsCastable.hpp" // for ptrsCastable |
---|
28 | #include "ResolvExpr/Unify.hpp" // for typesCompatibleIgnoreQualifiers |
---|
29 | |
---|
30 | #if 0 |
---|
31 | #define PRINT(x) x |
---|
32 | #else |
---|
33 | #define PRINT(x) |
---|
34 | #endif |
---|
35 | |
---|
36 | namespace ResolvExpr { |
---|
37 | |
---|
38 | namespace { |
---|
39 | struct CastCost : public ConversionCost { |
---|
40 | using ConversionCost::previsit; |
---|
41 | using ConversionCost::postvisit; |
---|
42 | |
---|
43 | CastCost( |
---|
44 | const ast::Type * dst, bool srcIsLvalue, const ast::SymbolTable & symtab, |
---|
45 | const ast::TypeEnvironment & env, CostCalculation costFunc ) |
---|
46 | : ConversionCost( dst, srcIsLvalue, symtab, env, costFunc ) {} |
---|
47 | |
---|
48 | void postvisit( const ast::BasicType * basicType ) { |
---|
49 | auto ptr = dynamic_cast< const ast::PointerType * >( dst ); |
---|
50 | if ( ptr && basicType->isInteger() ) { |
---|
51 | // needed for, e.g. unsigned long => void * |
---|
52 | cost = Cost::unsafe; |
---|
53 | } else { |
---|
54 | cost = conversionCost( basicType, dst, srcIsLvalue, symtab, env ); |
---|
55 | if ( Cost::unsafe < cost ) { |
---|
56 | if (auto enumInst = dynamic_cast<const ast::EnumInstType *>(dst)) { |
---|
57 | // Always explict cast only for typed enum |
---|
58 | if (enumInst->base->isTyped) cost = Cost::unsafe; |
---|
59 | } |
---|
60 | } |
---|
61 | } |
---|
62 | } |
---|
63 | |
---|
64 | void postvisit( const ast::ZeroType * zero ) { |
---|
65 | cost = conversionCost( zero, dst, srcIsLvalue, symtab, env ); |
---|
66 | if ( Cost::unsafe < cost ) { |
---|
67 | if (auto enumInst = dynamic_cast<const ast::EnumInstType *>(dst)) { |
---|
68 | if (enumInst->base->isTyped) cost = Cost::unsafe; |
---|
69 | } |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | void postvisit( const ast::OneType * one ) { |
---|
74 | cost = conversionCost( one, dst, srcIsLvalue, symtab, env ); |
---|
75 | if ( Cost::unsafe < cost ) { |
---|
76 | if (auto enumInst = dynamic_cast<const ast::EnumInstType *>(dst)) { |
---|
77 | if (enumInst->base->isTyped) cost = Cost::unsafe; |
---|
78 | } |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | void postvisit( const ast::PointerType * pointerType ) { |
---|
83 | if ( auto ptr = dynamic_cast< const ast::PointerType * >( dst ) ) { |
---|
84 | if ( |
---|
85 | pointerType->qualifiers <= ptr->qualifiers |
---|
86 | && typesCompatibleIgnoreQualifiers( pointerType->base, ptr->base, env ) |
---|
87 | ) { |
---|
88 | cost = Cost::safe; |
---|
89 | } else { |
---|
90 | ast::TypeEnvironment newEnv{ env }; |
---|
91 | if ( auto wParams = pointerType->base.as< ast::FunctionType >() ) { |
---|
92 | newEnv.add( wParams->forall ); |
---|
93 | } |
---|
94 | int castResult = ptrsCastable( pointerType->base, ptr->base, symtab, newEnv ); |
---|
95 | if ( castResult > 0 ) { |
---|
96 | cost = Cost::safe; |
---|
97 | } else if ( castResult < 0 ) { |
---|
98 | cost = Cost::infinity; |
---|
99 | } |
---|
100 | } |
---|
101 | } else if ( auto basic = dynamic_cast< const ast::BasicType * >( dst ) ) { |
---|
102 | if ( basic->isInteger() ) { |
---|
103 | // necessary for, e.g. void * => unsigned long |
---|
104 | cost = Cost::unsafe; |
---|
105 | } |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | void postvist( const ast::EnumInstType * ) { |
---|
110 | if ( auto basic = dynamic_cast< const ast::BasicType * >(dst) ) { |
---|
111 | if ( basic->isInteger() ) cost = Cost::unsafe; |
---|
112 | } |
---|
113 | } |
---|
114 | }; |
---|
115 | |
---|
116 | } // anonymous namespace |
---|
117 | |
---|
118 | Cost castCost( |
---|
119 | const ast::Type * src, const ast::Type * dst, bool srcIsLvalue, |
---|
120 | const ast::SymbolTable & symtab, const ast::TypeEnvironment & env |
---|
121 | ) { |
---|
122 | if ( auto typeInst = dynamic_cast< const ast::TypeInstType * >( dst ) ) { |
---|
123 | if ( const ast::EqvClass * eqvClass = env.lookup( *typeInst ) ) { |
---|
124 | // check cast cost against bound type, if present |
---|
125 | if ( eqvClass->bound ) { |
---|
126 | return castCost( src, eqvClass->bound, srcIsLvalue, symtab, env ); |
---|
127 | } else { |
---|
128 | return Cost::infinity; |
---|
129 | } |
---|
130 | } else if ( const ast::NamedTypeDecl * named = symtab.lookupType( typeInst->name ) ) { |
---|
131 | // all typedefs should be gone by now |
---|
132 | auto type = strict_dynamic_cast< const ast::TypeDecl * >( named ); |
---|
133 | if ( type->base ) { |
---|
134 | return castCost( src, type->base, srcIsLvalue, symtab, env ) + Cost::safe; |
---|
135 | } |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | PRINT( |
---|
140 | std::cerr << "castCost ::: src is "; |
---|
141 | ast::print( std::cerr, src ); |
---|
142 | std::cerr << std::endl << "dest is "; |
---|
143 | ast::print( std::cerr, dst ); |
---|
144 | std::cerr << std::endl << "env is" << std::endl; |
---|
145 | ast::print( std::cerr, env, 2 ); |
---|
146 | ) |
---|
147 | |
---|
148 | if ( typesCompatibleIgnoreQualifiers( src, dst, env ) ) { |
---|
149 | PRINT( std::cerr << "compatible!" << std::endl; ) |
---|
150 | if (dynamic_cast<const ast::ZeroType *>(dst) || dynamic_cast<const ast::OneType *>(dst)) { |
---|
151 | return Cost::spec; |
---|
152 | } |
---|
153 | return Cost::zero; |
---|
154 | } else if ( dynamic_cast< const ast::VoidType * >( dst ) ) { |
---|
155 | return Cost::safe; |
---|
156 | } else if ( auto refType = dynamic_cast< const ast::ReferenceType * >( dst ) ) { |
---|
157 | PRINT( std::cerr << "conversionCost: dest is reference" << std::endl; ) |
---|
158 | return convertToReferenceCost( |
---|
159 | src, refType, srcIsLvalue, symtab, env, ptrsCastable ); |
---|
160 | } else { |
---|
161 | ast::Pass< CastCost > converter( |
---|
162 | dst, srcIsLvalue, symtab, env, castCost ); |
---|
163 | src->accept( converter ); |
---|
164 | return converter.core.cost; |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | } // namespace ResolvExpr |
---|
169 | |
---|
170 | // Local Variables: // |
---|
171 | // tab-width: 4 // |
---|
172 | // mode: c++ // |
---|
173 | // compile-command: "make install" // |
---|
174 | // End: // |
---|