[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 | |
---|
[03321e4] | 18 | #include <iterator> // for back_inserter, back_insert_iterator |
---|
[141b786] | 19 | |
---|
[03321e4] | 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 |
---|
[141b786] | 24 | |
---|
[03321e4] | 25 | namespace SymTab { |
---|
| 26 | class Indexer; |
---|
| 27 | } // namespace SymTab |
---|
[141b786] | 28 | |
---|
| 29 | namespace Tuples { |
---|
[0b5d871] | 30 | Expression * distributeReference( Expression * ); |
---|
| 31 | |
---|
[141b786] | 32 | /// helper function used by explode |
---|
| 33 | template< typename OutputIterator > |
---|
[f831177] | 34 | void explodeUnique( Expression * expr, const ResolvExpr::Alternative & alt, const SymTab::Indexer & indexer, OutputIterator out, bool isTupleAssign ) { |
---|
[0b5d871] | 35 | if ( isTupleAssign ) { |
---|
| 36 | // tuple assignment needs CastExprs to be recursively exploded to easily get at all of the components |
---|
| 37 | if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) { |
---|
| 38 | ResolvExpr::AltList alts; |
---|
| 39 | explodeUnique( castExpr->get_arg(), alt, indexer, back_inserter( alts ), isTupleAssign ); |
---|
| 40 | for ( ResolvExpr::Alternative & alt : alts ) { |
---|
| 41 | // distribute reference cast over all components |
---|
| 42 | alt.expr = distributeReference( alt.expr ); |
---|
| 43 | *out++ = alt; |
---|
| 44 | } |
---|
| 45 | // 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) |
---|
| 46 | return; |
---|
| 47 | } |
---|
| 48 | } |
---|
[6d267ca] | 49 | Type * res = expr->get_result()->stripReferences(); |
---|
[f831177] | 50 | if ( TupleType * tupleType = dynamic_cast< TupleType * > ( res ) ) { |
---|
[141b786] | 51 | if ( TupleExpr * tupleExpr = dynamic_cast< TupleExpr * >( expr ) ) { |
---|
| 52 | // can open tuple expr and dump its exploded components |
---|
| 53 | for ( Expression * expr : tupleExpr->get_exprs() ) { |
---|
[f831177] | 54 | explodeUnique( expr, alt, indexer, out, isTupleAssign ); |
---|
[141b786] | 55 | } |
---|
| 56 | } else { |
---|
[6d267ca] | 57 | // tuple type, but not tuple expr - recursively index into its components. |
---|
| 58 | // if expr type is reference, convert to value type |
---|
[141b786] | 59 | Expression * arg = expr->clone(); |
---|
[0b5d871] | 60 | if ( Tuples::maybeImpureIgnoreUnique( arg ) ) { |
---|
[141b786] | 61 | // expressions which may contain side effects require a single unique instance of the expression. |
---|
| 62 | arg = new UniqueExpr( arg ); |
---|
| 63 | } |
---|
[0b5d871] | 64 | // cast reference to value type to facilitate further explosion |
---|
| 65 | if ( dynamic_cast<ReferenceType *>( arg->get_result() ) ) { |
---|
| 66 | arg = new CastExpr( arg, tupleType->clone() ); |
---|
| 67 | } |
---|
[141b786] | 68 | for ( unsigned int i = 0; i < tupleType->size(); i++ ) { |
---|
| 69 | TupleIndexExpr * idx = new TupleIndexExpr( arg->clone(), i ); |
---|
[f831177] | 70 | explodeUnique( idx, alt, indexer, out, isTupleAssign ); |
---|
[141b786] | 71 | delete idx; |
---|
| 72 | } |
---|
| 73 | delete arg; |
---|
| 74 | } |
---|
| 75 | } else { |
---|
| 76 | // atomic (non-tuple) type - output a clone of the expression in a new alternative |
---|
| 77 | *out++ = ResolvExpr::Alternative( expr->clone(), alt.env, alt.cost, alt.cvtCost ); |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | /// expands a tuple-valued alternative into multiple alternatives, each with a non-tuple-type |
---|
| 82 | template< typename OutputIterator > |
---|
[f831177] | 83 | void explode( const ResolvExpr::Alternative &alt, const SymTab::Indexer & indexer, OutputIterator out, bool isTupleAssign = false ) { |
---|
| 84 | explodeUnique( alt.expr, alt, indexer, out, isTupleAssign ); |
---|
[141b786] | 85 | } |
---|
| 86 | |
---|
| 87 | // explode list of alternatives |
---|
| 88 | template< typename AltIterator, typename OutputIterator > |
---|
[f831177] | 89 | void explode( AltIterator altBegin, AltIterator altEnd, const SymTab::Indexer & indexer, OutputIterator out, bool isTupleAssign = false ) { |
---|
[141b786] | 90 | for ( ; altBegin != altEnd; ++altBegin ) { |
---|
[f831177] | 91 | explode( *altBegin, indexer, out, isTupleAssign ); |
---|
[141b786] | 92 | } |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | template< typename OutputIterator > |
---|
[f831177] | 96 | void explode( const ResolvExpr::AltList & alts, const SymTab::Indexer & indexer, OutputIterator out, bool isTupleAssign = false ) { |
---|
| 97 | explode( alts.begin(), alts.end(), indexer, out, isTupleAssign ); |
---|
[141b786] | 98 | } |
---|
| 99 | } // namespace Tuples |
---|
| 100 | |
---|
| 101 | // Local Variables: // |
---|
| 102 | // tab-width: 4 // |
---|
| 103 | // mode: c++ // |
---|
| 104 | // compile-command: "make install" // |
---|
| 105 | // End: // |
---|