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