Changeset acd7c5dd for src/GenPoly


Ignore:
Timestamp:
Aug 8, 2017, 2:49:47 PM (7 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
c92c09c
Parents:
56e49b0
Message:

Apply generalized lvalue pass to MemberExpr?

Location:
src/GenPoly
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/Box.cc

    r56e49b0 racd7c5dd  
    2727#include "Box.h"
    2828#include "DeclMutator.h"
     29#include "Lvalue.h"
     30#include "FindFunction.h"
    2931#include "PolyMutator.h"
    30 #include "FindFunction.h"
    3132#include "ScopedSet.h"
    3233#include "ScrubTyVars.h"
     
    755756
    756757                void Pass1::boxParam( Type *param, Expression *&arg, const TyVarMap &exprTyVars ) {
    757                         assert( arg->has_result() );
     758                        assertf( arg->has_result(), "arg does not have result: %s", toString( arg ).c_str() );
    758759                        if ( isPolyType( param, exprTyVars ) ) {
    759                                 if ( isPolyType( arg->get_result() ) ) {
     760                                Type * newType = arg->get_result()->clone();
     761                                if ( env ) env->apply( newType );
     762                                std::auto_ptr<Type> manager( newType );
     763                                if ( isPolyType( newType ) ) {
    760764                                        // if the argument's type is polymorphic, we don't need to box again!
    761765                                        return;
    762766                                } else if ( arg->get_result()->get_lvalue() ) {
    763                                         // VariableExpr and MemberExpr are lvalues; need to check this isn't coming from the second arg of a comma expression though (not an lvalue)
    764                                         // xxx - need to test that this code is still reachable
    765                                         if ( CommaExpr *commaArg = dynamic_cast< CommaExpr* >( arg ) ) {
    766                                                 commaArg->set_arg2( new AddressExpr( commaArg->get_arg2() ) );
    767                                         } else {
    768                                                 arg = new AddressExpr( arg );
    769                                         }
     767                                        // argument expression may be CFA lvalue, but not C lvalue -- apply generalizedLvalue transformations.
     768                                        arg =  generalizedLvalue( new AddressExpr( arg ) );
    770769                                        if ( ! ResolvExpr::typesCompatible( param, arg->get_result(), SymTab::Indexer() ) ) {
    771770                                                // silence warnings by casting boxed parameters when the actual type does not match up with the formal type.
     
    18791878                        return structDecl;
    18801879                }
    1881                
     1880
    18821881                Declaration *Pass3::mutate( UnionDecl *unionDecl ) {
    18831882                        stripGenericMembers( unionDecl );
  • src/GenPoly/Lvalue.cc

    r56e49b0 racd7c5dd  
    2727#include "SynTree/Mutator.h"
    2828#include "SymTab/Indexer.h"
     29
    2930#include "ResolvExpr/Resolver.h"
     31#include "ResolvExpr/TypeEnvironment.h"
    3032#include "ResolvExpr/typeops.h"
     33#include "ResolvExpr/Unify.h"
    3134
    3235#include "Common/UniqueName.h"
     
    6063                        typedef Mutator Parent;
    6164
     65                        virtual Expression * mutate( MemberExpr * memExpr );
    6266                        virtual Expression * mutate( AddressExpr * addressExpr );
     67
     68                        template<typename Func>
     69                        Expression * applyTransformation( Expression * expr, Expression * arg, Func mkExpr );
    6370                };
    6471        } // namespace
     
    7178                acceptAll( translationUnit, p2 );
    7279                mutateAll( translationUnit, genLval );
     80        }
     81
     82        Expression * generalizedLvalue( Expression * expr ) {
     83                GeneralizedLvalue genLval;
     84                return expr->acceptMutator( genLval );
    7385        }
    7486
     
    163175                }
    164176
    165                 Expression * GeneralizedLvalue::mutate( AddressExpr * addrExpr ) {
    166                         addrExpr = safe_dynamic_cast< AddressExpr * >( Parent::mutate( addrExpr ) );
    167                         if ( CommaExpr * commaExpr = dynamic_cast< CommaExpr * >( addrExpr->get_arg() ) ) {
     177                template<typename Func>
     178                Expression * GeneralizedLvalue::applyTransformation( Expression * expr, Expression * arg, Func mkExpr ) {
     179                        if ( CommaExpr * commaExpr = dynamic_cast< CommaExpr * >( arg ) ) {
    168180                                Expression * arg1 = commaExpr->get_arg1()->clone();
    169181                                Expression * arg2 = commaExpr->get_arg2()->clone();
    170                                 delete addrExpr;
    171                                 return new CommaExpr( arg1, new AddressExpr( arg2 ) );
    172                         } else if ( ConditionalExpr * condExpr = dynamic_cast< ConditionalExpr * >( addrExpr->get_arg() ) ) {
     182                                Expression * ret = new CommaExpr( arg1, mkExpr( arg2 ) );
     183                                ret->set_env( expr->get_env() );
     184                                expr->set_env( nullptr );
     185                                delete expr;
     186                                return ret->acceptMutator( *this );
     187                        } else if ( ConditionalExpr * condExpr = dynamic_cast< ConditionalExpr * >( arg ) ) {
    173188                                Expression * arg1 = condExpr->get_arg1()->clone();
    174189                                Expression * arg2 = condExpr->get_arg2()->clone();
    175190                                Expression * arg3 = condExpr->get_arg3()->clone();
    176                                 delete addrExpr;
    177                                 return new ConditionalExpr( arg1, new AddressExpr( arg2 ), new AddressExpr( arg3 ) );
     191                                ConditionalExpr * ret = new ConditionalExpr( arg1, mkExpr( arg2 ), mkExpr( arg3 ) );
     192                                ret->set_env( expr->get_env() );
     193                                expr->set_env( nullptr );
     194                                delete expr;
     195
     196                                // conditional expr type may not be either of the argument types, need to unify
     197                                using namespace ResolvExpr;
     198                                Type* commonType = nullptr;
     199                                TypeEnvironment newEnv;
     200                                AssertionSet needAssertions, haveAssertions;
     201                                OpenVarSet openVars;
     202                                unify( ret->get_arg2()->get_result(), ret->get_arg3()->get_result(), newEnv, needAssertions, haveAssertions, openVars, SymTab::Indexer(), commonType );
     203                                ret->set_result( commonType ? commonType : ret->get_arg2()->get_result()->clone() );
     204                                return ret->acceptMutator( *this );
    178205                        }
    179                         return addrExpr;
     206                        return expr;
     207                }
     208
     209                Expression * GeneralizedLvalue::mutate( MemberExpr * memExpr ) {
     210                        Parent::mutate( memExpr );
     211                        return applyTransformation( memExpr, memExpr->get_aggregate(), [=]( Expression * aggr ) { return new MemberExpr( memExpr->get_member(), aggr ); } );
     212                }
     213
     214                Expression * GeneralizedLvalue::mutate( AddressExpr * addrExpr ) {
     215                        addrExpr = safe_dynamic_cast< AddressExpr * >( Parent::mutate( addrExpr ) );
     216                        return applyTransformation( addrExpr, addrExpr->get_arg(), []( Expression * arg ) { return new AddressExpr( arg ); } );
    180217                }
    181218        } // namespace
  • src/GenPoly/Lvalue.h

    r56e49b0 racd7c5dd  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // Lvalue.h -- 
     7// Lvalue.h --
    88//
    99// Author           : Richard C. Bilson
     
    2323        /// replaces return type of `lvalue T` with `T*`, along with appropriate address-of and dereference operators
    2424        void convertLvalue( std::list< Declaration* >& translationUnit );
     25
     26        /// applies transformations that allow GCC to accept more complicated lvalue expressions, e.g. &(a, b)
     27        Expression * generalizedLvalue( Expression * expr );
    2528} // namespace GenPoly
    2629
Note: See TracChangeset for help on using the changeset viewer.