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 | // TupleExpansionNew.cpp -- |
---|
8 | // |
---|
9 | // Author : Henry Xue |
---|
10 | // Created On : Mon Aug 23 15:36:09 2021 |
---|
11 | // Last Modified By : Henry Xue |
---|
12 | // Last Modified On : Mon Aug 23 15:36:09 2021 |
---|
13 | // Update Count : 1 |
---|
14 | // |
---|
15 | |
---|
16 | #include "Tuples.h" |
---|
17 | |
---|
18 | namespace Tuples { |
---|
19 | namespace { |
---|
20 | struct MemberTupleExpander final : public ast::WithShortCircuiting, public ast::WithVisitorRef< MemberTupleExpander > { |
---|
21 | void previsit( const ast::UntypedMemberExpr * ) { visit_children = false; } |
---|
22 | const ast::Expr * postvisit( const ast::UntypedMemberExpr * memberExpr ); |
---|
23 | }; |
---|
24 | } // namespace |
---|
25 | |
---|
26 | void expandMemberTuples( ast::TranslationUnit & translationUnit ) { |
---|
27 | ast::Pass< MemberTupleExpander >::run( translationUnit ); |
---|
28 | } |
---|
29 | |
---|
30 | namespace { |
---|
31 | namespace { |
---|
32 | /// given a expression representing the member and an expression representing the aggregate, |
---|
33 | /// reconstructs a flattened UntypedMemberExpr with the right precedence |
---|
34 | const ast::Expr * reconstructMemberExpr( const ast::Expr * member, const ast::Expr * aggr, const CodeLocation & loc ) { |
---|
35 | if ( auto memberExpr = dynamic_cast< const ast::UntypedMemberExpr * >( member ) ) { |
---|
36 | // construct a new UntypedMemberExpr with the correct structure , and recursively |
---|
37 | // expand that member expression. |
---|
38 | ast::Pass< MemberTupleExpander > expander; |
---|
39 | auto inner = new ast::UntypedMemberExpr( loc, memberExpr->aggregate, aggr ); |
---|
40 | auto newMemberExpr = new ast::UntypedMemberExpr( loc, memberExpr->member, inner ); |
---|
41 | //delete memberExpr; |
---|
42 | return newMemberExpr->accept( expander ); |
---|
43 | } else { |
---|
44 | // not a member expression, so there is nothing to do but attach and return |
---|
45 | return new ast::UntypedMemberExpr( loc, member, aggr ); |
---|
46 | } |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | const ast::Expr * MemberTupleExpander::postvisit( const ast::UntypedMemberExpr * memberExpr ) { |
---|
51 | const CodeLocation loc = memberExpr->location; |
---|
52 | if ( auto tupleExpr = memberExpr->member.as< ast::UntypedTupleExpr >() ) { |
---|
53 | auto mutExpr = mutate( tupleExpr ); |
---|
54 | ast::ptr< ast::Expr > aggr = memberExpr->aggregate->accept( *visitor ); |
---|
55 | // aggregate expressions which might be impure must be wrapped in unique expressions |
---|
56 | if ( Tuples::maybeImpureIgnoreUnique( memberExpr->aggregate ) ) aggr = new ast::UniqueExpr( loc, aggr ); |
---|
57 | for ( auto & expr : mutExpr->exprs ) { |
---|
58 | expr = reconstructMemberExpr( expr, aggr, loc ); |
---|
59 | } |
---|
60 | //delete aggr; |
---|
61 | return mutExpr; |
---|
62 | } else { |
---|
63 | // there may be a tuple expr buried in the aggregate |
---|
64 | return new ast::UntypedMemberExpr( loc, memberExpr->member, memberExpr->aggregate->accept( *visitor ) ); |
---|
65 | } |
---|
66 | } |
---|
67 | } // namespace |
---|
68 | } // namespace Tuples |
---|