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 |
---|
30 | template< typename OutputIterator > |
---|
31 | void explodeUnique( Expression * expr, const ResolvExpr::Alternative & alt, const SymTab::Indexer & indexer, OutputIterator out, bool isTupleAssign ) { |
---|
32 | Type * res = expr->get_result()->stripReferences(); |
---|
33 | if ( TupleType * tupleType = dynamic_cast< TupleType * > ( res ) ) { |
---|
34 | if ( TupleExpr * tupleExpr = dynamic_cast< TupleExpr * >( expr ) ) { |
---|
35 | // can open tuple expr and dump its exploded components |
---|
36 | for ( Expression * expr : tupleExpr->get_exprs() ) { |
---|
37 | explodeUnique( expr, alt, indexer, out, isTupleAssign ); |
---|
38 | } |
---|
39 | } else { |
---|
40 | // tuple type, but not tuple expr - recursively index into its components. |
---|
41 | // if expr type is reference, convert to value type |
---|
42 | Expression * arg = expr->clone(); |
---|
43 | |
---|
44 | if ( dynamic_cast<ReferenceType *>( arg->get_result() ) ) { |
---|
45 | // TODO: does this go here (inside uniqueexpr) or outside uniqueexpr? I'm guessing probably should go after... |
---|
46 | arg = new CastExpr( arg, tupleType->clone() ); |
---|
47 | } |
---|
48 | if ( Tuples::maybeImpure( arg ) && ! dynamic_cast< UniqueExpr * >( arg ) ) { |
---|
49 | // expressions which may contain side effects require a single unique instance of the expression. |
---|
50 | arg = new UniqueExpr( arg ); |
---|
51 | } |
---|
52 | for ( unsigned int i = 0; i < tupleType->size(); i++ ) { |
---|
53 | TupleIndexExpr * idx = new TupleIndexExpr( arg->clone(), i ); |
---|
54 | explodeUnique( idx, alt, indexer, out, isTupleAssign ); |
---|
55 | delete idx; |
---|
56 | } |
---|
57 | delete arg; |
---|
58 | } |
---|
59 | } else { |
---|
60 | // atomic (non-tuple) type - output a clone of the expression in a new alternative |
---|
61 | *out++ = ResolvExpr::Alternative( expr->clone(), alt.env, alt.cost, alt.cvtCost ); |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | /// expands a tuple-valued alternative into multiple alternatives, each with a non-tuple-type |
---|
66 | template< typename OutputIterator > |
---|
67 | void explode( const ResolvExpr::Alternative &alt, const SymTab::Indexer & indexer, OutputIterator out, bool isTupleAssign = false ) { |
---|
68 | explodeUnique( alt.expr, alt, indexer, out, isTupleAssign ); |
---|
69 | } |
---|
70 | |
---|
71 | // explode list of alternatives |
---|
72 | template< typename AltIterator, typename OutputIterator > |
---|
73 | void explode( AltIterator altBegin, AltIterator altEnd, const SymTab::Indexer & indexer, OutputIterator out, bool isTupleAssign = false ) { |
---|
74 | for ( ; altBegin != altEnd; ++altBegin ) { |
---|
75 | explode( *altBegin, indexer, out, isTupleAssign ); |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | template< typename OutputIterator > |
---|
80 | void explode( const ResolvExpr::AltList & alts, const SymTab::Indexer & indexer, OutputIterator out, bool isTupleAssign = false ) { |
---|
81 | explode( alts.begin(), alts.end(), indexer, out, isTupleAssign ); |
---|
82 | } |
---|
83 | } // namespace Tuples |
---|
84 | |
---|
85 | #endif // _TUPLE_ASSIGNMENT_H_ |
---|
86 | |
---|
87 | // Local Variables: // |
---|
88 | // tab-width: 4 // |
---|
89 | // mode: c++ // |
---|
90 | // compile-command: "make install" // |
---|
91 | // End: // |
---|