source: src/Tuples/Explode.h@ 10dc6908

ADT arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 10dc6908 was 6bbce58, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Fix bug where explode removes non-reference casts in tuple assignment

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