Changeset 62194cb for src/Tuples


Ignore:
Timestamp:
Nov 23, 2017, 4:24:48 PM (7 years ago)
Author:
Aaron Moss <a3moss@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, 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, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
178e4ec
Parents:
a8b27c6
Message:

Reduce duplication of cost/env in ExplodedActual?

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Tuples/Explode.h

    ra8b27c6 r62194cb  
    1616#pragma once
    1717
    18 #include <iterator>                  // for back_inserter, back_insert_iterator
     18#include <iterator>                     // for back_inserter, back_insert_iterator
     19#include <utility>                      // for forward
    1920
    20 #include "ResolvExpr/Alternative.h"  // for Alternative, AltList
    21 #include "SynTree/Expression.h"      // for Expression, UniqueExpr, AddressExpr
    22 #include "SynTree/Type.h"            // for TupleType, Type
    23 #include "Tuples.h"                  // for maybeImpure
     21#include "ResolvExpr/Alternative.h"     // for Alternative, AltList
     22#include "ResolvExpr/ExplodedActual.h"  // for ExplodedActual
     23#include "SynTree/Expression.h"         // for Expression, UniqueExpr, AddressExpr
     24#include "SynTree/Type.h"               // for TupleType, Type
     25#include "Tuples.h"                     // for maybeImpure
    2426
    2527namespace SymTab {
     
    3941        }
    4042
     43        /// Append alternative to an OutputIterator of Alternatives
     44        template<typename OutputIterator>
     45        void append( OutputIterator out, Expression* expr, const ResolvExpr::TypeEnvironment& env,
     46                        const ResolvExpr::Cost& cost, const ResolvExpr::Cost& cvtCost ) {
     47                *out++ = ResolvExpr::Alternative{ expr, env, cost, cvtCost };
     48        }
     49
     50        /// Append alternative to an ExplodedActual
     51        static inline void append( ResolvExpr::ExplodedActual& ea, Expression* expr,
     52                        const ResolvExpr::TypeEnvironment&, const ResolvExpr::Cost&, const ResolvExpr::Cost& ) {
     53                ea.exprs.emplace_back( expr );
     54                /// xxx -- merge environment, cost?
     55        }
     56
    4157        /// helper function used by explode
    42         template< typename OutputIterator >
    43         void explodeUnique( Expression * expr, const ResolvExpr::Alternative & alt, const SymTab::Indexer & indexer, OutputIterator out, bool isTupleAssign ) {
     58        template< typename Output >
     59        void explodeUnique( Expression * expr, const ResolvExpr::Alternative & alt,
     60                        const SymTab::Indexer & indexer, Output&& out, bool isTupleAssign ) {
    4461                if ( isTupleAssign ) {
    4562                        // tuple assignment needs CastExprs to be recursively exploded to easily get at all of the components
    4663                        if ( CastExpr * castExpr = isReferenceCast( expr ) ) {
    4764                                ResolvExpr::AltList alts;
    48                                 explodeUnique( castExpr->get_arg(), alt, indexer, back_inserter( alts ), isTupleAssign );
     65                                explodeUnique(
     66                                        castExpr->get_arg(), alt, indexer, back_inserter( alts ), isTupleAssign );
    4967                                for ( ResolvExpr::Alternative & alt : alts ) {
    5068                                        // distribute reference cast over all components
    51                                         alt.expr = distributeReference( alt.expr );
    52                                         *out++ = alt;
     69                                        append( std::forward<Output>(out), distributeReference( alt.release_expr() ),
     70                                                alt.env, alt.cost, alt.cvtCost );
    5371                                }
    5472                                // in tuple assignment, still need to handle the other cases, but only if not already handled here (don't want to output too many alternatives)
     
    6179                                // can open tuple expr and dump its exploded components
    6280                                for ( Expression * expr : tupleExpr->get_exprs() ) {
    63                                         explodeUnique( expr, alt, indexer, out, isTupleAssign );
     81                                        explodeUnique( expr, alt, indexer, std::forward<Output>(out), isTupleAssign );
    6482                                }
    6583                        } else {
     
    7795                                for ( unsigned int i = 0; i < tupleType->size(); i++ ) {
    7896                                        TupleIndexExpr * idx = new TupleIndexExpr( arg->clone(), i );
    79                                         explodeUnique( idx, alt, indexer, out, isTupleAssign );
     97                                        explodeUnique( idx, alt, indexer, std::forward<Output>(out), isTupleAssign );
    8098                                        delete idx;
    8199                                }
     
    84102                } else {
    85103                        // atomic (non-tuple) type - output a clone of the expression in a new alternative
    86                         *out++ = ResolvExpr::Alternative( expr->clone(), alt.env, alt.cost, alt.cvtCost );
     104                        append( std::forward<Output>(out), expr->clone(), alt.env, alt.cost, alt.cvtCost );
    87105                }
    88106        }
    89107
    90108        /// expands a tuple-valued alternative into multiple alternatives, each with a non-tuple-type
    91         template< typename OutputIterator >
    92         void explode( const ResolvExpr::Alternative &alt, const SymTab::Indexer & indexer, OutputIterator out, bool isTupleAssign = false ) {
    93                 explodeUnique( alt.expr, alt, indexer, out, isTupleAssign );
     109        template< typename Output >
     110        void explode( const ResolvExpr::Alternative &alt, const SymTab::Indexer & indexer,
     111                        Output&& out, bool isTupleAssign = false ) {
     112                explodeUnique( alt.expr, alt, indexer, std::forward<Output>(out), isTupleAssign );
    94113        }
    95114
    96115        // explode list of alternatives
    97         template< typename AltIterator, typename OutputIterator >
    98         void explode( AltIterator altBegin, AltIterator altEnd, const SymTab::Indexer & indexer, OutputIterator out, bool isTupleAssign = false ) {
     116        template< typename AltIterator, typename Output >
     117        void explode( AltIterator altBegin, AltIterator altEnd, const SymTab::Indexer & indexer,
     118                        Output&& out, bool isTupleAssign = false ) {
    99119                for ( ; altBegin != altEnd; ++altBegin ) {
    100                         explode( *altBegin, indexer, out, isTupleAssign );
     120                        explode( *altBegin, indexer, std::forward<Output>(out), isTupleAssign );
    101121                }
    102122        }
    103123
    104         template< typename OutputIterator >
    105         void explode( const ResolvExpr::AltList & alts, const SymTab::Indexer & indexer, OutputIterator out, bool isTupleAssign = false ) {
    106                 explode( alts.begin(), alts.end(), indexer, out, isTupleAssign );
     124        template< typename Output >
     125        void explode( const ResolvExpr::AltList & alts, const SymTab::Indexer & indexer, Output&& out,
     126                        bool isTupleAssign = false ) {
     127                explode( alts.begin(), alts.end(), indexer, std::forward<Output>(out), isTupleAssign );
    107128        }
    108129} // namespace Tuples
Note: See TracChangeset for help on using the changeset viewer.