Changeset 1d2b64f


Ignore:
Timestamp:
Dec 13, 2016, 5:20:54 PM (7 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
d5556a3
Parents:
722617d
git-author:
Rob Schluntz <rschlunt@…> (12/13/16 17:16:27)
git-committer:
Rob Schluntz <rschlunt@…> (12/13/16 17:20:54)
Message:

combine environments and costs in tuple assignment, resolve ctor/dtors for tuple assignment temporaries

Location:
src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/Resolver.cc

    r722617d r1d2b64f  
    3636          public:
    3737                Resolver() : SymTab::Indexer( false ) {}
    38 
    39                 using SymTab::Indexer::visit;
     38                Resolver( const SymTab:: Indexer & other ) : SymTab::Indexer( other ) {
     39                        if ( const Resolver * res = dynamic_cast< const Resolver * >( &other ) ) {
     40                                functionReturn = res->functionReturn;
     41                                initContext = res->initContext;
     42                                inEnumDecl = res->inEnumDecl;
     43                        }
     44                }
     45
     46                typedef SymTab::Indexer Parent;
     47                using Parent::visit;
    4048                virtual void visit( FunctionDecl *functionDecl ) override;
    4149                virtual void visit( ObjectDecl *functionDecl ) override;
     
    192200                        initContext = new BasicType( Type::Qualifiers(), BasicType::SignedInt );
    193201                }
    194                 SymTab::Indexer::visit( objectDecl );
     202                Parent::visit( objectDecl );
    195203                if ( inEnumDecl && dynamic_cast< EnumInstType * >( initContext ) ) {
    196204                        // delete newly created signed int type
     
    212220        void Resolver::visit( ArrayType * at ) {
    213221                handlePtrType( at );
    214                 Visitor::visit( at );
     222                Parent::visit( at );
    215223        }
    216224
    217225        void Resolver::visit( PointerType * pt ) {
    218226                handlePtrType( pt );
    219                 Visitor::visit( pt );
     227                Parent::visit( pt );
    220228        }
    221229
     
    225233                        typeDecl->set_base( new_type );
    226234                } // if
    227                 SymTab::Indexer::visit( typeDecl );
     235                Parent::visit( typeDecl );
    228236        }
    229237
     
    238246                ValueGuard< Type * > oldFunctionReturn( functionReturn );
    239247                functionReturn = ResolvExpr::extractResultType( functionDecl->get_functionType() );
    240                 SymTab::Indexer::visit( functionDecl );
     248                Parent::visit( functionDecl );
    241249        }
    242250
    243251        void Resolver::visit( EnumDecl * enumDecl ) {
    244252                // in case we decide to allow nested enums
    245                 bool oldInEnumDecl = inEnumDecl;
     253                ValueGuard< bool > oldInEnumDecl( inEnumDecl );
    246254                inEnumDecl = true;
    247                 SymTab::Indexer::visit( enumDecl );
    248                 inEnumDecl = oldInEnumDecl;
     255                Parent::visit( enumDecl );
    249256        }
    250257
    251258        void Resolver::visit( ExprStmt *exprStmt ) {
    252                 if ( exprStmt->get_expr() ) {
    253                         Expression *newExpr = findVoidExpression( exprStmt->get_expr(), *this );
    254                         delete exprStmt->get_expr();
    255                         exprStmt->set_expr( newExpr );
    256                 } // if
     259                assertf( exprStmt->get_expr(), "ExprStmt has null Expression in resolver" );
     260                Expression *newExpr = findVoidExpression( exprStmt->get_expr(), *this );
     261                delete exprStmt->get_expr();
     262                exprStmt->set_expr( newExpr );
    257263        }
    258264
     
    277283                delete ifStmt->get_condition();
    278284                ifStmt->set_condition( newExpr );
    279                 Visitor::visit( ifStmt );
     285                Parent::visit( ifStmt );
    280286        }
    281287
     
    284290                delete whileStmt->get_condition();
    285291                whileStmt->set_condition( newExpr );
    286                 Visitor::visit( whileStmt );
     292                Parent::visit( whileStmt );
    287293        }
    288294
    289295        void Resolver::visit( ForStmt *forStmt ) {
    290                 SymTab::Indexer::visit( forStmt );
     296                Parent::visit( forStmt );
    291297
    292298                if ( forStmt->get_condition() ) {
     
    318324
    319325        void Resolver::visit( CaseStmt *caseStmt ) {
    320                 Visitor::visit( caseStmt );
     326                Parent::visit( caseStmt );
    321327        }
    322328
     
    482488                        } else {
    483489                                // missing implementation type -- might be an unknown type variable, so try proceeding with the current init context
    484                                 Visitor::visit( listInit );
     490                                Parent::visit( listInit );
    485491                        }
    486492                } else {
    487493                        assert( dynamic_cast< BasicType * >( initContext ) || dynamic_cast< PointerType * >( initContext )
    488                                 || dynamic_cast< ZeroType * >( initContext ) || dynamic_cast< OneType * >( initContext ) );
     494                                || dynamic_cast< ZeroType * >( initContext ) || dynamic_cast< OneType * >( initContext ) || dynamic_cast < EnumInstType * > ( initContext ) );
    489495                        // basic types are handled here
    490                         Visitor::visit( listInit );
     496                        Parent::visit( listInit );
    491497                }
    492498
     
    554560        }
    555561
     562        // needs to be callable from outside the resolver, so this is a standalone function
     563        void resolveCtorInit( ConstructorInit * ctorInit, const SymTab::Indexer & indexer ) {
     564                assert( ctorInit );
     565                Resolver resolver( indexer );
     566                ctorInit->accept( resolver );
     567        }
     568
     569        void resolveStmtExpr( StmtExpr * stmtExpr, const SymTab::Indexer & indexer ) {
     570                assert( stmtExpr );
     571                Resolver resolver( indexer );
     572                stmtExpr->accept( resolver );
     573        }
     574
    556575        void Resolver::visit( ConstructorInit *ctorInit ) {
    557576                // xxx - fallback init has been removed => remove fallbackInit function and remove complexity from FixInit and remove C-init from ConstructorInit
  • src/ResolvExpr/Resolver.h

    r722617d r1d2b64f  
    2525        Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer );
    2626        Expression *findVoidExpression( Expression *untyped, const SymTab::Indexer &indexer );
     27        void resolveCtorInit( ConstructorInit * ctorInit, const SymTab::Indexer & indexer );
     28        void resolveStmtExpr( StmtExpr * stmtExpr, const SymTab::Indexer & indexer );
    2729} // namespace ResolvExpr
    2830
  • src/Tuples/TupleAssignment.cc

    r722617d r1d2b64f  
    2323#include "Common/SemanticError.h"
    2424#include "InitTweak/InitTweak.h"
     25#include "InitTweak/GenInit.h"
    2526
    2627#include <functional>
     
    4748                        virtual ~Matcher() {}
    4849                        virtual void match( std::list< Expression * > &out ) = 0;
     50                        ObjectDecl * newObject( UniqueName & namer, Expression * expr );
    4951                        ResolvExpr::AltList lhs, rhs;
    5052                        TupleAssignSpotter &spotter;
     53                        ResolvExpr::Cost baseCost;
    5154                        std::list< ObjectDecl * > tmpDecls;
     55                        ResolvExpr::TypeEnvironment compositeEnv;
    5256                };
    5357
     
    146150                                finder.findWithAdjustment(*i);
    147151                        } catch (...) {
    148                                 return; // xxx - no match should not mean failure, it just means this particular tuple assignment isn't valid
     152                                return; // no match should not mean failure, it just means this particular tuple assignment isn't valid
    149153                        }
    150154                        // prune expressions that don't coincide with
     
    161165                        solved_assigns.push_back( alt.expr->clone() );
    162166                }
    163                 // xxx - need to do this??
    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 ) ) );
    167         }
    168 
    169         TupleAssignSpotter::Matcher::Matcher( TupleAssignSpotter &spotter, const ResolvExpr::AltList &alts ) : spotter(spotter) {
     167                // combine assignment environments into combined expression environment
     168                simpleCombineEnvironments( current.begin(), current.end(), matcher->compositeEnv );
     169                currentFinder.get_alternatives().push_front( ResolvExpr::Alternative(new TupleAssignExpr(solved_assigns, matcher->tmpDecls), matcher->compositeEnv, ResolvExpr::sumCost( current  ) + matcher->baseCost ) );
     170        }
     171
     172        TupleAssignSpotter::Matcher::Matcher( TupleAssignSpotter &spotter, const ResolvExpr::AltList &alts ) : spotter(spotter), baseCost( ResolvExpr::sumCost( alts ) ) {
    170173                assert( ! alts.empty() );
     174                // combine argument environments into combined expression environment
     175                simpleCombineEnvironments( alts.begin(), alts.end(), compositeEnv );
     176
    171177                ResolvExpr::Alternative lhsAlt = alts.front();
    172178                // peel off the cast that exists on ctor/dtor expressions
     
    217223        }
    218224
    219         ObjectDecl * newObject( UniqueName & namer, Expression * expr ) {
     225        // removes environments from subexpressions within statement exprs, which could throw off later passes like those in Box which rely on PolyMutator.
     226        // xxx - maybe this should happen in alternative finder for every StmtExpr?
     227        // xxx - it's possible that these environments could contain some useful information. Maybe the right thing to do is aggregate the environments and pass the aggregate back to be added into the compositeEnv
     228        struct EnvRemover : public Visitor {
     229                virtual void visit( ExprStmt * stmt ) {
     230                        delete stmt->get_expr()->get_env();
     231                        stmt->get_expr()->set_env( nullptr );
     232                        Visitor::visit( stmt );
     233                }
     234        };
     235
     236        ObjectDecl * TupleAssignSpotter::Matcher::newObject( UniqueName & namer, Expression * expr ) {
    220237                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() ) );
     238                ObjectDecl * ret = new ObjectDecl( namer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, nullptr, expr->get_result()->clone(), new SingleInit( expr->clone() ) );
     239                ConstructorInit * ctorInit = InitTweak::genCtorInit( ret );
     240                ret->set_init( ctorInit );
     241                ResolvExpr::resolveCtorInit( ctorInit, spotter.currentFinder.get_indexer() ); // resolve ctor/dtors for the new object
     242                EnvRemover rm; // remove environments from subexpressions of StmtExprs
     243                ctorInit->accept( rm );
     244                return ret;
    222245        }
    223246
     
    244267                        std::list< ObjectDecl * > ltmp;
    245268                        std::list< ObjectDecl * > rtmp;
    246                         std::transform( lhs.begin(), lhs.end(), back_inserter( ltmp ), []( ResolvExpr::Alternative & alt ){
     269                        std::transform( lhs.begin(), lhs.end(), back_inserter( ltmp ), [&]( ResolvExpr::Alternative & alt ){
    247270                                return newObject( lhsNamer, alt.expr );
    248271                        });
    249                         std::transform( rhs.begin(), rhs.end(), back_inserter( rtmp ), []( ResolvExpr::Alternative & alt ){
     272                        std::transform( rhs.begin(), rhs.end(), back_inserter( rtmp ), [&]( ResolvExpr::Alternative & alt ){
    250273                                return newObject( rhsNamer, alt.expr );
    251274                        });
Note: See TracChangeset for help on using the changeset viewer.