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

Side by Side Diff: src/scopes.cc

Issue 1189743003: [destructuring] Implement parameter pattern matching. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 6 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 unified diff | Download patch
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/v8.h" 5 #include "src/v8.h"
6 6
7 #include "src/accessors.h" 7 #include "src/accessors.h"
8 #include "src/bootstrapper.h" 8 #include "src/bootstrapper.h"
9 #include "src/messages.h" 9 #include "src/messages.h"
10 #include "src/parser.h" 10 #include "src/parser.h"
(...skipping 441 matching lines...) Expand 10 before | Expand all | Expand 10 after
452 for (Scope* scope = this; 452 for (Scope* scope = this;
453 scope != NULL; 453 scope != NULL;
454 scope = scope->outer_scope()) { 454 scope = scope->outer_scope()) {
455 Variable* var = scope->LookupLocal(name); 455 Variable* var = scope->LookupLocal(name);
456 if (var != NULL) return var; 456 if (var != NULL) return var;
457 } 457 }
458 return NULL; 458 return NULL;
459 } 459 }
460 460
461 461
462 Variable* Scope::DeclareParameter(const AstRawString* name, VariableMode mode, 462 Variable* Scope::DeclareParameter(const AstRawString* name, Expression* pattern,
463 bool is_rest, bool* is_duplicate) { 463 VariableMode mode, bool is_rest,
464 bool* is_duplicate) {
464 DCHECK(!already_resolved()); 465 DCHECK(!already_resolved());
465 DCHECK(is_function_scope()); 466 DCHECK(is_function_scope());
466 Variable* var = variables_.Declare(this, name, mode, Variable::NORMAL, 467 Variable* var = variables_.Declare(this, name, mode, Variable::NORMAL,
467 kCreatedInitialized); 468 kCreatedInitialized);
468 if (is_rest) { 469 if (is_rest) {
469 DCHECK_NULL(rest_parameter_); 470 DCHECK_NULL(rest_parameter_);
470 rest_parameter_ = var; 471 rest_parameter_ = var;
471 rest_index_ = num_parameters(); 472 rest_index_ = num_parameters();
472 } 473 }
473 // TODO(wingo): Avoid O(n^2) check. 474
474 *is_duplicate = IsDeclaredParameter(name); 475 if (pattern == nullptr) {
475 params_.Add(var, zone()); 476 // TODO(wingo): Avoid O(n^2) check.
477 *is_duplicate = IsDeclaredParameter(name);
478 }
479 params_.Add(Parameter(var, pattern), zone());
476 return var; 480 return var;
477 } 481 }
478 482
479 483
480 Variable* Scope::DeclareLocal(const AstRawString* name, VariableMode mode, 484 Variable* Scope::DeclareLocal(const AstRawString* name, VariableMode mode,
481 InitializationFlag init_flag, Variable::Kind kind, 485 InitializationFlag init_flag, Variable::Kind kind,
482 MaybeAssignedFlag maybe_assigned_flag, 486 MaybeAssignedFlag maybe_assigned_flag,
483 int declaration_group_start) { 487 int declaration_group_start) {
484 DCHECK(!already_resolved()); 488 DCHECK(!already_resolved());
485 // This function handles VAR, LET, and CONST modes. DYNAMIC variables are 489 // This function handles VAR, LET, and CONST modes. DYNAMIC variables are
(...skipping 405 matching lines...) Expand 10 before | Expand all | Expand 10 after
891 if (!scope_name_->IsEmpty()) { 895 if (!scope_name_->IsEmpty()) {
892 PrintF(" "); 896 PrintF(" ");
893 PrintName(scope_name_); 897 PrintName(scope_name_);
894 } 898 }
895 899
896 // Print parameters, if any. 900 // Print parameters, if any.
897 if (is_function_scope()) { 901 if (is_function_scope()) {
898 PrintF(" ("); 902 PrintF(" (");
899 for (int i = 0; i < params_.length(); i++) { 903 for (int i = 0; i < params_.length(); i++) {
900 if (i > 0) PrintF(", "); 904 if (i > 0) PrintF(", ");
901 PrintName(params_[i]->raw_name()); 905 PrintName(params_[i].var->raw_name());
902 } 906 }
903 PrintF(")"); 907 PrintF(")");
904 } 908 }
905 909
906 PrintF(" { // (%d, %d)\n", start_position(), end_position()); 910 PrintF(" { // (%d, %d)\n", start_position(), end_position());
907 911
908 // Function name, if any (named function literals, only). 912 // Function name, if any (named function literals, only).
909 if (function_ != NULL) { 913 if (function_ != NULL) {
910 Indent(n1, "// (local) function name: "); 914 Indent(n1, "// (local) function name: ");
911 PrintName(function_->proxy()->raw_name()); 915 PrintName(function_->proxy()->raw_name());
(...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after
1330 if (is_script_scope() && IsLexicalVariableMode(var->mode())) return true; 1334 if (is_script_scope() && IsLexicalVariableMode(var->mode())) return true;
1331 return var->has_forced_context_allocation() || 1335 return var->has_forced_context_allocation() ||
1332 scope_calls_eval_ || 1336 scope_calls_eval_ ||
1333 inner_scope_calls_eval_ || 1337 inner_scope_calls_eval_ ||
1334 scope_contains_with_; 1338 scope_contains_with_;
1335 } 1339 }
1336 1340
1337 1341
1338 bool Scope::HasArgumentsParameter(Isolate* isolate) { 1342 bool Scope::HasArgumentsParameter(Isolate* isolate) {
1339 for (int i = 0; i < params_.length(); i++) { 1343 for (int i = 0; i < params_.length(); i++) {
1340 if (params_[i]->name().is_identical_to( 1344 if (params_[i].var->name().is_identical_to(
1341 isolate->factory()->arguments_string())) { 1345 isolate->factory()->arguments_string())) {
1342 return true; 1346 return true;
1343 } 1347 }
1344 } 1348 }
1345 return false; 1349 return false;
1346 } 1350 }
1347 1351
1348 1352
1349 void Scope::AllocateStackSlot(Variable* var) { 1353 void Scope::AllocateStackSlot(Variable* var) {
1350 if (is_block_scope()) { 1354 if (is_block_scope()) {
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
1391 1395
1392 if (rest_parameter_ && !MustAllocate(rest_parameter_)) { 1396 if (rest_parameter_ && !MustAllocate(rest_parameter_)) {
1393 rest_parameter_ = NULL; 1397 rest_parameter_ = NULL;
1394 } 1398 }
1395 1399
1396 // The same parameter may occur multiple times in the parameters_ list. 1400 // The same parameter may occur multiple times in the parameters_ list.
1397 // If it does, and if it is not copied into the context object, it must 1401 // If it does, and if it is not copied into the context object, it must
1398 // receive the highest parameter index for that parameter; thus iteration 1402 // receive the highest parameter index for that parameter; thus iteration
1399 // order is relevant! 1403 // order is relevant!
1400 for (int i = params_.length() - 1; i >= 0; --i) { 1404 for (int i = params_.length() - 1; i >= 0; --i) {
1401 Variable* var = params_[i]; 1405 Variable* var = params_[i].var;
1402 if (var == rest_parameter_) continue; 1406 if (var == rest_parameter_) continue;
1403 1407
1404 DCHECK(var->scope() == this); 1408 DCHECK(var->scope() == this);
1405 if (uses_sloppy_arguments || has_forced_context_allocation()) { 1409 if (uses_sloppy_arguments || has_forced_context_allocation()) {
1406 // Force context allocation of the parameter. 1410 // Force context allocation of the parameter.
1407 var->ForceContextAllocation(); 1411 var->ForceContextAllocation();
1408 } 1412 }
1409 AllocateParameter(var, i); 1413 AllocateParameter(var, i);
1410 } 1414 }
1411 } 1415 }
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
1564 } 1568 }
1565 1569
1566 1570
1567 int Scope::ContextLocalCount() const { 1571 int Scope::ContextLocalCount() const {
1568 if (num_heap_slots() == 0) return 0; 1572 if (num_heap_slots() == 0) return 0;
1569 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - 1573 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS -
1570 (function_ != NULL && function_->proxy()->var()->IsContextSlot() ? 1 : 0); 1574 (function_ != NULL && function_->proxy()->var()->IsContextSlot() ? 1 : 0);
1571 } 1575 }
1572 } // namespace internal 1576 } // namespace internal
1573 } // namespace v8 1577 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698