| [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 |
|
|---|
| 18 | #include "ResolvExpr/AlternativeFinder.h"
|
|---|
| 19 | #include "ResolvExpr/Resolver.h"
|
|---|
| 20 |
|
|---|
| 21 | #include "SynTree/Expression.h"
|
|---|
| 22 | #include "SynTree/Declaration.h"
|
|---|
| 23 | #include "SynTree/Type.h"
|
|---|
| 24 |
|
|---|
| 25 | #include "Tuples.h"
|
|---|
| 26 |
|
|---|
| 27 | namespace Tuples {
|
|---|
| 28 | /// helper function used by explode
|
|---|
| 29 | template< typename OutputIterator >
|
|---|
| [f831177] | 30 | void explodeUnique( Expression * expr, const ResolvExpr::Alternative & alt, const SymTab::Indexer & indexer, OutputIterator out, bool isTupleAssign ) {
|
|---|
| [6d267ca] | 31 | Type * res = expr->get_result()->stripReferences();
|
|---|
| [f831177] | 32 | if ( TupleType * tupleType = dynamic_cast< TupleType * > ( res ) ) {
|
|---|
| [141b786] | 33 | if ( TupleExpr * tupleExpr = dynamic_cast< TupleExpr * >( expr ) ) {
|
|---|
| 34 | // can open tuple expr and dump its exploded components
|
|---|
| 35 | for ( Expression * expr : tupleExpr->get_exprs() ) {
|
|---|
| [f831177] | 36 | explodeUnique( expr, alt, indexer, out, isTupleAssign );
|
|---|
| [141b786] | 37 | }
|
|---|
| 38 | } else {
|
|---|
| [6d267ca] | 39 | // tuple type, but not tuple expr - recursively index into its components.
|
|---|
| 40 | // if expr type is reference, convert to value type
|
|---|
| [141b786] | 41 | Expression * arg = expr->clone();
|
|---|
| [6d267ca] | 42 |
|
|---|
| 43 | if ( dynamic_cast<ReferenceType *>( arg->get_result() ) ) {
|
|---|
| 44 | // TODO: does this go here (inside uniqueexpr) or outside uniqueexpr? I'm guessing probably should go after...
|
|---|
| 45 | arg = new CastExpr( arg, tupleType->clone() );
|
|---|
| 46 | }
|
|---|
| [141b786] | 47 | if ( Tuples::maybeImpure( arg ) && ! dynamic_cast< UniqueExpr * >( arg ) ) {
|
|---|
| 48 | // expressions which may contain side effects require a single unique instance of the expression.
|
|---|
| 49 | arg = new UniqueExpr( arg );
|
|---|
| 50 | }
|
|---|
| 51 | for ( unsigned int i = 0; i < tupleType->size(); i++ ) {
|
|---|
| 52 | TupleIndexExpr * idx = new TupleIndexExpr( arg->clone(), i );
|
|---|
| [f831177] | 53 | explodeUnique( idx, alt, indexer, out, isTupleAssign );
|
|---|
| [141b786] | 54 | delete idx;
|
|---|
| 55 | }
|
|---|
| 56 | delete arg;
|
|---|
| 57 | }
|
|---|
| 58 | } else {
|
|---|
| 59 | // atomic (non-tuple) type - output a clone of the expression in a new alternative
|
|---|
| 60 | *out++ = ResolvExpr::Alternative( expr->clone(), alt.env, alt.cost, alt.cvtCost );
|
|---|
| 61 | }
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | /// expands a tuple-valued alternative into multiple alternatives, each with a non-tuple-type
|
|---|
| 65 | template< typename OutputIterator >
|
|---|
| [f831177] | 66 | void explode( const ResolvExpr::Alternative &alt, const SymTab::Indexer & indexer, OutputIterator out, bool isTupleAssign = false ) {
|
|---|
| 67 | explodeUnique( alt.expr, alt, indexer, out, isTupleAssign );
|
|---|
| [141b786] | 68 | }
|
|---|
| 69 |
|
|---|
| 70 | // explode list of alternatives
|
|---|
| 71 | template< typename AltIterator, typename OutputIterator >
|
|---|
| [f831177] | 72 | void explode( AltIterator altBegin, AltIterator altEnd, const SymTab::Indexer & indexer, OutputIterator out, bool isTupleAssign = false ) {
|
|---|
| [141b786] | 73 | for ( ; altBegin != altEnd; ++altBegin ) {
|
|---|
| [f831177] | 74 | explode( *altBegin, indexer, out, isTupleAssign );
|
|---|
| [141b786] | 75 | }
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | template< typename OutputIterator >
|
|---|
| [f831177] | 79 | void explode( const ResolvExpr::AltList & alts, const SymTab::Indexer & indexer, OutputIterator out, bool isTupleAssign = false ) {
|
|---|
| 80 | explode( alts.begin(), alts.end(), indexer, out, isTupleAssign );
|
|---|
| [141b786] | 81 | }
|
|---|
| 82 | } // namespace Tuples
|
|---|
| 83 |
|
|---|
| 84 | // Local Variables: //
|
|---|
| 85 | // tab-width: 4 //
|
|---|
| 86 | // mode: c++ //
|
|---|
| 87 | // compile-command: "make install" //
|
|---|
| 88 | // End: //
|
|---|