source: src/GenPoly/Lvalue.cc@ 463cb33

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 463cb33 was 07de76b, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

remove file TypeVar.h* and put TypeVar::Kind into TypeDecl, move LinkageSpec.* from directory Parse to SynTree

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