Changeset 0a60c04


Ignore:
Timestamp:
Dec 4, 2017, 3:37:28 PM (4 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:
866f560
Parents:
ac57659
Message:

Introduce a temporary for with expressions that may contain side-effects and evaluate the expression once

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/Resolver.cc

    rac57659 r0a60c04  
    2626#include "Common/utility.h"              // for ValueGuard, group_iterate
    2727#include "CurrentObject.h"               // for CurrentObject
     28#include "InitTweak/GenInit.h"
    2829#include "InitTweak/InitTweak.h"         // for isIntrinsicSingleArgCallStmt
    2930#include "RenameVars.h"                  // for RenameVars, global_renamer
     
    4041#include "SynTree/TypeSubstitution.h"    // for TypeSubstitution
    4142#include "SynTree/Visitor.h"             // for acceptAll, maybeAccept
     43#include "Tuples/Tuples.h"
    4244#include "typeops.h"                     // for extractResultType
    4345#include "Unify.h"                       // for unify
     
    4648
    4749namespace ResolvExpr {
    48         struct Resolver final : public WithIndexer, public WithGuards, public WithVisitorRef<Resolver>, public WithShortCircuiting {
     50        struct Resolver final : public WithIndexer, public WithGuards, public WithVisitorRef<Resolver>, public WithShortCircuiting, public WithStmtsToAdd {
    4951                Resolver() {}
    5052                Resolver( const SymTab::Indexer & other ) {
     
    611613                        expr = alt.expr;
    612614                        alt.expr = nullptr;
     615
     616                        // if with expression might be impure, create a temporary so that it is evaluated once
     617                        if ( Tuples::maybeImpure( expr ) ) {
     618                                static UniqueName tmpNamer( "_with_tmp_" );
     619                                ObjectDecl * tmp = ObjectDecl::newObject( tmpNamer.newName(), expr->result->clone(), new SingleInit( expr ) );
     620                                expr = new VariableExpr( tmp );
     621                                stmtsToAddBefore.push_back( new DeclStmt( tmp ) );
     622                                if ( InitTweak::isConstructable( tmp->type ) ) {
     623                                        // generate ctor/dtor and resolve them
     624                                        tmp->init = InitTweak::genCtorInit( tmp );
     625                                        tmp->accept( *visitor );
     626                                }
     627                        }
    613628                }
    614629        }
Note: See TracChangeset for help on using the changeset viewer.