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