Commit 4999717e authored by marja's avatar marja Committed by Commit bot

Move DeclarationDescriptor to ParserBase.

This is the first step towards unifying ParseVariableDeclarations.

BUG=

Review-Url: https://codereview.chromium.org/2297643002
Cr-Commit-Position: refs/heads/master@{#39036}
parent 896323eb
...@@ -632,6 +632,40 @@ class ParserBase { ...@@ -632,6 +632,40 @@ class ParserBase {
Mode old_mode_; Mode old_mode_;
}; };
struct DeclarationDescriptor {
enum Kind { NORMAL, PARAMETER };
Scope* scope;
Scope* hoist_scope;
VariableMode mode;
int declaration_pos;
int initialization_pos;
Kind declaration_kind;
};
struct DeclarationParsingResult {
struct Declaration {
Declaration(ExpressionT pattern, int initializer_position,
ExpressionT initializer)
: pattern(pattern),
initializer_position(initializer_position),
initializer(initializer) {}
ExpressionT pattern;
int initializer_position;
ExpressionT initializer;
};
DeclarationParsingResult()
: declarations(4),
first_initializer_loc(Scanner::Location::invalid()),
bindings_loc(Scanner::Location::invalid()) {}
DeclarationDescriptor descriptor;
List<Declaration> declarations;
Scanner::Location first_initializer_loc;
Scanner::Location bindings_loc;
};
DeclarationScope* NewScriptScope() const { DeclarationScope* NewScriptScope() const {
return new (zone()) DeclarationScope(zone()); return new (zone()) DeclarationScope(zone());
} }
......
...@@ -1907,14 +1907,15 @@ Block* Parser::ParseBlock(ZoneList<const AstRawString*>* labels, bool* ok) { ...@@ -1907,14 +1907,15 @@ Block* Parser::ParseBlock(ZoneList<const AstRawString*>* labels, bool* ok) {
return body; return body;
} }
Block* Parser::BuildInitializationBlock(
Block* Parser::DeclarationParsingResult::BuildInitializationBlock( DeclarationParsingResult* parsing_result,
ZoneList<const AstRawString*>* names, bool* ok) { ZoneList<const AstRawString*>* names, bool* ok) {
Block* result = descriptor.parser->factory()->NewBlock( Block* result = factory()->NewBlock(
NULL, 1, true, descriptor.declaration_pos); NULL, 1, true, parsing_result->descriptor.declaration_pos);
for (auto declaration : declarations) { for (auto declaration : parsing_result->declarations) {
PatternRewriter::DeclareAndInitializeVariables( PatternRewriter::DeclareAndInitializeVariables(
result, &descriptor, &declaration, names, CHECK_OK); this, result, &(parsing_result->descriptor), &declaration, names,
CHECK_OK);
} }
return result; return result;
} }
...@@ -1963,7 +1964,6 @@ Block* Parser::ParseVariableDeclarations( ...@@ -1963,7 +1964,6 @@ Block* Parser::ParseVariableDeclarations(
// ConstBinding :: // ConstBinding ::
// BindingPattern '=' AssignmentExpression // BindingPattern '=' AssignmentExpression
parsing_result->descriptor.parser = this;
parsing_result->descriptor.declaration_kind = DeclarationDescriptor::NORMAL; parsing_result->descriptor.declaration_kind = DeclarationDescriptor::NORMAL;
parsing_result->descriptor.declaration_pos = peek_position(); parsing_result->descriptor.declaration_pos = peek_position();
parsing_result->descriptor.initialization_pos = peek_position(); parsing_result->descriptor.initialization_pos = peek_position();
...@@ -2084,7 +2084,8 @@ Block* Parser::ParseVariableDeclarations( ...@@ -2084,7 +2084,8 @@ Block* Parser::ParseVariableDeclarations(
// and adding VariableProxies to the Scope (see bug 4699). // and adding VariableProxies to the Scope (see bug 4699).
DCHECK_NOT_NULL(init_block); DCHECK_NOT_NULL(init_block);
PatternRewriter::DeclareAndInitializeVariables( PatternRewriter::DeclareAndInitializeVariables(
init_block, &parsing_result->descriptor, &decl, names, CHECK_OK); this, init_block, &parsing_result->descriptor, &decl, names,
CHECK_OK);
} }
first_declaration = false; first_declaration = false;
} while (peek() == Token::COMMA); } while (peek() == Token::COMMA);
...@@ -2632,7 +2633,6 @@ TryStatement* Parser::ParseTryStatement(bool* ok) { ...@@ -2632,7 +2633,6 @@ TryStatement* Parser::ParseTryStatement(bool* ok) {
if (pattern != nullptr) { if (pattern != nullptr) {
DeclarationDescriptor descriptor; DeclarationDescriptor descriptor;
descriptor.declaration_kind = DeclarationDescriptor::NORMAL; descriptor.declaration_kind = DeclarationDescriptor::NORMAL;
descriptor.parser = this;
descriptor.scope = scope(); descriptor.scope = scope();
descriptor.hoist_scope = nullptr; descriptor.hoist_scope = nullptr;
descriptor.mode = LET; descriptor.mode = LET;
...@@ -2649,7 +2649,7 @@ TryStatement* Parser::ParseTryStatement(bool* ok) { ...@@ -2649,7 +2649,7 @@ TryStatement* Parser::ParseTryStatement(bool* ok) {
Block* init_block = Block* init_block =
factory()->NewBlock(nullptr, 8, true, kNoSourcePosition); factory()->NewBlock(nullptr, 8, true, kNoSourcePosition);
PatternRewriter::DeclareAndInitializeVariables( PatternRewriter::DeclareAndInitializeVariables(
init_block, &descriptor, &decl, &bound_names, CHECK_OK); this, init_block, &descriptor, &decl, &bound_names, CHECK_OK);
catch_block->statements()->Add(init_block, zone()); catch_block->statements()->Add(init_block, zone());
} else { } else {
bound_names.Add(name, zone()); bound_names.Add(name, zone());
...@@ -3378,7 +3378,7 @@ Statement* Parser::ParseForStatement(ZoneList<const AstRawString*>* labels, ...@@ -3378,7 +3378,7 @@ Statement* Parser::ParseForStatement(ZoneList<const AstRawString*>* labels,
parsing_result.descriptor.mode == VariableMode::VAR; parsing_result.descriptor.mode == VariableMode::VAR;
PatternRewriter::DeclareAndInitializeVariables( PatternRewriter::DeclareAndInitializeVariables(
each_initialization_block, &descriptor, &decl, this, each_initialization_block, &descriptor, &decl,
bound_names_are_lexical || is_for_var_of ? &bound_names bound_names_are_lexical || is_for_var_of ? &bound_names
: nullptr, : nullptr,
CHECK_OK); CHECK_OK);
...@@ -3453,8 +3453,9 @@ Statement* Parser::ParseForStatement(ZoneList<const AstRawString*>* labels, ...@@ -3453,8 +3453,9 @@ Statement* Parser::ParseForStatement(ZoneList<const AstRawString*>* labels,
} else { } else {
bound_names_are_lexical = bound_names_are_lexical =
IsLexicalVariableMode(parsing_result.descriptor.mode); IsLexicalVariableMode(parsing_result.descriptor.mode);
init = parsing_result.BuildInitializationBlock( init = BuildInitializationBlock(
bound_names_are_lexical ? &bound_names : nullptr, CHECK_OK); &parsing_result, bound_names_are_lexical ? &bound_names : nullptr,
CHECK_OK);
} }
} else { } else {
int lhs_beg_pos = peek_position(); int lhs_beg_pos = peek_position();
...@@ -4235,7 +4236,6 @@ Block* Parser::BuildParameterInitializationBlock( ...@@ -4235,7 +4236,6 @@ Block* Parser::BuildParameterInitializationBlock(
if (parameter.is_rest && parameter.pattern->IsVariableProxy()) break; if (parameter.is_rest && parameter.pattern->IsVariableProxy()) break;
DeclarationDescriptor descriptor; DeclarationDescriptor descriptor;
descriptor.declaration_kind = DeclarationDescriptor::PARAMETER; descriptor.declaration_kind = DeclarationDescriptor::PARAMETER;
descriptor.parser = this;
descriptor.scope = scope(); descriptor.scope = scope();
descriptor.hoist_scope = nullptr; descriptor.hoist_scope = nullptr;
descriptor.mode = LET; descriptor.mode = LET;
...@@ -4288,8 +4288,8 @@ Block* Parser::BuildParameterInitializationBlock( ...@@ -4288,8 +4288,8 @@ Block* Parser::BuildParameterInitializationBlock(
BlockState block_state(&scope_state_, param_scope); BlockState block_state(&scope_state_, param_scope);
DeclarationParsingResult::Declaration decl( DeclarationParsingResult::Declaration decl(
parameter.pattern, initializer_position, initial_value); parameter.pattern, initializer_position, initial_value);
PatternRewriter::DeclareAndInitializeVariables(param_block, &descriptor, PatternRewriter::DeclareAndInitializeVariables(
&decl, nullptr, CHECK_OK); this, param_block, &descriptor, &decl, nullptr, CHECK_OK);
if (param_block != init_block) { if (param_block != init_block) {
param_scope = block_state.FinalizedBlockScope(); param_scope = block_state.FinalizedBlockScope();
......
...@@ -295,54 +295,21 @@ class Parser : public ParserBase<Parser> { ...@@ -295,54 +295,21 @@ class Parser : public ParserBase<Parser> {
bool default_export, bool* ok); bool default_export, bool* ok);
Statement* ParseNativeDeclaration(bool* ok); Statement* ParseNativeDeclaration(bool* ok);
Block* ParseBlock(ZoneList<const AstRawString*>* labels, bool* ok); Block* ParseBlock(ZoneList<const AstRawString*>* labels, bool* ok);
Block* BuildInitializationBlock(DeclarationParsingResult* parsing_result,
ZoneList<const AstRawString*>* names,
bool* ok);
Block* ParseVariableStatement(VariableDeclarationContext var_context, Block* ParseVariableStatement(VariableDeclarationContext var_context,
ZoneList<const AstRawString*>* names, ZoneList<const AstRawString*>* names,
bool* ok); bool* ok);
DoExpression* ParseDoExpression(bool* ok); DoExpression* ParseDoExpression(bool* ok);
Expression* ParseYieldStarExpression(bool* ok); Expression* ParseYieldStarExpression(bool* ok);
struct DeclarationDescriptor {
enum Kind { NORMAL, PARAMETER };
Parser* parser;
Scope* scope;
Scope* hoist_scope;
VariableMode mode;
int declaration_pos;
int initialization_pos;
Kind declaration_kind;
};
struct DeclarationParsingResult {
struct Declaration {
Declaration(Expression* pattern, int initializer_position,
Expression* initializer)
: pattern(pattern),
initializer_position(initializer_position),
initializer(initializer) {}
Expression* pattern;
int initializer_position;
Expression* initializer;
};
DeclarationParsingResult()
: declarations(4),
first_initializer_loc(Scanner::Location::invalid()),
bindings_loc(Scanner::Location::invalid()) {}
Block* BuildInitializationBlock(ZoneList<const AstRawString*>* names,
bool* ok);
DeclarationDescriptor descriptor;
List<Declaration> declarations;
Scanner::Location first_initializer_loc;
Scanner::Location bindings_loc;
};
class PatternRewriter final : public AstVisitor<PatternRewriter> { class PatternRewriter final : public AstVisitor<PatternRewriter> {
public: public:
static void DeclareAndInitializeVariables( static void DeclareAndInitializeVariables(
Block* block, const DeclarationDescriptor* declaration_descriptor, Parser* parser, Block* block,
const DeclarationDescriptor* declaration_descriptor,
const DeclarationParsingResult::Declaration* declaration, const DeclarationParsingResult::Declaration* declaration,
ZoneList<const AstRawString*>* names, bool* ok); ZoneList<const AstRawString*>* names, bool* ok);
......
...@@ -12,7 +12,8 @@ namespace v8 { ...@@ -12,7 +12,8 @@ namespace v8 {
namespace internal { namespace internal {
void Parser::PatternRewriter::DeclareAndInitializeVariables( void Parser::PatternRewriter::DeclareAndInitializeVariables(
Block* block, const DeclarationDescriptor* declaration_descriptor, Parser* parser, Block* block,
const DeclarationDescriptor* declaration_descriptor,
const DeclarationParsingResult::Declaration* declaration, const DeclarationParsingResult::Declaration* declaration,
ZoneList<const AstRawString*>* names, bool* ok) { ZoneList<const AstRawString*>* names, bool* ok) {
PatternRewriter rewriter; PatternRewriter rewriter;
...@@ -20,7 +21,7 @@ void Parser::PatternRewriter::DeclareAndInitializeVariables( ...@@ -20,7 +21,7 @@ void Parser::PatternRewriter::DeclareAndInitializeVariables(
DCHECK(block->ignore_completion_value()); DCHECK(block->ignore_completion_value());
rewriter.scope_ = declaration_descriptor->scope; rewriter.scope_ = declaration_descriptor->scope;
rewriter.parser_ = declaration_descriptor->parser; rewriter.parser_ = parser;
rewriter.context_ = BINDING; rewriter.context_ = BINDING;
rewriter.pattern_ = declaration->pattern; rewriter.pattern_ = declaration->pattern;
rewriter.initializer_position_ = declaration->initializer_position; rewriter.initializer_position_ = declaration->initializer_position;
......
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