Commit a29225ce authored by Leszek Swirski's avatar Leszek Swirski Committed by Commit Bot

[parser] Use ScopedPtrList for init blocks

Change-Id: I021776d10dd8ef4bf406f286ee233aff9680a0ec
Reviewed-on: https://chromium-review.googlesource.com/c/1384315
Commit-Queue: Leszek Swirski <leszeks@chromium.org>
Reviewed-by: 's avatarToon Verwaest <verwaest@chromium.org>
Cr-Commit-Position: refs/heads/master@{#58730}
parent 7637925c
......@@ -1391,15 +1391,15 @@ void Parser::Declare(Declaration* declaration, VariableKind variable_kind,
}
}
Block* Parser::BuildInitializationBlock(
Statement* Parser::BuildInitializationBlock(
DeclarationParsingResult* parsing_result,
ZonePtrList<const AstRawString>* names) {
Block* result = factory()->NewBlock(1, true);
ScopedPtrList<Statement> statements(pointer_buffer());
for (const auto& declaration : parsing_result->declarations) {
InitializeVariables(result, &(parsing_result->descriptor), &declaration,
names);
InitializeVariables(&statements, &(parsing_result->descriptor),
&declaration, names);
}
return result;
return factory()->NewBlock(true, statements);
}
Statement* Parser::DeclareFunction(const AstRawString* variable_name,
......@@ -1589,9 +1589,9 @@ Block* Parser::RewriteCatchPattern(CatchInfo* catch_info) {
catch_info->pattern, initializer_position,
factory()->NewVariableProxy(catch_info->variable));
Block* init_block = factory()->NewBlock(8, true);
InitializeVariables(init_block, &descriptor, &decl, nullptr);
return init_block;
ScopedPtrList<Statement> init_statements(pointer_buffer());
InitializeVariables(&init_statements, &descriptor, &decl, nullptr);
return factory()->NewBlock(true, init_statements);
}
void Parser::ReportVarRedeclarationIn(const AstRawString* name, Scope* scope) {
......@@ -1881,7 +1881,7 @@ void Parser::DesugarBindingInForEachStatement(ForInfo* for_info,
DeclarationParsingResult::Declaration& decl =
for_info->parsing_result.declarations[0];
Variable* temp = NewTemporary(ast_value_factory()->dot_for_string());
auto each_initialization_block = factory()->NewBlock(1, true);
ScopedPtrList<Statement> each_initialization_statements(pointer_buffer());
{
auto descriptor = for_info->parsing_result.descriptor;
descriptor.declaration_pos = kNoSourcePosition;
......@@ -1895,7 +1895,7 @@ void Parser::DesugarBindingInForEachStatement(ForInfo* for_info,
IsLexicalVariableMode(for_info->parsing_result.descriptor.mode) ||
is_for_var_of;
InitializeVariables(each_initialization_block, &descriptor, &decl,
InitializeVariables(&each_initialization_statements, &descriptor, &decl,
collect_names ? &for_info->bound_names : nullptr);
// Annex B.3.5 prohibits the form
......@@ -1923,7 +1923,9 @@ void Parser::DesugarBindingInForEachStatement(ForInfo* for_info,
}
*body_block = factory()->NewBlock(3, false);
(*body_block)->statements()->Add(each_initialization_block, zone());
(*body_block)
->statements()
->Add(factory()->NewBlock(true, each_initialization_statements), zone());
*each_variable = factory()->NewVariableProxy(temp, for_info->position);
}
......@@ -2784,7 +2786,7 @@ Block* Parser::BuildParameterInitializationBlock(
DCHECK(!parameters.is_simple);
DCHECK(scope()->is_function_scope());
DCHECK_EQ(scope(), parameters.scope);
Block* init_block = factory()->NewBlock(parameters.num_parameters(), true);
ScopedPtrList<Statement> init_statements(pointer_buffer());
int index = 0;
for (auto parameter : parameters.params) {
DeclarationDescriptor descriptor;
......@@ -2826,15 +2828,17 @@ Block* Parser::BuildParameterInitializationBlock(
}
Scope* param_scope = scope();
Block* param_block = init_block;
ScopedPtrList<Statement>* param_init_statements = &init_statements;
base::Optional<ScopedPtrList<Statement>> non_simple_param_init_statements;
if (!parameter->is_simple() &&
scope()->AsDeclarationScope()->calls_sloppy_eval()) {
param_scope = NewVarblockScope();
param_scope->set_start_position(descriptor.initialization_pos);
param_scope->set_end_position(parameter->initializer_end_position);
param_scope->RecordEvalCall();
param_block = factory()->NewBlock(8, true);
param_block->set_scope(param_scope);
non_simple_param_init_statements.emplace(pointer_buffer());
param_init_statements = &non_simple_param_init_statements.value();
// Rewrite the outer initializer to point to param_scope
ReparentExpressionScope(stack_limit(), initial_value, param_scope);
}
......@@ -2842,15 +2846,25 @@ Block* Parser::BuildParameterInitializationBlock(
BlockState block_state(&scope_, param_scope);
DeclarationParsingResult::Declaration decl(
parameter->pattern, parameter->initializer_end_position, initial_value);
InitializeVariables(param_block, &descriptor, &decl, nullptr);
if (param_block != init_block) {
InitializeVariables(param_init_statements, &descriptor, &decl, nullptr);
if (param_init_statements != &init_statements) {
DCHECK_EQ(param_init_statements,
&non_simple_param_init_statements.value());
Block* param_block =
factory()->NewBlock(true, *non_simple_param_init_statements);
non_simple_param_init_statements.reset();
param_block->set_scope(param_scope);
param_scope = param_scope->FinalizeBlockScope();
init_block->statements()->Add(param_block, zone());
if (param_scope != nullptr) {
CheckConflictingVarDeclarations(param_scope);
}
init_statements.Add(param_block);
}
++index;
}
return init_block;
return factory()->NewBlock(true, init_statements);
}
Scope* Parser::NewHiddenCatchScope() {
......
......@@ -309,8 +309,8 @@ class V8_EXPORT_PRIVATE Parser : public NON_EXPORTED_BASE(ParserBase<Parser>) {
location(location) {}
};
ZonePtrList<const NamedImport>* ParseNamedImports(int pos);
Block* BuildInitializationBlock(DeclarationParsingResult* parsing_result,
ZonePtrList<const AstRawString>* names);
Statement* BuildInitializationBlock(DeclarationParsingResult* parsing_result,
ZonePtrList<const AstRawString>* names);
void DeclareLabel(ZonePtrList<const AstRawString>** labels,
ZonePtrList<const AstRawString>** own_labels,
VariableProxy* expr);
......@@ -373,7 +373,8 @@ class V8_EXPORT_PRIVATE Parser : public NON_EXPORTED_BASE(ParserBase<Parser>) {
// PatternRewriter and associated methods defined in pattern-rewriter.cc.
friend class PatternRewriter;
void InitializeVariables(
Block* block, const DeclarationDescriptor* declaration_descriptor,
ScopedPtrList<Statement>* statements,
const DeclarationDescriptor* declaration_descriptor,
const DeclarationParsingResult::Declaration* declaration,
ZonePtrList<const AstRawString>* names);
......
......@@ -39,8 +39,7 @@ class PatternRewriter final : public AstVisitor<PatternRewriter> {
typedef Parser::DeclarationDescriptor DeclarationDescriptor;
static void InitializeVariables(
Parser* parser, Block* block,
const DeclarationDescriptor* declaration_descriptor,
Parser* parser, const DeclarationDescriptor* declaration_descriptor,
const Parser::DeclarationParsingResult::Declaration* declaration,
ZonePtrList<const AstRawString>* names);
......@@ -101,11 +100,12 @@ class PatternRewriter final : public AstVisitor<PatternRewriter> {
};
void Parser::InitializeVariables(
Block* block, const DeclarationDescriptor* declaration_descriptor,
ScopedPtrList<Statement>* statements,
const DeclarationDescriptor* declaration_descriptor,
const DeclarationParsingResult::Declaration* declaration,
ZonePtrList<const AstRawString>* names) {
if (has_error()) return;
PatternRewriter::InitializeVariables(this, block, declaration_descriptor,
PatternRewriter::InitializeVariables(this, declaration_descriptor,
declaration, names);
if (declaration->initializer) {
......@@ -115,18 +115,14 @@ void Parser::InitializeVariables(
}
Assignment* assignment = factory()->NewAssignment(
Token::INIT, declaration->pattern, declaration->initializer, pos);
block->statements()->Add(factory()->NewExpressionStatement(assignment, pos),
zone());
statements->Add(factory()->NewExpressionStatement(assignment, pos));
}
}
void PatternRewriter::InitializeVariables(
Parser* parser, Block* block,
const DeclarationDescriptor* declaration_descriptor,
Parser* parser, const DeclarationDescriptor* declaration_descriptor,
const Parser::DeclarationParsingResult::Declaration* declaration,
ZonePtrList<const AstRawString>* names) {
DCHECK(block->ignore_completion_value());
PatternRewriter rewriter(parser, declaration_descriptor, names,
declaration->initializer != nullptr,
declaration->initializer_position,
......
......@@ -413,7 +413,7 @@ PreParserExpression PreParser::ExpressionFromIdentifier(
}
void PreParser::InitializeVariables(
PreParserStatement block,
PreParserScopedStatementList* statements,
const DeclarationDescriptor* declaration_descriptor,
const DeclarationParsingResult::Declaration* declaration,
ZonePtrList<const AstRawString>* names) {
......
......@@ -423,6 +423,7 @@ class PreParserScopedStatementList {
void Rewind() {}
void MergeInto(const PreParserScopedStatementList* other) {}
void Add(const PreParserStatement& element) {}
int length() { return 0; }
};
// The pre-parser doesn't need to build lists of expressions, identifiers, or
......@@ -1099,7 +1100,7 @@ class PreParser : public ParserBase<PreParser> {
const PreParserExpression& return_value) {}
void InitializeVariables(
PreParserStatement block,
PreParserScopedStatementList* statements,
const DeclarationDescriptor* declaration_descriptor,
const DeclarationParsingResult::Declaration* declaration,
ZonePtrList<const AstRawString>* names);
......@@ -1401,8 +1402,8 @@ class PreParser : public ParserBase<PreParser> {
BuildInitializationBlock(DeclarationParsingResult* parsing_result,
ZonePtrList<const AstRawString>* names) {
for (auto declaration : parsing_result->declarations) {
InitializeVariables(PreParserStatement::Default(),
&(parsing_result->descriptor), &declaration, names);
InitializeVariables(nullptr, &(parsing_result->descriptor), &declaration,
names);
}
return PreParserStatement::Default();
}
......@@ -1436,8 +1437,7 @@ class PreParser : public ParserBase<PreParser> {
IsLexicalVariableMode(for_info->parsing_result.descriptor.mode) ||
is_for_var_of;
InitializeVariables(PreParserStatement::Default(),
&for_info->parsing_result.descriptor,
InitializeVariables(nullptr, &for_info->parsing_result.descriptor,
&for_info->parsing_result.declarations[0],
collect_names ? &for_info->bound_names : nullptr);
}
......
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