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 | // Explode.h -- |
---|
8 | // |
---|
9 | // Author : Rob Schluntz |
---|
10 | // Created On : Wed Nov 9 13:12:24 2016 |
---|
11 | // Last Modified By : Rob Schluntz |
---|
12 | // Last Modified On : Wed Nov 9 13:20:24 2016 |
---|
13 | // Update Count : 2 |
---|
14 | // |
---|
15 | |
---|
16 | #ifndef _EXPLODE_H_ |
---|
17 | #define _EXPLODE_H_ |
---|
18 | |
---|
19 | #include "ResolvExpr/AlternativeFinder.h" |
---|
20 | #include "ResolvExpr/Resolver.h" |
---|
21 | |
---|
22 | #include "SynTree/Expression.h" |
---|
23 | #include "SynTree/Declaration.h" |
---|
24 | #include "SynTree/Type.h" |
---|
25 | |
---|
26 | #include "Tuples.h" |
---|
27 | |
---|
28 | namespace Tuples { |
---|
29 | /// helper function used by explode to properly distribute |
---|
30 | /// '&' across a tuple expression |
---|
31 | Expression * distributeAddr( Expression * expr ); |
---|
32 | |
---|
33 | /// helper function used by explode |
---|
34 | template< typename OutputIterator > |
---|
35 | void explodeUnique( Expression * expr, const ResolvExpr::Alternative & alt, const SymTab::Indexer & indexer, OutputIterator out ) { |
---|
36 | Type * res = expr->get_result(); |
---|
37 | if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( expr ) ) { |
---|
38 | ResolvExpr::AltList alts; |
---|
39 | explodeUnique( addrExpr->get_arg(), alt, indexer, back_inserter( alts ) ); |
---|
40 | for ( ResolvExpr::Alternative & alt : alts ) { |
---|
41 | // distribute '&' over all components |
---|
42 | alt.expr = distributeAddr( alt.expr ); |
---|
43 | *out++ = alt; |
---|
44 | } |
---|
45 | } else if ( TupleType * tupleType = dynamic_cast< TupleType * > ( res ) ) { |
---|
46 | if ( TupleExpr * tupleExpr = dynamic_cast< TupleExpr * >( expr ) ) { |
---|
47 | // can open tuple expr and dump its exploded components |
---|
48 | for ( Expression * expr : tupleExpr->get_exprs() ) { |
---|
49 | explodeUnique( expr, alt, indexer, out ); |
---|
50 | } |
---|
51 | } else { |
---|
52 | // tuple type, but not tuple expr - recursively index into its components |
---|
53 | Expression * arg = expr->clone(); |
---|
54 | if ( Tuples::maybeImpure( arg ) && ! dynamic_cast< UniqueExpr * >( arg ) ) { |
---|
55 | // expressions which may contain side effects require a single unique instance of the expression. |
---|
56 | arg = new UniqueExpr( arg ); |
---|
57 | } |
---|
58 | for ( unsigned int i = 0; i < tupleType->size(); i++ ) { |
---|
59 | TupleIndexExpr * idx = new TupleIndexExpr( arg->clone(), i ); |
---|
60 | explodeUnique( idx, alt, indexer, out ); |
---|
61 | delete idx; |
---|
62 | } |
---|
63 | delete arg; |
---|
64 | } |
---|
65 | } else { |
---|
66 | // atomic (non-tuple) type - output a clone of the expression in a new alternative |
---|
67 | *out++ = ResolvExpr::Alternative( expr->clone(), alt.env, alt.cost, alt.cvtCost ); |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | /// expands a tuple-valued alternative into multiple alternatives, each with a non-tuple-type |
---|
72 | template< typename OutputIterator > |
---|
73 | void explode( const ResolvExpr::Alternative &alt, const SymTab::Indexer & indexer, OutputIterator out ) { |
---|
74 | explodeUnique( alt.expr, alt, indexer, out ); |
---|
75 | } |
---|
76 | |
---|
77 | // explode list of alternatives |
---|
78 | template< typename AltIterator, typename OutputIterator > |
---|
79 | void explode( AltIterator altBegin, AltIterator altEnd, const SymTab::Indexer & indexer, OutputIterator out ) { |
---|
80 | for ( ; altBegin != altEnd; ++altBegin ) { |
---|
81 | explode( *altBegin, indexer, out ); |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | template< typename OutputIterator > |
---|
86 | void explode( const ResolvExpr::AltList & alts, const SymTab::Indexer & indexer, OutputIterator out ) { |
---|
87 | explode( alts.begin(), alts.end(), indexer, out ); |
---|
88 | } |
---|
89 | } // namespace Tuples |
---|
90 | |
---|
91 | #endif // _TUPLE_ASSIGNMENT_H_ |
---|
92 | |
---|
93 | // Local Variables: // |
---|
94 | // tab-width: 4 // |
---|
95 | // mode: c++ // |
---|
96 | // compile-command: "make install" // |
---|
97 | // End: // |
---|