Ignore:
Timestamp:
Dec 13, 2016, 5:20:54 PM (5 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
aaron-thesis, arm-eh, 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, resolv-new, with_gc
Children:
d5556a3
Parents:
722617d
git-author:
Rob Schluntz <rschlunt@…> (12/13/16 17:16:27)
git-committer:
Rob Schluntz <rschlunt@…> (12/13/16 17:20:54)
Message:

combine environments and costs in tuple assignment, resolve ctor/dtors for tuple assignment temporaries

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Tuples/TupleAssignment.cc

    r722617d r1d2b64f  
    2323#include "Common/SemanticError.h"
    2424#include "InitTweak/InitTweak.h"
     25#include "InitTweak/GenInit.h"
    2526
    2627#include <functional>
     
    4748                        virtual ~Matcher() {}
    4849                        virtual void match( std::list< Expression * > &out ) = 0;
     50                        ObjectDecl * newObject( UniqueName & namer, Expression * expr );
    4951                        ResolvExpr::AltList lhs, rhs;
    5052                        TupleAssignSpotter &spotter;
     53                        ResolvExpr::Cost baseCost;
    5154                        std::list< ObjectDecl * > tmpDecls;
     55                        ResolvExpr::TypeEnvironment compositeEnv;
    5256                };
    5357
     
    146150                                finder.findWithAdjustment(*i);
    147151                        } catch (...) {
    148                                 return; // xxx - no match should not mean failure, it just means this particular tuple assignment isn't valid
     152                                return; // no match should not mean failure, it just means this particular tuple assignment isn't valid
    149153                        }
    150154                        // prune expressions that don't coincide with
     
    161165                        solved_assigns.push_back( alt.expr->clone() );
    162166                }
    163                 // xxx - need to do this??
    164                 ResolvExpr::TypeEnvironment compositeEnv;
    165                 simpleCombineEnvironments( current.begin(), current.end(), compositeEnv );
    166                 currentFinder.get_alternatives().push_front( ResolvExpr::Alternative(new TupleAssignExpr(solved_assigns, matcher->tmpDecls), compositeEnv, ResolvExpr::sumCost( current ) ) );
    167         }
    168 
    169         TupleAssignSpotter::Matcher::Matcher( TupleAssignSpotter &spotter, const ResolvExpr::AltList &alts ) : spotter(spotter) {
     167                // combine assignment environments into combined expression environment
     168                simpleCombineEnvironments( current.begin(), current.end(), matcher->compositeEnv );
     169                currentFinder.get_alternatives().push_front( ResolvExpr::Alternative(new TupleAssignExpr(solved_assigns, matcher->tmpDecls), matcher->compositeEnv, ResolvExpr::sumCost( current  ) + matcher->baseCost ) );
     170        }
     171
     172        TupleAssignSpotter::Matcher::Matcher( TupleAssignSpotter &spotter, const ResolvExpr::AltList &alts ) : spotter(spotter), baseCost( ResolvExpr::sumCost( alts ) ) {
    170173                assert( ! alts.empty() );
     174                // combine argument environments into combined expression environment
     175                simpleCombineEnvironments( alts.begin(), alts.end(), compositeEnv );
     176
    171177                ResolvExpr::Alternative lhsAlt = alts.front();
    172178                // peel off the cast that exists on ctor/dtor expressions
     
    217223        }
    218224
    219         ObjectDecl * newObject( UniqueName & namer, Expression * expr ) {
     225        // removes environments from subexpressions within statement exprs, which could throw off later passes like those in Box which rely on PolyMutator.
     226        // xxx - maybe this should happen in alternative finder for every StmtExpr?
     227        // xxx - it's possible that these environments could contain some useful information. Maybe the right thing to do is aggregate the environments and pass the aggregate back to be added into the compositeEnv
     228        struct EnvRemover : public Visitor {
     229                virtual void visit( ExprStmt * stmt ) {
     230                        delete stmt->get_expr()->get_env();
     231                        stmt->get_expr()->set_env( nullptr );
     232                        Visitor::visit( stmt );
     233                }
     234        };
     235
     236        ObjectDecl * TupleAssignSpotter::Matcher::newObject( UniqueName & namer, Expression * expr ) {
    220237                assert( expr->has_result() && ! expr->get_result()->isVoid() );
    221                 return new ObjectDecl( namer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, nullptr, expr->get_result()->clone(), new SingleInit( expr->clone() ) );
     238                ObjectDecl * ret = new ObjectDecl( namer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, nullptr, expr->get_result()->clone(), new SingleInit( expr->clone() ) );
     239                ConstructorInit * ctorInit = InitTweak::genCtorInit( ret );
     240                ret->set_init( ctorInit );
     241                ResolvExpr::resolveCtorInit( ctorInit, spotter.currentFinder.get_indexer() ); // resolve ctor/dtors for the new object
     242                EnvRemover rm; // remove environments from subexpressions of StmtExprs
     243                ctorInit->accept( rm );
     244                return ret;
    222245        }
    223246
     
    244267                        std::list< ObjectDecl * > ltmp;
    245268                        std::list< ObjectDecl * > rtmp;
    246                         std::transform( lhs.begin(), lhs.end(), back_inserter( ltmp ), []( ResolvExpr::Alternative & alt ){
     269                        std::transform( lhs.begin(), lhs.end(), back_inserter( ltmp ), [&]( ResolvExpr::Alternative & alt ){
    247270                                return newObject( lhsNamer, alt.expr );
    248271                        });
    249                         std::transform( rhs.begin(), rhs.end(), back_inserter( rtmp ), []( ResolvExpr::Alternative & alt ){
     272                        std::transform( rhs.begin(), rhs.end(), back_inserter( rtmp ), [&]( ResolvExpr::Alternative & alt ){
    250273                                return newObject( rhsNamer, alt.expr );
    251274                        });
Note: See TracChangeset for help on using the changeset viewer.