| 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 | // Tuples.h --
|
|---|
| 8 | //
|
|---|
| 9 | // Author : Rodolfo G. Esteves
|
|---|
| 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 |
|
|---|
| 16 | #ifndef _TUPLES_H_
|
|---|
| 17 | #define _TUPLES_H_
|
|---|
| 18 |
|
|---|
| 19 | #include <string>
|
|---|
| 20 | #include <vector>
|
|---|
| 21 | #include "ResolvExpr/AlternativeFinder.h"
|
|---|
| 22 | #include "ResolvExpr/Resolver.h"
|
|---|
| 23 |
|
|---|
| 24 | #include "SynTree/Expression.h"
|
|---|
| 25 | #include "SynTree/Declaration.h"
|
|---|
| 26 | #include "SynTree/Type.h"
|
|---|
| 27 |
|
|---|
| 28 | namespace Tuples {
|
|---|
| 29 | // TupleAssignment.cc
|
|---|
| 30 | void handleTupleAssignment( ResolvExpr::AlternativeFinder & currentFinder, UntypedExpr * assign, const std::list<ResolvExpr::AltList> & possibilities );
|
|---|
| 31 |
|
|---|
| 32 | // TupleExpansion.cc
|
|---|
| 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.
|
|---|
| 37 | void expandTuples( std::list< Declaration * > & translationUnit );
|
|---|
| 38 |
|
|---|
| 39 | /// replaces UniqueExprs with a temporary variable and one call
|
|---|
| 40 | void expandUniqueExpr( std::list< Declaration * > & translationUnit );
|
|---|
| 41 |
|
|---|
| 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 );
|
|---|
| 44 |
|
|---|
| 45 | /// returns true if the expression may contain side-effects.
|
|---|
| 46 | bool maybeImpure( Expression * expr );
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 | /// helper function used by explode
|
|---|
| 50 | template< typename OutputIterator >
|
|---|
| 51 | void explodeUnique( Expression * expr, const ResolvExpr::Alternative & alt, const SymTab::Indexer & indexer, OutputIterator out ) {
|
|---|
| 52 | Type * res = expr->get_result();
|
|---|
| 53 | if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( expr ) ) {
|
|---|
| 54 | ResolvExpr::AltList alts;
|
|---|
| 55 | explodeUnique( addrExpr->get_arg(), alt, indexer, back_inserter( alts ) );
|
|---|
| 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() ) {
|
|---|
| 65 | explodeUnique( expr, alt, indexer, out );
|
|---|
| 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 ) ) {
|
|---|
| 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.
|
|---|
| 74 | arg = new UniqueExpr( arg );
|
|---|
| 75 | arg = ResolvExpr::resolveInVoidContext( arg, indexer );
|
|---|
| 76 | }
|
|---|
| 77 | for ( unsigned int i = 0; i < tupleType->size(); i++ ) {
|
|---|
| 78 | TupleIndexExpr * idx = new TupleIndexExpr( arg->clone(), i );
|
|---|
| 79 | explodeUnique( idx, alt, indexer, out );
|
|---|
| 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 >
|
|---|
| 92 | void explode( const ResolvExpr::Alternative &alt, const SymTab::Indexer & indexer, OutputIterator out ) {
|
|---|
| 93 | explodeUnique( alt.expr, alt, indexer, out );
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | // explode list of alternatives
|
|---|
| 97 | template< typename AltIterator, typename OutputIterator >
|
|---|
| 98 | void explode( AltIterator altBegin, AltIterator altEnd, const SymTab::Indexer & indexer, OutputIterator out ) {
|
|---|
| 99 | for ( ; altBegin != altEnd; ++altBegin ) {
|
|---|
| 100 | explode( *altBegin, indexer, out );
|
|---|
| 101 | }
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | template< typename OutputIterator >
|
|---|
| 105 | void explode( const ResolvExpr::AltList & alts, const SymTab::Indexer & indexer, OutputIterator out ) {
|
|---|
| 106 | explode( alts.begin(), alts.end(), indexer, out );
|
|---|
| 107 | }
|
|---|
| 108 | } // namespace Tuples
|
|---|
| 109 |
|
|---|
| 110 | #endif // _TUPLE_ASSIGNMENT_H_
|
|---|
| 111 |
|
|---|
| 112 | // Local Variables: //
|
|---|
| 113 | // tab-width: 4 //
|
|---|
| 114 | // mode: c++ //
|
|---|
| 115 | // compile-command: "make install" //
|
|---|
| 116 | // End: //
|
|---|