Changeset bdf40650 for src/GenPoly


Ignore:
Timestamp:
Nov 20, 2024, 5:19:15 PM (4 weeks ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
master
Children:
de7b7a5
Parents:
d945be9
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/Lvalue.cpp

    rd945be9 rbdf40650  
    365365                        ret = new ast::AddressExpr( ret->location, ret );
    366366                }
     367                // Must keep cast if types are different.
    367368                if ( !ResolvExpr::typesCompatible(
    368369                                srcType,
    369370                                strict_dynamic_cast<ast::ReferenceType const *>( dstType )->base ) ) {
    370                         // Must keep cast if cast-to type is different from the actual type.
    371371                        return ast::mutate_field( expr, &ast::CastExpr::arg, ret );
    372372                }
     
    503503}
    504504
     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
    505521ast::Expr const * CollapseAddressDeref::postvisit(
    506522                ast::AddressExpr const * expr ) {
     
    514530                        return ret;
    515531                }
    516         } else if ( auto cast = dynamic_cast<ast::CastExpr const *>( arg ) ) {
    517                 // Need to move cast to pointer type out a level since address of
    518                 // pointer is not valid C code (can be introduced in prior passes,
    519                 // e.g., InstantiateGeneric)
    520                 if ( ast::getPointerBase( cast->result ) ) {
    521                         auto mutExpr = ast::mutate( expr );
    522                         auto mutCast = strict_dynamic_cast<ast::CastExpr *>(
    523                                         ast::mutate( mutExpr->arg.release() ) );
    524                         mutExpr->arg = mutCast->arg;
    525                         mutCast->arg = mutExpr;
    526                         mutCast->result = new ast::PointerType( mutCast->result );
    527                         return mutCast;
    528                 }
     532        } else {
     533                return moveAddressUnderCast( expr );
    529534        }
    530535        return expr;
Note: See TracChangeset for help on using the changeset viewer.