OLD | NEW |
---|---|
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 Loading... | |
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 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
457 scope != NULL; | 460 scope != NULL; |
458 scope = scope->outer_scope()) { | 461 scope = scope->outer_scope()) { |
459 Variable* var = scope->LookupLocal(name); | 462 Variable* var = scope->LookupLocal(name); |
460 if (var != NULL) return var; | 463 if (var != NULL) return var; |
461 } | 464 } |
462 return NULL; | 465 return NULL; |
463 } | 466 } |
464 | 467 |
465 | 468 |
466 Variable* Scope::DeclareParameter(const AstRawString* name, VariableMode mode, | 469 Variable* Scope::DeclareParameter(const AstRawString* name, VariableMode mode, |
467 bool is_rest, bool* is_duplicate) { | 470 bool is_rest, bool* is_duplicate, int pos) { |
468 DCHECK(!already_resolved()); | 471 DCHECK(!already_resolved()); |
469 DCHECK(is_function_scope()); | 472 DCHECK(is_function_scope()); |
470 Variable* var = variables_.Declare(this, name, mode, Variable::NORMAL, | 473 Variable* var = variables_.Declare(this, name, mode, Variable::NORMAL, |
471 kCreatedInitialized); | 474 kCreatedInitialized); |
472 if (is_rest) { | 475 if (is_rest) { |
473 DCHECK_NULL(rest_parameter_); | 476 DCHECK_NULL(rest_parameter_); |
474 rest_parameter_ = var; | 477 rest_parameter_ = var; |
475 rest_index_ = num_parameters(); | 478 rest_index_ = num_parameters(); |
476 } | 479 } |
477 // TODO(wingo): Avoid O(n^2) check. | 480 // TODO(wingo): Avoid O(n^2) check. |
478 *is_duplicate = IsDeclaredParameter(name); | 481 *is_duplicate = IsDeclaredParameter(name); |
479 params_.Add(var, zone()); | 482 params_.Add(var, zone()); |
483 param_positions_.Add(pos, zone()); | |
480 return var; | 484 return var; |
481 } | 485 } |
482 | 486 |
483 | 487 |
484 Variable* Scope::DeclareLocal(const AstRawString* name, VariableMode mode, | 488 Variable* Scope::DeclareLocal(const AstRawString* name, VariableMode mode, |
485 InitializationFlag init_flag, Variable::Kind kind, | 489 InitializationFlag init_flag, Variable::Kind kind, |
486 MaybeAssignedFlag maybe_assigned_flag, | 490 MaybeAssignedFlag maybe_assigned_flag, |
487 int declaration_group_start) { | 491 int declaration_group_start) { |
488 DCHECK(!already_resolved()); | 492 DCHECK(!already_resolved()); |
489 // This function handles VAR, LET, and CONST modes. DYNAMIC variables are | 493 // This function handles VAR, LET, and CONST modes. DYNAMIC variables are |
490 // introduces during variable allocation, INTERNAL variables are allocated | 494 // introduces during variable allocation, INTERNAL variables are allocated |
491 // explicitly, and TEMPORARY variables are allocated via NewTemporary(). | 495 // explicitly, and TEMPORARY variables are allocated via NewTemporary(). |
492 DCHECK(IsDeclaredVariableMode(mode)); | 496 DCHECK(IsDeclaredVariableMode(mode)); |
493 ++num_var_or_const_; | 497 ++num_var_or_const_; |
494 return variables_.Declare(this, name, mode, kind, init_flag, | 498 return variables_.Declare(this, name, mode, kind, init_flag, |
495 maybe_assigned_flag, declaration_group_start); | 499 maybe_assigned_flag, declaration_group_start); |
496 } | 500 } |
497 | 501 |
498 | 502 |
499 Variable* Scope::DeclareDynamicGlobal(const AstRawString* name) { | 503 Variable* Scope::DeclareDynamicGlobal(const AstRawString* name) { |
500 DCHECK(is_script_scope()); | 504 DCHECK(is_script_scope()); |
501 return variables_.Declare(this, | 505 return variables_.Declare(this, |
502 name, | 506 name, |
503 DYNAMIC_GLOBAL, | 507 DYNAMIC_GLOBAL, |
504 Variable::NORMAL, | 508 Variable::NORMAL, |
505 kCreatedInitialized); | 509 kCreatedInitialized); |
506 } | 510 } |
507 | 511 |
508 | 512 |
513 void Scope::ShadowParametersForExpressions() { | |
rossberg
2015/05/12 14:04:53
Instead of spreading this out into a different fun
caitp (gmail)
2015/05/12 14:34:17
well the scope VariableMap is private, so it's har
rossberg
2015/05/20 07:41:47
Well, this whole removing variables from scope thi
caitp (gmail)
2015/05/20 11:29:38
I can try it
caitp (gmail)
2015/05/20 14:55:33
I'm not totally sure what needs to change.
Right
| |
514 // Parameters have expressions --- ensure that the declared parameters can | |
515 // never be resolved | |
516 DCHECK(is_function_scope()); | |
517 for (int i = 0; i < num_parameters(); ++i) { | |
518 Variable* p = parameter(i); | |
519 const AstRawString* name = p->raw_name(); | |
520 variables_.Remove(const_cast<AstRawString*>(name), name->hash()); | |
521 } | |
522 } | |
523 | |
524 | |
509 void Scope::RemoveUnresolved(VariableProxy* var) { | 525 void Scope::RemoveUnresolved(VariableProxy* var) { |
510 // Most likely (always?) any variable we want to remove | 526 // Most likely (always?) any variable we want to remove |
511 // was just added before, so we search backwards. | 527 // was just added before, so we search backwards. |
512 for (int i = unresolved_.length(); i-- > 0;) { | 528 for (int i = unresolved_.length(); i-- > 0;) { |
513 if (unresolved_[i] == var) { | 529 if (unresolved_[i] == var) { |
514 unresolved_.Remove(i); | 530 unresolved_.Remove(i); |
515 return; | 531 return; |
516 } | 532 } |
517 } | 533 } |
518 } | 534 } |
(...skipping 1024 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1543 (function_ != NULL && function_->proxy()->var()->IsStackLocal() ? 1 : 0); | 1559 (function_ != NULL && function_->proxy()->var()->IsStackLocal() ? 1 : 0); |
1544 } | 1560 } |
1545 | 1561 |
1546 | 1562 |
1547 int Scope::ContextLocalCount() const { | 1563 int Scope::ContextLocalCount() const { |
1548 if (num_heap_slots() == 0) return 0; | 1564 if (num_heap_slots() == 0) return 0; |
1549 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - | 1565 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - |
1550 (function_ != NULL && function_->proxy()->var()->IsContextSlot() ? 1 : 0); | 1566 (function_ != NULL && function_->proxy()->var()->IsContextSlot() ? 1 : 0); |
1551 } | 1567 } |
1552 } } // namespace v8::internal | 1568 } } // namespace v8::internal |
OLD | NEW |