Changeset 77971f6


Ignore:
Timestamp:
Oct 27, 2016, 3:24:02 PM (5 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
aaron-thesis, arm-eh, cleanup-dtors, deferred_resn, demangler, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, resolv-new, with_gc
Children:
3f0c6a5
Parents:
a1e67dd
git-author:
Rob Schluntz <rschlunt@…> (10/27/16 15:11:00)
git-committer:
Rob Schluntz <rschlunt@…> (10/27/16 15:24:02)
Message:

resolve ctor/dtors for UniqueExprs?

Location:
src
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/AlternativeFinder.cc

    ra1e67dd r77971f6  
    4141#include "Common/utility.h"
    4242#include "InitTweak/InitTweak.h"
     43#include "InitTweak/GenInit.h"
    4344#include "ResolveTypeof.h"
    4445
     
    207208        }
    208209
     210        // std::unordered_map< Expression *, UniqueExpr * > ;
     211
    209212        template< typename StructOrUnionType >
    210213        void AlternativeFinder::addAggMembers( StructOrUnionType *aggInst, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member ) {
    211 
    212214                // by this point, member must be a name expr
    213215                NameExpr * nameExpr = safe_dynamic_cast< NameExpr * >( member );
     
    434436                // flatten actuals so that each actual has an atomic (non-tuple) type
    435437                AltList exploded;
    436                 Tuples::explode( actuals, back_inserter( exploded ) );
     438                Tuples::explode( actuals, indexer, back_inserter( exploded ) );
    437439
    438440                AltList::iterator actualExpr = exploded.begin();
     
    10551057
    10561058        void AlternativeFinder::visit( UniqueExpr *unqExpr ) {
     1059                // this won't work because the unqExprs wont share an expression anymore...
    10571060                AlternativeFinder finder( indexer, env );
    10581061                finder.findWithAdjustment( unqExpr->get_expr() );
    10591062                for ( Alternative & alt : finder.alternatives ) {
    1060                         // xxx - it's possible that this won't always do the right thing, i.e. two UniqueExprs
    1061                         // with the same ID may resolve to different expressions. If this ever happens, it might be necessary
    1062                         // to try to select an alternative here (i.e. error is there is not a unique min-cost expression).
    1063                         // One other thought is to to resolve each ID once and map the IDs to resolved expressions,
    1064                         // but this isn't as simple as it sounds since the alternative is not selected here, meaning it might
    1065                         // require complicated tracking throughout the system.
    1066 
    1067                         // brand the new UniqueExprs with the same id so that they are recognized as the same expression by the expansion pass
    1068                         alternatives.push_back( Alternative( new UniqueExpr( alt.expr->clone(), unqExpr->get_id() ), env, Cost::zero ) );
     1063                        // xxx - attach a resolved ConstructorInit node?
     1064                        // xxx - is it possible to make the objDecl's type const?
     1065                        static UniqueName tempNamer( "_unq_expr_" );
     1066                        ObjectDecl * objDecl = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, nullptr, alt.expr->get_result()->clone(), nullptr );
     1067                        // must be done on two lines because genCtorInit accesses objDecl's fields
     1068                        objDecl->set_init( InitTweak::genCtorInit( objDecl ) );
     1069
     1070                        UniqueExpr * newUnqExpr = new UniqueExpr( alt.expr->clone(), unqExpr->get_id() );
     1071                        newUnqExpr->set_object( objDecl );
     1072
     1073                        resolveObject( indexer, objDecl );
     1074
     1075                        alternatives.push_back( Alternative( newUnqExpr, env, Cost::zero ) );
    10691076                }
    10701077        }
  • src/ResolvExpr/AlternativeFinder.h

    ra1e67dd r77971f6  
    9595        Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer, TypeEnvironment &env );
    9696
     97        void resolveObject( const SymTab::Indexer & indexer, ObjectDecl * objectDecl );
     98
    9799        template< typename InputIterator, typename OutputIterator >
    98100        void findMinCost( InputIterator begin, InputIterator end, OutputIterator out ) {
  • src/ResolvExpr/Resolver.cc

    ra1e67dd r77971f6  
    3535        class Resolver : public SymTab::Indexer {
    3636          public:
    37                 Resolver() : SymTab::Indexer( false ), switchType( 0 ) {}
     37                Resolver() : SymTab::Indexer( false ) {}
     38                Resolver( const SymTab::Indexer & indexer ) : SymTab::Indexer( indexer ) {}
    3839
    3940                virtual void visit( FunctionDecl *functionDecl );
     
    6869          void resolveSingleAggrInit( Declaration *, InitIterator &, InitIterator & );
    6970          void fallbackInit( ConstructorInit * ctorInit );
    70                 Type * functionReturn;
    71                 Type *initContext;
    72                 Type *switchType;
     71                Type * functionReturn = nullptr;
     72                Type *initContext = nullptr;
     73                Type *switchType = nullptr;
    7374                bool inEnumDecl = false;
    7475        };
     
    532533        }
    533534
     535        void resolveObject( const SymTab::Indexer & indexer, ObjectDecl * objectDecl ) {
     536                Resolver resolver( indexer );
     537                objectDecl->accept( resolver );
     538        }
     539
    534540        void Resolver::visit( ConstructorInit *ctorInit ) {
    535541                // xxx - fallback init has been removed => remove fallbackInit function and remove complexity from FixInit and remove C-init from ConstructorInit
  • src/SynTree/Expression.cc

    ra1e67dd r77971f6  
    581581
    582582long long UniqueExpr::count = 0;
    583 UniqueExpr::UniqueExpr( Expression *expr, long long idVal ) : expr( new Expression* ), id( idVal ) {
     583UniqueExpr::UniqueExpr( Expression *expr, long long idVal ) : expr( new Expression* ), object( new ObjectDecl* ), id( idVal ) {
    584584        assert( count != -1 );
    585585        if ( id == -1 ) id = count++;
     
    589589                set_result( expr->get_result()->clone() );
    590590        }
    591 }
    592 UniqueExpr::UniqueExpr( const UniqueExpr &other ) : Expression( other ), expr( other.expr ), id( other.id ) {
     591        set_object( nullptr );
     592}
     593UniqueExpr::UniqueExpr( const UniqueExpr &other ) : Expression( other ), expr( other.expr ), object( other.object ), id( other.id ) {
    593594}
    594595UniqueExpr::~UniqueExpr() {
     
    596597                delete *expr;
    597598        }
     599        if ( object.unique() ) {
     600                delete *object;
     601        }
    598602}
    599603void UniqueExpr::print( std::ostream &os, int indent ) const {
    600604        os << "Unique Expression with id:" << id << std::endl << std::string( indent+2, ' ' );
    601605        get_expr()->print( os, indent+2 );
     606        if ( get_object() ) {
     607                os << " with decl: ";
     608                get_object()->printShort( os, indent+2 );
     609        }
    602610}
    603611
  • src/SynTree/Expression.h

    ra1e67dd r77971f6  
    742742        UniqueExpr * set_expr( Expression * newValue ) { *expr = newValue; return this; }
    743743
     744        ObjectDecl * get_object() const { return *object; }
     745        UniqueExpr * set_object( ObjectDecl * newValue ) { *object = newValue; return this; }
     746
    744747        int get_id() const { return id; }
    745748
     
    750753private:
    751754        std::shared_ptr< Expression * > expr;
     755        std::shared_ptr< ObjectDecl * > object;
    752756        int id;
    753757        static long long count;
  • src/Tuples/TupleAssignment.cc

    ra1e67dd r77971f6  
    179179
    180180                // explode the lhs so that each field of the tuple-valued-expr is assigned.
    181                 explode( lhsAlt, back_inserter(lhs) );
     181                explode( lhsAlt, spotter.currentFinder.get_indexer(), back_inserter(lhs) );
    182182                // and finally, re-add the cast to each lhs expr, so that qualified tuple fields can be constructed
    183183                if ( isCast ) {
     
    204204        TupleAssignSpotter::MultipleAssignMatcher::MultipleAssignMatcher( TupleAssignSpotter &spotter, const ResolvExpr::AltList & alts ) : Matcher( spotter, alts ) {
    205205                // explode the rhs so that each field of the tuple-valued-expr is assigned.
    206                 explode( std::next(alts.begin(), 1), alts.end(), back_inserter(rhs) );
     206                explode( std::next(alts.begin(), 1), alts.end(), spotter.currentFinder.get_indexer(), back_inserter(rhs) );
    207207        }
    208208
  • src/Tuples/TupleExpansion.cc

    ra1e67dd r77971f6  
    141141
    142142        Expression * UniqueExprExpander::mutate( UniqueExpr * unqExpr ) {
    143                 static UniqueName tempNamer( "_unq_expr_" );
    144143                unqExpr = safe_dynamic_cast< UniqueExpr * > ( Parent::mutate( unqExpr ) );
    145144                if ( ! decls.count( unqExpr->get_id() ) ) {
     
    160159                        // }
    161160
    162                         // xxx - attach a resolved ConstructorInit node?
    163                         // xxx - is it possible to make the objDecl's type const?
    164                         ObjectDecl * objDecl = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, nullptr, unqExpr->get_result()->clone(), nullptr );
    165                         // must be done on two lines because genCtorInit accesses objDecl's fields
    166                         objDecl->set_init( InitTweak::genCtorInit( objDecl ) );
    167 
     161                        ObjectDecl * objDecl = unqExpr->get_object();
     162                        unqExpr->set_object( nullptr );
    168163                        decls[unqExpr->get_id()] = objDecl;
    169164                        addDeclaration( objDecl );
  • src/Tuples/Tuples.h

    ra1e67dd r77971f6  
    2020#include <vector>
    2121#include "ResolvExpr/AlternativeFinder.h"
     22#include "ResolvExpr/Resolver.h"
    2223
    2324#include "SynTree/Expression.h"
     
    4849        /// helper function used by explode
    4950        template< typename OutputIterator >
    50         void explodeUnique( Expression * expr, const ResolvExpr::Alternative & alt, OutputIterator out ) {
     51        void explodeUnique( Expression * expr, const ResolvExpr::Alternative & alt, const SymTab::Indexer & indexer, OutputIterator out ) {
    5152                Type * res = expr->get_result();
    5253                if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( expr ) ) {
    5354                        ResolvExpr::AltList alts;
    54                         explodeUnique( addrExpr->get_arg(), alt, back_inserter( alts ) );
     55                        explodeUnique( addrExpr->get_arg(), alt, indexer, back_inserter( alts ) );
    5556                        for ( ResolvExpr::Alternative & alt : alts ) {
    5657                                // distribute '&' over all components
     
    6263                                // can open tuple expr and dump its exploded components
    6364                                for ( Expression * expr : tupleExpr->get_exprs() ) {
    64                                         explodeUnique( expr, alt, out );
     65                                        explodeUnique( expr, alt, indexer, out );
    6566                                }
    6667                        } else {
     
    6869                                Expression * arg = expr->clone();
    6970                                if ( Tuples::maybeImpure( arg ) ) {
    70                                         // expressions which may contain side effects require a single unique instance of the expression
     71                                        // expressions which may contain side effects require a single unique instance of the expression.
     72                                        // resolve the UniqueExpr (which should be relatively cheap, since the argument is already resolved)
     73                                        // so that its accompanying object is properly constructed and destructed.
    7174                                        arg = new UniqueExpr( arg );
     75                                        arg = ResolvExpr::resolveInVoidContext( arg, indexer );
    7276                                }
    7377                                for ( unsigned int i = 0; i < tupleType->size(); i++ ) {
    7478                                        TupleIndexExpr * idx = new TupleIndexExpr( arg->clone(), i );
    75                                         explodeUnique( idx, alt, out );
     79                                        explodeUnique( idx, alt, indexer, out );
    7680                                        delete idx;
    7781                                }
     
    8690        /// expands a tuple-valued alternative into multiple alternatives, each with a non-tuple-type
    8791        template< typename OutputIterator >
    88         void explode( const ResolvExpr::Alternative &alt, OutputIterator out ) {
    89                 explodeUnique( alt.expr, alt, out );
     92        void explode( const ResolvExpr::Alternative &alt, const SymTab::Indexer & indexer, OutputIterator out ) {
     93                explodeUnique( alt.expr, alt, indexer, out );
    9094        }
    9195
    9296        // explode list of alternatives
    9397        template< typename AltIterator, typename OutputIterator >
    94         void explode( AltIterator altBegin, AltIterator altEnd, OutputIterator out ) {
     98        void explode( AltIterator altBegin, AltIterator altEnd, const SymTab::Indexer & indexer, OutputIterator out ) {
    9599                for ( ; altBegin != altEnd; ++altBegin ) {
    96                         explode( *altBegin, out );
     100                        explode( *altBegin, indexer, out );
    97101                }
    98102        }
    99103
    100104        template< typename OutputIterator >
    101         void explode( const ResolvExpr::AltList & alts, OutputIterator out ) {
    102                 explode( alts.begin(), alts.end(), out );
     105        void explode( const ResolvExpr::AltList & alts, const SymTab::Indexer & indexer, OutputIterator out ) {
     106                explode( alts.begin(), alts.end(), indexer, out );
    103107        }
    104108} // namespace Tuples
Note: See TracChangeset for help on using the changeset viewer.