Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/Resolver.cc

    r6f326b1 r2efe4b8  
    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
     
    5960                void previsit( TypeDecl *typeDecl );
    6061                void previsit( EnumDecl * enumDecl );
    61                 void previsit( StaticAssertDecl * assertDecl );
    6262
    6363                void previsit( ArrayType * at );
     
    139139                                        castExpr->arg = nullptr;
    140140                                        std::swap( expr->env, castExpr->env );
    141                                         delete castExpr;
    142141                                }
    143142                        }
     
    148147                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) {
    149148                        assertf( untyped, "expected a non-null expression." );
     149
     150                        auto guard = new_generation();  // set up GC generation for this top-level expression
     151
    150152                        TypeEnvironment env;
    151153                        AlternativeFinder finder( indexer, env );
     
    175177                        findMinCost( candidates.begin(), candidates.end(), back_inserter( winners ) );
    176178                        if ( winners.size() == 0 ) {
    177                                 SemanticError( untyped, toString( "No reasonable alternatives for ", kindStr, (kindStr != "" ? " " : ""), "expression: ") );
     179                                SemanticError( untyped, toString(
     180                                        "No reasonable alternatives for ", kindStr, (kindStr != "" ? " " : ""),
     181                                        "expression: ") );
    178182                        } else if ( winners.size() != 1 ) {
    179183                                std::ostringstream stream;
    180                                 stream << "Cannot choose between " << winners.size() << " alternatives for " << kindStr << (kindStr != "" ? " " : "") << "expression\n";
     184                                stream << "Cannot choose between " << winners.size() << " alternatives for "
     185                                        << kindStr << (kindStr != "" ? " " : "") << "expression\n";
    181186                                untyped->print( stream );
    182187                                stream << " Alternatives are:\n";
    183188                                printAlts( winners, stream, 1 );
     189                               
    184190                                SemanticError( untyped->location, stream.str() );
    185191                        }
     
    188194                        Alternative & choice = winners.front();
    189195                        if ( findDeletedExpr( choice.expr ) ) {
    190                                 SemanticError( choice.expr, "Unique best alternative includes deleted identifier in " );
     196                                trace( choice.expr );
     197                                SemanticError( choice.expr,
     198                                        "Unique best alternative includes deleted identifier in " );
    191199                        }
    192200                        alt = std::move( choice );
     201                        trace( alt );
    193202                }
    194203
     
    199208                        findUnfinishedKindExpression( untyped, choice, indexer, kindStr, pred, adjust, prune, failFast );
    200209                        finishExpr( choice.expr, choice.env, untyped->env );
    201                         delete untyped;
    202210                        untyped = choice.expr;
    203211                        choice.expr = nullptr;
     
    222230                assertf( expr, "expected a non-null expression." );
    223231
    224                 static CastExpr untyped( nullptr ); // cast to void
     232                auto untyped = new CastExpr{ expr }; // cast to void
    225233
    226234                // set up and resolve expression cast to void
    227                 untyped.arg = expr;
    228235                Alternative choice;
    229                 findUnfinishedKindExpression( &untyped, choice, indexer, "", standardAlternativeFilter, true );
     236                findUnfinishedKindExpression( untyped, choice, indexer, "", standardAlternativeFilter, true );
    230237                CastExpr * castExpr = strict_dynamic_cast< CastExpr * >( choice.expr );
    231238                env = std::move( choice.env );
    232239
    233240                // clean up resolved expression
    234                 Expression * ret = castExpr->arg;
    235                 castExpr->arg = nullptr;
    236 
    237                 // unlink the arg so that it isn't deleted twice at the end of the program
    238                 untyped.arg = nullptr;
    239                 return ret;
     241                return castExpr->arg;
    240242        }
    241243
     
    245247                Expression * newExpr = resolveInVoidContext( untyped, indexer, env );
    246248                finishExpr( newExpr, env, untyped->env );
    247                 delete untyped;
    248249                untyped = newExpr;
    249250        }
     
    364365        }
    365366
    366         void Resolver::previsit( StaticAssertDecl * assertDecl ) {
    367                 findIntegralExpression( assertDecl->condition, indexer );
    368         }
    369 
    370367        void Resolver::previsit( ExprStmt *exprStmt ) {
    371368                visit_children = false;
     
    423420                        caseStmt->condition = castExpr->arg;
    424421                        castExpr->arg = nullptr;
    425                         delete castExpr;
    426422                }
    427423        }
     
    490486                                ss << "' in call to waitfor";
    491487                                SemanticError( stmt->location, ss.str() );
    492                         }
    493 
    494                         if(clause.target.arguments.empty()) {
    495                                 SemanticError( stmt->location, "Waitfor clause must have at least one mutex parameter");
    496488                        }
    497489
     
    544536                                                        OpenVarSet openVars;
    545537                                                        AssertionSet resultNeed, resultHave;
    546                                                         TypeEnvironment resultEnv( func.env );
    547                                                         makeUnifiableVars( function, openVars, resultNeed );
    548                                                         // add all type variables as open variables now so that those not used in the parameter
    549                                                         // list are still considered open.
    550                                                         resultEnv.add( function->forall );
     538                                                        TypeEnvironment resultEnv;
    551539
    552540                                                        // Load type variables from arguemnts into one shared space
     
    564552                                                        auto param_end = function->parameters.end();
    565553
    566                                                         int n_mutex_arg = 0;
    567 
    568554                                                        // For every arguments of its set, check if it matches one of the parameter
    569555                                                        // The order is important
     
    574560                                                                        // We ran out of parameters but still have arguments
    575561                                                                        // this function doesn't match
    576                                                                         SemanticError( function, toString("candidate function not viable: too many mutex arguments, expected ", n_mutex_arg, "\n" ));
     562                                                                        SemanticError( function, "candidate function not viable: too many mutex arguments\n" );
    577563                                                                }
    578564
    579                                                                 n_mutex_arg++;
    580 
    581565                                                                // Check if the argument matches the parameter type in the current scope
    582                                                                 if( ! unify( arg.expr->get_result(), (*param)->get_type(), resultEnv, resultNeed, resultHave, openVars, this->indexer ) ) {
     566                                                                if( ! unify( (*param)->get_type(), arg.expr->get_result(), resultEnv, resultNeed, resultHave, openVars, this->indexer ) ) {
    583567                                                                        // Type doesn't match
    584568                                                                        stringstream ss;
    585569                                                                        ss << "candidate function not viable: no known convertion from '";
     570                                                                        arg.expr->get_result()->print( ss );
     571                                                                        ss << "' to '";
    586572                                                                        (*param)->get_type()->print( ss );
    587                                                                         ss << "' to '";
    588                                                                         arg.expr->get_result()->print( ss );
    589                                                                         ss << "' with env '";
    590                                                                         resultEnv.print(ss);
    591573                                                                        ss << "'\n";
    592574                                                                        SemanticError( function, ss.str() );
     
    602584                                                                // We ran out of arguments but still have parameters left
    603585                                                                // this function doesn't match
    604                                                                 SemanticError( function, toString("candidate function not viable: too few mutex arguments, expected ", n_mutex_arg, "\n" ));
     586                                                                SemanticError( function, "candidate function not viable: too few mutex arguments\n" );
    605587                                                        }
    606588
     
    719701                std::swap( initExpr->env, newExpr->env );
    720702                std::swap( initExpr->inferParams, newExpr->inferParams ) ;
    721                 delete initExpr;
    722703
    723704                // get the actual object's type (may not exactly match what comes back from the resolver due to conversions)
     
    737718                                                        ce->set_arg( nullptr );
    738719                                                        std::swap( ce->env, newExpr->env );
    739                                                         delete ce;
    740720                                                }
    741721                                        }
     
    788768                // could not find valid constructor, or found an intrinsic constructor
    789769                // fall back on C-style initializer
    790                 delete ctorInit->get_ctor();
    791                 ctorInit->set_ctor( NULL );
    792                 delete ctorInit->get_dtor();
    793                 ctorInit->set_dtor( NULL );
     770                ctorInit->set_ctor( nullptr );
     771                ctorInit->set_dtor( nullptr );
    794772                maybeAccept( ctorInit->get_init(), *visitor );
    795773        }
     
    817795
    818796                // found a constructor - can get rid of C-style initializer
    819                 delete ctorInit->init;
    820797                ctorInit->init = nullptr;
    821798
     
    824801                // to clean up generated code.
    825802                if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->ctor ) ) {
    826                         delete ctorInit->ctor;
    827803                        ctorInit->ctor = nullptr;
    828804                }
    829805
    830806                if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->dtor ) ) {
    831                         delete ctorInit->dtor;
    832807                        ctorInit->dtor = nullptr;
    833808                }
Note: See TracChangeset for help on using the changeset viewer.