// // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo // // The contents of this file are covered under the licence agreement in the // file "LICENCE" distributed with Cforall. // // Explode.h -- // // Author : Rob Schluntz // Created On : Wed Nov 9 13:12:24 2016 // Last Modified By : Rob Schluntz // Last Modified On : Wed Nov 9 13:20:24 2016 // Update Count : 2 // #ifndef _EXPLODE_H_ #define _EXPLODE_H_ #include "ResolvExpr/AlternativeFinder.h" #include "ResolvExpr/Resolver.h" #include "SynTree/Expression.h" #include "SynTree/Declaration.h" #include "SynTree/Type.h" #include "Tuples.h" namespace Tuples { /// helper function used by explode template< typename OutputIterator > void explodeUnique( Expression * expr, const ResolvExpr::Alternative & alt, const SymTab::Indexer & indexer, OutputIterator out, bool isTupleAssign ) { Type * res = expr->get_result(); if ( TupleType * tupleType = dynamic_cast< TupleType * > ( res ) ) { if ( TupleExpr * tupleExpr = dynamic_cast< TupleExpr * >( expr ) ) { // can open tuple expr and dump its exploded components for ( Expression * expr : tupleExpr->get_exprs() ) { explodeUnique( expr, alt, indexer, out, isTupleAssign ); } } else { // tuple type, but not tuple expr - recursively index into its components Expression * arg = expr->clone(); if ( Tuples::maybeImpure( arg ) && ! dynamic_cast< UniqueExpr * >( arg ) ) { // expressions which may contain side effects require a single unique instance of the expression. arg = new UniqueExpr( arg ); } for ( unsigned int i = 0; i < tupleType->size(); i++ ) { TupleIndexExpr * idx = new TupleIndexExpr( arg->clone(), i ); explodeUnique( idx, alt, indexer, out, isTupleAssign ); delete idx; } delete arg; } } else { // atomic (non-tuple) type - output a clone of the expression in a new alternative *out++ = ResolvExpr::Alternative( expr->clone(), alt.env, alt.cost, alt.cvtCost ); } } /// expands a tuple-valued alternative into multiple alternatives, each with a non-tuple-type template< typename OutputIterator > void explode( const ResolvExpr::Alternative &alt, const SymTab::Indexer & indexer, OutputIterator out, bool isTupleAssign = false ) { explodeUnique( alt.expr, alt, indexer, out, isTupleAssign ); } // explode list of alternatives template< typename AltIterator, typename OutputIterator > void explode( AltIterator altBegin, AltIterator altEnd, const SymTab::Indexer & indexer, OutputIterator out, bool isTupleAssign = false ) { for ( ; altBegin != altEnd; ++altBegin ) { explode( *altBegin, indexer, out, isTupleAssign ); } } template< typename OutputIterator > void explode( const ResolvExpr::AltList & alts, const SymTab::Indexer & indexer, OutputIterator out, bool isTupleAssign = false ) { explode( alts.begin(), alts.end(), indexer, out, isTupleAssign ); } } // namespace Tuples #endif // _TUPLE_ASSIGNMENT_H_ // Local Variables: // // tab-width: 4 // // mode: c++ // // compile-command: "make install" // // End: //