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 | |
---|
23 | #include "SynTree/Expression.h" |
---|
24 | #include "SynTree/Declaration.h" |
---|
25 | #include "SynTree/Type.h" |
---|
26 | |
---|
27 | namespace Tuples { |
---|
28 | // TupleAssignment.cc |
---|
29 | void handleTupleAssignment( ResolvExpr::AlternativeFinder & currentFinder, UntypedExpr * assign, const std::list<ResolvExpr::AltList> & possibilities ); |
---|
30 | |
---|
31 | // TupleExpansion.cc |
---|
32 | void expandTuples( std::list< Declaration * > & translationUnit ); |
---|
33 | |
---|
34 | void expandUniqueExpr( std::list< Declaration * > & translationUnit ); |
---|
35 | |
---|
36 | /// returns VoidType if any of the expressions have Voidtype, otherwise TupleType of the Expression result types |
---|
37 | Type * makeTupleType( const std::list< Expression * > & exprs ); |
---|
38 | |
---|
39 | bool maybeImpure( Expression * expr ); |
---|
40 | |
---|
41 | |
---|
42 | /// helper function used by explode |
---|
43 | template< typename OutputIterator > |
---|
44 | void explodeUnique( Expression * expr, const ResolvExpr::Alternative & alt, OutputIterator out ) { |
---|
45 | Type * res = expr->get_result(); |
---|
46 | if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( expr ) ) { |
---|
47 | ResolvExpr::AltList alts; |
---|
48 | explodeUnique( addrExpr->get_arg(), alt, back_inserter( alts ) ); |
---|
49 | for ( ResolvExpr::Alternative & alt : alts ) { |
---|
50 | // distribute '&' over all components |
---|
51 | alt.expr = new AddressExpr( alt.expr ); |
---|
52 | *out++ = alt; |
---|
53 | } |
---|
54 | } else if ( TupleType * tupleType = dynamic_cast< TupleType * > ( res ) ) { |
---|
55 | if ( TupleExpr * tupleExpr = dynamic_cast< TupleExpr * >( expr ) ) { |
---|
56 | // can open tuple expr and dump its exploded components |
---|
57 | for ( Expression * expr : tupleExpr->get_exprs() ) { |
---|
58 | explodeUnique( expr, alt, out ); |
---|
59 | } |
---|
60 | } else { |
---|
61 | // tuple type, but not tuple expr - recursively index into its components |
---|
62 | Expression * arg = expr->clone(); |
---|
63 | if ( Tuples::maybeImpure( arg ) ) { |
---|
64 | // expressions which may contain side effects require a single unique instance of the expression |
---|
65 | arg = new UniqueExpr( arg ); |
---|
66 | } |
---|
67 | for ( unsigned int i = 0; i < tupleType->size(); i++ ) { |
---|
68 | TupleIndexExpr * idx = new TupleIndexExpr( arg->clone(), i ); |
---|
69 | explodeUnique( idx, alt, out ); |
---|
70 | delete idx; |
---|
71 | } |
---|
72 | delete arg; |
---|
73 | } |
---|
74 | } else { |
---|
75 | // atomic (non-tuple) type - output a clone of the expression in a new alternative |
---|
76 | *out++ = ResolvExpr::Alternative( expr->clone(), alt.env, alt.cost, alt.cvtCost ); |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | /// expands a tuple-valued alternative into multiple alternatives, each with a non-tuple-type |
---|
81 | template< typename OutputIterator > |
---|
82 | void explode( const ResolvExpr::Alternative &alt, OutputIterator out ) { |
---|
83 | explodeUnique( alt.expr, alt, out ); |
---|
84 | } |
---|
85 | |
---|
86 | // explode list of alternatives |
---|
87 | template< typename AltIterator, typename OutputIterator > |
---|
88 | void explode( AltIterator altBegin, AltIterator altEnd, OutputIterator out ) { |
---|
89 | for ( ; altBegin != altEnd; ++altBegin ) { |
---|
90 | explode( *altBegin, out ); |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | template< typename OutputIterator > |
---|
95 | void explode( const ResolvExpr::AltList & alts, OutputIterator out ) { |
---|
96 | explode( alts.begin(), alts.end(), out ); |
---|
97 | } |
---|
98 | } // namespace Tuples |
---|
99 | |
---|
100 | #endif // _TUPLE_ASSIGNMENT_H_ |
---|
101 | |
---|
102 | // Local Variables: // |
---|
103 | // tab-width: 4 // |
---|
104 | // mode: c++ // |
---|
105 | // compile-command: "make install" // |
---|
106 | // End: // |
---|