source: src/Tuples/Explode.h@ 641c3d0

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 641c3d0 was 6b0b624, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

change #ifndef to #pragma once

  • Property mode set to 100644
File size: 3.8 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 "ResolvExpr/AlternativeFinder.h"
19#include "ResolvExpr/Resolver.h"
20
21#include "SynTree/Expression.h"
22#include "SynTree/Declaration.h"
23#include "SynTree/Type.h"
24
25#include "Tuples.h"
26
27namespace Tuples {
28 /// helper function used by explode to properly distribute
29 /// '&' across a tuple expression
30 Expression * distributeAddr( Expression * expr );
31
32 /// helper function used by explode
33 template< typename OutputIterator >
34 void explodeUnique( Expression * expr, const ResolvExpr::Alternative & alt, const SymTab::Indexer & indexer, OutputIterator out, bool isTupleAssign ) {
35 if ( isTupleAssign ) {
36 // tuple assignment needs AddressExprs to be recursively exploded to easily get at all of the components
37 if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( expr ) ) {
38 ResolvExpr::AltList alts;
39 explodeUnique( addrExpr->get_arg(), alt, indexer, back_inserter( alts ), isTupleAssign );
40 for ( ResolvExpr::Alternative & alt : alts ) {
41 // distribute '&' over all components
42 alt.expr = distributeAddr( alt.expr );
43 *out++ = alt;
44 }
45 // 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)
46 return;
47 }
48 }
49 Type * res = expr->get_result();
50 if ( TupleType * tupleType = dynamic_cast< TupleType * > ( res ) ) {
51 if ( TupleExpr * tupleExpr = dynamic_cast< TupleExpr * >( expr ) ) {
52 // can open tuple expr and dump its exploded components
53 for ( Expression * expr : tupleExpr->get_exprs() ) {
54 explodeUnique( expr, alt, indexer, out, isTupleAssign );
55 }
56 } else {
57 // tuple type, but not tuple expr - recursively index into its components
58 Expression * arg = expr->clone();
59 if ( Tuples::maybeImpure( arg ) && ! dynamic_cast< UniqueExpr * >( arg ) ) {
60 // expressions which may contain side effects require a single unique instance of the expression.
61 arg = new UniqueExpr( arg );
62 }
63 for ( unsigned int i = 0; i < tupleType->size(); i++ ) {
64 TupleIndexExpr * idx = new TupleIndexExpr( arg->clone(), i );
65 explodeUnique( idx, alt, indexer, out, isTupleAssign );
66 delete idx;
67 }
68 delete arg;
69 }
70 } else {
71 // atomic (non-tuple) type - output a clone of the expression in a new alternative
72 *out++ = ResolvExpr::Alternative( expr->clone(), alt.env, alt.cost, alt.cvtCost );
73 }
74 }
75
76 /// expands a tuple-valued alternative into multiple alternatives, each with a non-tuple-type
77 template< typename OutputIterator >
78 void explode( const ResolvExpr::Alternative &alt, const SymTab::Indexer & indexer, OutputIterator out, bool isTupleAssign = false ) {
79 explodeUnique( alt.expr, alt, indexer, out, isTupleAssign );
80 }
81
82 // explode list of alternatives
83 template< typename AltIterator, typename OutputIterator >
84 void explode( AltIterator altBegin, AltIterator altEnd, const SymTab::Indexer & indexer, OutputIterator out, bool isTupleAssign = false ) {
85 for ( ; altBegin != altEnd; ++altBegin ) {
86 explode( *altBegin, indexer, out, isTupleAssign );
87 }
88 }
89
90 template< typename OutputIterator >
91 void explode( const ResolvExpr::AltList & alts, const SymTab::Indexer & indexer, OutputIterator out, bool isTupleAssign = false ) {
92 explode( alts.begin(), alts.end(), indexer, out, isTupleAssign );
93 }
94} // namespace Tuples
95
96// Local Variables: //
97// tab-width: 4 //
98// mode: c++ //
99// compile-command: "make install" //
100// End: //
Note: See TracBrowser for help on using the repository browser.