source: src/GenPoly/Lvalue.cc@ 120867e

ADT ast-experimental pthread-emulation qualifiedEnum
Last change on this file since 120867e was 9939dc3, checked in by Andrew Beach <ajbeach@…>, 3 years ago

Reduced the number of object files linked into the demangler. Some of the divisions are rather odd, Lvalue2 and FixMain2, but they should be a better base to work from. Also improved the calling of the impurity detector visitors slightly.

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