Changeset 7543dec for src/SynTree


Ignore:
Timestamp:
Nov 9, 2017, 1:01:14 PM (7 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
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/SynTree
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • 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.