Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(972)

Unified Diff: src/pattern-rewriter.cc

Issue 1128043006: [destructuring] Adapting PatternRewriter to work in for-statements. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« src/parser.cc ('K') | « src/pattern-rewriter.h ('k') | tools/gyp/v8.gyp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/pattern-rewriter.cc
diff --git a/src/pattern-rewriter.cc b/src/pattern-rewriter.cc
index be14754b57864c7edf08858093030fdd2a8341b9..7ba01936c96993156d69aefbcdd117dad66652c3 100644
--- a/src/pattern-rewriter.cc
+++ b/src/pattern-rewriter.cc
@@ -4,7 +4,6 @@
#include "src/ast.h"
#include "src/parser.h"
-#include "src/pattern-rewriter.h"
namespace v8 {
@@ -22,20 +21,24 @@ const AstRawString* Parser::PatternRewriter::SingleName() const {
}
-void Parser::PatternRewriter::DeclareAndInitializeVariables(Expression* value,
- int* nvars,
- bool* ok) {
- ok_ = ok;
- nvars_ = nvars;
+void Parser::PatternRewriter::DeclareAndInitializeVariables(
+ Block* block, const DeclarationDescriptor* decl, Expression* value,
+ ZoneList<const AstRawString*>* names, bool* ok) {
+ VisitorState state;
arv (Not doing code reviews) 2015/05/13 16:29:29 or maybe: VisitorState state(block, decl, names,
Dmitry Lomov (no reviews) 2015/05/14 16:28:00 State removed.
+ state.block = block;
+ state.decl = decl;
+ state.names = names;
+ state.ok = ok;
+
+ state_ = &state;
RecurseIntoSubpattern(pattern_, value);
- ok_ = nullptr;
- nvars_ = nullptr;
+ state_ = nullptr;
}
void Parser::PatternRewriter::VisitVariableProxy(VariableProxy* pattern) {
- Expression* value = current_value_;
- decl_->scope->RemoveUnresolved(pattern->AsVariableProxy());
+ Expression* value = state_->current_value;
+ state_->decl->scope->RemoveUnresolved(pattern->AsVariableProxy());
// Declare variable.
// Note that we *always* must treat the initial value via a separate init
@@ -52,24 +55,28 @@ void Parser::PatternRewriter::VisitVariableProxy(VariableProxy* pattern) {
// For let/const declarations in harmony mode, we can also immediately
// pre-resolve the proxy because it resides in the same scope as the
// declaration.
- Parser* parser = decl_->parser;
+ Parser* parser = state_->decl->parser;
const AstRawString* name = pattern->raw_name();
- VariableProxy* proxy = parser->NewUnresolved(name, decl_->mode);
+ VariableProxy* proxy = parser->NewUnresolved(name, state_->decl->mode);
Declaration* declaration = factory()->NewVariableDeclaration(
- proxy, decl_->mode, decl_->scope, decl_->pos);
- Variable* var = parser->Declare(declaration, decl_->mode != VAR, ok_);
- if (!*ok_) return;
+ proxy, state_->decl->mode, state_->decl->scope, state_->decl->pos);
+ Variable* var =
+ parser->Declare(declaration, state_->decl->mode != VAR, state_->ok);
+ if (!*state_->ok) return;
DCHECK_NOT_NULL(var);
DCHECK(!proxy->is_resolved() || proxy->var() == var);
- var->set_initializer_position(decl_->initializer_position);
- (*nvars_)++;
- if (decl_->declaration_scope->num_var_or_const() > kMaxNumFunctionLocals) {
+ var->set_initializer_position(initializer_position_);
+
+ DCHECK(initializer_position_ != RelocInfo::kNoPosition);
+
+ if (state_->decl->declaration_scope->num_var_or_const() >
+ kMaxNumFunctionLocals) {
parser->ReportMessage("too_many_variables");
- *ok_ = false;
+ *state_->ok = false;
return;
}
- if (decl_->names) {
- decl_->names->Add(name, zone());
+ if (state_->names) {
+ state_->names->Add(name, zone());
}
// Initialize variables if needed. A
@@ -98,8 +105,9 @@ void Parser::PatternRewriter::VisitVariableProxy(VariableProxy* pattern) {
// The "variable" c initialized to x is the same as the declared
// one - there is no re-lookup (see the last parameter of the
// Declare() call above).
- Scope* initialization_scope =
- decl_->is_const ? decl_->declaration_scope : decl_->scope;
+ Scope* initialization_scope = state_->decl->is_const
+ ? state_->decl->declaration_scope
+ : state_->decl->scope;
// Global variable declarations must be compiled in a specific
@@ -121,16 +129,17 @@ void Parser::PatternRewriter::VisitVariableProxy(VariableProxy* pattern) {
// browsers where the global object (window) has lots of
// properties defined in prototype objects.
if (initialization_scope->is_script_scope() &&
- !IsLexicalVariableMode(decl_->mode)) {
+ !IsLexicalVariableMode(state_->decl->mode)) {
// Compute the arguments for the runtime
// call.test-parsing/InitializedDeclarationsInStrictForOfError
ZoneList<Expression*>* arguments =
new (zone()) ZoneList<Expression*>(3, zone());
// We have at least 1 parameter.
- arguments->Add(factory()->NewStringLiteral(name, decl_->pos), zone());
+ arguments->Add(factory()->NewStringLiteral(name, state_->decl->pos),
+ zone());
CallRuntime* initialize;
- if (decl_->is_const) {
+ if (state_->decl->is_const) {
arguments->Add(value, zone());
value = NULL; // zap the value to avoid the unnecessary assignment
@@ -141,13 +150,14 @@ void Parser::PatternRewriter::VisitVariableProxy(VariableProxy* pattern) {
initialize = factory()->NewCallRuntime(
ast_value_factory()->initialize_const_global_string(),
Runtime::FunctionForId(Runtime::kInitializeConstGlobal), arguments,
- decl_->pos);
+ state_->decl->pos);
} else {
// Add language mode.
// We may want to pass singleton to avoid Literal allocations.
LanguageMode language_mode = initialization_scope->language_mode();
- arguments->Add(factory()->NewNumberLiteral(language_mode, decl_->pos),
- zone());
+ arguments->Add(
+ factory()->NewNumberLiteral(language_mode, state_->decl->pos),
+ zone());
// Be careful not to assign a value to the global variable if
// we're in a with. The initialization value should not
@@ -161,18 +171,19 @@ void Parser::PatternRewriter::VisitVariableProxy(VariableProxy* pattern) {
initialize = factory()->NewCallRuntime(
ast_value_factory()->initialize_var_global_string(),
Runtime::FunctionForId(Runtime::kInitializeVarGlobal), arguments,
- decl_->pos);
+ state_->decl->pos);
} else {
initialize = NULL;
}
}
if (initialize != NULL) {
- decl_->block->AddStatement(
+ state_->block->AddStatement(
factory()->NewExpressionStatement(initialize, RelocInfo::kNoPosition),
zone());
}
- } else if (decl_->needs_init) {
+ } else if (value != nullptr && (state_->decl->needs_init ||
+ IsLexicalVariableMode(state_->decl->mode))) {
// Constant initializations always assign to the declared constant which
// is always at the function scope level. This is only relevant for
// dynamically looked-up variables and constants (the
@@ -183,9 +194,9 @@ void Parser::PatternRewriter::VisitVariableProxy(VariableProxy* pattern) {
DCHECK_NOT_NULL(proxy);
DCHECK_NOT_NULL(proxy->var());
DCHECK_NOT_NULL(value);
- Assignment* assignment =
- factory()->NewAssignment(decl_->init_op, proxy, value, decl_->pos);
- decl_->block->AddStatement(
+ Assignment* assignment = factory()->NewAssignment(
+ state_->decl->init_op, proxy, value, state_->decl->pos);
+ state_->block->AddStatement(
factory()->NewExpressionStatement(assignment, RelocInfo::kNoPosition),
zone());
value = NULL;
@@ -194,14 +205,14 @@ void Parser::PatternRewriter::VisitVariableProxy(VariableProxy* pattern) {
// Add an assignment node to the initialization statement block if we still
// have a pending initialization value.
if (value != NULL) {
- DCHECK(decl_->mode == VAR);
+ DCHECK(state_->decl->mode == VAR);
// 'var' initializations are simply assignments (with all the consequences
// if they are inside a 'with' statement - they may change a 'with' object
// property).
VariableProxy* proxy = initialization_scope->NewUnresolved(factory(), name);
- Assignment* assignment =
- factory()->NewAssignment(decl_->init_op, proxy, value, decl_->pos);
- decl_->block->AddStatement(
+ Assignment* assignment = factory()->NewAssignment(
+ state_->decl->init_op, proxy, value, state_->decl->pos);
+ state_->block->AddStatement(
factory()->NewExpressionStatement(assignment, RelocInfo::kNoPosition),
zone());
}
@@ -209,12 +220,12 @@ void Parser::PatternRewriter::VisitVariableProxy(VariableProxy* pattern) {
void Parser::PatternRewriter::VisitObjectLiteral(ObjectLiteral* pattern) {
- auto temp = decl_->declaration_scope->NewTemporary(
+ auto temp = state_->decl->declaration_scope->NewTemporary(
ast_value_factory()->empty_string());
auto assignment =
factory()->NewAssignment(Token::ASSIGN, factory()->NewVariableProxy(temp),
- current_value_, RelocInfo::kNoPosition);
- decl_->block->AddStatement(
+ state_->current_value, RelocInfo::kNoPosition);
+ state_->block->AddStatement(
factory()->NewExpressionStatement(assignment, RelocInfo::kNoPosition),
zone());
for (ObjectLiteralProperty* property : *pattern->properties()) {
« src/parser.cc ('K') | « src/pattern-rewriter.h ('k') | tools/gyp/v8.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698