[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
|
---|
[b910d15] | 11 | // Last Modified By : Andrew Beach
|
---|
| 12 | // Last Modified On : Mon Jun 17 14:36:00 2019
|
---|
| 13 | // Update Count : 4
|
---|
[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] | 30 | namespace ast {
|
---|
| 31 | class SymbolTable;
|
---|
| 32 | }
|
---|
| 33 |
|
---|
[03321e4] | 34 | namespace SymTab {
|
---|
| 35 | class Indexer;
|
---|
| 36 | } // namespace SymTab
|
---|
[141b786] | 37 |
|
---|
| 38 | namespace 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,
|
---|
[7870799] | 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,
|
---|
[7870799] | 60 | const ResolvExpr::TypeEnvironment&, const ResolvExpr::OpenVarSet&,
|
---|
[6d6e829] | 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
|
---|
[7870799] | 113 | append( std::forward<Output>(out), expr->clone(), alt.env, alt.openVars, alt.need,
|
---|
[6d6e829] | 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 |
|
---|
[b910d15] | 140 | const ast::Expr * distributeReference( const ast::Expr * );
|
---|
| 141 |
|
---|
| 142 | /// Append candidate to an OutputIterator of Candidates.
|
---|
| 143 | template<typename OutputIterator>
|
---|
| 144 | void append( OutputIterator out, const ast::Expr * expr, const ast::TypeEnvironment & env,
|
---|
| 145 | const ast::OpenVarSet & open, const ast::AssertionList & need,
|
---|
| 146 | const ResolvExpr::Cost & cost, const ResolvExpr::Cost & cvtCost ) {
|
---|
| 147 | ast::TypeEnvironment copyEnv = env;
|
---|
| 148 | ast::OpenVarSet copyOpen = open;
|
---|
| 149 | ast::AssertionSet set;
|
---|
| 150 | mergeAssertionSet( set, need );
|
---|
| 151 | *out++ = std::make_shared<ResolvExpr::Candidate>( expr, std::move( copyEnv ),
|
---|
| 152 | std::move( copyOpen ), std::move( set ), cost, cvtCost );
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | /// Append candidate to an ExplodedArg.
|
---|
| 156 | static inline void append( ResolvExpr::ExplodedArg& ea, const ast::Expr * expr,
|
---|
| 157 | const ast::TypeEnvironment&, const ast::OpenVarSet&,
|
---|
| 158 | const ast::AssertionList&, const ResolvExpr::Cost&, const ResolvExpr::Cost& ) {
|
---|
| 159 | // I'm not sure why most of the arguments are unused. But they were in the old version.
|
---|
| 160 | ea.exprs.emplace_back( expr );
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | /// Check if the expression is a cast to a reference type, return it if it is.
|
---|
| 164 | static inline const ast::CastExpr * isReferenceCast( const ast::Expr * expr ) {
|
---|
| 165 | if ( const ast::CastExpr * cast = dynamic_cast< const ast::CastExpr * >( expr ) ) {
|
---|
| 166 | if ( dynamic_cast< const ast::ReferenceType * >( cast->result.get() ) ) {
|
---|
| 167 | return cast;
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
| 170 | return nullptr;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | /// helper function (indirectely) used by explode
|
---|
| 174 | template< typename Output >
|
---|
| 175 | void explodeRecursive(
|
---|
[7870799] | 176 | const ast::CastExpr *, const ResolvExpr::Candidate &,
|
---|
| 177 | const ast::SymbolTable &, Output &&
|
---|
[b910d15] | 178 | ) {
|
---|
| 179 | }
|
---|
| 180 |
|
---|
[432ce7a] | 181 | /// helper function used by explode
|
---|
| 182 | template< typename Output >
|
---|
[b910d15] | 183 | void explodeUnique(
|
---|
| 184 | const ast::ptr< ast::Expr > & expr, const ResolvExpr::Candidate & arg,
|
---|
| 185 | const ast::SymbolTable & symtab, Output && out, bool isTupleAssign
|
---|
[432ce7a] | 186 | ) {
|
---|
[b910d15] | 187 | // Tuple assignment can use a faster method if it is cast. Uses recursive exploding.
|
---|
| 188 | if ( isTupleAssign ) if ( const ast::CastExpr * castExpr = isReferenceCast( expr ) ) {
|
---|
| 189 | ResolvExpr::CandidateList candidates;
|
---|
| 190 | explodeUnique( castExpr->arg, arg, symtab, back_inserter( candidates ), true );
|
---|
| 191 | for ( ResolvExpr::CandidateRef & cand : candidates ) {
|
---|
| 192 | // Distribute the reference cast over all components of the candidate.
|
---|
| 193 | append( std::forward<Output>(out), distributeReference( cand->expr ), cand->env,
|
---|
| 194 | cand->open, cand->need, cand->cost, cand->cvtCost );
|
---|
| 195 | }
|
---|
| 196 | return;
|
---|
| 197 | }
|
---|
| 198 | const ast::Type * res = expr->result->stripReferences();
|
---|
| 199 | if ( const ast::TupleType * tupleType = dynamic_cast< const ast::TupleType * >( res ) ) {
|
---|
| 200 | if ( const ast::ptr< ast::TupleExpr > & tupleExpr = expr.as< ast::TupleExpr >() ) {
|
---|
| 201 | // Open the tuple expr and continue on its components.
|
---|
| 202 | for ( const ast::Expr * expr : tupleExpr->exprs ) {
|
---|
| 203 | explodeUnique( expr, arg, symtab, std::forward<Output>(out), isTupleAssign );
|
---|
| 204 | }
|
---|
| 205 | } else {
|
---|
| 206 | ast::ptr< ast::Expr > local = expr;
|
---|
| 207 | // Expressions which may have side effects require a single unique instance.
|
---|
| 208 | if ( Tuples::maybeImpureIgnoreUnique( local ) ) {
|
---|
| 209 | local = new ast::UniqueExpr( local->location, local );
|
---|
| 210 | }
|
---|
| 211 | // Cast a reference away to a value-type to allow further explosion.
|
---|
| 212 | if ( dynamic_cast< const ast::ReferenceType *>( local->result.get() ) ) {
|
---|
[b8524ca] | 213 | local = new ast::CastExpr{ local, tupleType };
|
---|
[b910d15] | 214 | }
|
---|
| 215 | // Now we have to go across the tuple via indexing.
|
---|
| 216 | for ( unsigned int i = 0 ; i < tupleType->size() ; ++i ) {
|
---|
| 217 | ast::TupleIndexExpr * idx = new ast::TupleIndexExpr( local->location, local, i );
|
---|
| 218 | explodeUnique( idx, arg, symtab, std::forward<Output>(out), isTupleAssign );
|
---|
| 219 | // TODO: We need more input to figure out the exact lifetimes of these types.
|
---|
| 220 | // delete idx;
|
---|
| 221 | }
|
---|
| 222 | // delete local;
|
---|
| 223 | }
|
---|
| 224 | } else {
|
---|
| 225 | // For atomic/non-tuple types, no explosion is used.
|
---|
| 226 | append( std::forward<Output>(out), expr, arg.env, arg.open, arg.need, arg.cost,
|
---|
| 227 | arg.cvtCost );
|
---|
| 228 | }
|
---|
[432ce7a] | 229 | }
|
---|
| 230 |
|
---|
| 231 | /// expands a tuple-valued candidate into multiple candidates, each with a non-tuple type
|
---|
| 232 | template< typename Output >
|
---|
[b910d15] | 233 | void explode(
|
---|
| 234 | const ResolvExpr::Candidate & arg, const ast::SymbolTable & symtab, Output && out,
|
---|
[432ce7a] | 235 | bool isTupleAssign = false
|
---|
| 236 | ) {
|
---|
| 237 | explodeUnique( arg.expr, arg, symtab, std::forward< Output >( out ), isTupleAssign );
|
---|
| 238 | }
|
---|
| 239 |
|
---|
[234b1cb] | 240 | /// explode list of candidates into flattened list of candidates
|
---|
| 241 | template< typename Output >
|
---|
[7870799] | 242 | void explode(
|
---|
| 243 | const ResolvExpr::CandidateList & cands, const ast::SymbolTable & symtab, Output && out,
|
---|
[234b1cb] | 244 | bool isTupleAssign = false
|
---|
| 245 | ) {
|
---|
| 246 | for ( const ResolvExpr::CandidateRef & cand : cands ) {
|
---|
| 247 | explode( *cand, symtab, std::forward< Output >( out ), isTupleAssign );
|
---|
| 248 | }
|
---|
| 249 | }
|
---|
| 250 |
|
---|
[141b786] | 251 | } // namespace Tuples
|
---|
| 252 |
|
---|
| 253 | // Local Variables: //
|
---|
| 254 | // tab-width: 4 //
|
---|
| 255 | // mode: c++ //
|
---|
| 256 | // compile-command: "make install" //
|
---|
| 257 | // End: //
|
---|