source: src/Tuples/Explode.h@ 9d5089e

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 9d5089e was 432ce7a, checked in by Aaron Moss <a3moss@…>, 7 years ago

Port CandidateFinder::postvisit for UntypedExpr, stub dependencies

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