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 |
|
---|
22 | namespace 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( __attribute((unused)) 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(__attribute((unused)) ArrayType *arrayType) {}
|
---|
189 | void ConversionCost::visit(__attribute((unused)) FunctionType *functionType) {}
|
---|
190 |
|
---|
191 | void ConversionCost::visit(StructInstType *inst) {
|
---|
192 | if ( StructInstType *destAsInst = dynamic_cast< StructInstType* >( dest ) ) {
|
---|
193 | if ( inst->get_name() == destAsInst->get_name() ) {
|
---|
194 | cost = Cost::zero;
|
---|
195 | } // if
|
---|
196 | } // if
|
---|
197 | }
|
---|
198 |
|
---|
199 | void ConversionCost::visit(UnionInstType *inst) {
|
---|
200 | if ( StructInstType *destAsInst = dynamic_cast< StructInstType* >( dest ) ) {
|
---|
201 | if ( inst->get_name() == destAsInst->get_name() ) {
|
---|
202 | cost = Cost::zero;
|
---|
203 | } // if
|
---|
204 | } // if
|
---|
205 | }
|
---|
206 |
|
---|
207 | void ConversionCost::visit( __attribute((unused)) EnumInstType *inst ) {
|
---|
208 | static Type::Qualifiers q;
|
---|
209 | static BasicType integer( q, BasicType::SignedInt );
|
---|
210 | integer.accept( *this );
|
---|
211 | if ( cost < Cost( 1, 0, 0 ) ) {
|
---|
212 | cost.incSafe();
|
---|
213 | } // if
|
---|
214 | }
|
---|
215 |
|
---|
216 | void ConversionCost::visit( __attribute((unused)) TraitInstType *inst) {
|
---|
217 | }
|
---|
218 |
|
---|
219 | void ConversionCost::visit(TypeInstType *inst) {
|
---|
220 | EqvClass eqvClass;
|
---|
221 | NamedTypeDecl *namedType;
|
---|
222 | if ( env.lookup( inst->get_name(), eqvClass ) ) {
|
---|
223 | cost = conversionCost( eqvClass.type, dest, indexer, env );
|
---|
224 | } else if ( TypeInstType *destAsInst = dynamic_cast< TypeInstType* >( dest ) ) {
|
---|
225 | if ( inst->get_name() == destAsInst->get_name() ) {
|
---|
226 | cost = Cost::zero;
|
---|
227 | }
|
---|
228 | } else if ( ( namedType = indexer.lookupType( inst->get_name() ) ) ) {
|
---|
229 | TypeDecl *type = dynamic_cast< TypeDecl* >( namedType );
|
---|
230 | // all typedefs should be gone by this point
|
---|
231 | assert( type );
|
---|
232 | if ( type->get_base() ) {
|
---|
233 | cost = conversionCost( type->get_base(), dest, indexer, env ) + Cost( 0, 0, 1 );
|
---|
234 | } // if
|
---|
235 | } // if
|
---|
236 | }
|
---|
237 |
|
---|
238 | void ConversionCost::visit( __attribute((unused)) TupleType *tupleType) {
|
---|
239 | Cost c;
|
---|
240 | if ( TupleType *destAsTuple = dynamic_cast< TupleType* >( dest ) ) {
|
---|
241 | std::list< Type* >::const_iterator srcIt = tupleType->get_types().begin();
|
---|
242 | std::list< Type* >::const_iterator destIt = destAsTuple->get_types().begin();
|
---|
243 | while ( srcIt != tupleType->get_types().end() && destIt != destAsTuple->get_types().end() ) {
|
---|
244 | Cost newCost = conversionCost( *srcIt++, *destIt++, indexer, env );
|
---|
245 | if ( newCost == Cost::infinity ) {
|
---|
246 | return;
|
---|
247 | } // if
|
---|
248 | c += newCost;
|
---|
249 | } // while
|
---|
250 | if ( destIt != destAsTuple->get_types().end() ) {
|
---|
251 | cost = Cost::infinity;
|
---|
252 | } else {
|
---|
253 | cost = c;
|
---|
254 | } // if
|
---|
255 | } // if
|
---|
256 | }
|
---|
257 |
|
---|
258 | void ConversionCost::visit( __attribute((unused)) VarArgsType *varArgsType) {
|
---|
259 | if ( dynamic_cast< VarArgsType* >( dest ) ) {
|
---|
260 | cost = Cost::zero;
|
---|
261 | }
|
---|
262 | }
|
---|
263 |
|
---|
264 | void ConversionCost::visit( __attribute((unused)) ZeroType *zeroType) {
|
---|
265 | if ( dynamic_cast< ZeroType* >( dest ) ) {
|
---|
266 | cost = Cost::zero;
|
---|
267 | } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) {
|
---|
268 | // copied from visit(BasicType*) for signed int, but +1 for safe conversions
|
---|
269 | int tableResult = costMatrix[ BasicType::SignedInt ][ destAsBasic->get_kind() ];
|
---|
270 | if ( tableResult == -1 ) {
|
---|
271 | cost = Cost( 1, 0, 0 );
|
---|
272 | } else {
|
---|
273 | cost = Cost( 0, 0, tableResult + 1 );
|
---|
274 | }
|
---|
275 | } else if ( dynamic_cast< PointerType* >( dest ) ) {
|
---|
276 | cost = Cost( 0, 0, 1 );
|
---|
277 | }
|
---|
278 | }
|
---|
279 |
|
---|
280 | void ConversionCost::visit( __attribute((unused)) OneType *oneType) {
|
---|
281 | if ( dynamic_cast< OneType* >( dest ) ) {
|
---|
282 | cost = Cost::zero;
|
---|
283 | } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) {
|
---|
284 | // copied from visit(BasicType*) for signed int, but +1 for safe conversions
|
---|
285 | int tableResult = costMatrix[ BasicType::SignedInt ][ destAsBasic->get_kind() ];
|
---|
286 | if ( tableResult == -1 ) {
|
---|
287 | cost = Cost( 1, 0, 0 );
|
---|
288 | } else {
|
---|
289 | cost = Cost( 0, 0, tableResult + 1 );
|
---|
290 | }
|
---|
291 | }
|
---|
292 | }
|
---|
293 | } // namespace ResolvExpr
|
---|
294 |
|
---|
295 | // Local Variables: //
|
---|
296 | // tab-width: 4 //
|
---|
297 | // mode: c++ //
|
---|
298 | // compile-command: "make install" //
|
---|
299 | // End: //
|
---|