source: src/Tuples/Explode.h@ d56ca354

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 d56ca354 was 62194cb, checked in by Aaron Moss <a3moss@…>, 8 years ago

Reduce duplication of cost/env in ExplodedActual

  • Property mode set to 100644
File size: 5.2 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#include <utility> // for forward
20
21#include "ResolvExpr/Alternative.h" // for Alternative, AltList
22#include "ResolvExpr/ExplodedActual.h" // for ExplodedActual
23#include "SynTree/Expression.h" // for Expression, UniqueExpr, AddressExpr
24#include "SynTree/Type.h" // for TupleType, Type
25#include "Tuples.h" // for maybeImpure
26
27namespace SymTab {
28class Indexer;
29} // namespace SymTab
30
31namespace Tuples {
32 Expression * distributeReference( Expression * );
33
34 static inline CastExpr * isReferenceCast( Expression * expr ) {
35 if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) {
36 if ( dynamic_cast< ReferenceType * >( castExpr->result ) ) {
37 return castExpr;
38 }
39 }
40 return nullptr;
41 }
42
43 /// Append alternative to an OutputIterator of Alternatives
44 template<typename OutputIterator>
45 void append( OutputIterator out, Expression* expr, const ResolvExpr::TypeEnvironment& env,
46 const ResolvExpr::Cost& cost, const ResolvExpr::Cost& cvtCost ) {
47 *out++ = ResolvExpr::Alternative{ expr, env, cost, cvtCost };
48 }
49
50 /// Append alternative to an ExplodedActual
51 static inline void append( ResolvExpr::ExplodedActual& ea, Expression* expr,
52 const ResolvExpr::TypeEnvironment&, const ResolvExpr::Cost&, const ResolvExpr::Cost& ) {
53 ea.exprs.emplace_back( expr );
54 /// xxx -- merge environment, cost?
55 }
56
57 /// helper function used by explode
58 template< typename Output >
59 void explodeUnique( Expression * expr, const ResolvExpr::Alternative & alt,
60 const SymTab::Indexer & indexer, Output&& out, bool isTupleAssign ) {
61 if ( isTupleAssign ) {
62 // tuple assignment needs CastExprs to be recursively exploded to easily get at all of the components
63 if ( CastExpr * castExpr = isReferenceCast( expr ) ) {
64 ResolvExpr::AltList alts;
65 explodeUnique(
66 castExpr->get_arg(), alt, indexer, back_inserter( alts ), isTupleAssign );
67 for ( ResolvExpr::Alternative & alt : alts ) {
68 // distribute reference cast over all components
69 append( std::forward<Output>(out), distributeReference( alt.release_expr() ),
70 alt.env, alt.cost, alt.cvtCost );
71 }
72 // 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)
73 return;
74 }
75 }
76 Type * res = expr->get_result()->stripReferences();
77 if ( TupleType * tupleType = dynamic_cast< TupleType * > ( res ) ) {
78 if ( TupleExpr * tupleExpr = dynamic_cast< TupleExpr * >( expr ) ) {
79 // can open tuple expr and dump its exploded components
80 for ( Expression * expr : tupleExpr->get_exprs() ) {
81 explodeUnique( expr, alt, indexer, std::forward<Output>(out), isTupleAssign );
82 }
83 } else {
84 // tuple type, but not tuple expr - recursively index into its components.
85 // if expr type is reference, convert to value type
86 Expression * arg = expr->clone();
87 if ( Tuples::maybeImpureIgnoreUnique( arg ) ) {
88 // expressions which may contain side effects require a single unique instance of the expression.
89 arg = new UniqueExpr( arg );
90 }
91 // cast reference to value type to facilitate further explosion
92 if ( dynamic_cast<ReferenceType *>( arg->get_result() ) ) {
93 arg = new CastExpr( arg, tupleType->clone() );
94 }
95 for ( unsigned int i = 0; i < tupleType->size(); i++ ) {
96 TupleIndexExpr * idx = new TupleIndexExpr( arg->clone(), i );
97 explodeUnique( idx, alt, indexer, std::forward<Output>(out), isTupleAssign );
98 delete idx;
99 }
100 delete arg;
101 }
102 } else {
103 // atomic (non-tuple) type - output a clone of the expression in a new alternative
104 append( std::forward<Output>(out), expr->clone(), alt.env, alt.cost, alt.cvtCost );
105 }
106 }
107
108 /// expands a tuple-valued alternative into multiple alternatives, each with a non-tuple-type
109 template< typename Output >
110 void explode( const ResolvExpr::Alternative &alt, const SymTab::Indexer & indexer,
111 Output&& out, bool isTupleAssign = false ) {
112 explodeUnique( alt.expr, alt, indexer, std::forward<Output>(out), isTupleAssign );
113 }
114
115 // explode list of alternatives
116 template< typename AltIterator, typename Output >
117 void explode( AltIterator altBegin, AltIterator altEnd, const SymTab::Indexer & indexer,
118 Output&& out, bool isTupleAssign = false ) {
119 for ( ; altBegin != altEnd; ++altBegin ) {
120 explode( *altBegin, indexer, std::forward<Output>(out), isTupleAssign );
121 }
122 }
123
124 template< typename Output >
125 void explode( const ResolvExpr::AltList & alts, const SymTab::Indexer & indexer, Output&& out,
126 bool isTupleAssign = false ) {
127 explode( alts.begin(), alts.end(), indexer, std::forward<Output>(out), isTupleAssign );
128 }
129} // namespace Tuples
130
131// Local Variables: //
132// tab-width: 4 //
133// mode: c++ //
134// compile-command: "make install" //
135// End: //
Note: See TracBrowser for help on using the repository browser.