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

Side by Side Diff: src/scopes.cc

Issue 1127063003: [es6] implement default parameters via desugaring (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: arv's comments 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 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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 // ---------------------------------------------------------------------------- 70 // ----------------------------------------------------------------------------
71 // Implementation of Scope 71 // Implementation of Scope
72 72
73 Scope::Scope(Zone* zone, Scope* outer_scope, ScopeType scope_type, 73 Scope::Scope(Zone* zone, Scope* outer_scope, ScopeType scope_type,
74 AstValueFactory* ast_value_factory, FunctionKind function_kind) 74 AstValueFactory* ast_value_factory, FunctionKind function_kind)
75 : inner_scopes_(4, zone), 75 : inner_scopes_(4, zone),
76 variables_(zone), 76 variables_(zone),
77 internals_(4, zone), 77 internals_(4, zone),
78 temps_(4, zone), 78 temps_(4, zone),
79 params_(4, zone), 79 params_(4, zone),
80 param_positions_(4, zone),
80 unresolved_(16, zone), 81 unresolved_(16, zone),
81 decls_(4, zone), 82 decls_(4, zone),
82 module_descriptor_( 83 module_descriptor_(
83 scope_type == MODULE_SCOPE ? ModuleDescriptor::New(zone) : NULL), 84 scope_type == MODULE_SCOPE ? ModuleDescriptor::New(zone) : NULL),
84 already_resolved_(false), 85 already_resolved_(false),
85 ast_value_factory_(ast_value_factory), 86 ast_value_factory_(ast_value_factory),
86 zone_(zone), 87 zone_(zone),
87 class_declaration_group_start_(-1) { 88 class_declaration_group_start_(-1) {
88 SetDefaults(scope_type, outer_scope, Handle<ScopeInfo>::null(), 89 SetDefaults(scope_type, outer_scope, Handle<ScopeInfo>::null(),
89 function_kind); 90 function_kind);
90 // The outermost scope must be a script scope. 91 // The outermost scope must be a script scope.
91 DCHECK(scope_type == SCRIPT_SCOPE || outer_scope != NULL); 92 DCHECK(scope_type == SCRIPT_SCOPE || outer_scope != NULL);
92 DCHECK(!HasIllegalRedeclaration()); 93 DCHECK(!HasIllegalRedeclaration());
93 } 94 }
94 95
95 96
96 Scope::Scope(Zone* zone, Scope* inner_scope, ScopeType scope_type, 97 Scope::Scope(Zone* zone, Scope* inner_scope, ScopeType scope_type,
97 Handle<ScopeInfo> scope_info, AstValueFactory* value_factory) 98 Handle<ScopeInfo> scope_info, AstValueFactory* value_factory)
98 : inner_scopes_(4, zone), 99 : inner_scopes_(4, zone),
99 variables_(zone), 100 variables_(zone),
100 internals_(4, zone), 101 internals_(4, zone),
101 temps_(4, zone), 102 temps_(4, zone),
102 params_(4, zone), 103 params_(4, zone),
104 param_positions_(4, zone),
103 unresolved_(16, zone), 105 unresolved_(16, zone),
104 decls_(4, zone), 106 decls_(4, zone),
105 module_descriptor_(NULL), 107 module_descriptor_(NULL),
106 already_resolved_(true), 108 already_resolved_(true),
107 ast_value_factory_(value_factory), 109 ast_value_factory_(value_factory),
108 zone_(zone), 110 zone_(zone),
109 class_declaration_group_start_(-1) { 111 class_declaration_group_start_(-1) {
110 SetDefaults(scope_type, NULL, scope_info); 112 SetDefaults(scope_type, NULL, scope_info);
111 if (!scope_info.is_null()) { 113 if (!scope_info.is_null()) {
112 num_heap_slots_ = scope_info_->ContextLength(); 114 num_heap_slots_ = scope_info_->ContextLength();
113 } 115 }
114 // Ensure at least MIN_CONTEXT_SLOTS to indicate a materialized context. 116 // Ensure at least MIN_CONTEXT_SLOTS to indicate a materialized context.
115 num_heap_slots_ = Max(num_heap_slots_, 117 num_heap_slots_ = Max(num_heap_slots_,
116 static_cast<int>(Context::MIN_CONTEXT_SLOTS)); 118 static_cast<int>(Context::MIN_CONTEXT_SLOTS));
117 AddInnerScope(inner_scope); 119 AddInnerScope(inner_scope);
118 } 120 }
119 121
120 122
121 Scope::Scope(Zone* zone, Scope* inner_scope, 123 Scope::Scope(Zone* zone, Scope* inner_scope,
122 const AstRawString* catch_variable_name, 124 const AstRawString* catch_variable_name,
123 AstValueFactory* value_factory) 125 AstValueFactory* value_factory)
124 : inner_scopes_(1, zone), 126 : inner_scopes_(1, zone),
125 variables_(zone), 127 variables_(zone),
126 internals_(0, zone), 128 internals_(0, zone),
127 temps_(0, zone), 129 temps_(0, zone),
128 params_(0, zone), 130 params_(0, zone),
131 param_positions_(0, zone),
129 unresolved_(0, zone), 132 unresolved_(0, zone),
130 decls_(0, zone), 133 decls_(0, zone),
131 module_descriptor_(NULL), 134 module_descriptor_(NULL),
132 already_resolved_(true), 135 already_resolved_(true),
133 ast_value_factory_(value_factory), 136 ast_value_factory_(value_factory),
134 zone_(zone), 137 zone_(zone),
135 class_declaration_group_start_(-1) { 138 class_declaration_group_start_(-1) {
136 SetDefaults(CATCH_SCOPE, NULL, Handle<ScopeInfo>::null()); 139 SetDefaults(CATCH_SCOPE, NULL, Handle<ScopeInfo>::null());
137 AddInnerScope(inner_scope); 140 AddInnerScope(inner_scope);
138 ++num_var_or_const_; 141 ++num_var_or_const_;
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after
442 scope != NULL; 445 scope != NULL;
443 scope = scope->outer_scope()) { 446 scope = scope->outer_scope()) {
444 Variable* var = scope->LookupLocal(name); 447 Variable* var = scope->LookupLocal(name);
445 if (var != NULL) return var; 448 if (var != NULL) return var;
446 } 449 }
447 return NULL; 450 return NULL;
448 } 451 }
449 452
450 453
451 Variable* Scope::DeclareParameter(const AstRawString* name, VariableMode mode, 454 Variable* Scope::DeclareParameter(const AstRawString* name, VariableMode mode,
452 bool is_rest, bool* is_duplicate) { 455 bool is_rest, bool* is_duplicate, int pos) {
453 DCHECK(!already_resolved()); 456 DCHECK(!already_resolved());
454 DCHECK(is_function_scope()); 457 DCHECK(is_function_scope());
455 Variable* var = variables_.Declare(this, name, mode, Variable::NORMAL, 458 Variable* var = variables_.Declare(this, name, mode, Variable::NORMAL,
456 kCreatedInitialized); 459 kCreatedInitialized);
457 if (is_rest) { 460 if (is_rest) {
458 DCHECK_NULL(rest_parameter_); 461 DCHECK_NULL(rest_parameter_);
459 rest_parameter_ = var; 462 rest_parameter_ = var;
460 rest_index_ = num_parameters(); 463 rest_index_ = num_parameters();
461 } 464 }
462 // TODO(wingo): Avoid O(n^2) check. 465 // TODO(wingo): Avoid O(n^2) check.
463 *is_duplicate = IsDeclaredParameter(name); 466 *is_duplicate = IsDeclaredParameter(name);
464 params_.Add(var, zone()); 467 params_.Add(var, zone());
468 param_positions_.Add(pos, zone());
wingo 2015/05/11 15:02:51 Don't we require already that parameters are decla
caitp (gmail) 2015/05/11 15:34:24 It's needed because they end up as lexical declara
465 return var; 469 return var;
466 } 470 }
467 471
468 472
469 Variable* Scope::DeclareLocal(const AstRawString* name, VariableMode mode, 473 Variable* Scope::DeclareLocal(const AstRawString* name, VariableMode mode,
470 InitializationFlag init_flag, Variable::Kind kind, 474 InitializationFlag init_flag, Variable::Kind kind,
471 MaybeAssignedFlag maybe_assigned_flag, 475 MaybeAssignedFlag maybe_assigned_flag,
472 int declaration_group_start) { 476 int declaration_group_start) {
473 DCHECK(!already_resolved()); 477 DCHECK(!already_resolved());
474 // This function handles VAR, LET, and CONST modes. DYNAMIC variables are 478 // This function handles VAR, LET, and CONST modes. DYNAMIC variables are
475 // introduces during variable allocation, INTERNAL variables are allocated 479 // introduces during variable allocation, INTERNAL variables are allocated
476 // explicitly, and TEMPORARY variables are allocated via NewTemporary(). 480 // explicitly, and TEMPORARY variables are allocated via NewTemporary().
477 DCHECK(IsDeclaredVariableMode(mode)); 481 DCHECK(IsDeclaredVariableMode(mode));
478 ++num_var_or_const_; 482 ++num_var_or_const_;
479 return variables_.Declare(this, name, mode, kind, init_flag, 483 return variables_.Declare(this, name, mode, kind, init_flag,
480 maybe_assigned_flag, declaration_group_start); 484 maybe_assigned_flag, declaration_group_start);
481 } 485 }
482 486
483 487
484 Variable* Scope::DeclareDynamicGlobal(const AstRawString* name) { 488 Variable* Scope::DeclareDynamicGlobal(const AstRawString* name) {
485 DCHECK(is_script_scope()); 489 DCHECK(is_script_scope());
486 return variables_.Declare(this, 490 return variables_.Declare(this,
487 name, 491 name,
488 DYNAMIC_GLOBAL, 492 DYNAMIC_GLOBAL,
489 Variable::NORMAL, 493 Variable::NORMAL,
490 kCreatedInitialized); 494 kCreatedInitialized);
491 } 495 }
492 496
493 497
498 void Scope::ShadowParametersForExpressions() {
499 // Parameters have expressions --- ensure that the declared parameters can
500 // never be resolved
501 DCHECK(is_function_scope());
502 for (int i = 0; i < num_parameters(); ++i) {
503 Variable* p = parameter(i);
504 const AstRawString* name = p->raw_name();
505 variables_.Remove(const_cast<AstRawString*>(name), name->hash());
506 }
507 }
508
509
494 void Scope::RemoveUnresolved(VariableProxy* var) { 510 void Scope::RemoveUnresolved(VariableProxy* var) {
495 // Most likely (always?) any variable we want to remove 511 // Most likely (always?) any variable we want to remove
496 // was just added before, so we search backwards. 512 // was just added before, so we search backwards.
497 for (int i = unresolved_.length(); i-- > 0;) { 513 for (int i = unresolved_.length(); i-- > 0;) {
498 if (unresolved_[i] == var) { 514 if (unresolved_[i] == var) {
499 unresolved_.Remove(i); 515 unresolved_.Remove(i);
500 return; 516 return;
501 } 517 }
502 } 518 }
503 } 519 }
(...skipping 1029 matching lines...) Expand 10 before | Expand all | Expand 10 after
1533 (function_ != NULL && function_->proxy()->var()->IsStackLocal() ? 1 : 0); 1549 (function_ != NULL && function_->proxy()->var()->IsStackLocal() ? 1 : 0);
1534 } 1550 }
1535 1551
1536 1552
1537 int Scope::ContextLocalCount() const { 1553 int Scope::ContextLocalCount() const {
1538 if (num_heap_slots() == 0) return 0; 1554 if (num_heap_slots() == 0) return 0;
1539 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - 1555 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS -
1540 (function_ != NULL && function_->proxy()->var()->IsContextSlot() ? 1 : 0); 1556 (function_ != NULL && function_->proxy()->var()->IsContextSlot() ? 1 : 0);
1541 } 1557 }
1542 } } // namespace v8::internal 1558 } } // namespace v8::internal
OLDNEW
« src/preparser.h ('K') | « src/scopes.h ('k') | test/mjsunit/harmony/optional-arguments.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698