Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/Resolver.cc

    rd286cf68 rb60f9d9  
    2222#include "Alternative.h"                 // for Alternative, AltList
    2323#include "AlternativeFinder.h"           // for AlternativeFinder, resolveIn...
     24#include "Common/GC.h"                   // for new_generation, collect_young
    2425#include "Common/PassVisitor.h"          // for PassVisitor
    2526#include "Common/SemanticError.h"        // for SemanticError
     
    3233#include "ResolveTypeof.h"               // for resolveTypeof
    3334#include "Resolver.h"
     35#include "ResolvMode.h"                  // for ResolvMode
    3436#include "SymTab/Autogen.h"              // for SizeType
    3537#include "SymTab/Indexer.h"              // for Indexer
     
    4951namespace ResolvExpr {
    5052        struct Resolver final : public WithIndexer, public WithGuards, public WithVisitorRef<Resolver>, public WithShortCircuiting, public WithStmtsToAdd {
     53       
     54        friend void resolve( std::list<Declaration*> );
     55
    5156                Resolver() {}
    5257                Resolver( const SymTab::Indexer & other ) {
     
    9499                CurrentObject currentObject = nullptr;
    95100                bool inEnumDecl = false;
     101                bool atTopLevel = false;  ///< Was this resolver set up at the top level of resolution
    96102        };
    97103
    98104        void resolve( std::list< Declaration * > translationUnit ) {
    99105                PassVisitor<Resolver> resolver;
     106                resolver.pass.atTopLevel = true;  // mark resolver as top-level
    100107                acceptAll( translationUnit, resolver );
    101108        }
     
    158165                                        castExpr->arg = nullptr;
    159166                                        std::swap( expr->env, castExpr->env );
    160                                         delete castExpr;
    161167                                }
    162168                        }
     
    165171
    166172        namespace {
    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) {
     173                void findUnfinishedKindExpression( Expression * untyped, Alternative & alt, const SymTab::Indexer & indexer, const std::string & kindStr, std::function<bool(const Alternative &)> pred, ResolvMode mode = ResolvMode{} ) {
    168174                        assertf( untyped, "expected a non-null expression." );
     175
     176                        auto guard = new_generation();  // set up GC generation for this top-level expression
     177
    169178                        TypeEnvironment env;
    170179                        AlternativeFinder finder( indexer, env );
    171                         finder.find( untyped, adjust, prune, failFast );
     180                        finder.find( untyped, mode );
    172181
    173182                        #if 0
     
    207216                        Alternative & choice = winners.front();
    208217                        if ( findDeletedExpr( choice.expr ) ) {
    209                                 SemanticError( untyped->location, choice.expr, "Unique best alternative includes deleted identifier in " );
     218                                trace( choice.expr );
     219                                SemanticError( choice.expr, "Unique best alternative includes deleted identifier in " );
    210220                        }
    211221                        alt = std::move( choice );
     222                        trace( alt );
    212223                }
    213224
    214225                /// resolve `untyped` to the expression whose alternative satisfies `pred` with the lowest cost; kindStr is used for providing better error messages
    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) {
     226                void findKindExpression(Expression *& untyped, const SymTab::Indexer & indexer, const std::string & kindStr, std::function<bool(const Alternative &)> pred, ResolvMode mode = ResolvMode{}) {
    216227                        if ( ! untyped ) return;
    217228                        Alternative choice;
    218                         findUnfinishedKindExpression( untyped, choice, indexer, kindStr, pred, adjust, prune, failFast );
     229                        findUnfinishedKindExpression( untyped, choice, indexer, kindStr, pred, mode );
    219230                        finishExpr( choice.expr, choice.env, untyped->env );
    220                         delete untyped;
    221231                        untyped = choice.expr;
    222232                        choice.expr = nullptr;
     
    241251                assertf( expr, "expected a non-null expression." );
    242252
    243                 static CastExpr untyped( nullptr ); // cast to void
    244                 untyped.location = expr->location;
     253                auto untyped = new CastExpr{ expr }; // cast to void
    245254
    246255                // set up and resolve expression cast to void
    247                 untyped.arg = expr;
    248256                Alternative choice;
    249                 findUnfinishedKindExpression( &untyped, choice, indexer, "", standardAlternativeFilter, true );
     257                findUnfinishedKindExpression( untyped, choice, indexer, "", standardAlternativeFilter, ResolvMode::withAdjustment() );
    250258                CastExpr * castExpr = strict_dynamic_cast< CastExpr * >( choice.expr );
    251259                env = std::move( choice.env );
    252260
    253261                // clean up resolved expression
    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;
     262                return castExpr->arg;
    260263        }
    261264
     
    265268                Expression * newExpr = resolveInVoidContext( untyped, indexer, env );
    266269                finishExpr( newExpr, env, untyped->env );
    267                 delete untyped;
    268270                untyped = newExpr;
    269271        }
     
    275277        void findSingleExpression( Expression *& untyped, Type * type, const SymTab::Indexer & indexer ) {
    276278                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;
    281280                findSingleExpression( untyped, indexer );
    282281                removeExtraneousCast( untyped, indexer );
     
    449448                                castExpr->arg = nullptr;
    450449                                std::swap( newExpr->env, castExpr->env );
    451                                 delete castExpr;
    452450                        }
    453451                        caseStmt->condition = newExpr;
     
    582580
    583581                                                        // Make sure we don't widen any existing bindings
    584                                                         resultEnv.forbidWidening();
    585                                                        
     582                                                        for ( auto & i : resultEnv ) {
     583                                                                i.allowWidening = false;
     584                                                        }
     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;
    750749
    751750                // get the actual object's type (may not exactly match what comes back from the resolver due to conversions)
     
    765764                                                        ce->set_arg( nullptr );
    766765                                                        std::swap( ce->env, newExpr->env );
    767                                                         delete ce;
    768766                                                }
    769767                                        }
     
    816814                // could not find valid constructor, or found an intrinsic constructor
    817815                // fall back on C-style initializer
    818                 delete ctorInit->get_ctor();
    819                 ctorInit->set_ctor( NULL );
    820                 delete ctorInit->get_dtor();
    821                 ctorInit->set_dtor( NULL );
     816                ctorInit->set_ctor( nullptr );
     817                ctorInit->set_dtor( nullptr );
    822818                maybeAccept( ctorInit->get_init(), *visitor );
    823819        }
     
    845841
    846842                // found a constructor - can get rid of C-style initializer
    847                 delete ctorInit->init;
    848843                ctorInit->init = nullptr;
    849844
     
    852847                // to clean up generated code.
    853848                if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->ctor ) ) {
    854                         delete ctorInit->ctor;
    855849                        ctorInit->ctor = nullptr;
    856850                }
    857851
    858852                if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->dtor ) ) {
    859                         delete ctorInit->dtor;
    860853                        ctorInit->dtor = nullptr;
    861854                }
Note: See TracChangeset for help on using the changeset viewer.