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