[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] | 27 | namespace SymTab { |
---|
| 28 | class Indexer; |
---|
| 29 | } // namespace SymTab |
---|
[141b786] | 30 | |
---|
| 31 | namespace 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, |
---|
[6d6e829] | 46 | const ResolvExpr::OpenVarSet& openVars, const ResolvExpr::AssertionList& need, |
---|
[62194cb] | 47 | const ResolvExpr::Cost& cost, const ResolvExpr::Cost& cvtCost ) { |
---|
[6d6e829] | 48 | *out++ = ResolvExpr::Alternative{ expr, env, openVars, need, cost, cvtCost }; |
---|
[62194cb] | 49 | } |
---|
| 50 | |
---|
| 51 | /// Append alternative to an ExplodedActual |
---|
[490ff5c3] | 52 | static inline void append( ResolvExpr::ExplodedActual& ea, Expression* expr, |
---|
[6d6e829] | 53 | const ResolvExpr::TypeEnvironment&, const ResolvExpr::OpenVarSet&, |
---|
| 54 | const ResolvExpr::AssertionList&, const ResolvExpr::Cost&, const ResolvExpr::Cost& ) { |
---|
[62194cb] | 55 | ea.exprs.emplace_back( expr ); |
---|
[6d6e829] | 56 | /// xxx -- merge environment, openVars, need, cost? |
---|
[62194cb] | 57 | } |
---|
| 58 | |
---|
[141b786] | 59 | /// helper function used by explode |
---|
[62194cb] | 60 | template< typename Output > |
---|
[490ff5c3] | 61 | void explodeUnique( Expression * expr, const ResolvExpr::Alternative & alt, |
---|
[62194cb] | 62 | const SymTab::Indexer & indexer, Output&& out, bool isTupleAssign ) { |
---|
[0b5d871] | 63 | if ( isTupleAssign ) { |
---|
| 64 | // tuple assignment needs CastExprs to be recursively exploded to easily get at all of the components |
---|
[6bbce58] | 65 | if ( CastExpr * castExpr = isReferenceCast( expr ) ) { |
---|
[0b5d871] | 66 | ResolvExpr::AltList alts; |
---|
[490ff5c3] | 67 | explodeUnique( |
---|
[62194cb] | 68 | castExpr->get_arg(), alt, indexer, back_inserter( alts ), isTupleAssign ); |
---|
[0b5d871] | 69 | for ( ResolvExpr::Alternative & alt : alts ) { |
---|
| 70 | // distribute reference cast over all components |
---|
[490ff5c3] | 71 | append( std::forward<Output>(out), distributeReference( alt.release_expr() ), |
---|
[6d6e829] | 72 | alt.env, alt.openVars, alt.need, alt.cost, alt.cvtCost ); |
---|
[0b5d871] | 73 | } |
---|
| 74 | // 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) |
---|
| 75 | return; |
---|
| 76 | } |
---|
| 77 | } |
---|
[6d267ca] | 78 | Type * res = expr->get_result()->stripReferences(); |
---|
[f831177] | 79 | if ( TupleType * tupleType = dynamic_cast< TupleType * > ( res ) ) { |
---|
[141b786] | 80 | if ( TupleExpr * tupleExpr = dynamic_cast< TupleExpr * >( expr ) ) { |
---|
| 81 | // can open tuple expr and dump its exploded components |
---|
| 82 | for ( Expression * expr : tupleExpr->get_exprs() ) { |
---|
[62194cb] | 83 | explodeUnique( expr, alt, indexer, std::forward<Output>(out), isTupleAssign ); |
---|
[141b786] | 84 | } |
---|
| 85 | } else { |
---|
[6d267ca] | 86 | // tuple type, but not tuple expr - recursively index into its components. |
---|
| 87 | // if expr type is reference, convert to value type |
---|
[141b786] | 88 | Expression * arg = expr->clone(); |
---|
[0b5d871] | 89 | if ( Tuples::maybeImpureIgnoreUnique( arg ) ) { |
---|
[141b786] | 90 | // expressions which may contain side effects require a single unique instance of the expression. |
---|
| 91 | arg = new UniqueExpr( arg ); |
---|
| 92 | } |
---|
[0b5d871] | 93 | // cast reference to value type to facilitate further explosion |
---|
| 94 | if ( dynamic_cast<ReferenceType *>( arg->get_result() ) ) { |
---|
| 95 | arg = new CastExpr( arg, tupleType->clone() ); |
---|
| 96 | } |
---|
[141b786] | 97 | for ( unsigned int i = 0; i < tupleType->size(); i++ ) { |
---|
| 98 | TupleIndexExpr * idx = new TupleIndexExpr( arg->clone(), i ); |
---|
[62194cb] | 99 | explodeUnique( idx, alt, indexer, std::forward<Output>(out), isTupleAssign ); |
---|
[141b786] | 100 | delete idx; |
---|
| 101 | } |
---|
| 102 | delete arg; |
---|
| 103 | } |
---|
| 104 | } else { |
---|
| 105 | // atomic (non-tuple) type - output a clone of the expression in a new alternative |
---|
[6d6e829] | 106 | append( std::forward<Output>(out), expr->clone(), alt.env, alt.openVars, alt.need, |
---|
| 107 | alt.cost, alt.cvtCost ); |
---|
[141b786] | 108 | } |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | /// expands a tuple-valued alternative into multiple alternatives, each with a non-tuple-type |
---|
[62194cb] | 112 | template< typename Output > |
---|
[490ff5c3] | 113 | void explode( const ResolvExpr::Alternative &alt, const SymTab::Indexer & indexer, |
---|
[62194cb] | 114 | Output&& out, bool isTupleAssign = false ) { |
---|
| 115 | explodeUnique( alt.expr, alt, indexer, std::forward<Output>(out), isTupleAssign ); |
---|
[141b786] | 116 | } |
---|
| 117 | |
---|
| 118 | // explode list of alternatives |
---|
[62194cb] | 119 | template< typename AltIterator, typename Output > |
---|
[490ff5c3] | 120 | void explode( AltIterator altBegin, AltIterator altEnd, const SymTab::Indexer & indexer, |
---|
[62194cb] | 121 | Output&& out, bool isTupleAssign = false ) { |
---|
[141b786] | 122 | for ( ; altBegin != altEnd; ++altBegin ) { |
---|
[62194cb] | 123 | explode( *altBegin, indexer, std::forward<Output>(out), isTupleAssign ); |
---|
[141b786] | 124 | } |
---|
| 125 | } |
---|
| 126 | |
---|
[62194cb] | 127 | template< typename Output > |
---|
[490ff5c3] | 128 | void explode( const ResolvExpr::AltList & alts, const SymTab::Indexer & indexer, Output&& out, |
---|
[62194cb] | 129 | bool isTupleAssign = false ) { |
---|
| 130 | explode( alts.begin(), alts.end(), indexer, std::forward<Output>(out), isTupleAssign ); |
---|
[141b786] | 131 | } |
---|
| 132 | } // namespace Tuples |
---|
| 133 | |
---|
| 134 | // Local Variables: // |
---|
| 135 | // tab-width: 4 // |
---|
| 136 | // mode: c++ // |
---|
| 137 | // compile-command: "make install" // |
---|
| 138 | // End: // |
---|