Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/Resolver.cc

    rb60f9d9 rd286cf68  
    2222#include "Alternative.h"                 // for Alternative, AltList
    2323#include "AlternativeFinder.h"           // for AlternativeFinder, resolveIn...
    24 #include "Common/GC.h"                   // for new_generation, collect_young
    2524#include "Common/PassVisitor.h"          // for PassVisitor
    2625#include "Common/SemanticError.h"        // for SemanticError
     
    3332#include "ResolveTypeof.h"               // for resolveTypeof
    3433#include "Resolver.h"
    35 #include "ResolvMode.h"                  // for ResolvMode
    3634#include "SymTab/Autogen.h"              // for SizeType
    3735#include "SymTab/Indexer.h"              // for Indexer
     
    5149namespace ResolvExpr {
    5250        struct Resolver final : public WithIndexer, public WithGuards, public WithVisitorRef<Resolver>, public WithShortCircuiting, public WithStmtsToAdd {
    53        
    54         friend void resolve( std::list<Declaration*> );
    55 
    5651                Resolver() {}
    5752                Resolver( const SymTab::Indexer & other ) {
     
    9994                CurrentObject currentObject = nullptr;
    10095                bool inEnumDecl = false;
    101                 bool atTopLevel = false;  ///< Was this resolver set up at the top level of resolution
    10296        };
    10397
    10498        void resolve( std::list< Declaration * > translationUnit ) {
    10599                PassVisitor<Resolver> resolver;
    106                 resolver.pass.atTopLevel = true;  // mark resolver as top-level
    107100                acceptAll( translationUnit, resolver );
    108101        }
     
    165158                                        castExpr->arg = nullptr;
    166159                                        std::swap( expr->env, castExpr->env );
     160                                        delete castExpr;
    167161                                }
    168162                        }
     
    171165
    172166        namespace {
    173                 void findUnfinishedKindExpression( Expression * untyped, Alternative & alt, const SymTab::Indexer & indexer, const std::string & kindStr, std::function<bool(const Alternative &)> pred, ResolvMode mode = ResolvMode{} ) {
     167                void findUnfinishedKindExpression(Expression * untyped, Alternative & alt, const SymTab::Indexer & indexer, const std::string & kindStr, std::function<bool(const Alternative &)> pred, bool adjust = false, bool prune = true, bool failFast = true) {
    174168                        assertf( untyped, "expected a non-null expression." );
    175 
    176                         auto guard = new_generation();  // set up GC generation for this top-level expression
    177 
    178169                        TypeEnvironment env;
    179170                        AlternativeFinder finder( indexer, env );
    180                         finder.find( untyped, mode );
     171                        finder.find( untyped, adjust, prune, failFast );
    181172
    182173                        #if 0
     
    216207                        Alternative & choice = winners.front();
    217208                        if ( findDeletedExpr( choice.expr ) ) {
    218                                 trace( choice.expr );
    219                                 SemanticError( choice.expr, "Unique best alternative includes deleted identifier in " );
     209                                SemanticError( untyped->location, choice.expr, "Unique best alternative includes deleted identifier in " );
    220210                        }
    221211                        alt = std::move( choice );
    222                         trace( alt );
    223212                }
    224213
    225214                /// resolve `untyped` to the expression whose alternative satisfies `pred` with the lowest cost; kindStr is used for providing better error messages
    226                 void findKindExpression(Expression *& untyped, const SymTab::Indexer & indexer, const std::string & kindStr, std::function<bool(const Alternative &)> pred, ResolvMode mode = ResolvMode{}) {
     215                void findKindExpression(Expression *& untyped, const SymTab::Indexer & indexer, const std::string & kindStr, std::function<bool(const Alternative &)> pred, bool adjust = false, bool prune = true, bool failFast = true) {
    227216                        if ( ! untyped ) return;
    228217                        Alternative choice;
    229                         findUnfinishedKindExpression( untyped, choice, indexer, kindStr, pred, mode );
     218                        findUnfinishedKindExpression( untyped, choice, indexer, kindStr, pred, adjust, prune, failFast );
    230219                        finishExpr( choice.expr, choice.env, untyped->env );
     220                        delete untyped;
    231221                        untyped = choice.expr;
    232222                        choice.expr = nullptr;
     
    251241                assertf( expr, "expected a non-null expression." );
    252242
    253                 auto untyped = new CastExpr{ expr }; // cast to void
     243                static CastExpr untyped( nullptr ); // cast to void
     244                untyped.location = expr->location;
    254245
    255246                // set up and resolve expression cast to void
     247                untyped.arg = expr;
    256248                Alternative choice;
    257                 findUnfinishedKindExpression( untyped, choice, indexer, "", standardAlternativeFilter, ResolvMode::withAdjustment() );
     249                findUnfinishedKindExpression( &untyped, choice, indexer, "", standardAlternativeFilter, true );
    258250                CastExpr * castExpr = strict_dynamic_cast< CastExpr * >( choice.expr );
    259251                env = std::move( choice.env );
    260252
    261253                // clean up resolved expression
    262                 return castExpr->arg;
     254                Expression * ret = castExpr->arg;
     255                castExpr->arg = nullptr;
     256
     257                // unlink the arg so that it isn't deleted twice at the end of the program
     258                untyped.arg = nullptr;
     259                return ret;
    263260        }
    264261
     
    268265                Expression * newExpr = resolveInVoidContext( untyped, indexer, env );
    269266                finishExpr( newExpr, env, untyped->env );
     267                delete untyped;
    270268                untyped = newExpr;
    271269        }
     
    277275        void findSingleExpression( Expression *& untyped, Type * type, const SymTab::Indexer & indexer ) {
    278276                assert( untyped && type );
     277                // transfer location to generated cast for error purposes
     278                CodeLocation location = untyped->location;
    279279                untyped = new CastExpr( untyped, type );
     280                untyped->location = location;
    280281                findSingleExpression( untyped, indexer );
    281282                removeExtraneousCast( untyped, indexer );
     
    448449                                castExpr->arg = nullptr;
    449450                                std::swap( newExpr->env, castExpr->env );
     451                                delete castExpr;
    450452                        }
    451453                        caseStmt->condition = newExpr;
     
    580582
    581583                                                        // Make sure we don't widen any existing bindings
    582                                                         for ( auto & i : resultEnv ) {
    583                                                                 i.allowWidening = false;
    584                                                         }
    585 
     584                                                        resultEnv.forbidWidening();
     585                                                       
    586586                                                        // Find any unbound type variables
    587587                                                        resultEnv.extractOpenVars( openVars );
     
    747747                // and newExpr may already have inferParams of its own, so a simple swap is not sufficient.
    748748                newExpr->spliceInferParams( initExpr );
     749                delete initExpr;
    749750
    750751                // get the actual object's type (may not exactly match what comes back from the resolver due to conversions)
     
    764765                                                        ce->set_arg( nullptr );
    765766                                                        std::swap( ce->env, newExpr->env );
     767                                                        delete ce;
    766768                                                }
    767769                                        }
     
    814816                // could not find valid constructor, or found an intrinsic constructor
    815817                // fall back on C-style initializer
    816                 ctorInit->set_ctor( nullptr );
    817                 ctorInit->set_dtor( nullptr );
     818                delete ctorInit->get_ctor();
     819                ctorInit->set_ctor( NULL );
     820                delete ctorInit->get_dtor();
     821                ctorInit->set_dtor( NULL );
    818822                maybeAccept( ctorInit->get_init(), *visitor );
    819823        }
     
    841845
    842846                // found a constructor - can get rid of C-style initializer
     847                delete ctorInit->init;
    843848                ctorInit->init = nullptr;
    844849
     
    847852                // to clean up generated code.
    848853                if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->ctor ) ) {
     854                        delete ctorInit->ctor;
    849855                        ctorInit->ctor = nullptr;
    850856                }
    851857
    852858                if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->dtor ) ) {
     859                        delete ctorInit->dtor;
    853860                        ctorInit->dtor = nullptr;
    854861                }
Note: See TracChangeset for help on using the changeset viewer.