Changeset 7543dec


Ignore:
Timestamp:
Nov 9, 2017, 1:01:14 PM (4 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
arm-eh, cleanup-dtors, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr
Children:
96fc67b
Parents:
049ead9
git-author:
Rob Schluntz <rschlunt@…> (11/08/17 16:40:12)
git-committer:
Rob Schluntz <rschlunt@…> (11/09/17 13:01:14)
Message:

Modify VarExprReplacer? to replace VariableExpr? with arbitrary expression

Location:
src
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • src/ControlStruct/ExceptTranslate.cc

    r049ead9 r7543dec  
    315315                        {
    316316                                VarExprReplacer::DeclMap mapping;
    317                                 mapping[ handler_decl ] = local_except;
     317                                mapping[ handler_decl ] = new VariableExpr( local_except );
    318318                                VarExprReplacer mapper( mapping );
    319                                 handler->get_body()->accept( mapper );
     319                                handler->body->acceptMutator( mapper );
    320320                        }
    321321
    322                         block->push_back( handler->get_body() );
    323                         handler->set_body( nullptr );
     322                        block->push_back( handler->body );
     323                        handler->body = nullptr;
    324324
    325325                        std::list<Statement *> caseBody
  • src/InitTweak/FixInit.cc

    r049ead9 r7543dec  
    676676                        // the original code contains uses of objDecl - replace them with the newly generated 'this' parameter.
    677677                        ObjectDecl * thisParam = getParamThis( dtorFunc->type );
    678                         VarExprReplacer::replace( dtor, { std::make_pair( objDecl, thisParam ) } );
     678                        VarExprReplacer::replace( dtor, { std::make_pair( objDecl, new VariableExpr( thisParam ) ) } );
    679679                        dtorFunc->statements->push_back( dtor );
    680680
  • src/SynTree/CompoundStmt.cc

    r049ead9 r7543dec  
    5959                                DeclarationWithType * origdwt = strict_dynamic_cast< DeclarationWithType * > ( origDeclStmt->get_decl() );
    6060                                assert( dwt->get_name() == origdwt->get_name() );
    61                                 declMap[ origdwt ] = dwt;
     61                                declMap[ origdwt ] = new VariableExpr( dwt );
    6262                        } else assert( ! dynamic_cast< DeclarationWithType * > ( origDeclStmt->get_decl() ) );
    6363                } else assert( ! dynamic_cast< DeclStmt * > ( s ) );
     
    6565        if ( ! declMap.empty() ) {
    6666                VarExprReplacer replacer( declMap );
    67                 accept( replacer );
     67                acceptMutator( replacer );
    6868        }
    6969}
  • src/SynTree/FunctionDecl.cc

    r049ead9 r7543dec  
    4343        VarExprReplacer::DeclMap declMap;
    4444        for ( auto p : group_iterate( other.type->parameters, type->parameters ) ) {
    45                 declMap[ std::get<0>(p) ] = std::get<1>(p);
     45                declMap[ std::get<0>(p) ] = new VariableExpr( std::get<1>(p) );
    4646        }
    4747        for ( auto p : group_iterate( other.type->returnVals, type->returnVals ) ) {
    48                 declMap[ std::get<0>(p) ] = std::get<1>(p);
     48                declMap[ std::get<0>(p) ] = new VariableExpr( std::get<1>(p) );
    4949        }
    5050        if ( ! declMap.empty() ) {
    5151                VarExprReplacer replacer( declMap );
    52                 accept( replacer );
     52                acceptMutator( replacer );
    5353        }
    5454}
  • src/SynTree/VarExprReplacer.cc

    r049ead9 r7543dec  
    2222VarExprReplacer::VarExprReplacer( const DeclMap & declMap, bool debug ) : declMap( declMap ), debug( debug ) {}
    2323
    24 // replace variable with new node from decl map
    25 void VarExprReplacer::visit( VariableExpr * varExpr ) {
    26         // xxx - assertions and parameters aren't accounted for in this... (i.e. they aren't inserted into the map when it's made, only DeclStmts are)
    27         if ( declMap.count( varExpr->get_var() ) ) {
    28                 if ( debug ) {
    29                         std::cerr << "replacing variable reference: " << (void*)varExpr->get_var() << " " << varExpr->get_var() << " with " << (void*)declMap.at( varExpr->get_var() ) << " " << declMap.at( varExpr->get_var() ) << std::endl;
    30                 }
    31                 varExpr->set_var( declMap.at( varExpr->get_var() ) );
     24VarExprReplacer::~VarExprReplacer() {
     25        for ( auto p : declMap ) {
     26                delete p.second;
    3227        }
    3328}
     29
     30// replace variable with new node from decl map
     31Expression * VarExprReplacer::mutate( VariableExpr * varExpr ) {
     32        // xxx - assertions and parameters aren't accounted for in this... (i.e. they aren't inserted into the map when it's made, only DeclStmts are)
     33        if ( declMap.count( varExpr->var ) ) {
     34                Expression * expr = declMap.at( varExpr->var );
     35                if ( debug ) {
     36                        std::cerr << "replacing variable reference: " << (void*)varExpr->var << " " << varExpr->var << " with " << (void*)expr << " " << expr << std::endl;
     37                }
     38                delete varExpr;
     39                return expr->clone();
     40        }
     41        return varExpr;
     42}
  • src/SynTree/VarExprReplacer.h

    r049ead9 r7543dec  
    2424
    2525/// Visitor that replaces the declarations that VariableExprs refer to, according to the supplied mapping
    26 class VarExprReplacer : public Visitor {
     26class VarExprReplacer : public Mutator {
    2727public:
    28         typedef std::map< DeclarationWithType *, DeclarationWithType * > DeclMap;
     28        typedef std::map< DeclarationWithType *, Expression * > DeclMap;
    2929private:
    3030        const DeclMap & declMap;
     
    3232public:
    3333        VarExprReplacer( const DeclMap & declMap, bool debug = false );
     34        ~VarExprReplacer();
    3435
    3536        // replace variable with new node from decl map
    36         virtual void visit( VariableExpr * varExpr );
     37        virtual Expression * mutate( VariableExpr * varExpr );
    3738
    38         static void replace( BaseSyntaxNode * node, const DeclMap & declMap, bool debug = false ) {
     39        template<typename Node>
     40        static void replace( Node *& node, const DeclMap & declMap, bool debug = false ) {
    3941                VarExprReplacer replacer( declMap, debug );
    40                 maybeAccept( node, replacer );
     42                node = maybeMutate( node, replacer );
    4143        }
    4244};
Note: See TracChangeset for help on using the changeset viewer.