| [51587aa] | 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 | //
 | 
|---|
| [5af62f1] | 7 | // TupleAssignment.cc --
 | 
|---|
| [51587aa] | 8 | //
 | 
|---|
| [843054c2] | 9 | // Author           : Rodolfo G. Esteves
 | 
|---|
| [51587aa] | 10 | // Created On       : Mon May 18 07:44:20 2015
 | 
|---|
| [c0aa336] | 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
| [07de76b] | 12 | // Last Modified On : Fri Dec 13 23:45:33 2019
 | 
|---|
 | 13 | // Update Count     : 9
 | 
|---|
| [51587aa] | 14 | //
 | 
|---|
 | 15 | 
 | 
|---|
| [03321e4] | 16 | #include <algorithm>                       // for transform
 | 
|---|
 | 17 | #include <cassert>                         // for assert
 | 
|---|
 | 18 | #include <iterator>                        // for back_insert_iterator, back...
 | 
|---|
 | 19 | #include <list>                            // for _List_const_iterator, _Lis...
 | 
|---|
 | 20 | #include <memory>                          // for unique_ptr, allocator_trai...
 | 
|---|
 | 21 | #include <string>                          // for string
 | 
|---|
| [78315272] | 22 | #include <vector>
 | 
|---|
| [51b73452] | 23 | 
 | 
|---|
| [234b1cb] | 24 | #include "AST/Decl.hpp"
 | 
|---|
 | 25 | #include "AST/Init.hpp"
 | 
|---|
 | 26 | #include "AST/Pass.hpp"
 | 
|---|
 | 27 | #include "AST/Stmt.hpp"
 | 
|---|
 | 28 | #include "AST/TypeEnvironment.hpp"
 | 
|---|
| [8135d4c] | 29 | #include "CodeGen/OperatorTable.h"
 | 
|---|
| [2449aef] | 30 | #include "Common/PassVisitor.h"
 | 
|---|
| [03321e4] | 31 | #include "Common/UniqueName.h"             // for UniqueName
 | 
|---|
| [234b1cb] | 32 | #include "Common/utility.h"                // for splice, zipWith
 | 
|---|
| [03321e4] | 33 | #include "Explode.h"                       // for explode
 | 
|---|
 | 34 | #include "InitTweak/GenInit.h"             // for genCtorInit
 | 
|---|
 | 35 | #include "InitTweak/InitTweak.h"           // for getPointerBase, isAssignment
 | 
|---|
 | 36 | #include "ResolvExpr/Alternative.h"        // for AltList, Alternative
 | 
|---|
 | 37 | #include "ResolvExpr/AlternativeFinder.h"  // for AlternativeFinder, simpleC...
 | 
|---|
 | 38 | #include "ResolvExpr/Cost.h"               // for Cost
 | 
|---|
 | 39 | #include "ResolvExpr/Resolver.h"           // for resolveCtorInit
 | 
|---|
 | 40 | #include "ResolvExpr/TypeEnvironment.h"    // for TypeEnvironment
 | 
|---|
| [c43c171] | 41 | #include "ResolvExpr/typeops.h"            // for combos
 | 
|---|
| [07de76b] | 42 | #include "SynTree/LinkageSpec.h"           // for Cforall
 | 
|---|
| [03321e4] | 43 | #include "SynTree/Declaration.h"           // for ObjectDecl
 | 
|---|
 | 44 | #include "SynTree/Expression.h"            // for Expression, CastExpr, Name...
 | 
|---|
 | 45 | #include "SynTree/Initializer.h"           // for ConstructorInit, SingleInit
 | 
|---|
 | 46 | #include "SynTree/Statement.h"             // for ExprStmt
 | 
|---|
 | 47 | #include "SynTree/Type.h"                  // for Type, Type::Qualifiers
 | 
|---|
 | 48 | #include "SynTree/TypeSubstitution.h"      // for TypeSubstitution
 | 
|---|
 | 49 | #include "SynTree/Visitor.h"               // for Visitor
 | 
|---|
| [51b73452] | 50 | 
 | 
|---|
| [4b6ef70] | 51 | #if 0
 | 
|---|
 | 52 | #define PRINT(x) x
 | 
|---|
 | 53 | #else
 | 
|---|
 | 54 | #define PRINT(x)
 | 
|---|
 | 55 | #endif
 | 
|---|
 | 56 | 
 | 
|---|
| [51b73452] | 57 | namespace Tuples {
 | 
|---|
| [234b1cb] | 58 |         class TupleAssignSpotter_old {
 | 
|---|
| [5af62f1] | 59 |           public:
 | 
|---|
 | 60 |                 // dispatcher for Tuple (multiple and mass) assignment operations
 | 
|---|
| [234b1cb] | 61 |                 TupleAssignSpotter_old( ResolvExpr::AlternativeFinder & );
 | 
|---|
| [c43c171] | 62 |                 void spot( UntypedExpr * expr, std::vector<ResolvExpr::AlternativeFinder> &args );
 | 
|---|
| [5af62f1] | 63 | 
 | 
|---|
 | 64 |           private:
 | 
|---|
 | 65 |                 void match();
 | 
|---|
 | 66 | 
 | 
|---|
| [6eb8948] | 67 |                 struct Matcher {
 | 
|---|
| [5af62f1] | 68 |                   public:
 | 
|---|
| [7870799] | 69 |                         Matcher( TupleAssignSpotter_old &spotter, const ResolvExpr::AltList& lhs,
 | 
|---|
| [6d6e829] | 70 |                                 const ResolvExpr::AltList& rhs );
 | 
|---|
| [5af62f1] | 71 |                         virtual ~Matcher() {}
 | 
|---|
| [7870799] | 72 | 
 | 
|---|
| [5af62f1] | 73 |                         virtual void match( std::list< Expression * > &out ) = 0;
 | 
|---|
| [1d2b64f] | 74 |                         ObjectDecl * newObject( UniqueName & namer, Expression * expr );
 | 
|---|
| [6d6e829] | 75 | 
 | 
|---|
 | 76 |                         void combineState( const ResolvExpr::Alternative& alt ) {
 | 
|---|
 | 77 |                                 compositeEnv.simpleCombine( alt.env );
 | 
|---|
 | 78 |                                 ResolvExpr::mergeOpenVars( openVars, alt.openVars );
 | 
|---|
| [2c187378] | 79 |                                 cloneAll( alt.need, need );
 | 
|---|
| [6d6e829] | 80 |                         }
 | 
|---|
 | 81 | 
 | 
|---|
 | 82 |                         void combineState( const ResolvExpr::AltList& alts ) {
 | 
|---|
 | 83 |                                 for ( const ResolvExpr::Alternative& alt : alts ) { combineState( alt ); }
 | 
|---|
 | 84 |                         }
 | 
|---|
| [7870799] | 85 | 
 | 
|---|
| [3c13c03] | 86 |                         ResolvExpr::AltList lhs, rhs;
 | 
|---|
| [234b1cb] | 87 |                         TupleAssignSpotter_old &spotter;
 | 
|---|
| [1d2b64f] | 88 |                         ResolvExpr::Cost baseCost;
 | 
|---|
| [6eb8948] | 89 |                         std::list< ObjectDecl * > tmpDecls;
 | 
|---|
| [1d2b64f] | 90 |                         ResolvExpr::TypeEnvironment compositeEnv;
 | 
|---|
| [6d6e829] | 91 |                         ResolvExpr::OpenVarSet openVars;
 | 
|---|
 | 92 |                         ResolvExpr::AssertionSet need;
 | 
|---|
| [5af62f1] | 93 |                 };
 | 
|---|
 | 94 | 
 | 
|---|
| [6eb8948] | 95 |                 struct MassAssignMatcher : public Matcher {
 | 
|---|
| [5af62f1] | 96 |                   public:
 | 
|---|
| [234b1cb] | 97 |                         MassAssignMatcher( TupleAssignSpotter_old &spotter, const ResolvExpr::AltList& lhs,
 | 
|---|
| [78315272] | 98 |                                 const ResolvExpr::AltList& rhs ) : Matcher(spotter, lhs, rhs) {}
 | 
|---|
| [5af62f1] | 99 |                         virtual void match( std::list< Expression * > &out );
 | 
|---|
 | 100 |                 };
 | 
|---|
 | 101 | 
 | 
|---|
| [6eb8948] | 102 |                 struct MultipleAssignMatcher : public Matcher {
 | 
|---|
| [5af62f1] | 103 |                   public:
 | 
|---|
| [234b1cb] | 104 |                         MultipleAssignMatcher( TupleAssignSpotter_old &spotter, const ResolvExpr::AltList& lhs,
 | 
|---|
| [78315272] | 105 |                                 const ResolvExpr::AltList& rhs ) : Matcher(spotter, lhs, rhs) {}
 | 
|---|
| [5af62f1] | 106 |                         virtual void match( std::list< Expression * > &out );
 | 
|---|
 | 107 |                 };
 | 
|---|
 | 108 | 
 | 
|---|
 | 109 |                 ResolvExpr::AlternativeFinder ¤tFinder;
 | 
|---|
| [65660bd] | 110 |                 std::string fname;
 | 
|---|
 | 111 |                 std::unique_ptr< Matcher > matcher;
 | 
|---|
| [5af62f1] | 112 |         };
 | 
|---|
 | 113 | 
 | 
|---|
| [f831177] | 114 |         /// true if expr is an expression of tuple type
 | 
|---|
| [5af62f1] | 115 |         bool isTuple( Expression *expr ) {
 | 
|---|
| [51587aa] | 116 |                 if ( ! expr ) return false;
 | 
|---|
| [d29fa5f] | 117 |                 assert( expr->result );
 | 
|---|
| [e6cee92] | 118 |                 return dynamic_cast< TupleType * >( expr->get_result()->stripReferences() );
 | 
|---|
| [51587aa] | 119 |         }
 | 
|---|
 | 120 | 
 | 
|---|
| [65660bd] | 121 |         template< typename AltIter >
 | 
|---|
 | 122 |         bool isMultAssign( AltIter begin, AltIter end ) {
 | 
|---|
 | 123 |                 // multiple assignment if more than one alternative in the range or if
 | 
|---|
 | 124 |                 // the alternative is a tuple
 | 
|---|
 | 125 |                 if ( begin == end ) return false;
 | 
|---|
 | 126 |                 if ( isTuple( begin->expr ) ) return true;
 | 
|---|
 | 127 |                 return ++begin != end;
 | 
|---|
 | 128 |         }
 | 
|---|
 | 129 | 
 | 
|---|
| [e6cee92] | 130 |         bool refToTuple( Expression *expr ) {
 | 
|---|
 | 131 |                 assert( expr->get_result() );
 | 
|---|
| [5af62f1] | 132 |                 // also check for function returning tuple of reference types
 | 
|---|
| [65660bd] | 133 |                 if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) {
 | 
|---|
| [e6cee92] | 134 |                         return refToTuple( castExpr->get_arg() );
 | 
|---|
 | 135 |                 } else {
 | 
|---|
 | 136 |                         return isTuple( expr );
 | 
|---|
| [51587aa] | 137 |                 }
 | 
|---|
 | 138 |                 return false;
 | 
|---|
 | 139 |         }
 | 
|---|
 | 140 | 
 | 
|---|
| [82f3226] | 141 |         void handleTupleAssignment( ResolvExpr::AlternativeFinder & currentFinder, UntypedExpr * expr,
 | 
|---|
| [78315272] | 142 |                                 std::vector<ResolvExpr::AlternativeFinder> &args ) {
 | 
|---|
| [234b1cb] | 143 |                 TupleAssignSpotter_old spotter( currentFinder );
 | 
|---|
| [78315272] | 144 |                 spotter.spot( expr, args );
 | 
|---|
| [5af62f1] | 145 |         }
 | 
|---|
| [51587aa] | 146 | 
 | 
|---|
| [234b1cb] | 147 |         TupleAssignSpotter_old::TupleAssignSpotter_old( ResolvExpr::AlternativeFinder &f )
 | 
|---|
| [5af62f1] | 148 |                 : currentFinder(f) {}
 | 
|---|
| [51587aa] | 149 | 
 | 
|---|
| [234b1cb] | 150 |         void TupleAssignSpotter_old::spot( UntypedExpr * expr,
 | 
|---|
| [78315272] | 151 |                         std::vector<ResolvExpr::AlternativeFinder> &args ) {
 | 
|---|
| [65660bd] | 152 |                 if (  NameExpr *op = dynamic_cast< NameExpr * >(expr->get_function()) ) {
 | 
|---|
| [bff227f] | 153 |                         if ( CodeGen::isCtorDtorAssign( op->get_name() ) ) {
 | 
|---|
| [78315272] | 154 |                                 fname = op->get_name();
 | 
|---|
 | 155 | 
 | 
|---|
 | 156 |                                 // AlternativeFinder will naturally handle this case case, if it's legal
 | 
|---|
 | 157 |                                 if ( args.size() == 0 ) return;
 | 
|---|
 | 158 | 
 | 
|---|
| [82f3226] | 159 |                                 // if an assignment only takes 1 argument, that's odd, but maybe someone wrote
 | 
|---|
| [78315272] | 160 |                                 // the function, in which case AlternativeFinder will handle it normally
 | 
|---|
 | 161 |                                 if ( args.size() == 1 && CodeGen::isAssignment( fname ) ) return;
 | 
|---|
 | 162 | 
 | 
|---|
 | 163 |                                 // look over all possible left-hand-sides
 | 
|---|
 | 164 |                                 for ( ResolvExpr::Alternative& lhsAlt : args[0] ) {
 | 
|---|
 | 165 |                                         // skip non-tuple LHS
 | 
|---|
 | 166 |                                         if ( ! refToTuple(lhsAlt.expr) ) continue;
 | 
|---|
 | 167 | 
 | 
|---|
| [82f3226] | 168 |                                         // explode is aware of casts - ensure every LHS expression is sent into explode
 | 
|---|
| [78315272] | 169 |                                         // with a reference cast
 | 
|---|
| [82f3226] | 170 |                                         // xxx - this seems to change the alternatives before the normal
 | 
|---|
| [78315272] | 171 |                                         //  AlternativeFinder flow; maybe this is desired?
 | 
|---|
 | 172 |                                         if ( ! dynamic_cast<CastExpr*>( lhsAlt.expr ) ) {
 | 
|---|
| [82f3226] | 173 |                                                 lhsAlt.expr = new CastExpr( lhsAlt.expr,
 | 
|---|
 | 174 |                                                                 new ReferenceType( Type::Qualifiers(),
 | 
|---|
| [4f68f6d] | 175 |                                                                         lhsAlt.expr->result->clone() ) );
 | 
|---|
| [c43c171] | 176 |                                         }
 | 
|---|
 | 177 | 
 | 
|---|
| [78315272] | 178 |                                         // explode the LHS so that each field of a tuple-valued-expr is assigned
 | 
|---|
 | 179 |                                         ResolvExpr::AltList lhs;
 | 
|---|
 | 180 |                                         explode( lhsAlt, currentFinder.get_indexer(), back_inserter(lhs), true );
 | 
|---|
 | 181 |                                         for ( ResolvExpr::Alternative& alt : lhs ) {
 | 
|---|
| [82f3226] | 182 |                                                 // each LHS value must be a reference - some come in with a cast expression,
 | 
|---|
| [78315272] | 183 |                                                 // if not just cast to reference here
 | 
|---|
 | 184 |                                                 if ( ! dynamic_cast<ReferenceType*>( alt.expr->get_result() ) ) {
 | 
|---|
| [82f3226] | 185 |                                                         alt.expr = new CastExpr( alt.expr,
 | 
|---|
 | 186 |                                                                 new ReferenceType( Type::Qualifiers(),
 | 
|---|
| [78315272] | 187 |                                                                         alt.expr->get_result()->clone() ) );
 | 
|---|
| [51587aa] | 188 |                                                 }
 | 
|---|
| [78315272] | 189 |                                         }
 | 
|---|
 | 190 | 
 | 
|---|
 | 191 |                                         if ( args.size() == 1 ) {
 | 
|---|
 | 192 |                                                 // mass default-initialization/destruction
 | 
|---|
 | 193 |                                                 ResolvExpr::AltList rhs{};
 | 
|---|
 | 194 |                                                 matcher.reset( new MassAssignMatcher( *this, lhs, rhs ) );
 | 
|---|
| [4b6ef70] | 195 |                                                 match();
 | 
|---|
| [78315272] | 196 |                                         } else if ( args.size() > 2 ) {
 | 
|---|
 | 197 |                                                 // expand all possible RHS possibilities
 | 
|---|
 | 198 |                                                 // TODO build iterative version of this instead of using combos
 | 
|---|
 | 199 |                                                 std::vector< ResolvExpr::AltList > rhsAlts;
 | 
|---|
| [82f3226] | 200 |                                                 combos( std::next(args.begin(), 1), args.end(),
 | 
|---|
| [78315272] | 201 |                                                         std::back_inserter( rhsAlts ) );
 | 
|---|
 | 202 |                                                 for ( const ResolvExpr::AltList& rhsAlt : rhsAlts ) {
 | 
|---|
 | 203 |                                                         // multiple assignment
 | 
|---|
 | 204 |                                                         ResolvExpr::AltList rhs;
 | 
|---|
| [82f3226] | 205 |                                                         explode( rhsAlt, currentFinder.get_indexer(),
 | 
|---|
| [78315272] | 206 |                                                                 std::back_inserter(rhs), true );
 | 
|---|
 | 207 |                                                         matcher.reset( new MultipleAssignMatcher( *this, lhs, rhs ) );
 | 
|---|
 | 208 |                                                         match();
 | 
|---|
 | 209 |                                                 }
 | 
|---|
 | 210 |                                         } else {
 | 
|---|
 | 211 |                                                 for ( const ResolvExpr::Alternative& rhsAlt : args[1] ) {
 | 
|---|
 | 212 |                                                         ResolvExpr::AltList rhs;
 | 
|---|
 | 213 |                                                         if ( isTuple(rhsAlt.expr) ) {
 | 
|---|
 | 214 |                                                                 // multiple assignment
 | 
|---|
| [82f3226] | 215 |                                                                 explode( rhsAlt, currentFinder.get_indexer(),
 | 
|---|
| [78315272] | 216 |                                                                         std::back_inserter(rhs), true );
 | 
|---|
 | 217 |                                                                 matcher.reset( new MultipleAssignMatcher( *this, lhs, rhs ) );
 | 
|---|
 | 218 |                                                         } else {
 | 
|---|
 | 219 |                                                                 // mass assignment
 | 
|---|
 | 220 |                                                                 rhs.push_back( rhsAlt );
 | 
|---|
 | 221 |                                                                 matcher.reset( new MassAssignMatcher( *this, lhs, rhs ) );
 | 
|---|
 | 222 |                                                         }
 | 
|---|
 | 223 |                                                         match();
 | 
|---|
 | 224 |                                                 }
 | 
|---|
| [51587aa] | 225 |                                         }
 | 
|---|
 | 226 |                                 }
 | 
|---|
 | 227 |                         }
 | 
|---|
 | 228 |                 }
 | 
|---|
 | 229 |         }
 | 
|---|
 | 230 | 
 | 
|---|
| [234b1cb] | 231 |         void TupleAssignSpotter_old::match() {
 | 
|---|
| [5af62f1] | 232 |                 assert ( matcher != 0 );
 | 
|---|
| [51587aa] | 233 | 
 | 
|---|
| [5af62f1] | 234 |                 std::list< Expression * > new_assigns;
 | 
|---|
 | 235 |                 matcher->match( new_assigns );
 | 
|---|
 | 236 | 
 | 
|---|
| [f831177] | 237 |                 if ( ! matcher->lhs.empty() || ! matcher->rhs.empty() ) {
 | 
|---|
 | 238 |                         // if both lhs and rhs are empty then this is the empty tuple case, wherein it's okay for new_assigns to be empty.
 | 
|---|
 | 239 |                         // if not the empty tuple case, return early so that no new alternatives are generated.
 | 
|---|
 | 240 |                         if ( new_assigns.empty() ) return;
 | 
|---|
 | 241 |                 }
 | 
|---|
| [5af62f1] | 242 |                 ResolvExpr::AltList current;
 | 
|---|
 | 243 |                 // now resolve new assignments
 | 
|---|
| [82f3226] | 244 |                 for ( std::list< Expression * >::iterator i = new_assigns.begin();
 | 
|---|
| [78315272] | 245 |                                 i != new_assigns.end(); ++i ) {
 | 
|---|
| [4b6ef70] | 246 |                         PRINT(
 | 
|---|
 | 247 |                                 std::cerr << "== resolving tuple assign ==" << std::endl;
 | 
|---|
 | 248 |                                 std::cerr << *i << std::endl;
 | 
|---|
 | 249 |                         )
 | 
|---|
 | 250 | 
 | 
|---|
| [82f3226] | 251 |                         ResolvExpr::AlternativeFinder finder{ currentFinder.get_indexer(),
 | 
|---|
| [753bf60] | 252 |                                 matcher->compositeEnv };
 | 
|---|
 | 253 | 
 | 
|---|
| [ac9ca96] | 254 |                         try {
 | 
|---|
 | 255 |                                 finder.findWithAdjustment(*i);
 | 
|---|
 | 256 |                         } catch (...) {
 | 
|---|
| [1d2b64f] | 257 |                                 return; // no match should not mean failure, it just means this particular tuple assignment isn't valid
 | 
|---|
| [ac9ca96] | 258 |                         }
 | 
|---|
| [5af62f1] | 259 |                         // prune expressions that don't coincide with
 | 
|---|
 | 260 |                         ResolvExpr::AltList alts = finder.get_alternatives();
 | 
|---|
 | 261 |                         assert( alts.size() == 1 );
 | 
|---|
 | 262 |                         assert( alts.front().expr != 0 );
 | 
|---|
| [908cc83] | 263 |                         current.push_back( alts.front() );
 | 
|---|
| [5af62f1] | 264 |                 }
 | 
|---|
 | 265 | 
 | 
|---|
| [7870799] | 266 |                 // extract expressions from the assignment alternatives to produce a list of assignments
 | 
|---|
| [6d6e829] | 267 |                 // that together form a single alternative
 | 
|---|
| [6eb8948] | 268 |                 std::list< Expression *> solved_assigns;
 | 
|---|
 | 269 |                 for ( ResolvExpr::Alternative & alt : current ) {
 | 
|---|
 | 270 |                         solved_assigns.push_back( alt.expr->clone() );
 | 
|---|
| [6d6e829] | 271 |                         matcher->combineState( alt );
 | 
|---|
| [6eb8948] | 272 |                 }
 | 
|---|
| [7870799] | 273 | 
 | 
|---|
| [bd4f2e9] | 274 |                 // xxx -- was push_front
 | 
|---|
| [6d6e829] | 275 |                 currentFinder.get_alternatives().push_back( ResolvExpr::Alternative{
 | 
|---|
| [7870799] | 276 |                         new TupleAssignExpr{ solved_assigns, matcher->tmpDecls }, matcher->compositeEnv,
 | 
|---|
 | 277 |                         matcher->openVars,
 | 
|---|
 | 278 |                         ResolvExpr::AssertionList( matcher->need.begin(), matcher->need.end() ),
 | 
|---|
| [6d6e829] | 279 |                         ResolvExpr::sumCost( current ) + matcher->baseCost } );
 | 
|---|
| [4b6ef70] | 280 |         }
 | 
|---|
 | 281 | 
 | 
|---|
| [234b1cb] | 282 |         TupleAssignSpotter_old::Matcher::Matcher( TupleAssignSpotter_old &spotter,
 | 
|---|
| [82f3226] | 283 |                 const ResolvExpr::AltList &lhs, const ResolvExpr::AltList &rhs )
 | 
|---|
 | 284 |         : lhs(lhs), rhs(rhs), spotter(spotter),
 | 
|---|
| [78315272] | 285 |           baseCost( ResolvExpr::sumCost( lhs ) + ResolvExpr::sumCost( rhs ) ) {
 | 
|---|
| [7870799] | 286 |                 combineState( lhs );
 | 
|---|
| [6d6e829] | 287 |                 combineState( rhs );
 | 
|---|
| [4b6ef70] | 288 |         }
 | 
|---|
| [51587aa] | 289 | 
 | 
|---|
| [65660bd] | 290 |         UntypedExpr * createFunc( const std::string &fname, ObjectDecl *left, ObjectDecl *right ) {
 | 
|---|
 | 291 |                 assert( left );
 | 
|---|
| [5af62f1] | 292 |                 std::list< Expression * > args;
 | 
|---|
| [e6cee92] | 293 |                 args.push_back( new VariableExpr( left ) );
 | 
|---|
| [65660bd] | 294 |                 // args.push_back( new AddressExpr( new VariableExpr( left ) ) );
 | 
|---|
 | 295 |                 if ( right ) args.push_back( new VariableExpr( right ) );
 | 
|---|
| [9058414] | 296 |                 if ( left->type->referenceDepth() > 1 && CodeGen::isConstructor( fname ) ) {
 | 
|---|
 | 297 |                         args.front() = new AddressExpr( args.front() );
 | 
|---|
 | 298 |                         if ( right ) args.back() = new AddressExpr( args.back() );
 | 
|---|
 | 299 |                         return new UntypedExpr( new NameExpr( "?=?" ), args );
 | 
|---|
 | 300 |                 } else {
 | 
|---|
 | 301 |                         return new UntypedExpr( new NameExpr( fname ), args );
 | 
|---|
 | 302 |                 }
 | 
|---|
| [51587aa] | 303 |         }
 | 
|---|
 | 304 | 
 | 
|---|
| [938dd75] | 305 |         // removes environments from subexpressions within statement exprs, which could throw off later passes like those in Box which rely on PolyMutator, and adds the bindings to the compositeEnv
 | 
|---|
| [1d2b64f] | 306 |         // xxx - maybe this should happen in alternative finder for every StmtExpr?
 | 
|---|
| [2449aef] | 307 |         struct EnvRemover {
 | 
|---|
 | 308 |                 void previsit( ExprStmt * stmt ) {
 | 
|---|
| [938dd75] | 309 |                         assert( compositeEnv );
 | 
|---|
 | 310 |                         if ( stmt->expr->env ) {
 | 
|---|
 | 311 |                                 compositeEnv->add( *stmt->expr->env );
 | 
|---|
 | 312 |                                 delete stmt->expr->env;
 | 
|---|
 | 313 |                                 stmt->expr->env = nullptr;
 | 
|---|
 | 314 |                         }
 | 
|---|
| [1d2b64f] | 315 |                 }
 | 
|---|
| [938dd75] | 316 | 
 | 
|---|
 | 317 |                 ResolvExpr::TypeEnvironment * compositeEnv = nullptr;
 | 
|---|
| [1d2b64f] | 318 |         };
 | 
|---|
 | 319 | 
 | 
|---|
| [234b1cb] | 320 |         ObjectDecl * TupleAssignSpotter_old::Matcher::newObject( UniqueName & namer, Expression * expr ) {
 | 
|---|
| [d29fa5f] | 321 |                 assert( expr->result && ! expr->get_result()->isVoid() );
 | 
|---|
| [4f68f6d] | 322 |                 ObjectDecl * ret = new ObjectDecl( namer.newName(), Type::StorageClasses(), LinkageSpec::Cforall, nullptr, expr->result->clone(), new SingleInit( expr->clone() ) );
 | 
|---|
| [e6cee92] | 323 |                 // if expression type is a reference, don't need to construct anything, a simple initializer is sufficient.
 | 
|---|
| [4f68f6d] | 324 |                 if ( ! dynamic_cast< ReferenceType * >( expr->result ) ) {
 | 
|---|
| [e6cee92] | 325 |                         ConstructorInit * ctorInit = InitTweak::genCtorInit( ret );
 | 
|---|
| [9058414] | 326 |                         ret->init = ctorInit;
 | 
|---|
| [e6cee92] | 327 |                         ResolvExpr::resolveCtorInit( ctorInit, spotter.currentFinder.get_indexer() ); // resolve ctor/dtors for the new object
 | 
|---|
| [2449aef] | 328 |                         PassVisitor<EnvRemover> rm; // remove environments from subexpressions of StmtExprs
 | 
|---|
| [938dd75] | 329 |                         rm.pass.compositeEnv = &compositeEnv;
 | 
|---|
| [e6cee92] | 330 |                         ctorInit->accept( rm );
 | 
|---|
 | 331 |                 }
 | 
|---|
| [f5854507] | 332 |                 PRINT( std::cerr << "new object: " << ret << std::endl; )
 | 
|---|
| [1d2b64f] | 333 |                 return ret;
 | 
|---|
| [6eb8948] | 334 |         }
 | 
|---|
 | 335 | 
 | 
|---|
| [234b1cb] | 336 |         void TupleAssignSpotter_old::MassAssignMatcher::match( std::list< Expression * > &out ) {
 | 
|---|
| [6eb8948] | 337 |                 static UniqueName lhsNamer( "__massassign_L" );
 | 
|---|
 | 338 |                 static UniqueName rhsNamer( "__massassign_R" );
 | 
|---|
| [f831177] | 339 |                 // empty tuple case falls into this matcher, hence the second part of the assert
 | 
|---|
 | 340 |                 assert( (! lhs.empty() && rhs.size() <= 1) || (lhs.empty() && rhs.empty()) );
 | 
|---|
| [51587aa] | 341 | 
 | 
|---|
| [d20a343] | 342 |                 // xxx - may need to split this up into multiple declarations, because potential conversion to references
 | 
|---|
 | 343 |                 //  probably should not reference local variable - see MultipleAssignMatcher::match
 | 
|---|
| [65660bd] | 344 |                 ObjectDecl * rtmp = rhs.size() == 1 ? newObject( rhsNamer, rhs.front().expr ) : nullptr;
 | 
|---|
| [3c13c03] | 345 |                 for ( ResolvExpr::Alternative & lhsAlt : lhs ) {
 | 
|---|
| [f831177] | 346 |                         // create a temporary object for each value in the lhs and create a call involving the rhs
 | 
|---|
| [65660bd] | 347 |                         ObjectDecl * ltmp = newObject( lhsNamer, lhsAlt.expr );
 | 
|---|
 | 348 |                         out.push_back( createFunc( spotter.fname, ltmp, rtmp ) );
 | 
|---|
| [6eb8948] | 349 |                         tmpDecls.push_back( ltmp );
 | 
|---|
| [5af62f1] | 350 |                 }
 | 
|---|
| [65660bd] | 351 |                 if ( rtmp ) tmpDecls.push_back( rtmp );
 | 
|---|
| [51b73452] | 352 |         }
 | 
|---|
 | 353 | 
 | 
|---|
| [234b1cb] | 354 |         void TupleAssignSpotter_old::MultipleAssignMatcher::match( std::list< Expression * > &out ) {
 | 
|---|
| [6eb8948] | 355 |                 static UniqueName lhsNamer( "__multassign_L" );
 | 
|---|
 | 356 |                 static UniqueName rhsNamer( "__multassign_R" );
 | 
|---|
| [b3b2077] | 357 | 
 | 
|---|
| [51587aa] | 358 |                 if ( lhs.size() == rhs.size() ) {
 | 
|---|
| [f831177] | 359 |                         // produce a new temporary object for each value in the lhs and rhs and pairwise create the calls
 | 
|---|
| [6eb8948] | 360 |                         std::list< ObjectDecl * > ltmp;
 | 
|---|
 | 361 |                         std::list< ObjectDecl * > rtmp;
 | 
|---|
| [d20a343] | 362 |                         for ( auto p : group_iterate( lhs, rhs ) ) {
 | 
|---|
 | 363 |                                 ResolvExpr::Alternative & lhsAlt = std::get<0>(p);
 | 
|---|
 | 364 |                                 ResolvExpr::Alternative & rhsAlt = std::get<1>(p);
 | 
|---|
 | 365 |                                 // convert RHS to LHS type minus one reference -- important for the case where LHS is && and RHS is lvalue, etc.
 | 
|---|
 | 366 |                                 ReferenceType * lhsType = strict_dynamic_cast<ReferenceType *>( lhsAlt.expr->result );
 | 
|---|
 | 367 |                                 rhsAlt.expr = new CastExpr( rhsAlt.expr, lhsType->base->clone() );
 | 
|---|
 | 368 |                                 ObjectDecl * lobj = newObject( lhsNamer, lhsAlt.expr );
 | 
|---|
 | 369 |                                 ObjectDecl * robj = newObject( rhsNamer, rhsAlt.expr );
 | 
|---|
 | 370 |                                 out.push_back( createFunc(spotter.fname, lobj, robj) );
 | 
|---|
 | 371 |                                 ltmp.push_back( lobj );
 | 
|---|
 | 372 |                                 rtmp.push_back( robj );
 | 
|---|
| [753bf60] | 373 | 
 | 
|---|
 | 374 |                                 // resolve the cast expression so that rhsAlt return type is bound by the cast type as needed, and transfer the resulting environment
 | 
|---|
 | 375 |                                 ResolvExpr::AlternativeFinder finder{ spotter.currentFinder.get_indexer(), compositeEnv };
 | 
|---|
 | 376 |                                 finder.findWithAdjustment( rhsAlt.expr );
 | 
|---|
 | 377 |                                 assert( finder.get_alternatives().size() == 1 );
 | 
|---|
 | 378 |                                 compositeEnv = std::move( finder.get_alternatives().front().env );
 | 
|---|
| [d20a343] | 379 |                         }
 | 
|---|
| [6eb8948] | 380 |                         tmpDecls.splice( tmpDecls.end(), ltmp );
 | 
|---|
 | 381 |                         tmpDecls.splice( tmpDecls.end(), rtmp );
 | 
|---|
| [5af62f1] | 382 |                 }
 | 
|---|
| [51587aa] | 383 |         }
 | 
|---|
| [432ce7a] | 384 | 
 | 
|---|
| [234b1cb] | 385 | namespace {
 | 
|---|
 | 386 |         /// true if `expr` is of tuple type
 | 
|---|
 | 387 |         bool isTuple( const ast::Expr * expr ) {
 | 
|---|
 | 388 |                 if ( ! expr ) return false;
 | 
|---|
 | 389 |                 assert( expr->result );
 | 
|---|
 | 390 |                 return dynamic_cast< const ast::TupleType * >( expr->result->stripReferences() );
 | 
|---|
 | 391 |         }
 | 
|---|
| [7870799] | 392 | 
 | 
|---|
| [234b1cb] | 393 |         /// true if `expr` is of tuple type or a reference to one
 | 
|---|
 | 394 |         bool refToTuple( const ast::Expr * expr ) {
 | 
|---|
 | 395 |                 assert( expr->result );
 | 
|---|
 | 396 |                 // check for function returning tuple of reference types
 | 
|---|
 | 397 |                 if ( auto castExpr = dynamic_cast< const ast::CastExpr * >( expr ) ) {
 | 
|---|
 | 398 |                         return refToTuple( castExpr->arg );
 | 
|---|
 | 399 |                 } else {
 | 
|---|
 | 400 |                         return isTuple( expr );
 | 
|---|
 | 401 |                 }
 | 
|---|
| [432ce7a] | 402 |         }
 | 
|---|
| [234b1cb] | 403 | 
 | 
|---|
 | 404 |         /// Dispatcher for tuple (multiple and mass) assignment operations
 | 
|---|
 | 405 |         class TupleAssignSpotter_new final {
 | 
|---|
 | 406 |                 /// Actually finds tuple assignment operations, by subclass
 | 
|---|
 | 407 |                 struct Matcher {
 | 
|---|
 | 408 |                         ResolvExpr::CandidateList lhs, rhs;
 | 
|---|
 | 409 |                         TupleAssignSpotter_new & spotter;
 | 
|---|
 | 410 |                         CodeLocation location;
 | 
|---|
 | 411 |                         ResolvExpr::Cost baseCost;
 | 
|---|
 | 412 |                         std::vector< ast::ptr< ast::ObjectDecl > > tmpDecls;
 | 
|---|
 | 413 |                         ast::TypeEnvironment env;
 | 
|---|
 | 414 |                         ast::OpenVarSet open;
 | 
|---|
 | 415 |                         ast::AssertionSet need;
 | 
|---|
 | 416 | 
 | 
|---|
 | 417 |                         void combineState( const ResolvExpr::Candidate & cand ) {
 | 
|---|
 | 418 |                                 env.simpleCombine( cand.env );
 | 
|---|
 | 419 |                                 ast::mergeOpenVars( open, cand.open );
 | 
|---|
 | 420 |                                 need.insert( cand.need.begin(), cand.need.end() );
 | 
|---|
 | 421 |                         }
 | 
|---|
 | 422 | 
 | 
|---|
| [7870799] | 423 |                         Matcher(
 | 
|---|
| [234b1cb] | 424 |                                 TupleAssignSpotter_new & s, const CodeLocation & loc,
 | 
|---|
 | 425 |                                 const ResolvExpr::CandidateList & l, const ResolvExpr::CandidateList & r )
 | 
|---|
| [7870799] | 426 |                         : lhs( l ), rhs( r ), spotter( s ), location( loc ),
 | 
|---|
 | 427 |                           baseCost( ResolvExpr::sumCost( lhs ) + ResolvExpr::sumCost( rhs ) ), tmpDecls(),
 | 
|---|
| [234b1cb] | 428 |                           env(), open(), need() {
 | 
|---|
 | 429 |                                 for ( auto & cand : lhs ) combineState( *cand );
 | 
|---|
 | 430 |                                 for ( auto & cand : rhs ) combineState( *cand );
 | 
|---|
 | 431 |                         }
 | 
|---|
| [7870799] | 432 |                         virtual ~Matcher() = default;
 | 
|---|
| [234b1cb] | 433 | 
 | 
|---|
 | 434 |                         virtual std::vector< ast::ptr< ast::Expr > > match() = 0;
 | 
|---|
 | 435 | 
 | 
|---|
| [7870799] | 436 |                         /// removes environments from subexpressions within statement expressions, which could
 | 
|---|
 | 437 |                         /// throw off later passes like those in Box which rely on PolyMutator, and adds the
 | 
|---|
| [234b1cb] | 438 |                         /// bindings to the env
 | 
|---|
 | 439 |                         struct EnvRemover {
 | 
|---|
 | 440 |                                 /// environment to hoist ExprStmt environments to
 | 
|---|
 | 441 |                                 ast::TypeEnvironment & tenv;
 | 
|---|
 | 442 | 
 | 
|---|
 | 443 |                                 EnvRemover( ast::TypeEnvironment & e ) : tenv( e ) {}
 | 
|---|
 | 444 | 
 | 
|---|
 | 445 |                                 const ast::ExprStmt * previsit( const ast::ExprStmt * stmt ) {
 | 
|---|
 | 446 |                                         if ( stmt->expr->env ) {
 | 
|---|
 | 447 |                                                 tenv.add( *stmt->expr->env );
 | 
|---|
 | 448 |                                                 ast::ExprStmt * mut = mutate( stmt );
 | 
|---|
 | 449 |                                                 mut->expr.get_and_mutate()->env = nullptr;
 | 
|---|
 | 450 |                                                 return mut;
 | 
|---|
 | 451 |                                         }
 | 
|---|
 | 452 |                                         return stmt;
 | 
|---|
 | 453 |                                 }
 | 
|---|
 | 454 |                         };
 | 
|---|
 | 455 | 
 | 
|---|
 | 456 |                         ast::ObjectDecl * newObject( UniqueName & namer, const ast::Expr * expr ) {
 | 
|---|
 | 457 |                                 assert( expr->result && ! expr->result->isVoid() );
 | 
|---|
| [7870799] | 458 | 
 | 
|---|
 | 459 |                                 ast::ObjectDecl * ret = new ast::ObjectDecl{
 | 
|---|
 | 460 |                                         location, namer.newName(), expr->result, new ast::SingleInit{ location, expr },
 | 
|---|
| [234b1cb] | 461 |                                         ast::Storage::Classes{}, ast::Linkage::Cforall };
 | 
|---|
| [7870799] | 462 | 
 | 
|---|
| [234b1cb] | 463 |                                 // if expression type is a reference, just need an initializer, otherwise construct
 | 
|---|
 | 464 |                                 if ( ! expr->result.as< ast::ReferenceType >() ) {
 | 
|---|
 | 465 |                                         // resolve ctor/dtor for the new object
 | 
|---|
| [7870799] | 466 |                                         ast::ptr< ast::Init > ctorInit = ResolvExpr::resolveCtorInit(
 | 
|---|
| [9ea38de] | 467 |                                                         InitTweak::genCtorInit( location, ret ), spotter.crntFinder.localSyms );
 | 
|---|
| [234b1cb] | 468 |                                         // remove environments from subexpressions of stmtExpr
 | 
|---|
 | 469 |                                         ast::Pass< EnvRemover > rm{ env };
 | 
|---|
 | 470 |                                         ret->init = ctorInit->accept( rm );
 | 
|---|
 | 471 |                                 }
 | 
|---|
 | 472 | 
 | 
|---|
 | 473 |                                 PRINT( std::cerr << "new object: " << ret << std::endl; )
 | 
|---|
 | 474 |                                 return ret;
 | 
|---|
 | 475 |                         }
 | 
|---|
 | 476 | 
 | 
|---|
| [7870799] | 477 |                         ast::UntypedExpr * createFunc(
 | 
|---|
 | 478 |                                 const std::string & fname, const ast::ObjectDecl * left,
 | 
|---|
 | 479 |                                 const ast::ObjectDecl * right
 | 
|---|
| [234b1cb] | 480 |                         ) {
 | 
|---|
 | 481 |                                 assert( left );
 | 
|---|
 | 482 |                                 std::vector< ast::ptr< ast::Expr > > args;
 | 
|---|
 | 483 |                                 args.emplace_back( new ast::VariableExpr{ location, left } );
 | 
|---|
 | 484 |                                 if ( right ) { args.emplace_back( new ast::VariableExpr{ location, right } ); }
 | 
|---|
 | 485 | 
 | 
|---|
 | 486 |                                 if ( left->type->referenceDepth() > 1 && CodeGen::isConstructor( fname ) ) {
 | 
|---|
 | 487 |                                         args.front() = new ast::AddressExpr{ location, args.front() };
 | 
|---|
 | 488 |                                         if ( right ) { args.back() = new ast::AddressExpr{ location, args.back() }; }
 | 
|---|
| [7870799] | 489 |                                         return new ast::UntypedExpr{
 | 
|---|
| [234b1cb] | 490 |                                                 location, new ast::NameExpr{ location, "?=?" }, std::move(args) };
 | 
|---|
 | 491 |                                 } else {
 | 
|---|
| [7870799] | 492 |                                         return new ast::UntypedExpr{
 | 
|---|
| [234b1cb] | 493 |                                                 location, new ast::NameExpr{ location, fname }, std::move(args) };
 | 
|---|
 | 494 |                                 }
 | 
|---|
 | 495 |                         }
 | 
|---|
 | 496 |                 };
 | 
|---|
 | 497 | 
 | 
|---|
 | 498 |                 /// Finds mass-assignment operations
 | 
|---|
 | 499 |                 struct MassAssignMatcher final : public Matcher {
 | 
|---|
 | 500 |                         MassAssignMatcher(
 | 
|---|
| [7870799] | 501 |                                 TupleAssignSpotter_new & s, const CodeLocation & loc,
 | 
|---|
| [234b1cb] | 502 |                                 const ResolvExpr::CandidateList & l, const ResolvExpr::CandidateList & r )
 | 
|---|
 | 503 |                         : Matcher( s, loc, l, r ) {}
 | 
|---|
 | 504 | 
 | 
|---|
 | 505 |                         std::vector< ast::ptr< ast::Expr > > match() override {
 | 
|---|
| [3e5dd913] | 506 |                                 static UniqueName lhsNamer( "__massassign_L" );
 | 
|---|
 | 507 |                                 static UniqueName rhsNamer( "__massassign_R" );
 | 
|---|
| [234b1cb] | 508 |                                 // empty tuple case falls into this matcher
 | 
|---|
 | 509 |                                 assert( lhs.empty() ? rhs.empty() : rhs.size() <= 1 );
 | 
|---|
 | 510 | 
 | 
|---|
| [7870799] | 511 |                                 ast::ptr< ast::ObjectDecl > rtmp =
 | 
|---|
| [234b1cb] | 512 |                                         rhs.size() == 1 ? newObject( rhsNamer, rhs.front()->expr ) : nullptr;
 | 
|---|
 | 513 | 
 | 
|---|
 | 514 |                                 std::vector< ast::ptr< ast::Expr > > out;
 | 
|---|
 | 515 |                                 for ( ResolvExpr::CandidateRef & lhsCand : lhs ) {
 | 
|---|
| [7870799] | 516 |                                         // create a temporary object for each value in the LHS and create a call
 | 
|---|
| [234b1cb] | 517 |                                         // involving the RHS
 | 
|---|
 | 518 |                                         ast::ptr< ast::ObjectDecl > ltmp = newObject( lhsNamer, lhsCand->expr );
 | 
|---|
 | 519 |                                         out.emplace_back( createFunc( spotter.fname, ltmp, rtmp ) );
 | 
|---|
 | 520 |                                         tmpDecls.emplace_back( std::move( ltmp ) );
 | 
|---|
 | 521 |                                 }
 | 
|---|
 | 522 |                                 if ( rtmp ) tmpDecls.emplace_back( std::move( rtmp ) );
 | 
|---|
 | 523 | 
 | 
|---|
 | 524 |                                 return out;
 | 
|---|
 | 525 |                         }
 | 
|---|
 | 526 |                 };
 | 
|---|
 | 527 | 
 | 
|---|
 | 528 |                 /// Finds multiple-assignment operations
 | 
|---|
 | 529 |                 struct MultipleAssignMatcher final : public Matcher {
 | 
|---|
 | 530 |                         MultipleAssignMatcher(
 | 
|---|
| [7870799] | 531 |                                 TupleAssignSpotter_new & s, const CodeLocation & loc,
 | 
|---|
| [234b1cb] | 532 |                                 const ResolvExpr::CandidateList & l, const ResolvExpr::CandidateList & r )
 | 
|---|
 | 533 |                         : Matcher( s, loc, l, r ) {}
 | 
|---|
 | 534 | 
 | 
|---|
 | 535 |                         std::vector< ast::ptr< ast::Expr > > match() override {
 | 
|---|
| [3e5dd913] | 536 |                                 static UniqueName lhsNamer( "__multassign_L" );
 | 
|---|
 | 537 |                                 static UniqueName rhsNamer( "__multassign_R" );
 | 
|---|
| [234b1cb] | 538 | 
 | 
|---|
 | 539 |                                 if ( lhs.size() != rhs.size() ) return {};
 | 
|---|
 | 540 | 
 | 
|---|
| [7870799] | 541 |                                 // produce a new temporary object for each value in the LHS and RHS and pairwise
 | 
|---|
| [234b1cb] | 542 |                                 // create the calls
 | 
|---|
 | 543 |                                 std::vector< ast::ptr< ast::ObjectDecl > > ltmp, rtmp;
 | 
|---|
 | 544 | 
 | 
|---|
 | 545 |                                 std::vector< ast::ptr< ast::Expr > > out;
 | 
|---|
 | 546 |                                 for ( unsigned i = 0; i < lhs.size(); ++i ) {
 | 
|---|
 | 547 |                                         ResolvExpr::CandidateRef & lhsCand = lhs[i];
 | 
|---|
 | 548 |                                         ResolvExpr::CandidateRef & rhsCand = rhs[i];
 | 
|---|
 | 549 | 
 | 
|---|
| [7870799] | 550 |                                         // convert RHS to LHS type minus one reference -- important for case where LHS
 | 
|---|
| [234b1cb] | 551 |                                         // is && and RHS is lvalue
 | 
|---|
 | 552 |                                         auto lhsType = lhsCand->expr->result.strict_as< ast::ReferenceType >();
 | 
|---|
| [b8524ca] | 553 |                                         rhsCand->expr = new ast::CastExpr{ rhsCand->expr, lhsType->base };
 | 
|---|
| [234b1cb] | 554 |                                         ast::ptr< ast::ObjectDecl > lobj = newObject( lhsNamer, lhsCand->expr );
 | 
|---|
 | 555 |                                         ast::ptr< ast::ObjectDecl > robj = newObject( rhsNamer, rhsCand->expr );
 | 
|---|
 | 556 |                                         out.emplace_back( createFunc( spotter.fname, lobj, robj ) );
 | 
|---|
 | 557 |                                         ltmp.emplace_back( std::move( lobj ) );
 | 
|---|
 | 558 |                                         rtmp.emplace_back( std::move( robj ) );
 | 
|---|
 | 559 | 
 | 
|---|
| [7870799] | 560 |                                         // resolve the cast expression so that rhsCand return type is bound by the cast
 | 
|---|
| [234b1cb] | 561 |                                         // type as needed, and transfer the resulting environment
 | 
|---|
| [9ea38de] | 562 |                                         ResolvExpr::CandidateFinder finder{ spotter.crntFinder.localSyms, env };
 | 
|---|
| [234b1cb] | 563 |                                         finder.find( rhsCand->expr, ResolvExpr::ResolvMode::withAdjustment() );
 | 
|---|
 | 564 |                                         assert( finder.candidates.size() == 1 );
 | 
|---|
 | 565 |                                         env = std::move( finder.candidates.front()->env );
 | 
|---|
 | 566 |                                 }
 | 
|---|
| [7870799] | 567 | 
 | 
|---|
| [234b1cb] | 568 |                                 splice( tmpDecls, ltmp );
 | 
|---|
 | 569 |                                 splice( tmpDecls, rtmp );
 | 
|---|
| [7870799] | 570 | 
 | 
|---|
| [234b1cb] | 571 |                                 return out;
 | 
|---|
 | 572 |                         }
 | 
|---|
 | 573 |                 };
 | 
|---|
 | 574 | 
 | 
|---|
 | 575 |                 ResolvExpr::CandidateFinder & crntFinder;
 | 
|---|
 | 576 |                 std::string fname;
 | 
|---|
 | 577 |                 std::unique_ptr< Matcher > matcher;
 | 
|---|
| [7870799] | 578 | 
 | 
|---|
| [234b1cb] | 579 |         public:
 | 
|---|
| [7870799] | 580 |                 TupleAssignSpotter_new( ResolvExpr::CandidateFinder & f )
 | 
|---|
| [234b1cb] | 581 |                 : crntFinder( f ), fname(), matcher() {}
 | 
|---|
 | 582 | 
 | 
|---|
 | 583 |                 // find left- and right-hand-sides for mass or multiple assignment
 | 
|---|
| [7870799] | 584 |                 void spot(
 | 
|---|
 | 585 |                         const ast::UntypedExpr * expr, std::vector< ResolvExpr::CandidateFinder > & args
 | 
|---|
| [234b1cb] | 586 |                 ) {
 | 
|---|
 | 587 |                         if ( auto op = expr->func.as< ast::NameExpr >() ) {
 | 
|---|
 | 588 |                                 // skip non-assignment functions
 | 
|---|
 | 589 |                                 if ( ! CodeGen::isCtorDtorAssign( op->name ) ) return;
 | 
|---|
 | 590 |                                 fname = op->name;
 | 
|---|
 | 591 | 
 | 
|---|
 | 592 |                                 // handled by CandidateFinder if applicable (both odd cases)
 | 
|---|
 | 593 |                                 if ( args.empty() || ( args.size() == 1 && CodeGen::isAssignment( fname ) ) ) {
 | 
|---|
 | 594 |                                         return;
 | 
|---|
 | 595 |                                 }
 | 
|---|
 | 596 | 
 | 
|---|
 | 597 |                                 // look over all possible left-hand-side
 | 
|---|
 | 598 |                                 for ( ResolvExpr::CandidateRef & lhsCand : args[0] ) {
 | 
|---|
 | 599 |                                         // skip non-tuple LHS
 | 
|---|
 | 600 |                                         if ( ! refToTuple( lhsCand->expr ) ) continue;
 | 
|---|
 | 601 | 
 | 
|---|
| [7870799] | 602 |                                         // explode is aware of casts - ensure every LHS is sent into explode with a
 | 
|---|
| [234b1cb] | 603 |                                         // reference cast
 | 
|---|
 | 604 |                                         if ( ! lhsCand->expr.as< ast::CastExpr >() ) {
 | 
|---|
| [7870799] | 605 |                                                 lhsCand->expr = new ast::CastExpr{
 | 
|---|
| [b8524ca] | 606 |                                                         lhsCand->expr, new ast::ReferenceType{ lhsCand->expr->result } };
 | 
|---|
| [234b1cb] | 607 |                                         }
 | 
|---|
 | 608 | 
 | 
|---|
 | 609 |                                         // explode the LHS so that each field of a tuple-valued expr is assigned
 | 
|---|
 | 610 |                                         ResolvExpr::CandidateList lhs;
 | 
|---|
| [9ea38de] | 611 |                                         explode( *lhsCand, crntFinder.localSyms, back_inserter(lhs), true );
 | 
|---|
| [234b1cb] | 612 |                                         for ( ResolvExpr::CandidateRef & cand : lhs ) {
 | 
|---|
| [7870799] | 613 |                                                 // each LHS value must be a reference - some come in with a cast, if not
 | 
|---|
| [234b1cb] | 614 |                                                 // just cast to reference here
 | 
|---|
 | 615 |                                                 if ( ! cand->expr->result.as< ast::ReferenceType >() ) {
 | 
|---|
 | 616 |                                                         cand->expr = new ast::CastExpr{
 | 
|---|
| [b8524ca] | 617 |                                                                 cand->expr, new ast::ReferenceType{ cand->expr->result } };
 | 
|---|
| [234b1cb] | 618 |                                                 }
 | 
|---|
 | 619 |                                         }
 | 
|---|
 | 620 | 
 | 
|---|
 | 621 |                                         if ( args.size() == 1 ) {
 | 
|---|
 | 622 |                                                 // mass default-initialization/destruction
 | 
|---|
 | 623 |                                                 ResolvExpr::CandidateList rhs{};
 | 
|---|
 | 624 |                                                 matcher.reset( new MassAssignMatcher{ *this, expr->location, lhs, rhs } );
 | 
|---|
 | 625 |                                                 match();
 | 
|---|
 | 626 |                                         } else if ( args.size() == 2 ) {
 | 
|---|
 | 627 |                                                 for ( const ResolvExpr::CandidateRef & rhsCand : args[1] ) {
 | 
|---|
 | 628 |                                                         ResolvExpr::CandidateList rhs;
 | 
|---|
 | 629 |                                                         if ( isTuple( rhsCand->expr ) ) {
 | 
|---|
 | 630 |                                                                 // multiple assignment
 | 
|---|
| [9ea38de] | 631 |                                                                 explode( *rhsCand, crntFinder.localSyms, back_inserter(rhs), true );
 | 
|---|
| [7870799] | 632 |                                                                 matcher.reset(
 | 
|---|
| [234b1cb] | 633 |                                                                         new MultipleAssignMatcher{ *this, expr->location, lhs, rhs } );
 | 
|---|
 | 634 |                                                         } else {
 | 
|---|
 | 635 |                                                                 // mass assignment
 | 
|---|
 | 636 |                                                                 rhs.emplace_back( rhsCand );
 | 
|---|
| [7870799] | 637 |                                                                 matcher.reset(
 | 
|---|
| [234b1cb] | 638 |                                                                         new MassAssignMatcher{ *this, expr->location, lhs, rhs } );
 | 
|---|
 | 639 |                                                         }
 | 
|---|
 | 640 |                                                         match();
 | 
|---|
 | 641 |                                                 }
 | 
|---|
 | 642 |                                         } else {
 | 
|---|
 | 643 |                                                 // expand all possible RHS possibilities
 | 
|---|
 | 644 |                                                 std::vector< ResolvExpr::CandidateList > rhsCands;
 | 
|---|
| [7870799] | 645 |                                                 combos(
 | 
|---|
| [234b1cb] | 646 |                                                         std::next( args.begin(), 1 ), args.end(), back_inserter( rhsCands ) );
 | 
|---|
 | 647 |                                                 for ( const ResolvExpr::CandidateList & rhsCand : rhsCands ) {
 | 
|---|
 | 648 |                                                         // multiple assignment
 | 
|---|
 | 649 |                                                         ResolvExpr::CandidateList rhs;
 | 
|---|
| [9ea38de] | 650 |                                                         explode( rhsCand, crntFinder.localSyms, back_inserter(rhs), true );
 | 
|---|
| [7870799] | 651 |                                                         matcher.reset(
 | 
|---|
| [234b1cb] | 652 |                                                                 new MultipleAssignMatcher{ *this, expr->location, lhs, rhs } );
 | 
|---|
 | 653 |                                                         match();
 | 
|---|
 | 654 |                                                 }
 | 
|---|
 | 655 |                                         }
 | 
|---|
 | 656 |                                 }
 | 
|---|
 | 657 |                         }
 | 
|---|
 | 658 |                 }
 | 
|---|
 | 659 | 
 | 
|---|
 | 660 |                 void match() {
 | 
|---|
 | 661 |                         assert( matcher );
 | 
|---|
 | 662 | 
 | 
|---|
 | 663 |                         std::vector< ast::ptr< ast::Expr > > newAssigns = matcher->match();
 | 
|---|
 | 664 | 
 | 
|---|
 | 665 |                         if ( ! ( matcher->lhs.empty() && matcher->rhs.empty() ) ) {
 | 
|---|
| [7870799] | 666 |                                 // if both LHS and RHS are empty than this is the empty tuple case, wherein it's
 | 
|---|
 | 667 |                                 // okay for newAssigns to be empty. Otherwise, return early so that no new
 | 
|---|
| [234b1cb] | 668 |                                 // candidates are generated
 | 
|---|
 | 669 |                                 if ( newAssigns.empty() ) return;
 | 
|---|
 | 670 |                         }
 | 
|---|
 | 671 | 
 | 
|---|
 | 672 |                         ResolvExpr::CandidateList crnt;
 | 
|---|
 | 673 |                         // now resolve new assignments
 | 
|---|
 | 674 |                         for ( const ast::Expr * expr : newAssigns ) {
 | 
|---|
 | 675 |                                 PRINT(
 | 
|---|
 | 676 |                                         std::cerr << "== resolving tuple assign ==" << std::endl;
 | 
|---|
 | 677 |                                         std::cerr << expr << std::endl;
 | 
|---|
 | 678 |                                 )
 | 
|---|
 | 679 | 
 | 
|---|
| [9ea38de] | 680 |                                 ResolvExpr::CandidateFinder finder{ crntFinder.localSyms, matcher->env };
 | 
|---|
| [234b1cb] | 681 | 
 | 
|---|
 | 682 |                                 try {
 | 
|---|
 | 683 |                                         finder.find( expr, ResolvExpr::ResolvMode::withAdjustment() );
 | 
|---|
 | 684 |                                 } catch (...) {
 | 
|---|
 | 685 |                                         // no match is not failure, just that this tuple assignment is invalid
 | 
|---|
 | 686 |                                         return;
 | 
|---|
 | 687 |                                 }
 | 
|---|
 | 688 | 
 | 
|---|
 | 689 |                                 ResolvExpr::CandidateList & cands = finder.candidates;
 | 
|---|
 | 690 |                                 assert( cands.size() == 1 );
 | 
|---|
 | 691 |                                 assert( cands.front()->expr );
 | 
|---|
 | 692 |                                 crnt.emplace_back( std::move( cands.front() ) );
 | 
|---|
 | 693 |                         }
 | 
|---|
 | 694 | 
 | 
|---|
| [7870799] | 695 |                         // extract expressions from the assignment candidates to produce a list of assignments
 | 
|---|
| [234b1cb] | 696 |                         // that together form a sigle candidate
 | 
|---|
 | 697 |                         std::vector< ast::ptr< ast::Expr > > solved;
 | 
|---|
 | 698 |                         for ( ResolvExpr::CandidateRef & cand : crnt ) {
 | 
|---|
 | 699 |                                 solved.emplace_back( cand->expr );
 | 
|---|
 | 700 |                                 matcher->combineState( *cand );
 | 
|---|
 | 701 |                         }
 | 
|---|
 | 702 | 
 | 
|---|
 | 703 |                         crntFinder.candidates.emplace_back( std::make_shared< ResolvExpr::Candidate >(
 | 
|---|
| [7870799] | 704 |                                 new ast::TupleAssignExpr{
 | 
|---|
 | 705 |                                         matcher->location, std::move( solved ), std::move( matcher->tmpDecls ) },
 | 
|---|
 | 706 |                                 std::move( matcher->env ), std::move( matcher->open ), std::move( matcher->need ),
 | 
|---|
| [234b1cb] | 707 |                                 ResolvExpr::sumCost( crnt ) + matcher->baseCost ) );
 | 
|---|
 | 708 |                 }
 | 
|---|
 | 709 |         };
 | 
|---|
 | 710 | } // anonymous namespace
 | 
|---|
 | 711 | 
 | 
|---|
| [7870799] | 712 | void handleTupleAssignment(
 | 
|---|
 | 713 |         ResolvExpr::CandidateFinder & finder, const ast::UntypedExpr * assign,
 | 
|---|
| [234b1cb] | 714 |         std::vector< ResolvExpr::CandidateFinder > & args
 | 
|---|
 | 715 | ) {
 | 
|---|
 | 716 |         TupleAssignSpotter_new spotter{ finder };
 | 
|---|
 | 717 |         spotter.spot( assign, args );
 | 
|---|
 | 718 | }
 | 
|---|
 | 719 | 
 | 
|---|
| [51b73452] | 720 | } // namespace Tuples
 | 
|---|
| [51587aa] | 721 | 
 | 
|---|
 | 722 | // Local Variables: //
 | 
|---|
 | 723 | // tab-width: 4 //
 | 
|---|
 | 724 | // mode: c++ //
 | 
|---|
 | 725 | // compile-command: "make install" //
 | 
|---|
 | 726 | // End: //
 | 
|---|