source: src/Tuples/TupleAssignment.cc @ f7e749f

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since f7e749f was 141b786, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

rework UniqueExpr?, handle UniqueExpr? in FixInit?, fix translation for UniqueExprs?, refactor explode functions and fix AddressExpr? distribution over exploded tuple exprs

  • Property mode set to 100644
File size: 9.9 KB
RevLine 
[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
[b3b2077]11// Last Modified By : Rob Schluntz
12// Last Modified On : Wed Nov 9 13:48:42 2016
[51587aa]13// Update Count     : 2
14//
15
[51b7345]16#include "ResolvExpr/AlternativeFinder.h"
17#include "ResolvExpr/Alternative.h"
18#include "ResolvExpr/typeops.h"
19#include "SynTree/Expression.h"
[6eb8948]20#include "SynTree/Initializer.h"
21#include "Tuples.h"
[141b786]22#include "Explode.h"
[d3b7937]23#include "Common/SemanticError.h"
[65660bd]24#include "InitTweak/InitTweak.h"
[51b7345]25
26#include <functional>
27#include <algorithm>
28#include <iterator>
29#include <iostream>
30#include <cassert>
31#include <set>
[5af62f1]32#include <unordered_set>
[51b7345]33
34namespace Tuples {
[5af62f1]35        class TupleAssignSpotter {
36          public:
37                // dispatcher for Tuple (multiple and mass) assignment operations
38                TupleAssignSpotter( ResolvExpr::AlternativeFinder & );
[65660bd]39                void spot( UntypedExpr * expr, const std::list<ResolvExpr::AltList> &possibilities );
[5af62f1]40
41          private:
42                void match();
43
[6eb8948]44                struct Matcher {
[5af62f1]45                  public:
[65660bd]46                        Matcher( TupleAssignSpotter &spotter, const ResolvExpr::AltList & alts );
[5af62f1]47                        virtual ~Matcher() {}
48                        virtual void match( std::list< Expression * > &out ) = 0;
[3c13c03]49                        ResolvExpr::AltList lhs, rhs;
[5af62f1]50                        TupleAssignSpotter &spotter;
[6eb8948]51                        std::list< ObjectDecl * > tmpDecls;
[5af62f1]52                };
53
[6eb8948]54                struct MassAssignMatcher : public Matcher {
[5af62f1]55                  public:
[65660bd]56                        MassAssignMatcher( TupleAssignSpotter &spotter, const ResolvExpr::AltList & alts );
[5af62f1]57                        virtual void match( std::list< Expression * > &out );
58                };
59
[6eb8948]60                struct MultipleAssignMatcher : public Matcher {
[5af62f1]61                  public:
[65660bd]62                        MultipleAssignMatcher( TupleAssignSpotter &spot, const ResolvExpr::AltList & alts );
[5af62f1]63                        virtual void match( std::list< Expression * > &out );
64                };
65
66                ResolvExpr::AlternativeFinder &currentFinder;
[65660bd]67                std::string fname;
68                std::unique_ptr< Matcher > matcher;
[5af62f1]69        };
70
71        /// true if expr is an expression of tuple type, i.e. a tuple expression, tuple variable, or MRV (multiple-return-value) function
72        bool isTuple( Expression *expr ) {
[51587aa]73                if ( ! expr ) return false;
[aa8f9df]74                assert( expr->has_result() );
75                return dynamic_cast<TupleExpr *>(expr) || expr->get_result()->size() > 1;
[51587aa]76        }
77
[65660bd]78        template< typename AltIter >
79        bool isMultAssign( AltIter begin, AltIter end ) {
80                // multiple assignment if more than one alternative in the range or if
81                // the alternative is a tuple
82                if ( begin == end ) return false;
83                if ( isTuple( begin->expr ) ) return true;
84                return ++begin != end;
85        }
86
[5af62f1]87        bool pointsToTuple( Expression *expr ) {
88                // also check for function returning tuple of reference types
[65660bd]89                if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) {
90                        return pointsToTuple( castExpr->get_arg() );
91                } else if ( AddressExpr *addr = dynamic_cast< AddressExpr * >( expr) ) {
[5af62f1]92                        return isTuple( addr->get_arg() );
[51587aa]93                }
94                return false;
95        }
96
[65660bd]97        void handleTupleAssignment( ResolvExpr::AlternativeFinder & currentFinder, UntypedExpr * expr, const std::list<ResolvExpr::AltList> &possibilities ) {
[5af62f1]98                TupleAssignSpotter spotter( currentFinder );
99                spotter.spot( expr, possibilities );
100        }
[51587aa]101
[5af62f1]102        TupleAssignSpotter::TupleAssignSpotter( ResolvExpr::AlternativeFinder &f )
103                : currentFinder(f) {}
[51587aa]104
[65660bd]105        void TupleAssignSpotter::spot( UntypedExpr * expr, const std::list<ResolvExpr::AltList> &possibilities ) {
106                if (  NameExpr *op = dynamic_cast< NameExpr * >(expr->get_function()) ) {
107                        if ( InitTweak::isCtorDtorAssign( op->get_name() ) ) {
108                                fname = op->get_name();
109                                for ( std::list<ResolvExpr::AltList>::const_iterator ali = possibilities.begin(); ali != possibilities.end(); ++ali ) {
110                                        if ( ali->size() == 0 ) continue; // AlternativeFinder will natrually handle this case, if it's legal
111                                        if ( ali->size() <= 1 && InitTweak::isAssignment( op->get_name() ) ) {
112                                                // what does it mean if an assignment takes 1 argument? maybe someone defined such a function, in which case AlternativeFinder will naturally handle it
113                                                continue;
114                                        }
[51587aa]115
[65660bd]116                                        assert( ! ali->empty() );
117                                        // grab args 2-N and group into a TupleExpr
118                                        const ResolvExpr::Alternative & alt1 = ali->front();
119                                        auto begin = std::next(ali->begin(), 1), end = ali->end();
[3c13c03]120                                        if ( pointsToTuple(alt1.expr) ) {
[65660bd]121                                                if ( isMultAssign( begin, end ) ) {
122                                                        matcher.reset( new MultipleAssignMatcher( *this, *ali ) );
[5af62f1]123                                                } else {
[51587aa]124                                                        // mass assignment
[65660bd]125                                                        matcher.reset( new MassAssignMatcher( *this,  *ali ) );
[51587aa]126                                                }
[5af62f1]127                                                match();
[51587aa]128                                        }
129                                }
130                        }
131                }
132        }
133
[5af62f1]134        void TupleAssignSpotter::match() {
135                assert ( matcher != 0 );
[51587aa]136
[5af62f1]137                std::list< Expression * > new_assigns;
138                matcher->match( new_assigns );
139
140                if ( new_assigns.empty() ) return;
141                ResolvExpr::AltList current;
142                // now resolve new assignments
143                for ( std::list< Expression * >::iterator i = new_assigns.begin(); i != new_assigns.end(); ++i ) {
144                        ResolvExpr::AlternativeFinder finder( currentFinder.get_indexer(), currentFinder.get_environ() );
[ac9ca96]145                        try {
146                                finder.findWithAdjustment(*i);
147                        } catch (...) {
148                                return; // xxx - no match should not mean failure, it just means this particular tuple assignment isn't valid
149                        }
[5af62f1]150                        // prune expressions that don't coincide with
151                        ResolvExpr::AltList alts = finder.get_alternatives();
152                        assert( alts.size() == 1 );
153                        assert( alts.front().expr != 0 );
[908cc83]154                        current.push_back( alts.front() );
[5af62f1]155                }
156
[6eb8948]157                // extract expressions from the assignment alternatives to produce a list of assignments that
158                // together form a single alternative
159                std::list< Expression *> solved_assigns;
160                for ( ResolvExpr::Alternative & alt : current ) {
161                        solved_assigns.push_back( alt.expr->clone() );
162                }
163                // xxx - need to do this??
[ac9ca96]164                ResolvExpr::TypeEnvironment compositeEnv;
165                simpleCombineEnvironments( current.begin(), current.end(), compositeEnv );
166                currentFinder.get_alternatives().push_front( ResolvExpr::Alternative(new TupleAssignExpr(solved_assigns, matcher->tmpDecls), compositeEnv, ResolvExpr::sumCost( current ) ) );
[51587aa]167        }
168
[65660bd]169        TupleAssignSpotter::Matcher::Matcher( TupleAssignSpotter &spotter, const ResolvExpr::AltList &alts ) : spotter(spotter) {
170                assert( ! alts.empty() );
171                ResolvExpr::Alternative lhsAlt = alts.front();
172                // peel off the cast that exists on ctor/dtor expressions
173                bool isCast = false;
174                if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( lhsAlt.expr ) ) {
175                        lhsAlt.expr = castExpr->get_arg();
176                        castExpr->set_arg( nullptr );
177                        delete castExpr;
178                        isCast = true;
179                }
[3c13c03]180
[65660bd]181                // explode the lhs so that each field of the tuple-valued-expr is assigned.
[77971f6]182                explode( lhsAlt, spotter.currentFinder.get_indexer(), back_inserter(lhs) );
[b3b2077]183
[65660bd]184                // and finally, re-add the cast to each lhs expr, so that qualified tuple fields can be constructed
185                if ( isCast ) {
186                        for ( ResolvExpr::Alternative & alt : lhs ) {
187                                Expression *& expr = alt.expr;
188                                Type * castType = expr->get_result()->clone();
189                                Type * type = InitTweak::getPointerBase( castType );
190                                assert( type );
191                                type->get_qualifiers() -= Type::Qualifiers(true, true, true, false, true, true);
192                                type->set_isLvalue( true ); // xxx - might not need this
193                                expr = new CastExpr( expr, castType );
194                        }
[3c13c03]195                }
196        }
197
[b3b2077]198        TupleAssignSpotter::MassAssignMatcher::MassAssignMatcher( TupleAssignSpotter &spotter, const ResolvExpr::AltList & alts ) : Matcher( spotter, alts ) {
[65660bd]199                assert( alts.size() == 1 || alts.size() == 2 );
200                if ( alts.size() == 2 ) {
201                        rhs.push_back( alts.back() );
202                }
[51587aa]203        }
204
[65660bd]205        TupleAssignSpotter::MultipleAssignMatcher::MultipleAssignMatcher( TupleAssignSpotter &spotter, const ResolvExpr::AltList & alts ) : Matcher( spotter, alts ) {
[3c13c03]206                // explode the rhs so that each field of the tuple-valued-expr is assigned.
[77971f6]207                explode( std::next(alts.begin(), 1), alts.end(), spotter.currentFinder.get_indexer(), back_inserter(rhs) );
[51587aa]208        }
209
[65660bd]210        UntypedExpr * createFunc( const std::string &fname, ObjectDecl *left, ObjectDecl *right ) {
211                assert( left );
[5af62f1]212                std::list< Expression * > args;
[b3b2077]213                args.push_back( new AddressExpr( UntypedExpr::createDeref( new VariableExpr( left ) ) ) );
[65660bd]214                // args.push_back( new AddressExpr( new VariableExpr( left ) ) );
215                if ( right ) args.push_back( new VariableExpr( right ) );
216                return new UntypedExpr( new NameExpr( fname ), args );
[51587aa]217        }
218
[6eb8948]219        ObjectDecl * newObject( UniqueName & namer, Expression * expr ) {
[aa8f9df]220                assert( expr->has_result() && ! expr->get_result()->isVoid() );
221                return new ObjectDecl( namer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, nullptr, expr->get_result()->clone(), new SingleInit( expr->clone() ) );
[6eb8948]222        }
223
[5af62f1]224        void TupleAssignSpotter::MassAssignMatcher::match( std::list< Expression * > &out ) {
[6eb8948]225                static UniqueName lhsNamer( "__massassign_L" );
226                static UniqueName rhsNamer( "__massassign_R" );
[65660bd]227                assert ( ! lhs.empty() && rhs.size() <= 1);
[51587aa]228
[65660bd]229                ObjectDecl * rtmp = rhs.size() == 1 ? newObject( rhsNamer, rhs.front().expr ) : nullptr;
[3c13c03]230                for ( ResolvExpr::Alternative & lhsAlt : lhs ) {
[65660bd]231                        ObjectDecl * ltmp = newObject( lhsNamer, lhsAlt.expr );
232                        out.push_back( createFunc( spotter.fname, ltmp, rtmp ) );
[6eb8948]233                        tmpDecls.push_back( ltmp );
[5af62f1]234                }
[65660bd]235                if ( rtmp ) tmpDecls.push_back( rtmp );
[51b7345]236        }
237
[5af62f1]238        void TupleAssignSpotter::MultipleAssignMatcher::match( std::list< Expression * > &out ) {
[6eb8948]239                static UniqueName lhsNamer( "__multassign_L" );
240                static UniqueName rhsNamer( "__multassign_R" );
[b3b2077]241
[5af62f1]242                // xxx - need more complicated matching?
[51587aa]243                if ( lhs.size() == rhs.size() ) {
[6eb8948]244                        std::list< ObjectDecl * > ltmp;
245                        std::list< ObjectDecl * > rtmp;
[3c13c03]246                        std::transform( lhs.begin(), lhs.end(), back_inserter( ltmp ), []( ResolvExpr::Alternative & alt ){
[65660bd]247                                return newObject( lhsNamer, alt.expr );
[6eb8948]248                        });
[3c13c03]249                        std::transform( rhs.begin(), rhs.end(), back_inserter( rtmp ), []( ResolvExpr::Alternative & alt ){
250                                return newObject( rhsNamer, alt.expr );
[6eb8948]251                        });
[65660bd]252                        zipWith( ltmp.begin(), ltmp.end(), rtmp.begin(), rtmp.end(), back_inserter(out), [&](ObjectDecl * obj1, ObjectDecl * obj2 ) { return createFunc(spotter.fname, obj1, obj2); } );
[6eb8948]253                        tmpDecls.splice( tmpDecls.end(), ltmp );
254                        tmpDecls.splice( tmpDecls.end(), rtmp );
[5af62f1]255                }
[51587aa]256        }
[51b7345]257} // namespace Tuples
[51587aa]258
259// Local Variables: //
260// tab-width: 4 //
261// mode: c++ //
262// compile-command: "make install" //
263// End: //
Note: See TracBrowser for help on using the repository browser.