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 | // Alternative.h -- |
---|
8 | // |
---|
9 | // Author : Richard C. Bilson |
---|
10 | // Created On : Sat May 16 23:45:43 2015 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Sat May 16 23:54:39 2015 |
---|
13 | // Update Count : 2 |
---|
14 | // |
---|
15 | |
---|
16 | #ifndef ALTERNATIVE_H |
---|
17 | #define ALTERNATIVE_H |
---|
18 | |
---|
19 | #include <list> |
---|
20 | #include "SynTree/SynTree.h" |
---|
21 | #include "Cost.h" |
---|
22 | #include "TypeEnvironment.h" |
---|
23 | |
---|
24 | namespace ResolvExpr { |
---|
25 | struct Alternative; |
---|
26 | typedef std::list< Alternative > AltList; |
---|
27 | |
---|
28 | struct Alternative { |
---|
29 | Alternative(); |
---|
30 | Alternative( Expression *expr, const TypeEnvironment &env, const Cost &cost ); |
---|
31 | Alternative( Expression *expr, const TypeEnvironment &env, const Cost &cost, const Cost &cvtCost ); |
---|
32 | Alternative( const Alternative &other ); |
---|
33 | Alternative &operator=( const Alternative &other ); |
---|
34 | Alternative( Alternative && other ); |
---|
35 | Alternative &operator=( Alternative && other ); |
---|
36 | ~Alternative(); |
---|
37 | |
---|
38 | void initialize( const Alternative &src, Alternative &dest ); |
---|
39 | |
---|
40 | void print( std::ostream &os, int indent = 0 ) const; |
---|
41 | |
---|
42 | Cost cost; |
---|
43 | Cost cvtCost; |
---|
44 | Expression *expr; |
---|
45 | TypeEnvironment env; |
---|
46 | }; |
---|
47 | |
---|
48 | /// helper function used by explode |
---|
49 | template< typename OutputIterator > |
---|
50 | void explodeUnique( Expression * expr, const Alternative & alt, OutputIterator out ) { |
---|
51 | Type * res = expr->get_result(); |
---|
52 | if ( TupleType * tupleType = dynamic_cast< TupleType * > ( res ) ) { |
---|
53 | if ( TupleExpr * tupleExpr = dynamic_cast< TupleExpr * >( expr ) ) { |
---|
54 | // can open tuple expr and dump its exploded components |
---|
55 | for ( Expression * expr : tupleExpr->get_exprs() ) { |
---|
56 | explodeUnique( expr, alt, out ); |
---|
57 | } |
---|
58 | } else { |
---|
59 | // tuple type, but not tuple expr - need to refer to single instance of the argument |
---|
60 | // expression and index into its components |
---|
61 | UniqueExpr * unq = new UniqueExpr( expr->clone() ); |
---|
62 | for ( unsigned int i = 0; i < tupleType->size(); i++ ) { |
---|
63 | TupleIndexExpr * idx = new TupleIndexExpr( unq->clone(), i ); |
---|
64 | explodeUnique( idx, alt, out ); |
---|
65 | delete idx; |
---|
66 | } |
---|
67 | delete unq; |
---|
68 | } |
---|
69 | } else { |
---|
70 | // atomic (non-tuple) type - output a clone of the expression in a new alternative |
---|
71 | *out++ = Alternative( expr->clone(), alt.env, alt.cost, alt.cvtCost ); |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | /// expands a tuple-valued alternative into multiple alternatives, each with a non-tuple-type |
---|
76 | template< typename OutputIterator > |
---|
77 | void explode( const Alternative &alt, OutputIterator out ) { |
---|
78 | explodeUnique( alt.expr, alt, out ); |
---|
79 | } |
---|
80 | |
---|
81 | // explode list of alternatives |
---|
82 | template< typename OutputIterator > |
---|
83 | void explode( const AltList & alts, OutputIterator out ) { |
---|
84 | for ( const Alternative & alt : alts ) { |
---|
85 | explode( alt, out ); |
---|
86 | } |
---|
87 | } |
---|
88 | } // namespace ResolvExpr |
---|
89 | |
---|
90 | #endif // ALTERNATIVE_H |
---|
91 | |
---|
92 | // Local Variables: // |
---|
93 | // tab-width: 4 // |
---|
94 | // mode: c++ // |
---|
95 | // compile-command: "make install" // |
---|
96 | // End: // |
---|