source: src/Tuples/Explode.h@ 682dcae

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 with_gc
Last change on this file since 682dcae was 490ff5c3, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Minor code cleanup

  • Property mode set to 100644
File size: 5.2 KB
RevLine 
[141b786]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
[6b0b624]11// Last Modified By : Peter A. Buhr
12// Last Modified On : Sat Jul 22 09:55:16 2017
13// Update Count : 3
[141b786]14//
15
[6b0b624]16#pragma once
[141b786]17
[62194cb]18#include <iterator> // for back_inserter, back_insert_iterator
19#include <utility> // for forward
[141b786]20
[62194cb]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
[141b786]26
[03321e4]27namespace SymTab {
28class Indexer;
29} // namespace SymTab
[141b786]30
31namespace Tuples {
[0b5d871]32 Expression * distributeReference( Expression * );
33
[6bbce58]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
[62194cb]43 /// Append alternative to an OutputIterator of Alternatives
44 template<typename OutputIterator>
[490ff5c3]45 void append( OutputIterator out, Expression* expr, const ResolvExpr::TypeEnvironment& env,
[62194cb]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
[490ff5c3]51 static inline void append( ResolvExpr::ExplodedActual& ea, Expression* expr,
[62194cb]52 const ResolvExpr::TypeEnvironment&, const ResolvExpr::Cost&, const ResolvExpr::Cost& ) {
53 ea.exprs.emplace_back( expr );
54 /// xxx -- merge environment, cost?
55 }
56
[141b786]57 /// helper function used by explode
[62194cb]58 template< typename Output >
[490ff5c3]59 void explodeUnique( Expression * expr, const ResolvExpr::Alternative & alt,
[62194cb]60 const SymTab::Indexer & indexer, Output&& out, bool isTupleAssign ) {
[0b5d871]61 if ( isTupleAssign ) {
62 // tuple assignment needs CastExprs to be recursively exploded to easily get at all of the components
[6bbce58]63 if ( CastExpr * castExpr = isReferenceCast( expr ) ) {
[0b5d871]64 ResolvExpr::AltList alts;
[490ff5c3]65 explodeUnique(
[62194cb]66 castExpr->get_arg(), alt, indexer, back_inserter( alts ), isTupleAssign );
[0b5d871]67 for ( ResolvExpr::Alternative & alt : alts ) {
68 // distribute reference cast over all components
[490ff5c3]69 append( std::forward<Output>(out), distributeReference( alt.release_expr() ),
[62194cb]70 alt.env, alt.cost, alt.cvtCost );
[0b5d871]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 }
[6d267ca]76 Type * res = expr->get_result()->stripReferences();
[f831177]77 if ( TupleType * tupleType = dynamic_cast< TupleType * > ( res ) ) {
[141b786]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() ) {
[62194cb]81 explodeUnique( expr, alt, indexer, std::forward<Output>(out), isTupleAssign );
[141b786]82 }
83 } else {
[6d267ca]84 // tuple type, but not tuple expr - recursively index into its components.
85 // if expr type is reference, convert to value type
[141b786]86 Expression * arg = expr->clone();
[0b5d871]87 if ( Tuples::maybeImpureIgnoreUnique( arg ) ) {
[141b786]88 // expressions which may contain side effects require a single unique instance of the expression.
89 arg = new UniqueExpr( arg );
90 }
[0b5d871]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 }
[141b786]95 for ( unsigned int i = 0; i < tupleType->size(); i++ ) {
96 TupleIndexExpr * idx = new TupleIndexExpr( arg->clone(), i );
[62194cb]97 explodeUnique( idx, alt, indexer, std::forward<Output>(out), isTupleAssign );
[141b786]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
[62194cb]104 append( std::forward<Output>(out), expr->clone(), alt.env, alt.cost, alt.cvtCost );
[141b786]105 }
106 }
107
108 /// expands a tuple-valued alternative into multiple alternatives, each with a non-tuple-type
[62194cb]109 template< typename Output >
[490ff5c3]110 void explode( const ResolvExpr::Alternative &alt, const SymTab::Indexer & indexer,
[62194cb]111 Output&& out, bool isTupleAssign = false ) {
112 explodeUnique( alt.expr, alt, indexer, std::forward<Output>(out), isTupleAssign );
[141b786]113 }
114
115 // explode list of alternatives
[62194cb]116 template< typename AltIterator, typename Output >
[490ff5c3]117 void explode( AltIterator altBegin, AltIterator altEnd, const SymTab::Indexer & indexer,
[62194cb]118 Output&& out, bool isTupleAssign = false ) {
[141b786]119 for ( ; altBegin != altEnd; ++altBegin ) {
[62194cb]120 explode( *altBegin, indexer, std::forward<Output>(out), isTupleAssign );
[141b786]121 }
122 }
123
[62194cb]124 template< typename Output >
[490ff5c3]125 void explode( const ResolvExpr::AltList & alts, const SymTab::Indexer & indexer, Output&& out,
[62194cb]126 bool isTupleAssign = false ) {
127 explode( alts.begin(), alts.end(), indexer, std::forward<Output>(out), isTupleAssign );
[141b786]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.