[51587aa] | 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 | // |
---|
[6eb8948] | 7 | // Tuples.h -- |
---|
[51587aa] | 8 | // |
---|
[843054c2] | 9 | // Author : Rodolfo G. Esteves |
---|
[51587aa] | 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
| 12 | // Last Modified On : Mon May 18 15:04:02 2015 |
---|
| 13 | // Update Count : 2 |
---|
| 14 | // |
---|
| 15 | |
---|
[6eb8948] | 16 | #ifndef _TUPLES_H_ |
---|
[65660bd] | 17 | #define _TUPLES_H_ |
---|
[51b7345] | 18 | |
---|
| 19 | #include <string> |
---|
| 20 | #include <vector> |
---|
| 21 | #include "ResolvExpr/AlternativeFinder.h" |
---|
| 22 | |
---|
| 23 | #include "SynTree/Expression.h" |
---|
| 24 | #include "SynTree/Declaration.h" |
---|
| 25 | #include "SynTree/Type.h" |
---|
| 26 | |
---|
| 27 | namespace Tuples { |
---|
[6eb8948] | 28 | // TupleAssignment.cc |
---|
[65660bd] | 29 | void handleTupleAssignment( ResolvExpr::AlternativeFinder & currentFinder, UntypedExpr * assign, const std::list<ResolvExpr::AltList> & possibilities ); |
---|
[6eb8948] | 30 | |
---|
| 31 | // TupleExpansion.cc |
---|
[bf32bb8] | 32 | /// expands z.[a, b.[x, y], c] into [z.a, z.b.x, z.b.y, z.c], inserting UniqueExprs as appropriate |
---|
| 33 | void expandMemberTuples( std::list< Declaration * > & translationUnit ); |
---|
| 34 | |
---|
| 35 | /// replaces tuple-related elements, such as TupleType, TupleExpr, TupleAssignExpr, etc. |
---|
[6eb8948] | 36 | void expandTuples( std::list< Declaration * > & translationUnit ); |
---|
[3c13c03] | 37 | |
---|
[bf32bb8] | 38 | /// replaces UniqueExprs with a temporary variable and one call |
---|
| 39 | void expandUniqueExpr( std::list< Declaration * > & translationUnit ); |
---|
[aefcc3b] | 40 | |
---|
[bf32bb8] | 41 | /// returns VoidType if any of the expressions have Voidtype, otherwise TupleType of the Expression result types |
---|
| 42 | Type * makeTupleType( const std::list< Expression * > & exprs ); |
---|
[65660bd] | 43 | |
---|
[bf32bb8] | 44 | /// returns true if the expression may contain side-effects. |
---|
| 45 | bool maybeImpure( Expression * expr ); |
---|
[65660bd] | 46 | |
---|
| 47 | |
---|
| 48 | /// helper function used by explode |
---|
| 49 | template< typename OutputIterator > |
---|
| 50 | void explodeUnique( Expression * expr, const ResolvExpr::Alternative & alt, OutputIterator out ) { |
---|
| 51 | Type * res = expr->get_result(); |
---|
| 52 | if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( expr ) ) { |
---|
| 53 | ResolvExpr::AltList alts; |
---|
| 54 | explodeUnique( addrExpr->get_arg(), alt, back_inserter( alts ) ); |
---|
| 55 | for ( ResolvExpr::Alternative & alt : alts ) { |
---|
| 56 | // distribute '&' over all components |
---|
| 57 | alt.expr = new AddressExpr( alt.expr ); |
---|
| 58 | *out++ = alt; |
---|
| 59 | } |
---|
| 60 | } else if ( TupleType * tupleType = dynamic_cast< TupleType * > ( res ) ) { |
---|
| 61 | if ( TupleExpr * tupleExpr = dynamic_cast< TupleExpr * >( expr ) ) { |
---|
| 62 | // can open tuple expr and dump its exploded components |
---|
| 63 | for ( Expression * expr : tupleExpr->get_exprs() ) { |
---|
| 64 | explodeUnique( expr, alt, out ); |
---|
| 65 | } |
---|
| 66 | } else { |
---|
| 67 | // tuple type, but not tuple expr - recursively index into its components |
---|
| 68 | Expression * arg = expr->clone(); |
---|
| 69 | if ( Tuples::maybeImpure( arg ) ) { |
---|
| 70 | // expressions which may contain side effects require a single unique instance of the expression |
---|
| 71 | arg = new UniqueExpr( arg ); |
---|
| 72 | } |
---|
| 73 | for ( unsigned int i = 0; i < tupleType->size(); i++ ) { |
---|
| 74 | TupleIndexExpr * idx = new TupleIndexExpr( arg->clone(), i ); |
---|
| 75 | explodeUnique( idx, alt, out ); |
---|
| 76 | delete idx; |
---|
| 77 | } |
---|
| 78 | delete arg; |
---|
| 79 | } |
---|
| 80 | } else { |
---|
| 81 | // atomic (non-tuple) type - output a clone of the expression in a new alternative |
---|
| 82 | *out++ = ResolvExpr::Alternative( expr->clone(), alt.env, alt.cost, alt.cvtCost ); |
---|
| 83 | } |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | /// expands a tuple-valued alternative into multiple alternatives, each with a non-tuple-type |
---|
| 87 | template< typename OutputIterator > |
---|
| 88 | void explode( const ResolvExpr::Alternative &alt, OutputIterator out ) { |
---|
| 89 | explodeUnique( alt.expr, alt, out ); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | // explode list of alternatives |
---|
| 93 | template< typename AltIterator, typename OutputIterator > |
---|
| 94 | void explode( AltIterator altBegin, AltIterator altEnd, OutputIterator out ) { |
---|
| 95 | for ( ; altBegin != altEnd; ++altBegin ) { |
---|
| 96 | explode( *altBegin, out ); |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | template< typename OutputIterator > |
---|
| 101 | void explode( const ResolvExpr::AltList & alts, OutputIterator out ) { |
---|
| 102 | explode( alts.begin(), alts.end(), out ); |
---|
| 103 | } |
---|
[51b7345] | 104 | } // namespace Tuples |
---|
| 105 | |
---|
[51587aa] | 106 | #endif // _TUPLE_ASSIGNMENT_H_ |
---|
[51b7345] | 107 | |
---|
[51587aa] | 108 | // Local Variables: // |
---|
| 109 | // tab-width: 4 // |
---|
| 110 | // mode: c++ // |
---|
| 111 | // compile-command: "make install" // |
---|
| 112 | // End: // |
---|