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 | // Lvalue.cc --
|
---|
8 | //
|
---|
9 | // Author : Richard C. Bilson
|
---|
10 | // Created On : Mon May 18 07:44:20 2015
|
---|
11 | // Last Modified By : Peter A. Buhr
|
---|
12 | // Last Modified On : Fri Mar 17 09:11:18 2017
|
---|
13 | // Update Count : 5
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include <cassert> // for strict_dynamic_cast
|
---|
17 | #include <string> // for string
|
---|
18 |
|
---|
19 | #include "Common/PassVisitor.h"
|
---|
20 | #include "GenPoly.h" // for isPolyType
|
---|
21 | #include "Lvalue.h"
|
---|
22 |
|
---|
23 | #include "InitTweak/InitTweak.h"
|
---|
24 | #include "Parser/LinkageSpec.h" // for Spec, isBuiltin, Intrinsic
|
---|
25 | #include "ResolvExpr/TypeEnvironment.h" // for AssertionSet, OpenVarSet
|
---|
26 | #include "ResolvExpr/Unify.h" // for unify
|
---|
27 | #include "ResolvExpr/typeops.h"
|
---|
28 | #include "SymTab/Indexer.h" // for Indexer
|
---|
29 | #include "SynTree/Declaration.h" // for Declaration, FunctionDecl
|
---|
30 | #include "SynTree/Expression.h" // for Expression, ConditionalExpr
|
---|
31 | #include "SynTree/Mutator.h" // for mutateAll, Mutator
|
---|
32 | #include "SynTree/Statement.h" // for ReturnStmt, Statement (ptr o...
|
---|
33 | #include "SynTree/Type.h" // for PointerType, Type, FunctionType
|
---|
34 | #include "SynTree/Visitor.h" // for Visitor, acceptAll
|
---|
35 | #include "Validate/FindSpecialDecls.h" // for dereferenceOperator
|
---|
36 |
|
---|
37 | #if 0
|
---|
38 | #define PRINT(x) x
|
---|
39 | #else
|
---|
40 | #define PRINT(x)
|
---|
41 | #endif
|
---|
42 |
|
---|
43 | namespace GenPoly {
|
---|
44 | namespace {
|
---|
45 | // TODO: fold this into the general createDeref function??
|
---|
46 | Expression * mkDeref( Expression * arg ) {
|
---|
47 | if ( Validate::dereferenceOperator ) {
|
---|
48 | // note: reference depth can be arbitrarily deep here, so peel off the outermost pointer/reference, not just pointer because they are effecitvely equivalent in this pass
|
---|
49 | VariableExpr * deref = new VariableExpr( Validate::dereferenceOperator );
|
---|
50 | deref->result = new PointerType( Type::Qualifiers(), deref->result );
|
---|
51 | Type * base = InitTweak::getPointerBase( arg->result );
|
---|
52 | assertf( base, "expected pointer type in dereference (type was %s)", toString( arg->result ).c_str() );
|
---|
53 | ApplicationExpr * ret = new ApplicationExpr( deref, { arg } );
|
---|
54 | delete ret->result;
|
---|
55 | ret->result = base->clone();
|
---|
56 | ret->result->set_lvalue( true );
|
---|
57 | return ret;
|
---|
58 | } else {
|
---|
59 | return UntypedExpr::createDeref( arg );
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | struct ReferenceConversions final : public WithStmtsToAdd {
|
---|
64 | Expression * postmutate( CastExpr * castExpr );
|
---|
65 | Expression * postmutate( AddressExpr * addrExpr );
|
---|
66 | };
|
---|
67 |
|
---|
68 | /// Intrinsic functions that take reference parameters don't REALLY take reference parameters -- their reference arguments must always be implicitly dereferenced.
|
---|
69 | struct FixIntrinsicArgs final {
|
---|
70 | Expression * postmutate( ApplicationExpr * appExpr );
|
---|
71 | };
|
---|
72 |
|
---|
73 | struct FixIntrinsicResult final : public WithGuards {
|
---|
74 | Expression * postmutate( ApplicationExpr * appExpr );
|
---|
75 | void premutate( FunctionDecl * funcDecl );
|
---|
76 | bool inIntrinsic = false;
|
---|
77 | };
|
---|
78 |
|
---|
79 | /// Replace reference types with pointer types
|
---|
80 | struct ReferenceTypeElimination final {
|
---|
81 | Type * postmutate( ReferenceType * refType );
|
---|
82 | };
|
---|
83 |
|
---|
84 | /// GCC-like Generalized Lvalues (which have since been removed from GCC)
|
---|
85 | /// https://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Lvalues.html#Lvalues
|
---|
86 | /// Replaces &(a,b) with (a, &b), &(a ? b : c) with (a ? &b : &c)
|
---|
87 | struct GeneralizedLvalue final : public WithVisitorRef<GeneralizedLvalue> {
|
---|
88 | Expression * postmutate( AddressExpr * addressExpr );
|
---|
89 | Expression * postmutate( MemberExpr * memExpr );
|
---|
90 |
|
---|
91 | template<typename Func>
|
---|
92 | Expression * applyTransformation( Expression * expr, Expression * arg, Func mkExpr );
|
---|
93 | };
|
---|
94 |
|
---|
95 | /// Removes redundant &*/*& pattern that this pass can generate
|
---|
96 | struct CollapseAddrDeref final {
|
---|
97 | Expression * postmutate( AddressExpr * addressExpr );
|
---|
98 | Expression * postmutate( ApplicationExpr * appExpr );
|
---|
99 | };
|
---|
100 |
|
---|
101 | struct AddrRef final : public WithGuards, public WithVisitorRef<AddrRef>, public WithShortCircuiting {
|
---|
102 | void premutate( AddressExpr * addrExpr );
|
---|
103 | Expression * postmutate( AddressExpr * addrExpr );
|
---|
104 | void premutate( Expression * expr );
|
---|
105 | void premutate( ApplicationExpr * appExpr );
|
---|
106 | void premutate( SingleInit * init );
|
---|
107 |
|
---|
108 | void handleNonAddr( Expression * );
|
---|
109 |
|
---|
110 | bool first = true;
|
---|
111 | bool current = false;
|
---|
112 | int refDepth = 0;
|
---|
113 | bool addCast = false;
|
---|
114 | };
|
---|
115 | } // namespace
|
---|
116 |
|
---|
117 | static bool referencesEliminated = false;
|
---|
118 | // used by UntypedExpr::createDeref to determine whether result type of dereference should be ReferenceType or value type.
|
---|
119 | bool referencesPermissable() {
|
---|
120 | return ! referencesEliminated;
|
---|
121 | }
|
---|
122 |
|
---|
123 | void convertLvalue( std::list< Declaration* > & translationUnit ) {
|
---|
124 | PassVisitor<ReferenceConversions> refCvt;
|
---|
125 | PassVisitor<ReferenceTypeElimination> elim;
|
---|
126 | PassVisitor<GeneralizedLvalue> genLval;
|
---|
127 | PassVisitor<FixIntrinsicArgs> fixer;
|
---|
128 | PassVisitor<CollapseAddrDeref> collapser;
|
---|
129 | PassVisitor<AddrRef> addrRef;
|
---|
130 | PassVisitor<FixIntrinsicResult> intrinsicResults;
|
---|
131 | mutateAll( translationUnit, intrinsicResults );
|
---|
132 | mutateAll( translationUnit, addrRef );
|
---|
133 | mutateAll( translationUnit, refCvt );
|
---|
134 | mutateAll( translationUnit, fixer );
|
---|
135 | mutateAll( translationUnit, collapser );
|
---|
136 | mutateAll( translationUnit, genLval );
|
---|
137 | mutateAll( translationUnit, elim ); // last because other passes need reference types to work
|
---|
138 |
|
---|
139 | // from this point forward, no other pass should create reference types.
|
---|
140 | referencesEliminated = true;
|
---|
141 | }
|
---|
142 |
|
---|
143 | Expression * generalizedLvalue( Expression * expr ) {
|
---|
144 | PassVisitor<GeneralizedLvalue> genLval;
|
---|
145 | return expr->acceptMutator( genLval );
|
---|
146 | }
|
---|
147 |
|
---|
148 | namespace {
|
---|
149 | // true for intrinsic function calls that return an lvalue in C
|
---|
150 | bool isIntrinsicReference( Expression * expr ) {
|
---|
151 | // known intrinsic-reference prelude functions
|
---|
152 | static std::set<std::string> lvalueFunctions = { "*?", "?[?]" };
|
---|
153 | if ( UntypedExpr * untyped = dynamic_cast< UntypedExpr * >( expr ) ) {
|
---|
154 | std::string fname = InitTweak::getFunctionName( untyped );
|
---|
155 | return lvalueFunctions.count(fname);
|
---|
156 | } else if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * > ( expr ) ) {
|
---|
157 | if ( DeclarationWithType * func = InitTweak::getFunction( appExpr ) ) {
|
---|
158 | return func->linkage == LinkageSpec::Intrinsic && lvalueFunctions.count(func->name);
|
---|
159 | }
|
---|
160 | }
|
---|
161 | return false;
|
---|
162 | }
|
---|
163 |
|
---|
164 | Expression * FixIntrinsicResult::postmutate( ApplicationExpr * appExpr ) {
|
---|
165 | if ( isIntrinsicReference( appExpr ) ) {
|
---|
166 | // eliminate reference types from intrinsic applications - now they return lvalues
|
---|
167 | ReferenceType * result = strict_dynamic_cast< ReferenceType * >( appExpr->result );
|
---|
168 | appExpr->result = result->base->clone();
|
---|
169 | appExpr->result->set_lvalue( true );
|
---|
170 | if ( ! inIntrinsic ) {
|
---|
171 | // when not in an intrinsic function, add a cast to
|
---|
172 | // don't add cast when in an intrinsic function, since they already have the cast
|
---|
173 | Expression * ret = new CastExpr( appExpr, result );
|
---|
174 | std::swap( ret->env, appExpr->env );
|
---|
175 | return ret;
|
---|
176 | }
|
---|
177 | delete result;
|
---|
178 | }
|
---|
179 | return appExpr;
|
---|
180 | }
|
---|
181 |
|
---|
182 | void FixIntrinsicResult::premutate( FunctionDecl * funcDecl ) {
|
---|
183 | GuardValue( inIntrinsic );
|
---|
184 | inIntrinsic = funcDecl->linkage == LinkageSpec::Intrinsic;
|
---|
185 | }
|
---|
186 |
|
---|
187 | Expression * FixIntrinsicArgs::postmutate( ApplicationExpr * appExpr ) {
|
---|
188 | // intrinsic functions don't really take reference-typed parameters, so they require an implicit dereference on their arguments.
|
---|
189 | if ( DeclarationWithType * function = InitTweak::getFunction( appExpr ) ) {
|
---|
190 | FunctionType * ftype = GenPoly::getFunctionType( function->get_type() );
|
---|
191 | assertf( ftype, "Function declaration does not have function type." );
|
---|
192 | // can be of differing lengths only when function is variadic
|
---|
193 | assertf( ftype->parameters.size() == appExpr->args.size() || ftype->isVarArgs, "ApplicationExpr args do not match formal parameter type." );
|
---|
194 |
|
---|
195 |
|
---|
196 | unsigned int i = 0;
|
---|
197 | const unsigned int end = ftype->parameters.size();
|
---|
198 | for ( auto p : unsafe_group_iterate( appExpr->args, ftype->parameters ) ) {
|
---|
199 | if (i == end) break;
|
---|
200 | Expression *& arg = std::get<0>( p );
|
---|
201 | Type * formal = std::get<1>( p )->get_type();
|
---|
202 | PRINT(
|
---|
203 | std::cerr << "pair<0>: " << arg << std::endl;
|
---|
204 | std::cerr << " -- " << arg->result << std::endl;
|
---|
205 | std::cerr << "pair<1>: " << formal << std::endl;
|
---|
206 | )
|
---|
207 | if ( dynamic_cast<ReferenceType*>( formal ) ) {
|
---|
208 | PRINT(
|
---|
209 | std::cerr << "===formal is reference" << std::endl;
|
---|
210 | )
|
---|
211 | // TODO: it's likely that the second condition should be ... && ! isIntrinsicReference( arg ), but this requires investigation.
|
---|
212 |
|
---|
213 | if ( function->linkage != LinkageSpec::Intrinsic && isIntrinsicReference( arg ) ) {
|
---|
214 | // needed for definition of prelude functions, etc.
|
---|
215 | // if argument is dereference or array subscript, the result isn't REALLY a reference, but non-intrinsic functions expect a reference: take address
|
---|
216 |
|
---|
217 | // NOTE: previously, this condition fixed
|
---|
218 | // void f(int *&);
|
---|
219 | // int & x = ...;
|
---|
220 | // f(&x);
|
---|
221 | // But now this is taken care of by a reference cast added by AddrRef. Need to find a new
|
---|
222 | // example or remove this branch.
|
---|
223 |
|
---|
224 | PRINT(
|
---|
225 | std::cerr << "===is intrinsic arg in non-intrinsic call - adding address" << std::endl;
|
---|
226 | )
|
---|
227 | arg = new AddressExpr( arg );
|
---|
228 | // } else if ( function->get_linkage() == LinkageSpec::Intrinsic && InitTweak::getPointerBase( arg->result ) ) {
|
---|
229 | } else if ( function->linkage == LinkageSpec::Intrinsic && arg->result->referenceDepth() != 0 ) {
|
---|
230 | // argument is a 'real' reference, but function expects a C lvalue: add a dereference to the reference-typed argument
|
---|
231 | PRINT(
|
---|
232 | std::cerr << "===is non-intrinsic arg in intrinsic call - adding deref to arg" << std::endl;
|
---|
233 | )
|
---|
234 | Type * baseType = InitTweak::getPointerBase( arg->result );
|
---|
235 | assertf( baseType, "parameter is reference, arg must be pointer or reference: %s", toString( arg->result ).c_str() );
|
---|
236 | PointerType * ptrType = new PointerType( Type::Qualifiers(), baseType->clone() );
|
---|
237 | delete arg->result;
|
---|
238 | arg->result = ptrType;
|
---|
239 | arg = mkDeref( arg );
|
---|
240 | // assertf( arg->result->referenceDepth() == 0, "Reference types should have been eliminated from intrinsic function calls, but weren't: %s", toCString( arg->result ) );
|
---|
241 | }
|
---|
242 | }
|
---|
243 | ++i;
|
---|
244 | }
|
---|
245 | }
|
---|
246 | return appExpr;
|
---|
247 | }
|
---|
248 |
|
---|
249 | // idea: &&&E: get outer &, inner &
|
---|
250 | // at inner &, record depth D of reference type of argument of &
|
---|
251 | // at outer &, add D derefs.
|
---|
252 | void AddrRef::handleNonAddr( Expression * ) {
|
---|
253 | // non-address-of: reset status variables:
|
---|
254 | // * current expr is NOT the first address-of expr in an address-of chain
|
---|
255 | // * next seen address-of expr IS the first in the chain.
|
---|
256 | GuardValue( current );
|
---|
257 | GuardValue( first );
|
---|
258 | current = false;
|
---|
259 | first = true;
|
---|
260 | }
|
---|
261 |
|
---|
262 | void AddrRef::premutate( Expression * expr ) {
|
---|
263 | handleNonAddr( expr );
|
---|
264 | GuardValue( addCast );
|
---|
265 | addCast = false;
|
---|
266 | }
|
---|
267 |
|
---|
268 | void AddrRef::premutate( AddressExpr * ) {
|
---|
269 | GuardValue( current );
|
---|
270 | GuardValue( first );
|
---|
271 | current = first; // is this the first address-of in the chain?
|
---|
272 | first = false; // from here out, no longer possible for next address-of to be first in chain
|
---|
273 | if ( current ) { // this is the outermost address-of in a chain
|
---|
274 | GuardValue( refDepth );
|
---|
275 | refDepth = 0; // set depth to 0 so that postmutate can find the innermost address-of easily
|
---|
276 | }
|
---|
277 | }
|
---|
278 |
|
---|
279 | Expression * AddrRef::postmutate( AddressExpr * addrExpr ) {
|
---|
280 | PRINT( std::cerr << "addr ref at " << addrExpr << std::endl; )
|
---|
281 | if ( refDepth == 0 ) {
|
---|
282 | PRINT( std::cerr << "depth 0, get new depth..." << std::endl; )
|
---|
283 | // this is the innermost address-of in a chain, record depth D
|
---|
284 | if ( ! isIntrinsicReference( addrExpr->arg ) ) {
|
---|
285 | // try to avoid ?[?]
|
---|
286 | // xxx - is this condition still necessary? intrinsicReferences should have a cast around them at this point, so I don't think this condition ever fires.
|
---|
287 | refDepth = addrExpr->arg->result->referenceDepth();
|
---|
288 | PRINT( std::cerr << "arg not intrinsic reference, new depth is: " << refDepth << std::endl; )
|
---|
289 | } else {
|
---|
290 | assertf( false, "AddrRef : address-of should not have intrinsic reference argument: %s", toCString( addrExpr->arg ) );
|
---|
291 | }
|
---|
292 | }
|
---|
293 | if ( current ) { // this is the outermost address-of in a chain
|
---|
294 | PRINT( std::cerr << "current, depth is: " << refDepth << std::endl; )
|
---|
295 | Expression * ret = addrExpr;
|
---|
296 | while ( refDepth ) {
|
---|
297 | // add one dereference for each
|
---|
298 | ret = mkDeref( ret );
|
---|
299 | refDepth--;
|
---|
300 | }
|
---|
301 |
|
---|
302 | // if addrExpr depth is 0, then the result is a pointer because the arg was depth 1 and not lvalue.
|
---|
303 | // This means the dereference result is not a reference, is lvalue, and one less pointer depth than
|
---|
304 | // the addrExpr. Thus the cast is meaningless.
|
---|
305 | // TODO: One thing to double check is whether it is possible for the types to differ outside of the single
|
---|
306 | // pointer level (i.e. can the base type of addrExpr differ from the type of addrExpr-arg?).
|
---|
307 | // If so then the cast might need to be added, conditional on a more sophisticated check.
|
---|
308 | if ( addCast && addrExpr->result->referenceDepth() != 0 ) {
|
---|
309 | PRINT( std::cerr << "adding cast to " << addrExpr->result << std::endl; )
|
---|
310 | return new CastExpr( ret, addrExpr->result->clone() );
|
---|
311 | }
|
---|
312 | return ret;
|
---|
313 | }
|
---|
314 | PRINT( std::cerr << "not current..." << std::endl; )
|
---|
315 | return addrExpr;
|
---|
316 | }
|
---|
317 |
|
---|
318 | void AddrRef::premutate( ApplicationExpr * appExpr ) {
|
---|
319 | visit_children = false;
|
---|
320 | GuardValue( addCast );
|
---|
321 | handleNonAddr( appExpr );
|
---|
322 | for ( Expression *& arg : appExpr->args ) {
|
---|
323 | // each argument with address-of requires a cast
|
---|
324 | addCast = true;
|
---|
325 | arg = arg->acceptMutator( *visitor );
|
---|
326 | }
|
---|
327 | }
|
---|
328 |
|
---|
329 | void AddrRef::premutate( SingleInit * ) {
|
---|
330 | GuardValue( addCast );
|
---|
331 | // each initialization context with address-of requires a cast
|
---|
332 | addCast = true;
|
---|
333 | }
|
---|
334 |
|
---|
335 |
|
---|
336 | Expression * ReferenceConversions::postmutate( AddressExpr * addrExpr ) {
|
---|
337 | // Inner expression may have been lvalue to reference conversion, which becomes an address expression.
|
---|
338 | // In this case, remove the outer address expression and return the argument.
|
---|
339 | // TODO: It's possible that this might catch too much and require a more sophisticated check.
|
---|
340 | return addrExpr;
|
---|
341 | }
|
---|
342 |
|
---|
343 | Expression * ReferenceConversions::postmutate( CastExpr * castExpr ) {
|
---|
344 | // xxx - is it possible to convert directly between reference types with a different base? E.g.,
|
---|
345 | // int x;
|
---|
346 | // (double&)x;
|
---|
347 | // At the moment, I am working off of the assumption that this is illegal, thus the cast becomes redundant
|
---|
348 | // after this pass, so trash the cast altogether. If that changes, care must be taken to insert the correct
|
---|
349 | // pointer casts in the right places.
|
---|
350 |
|
---|
351 | // Note: reference depth difference is the determining factor in what code is run, rather than whether something is
|
---|
352 | // reference type or not, since conversion still needs to occur when both types are references that differ in depth.
|
---|
353 |
|
---|
354 | Type * destType = castExpr->result;
|
---|
355 | Type * srcType = castExpr->arg->result;
|
---|
356 | assertf( destType, "Cast to no type in: %s", toCString( castExpr ) );
|
---|
357 | assertf( srcType, "Cast from no type in: %s", toCString( castExpr ) );
|
---|
358 | int depth1 = destType->referenceDepth();
|
---|
359 | int depth2 = srcType->referenceDepth();
|
---|
360 | int diff = depth1 - depth2;
|
---|
361 |
|
---|
362 | if ( diff > 0 && ! srcType->get_lvalue() ) {
|
---|
363 | // rvalue to reference conversion -- introduce temporary
|
---|
364 | // know that reference depth of cast argument is 0, need to introduce n temporaries for reference depth of n, e.g.
|
---|
365 | // (int &&&)3;
|
---|
366 | // becomes
|
---|
367 | // int __ref_tmp_0 = 3;
|
---|
368 | // int & __ref_tmp_1 = _&_ref_tmp_0;
|
---|
369 | // int && __ref_tmp_2 = &__ref_tmp_1;
|
---|
370 | // &__ref_tmp_2;
|
---|
371 | // the last & comes from the remaining reference conversion code
|
---|
372 | SemanticWarning( castExpr->arg->location, Warning::RvalueToReferenceConversion, toCString( castExpr->arg ) );
|
---|
373 |
|
---|
374 | static UniqueName tempNamer( "__ref_tmp_" );
|
---|
375 | ObjectDecl * temp = ObjectDecl::newObject( tempNamer.newName(), castExpr->arg->result->clone(), new SingleInit( castExpr->arg ) );
|
---|
376 | PRINT( std::cerr << "made temp: " << temp << std::endl; )
|
---|
377 | stmtsToAddBefore.push_back( new DeclStmt( temp ) );
|
---|
378 | for ( int i = 0; i < depth1-1; i++ ) { // xxx - maybe this should be diff-1? check how this works with reference type for srcType
|
---|
379 | ObjectDecl * newTemp = ObjectDecl::newObject( tempNamer.newName(), new ReferenceType( Type::Qualifiers(), temp->type->clone() ), new SingleInit( new AddressExpr( new VariableExpr( temp ) ) ) );
|
---|
380 | PRINT( std::cerr << "made temp" << i << ": " << newTemp << std::endl; )
|
---|
381 | stmtsToAddBefore.push_back( new DeclStmt( newTemp ) );
|
---|
382 | temp = newTemp;
|
---|
383 | }
|
---|
384 | // update diff so that remaining code works out correctly
|
---|
385 | castExpr->arg = new VariableExpr( temp );
|
---|
386 | PRINT( std::cerr << "update cast to: " << castExpr << std::endl; )
|
---|
387 | srcType = castExpr->arg->result;
|
---|
388 | depth2 = srcType->referenceDepth();
|
---|
389 | diff = depth1 - depth2;
|
---|
390 | assert( diff == 1 );
|
---|
391 | }
|
---|
392 |
|
---|
393 | // handle conversion between different depths
|
---|
394 | PRINT (
|
---|
395 | if ( depth1 || depth2 ) {
|
---|
396 | std::cerr << "destType: " << destType << " / srcType: " << srcType << std::endl;
|
---|
397 | std::cerr << "depth: " << depth1 << " / " << depth2 << std::endl;
|
---|
398 | }
|
---|
399 | )
|
---|
400 | if ( diff > 0 ) {
|
---|
401 | // conversion to type with more depth (e.g. int & -> int &&): add address-of for each level of difference
|
---|
402 | Expression * ret = castExpr->arg;
|
---|
403 | for ( int i = 0; i < diff; ++i ) {
|
---|
404 | ret = new AddressExpr( ret );
|
---|
405 | }
|
---|
406 | if ( srcType->get_lvalue() && ! ResolvExpr::typesCompatible( srcType, strict_dynamic_cast<ReferenceType *>( destType )->base, SymTab::Indexer() ) ) {
|
---|
407 | // must keep cast if cast-to type is different from the actual type
|
---|
408 | castExpr->arg = ret;
|
---|
409 | return castExpr;
|
---|
410 | }
|
---|
411 | ret->env = castExpr->env;
|
---|
412 | delete ret->result;
|
---|
413 | ret->result = castExpr->result;
|
---|
414 | castExpr->env = nullptr;
|
---|
415 | castExpr->arg = nullptr;
|
---|
416 | castExpr->result = nullptr;
|
---|
417 | delete castExpr;
|
---|
418 | return ret;
|
---|
419 | } else if ( diff < 0 ) {
|
---|
420 | // conversion to type with less depth (e.g. int && -> int &): add dereferences for each level of difference
|
---|
421 | diff = -diff; // care only about magnitude now
|
---|
422 | Expression * ret = castExpr->arg;
|
---|
423 | for ( int i = 0; i < diff; ++i ) {
|
---|
424 | ret = mkDeref( ret );
|
---|
425 | // xxx - try removing one reference here? actually, looks like mkDeref already does this, so more closely look at the types generated.
|
---|
426 | }
|
---|
427 | if ( ! ResolvExpr::typesCompatibleIgnoreQualifiers( destType->stripReferences(), srcType->stripReferences(), SymTab::Indexer() ) ) {
|
---|
428 | // must keep cast if types are different
|
---|
429 | castExpr->arg = ret;
|
---|
430 | return castExpr;
|
---|
431 | }
|
---|
432 | ret->env = castExpr->env;
|
---|
433 | delete ret->result;
|
---|
434 | ret->result = castExpr->result;
|
---|
435 | ret->result->set_lvalue( true ); // ensure result is lvalue
|
---|
436 | castExpr->env = nullptr;
|
---|
437 | castExpr->arg = nullptr;
|
---|
438 | castExpr->result = nullptr;
|
---|
439 | delete castExpr;
|
---|
440 | return ret;
|
---|
441 | } else {
|
---|
442 | assert( diff == 0 );
|
---|
443 | // conversion between references of the same depth
|
---|
444 | if ( ResolvExpr::typesCompatible( castExpr->result, castExpr->arg->result, SymTab::Indexer() ) && castExpr->isGenerated ) {
|
---|
445 | // Remove useless generated casts
|
---|
446 | PRINT(
|
---|
447 | std::cerr << "types are compatible, removing cast: " << castExpr << std::endl;
|
---|
448 | std::cerr << "-- " << castExpr->result << std::endl;
|
---|
449 | std::cerr << "-- " << castExpr->arg->result << std::endl;
|
---|
450 | )
|
---|
451 | Expression * ret = castExpr->arg;
|
---|
452 | castExpr->arg = nullptr;
|
---|
453 | std::swap( castExpr->env, ret->env );
|
---|
454 | delete castExpr;
|
---|
455 | return ret;
|
---|
456 | }
|
---|
457 | return castExpr;
|
---|
458 | }
|
---|
459 | }
|
---|
460 |
|
---|
461 | Type * ReferenceTypeElimination::postmutate( ReferenceType * refType ) {
|
---|
462 | Type * base = refType->base;
|
---|
463 | Type::Qualifiers qualifiers = refType->get_qualifiers();
|
---|
464 | refType->base = nullptr;
|
---|
465 | delete refType;
|
---|
466 | return new PointerType( qualifiers, base );
|
---|
467 | }
|
---|
468 |
|
---|
469 | template<typename Func>
|
---|
470 | Expression * GeneralizedLvalue::applyTransformation( Expression * expr, Expression * arg, Func mkExpr ) {
|
---|
471 | if ( CommaExpr * commaExpr = dynamic_cast< CommaExpr * >( arg ) ) {
|
---|
472 | Expression * arg1 = commaExpr->arg1->clone();
|
---|
473 | Expression * arg2 = commaExpr->arg2->clone();
|
---|
474 | Expression * ret = new CommaExpr( arg1, mkExpr( arg2 )->acceptMutator( *visitor ) );
|
---|
475 | ret->env = expr->env;
|
---|
476 | expr->env = nullptr;
|
---|
477 | delete expr;
|
---|
478 | return ret;
|
---|
479 | } else if ( ConditionalExpr * condExpr = dynamic_cast< ConditionalExpr * >( arg ) ) {
|
---|
480 | Expression * arg1 = condExpr->arg1->clone();
|
---|
481 | Expression * arg2 = condExpr->arg2->clone();
|
---|
482 | Expression * arg3 = condExpr->arg3->clone();
|
---|
483 | ConditionalExpr * ret = new ConditionalExpr( arg1, mkExpr( arg2 )->acceptMutator( *visitor ), mkExpr( arg3 )->acceptMutator( *visitor ) );
|
---|
484 | ret->env = expr->env;
|
---|
485 | expr->env = nullptr;
|
---|
486 | delete expr;
|
---|
487 |
|
---|
488 | // conditional expr type may not be either of the argument types, need to unify
|
---|
489 | using namespace ResolvExpr;
|
---|
490 | Type* commonType = nullptr;
|
---|
491 | TypeEnvironment newEnv;
|
---|
492 | AssertionSet needAssertions, haveAssertions;
|
---|
493 | OpenVarSet openVars;
|
---|
494 | unify( ret->arg2->result, ret->arg3->result, newEnv, needAssertions, haveAssertions, openVars, SymTab::Indexer(), commonType );
|
---|
495 | ret->result = commonType ? commonType : ret->arg2->result->clone();
|
---|
496 | return ret;
|
---|
497 | }
|
---|
498 | return expr;
|
---|
499 | }
|
---|
500 |
|
---|
501 | Expression * GeneralizedLvalue::postmutate( MemberExpr * memExpr ) {
|
---|
502 | return applyTransformation( memExpr, memExpr->aggregate, [=]( Expression * aggr ) { return new MemberExpr( memExpr->member, aggr ); } );
|
---|
503 | }
|
---|
504 |
|
---|
505 | Expression * GeneralizedLvalue::postmutate( AddressExpr * addrExpr ) {
|
---|
506 | return applyTransformation( addrExpr, addrExpr->arg, []( Expression * arg ) { return new AddressExpr( arg ); } );
|
---|
507 | }
|
---|
508 |
|
---|
509 | Expression * CollapseAddrDeref::postmutate( AddressExpr * addrExpr ) {
|
---|
510 | Expression * arg = addrExpr->arg;
|
---|
511 | if ( isIntrinsicReference( arg ) ) {
|
---|
512 | std::string fname = InitTweak::getFunctionName( arg );
|
---|
513 | if ( fname == "*?" ) {
|
---|
514 | Expression *& arg0 = InitTweak::getCallArg( arg, 0 );
|
---|
515 | Expression * ret = arg0;
|
---|
516 | ret->set_env( addrExpr->env );
|
---|
517 | arg0 = nullptr;
|
---|
518 | addrExpr->env = nullptr;
|
---|
519 | delete addrExpr;
|
---|
520 | return ret;
|
---|
521 | }
|
---|
522 | } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * > ( arg ) ) {
|
---|
523 | // need to move cast to pointer type out a level since address of pointer
|
---|
524 | // is not valid C code (can be introduced in prior passes, e.g., InstantiateGeneric)
|
---|
525 | if ( InitTweak::getPointerBase( castExpr->result ) ) {
|
---|
526 | addrExpr->arg = castExpr->arg;
|
---|
527 | castExpr->arg = addrExpr;
|
---|
528 | castExpr->result = new PointerType( Type::Qualifiers(), castExpr->result );
|
---|
529 | return castExpr;
|
---|
530 | }
|
---|
531 | }
|
---|
532 | return addrExpr;
|
---|
533 | }
|
---|
534 |
|
---|
535 | Expression * CollapseAddrDeref::postmutate( ApplicationExpr * appExpr ) {
|
---|
536 | if ( isIntrinsicReference( appExpr ) ) {
|
---|
537 | std::string fname = InitTweak::getFunctionName( appExpr );
|
---|
538 | if ( fname == "*?" ) {
|
---|
539 | Expression * arg = InitTweak::getCallArg( appExpr, 0 );
|
---|
540 | // xxx - this isn't right, because it can remove casts that should be there...
|
---|
541 | // while ( CastExpr * castExpr = dynamic_cast< CastExpr * >( arg ) ) {
|
---|
542 | // arg = castExpr->get_arg();
|
---|
543 | // }
|
---|
544 | if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( arg ) ) {
|
---|
545 | Expression * ret = addrExpr->arg;
|
---|
546 | ret->env = appExpr->env;
|
---|
547 | addrExpr->arg = nullptr;
|
---|
548 | appExpr->env = nullptr;
|
---|
549 | delete appExpr;
|
---|
550 | return ret;
|
---|
551 | }
|
---|
552 | }
|
---|
553 | }
|
---|
554 | return appExpr;
|
---|
555 | }
|
---|
556 | } // namespace
|
---|
557 | } // namespace GenPoly
|
---|
558 |
|
---|
559 | // Local Variables: //
|
---|
560 | // tab-width: 4 //
|
---|
561 | // mode: c++ //
|
---|
562 | // compile-command: "make install" //
|
---|
563 | // End: //
|
---|