source: src/ResolvExpr/ConversionCost.cc@ 5c69a1e

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 5c69a1e 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: 11.1 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// ConversionCost.cc --
8//
9// Author : Richard C. Bilson
10// Created On : Sun May 17 07:06:19 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Wed Mar 2 17:35:46 2016
13// Update Count : 6
14//
15
16#include "ConversionCost.h"
17#include "typeops.h"
18#include "SynTree/Type.h"
19#include "SynTree/Visitor.h"
20#include "SymTab/Indexer.h"
21
22namespace ResolvExpr {
23 const Cost Cost::zero = Cost( 0, 0, 0 );
24 const Cost Cost::infinity = Cost( -1, -1, -1 );
25
26 Cost conversionCost( Type *src, Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
27 if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) {
28 EqvClass eqvClass;
29 NamedTypeDecl *namedType;
30/// std::cout << "type inst " << destAsTypeInst->get_name();
31 if ( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) {
32 if ( eqvClass.type ) {
33 return conversionCost( src, eqvClass.type, indexer, env );
34 } else {
35 return Cost::infinity;
36 }
37 } else if ( ( namedType = indexer.lookupType( destAsTypeInst->get_name() ) ) ) {
38/// std::cout << " found" << std::endl;
39 TypeDecl *type = dynamic_cast< TypeDecl* >( namedType );
40 // all typedefs should be gone by this point
41 assert( type );
42 if ( type->get_base() ) {
43 return conversionCost( src, type->get_base(), indexer, env ) + Cost( 0, 0, 1 );
44 } // if
45 } // if
46/// std::cout << " not found" << std::endl;
47 } // if
48/// std::cout << "src is ";
49/// src->print( std::cout );
50/// std::cout << std::endl << "dest is ";
51/// dest->print( std::cout );
52/// std::cout << std::endl << "env is" << std::endl;
53/// env.print( std::cout, 8 );
54 if ( typesCompatibleIgnoreQualifiers( src, dest, indexer, env ) ) {
55/// std::cout << "compatible!" << std::endl;
56 return Cost( 0, 0, 0 );
57 } else if ( dynamic_cast< VoidType* >( dest ) ) {
58 return Cost( 0, 0, 1 );
59 } else {
60 ConversionCost converter( dest, indexer, env );
61 src->accept( converter );
62 if ( converter.get_cost() == Cost::infinity ) {
63 return Cost::infinity;
64 } else {
65 return converter.get_cost() + Cost( 0, 0, 0 );
66 } // if
67 } // if
68 }
69
70 ConversionCost::ConversionCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env )
71 : dest( dest ), indexer( indexer ), cost( Cost::infinity ), env( env ) {
72 }
73
74/*
75 Old
76 ===
77 Double
78 |
79 Float
80 |
81 ULong
82 / \
83 UInt Long
84 \ /
85 Int
86 |
87 Ushort
88 |
89 Short
90 |
91 Uchar
92 / \
93 Schar Char
94
95 New
96 ===
97 +-----LongDoubleComplex--+
98 LongDouble--+ | +-LongDoubleImag
99 | +---DoubleComplex---+ |
100 Double------+ | +----DoubleImag
101 | +-FloatComplex-+ |
102 Float---------+ +-------FloatImag
103 |
104 ULongLong
105 |
106 LongLong
107 |
108 ULong
109 / \
110 UInt Long
111 \ /
112 Int
113 |
114 Ushort
115 |
116 Short
117 |
118 Uchar
119 / \
120 Schar Char
121 \ /
122 Bool
123*/
124
125 static const int costMatrix[ BasicType::NUMBER_OF_BASIC_TYPES ][ BasicType::NUMBER_OF_BASIC_TYPES ] =
126 {
127 /* Src \ Dest: Bool Char SChar UChar Short UShort Int UInt Long ULong LLong ULLong Float Double LDbl FCplex DCplex LDCplex FImag DImag LDImag */
128 /* Bool */ { 0, 1, 1, 2, 3, 4, 5, 6, 6, 7, 8, 9, 10, 11, 12, 11, 12, 13, -1, -1, -1 },
129 /* Char */ { -1, 0, -1, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10, 11, 10, 11, 12, -1, -1, -1 },
130 /* SChar */ { -1, -1, 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10, 11, 10, 11, 12, -1, -1, -1 },
131 /* UChar */ { -1, -1, -1, 0, 1, 2, 3, 4, 4, 5, 6, 7, 8, 9, 10, 9, 10, 11, -1, -1, -1 },
132 /* Short */ { -1, -1, -1, -1, 0, 1, 2, 3, 3, 4, 5, 6, 7, 8, 9, 8, 9, 10, -1, -1, -1 },
133 /* UShort */{ -1, -1, -1, -1, -1, 0, 1, 2, 2, 3, 4, 5, 6, 7, 8, 7, 8, 9, -1, -1, -1 },
134 /* Int */ { -1, -1, -1, -1, -1, -1, 0, 1, 1, 2, 3, 4, 5, 6, 7, 6, 7, 8, -1, -1, -1 },
135 /* UInt */ { -1, -1, -1, -1, -1, -1, -1, 0, -1, 1, 2, 3, 4, 5, 6, 5, 6, 7, -1, -1, -1 },
136 /* Long */ { -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 5, 6, 7, -1, -1, -1 },
137 /* ULong */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 4, 5, 6, -1, -1, -1 },
138 /* LLong */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 3, 4, 5, -1, -1, -1 },
139 /* ULLong */{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 2, 3, 4, -1, -1, -1 },
140 /* Float */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 1, 2, 3, -1, -1, -1 },
141 /* Double */{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, -1, 1, 2, -1, -1, -1 },
142 /* LDbl */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, 1, -1, -1, -1 },
143 /* FCplex */{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, -1, -1, -1 },
144 /* DCplex */{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, -1, -1, -1 },
145 /* LDCplex */{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1 },
146 /* FImag */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 3, 0, 1, 2 },
147 /* DImag */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, -1, 0, 1 },
148 /* LDImag */{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 0 }
149 };
150
151 void ConversionCost::visit(VoidType *voidType) {
152 cost = Cost::infinity;
153 }
154
155 void ConversionCost::visit(BasicType *basicType) {
156 if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) {
157 int tableResult = costMatrix[ basicType->get_kind() ][ destAsBasic->get_kind() ];
158 if ( tableResult == -1 ) {
159 cost = Cost( 1, 0, 0 );
160 } else {
161 cost = Cost( 0, 0, tableResult );
162 } // if
163 } else if ( dynamic_cast< EnumInstType *>( dest ) ) {
164 // xxx - not positive this is correct, but appears to allow casting int => enum
165 cost = Cost( 1, 0, 0 );
166 } else if ( dynamic_cast< ZeroType* >( dest ) != nullptr || dynamic_cast< OneType* >( dest ) != nullptr ) {
167 cost = Cost( 1, 0, 0 );
168 } // if
169 }
170
171 void ConversionCost::visit(PointerType *pointerType) {
172 if ( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) {
173 if ( pointerType->get_base()->get_qualifiers() <= destAsPtr->get_base()->get_qualifiers() && typesCompatibleIgnoreQualifiers( pointerType->get_base(), destAsPtr->get_base(), indexer, env ) ) {
174 cost = Cost( 0, 0, 1 );
175 } else {
176 int assignResult = ptrsAssignable( pointerType->get_base(), destAsPtr->get_base(), env );
177 if ( assignResult < 0 ) {
178 cost = Cost( 0, 0, 1 );
179 } else if ( assignResult > 0 ) {
180 cost = Cost( 1, 0, 0 );
181 } // if
182 } // if
183 } else if ( dynamic_cast< ZeroType* >( dest ) != nullptr || dynamic_cast< OneType* >( dest ) != nullptr ) {
184 cost = Cost( 1, 0, 0 );
185 } // if
186 }
187
188 void ConversionCost::visit(ArrayType *arrayType) {
189 }
190
191 void ConversionCost::visit(FunctionType *functionType) {
192 }
193
194 void ConversionCost::visit(StructInstType *inst) {
195 if ( StructInstType *destAsInst = dynamic_cast< StructInstType* >( dest ) ) {
196 if ( inst->get_name() == destAsInst->get_name() ) {
197 cost = Cost::zero;
198 } // if
199 } // if
200 }
201
202 void ConversionCost::visit(UnionInstType *inst) {
203 if ( StructInstType *destAsInst = dynamic_cast< StructInstType* >( dest ) ) {
204 if ( inst->get_name() == destAsInst->get_name() ) {
205 cost = Cost::zero;
206 } // if
207 } // if
208 }
209
210 void ConversionCost::visit(EnumInstType *inst) {
211 static Type::Qualifiers q;
212 static BasicType integer( q, BasicType::SignedInt );
213 integer.accept( *this );
214 if ( cost < Cost( 1, 0, 0 ) ) {
215 cost.incSafe();
216 } // if
217 }
218
219 void ConversionCost::visit(TraitInstType *inst) {
220 }
221
222 void ConversionCost::visit(TypeInstType *inst) {
223 EqvClass eqvClass;
224 NamedTypeDecl *namedType;
225 if ( env.lookup( inst->get_name(), eqvClass ) ) {
226 cost = conversionCost( eqvClass.type, dest, indexer, env );
227 } else if ( TypeInstType *destAsInst = dynamic_cast< TypeInstType* >( dest ) ) {
228 if ( inst->get_name() == destAsInst->get_name() ) {
229 cost = Cost::zero;
230 }
231 } else if ( ( namedType = indexer.lookupType( inst->get_name() ) ) ) {
232 TypeDecl *type = dynamic_cast< TypeDecl* >( namedType );
233 // all typedefs should be gone by this point
234 assert( type );
235 if ( type->get_base() ) {
236 cost = conversionCost( type->get_base(), dest, indexer, env ) + Cost( 0, 0, 1 );
237 } // if
238 } // if
239 }
240
241 void ConversionCost::visit(TupleType *tupleType) {
242 Cost c;
243 if ( TupleType *destAsTuple = dynamic_cast< TupleType* >( dest ) ) {
244 std::list< Type* >::const_iterator srcIt = tupleType->get_types().begin();
245 std::list< Type* >::const_iterator destIt = destAsTuple->get_types().begin();
246 while ( srcIt != tupleType->get_types().end() && destIt != destAsTuple->get_types().end() ) {
247 Cost newCost = conversionCost( *srcIt++, *destIt++, indexer, env );
248 if ( newCost == Cost::infinity ) {
249 return;
250 } // if
251 c += newCost;
252 } // while
253 if ( destIt != destAsTuple->get_types().end() ) {
254 cost = Cost::infinity;
255 } else {
256 cost = c;
257 } // if
258 } // if
259 }
260
261 void ConversionCost::visit(VarArgsType *varArgsType) {
262 if ( dynamic_cast< VarArgsType* >( dest ) ) {
263 cost = Cost::zero;
264 }
265 }
266
267 void ConversionCost::visit(ZeroType *zeroType) {
268 if ( dynamic_cast< ZeroType* >( dest ) ) {
269 cost = Cost::zero;
270 } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) {
271 // copied from visit(BasicType*) for signed int, but +1 for safe conversions
272 int tableResult = costMatrix[ BasicType::SignedInt ][ destAsBasic->get_kind() ];
273 if ( tableResult == -1 ) {
274 cost = Cost( 1, 0, 0 );
275 } else {
276 cost = Cost( 0, 0, tableResult + 1 );
277 }
278 } else if ( dynamic_cast< PointerType* >( dest ) ) {
279 cost = Cost( 0, 0, 1 );
280 }
281 }
282
283 void ConversionCost::visit(OneType *oneType) {
284 if ( dynamic_cast< OneType* >( dest ) ) {
285 cost = Cost::zero;
286 } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) {
287 // copied from visit(BasicType*) for signed int, but +1 for safe conversions
288 int tableResult = costMatrix[ BasicType::SignedInt ][ destAsBasic->get_kind() ];
289 if ( tableResult == -1 ) {
290 cost = Cost( 1, 0, 0 );
291 } else {
292 cost = Cost( 0, 0, tableResult + 1 );
293 }
294 }
295 }
296} // namespace ResolvExpr
297
298// Local Variables: //
299// tab-width: 4 //
300// mode: c++ //
301// compile-command: "make install" //
302// End: //
Note: See TracBrowser for help on using the repository browser.