source: src/Tuples/Explode.h @ 68f9c43

new-envwith_gc
Last change on this file since 68f9c43 was 68f9c43, checked in by Aaron Moss <a3moss@…>, 6 years ago

First pass at delete removal

  • Property mode set to 100644
File size: 5.2 KB
RevLine 
[141b786]1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// Explode.h --
8//
9// Author           : Rob Schluntz
10// Created On       : Wed Nov 9 13:12:24 2016
[6b0b624]11// Last Modified By : Peter A. Buhr
12// Last Modified On : Sat Jul 22 09:55:16 2017
13// Update Count     : 3
[141b786]14//
15
[6b0b624]16#pragma once
[141b786]17
[62194cb]18#include <iterator>                     // for back_inserter, back_insert_iterator
19#include <utility>                      // for forward
[141b786]20
[62194cb]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
[141b786]26
[03321e4]27namespace SymTab {
28class Indexer;
[68f9c43]29}  // namespace SymTabf
[141b786]30
31namespace Tuples {
[0b5d871]32        Expression * distributeReference( Expression * );
33
[6bbce58]34        static inline CastExpr * isReferenceCast( Expression * expr ) {
35                if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) {
36                        if ( dynamic_cast< ReferenceType * >( castExpr->result ) ) {
37                                return castExpr;
38                        }
39                }
40                return nullptr;
41        }
42
[62194cb]43        /// Append alternative to an OutputIterator of Alternatives
44        template<typename OutputIterator>
[490ff5c3]45        void append( OutputIterator out, Expression* expr, const ResolvExpr::TypeEnvironment& env,
[62194cb]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
[490ff5c3]51        static inline void append( ResolvExpr::ExplodedActual& ea, Expression* expr,
[62194cb]52                        const ResolvExpr::TypeEnvironment&, const ResolvExpr::Cost&, const ResolvExpr::Cost& ) {
53                ea.exprs.emplace_back( expr );
54                /// xxx -- merge environment, cost?
55        }
56
[141b786]57        /// helper function used by explode
[62194cb]58        template< typename Output >
[490ff5c3]59        void explodeUnique( Expression * expr, const ResolvExpr::Alternative & alt,
[62194cb]60                        const SymTab::Indexer & indexer, Output&& out, bool isTupleAssign ) {
[0b5d871]61                if ( isTupleAssign ) {
62                        // tuple assignment needs CastExprs to be recursively exploded to easily get at all of the components
[6bbce58]63                        if ( CastExpr * castExpr = isReferenceCast( expr ) ) {
[0b5d871]64                                ResolvExpr::AltList alts;
[490ff5c3]65                                explodeUnique(
[62194cb]66                                        castExpr->get_arg(), alt, indexer, back_inserter( alts ), isTupleAssign );
[0b5d871]67                                for ( ResolvExpr::Alternative & alt : alts ) {
68                                        // distribute reference cast over all components
[68f9c43]69                                        append( std::forward<Output>(out), distributeReference( alt.expr ),
[62194cb]70                                                alt.env, alt.cost, alt.cvtCost );
[0b5d871]71                                }
72                                // 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)
73                                return;
74                        }
75                }
[6d267ca]76                Type * res = expr->get_result()->stripReferences();
[f831177]77                if ( TupleType * tupleType = dynamic_cast< TupleType * > ( res ) ) {
[141b786]78                        if ( TupleExpr * tupleExpr = dynamic_cast< TupleExpr * >( expr ) ) {
79                                // can open tuple expr and dump its exploded components
80                                for ( Expression * expr : tupleExpr->get_exprs() ) {
[62194cb]81                                        explodeUnique( expr, alt, indexer, std::forward<Output>(out), isTupleAssign );
[141b786]82                                }
83                        } else {
[6d267ca]84                                // tuple type, but not tuple expr - recursively index into its components.
85                                // if expr type is reference, convert to value type
[141b786]86                                Expression * arg = expr->clone();
[0b5d871]87                                if ( Tuples::maybeImpureIgnoreUnique( arg ) ) {
[141b786]88                                        // expressions which may contain side effects require a single unique instance of the expression.
89                                        arg = new UniqueExpr( arg );
90                                }
[0b5d871]91                                // cast reference to value type to facilitate further explosion
92                                if ( dynamic_cast<ReferenceType *>( arg->get_result() ) ) {
93                                        arg = new CastExpr( arg, tupleType->clone() );
94                                }
[141b786]95                                for ( unsigned int i = 0; i < tupleType->size(); i++ ) {
96                                        TupleIndexExpr * idx = new TupleIndexExpr( arg->clone(), i );
[62194cb]97                                        explodeUnique( idx, alt, indexer, std::forward<Output>(out), isTupleAssign );
[141b786]98                                }
99                        }
100                } else {
101                        // atomic (non-tuple) type - output a clone of the expression in a new alternative
[62194cb]102                        append( std::forward<Output>(out), expr->clone(), alt.env, alt.cost, alt.cvtCost );
[141b786]103                }
104        }
105
106        /// expands a tuple-valued alternative into multiple alternatives, each with a non-tuple-type
[62194cb]107        template< typename Output >
[490ff5c3]108        void explode( const ResolvExpr::Alternative &alt, const SymTab::Indexer & indexer,
[62194cb]109                        Output&& out, bool isTupleAssign = false ) {
110                explodeUnique( alt.expr, alt, indexer, std::forward<Output>(out), isTupleAssign );
[141b786]111        }
112
113        // explode list of alternatives
[62194cb]114        template< typename AltIterator, typename Output >
[490ff5c3]115        void explode( AltIterator altBegin, AltIterator altEnd, const SymTab::Indexer & indexer,
[62194cb]116                        Output&& out, bool isTupleAssign = false ) {
[141b786]117                for ( ; altBegin != altEnd; ++altBegin ) {
[62194cb]118                        explode( *altBegin, indexer, std::forward<Output>(out), isTupleAssign );
[141b786]119                }
120        }
121
[62194cb]122        template< typename Output >
[490ff5c3]123        void explode( const ResolvExpr::AltList & alts, const SymTab::Indexer & indexer, Output&& out,
[62194cb]124                        bool isTupleAssign = false ) {
125                explode( alts.begin(), alts.end(), indexer, std::forward<Output>(out), isTupleAssign );
[141b786]126        }
127} // namespace Tuples
128
129// Local Variables: //
130// tab-width: 4 //
131// mode: c++ //
132// compile-command: "make install" //
133// End: //
Note: See TracBrowser for help on using the repository browser.