Commit 2ca60804 authored by verwaest's avatar verwaest Committed by Commit bot

Split NewScope into NewScope and NewScopeWithParent

NewScope automatically uses scope(), whereas NewScopeWithParent can pass along any local Scope* as outer scope. The number of calls to NewScopeWithParent should be reduced over time.

BUG=v8:5209

Review-Url: https://codereview.chromium.org/2164943002
Cr-Commit-Position: refs/heads/master@{#37912}
parent 6627d81c
......@@ -619,7 +619,14 @@ class ParserBase : public Traits {
return new (zone()) Scope(zone(), nullptr, SCRIPT_SCOPE, kNormalFunction);
}
Scope* NewScope(Scope* parent, ScopeType scope_type) {
Scope* NewScope(ScopeType scope_type) {
return NewScopeWithParent(scope(), scope_type);
}
// This constructor should only be used when absolutely necessary. Most scopes
// should automatically use scope() as parent, and be fine with
// NewScope(ScopeType) above.
Scope* NewScopeWithParent(Scope* parent, ScopeType scope_type) {
// Must always use the specific constructors for the blacklisted scope
// types.
DCHECK_NE(FUNCTION_SCOPE, scope_type);
......
......@@ -957,9 +957,9 @@ FunctionLiteral* Parser::DoParseProgram(ParseInfo* info) {
if (!scope->is_script_scope() || is_strict(info->language_mode())) {
parsing_mode = PARSE_EAGERLY;
}
scope = NewScope(scope, EVAL_SCOPE);
scope = NewScopeWithParent(scope, EVAL_SCOPE);
} else if (info->is_module()) {
scope = NewScope(scope, MODULE_SCOPE);
scope = NewScopeWithParent(scope, MODULE_SCOPE);
}
scope->set_start_position(0);
......@@ -2294,7 +2294,7 @@ Block* Parser::ParseBlock(ZoneList<const AstRawString*>* labels,
// Construct block expecting 16 statements.
Block* body = factory()->NewBlock(labels, 16, false, kNoSourcePosition);
Scope* block_scope = NewScope(scope(), BLOCK_SCOPE);
Scope* block_scope = NewScope(BLOCK_SCOPE);
// Parse the statements and collect escaping labels.
Expect(Token::LBRACE, CHECK_OK);
......@@ -2833,7 +2833,7 @@ Statement* Parser::ParseWithStatement(ZoneList<const AstRawString*>* labels,
Expression* expr = ParseExpression(true, CHECK_OK);
Expect(Token::RPAREN, CHECK_OK);
Scope* with_scope = NewScope(scope(), WITH_SCOPE);
Scope* with_scope = NewScope(WITH_SCOPE);
Statement* body;
{
BlockState block_state(&scope_state_, with_scope);
......@@ -2918,7 +2918,7 @@ Statement* Parser::ParseSwitchStatement(ZoneList<const AstRawString*>* labels,
zone());
Block* cases_block = factory()->NewBlock(NULL, 1, false, kNoSourcePosition);
Scope* cases_scope = NewScope(scope(), BLOCK_SCOPE);
Scope* cases_scope = NewScope(BLOCK_SCOPE);
cases_scope->SetNonlinear();
SwitchStatement* switch_statement =
......@@ -3010,7 +3010,7 @@ TryStatement* Parser::ParseTryStatement(bool* ok) {
Consume(Token::CATCH);
Expect(Token::LPAREN, CHECK_OK);
catch_scope = NewScope(scope(), CATCH_SCOPE);
catch_scope = NewScope(CATCH_SCOPE);
catch_scope->set_start_position(scanner()->location().beg_pos);
{
......@@ -3023,7 +3023,7 @@ TryStatement* Parser::ParseTryStatement(bool* ok) {
// Create a block scope to hold any lexical declarations created
// as part of destructuring the catch parameter.
Scope* block_scope = NewScope(scope(), BLOCK_SCOPE);
Scope* block_scope = NewScope(BLOCK_SCOPE);
block_scope->set_start_position(scanner()->location().beg_pos);
{
BlockState block_state(&scope_state_, block_scope);
......@@ -3650,7 +3650,7 @@ Statement* Parser::ParseScopedStatement(ZoneList<const AstRawString*>* labels,
}
// Make a block around the statement for a lexical binding
// is introduced by a FunctionDeclaration.
Scope* body_scope = NewScope(scope(), BLOCK_SCOPE);
Scope* body_scope = NewScope(BLOCK_SCOPE);
body_scope->set_start_position(scanner()->location().beg_pos);
BlockState block_state(&scope_state_, body_scope);
Block* block = factory()->NewBlock(NULL, 1, false, kNoSourcePosition);
......@@ -3671,7 +3671,7 @@ Statement* Parser::ParseForStatement(ZoneList<const AstRawString*>* labels,
bool bound_names_are_lexical = false;
// Create an in-between scope for let-bound iteration variables.
Scope* for_scope = NewScope(scope(), BLOCK_SCOPE);
Scope* for_scope = NewScope(BLOCK_SCOPE);
BlockState block_state(&scope_state_, for_scope);
Expect(Token::FOR, CHECK_OK);
......@@ -3776,7 +3776,7 @@ Statement* Parser::ParseForStatement(ZoneList<const AstRawString*>* labels,
Expect(Token::RPAREN, CHECK_OK);
Scope* body_scope = NewScope(scope(), BLOCK_SCOPE);
Scope* body_scope = NewScope(BLOCK_SCOPE);
body_scope->set_start_position(scanner()->location().beg_pos);
Block* body_block =
......@@ -3959,7 +3959,7 @@ Statement* Parser::ParseForStatement(ZoneList<const AstRawString*>* labels,
// for loop must be parsed in a new scope.
Scope* inner_scope = scope();
if (bound_names_are_lexical && bound_names.length() > 0) {
inner_scope = NewScope(for_scope, BLOCK_SCOPE);
inner_scope = NewScopeWithParent(for_scope, BLOCK_SCOPE);
inner_scope->set_start_position(scanner()->location().beg_pos);
}
{
......@@ -4707,7 +4707,7 @@ Block* Parser::BuildParameterInitializationBlock(
Scope* param_scope = scope();
Block* param_block = init_block;
if (!parameter.is_simple() && scope()->calls_sloppy_eval()) {
param_scope = NewScope(scope(), BLOCK_SCOPE);
param_scope = NewScope(BLOCK_SCOPE);
param_scope->set_is_declaration_scope();
param_scope->set_start_position(descriptor.initialization_pos);
param_scope->set_end_position(parameter.initializer_end_position);
......@@ -4745,7 +4745,7 @@ Block* Parser::BuildParameterInitializationBlock(
Block* Parser::BuildRejectPromiseOnException(Block* block) {
// try { <block> } catch (error) { return Promise.reject(error); }
Block* try_block = block;
Scope* catch_scope = NewScope(scope(), CATCH_SCOPE);
Scope* catch_scope = NewScope(CATCH_SCOPE);
catch_scope->set_is_hidden();
Variable* catch_variable =
catch_scope->DeclareLocal(ast_value_factory()->dot_catch_string(), VAR,
......@@ -4817,7 +4817,7 @@ ZoneList<Statement*>* Parser::ParseEagerFunctionBody(
Scope* inner_scope = scope();
Block* inner_block = nullptr;
if (!parameters.is_simple) {
inner_scope = NewScope(scope(), BLOCK_SCOPE);
inner_scope = NewScope(BLOCK_SCOPE);
inner_scope->set_is_declaration_scope();
inner_scope->set_start_position(scanner()->location().beg_pos);
inner_block = factory()->NewBlock(NULL, 8, true, kNoSourcePosition);
......@@ -5017,7 +5017,7 @@ ClassLiteral* Parser::ParseClassLiteral(ExpressionClassifier* classifier,
return NULL;
}
Scope* block_scope = NewScope(scope(), BLOCK_SCOPE);
Scope* block_scope = NewScope(BLOCK_SCOPE);
BlockState block_state(&scope_state_, block_scope);
RaiseLanguageMode(STRICT);
scope()->SetScopeName(name);
......@@ -6511,7 +6511,7 @@ Expression* ParserTraits::RewriteYieldStar(
Block* catch_block = factory->NewBlock(nullptr, 1, false, nopos);
catch_block->statements()->Add(set_mode_throw, zone);
Scope* catch_scope = NewScope(scope, CATCH_SCOPE);
Scope* catch_scope = NewScopeWithParent(scope, CATCH_SCOPE);
catch_scope->set_is_hidden();
const AstRawString* name = avfactory->dot_catch_string();
Variable* catch_variable =
......@@ -6810,7 +6810,7 @@ void ParserTraits::FinalizeIteratorUse(Variable* completion,
// }
Statement* try_catch;
{
Scope* catch_scope = parser_->NewScope(scope, CATCH_SCOPE);
Scope* catch_scope = parser_->NewScopeWithParent(scope, CATCH_SCOPE);
Variable* catch_variable =
catch_scope->DeclareLocal(avfactory->dot_catch_string(), VAR,
kCreatedInitialized, Variable::NORMAL);
......@@ -6922,7 +6922,7 @@ void ParserTraits::BuildIteratorCloseForCompletion(
Block* catch_block = factory->NewBlock(nullptr, 0, false, nopos);
Scope* catch_scope = NewScope(scope, CATCH_SCOPE);
Scope* catch_scope = NewScopeWithParent(scope, CATCH_SCOPE);
Variable* catch_variable = catch_scope->DeclareLocal(
avfactory->dot_catch_string(), VAR, kCreatedInitialized,
Variable::NORMAL);
......
......@@ -542,8 +542,9 @@ class ParserTraits {
ZoneList<Statement*>* body, bool accept_IN,
Type::ExpressionClassifier* classifier, int pos, bool* ok);
V8_INLINE Scope* NewScope(Scope* parent_scope, ScopeType scope_type);
V8_INLINE Scope* NewScope(ScopeType scope_type);
V8_INLINE Scope* NewFunctionScope(FunctionKind kind);
V8_INLINE Scope* NewScopeWithParent(Scope* parent, ScopeType scope_type);
V8_INLINE void AddFormalParameter(ParserFormalParameters* parameters,
Expression* pattern,
......@@ -1143,8 +1144,12 @@ bool ParserTraits::IsFutureStrictReserved(
return parser_->scanner()->IdentifierIsFutureStrictReserved(identifier);
}
Scope* ParserTraits::NewScope(Scope* parent_scope, ScopeType scope_type) {
return parser_->NewScope(parent_scope, scope_type);
Scope* ParserTraits::NewScopeWithParent(Scope* parent, ScopeType scope_type) {
return parser_->NewScopeWithParent(parent, scope_type);
}
Scope* ParserTraits::NewScope(ScopeType scope_type) {
return parser_->NewScope(scope_type);
}
Scope* ParserTraits::NewFunctionScope(FunctionKind kind) {
......
......@@ -307,7 +307,7 @@ PreParser::Statement PreParser::ParseScopedStatement(bool legacy, bool* ok) {
(legacy && allow_harmony_restrictive_declarations())) {
return ParseSubStatement(kDisallowLabelledFunctionStatement, ok);
} else {
Scope* body_scope = NewScope(scope(), BLOCK_SCOPE);
Scope* body_scope = NewScope(BLOCK_SCOPE);
BlockState block_state(&scope_state_, body_scope);
return ParseFunctionDeclaration(ok);
}
......@@ -478,7 +478,7 @@ PreParser::Statement PreParser::ParseBlock(bool* ok) {
// Block ::
// '{' StatementList '}'
Scope* block_scope = NewScope(scope(), BLOCK_SCOPE);
Scope* block_scope = NewScope(BLOCK_SCOPE);
Expect(Token::LBRACE, CHECK_OK);
Statement final = Statement::Default();
{
......@@ -792,7 +792,7 @@ PreParser::Statement PreParser::ParseWithStatement(bool* ok) {
ParseExpression(true, CHECK_OK);
Expect(Token::RPAREN, CHECK_OK);
Scope* with_scope = NewScope(scope(), WITH_SCOPE);
Scope* with_scope = NewScope(WITH_SCOPE);
BlockState block_state(&scope_state_, with_scope);
ParseScopedStatement(true, CHECK_OK);
return Statement::Default();
......@@ -808,7 +808,7 @@ PreParser::Statement PreParser::ParseSwitchStatement(bool* ok) {
ParseExpression(true, CHECK_OK);
Expect(Token::RPAREN, CHECK_OK);
Scope* cases_scope = NewScope(scope(), BLOCK_SCOPE);
Scope* cases_scope = NewScope(BLOCK_SCOPE);
{
BlockState cases_block_state(&scope_state_, cases_scope);
Expect(Token::LBRACE, CHECK_OK);
......@@ -869,7 +869,7 @@ PreParser::Statement PreParser::ParseForStatement(bool* ok) {
// 'for' '(' Expression? ';' Expression? ';' Expression? ')' Statement
// Create an in-between scope for let-bound iteration variables.
Scope* for_scope = NewScope(scope(), BLOCK_SCOPE);
Scope* for_scope = NewScope(BLOCK_SCOPE);
bool has_lexical = false;
BlockState block_state(&scope_state_, for_scope);
......@@ -959,7 +959,7 @@ PreParser::Statement PreParser::ParseForStatement(bool* ok) {
}
Expect(Token::RPAREN, CHECK_OK);
Scope* body_scope = NewScope(scope(), BLOCK_SCOPE);
Scope* body_scope = NewScope(BLOCK_SCOPE);
{
BlockState block_state(&scope_state_, body_scope);
ParseScopedStatement(true, CHECK_OK);
......@@ -975,7 +975,7 @@ PreParser::Statement PreParser::ParseForStatement(bool* ok) {
// If there are let bindings, then condition and the next statement of the
// for loop must be parsed in a new scope.
Scope* inner_scope = scope();
if (has_lexical) inner_scope = NewScope(for_scope, BLOCK_SCOPE);
if (has_lexical) inner_scope = NewScopeWithParent(for_scope, BLOCK_SCOPE);
{
BlockState block_state(&scope_state_, inner_scope);
......@@ -1043,7 +1043,7 @@ PreParser::Statement PreParser::ParseTryStatement(bool* ok) {
if (tok == Token::CATCH) {
Consume(Token::CATCH);
Expect(Token::LPAREN, CHECK_OK);
Scope* catch_scope = NewScope(scope(), CATCH_SCOPE);
Scope* catch_scope = NewScope(CATCH_SCOPE);
ExpressionClassifier pattern_classifier(this);
ParsePrimaryExpression(&pattern_classifier, CHECK_OK);
ValidateBindingPattern(&pattern_classifier, CHECK_OK);
......@@ -1053,7 +1053,7 @@ PreParser::Statement PreParser::ParseTryStatement(bool* ok) {
collect_tail_call_expressions_scope(
function_state_, &tail_call_expressions_in_catch_block);
BlockState block_state(&scope_state_, catch_scope);
Scope* block_scope = NewScope(scope(), BLOCK_SCOPE);
Scope* block_scope = NewScope(BLOCK_SCOPE);
{
BlockState block_state(&scope_state_, block_scope);
ParseBlock(CHECK_OK);
......@@ -1231,7 +1231,7 @@ PreParserExpression PreParser::ParseClassLiteral(
}
LanguageMode class_language_mode = language_mode();
Scope* scope = NewScope(this->scope(), BLOCK_SCOPE);
Scope* scope = NewScope(BLOCK_SCOPE);
BlockState block_state(&scope_state_, scope);
this->scope()->SetLanguageMode(
static_cast<LanguageMode>(class_language_mode | STRICT));
......
......@@ -1046,7 +1046,7 @@ class PreParser : public ParserBase<PreParserTraits> {
// new Module Environment Record whose outer lexical environment record is
// the global scope.
if (is_module) {
scope = NewScope(scope, MODULE_SCOPE);
scope = NewScopeWithParent(scope, MODULE_SCOPE);
}
PreParserFactory factory(nullptr);
......@@ -1249,7 +1249,7 @@ PreParserStatementList PreParser::ParseEagerFunctionBody(
ParsingModeScope parsing_mode(this, PARSE_EAGERLY);
Scope* inner_scope = scope();
if (!parameters.is_simple) inner_scope = NewScope(scope(), BLOCK_SCOPE);
if (!parameters.is_simple) inner_scope = NewScope(BLOCK_SCOPE);
{
BlockState block_state(&scope_state_, inner_scope);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment