source: src/GenPoly/Lvalue.cc@ baf608a

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since baf608a was 78cdb06, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Modified Lvalue cast handling to ignore top level dereferencing in AsmExpr, also added test case for this fixes #152

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