[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 |
---|
| 11 | // Last Modified By : Rob Schluntz |
---|
| 12 | // Last Modified On : Wed Nov 9 13:20:24 2016 |
---|
| 13 | // Update Count : 2 |
---|
| 14 | // |
---|
| 15 | |
---|
| 16 | #ifndef _EXPLODE_H_ |
---|
| 17 | #define _EXPLODE_H_ |
---|
| 18 | |
---|
| 19 | #include "ResolvExpr/AlternativeFinder.h" |
---|
| 20 | #include "ResolvExpr/Resolver.h" |
---|
| 21 | |
---|
| 22 | #include "SynTree/Expression.h" |
---|
| 23 | #include "SynTree/Declaration.h" |
---|
| 24 | #include "SynTree/Type.h" |
---|
| 25 | |
---|
| 26 | #include "Tuples.h" |
---|
| 27 | |
---|
| 28 | namespace Tuples { |
---|
| 29 | /// helper function used by explode |
---|
| 30 | template< typename OutputIterator > |
---|
[f831177] | 31 | void explodeUnique( Expression * expr, const ResolvExpr::Alternative & alt, const SymTab::Indexer & indexer, OutputIterator out, bool isTupleAssign ) { |
---|
| 32 | Type * res = expr->get_result(); |
---|
| 33 | if ( TupleType * tupleType = dynamic_cast< TupleType * > ( res ) ) { |
---|
[141b786] | 34 | if ( TupleExpr * tupleExpr = dynamic_cast< TupleExpr * >( expr ) ) { |
---|
| 35 | // can open tuple expr and dump its exploded components |
---|
| 36 | for ( Expression * expr : tupleExpr->get_exprs() ) { |
---|
[f831177] | 37 | explodeUnique( expr, alt, indexer, out, isTupleAssign ); |
---|
[141b786] | 38 | } |
---|
| 39 | } else { |
---|
| 40 | // tuple type, but not tuple expr - recursively index into its components |
---|
| 41 | Expression * arg = expr->clone(); |
---|
| 42 | if ( Tuples::maybeImpure( arg ) && ! dynamic_cast< UniqueExpr * >( arg ) ) { |
---|
| 43 | // expressions which may contain side effects require a single unique instance of the expression. |
---|
| 44 | arg = new UniqueExpr( arg ); |
---|
| 45 | } |
---|
| 46 | for ( unsigned int i = 0; i < tupleType->size(); i++ ) { |
---|
| 47 | TupleIndexExpr * idx = new TupleIndexExpr( arg->clone(), i ); |
---|
[f831177] | 48 | explodeUnique( idx, alt, indexer, out, isTupleAssign ); |
---|
[141b786] | 49 | delete idx; |
---|
| 50 | } |
---|
| 51 | delete arg; |
---|
| 52 | } |
---|
| 53 | } else { |
---|
| 54 | // atomic (non-tuple) type - output a clone of the expression in a new alternative |
---|
| 55 | *out++ = ResolvExpr::Alternative( expr->clone(), alt.env, alt.cost, alt.cvtCost ); |
---|
| 56 | } |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | /// expands a tuple-valued alternative into multiple alternatives, each with a non-tuple-type |
---|
| 60 | template< typename OutputIterator > |
---|
[f831177] | 61 | void explode( const ResolvExpr::Alternative &alt, const SymTab::Indexer & indexer, OutputIterator out, bool isTupleAssign = false ) { |
---|
| 62 | explodeUnique( alt.expr, alt, indexer, out, isTupleAssign ); |
---|
[141b786] | 63 | } |
---|
| 64 | |
---|
| 65 | // explode list of alternatives |
---|
| 66 | template< typename AltIterator, typename OutputIterator > |
---|
[f831177] | 67 | void explode( AltIterator altBegin, AltIterator altEnd, const SymTab::Indexer & indexer, OutputIterator out, bool isTupleAssign = false ) { |
---|
[141b786] | 68 | for ( ; altBegin != altEnd; ++altBegin ) { |
---|
[f831177] | 69 | explode( *altBegin, indexer, out, isTupleAssign ); |
---|
[141b786] | 70 | } |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | template< typename OutputIterator > |
---|
[f831177] | 74 | void explode( const ResolvExpr::AltList & alts, const SymTab::Indexer & indexer, OutputIterator out, bool isTupleAssign = false ) { |
---|
| 75 | explode( alts.begin(), alts.end(), indexer, out, isTupleAssign ); |
---|
[141b786] | 76 | } |
---|
| 77 | } // namespace Tuples |
---|
| 78 | |
---|
| 79 | #endif // _TUPLE_ASSIGNMENT_H_ |
---|
| 80 | |
---|
| 81 | // Local Variables: // |
---|
| 82 | // tab-width: 4 // |
---|
| 83 | // mode: c++ // |
---|
| 84 | // compile-command: "make install" // |
---|
| 85 | // End: // |
---|