Changeset 23bb1b9


Ignore:
Timestamp:
Nov 9, 2016, 2:10:00 PM (5 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
aaron-thesis, arm-eh, cleanup-dtors, deferred_resn, demangler, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, resolv-new, with_gc
Children:
141b786
Parents:
b3b2077
git-author:
Rob Schluntz <rschlunt@…> (11/09/16 14:09:29)
git-committer:
Rob Schluntz <rschlunt@…> (11/09/16 14:10:00)
Message:

refactor VarExprReplacer? and reuse it in TupleAssignExpr?

Location:
src/SynTree
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • src/SynTree/CompoundStmt.cc

    rb3b2077 r23bb1b9  
    2020#include "Expression.h"
    2121#include "Declaration.h"
     22#include "SynTree/VarExprReplacer.h"
    2223
    2324using std::string;
    2425using std::endl;
    25 
    26 class VarExprReplacer : public Visitor {
    27 public:
    28   typedef std::map< DeclarationWithType *, DeclarationWithType * > DeclMap;
    29 private:
    30   const DeclMap & declMap;
    31 public:
    32   VarExprReplacer( const DeclMap & declMap ) : declMap( declMap ) {}
    33 
    34   // replace variable with new node from decl map
    35   virtual void visit( VariableExpr * varExpr ) {
    36     if ( declMap.count( varExpr->get_var() ) ) {
    37       varExpr->set_var( declMap.at( varExpr->get_var() ) );
    38     }
    39   }
    40 };
    41 
    4226
    4327CompoundStmt::CompoundStmt( std::list<Label> labels ) : Statement( labels ) {
     
    4731        cloneAll( other.kids, kids );
    4832
    49   // when cloning a compound statement, we may end up cloning declarations which
    50   // are referred to by VariableExprs throughout the block. Cloning a VariableExpr
    51   // does a shallow copy, so the VariableExpr will end up pointing to the original
    52   // declaration. If the original declaration is deleted, e.g. because the original
    53   // CompoundStmt is deleted, then we have a dangling pointer. To avoid this case,
    54   // find all DeclarationWithType nodes (since a VariableExpr must point to a
    55   // DeclarationWithType) in the original CompoundStmt and map them to the cloned
    56   // node in the new CompoundStmt ('this'), then replace the Declarations referred to
    57   // by each VariableExpr according to the constructed map. Note that only the declarations
    58   // in the current level are collected into the map, because child CompoundStmts will
    59   // recursively execute this routine. There may be more efficient ways of doing
    60   // this.
    61   VarExprReplacer::DeclMap declMap;
    62   std::list< Statement * >::const_iterator origit = other.kids.begin();
    63   for ( Statement * s : kids ) {
    64     assert( origit != other.kids.end() );
    65     if ( DeclStmt * declStmt = dynamic_cast< DeclStmt * >( s ) ) {
    66       DeclStmt * origDeclStmt = dynamic_cast< DeclStmt * >( *origit );
    67       assert( origDeclStmt );
    68       if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * > ( declStmt->get_decl() ) ) {
    69         DeclarationWithType * origdwt = dynamic_cast< DeclarationWithType * > ( origDeclStmt->get_decl() );
    70         assert( origdwt );
    71         declMap[ origdwt ] = dwt;
    72       }
    73     }
    74   }
    75   if ( ! declMap.empty() ) {
    76     VarExprReplacer replacer( declMap );
    77     accept( replacer );
    78   }
     33        // when cloning a compound statement, we may end up cloning declarations which
     34        // are referred to by VariableExprs throughout the block. Cloning a VariableExpr
     35        // does a shallow copy, so the VariableExpr will end up pointing to the original
     36        // declaration. If the original declaration is deleted, e.g. because the original
     37        // CompoundStmt is deleted, then we have a dangling pointer. To avoid this case,
     38        // find all DeclarationWithType nodes (since a VariableExpr must point to a
     39        // DeclarationWithType) in the original CompoundStmt and map them to the cloned
     40        // node in the new CompoundStmt ('this'), then replace the Declarations referred to
     41        // by each VariableExpr according to the constructed map. Note that only the declarations
     42        // in the current level are collected into the map, because child CompoundStmts will
     43        // recursively execute this routine. There may be more efficient ways of doing
     44        // this.
     45        VarExprReplacer::DeclMap declMap;
     46        std::list< Statement * >::const_iterator origit = other.kids.begin();
     47        for ( Statement * s : kids ) {
     48                assert( origit != other.kids.end() );
     49                Statement * origStmt = *origit++;
     50                if ( DeclStmt * declStmt = dynamic_cast< DeclStmt * >( s ) ) {
     51                        DeclStmt * origDeclStmt = dynamic_cast< DeclStmt * >( origStmt );
     52                        assert( origDeclStmt );
     53                        if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * > ( declStmt->get_decl() ) ) {
     54                                DeclarationWithType * origdwt = dynamic_cast< DeclarationWithType * > ( origDeclStmt->get_decl() );
     55                                assert( origdwt );
     56                                assert( dwt->get_name() == origdwt->get_name() );
     57                                declMap[ origdwt ] = dwt;
     58                        }
     59                }
     60        }
     61        if ( ! declMap.empty() ) {
     62                VarExprReplacer replacer( declMap );
     63                accept( replacer );
     64        }
    7965}
    8066
  • src/SynTree/TupleExpr.cc

    rb3b2077 r23bb1b9  
    1919#include "Declaration.h"
    2020#include "Tuples/Tuples.h"
     21#include "VarExprReplacer.h"
    2122
    2223TupleExpr::TupleExpr( const std::list< Expression * > & exprs, Expression *_aname ) : Expression( _aname ), exprs( exprs ) {
     
    9192}
    9293
    93 TupleAssignExpr::TupleAssignExpr( const TupleAssignExpr &other ) : Expression( other ), tempDecls( other.tempDecls ) /* temporary */ {
     94TupleAssignExpr::TupleAssignExpr( const TupleAssignExpr &other ) : Expression( other ) {
    9495        cloneAll( other.assigns, assigns );
    95         // xxx - clone needs to go into assigns and replace tempDecls
     96        cloneAll( other.tempDecls, tempDecls );
     97
     98        // clone needs to go into assigns and replace tempDecls
     99        VarExprReplacer::DeclMap declMap;
     100        std::list< ObjectDecl * >::const_iterator origit = other.tempDecls.begin();
     101        for ( ObjectDecl * temp : tempDecls ) {
     102                assert( origit != other.tempDecls.end() );
     103                ObjectDecl * origTemp = *origit++;
     104                assert( origTemp );
     105                assert( temp->get_name() == origTemp->get_name() );
     106                declMap[ origTemp ] = temp;
     107        }
     108        if ( ! declMap.empty() ) {
     109                VarExprReplacer replacer( declMap );
     110                for ( Expression * assn : assigns ) {
     111                        assn->accept( replacer );
     112                }
     113        }
    96114}
    97115
  • src/SynTree/module.mk

    rb3b2077 r23bb1b9  
    4949       SynTree/AddStmtVisitor.cc \
    5050       SynTree/TypeSubstitution.cc \
    51        SynTree/Attribute.cc
     51       SynTree/Attribute.cc \
     52       SynTree/VarExprReplacer.cc
    5253
Note: See TracChangeset for help on using the changeset viewer.