source: src/Tuples/Explode.h@ d82daa1

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since d82daa1 was 03321e4, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Fixed headers for tuples folder

  • Property mode set to 100644
File size: 4.0 KB
Line 
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 : Peter A. Buhr
12// Last Modified On : Sat Jul 22 09:55:16 2017
13// Update Count : 3
14//
15
16#pragma once
17
18#include <iterator> // for back_inserter, back_insert_iterator
19
20#include "ResolvExpr/Alternative.h" // for Alternative, AltList
21#include "SynTree/Expression.h" // for Expression, UniqueExpr, AddressExpr
22#include "SynTree/Type.h" // for TupleType, Type
23#include "Tuples.h" // for maybeImpure
24
25namespace SymTab {
26class Indexer;
27} // namespace SymTab
28
29namespace Tuples {
30 /// helper function used by explode to properly distribute
31 /// '&' across a tuple expression
32 Expression * distributeAddr( Expression * expr );
33
34 /// helper function used by explode
35 template< typename OutputIterator >
36 void explodeUnique( Expression * expr, const ResolvExpr::Alternative & alt, const SymTab::Indexer & indexer, OutputIterator out, bool isTupleAssign ) {
37 if ( isTupleAssign ) {
38 // tuple assignment needs AddressExprs to be recursively exploded to easily get at all of the components
39 if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( expr ) ) {
40 ResolvExpr::AltList alts;
41 explodeUnique( addrExpr->get_arg(), alt, indexer, back_inserter( alts ), isTupleAssign );
42 for ( ResolvExpr::Alternative & alt : alts ) {
43 // distribute '&' over all components
44 alt.expr = distributeAddr( alt.expr );
45 *out++ = alt;
46 }
47 // in tuple assignment, still need to handle the other cases, but only if not already handled here (don't want to output too many alternatives)
48 return;
49 }
50 }
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, indexer, out, isTupleAssign );
57 }
58 } else {
59 // tuple type, but not tuple expr - recursively index into its components
60 Expression * arg = expr->clone();
61 if ( Tuples::maybeImpure( arg ) && ! dynamic_cast< UniqueExpr * >( arg ) ) {
62 // expressions which may contain side effects require a single unique instance of the expression.
63 arg = new UniqueExpr( arg );
64 }
65 for ( unsigned int i = 0; i < tupleType->size(); i++ ) {
66 TupleIndexExpr * idx = new TupleIndexExpr( arg->clone(), i );
67 explodeUnique( idx, alt, indexer, out, isTupleAssign );
68 delete idx;
69 }
70 delete arg;
71 }
72 } else {
73 // atomic (non-tuple) type - output a clone of the expression in a new alternative
74 *out++ = ResolvExpr::Alternative( expr->clone(), alt.env, alt.cost, alt.cvtCost );
75 }
76 }
77
78 /// expands a tuple-valued alternative into multiple alternatives, each with a non-tuple-type
79 template< typename OutputIterator >
80 void explode( const ResolvExpr::Alternative &alt, const SymTab::Indexer & indexer, OutputIterator out, bool isTupleAssign = false ) {
81 explodeUnique( alt.expr, alt, indexer, out, isTupleAssign );
82 }
83
84 // explode list of alternatives
85 template< typename AltIterator, typename OutputIterator >
86 void explode( AltIterator altBegin, AltIterator altEnd, const SymTab::Indexer & indexer, OutputIterator out, bool isTupleAssign = false ) {
87 for ( ; altBegin != altEnd; ++altBegin ) {
88 explode( *altBegin, indexer, out, isTupleAssign );
89 }
90 }
91
92 template< typename OutputIterator >
93 void explode( const ResolvExpr::AltList & alts, const SymTab::Indexer & indexer, OutputIterator out, bool isTupleAssign = false ) {
94 explode( alts.begin(), alts.end(), indexer, out, isTupleAssign );
95 }
96} // namespace Tuples
97
98// Local Variables: //
99// tab-width: 4 //
100// mode: c++ //
101// compile-command: "make install" //
102// End: //
Note: See TracBrowser for help on using the repository browser.