source: src/GenPoly/Lvalue.cpp@ fc276f3

Last change on this file since fc276f3 was bdf40650, checked in by Andrew Beach <ajbeach@…>, 10 months ago

Address-of is now moved under casts of any type and is done so recursively. Solves trac#166 and a modified version of the code there has been added as a test.

  • Property mode set to 100644
File size: 23.6 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.cpp -- Clean up lvalues and remove references.
8//
9// Author : Andrew Beach
10// Created On : Thu Sep 15 14:08:00 2022
11// Last Modified By : Andrew Beach
12// Last Modified On : Mon Aug 12 18:07:00 2024
13// Update Count : 1
14//
15
16#include "Lvalue.hpp"
17
18#include <set>
19#include <iostream>
20
21#include "AST/Copy.hpp" // for deepCopy
22#include "AST/Expr.hpp"
23#include "AST/Inspect.hpp"
24#include "AST/LinkageSpec.hpp" // for Linkage
25#include "AST/Pass.hpp"
26#include "Common/SemanticError.hpp" // for SemanticWarning
27#include "Common/ToString.hpp" // for toCString
28#include "Common/UniqueName.hpp" // for UniqueName
29#include "GenPoly/GenPoly.hpp" // for genFunctionType
30#include "ResolvExpr/Typeops.hpp" // for typesCompatible
31#include "ResolvExpr/Unify.hpp" // for unify
32
33#if 0
34#define PRINT(x) x
35#else
36#define PRINT(x)
37#endif
38
39namespace GenPoly {
40
41namespace {
42
43/// Intrinsic functions that return references now instead return lvalues.
44struct FixIntrinsicResults final : public ast::WithGuards {
45 enum {
46 NoSkip,
47 Skip,
48 SkipInProgress,
49 } skip = NoSkip;
50
51 void previsit( ast::AsmExpr const * ) {
52 GuardValue( skip ) = Skip;
53 }
54 void previsit( ast::ApplicationExpr const * ) {
55 GuardValue( skip ) = (skip == Skip) ? SkipInProgress : NoSkip;
56 }
57
58 ast::Expr const * postvisit( ast::ApplicationExpr const * expr );
59 void previsit( ast::FunctionDecl const * decl );
60 bool inIntrinsic = false;
61};
62
63/// Add de-references around address-of operations on reference types.
64struct AddressRef final :
65 public ast::WithConstTranslationUnit,
66 public ast::WithGuards,
67 public ast::WithShortCircuiting,
68 public ast::WithVisitorRef<AddressRef> {
69 void previsit( ast::AddressExpr const * expr );
70 ast::Expr const * postvisit( ast::AddressExpr const * expr );
71 void previsit( ast::Expr const * expr );
72 ast::ApplicationExpr const * previsit( ast::ApplicationExpr const * expr );
73 void previsit( ast::SingleInit const * init );
74
75 void handleNonAddr( ast::Expr const * expr );
76
77 bool first = true;
78 bool current = false;
79 bool addCast = false;
80 int refDepth = 0;
81};
82
83/// Handles casts between references and pointers,
84/// creating temporaries for the conversion.
85struct ReferenceConversions final :
86 public ast::WithConstTranslationUnit,
87 public ast::WithGuards, public ast::WithStmtsToAdd {
88 ast::Expr const * postvisit( ast::CastExpr const * expr );
89 ast::Expr const * postvisit( ast::AddressExpr const * expr );
90};
91
92/// Intrinsic functions that take reference parameters don't actually do.
93/// Their reference arguments must be implicity dereferenced.
94/// TODO Also appears to contain redundent code with AddressRef
95struct FixIntrinsicArgs final :
96 public ast::WithConstTranslationUnit {
97 ast::Expr const * postvisit( ast::ApplicationExpr const * expr );
98};
99
100/// Removes redundant &* / *& patterns that may be generated.
101struct CollapseAddressDeref final {
102 ast::Expr const * postvisit( ast::AddressExpr const * expr );
103 ast::Expr const * postvisit( ast::ApplicationExpr const * expr );
104};
105
106/// GCC-like Generalized Lvalues (which have since been removed from GCC).
107/// https://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Lvalues.html#Lvalues
108/// Replaces &(a,b) with (a, &b), &(a ? b : c) with (a ? &b : &c)
109struct GeneralizedLvalue final :
110 public ast::WithVisitorRef<GeneralizedLvalue> {
111 ast::Expr const * postvisit( ast::AddressExpr const * expr );
112 ast::Expr const * postvisit( ast::MemberExpr const * expr );
113
114 template<typename Node, typename Func>
115 ast::Expr const * applyTransformation(
116 Node const * expr, ast::ptr<ast::Expr> Node::*field, Func mkExpr );
117};
118
119/// Replace all reference types with pointer types.
120struct ReferenceTypeElimination final {
121 ast::SizeofExpr const * previsit( ast::SizeofExpr const * expr );
122 ast::AlignofExpr const * previsit( ast::AlignofExpr const * expr );
123 ast::Type const * postvisit( ast::ReferenceType const * type );
124};
125
126/// True for intrinsic function calls that return an lvalue in C.
127bool isIntrinsicReference( ast::Expr const * expr ) {
128 // The known intrinsic-reference prelude functions.
129 static std::set<std::string> const lvalueFunctions = { "*?", "?[?]" };
130 if ( auto untyped = dynamic_cast<ast::UntypedExpr const *>( expr ) ) {
131 std::string fname = ast::getFunctionName( untyped );
132 return lvalueFunctions.count( fname );
133 } else if ( auto app = dynamic_cast<ast::ApplicationExpr const *>( expr ) ) {
134 if ( auto func = ast::getFunction( app ) ) {
135 return func->linkage == ast::Linkage::Intrinsic
136 && lvalueFunctions.count( func->name );
137 }
138 }
139 return false;
140}
141
142// A maybe typed variant of the createDeref function (only UntypedExpr).
143ast::Expr * mkDeref(
144 ast::TranslationGlobal const & global, ast::Expr const * arg ) {
145 if ( global.dereference ) {
146 // Note: Reference depth can be arbitrarily deep here,
147 // so peel off the outermost pointer/reference, not just
148 // pointer because they are effecitvely equivalent in this pass
149 ast::VariableExpr * deref = new ast::VariableExpr(
150 arg->location, global.dereference );
151 deref->result = new ast::PointerType( deref->result );
152 ast::Type const * base = ast::getPointerBase( arg->result );
153 assertf( base, "expected pointer type in dereference (type was %s)", toString( arg->result ).c_str() );
154 ast::ApplicationExpr * ret =
155 new ast::ApplicationExpr( arg->location, deref, { arg } );
156 ret->result = ast::deepCopy( base );
157 return ret;
158 } else {
159 return ast::UntypedExpr::createDeref( arg->location, arg );
160 }
161}
162
163ast::Expr const * FixIntrinsicResults::postvisit(
164 ast::ApplicationExpr const * expr ) {
165
166 if ( skip == SkipInProgress || !isIntrinsicReference( expr ) ) {
167 return expr;
168 }
169 // Eliminate reference types from intrinsic applications
170 // now they return lvalues.
171 ast::ptr<ast::ReferenceType> result =
172 expr->result.strict_as<ast::ReferenceType>();
173 expr = ast::mutate_field( expr, &ast::ApplicationExpr::result,
174 ast::deepCopy( result->base ) );
175 if ( inIntrinsic ) {
176 return expr;
177 }
178 // When not in an intrinsic function, add a cast to don't add cast when
179 // in an intrinsic function, since they already have the cast.
180 auto * ret = new ast::CastExpr( expr->location, expr, result.get() );
181 ret->env = expr->env;
182 return ret;
183}
184
185void FixIntrinsicResults::previsit( ast::FunctionDecl const * decl ) {
186 GuardValue( inIntrinsic ) = decl->linkage == ast::Linkage::Intrinsic;
187}
188
189void AddressRef::previsit( ast::AddressExpr const * ) {
190 // Is this the first address-of in the chain?
191 GuardValue( current ) = first;
192 // Later references will not be for next address-of to be first in chain.
193 GuardValue( first ) = false;
194 // If is the outermost address-of in a chain:
195 if ( current ) {
196 // Set depth to 0 so that postvisit can
197 // find the innermost address-of easily.
198 GuardValue( refDepth ) = 0;
199 }
200}
201
202ast::Expr const * AddressRef::postvisit( ast::AddressExpr const * expr ) {
203 PRINT( std::cerr << "addr ref at " << expr << std::endl; )
204 if ( 0 == refDepth ) {
205 PRINT( std::cerr << "depth 0, get new depth..." << std::endl; )
206 // Is this the innermost address-of in a chain? record depth D.
207 if ( isIntrinsicReference( expr->arg ) ) {
208 assertf( false, "AddrRef : address-of should not have intrinsic reference argument: %s", toCString( expr->arg ) );
209 } else {
210 // try to avoid ?[?]
211 // TODO is this condition still necessary? intrinsicReferences
212 // should have a cast around them at this point, so I don't think
213 // this condition ever fires.
214 refDepth = expr->arg->result->referenceDepth();
215 PRINT( std::cerr << "arg not intrinsic reference, new depth is: " << refDepth << std::endl; )
216 }
217 }
218 if ( current ) {
219 PRINT( std::cerr << "current, depth is: " << refDepth << std::endl; )
220 ast::Expr const * ret = expr;
221 while ( refDepth ) {
222 // Add one dereference for each address-of in the chain.
223 ret = mkDeref( transUnit().global, ret );
224 --refDepth;
225 }
226
227 // if addrExpr depth is 0, then the result is a pointer because the
228 // arg was depth 1 and not lvalue. This means the dereference result
229 // is not a reference, is lvalue, and one less pointer depth than the
230 // addrExpr. Thus the cast is meaningless.
231 // TODO: One thing to double check is whether it is possible for the
232 // types to differ outside of the single pointer level (i.e. can the
233 // base type of addrExpr differ from the type of addrExpr-arg?). If
234 // so then the cast might need to be added, conditional on a more
235 // sophisticated check.
236 if ( addCast && 0 != expr->result->referenceDepth() ) {
237 PRINT( std::cerr << "adding cast to " << expr->result << std::endl; )
238 return new ast::CastExpr( expr->location,
239 ret, ast::deepCopy( expr->result ) );
240 }
241 return ret;
242 }
243 PRINT( std::cerr << "not current..." << std::endl; )
244 return expr;
245}
246
247void AddressRef::previsit( ast::Expr const * expr ) {
248 handleNonAddr( expr );
249 GuardValue( addCast ) = false;
250}
251
252// So we want to skip traversing to the head?
253ast::ApplicationExpr const * AddressRef::previsit(
254 ast::ApplicationExpr const * expr ) {
255 visit_children = false;
256 GuardValue( addCast );
257 handleNonAddr( expr );
258 auto mutExpr = ast::mutate( expr );
259 for ( ast::ptr<ast::Expr> & arg : mutExpr->args ) {
260 addCast = true;
261 arg = arg->accept( *visitor );
262 }
263 return mutExpr;
264}
265
266void AddressRef::previsit( ast::SingleInit const * ) {
267 // Each initialization context with address-of requires a cast.
268 GuardValue( addCast ) = true;
269}
270
271// idea: &&&E: get outer &, inner &
272// at inner &, record depth D of reference type of argument of &.
273// at auter &, add D derefs.
274void AddressRef::handleNonAddr( ast::Expr const * ) {
275 // non-address-of: reset status variables:
276 // * current expr is NOT the first address-of expr in an address-of chain.
277 // * next seen address-of expr IS the first in the chain.
278 GuardValue( current ) = false;
279 GuardValue( first ) = true;
280}
281
282ast::Expr const * ReferenceConversions::postvisit(
283 ast::CastExpr const * expr ) {
284 // TODO: Is it possible to convert directly between reference types with
285 // a different base. e.g.
286 // int x;
287 // (double&)x;
288 // At the moment, I (who?) am working off of the assumption that this is
289 // illegal, thus the cast becomes redundant after this pass, so trash the
290 // cast altogether. If that changes, care must be taken to insert the
291 // correct pointer casts in the right places.
292
293 // Note: reference depth difference is the determining factor in what
294 // code is run, rather than whether something is reference type or not,
295 // since conversion still needs to occur when both types are references
296 // that differ in depth.
297 ast::Type const * dstType = expr->result.get();
298 ast::Type const * srcType = expr->arg->result.get();
299 assertf( dstType, "Cast to no type in: %s", toCString( expr ) );
300 assertf( srcType, "Cast from no type in: %s", toCString( expr ) );
301 int dstDepth = dstType->referenceDepth();
302 int srcDepth = srcType->referenceDepth();
303 int diff = dstDepth - srcDepth;
304
305 if ( 0 < diff && !expr->arg->get_lvalue() ) {
306 // rvalue to reference conversion -- introduce temporary
307 // know that reference depth of cast argument is 0
308 // (int &&&)3;
309 // becomes
310 // int __ref_tmp_0 = 3;
311 // int & __ref_tmp_1 = &__ref_tmp_0;
312 // int && __ref_tmp_2 = &__ref_tmp_1;
313 // &__ref_tmp_2;
314 // The last & comes from the remaining reference conversion code.
315 SemanticWarning( expr->arg->location,
316 Warning::RvalueToReferenceConversion, toCString( expr->arg ) );
317
318 // allowing conversion in the rvalue to const ref case
319 // use the referenced-to type to create temp variables
320 ast::Type const * targetType = dstType;
321 for (int i = 0; i < diff; ++i) targetType = (strict_dynamic_cast<ast::ReferenceType const *>(targetType))->base;
322
323 static UniqueName tmpNamer( "__ref_tmp_" );
324 ast::ObjectDecl * tmp = new ast::ObjectDecl( expr->arg->location,
325 tmpNamer.newName(),
326 // ast::deepCopy( expr->arg->result ),
327 ast::deepCopy (targetType),
328 new ast::SingleInit( expr->arg->location, expr->arg ) );
329 PRINT( std::cerr << "make tmp: " << tmp << std::endl; )
330 stmtsToAddBefore.push_back( new ast::DeclStmt( tmp->location, tmp ) );
331 for ( int i = 0 ; i < dstDepth - 1 ; ++i ) {
332 ast::ObjectDecl * newTmp = new ast::ObjectDecl( tmp->location,
333 tmpNamer.newName(),
334 new ast::ReferenceType( ast::deepCopy( tmp->type ) ),
335 new ast::SingleInit( tmp->location,
336 new ast::AddressExpr( tmp->location,
337 new ast::VariableExpr( tmp->location, tmp ) ) ) );
338 PRINT( std::cerr << "make tmp: " << i << ": " << newTmp << std::endl; )
339 stmtsToAddBefore.push_back(
340 new ast::DeclStmt( newTmp->location, newTmp ) );
341 tmp = newTmp;
342 }
343 // Update diff so that remaining code works out correctly.
344 expr = ast::mutate_field( expr, &ast::CastExpr::arg,
345 new ast::VariableExpr( tmp->location, tmp ) );
346 PRINT( std::cerr << "update cast to: " << expr << std::endl; )
347 srcType = expr->arg->result;
348 srcDepth = srcType->referenceDepth();
349 diff = dstDepth - srcDepth;
350 assert( 1 == diff );
351 }
352
353 // Handle conversion between different depths.
354 PRINT(
355 if ( dstDepth || srcDepth ) {
356 std::cerr << "dstType: " << dstType << " / srcType: " << srcType << '\n';
357 std::cerr << "depth: " << dstDepth << " / " << srcDepth << std::endl;
358 }
359 )
360 // Conversion to type with more depth/more references.
361 // Add address-of for each level of difference.
362 if ( 0 < diff ) {
363 ast::Expr * ret = ast::mutate( expr->arg.get() );
364 for ( int i = 0 ; i < diff ; ++i ) {
365 ret = new ast::AddressExpr( ret->location, ret );
366 }
367 // Must keep cast if types are different.
368 if ( !ResolvExpr::typesCompatible(
369 srcType,
370 strict_dynamic_cast<ast::ReferenceType const *>( dstType )->base ) ) {
371 return ast::mutate_field( expr, &ast::CastExpr::arg, ret );
372 }
373 ret->env = expr->env;
374 ret->result = expr->result;
375 return ret;
376 // Conversion to type with less depth/fewer references.
377 // Add dereferences for each level of difference.
378 } else if ( diff < 0 ) {
379 ast::Expr * ret = ast::mutate( expr->arg.get() );
380 for ( int i = 0 ; i < -diff ; ++i ) {
381 ret = mkDeref( transUnit().global, ret );
382 }
383 // Must keep cast if types are different.
384 if ( !ResolvExpr::typesCompatible(
385 dstType->stripReferences(),
386 srcType->stripReferences() ) ) {
387 return ast::mutate_field( expr, &ast::CastExpr::arg, ret );
388 }
389 ret->env = expr->env;
390 ret->result = expr->result;
391 // The result must be an lvalue.
392 assert( ret->get_lvalue() );
393 return ret;
394 // Conversion with the same depth.
395 } else {
396 assert( 0 == diff );
397 // Must keep cast if types are different. (Or it is explicit.)
398 if ( ast::ExplicitCast == expr->isGenerated ||
399 !ResolvExpr::typesCompatible(
400 expr->result,
401 expr->arg->result ) ) {
402 return expr;
403 }
404 PRINT(
405 std::cerr << "types are compatible, removing cast: " << expr << '\n';
406 std::cerr << "-- " << expr->result << '\n';
407 std::cerr << "-- " << expr->arg->result << std::endl;
408 )
409 return ast::mutate_field( expr->arg.get(),
410 &ast::Expr::env, expr->env.get() );
411 }
412}
413
414ast::Expr const * ReferenceConversions::postvisit(
415 ast::AddressExpr const * expr ) {
416 // Inner expression may have been lvalue to reference conversion, which
417 // becomes an address expression. In this case, remove the outer address
418 // expression and return the argument.
419 // TODO: It's possible that this might catch too much and require a more
420 // sophisticated check. TODO What check are we talking about here?
421 return expr;
422}
423
424ast::Expr const * FixIntrinsicArgs::postvisit(
425 ast::ApplicationExpr const * expr ) {
426 // Intrinsic functions don't really take reference-typed parameters,
427 // so they require an implicit dereference on their arguments.
428 auto function = ast::getFunction( expr );
429 if ( function == nullptr ) {
430 return expr;
431 }
432
433 ast::FunctionType const * ftype = GenPoly::getFunctionType( function->get_type() );
434 assertf( ftype, "Function declaration does not have function type." );
435 // Can be of different lengths only when function is variadic.
436 assertf( ftype->params.size() == expr->args.size() || ftype->isVarArgs,
437 "ApplicationExpr args do not match formal parameter type." );
438 assertf( ftype->params.size() <= expr->args.size(),
439 "Cannot have more parameters than arguments." );
440
441 unsigned int i = 0;
442 unsigned int const end = ftype->params.size();
443
444 // This is used to make sure we get a zip on shortests.
445 if ( end == i ) return expr;
446
447 // This mutate could be redundent, but it is simpler this way.
448 auto mutExpr = ast::mutate( expr );
449
450 for ( auto pair : unsafe_group_iterate( mutExpr->args, ftype->params ) ) {
451 ast::ptr<ast::Expr> & arg = std::get<0>( pair );
452 ast::ptr<ast::Type> const & formal = std::get<1>( pair );
453 PRINT(
454 std::cerr << "pair<0>: " << arg.get() << std::endl;
455 std::cerr << " -- " << arg->result << std::endl;
456 std::cerr << "pair<1>: " << formal << std::endl;
457 )
458 //if ( dynamic_cast<ast::ReferenceType const *>( formal.get() ) ) {
459 if ( formal.as<ast::ReferenceType>() ) {
460 PRINT( std::cerr << "===formal is reference" << std::endl; )
461 // TODO: It's likely that the second condition should be
462 // `... && ! isIntrinsicReference( arg )`, but this requires
463 // investigation.
464
465 if ( ast::Linkage::Intrinsic != function->linkage
466 && isIntrinsicReference( arg ) ) {
467 // Needed for definition of prelude functions, etc.
468 // If argument is dereference or array subscript, the result
469 // isn't REALLY a reference, but non-intrinsic functions
470 // expect a reference: take address
471
472 // TODO: OK, so this should be cut?!
473 // NODE: Previously, this condition fixed
474 // void f(int *&);
475 // int & x = ...;
476 // f(&x);
477 // But now this is taken care of by a reference cast added by
478 // AddressRef. Need to find a new example or remove this
479 // branch.
480 PRINT(
481 std::cerr << "===is intrinsic arg in non-intrinsic call - adding address" << std::endl;
482 )
483 arg = new ast::AddressExpr( arg->location, arg );
484 } else if ( ast::Linkage::Intrinsic == function->linkage
485 && arg->result->referenceDepth() != 0 ) {
486 // Argument is a 'real' reference, but function expects a C
487 // lvalue: Add a dereference to the reference-typed argument.
488 PRINT(
489 std::cerr << "===is non-intrinsic arg in intrinsic call - adding deref to arg" << std::endl;
490 )
491 ast::Type const * base = ast::getPointerBase( arg->result );
492 assertf( base, "parameter is reference, arg must be pointer or reference: %s", toString( arg->result ).c_str() );
493 ast::PointerType * ptr = new ast::PointerType( ast::deepCopy( base ) );
494 arg = ast::mutate_field( arg.get(),
495 &ast::ApplicationExpr::result, ptr );
496 arg = mkDeref( transUnit().global, arg );
497 }
498 }
499 ++i;
500 if ( end == i ) break;
501 }
502 return mutExpr;
503}
504
505/// Recursively move an address expression underneath casts. Casts are not
506/// lvalue expressions in C but are sometimes considered as such in Cforall,
507/// (passes like InstantiateGeneric can add them.) - &(int) => (int*)&
508ast::Expr const * moveAddressUnderCast( ast::AddressExpr const * expr ) {
509 if ( !dynamic_cast<ast::CastExpr const *>( expr->arg.get() ) ) {
510 return expr;
511 }
512 auto mutExpr = ast::mutate( expr );
513 auto mutCast = strict_dynamic_cast<ast::CastExpr *>(
514 ast::mutate( mutExpr->arg.release() ) );
515 mutExpr->arg = mutCast->arg;
516 mutCast->arg = moveAddressUnderCast( mutExpr );
517 mutCast->result = new ast::PointerType( mutCast->result );
518 return mutCast;
519}
520
521ast::Expr const * CollapseAddressDeref::postvisit(
522 ast::AddressExpr const * expr ) {
523 ast::Expr const * arg = expr->arg;
524 if ( isIntrinsicReference( arg ) ) {
525 std::string fname = ast::getFunctionName( arg );
526 if ( fname == "*?" ) {
527 ast::Expr const * arg0 = ast::getCallArg( arg, 0 );
528 ast::Expr * ret = ast::mutate( arg0 );
529 ret->env = expr->env;
530 return ret;
531 }
532 } else {
533 return moveAddressUnderCast( expr );
534 }
535 return expr;
536}
537
538ast::Expr const * CollapseAddressDeref::postvisit(
539 ast::ApplicationExpr const * expr ) {
540 if ( isIntrinsicReference( expr ) ) {
541 std::string fname = ast::getFunctionName( expr );
542 if ( fname == "*?" ) {
543 assert( 1 == expr->args.size() );
544 ast::Expr const * arg = ast::getCallArg( expr, 0 );
545 // xxx - this isn't right, because it can remove casts that
546 // should be there...
547 // while ( auto cast = dynamic_cast< ast::CastExpr const * >( arg ) ) {
548 // arg = cast->arg;
549 // }
550 if ( auto addr = dynamic_cast<ast::AddressExpr const *>( arg ) ) {
551 return ast::mutate_field( addr->arg.get(),
552 &ast::Expr::env, expr->env.get() );
553 }
554 }
555 }
556 return expr;
557}
558
559ast::Expr const * GeneralizedLvalue::postvisit(
560 ast::AddressExpr const * expr ) {
561 return applyTransformation( expr, &ast::AddressExpr::arg,
562 []( ast::Expr const * arg ) {
563 return new ast::AddressExpr( arg->location, arg );
564 }
565 );
566}
567
568ast::Expr const * GeneralizedLvalue::postvisit(
569 ast::MemberExpr const * expr ) {
570 return applyTransformation( expr, &ast::MemberExpr::aggregate,
571 [expr]( ast::Expr const * aggr ) {
572 return new ast::MemberExpr( aggr->location, expr->member, aggr );
573 }
574 );
575}
576
577template<typename Node, typename Func>
578ast::Expr const * GeneralizedLvalue::applyTransformation(
579 Node const * expr, ast::ptr<ast::Expr> Node::*field, Func mkExpr ) {
580 ast::ptr<ast::Expr> const & arg = expr->*field;
581 if ( auto commaArg = arg.as<ast::CommaExpr>() ) {
582 ast::Expr const * arg1 = ast::deepCopy( commaArg->arg1 );
583 ast::Expr const * arg2 = ast::deepCopy( commaArg->arg2 );
584 ast::Expr const * ret = new ast::CommaExpr(
585 commaArg->location, arg1, mkExpr( arg2 )->accept( *visitor ) );
586 return ret;
587 } else if ( auto condArg = arg.as<ast::ConditionalExpr>() ) {
588 ast::Expr const * arg1 = ast::deepCopy( condArg->arg1 );
589 ast::Expr const * arg2 = ast::deepCopy( condArg->arg2 );
590 ast::Expr const * arg3 = ast::deepCopy( condArg->arg3 );
591 ast::ConditionalExpr * ret = new ast::ConditionalExpr(
592 condArg->location, arg1, mkExpr( arg2 )->accept( *visitor ),
593 mkExpr( arg3 )->accept( *visitor ) );
594
595 // Conditional expr type may not be either of the arguments,
596 // so unify to get the result.
597 // TODO: Maybe I could create a wrapper for this.
598 ast::ptr<ast::Type> common = nullptr;
599 ast::TypeEnvironment newEnv;
600 ast::AssertionSet needAssertions, haveAssertions;
601 ast::OpenVarSet openVars;
602 ResolvExpr::unify( ret->arg2->result, ret->arg3->result, newEnv,
603 needAssertions, haveAssertions, openVars, common );
604 ret->result = common ? common : ast::deepCopy( ret->arg2->result );
605 return ret;
606 }
607 return expr;
608}
609
610ast::SizeofExpr const * ReferenceTypeElimination::previsit(
611 ast::SizeofExpr const * expr ) {
612 return ast::mutate_field( expr, &ast::SizeofExpr::type,
613 expr->type->stripReferences() );
614}
615
616ast::AlignofExpr const * ReferenceTypeElimination::previsit(
617 ast::AlignofExpr const * expr ) {
618 return ast::mutate_field( expr, &ast::AlignofExpr::type,
619 expr->type->stripReferences() );
620}
621
622ast::Type const * ReferenceTypeElimination::postvisit(
623 ast::ReferenceType const * type ) {
624 return new ast::PointerType( type->base, type->qualifiers );
625}
626
627} // namespace
628
629// Stored elsewhere (Lvalue2, initially false):
630extern bool referencesEliminated;
631
632void convertLvalue( ast::TranslationUnit & translationUnit ) {
633 ast::Pass<FixIntrinsicResults>::run( translationUnit );
634 ast::Pass<AddressRef>::run( translationUnit );
635 ast::Pass<ReferenceConversions>::run( translationUnit );
636 ast::Pass<FixIntrinsicArgs>::run( translationUnit );
637 ast::Pass<CollapseAddressDeref>::run( translationUnit );
638 ast::Pass<GeneralizedLvalue>::run( translationUnit );
639 // Last because other passes need reference types to work.
640 ast::Pass<ReferenceTypeElimination>::run( translationUnit );
641 // From this point forward, nothing should create reference types.
642 referencesEliminated = true;
643}
644
645ast::Expr const * generalizedLvalue( ast::Expr const * expr ) {
646 ast::Pass<GeneralizedLvalue> visitor;
647 return expr->accept( visitor );
648}
649
650} // namespace GenPoly
651
652// Local Variables: //
653// tab-width: 4 //
654// mode: c++ //
655// compile-command: "make install" //
656// End: //
Note: See TracBrowser for help on using the repository browser.