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
Line 
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
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Sat Jul 22 09:55:16 2017
13// Update Count     : 3
14//
15
16#pragma once
17
18#include <iterator>                     // for back_inserter, back_insert_iterator
19#include <utility>                      // for forward
20
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
26
27namespace SymTab {
28class Indexer;
29}  // namespace SymTabf
30
31namespace Tuples {
32        Expression * distributeReference( Expression * );
33
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
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
57        /// helper function used by explode
58        template< typename Output >
59        void explodeUnique( Expression * expr, const ResolvExpr::Alternative & alt,
60                        const SymTab::Indexer & indexer, Output&& out, bool isTupleAssign ) {
61                if ( isTupleAssign ) {
62                        // tuple assignment needs CastExprs to be recursively exploded to easily get at all of the components
63                        if ( CastExpr * castExpr = isReferenceCast( expr ) ) {
64                                ResolvExpr::AltList alts;
65                                explodeUnique(
66                                        castExpr->get_arg(), alt, indexer, back_inserter( alts ), isTupleAssign );
67                                for ( ResolvExpr::Alternative & alt : alts ) {
68                                        // distribute reference cast over all components
69                                        append( std::forward<Output>(out), distributeReference( alt.expr ),
70                                                alt.env, alt.cost, alt.cvtCost );
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                }
76                Type * res = expr->get_result()->stripReferences();
77                if ( TupleType * tupleType = dynamic_cast< TupleType * > ( res ) ) {
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() ) {
81                                        explodeUnique( expr, alt, indexer, std::forward<Output>(out), isTupleAssign );
82                                }
83                        } else {
84                                // tuple type, but not tuple expr - recursively index into its components.
85                                // if expr type is reference, convert to value type
86                                Expression * arg = expr->clone();
87                                if ( Tuples::maybeImpureIgnoreUnique( arg ) ) {
88                                        // expressions which may contain side effects require a single unique instance of the expression.
89                                        arg = new UniqueExpr( arg );
90                                }
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                                }
95                                for ( unsigned int i = 0; i < tupleType->size(); i++ ) {
96                                        TupleIndexExpr * idx = new TupleIndexExpr( arg->clone(), i );
97                                        explodeUnique( idx, alt, indexer, std::forward<Output>(out), isTupleAssign );
98                                }
99                        }
100                } else {
101                        // atomic (non-tuple) type - output a clone of the expression in a new alternative
102                        append( std::forward<Output>(out), expr->clone(), alt.env, alt.cost, alt.cvtCost );
103                }
104        }
105
106        /// expands a tuple-valued alternative into multiple alternatives, each with a non-tuple-type
107        template< typename Output >
108        void explode( const ResolvExpr::Alternative &alt, const SymTab::Indexer & indexer,
109                        Output&& out, bool isTupleAssign = false ) {
110                explodeUnique( alt.expr, alt, indexer, std::forward<Output>(out), isTupleAssign );
111        }
112
113        // explode list of alternatives
114        template< typename AltIterator, typename Output >
115        void explode( AltIterator altBegin, AltIterator altEnd, const SymTab::Indexer & indexer,
116                        Output&& out, bool isTupleAssign = false ) {
117                for ( ; altBegin != altEnd; ++altBegin ) {
118                        explode( *altBegin, indexer, std::forward<Output>(out), isTupleAssign );
119                }
120        }
121
122        template< typename Output >
123        void explode( const ResolvExpr::AltList & alts, const SymTab::Indexer & indexer, Output&& out,
124                        bool isTupleAssign = false ) {
125                explode( alts.begin(), alts.end(), indexer, std::forward<Output>(out), isTupleAssign );
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.