Commit dd59ff29 authored by Toon Verwaest's avatar Toon Verwaest Committed by Commit Bot

[parser] Move MakeNonSimpleParameter to DeclarationScope

Walk the VariableMap instead of the ast.

Change-Id: I03ee9145230bcbfe04c5e31dc8d8b3a98a00a4be
Reviewed-on: https://chromium-review.googlesource.com/c/1424865
Commit-Queue: Toon Verwaest <verwaest@chromium.org>
Reviewed-by: 's avatarLeszek Swirski <leszeks@chromium.org>
Cr-Commit-Position: refs/heads/master@{#58968}
parent 337c7730
...@@ -889,6 +889,15 @@ class V8_EXPORT_PRIVATE DeclarationScope : public Scope { ...@@ -889,6 +889,15 @@ class V8_EXPORT_PRIVATE DeclarationScope : public Scope {
has_simple_parameters_ = false; has_simple_parameters_ = false;
} }
void MakeParametersNonSimple() {
SetHasNonSimpleParameters();
for (ZoneHashMap::Entry* p = variables_.Start(); p != nullptr;
p = variables_.Next(p)) {
Variable* var = reinterpret_cast<Variable*>(p->value);
if (var->is_parameter()) var->MakeParameterNonSimple();
}
}
// Returns whether the arguments object aliases formal parameters. // Returns whether the arguments object aliases formal parameters.
CreateArgumentsType GetArgumentsType() const { CreateArgumentsType GetArgumentsType() const {
DCHECK(is_function_scope()); DCHECK(is_function_scope());
......
...@@ -176,7 +176,7 @@ class Variable final : public ZoneObject { ...@@ -176,7 +176,7 @@ class Variable final : public ZoneObject {
index_ = index; index_ = index;
} }
void MakeNonSimpleParameter() { void MakeParameterNonSimple() {
DCHECK(is_parameter()); DCHECK(is_parameter());
bit_field_ = VariableModeField::update(bit_field_, VariableMode::kLet); bit_field_ = VariableModeField::update(bit_field_, VariableMode::kLet);
bit_field_ = bit_field_ =
......
...@@ -867,7 +867,7 @@ class V8_EXPORT_PRIVATE Parser : public NON_EXPORTED_BASE(ParserBase<Parser>) { ...@@ -867,7 +867,7 @@ class V8_EXPORT_PRIVATE Parser : public NON_EXPORTED_BASE(ParserBase<Parser>) {
V8_INLINE void DeclareFormalParameters(ParserFormalParameters* parameters) { V8_INLINE void DeclareFormalParameters(ParserFormalParameters* parameters) {
bool is_simple = parameters->is_simple; bool is_simple = parameters->is_simple;
DeclarationScope* scope = parameters->scope; DeclarationScope* scope = parameters->scope;
if (!is_simple) scope->SetHasNonSimpleParameters(); if (!is_simple) scope->MakeParametersNonSimple();
for (auto parameter : parameters->params) { for (auto parameter : parameters->params) {
bool is_optional = parameter->initializer() != nullptr; bool is_optional = parameter->initializer() != nullptr;
// If the parameter list is simple, declare the parameters normally with // If the parameter list is simple, declare the parameters normally with
......
...@@ -34,11 +34,9 @@ class PatternRewriter final : public AstVisitor<PatternRewriter> { ...@@ -34,11 +34,9 @@ class PatternRewriter final : public AstVisitor<PatternRewriter> {
const Parser::DeclarationParsingResult::Declaration* declaration); const Parser::DeclarationParsingResult::Declaration* declaration);
private: private:
PatternRewriter(Parser* parser, VariableKind kind, PatternRewriter(Parser* parser, VariableKind kind, int initializer_position,
int initializer_position = kNoSourcePosition, bool declares_parameter_containing_sloppy_eval)
bool declares_parameter_containing_sloppy_eval = false)
: parser_(parser), : parser_(parser),
kind_(kind),
initializer_position_(initializer_position), initializer_position_(initializer_position),
declares_parameter_containing_sloppy_eval_( declares_parameter_containing_sloppy_eval_(
declares_parameter_containing_sloppy_eval) {} declares_parameter_containing_sloppy_eval) {}
...@@ -48,8 +46,6 @@ class PatternRewriter final : public AstVisitor<PatternRewriter> { ...@@ -48,8 +46,6 @@ class PatternRewriter final : public AstVisitor<PatternRewriter> {
AST_NODE_LIST(DECLARE_VISIT) AST_NODE_LIST(DECLARE_VISIT)
#undef DECLARE_VISIT #undef DECLARE_VISIT
void RecurseIntoSubpattern(AstNode* pattern) { Visit(pattern); }
Expression* Visit(Assignment* assign) { Expression* Visit(Assignment* assign) {
if (parser_->has_error()) return parser_->FailureExpression(); if (parser_->has_error()) return parser_->FailureExpression();
DCHECK_EQ(Token::ASSIGN, assign->op()); DCHECK_EQ(Token::ASSIGN, assign->op());
...@@ -69,7 +65,6 @@ class PatternRewriter final : public AstVisitor<PatternRewriter> { ...@@ -69,7 +65,6 @@ class PatternRewriter final : public AstVisitor<PatternRewriter> {
Scope* scope() const { return parser_->scope(); } Scope* scope() const { return parser_->scope(); }
Parser* const parser_; Parser* const parser_;
VariableKind kind_;
const int initializer_position_; const int initializer_position_;
const bool declares_parameter_containing_sloppy_eval_; const bool declares_parameter_containing_sloppy_eval_;
...@@ -80,17 +75,22 @@ void Parser::InitializeVariables( ...@@ -80,17 +75,22 @@ void Parser::InitializeVariables(
ScopedPtrList<Statement>* statements, VariableKind kind, ScopedPtrList<Statement>* statements, VariableKind kind,
const DeclarationParsingResult::Declaration* declaration) { const DeclarationParsingResult::Declaration* declaration) {
if (has_error()) return; if (has_error()) return;
PatternRewriter::InitializeVariables(this, kind, declaration);
if (declaration->initializer) { if (!declaration->initializer) {
int pos = declaration->value_beg_position; // The parameter scope is only a block scope if the initializer calls sloppy
if (pos == kNoSourcePosition) { // eval. Since there is no initializer, we can't be calling sloppy eval.
pos = declaration->initializer_position; DCHECK_IMPLIES(kind == PARAMETER_VARIABLE, scope()->is_function_scope());
} return;
Assignment* assignment = factory()->NewAssignment(
Token::INIT, declaration->pattern, declaration->initializer, pos);
statements->Add(factory()->NewExpressionStatement(assignment, pos));
} }
PatternRewriter::InitializeVariables(this, kind, declaration);
int pos = declaration->value_beg_position;
if (pos == kNoSourcePosition) {
pos = declaration->initializer_position;
}
Assignment* assignment = factory()->NewAssignment(
Token::INIT, declaration->pattern, declaration->initializer, pos);
statements->Add(factory()->NewExpressionStatement(assignment, pos));
} }
void PatternRewriter::InitializeVariables( void PatternRewriter::InitializeVariables(
...@@ -100,7 +100,7 @@ void PatternRewriter::InitializeVariables( ...@@ -100,7 +100,7 @@ void PatternRewriter::InitializeVariables(
parser, kind, declaration->initializer_position, parser, kind, declaration->initializer_position,
kind == PARAMETER_VARIABLE && parser->scope()->is_block_scope()); kind == PARAMETER_VARIABLE && parser->scope()->is_block_scope());
rewriter.RecurseIntoSubpattern(declaration->pattern); rewriter.Visit(declaration->pattern);
} }
void PatternRewriter::VisitVariableProxy(VariableProxy* proxy) { void PatternRewriter::VisitVariableProxy(VariableProxy* proxy) {
...@@ -110,11 +110,8 @@ void PatternRewriter::VisitVariableProxy(VariableProxy* proxy) { ...@@ -110,11 +110,8 @@ void PatternRewriter::VisitVariableProxy(VariableProxy* proxy) {
? proxy->var() ? proxy->var()
: scope()->GetDeclarationScope()->LookupLocal(proxy->raw_name()); : scope()->GetDeclarationScope()->LookupLocal(proxy->raw_name());
// TODO(verwaest): Use ScopedPtrList of Variable(Proxy?) in the
// ExpressionScope instead.
if (kind_ == PARAMETER_VARIABLE) var->MakeNonSimpleParameter();
DCHECK_NOT_NULL(var); DCHECK_NOT_NULL(var);
DCHECK_NE(initializer_position_, kNoSourcePosition); DCHECK_NE(initializer_position_, kNoSourcePosition);
var->set_initializer_position(initializer_position_); var->set_initializer_position(initializer_position_);
} }
...@@ -137,14 +134,14 @@ void PatternRewriter::VisitObjectLiteral(ObjectLiteral* pattern) { ...@@ -137,14 +134,14 @@ void PatternRewriter::VisitObjectLiteral(ObjectLiteral* pattern) {
// scope rewriting. // scope rewriting.
RewriteParameterScopes(key); RewriteParameterScopes(key);
} }
RecurseIntoSubpattern(property->value()); Visit(property->value());
} }
} }
void PatternRewriter::VisitArrayLiteral(ArrayLiteral* node) { void PatternRewriter::VisitArrayLiteral(ArrayLiteral* node) {
for (Expression* value : *node->values()) { for (Expression* value : *node->values()) {
if (value->IsTheHoleLiteral()) continue; if (value->IsTheHoleLiteral()) continue;
RecurseIntoSubpattern(value); Visit(value);
} }
} }
...@@ -154,12 +151,10 @@ void PatternRewriter::VisitAssignment(Assignment* node) { ...@@ -154,12 +151,10 @@ void PatternRewriter::VisitAssignment(Assignment* node) {
// Initializer may have been parsed in the wrong scope. // Initializer may have been parsed in the wrong scope.
RewriteParameterScopes(node->value()); RewriteParameterScopes(node->value());
RecurseIntoSubpattern(node->target()); Visit(node->target());
} }
void PatternRewriter::VisitSpread(Spread* node) { void PatternRewriter::VisitSpread(Spread* node) { Visit(node->expression()); }
RecurseIntoSubpattern(node->expression());
}
// =============== UNREACHABLE ============================= // =============== UNREACHABLE =============================
......
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