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