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

Side by Side Diff: src/scopes.h

Issue 883823002: Implement proper scoping for "this" in arrow functions Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Make sure an unresolved VariableProxy for "this" is not considered a valid LHS Created 5 years, 10 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 #ifndef V8_SCOPES_H_ 5 #ifndef V8_SCOPES_H_
6 #define V8_SCOPES_H_ 6 #define V8_SCOPES_H_
7 7
8 #include "src/ast.h" 8 #include "src/ast.h"
9 #include "src/zone.h" 9 #include "src/zone.h"
10 10
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 // Declare an implicit global variable in this scope which must be a 138 // Declare an implicit global variable in this scope which must be a
139 // script scope. The variable was introduced (possibly from an inner 139 // script scope. The variable was introduced (possibly from an inner
140 // scope) by a reference to an unresolved variable with no intervening 140 // scope) by a reference to an unresolved variable with no intervening
141 // with statements or eval calls. 141 // with statements or eval calls.
142 Variable* DeclareDynamicGlobal(const AstRawString* name); 142 Variable* DeclareDynamicGlobal(const AstRawString* name);
143 143
144 // Create a new unresolved variable. 144 // Create a new unresolved variable.
145 VariableProxy* NewUnresolved(AstNodeFactory* factory, 145 VariableProxy* NewUnresolved(AstNodeFactory* factory,
146 const AstRawString* name, 146 const AstRawString* name,
147 Interface* interface = Interface::NewValue(), 147 Interface* interface = Interface::NewValue(),
148 int position = RelocInfo::kNoPosition) { 148 int position = RelocInfo::kNoPosition,
149 bool is_this = false) {
wingo 2015/02/04 10:02:13 Boolean parameters are an antipattern, especially
aperez 2015/02/04 21:00:42 Acknowledged. Even better: the actual name of the
149 // Note that we must not share the unresolved variables with 150 // Note that we must not share the unresolved variables with
150 // the same name because they may be removed selectively via 151 // the same name because they may be removed selectively via
151 // RemoveUnresolved(). 152 // RemoveUnresolved().
152 DCHECK(!already_resolved()); 153 DCHECK(!already_resolved());
153 VariableProxy* proxy = 154 VariableProxy* proxy =
154 factory->NewVariableProxy(name, false, interface, position); 155 factory->NewVariableProxy(name, is_this, interface, position);
155 unresolved_.Add(proxy, zone_); 156 unresolved_.Add(proxy, zone_);
156 return proxy; 157 return proxy;
157 } 158 }
158 159
159 // Remove a unresolved variable. During parsing, an unresolved variable 160 // Remove a unresolved variable. During parsing, an unresolved variable
160 // may have been added optimistically, but then only the variable name 161 // may have been added optimistically, but then only the variable name
161 // was used (typically for labels). If the variable was not declared, the 162 // was used (typically for labels). If the variable was not declared, the
162 // addition introduced a new unresolved variable which may end up being 163 // addition introduced a new unresolved variable which may end up being
163 // allocated globally as a "ghost" variable. RemoveUnresolved removes 164 // allocated globally as a "ghost" variable. RemoveUnresolved removes
164 // such a variable again if it was added; otherwise this is a no-op. 165 // such a variable again if it was added; otherwise this is a no-op.
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 void RecordArgumentsUsage() { scope_uses_arguments_ = true; } 216 void RecordArgumentsUsage() { scope_uses_arguments_ = true; }
216 217
217 // Inform the scope that the corresponding code uses "super". 218 // Inform the scope that the corresponding code uses "super".
218 void RecordSuperPropertyUsage() { scope_uses_super_property_ = true; } 219 void RecordSuperPropertyUsage() { scope_uses_super_property_ = true; }
219 220
220 // Inform the scope that the corresponding code invokes "super" constructor. 221 // Inform the scope that the corresponding code invokes "super" constructor.
221 void RecordSuperConstructorCallUsage() { 222 void RecordSuperConstructorCallUsage() {
222 scope_uses_super_constructor_call_ = true; 223 scope_uses_super_constructor_call_ = true;
223 } 224 }
224 225
225 // Inform the scope that the corresponding code uses "this".
226 void RecordThisUsage() { scope_uses_this_ = true; }
227
228 // Set the strict mode flag (unless disabled by a global flag). 226 // Set the strict mode flag (unless disabled by a global flag).
229 void SetStrictMode(StrictMode strict_mode) { strict_mode_ = strict_mode; } 227 void SetStrictMode(StrictMode strict_mode) { strict_mode_ = strict_mode; }
230 228
231 // Set the ASM module flag. 229 // Set the ASM module flag.
232 void SetAsmModule() { asm_module_ = true; } 230 void SetAsmModule() { asm_module_ = true; }
233 231
234 // Position in the source where this scope begins and ends. 232 // Position in the source where this scope begins and ends.
235 // 233 //
236 // * For the scope of a with statement 234 // * For the scope of a with statement
237 // with (obj) stmt 235 // with (obj) stmt
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
321 } 319 }
322 // Does this scope calls "super" constructor. 320 // Does this scope calls "super" constructor.
323 bool uses_super_constructor_call() const { 321 bool uses_super_constructor_call() const {
324 return scope_uses_super_constructor_call_; 322 return scope_uses_super_constructor_call_;
325 } 323 }
326 // Does any inner scope calls "super" constructor. 324 // Does any inner scope calls "super" constructor.
327 bool inner_uses_super_constructor_call() const { 325 bool inner_uses_super_constructor_call() const {
328 return inner_scope_uses_super_constructor_call_; 326 return inner_scope_uses_super_constructor_call_;
329 } 327 }
330 // Does this scope access "this". 328 // Does this scope access "this".
331 bool uses_this() const { return scope_uses_this_; } 329 bool uses_this() const { return receiver_->is_used(); }
332 // Does any inner scope access "this". 330 // Does any inner scope access "this".
333 bool inner_uses_this() const { return inner_scope_uses_this_; }
334 331
335 // --------------------------------------------------------------------------- 332 // ---------------------------------------------------------------------------
336 // Accessors. 333 // Accessors.
337 334
338 // The type of this scope. 335 // The type of this scope.
339 ScopeType scope_type() const { return scope_type_; } 336 ScopeType scope_type() const { return scope_type_; }
340 337
341 // The language mode of this scope. 338 // The language mode of this scope.
342 StrictMode strict_mode() const { return strict_mode_; } 339 StrictMode strict_mode() const { return strict_mode_; }
343 340
344 // The variable corresponding the 'this' value. 341 // The variable corresponding the 'this' value.
345 Variable* receiver() { return receiver_; } 342 Variable* receiver() {
343 DCHECK(has_this_declaration());
wingo 2015/02/04 10:02:13 DCHECK(receiver_) also perhaps?
aperez 2015/02/04 21:00:42 Acknowledged.
344 return receiver_;
345 }
346
347 bool has_this_declaration() const {
348 return !is_arrow_scope() && is_declaration_scope();
349 }
346 350
347 // The variable holding the function literal for named function 351 // The variable holding the function literal for named function
348 // literals, or NULL. Only valid for function scopes. 352 // literals, or NULL. Only valid for function scopes.
349 VariableDeclaration* function() const { 353 VariableDeclaration* function() const {
350 DCHECK(is_function_scope()); 354 DCHECK(is_function_scope());
351 return function_; 355 return function_;
352 } 356 }
353 357
354 // Parameters. The left-most parameter has index 0. 358 // Parameters. The left-most parameter has index 0.
355 // Only valid for function scopes. 359 // Only valid for function scopes.
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
509 bool scope_contains_with_; 513 bool scope_contains_with_;
510 // This scope or a nested catch scope or with scope contain an 'eval' call. At 514 // This scope or a nested catch scope or with scope contain an 'eval' call. At
511 // the 'eval' call site this scope is the declaration scope. 515 // the 'eval' call site this scope is the declaration scope.
512 bool scope_calls_eval_; 516 bool scope_calls_eval_;
513 // This scope uses "arguments". 517 // This scope uses "arguments".
514 bool scope_uses_arguments_; 518 bool scope_uses_arguments_;
515 // This scope uses "super" property ('super.foo'). 519 // This scope uses "super" property ('super.foo').
516 bool scope_uses_super_property_; 520 bool scope_uses_super_property_;
517 // This scope uses "super" constructor ('super(..)'). 521 // This scope uses "super" constructor ('super(..)').
518 bool scope_uses_super_constructor_call_; 522 bool scope_uses_super_constructor_call_;
519 // This scope uses "this".
520 bool scope_uses_this_;
521 // This scope contains an "use asm" annotation. 523 // This scope contains an "use asm" annotation.
522 bool asm_module_; 524 bool asm_module_;
523 // This scope's outer context is an asm module. 525 // This scope's outer context is an asm module.
524 bool asm_function_; 526 bool asm_function_;
525 // The strict mode of this scope. 527 // The strict mode of this scope.
526 StrictMode strict_mode_; 528 StrictMode strict_mode_;
527 // Source positions. 529 // Source positions.
528 int start_position_; 530 int start_position_;
529 int end_position_; 531 int end_position_;
530 532
531 // Computed via PropagateScopeInfo. 533 // Computed via PropagateScopeInfo.
532 bool outer_scope_calls_sloppy_eval_; 534 bool outer_scope_calls_sloppy_eval_;
533 bool inner_scope_calls_eval_; 535 bool inner_scope_calls_eval_;
534 bool inner_scope_uses_arguments_; 536 bool inner_scope_uses_arguments_;
535 bool inner_scope_uses_super_property_; 537 bool inner_scope_uses_super_property_;
536 bool inner_scope_uses_super_constructor_call_; 538 bool inner_scope_uses_super_constructor_call_;
537 bool inner_scope_uses_this_;
538 bool force_eager_compilation_; 539 bool force_eager_compilation_;
539 bool force_context_allocation_; 540 bool force_context_allocation_;
540 541
541 // True if it doesn't need scope resolution (e.g., if the scope was 542 // True if it doesn't need scope resolution (e.g., if the scope was
542 // constructed based on a serialized scope info or a catch context). 543 // constructed based on a serialized scope info or a catch context).
543 bool already_resolved_; 544 bool already_resolved_;
544 545
545 // Computed as variables are declared. 546 // Computed as variables are declared.
546 int num_var_or_const_; 547 int num_var_or_const_;
547 548
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
627 628
628 // Predicates. 629 // Predicates.
629 bool MustAllocate(Variable* var); 630 bool MustAllocate(Variable* var);
630 bool MustAllocateInContext(Variable* var); 631 bool MustAllocateInContext(Variable* var);
631 bool HasArgumentsParameter(); 632 bool HasArgumentsParameter();
632 633
633 // Variable allocation. 634 // Variable allocation.
634 void AllocateStackSlot(Variable* var); 635 void AllocateStackSlot(Variable* var);
635 void AllocateHeapSlot(Variable* var); 636 void AllocateHeapSlot(Variable* var);
636 void AllocateParameterLocals(); 637 void AllocateParameterLocals();
638 void AllocateParameter(Variable* var, int index);
639 void AllocateReceiver();
637 void AllocateNonParameterLocal(Variable* var); 640 void AllocateNonParameterLocal(Variable* var);
638 void AllocateNonParameterLocals(); 641 void AllocateNonParameterLocals();
639 void AllocateVariablesRecursively(); 642 void AllocateVariablesRecursively();
640 void AllocateModulesRecursively(Scope* host_scope); 643 void AllocateModulesRecursively(Scope* host_scope);
641 644
642 // Resolve and fill in the allocation information for all variables 645 // Resolve and fill in the allocation information for all variables
643 // in this scopes. Must be called *after* all scopes have been 646 // in this scopes. Must be called *after* all scopes have been
644 // processed (parsed) to ensure that unresolved variables can be 647 // processed (parsed) to ensure that unresolved variables can be
645 // resolved properly. 648 // resolved properly.
646 // 649 //
(...skipping 24 matching lines...) Expand all
671 Scope* outer_scope, 674 Scope* outer_scope,
672 Handle<ScopeInfo> scope_info); 675 Handle<ScopeInfo> scope_info);
673 676
674 AstValueFactory* ast_value_factory_; 677 AstValueFactory* ast_value_factory_;
675 Zone* zone_; 678 Zone* zone_;
676 }; 679 };
677 680
678 } } // namespace v8::internal 681 } } // namespace v8::internal
679 682
680 #endif // V8_SCOPES_H_ 683 #endif // V8_SCOPES_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698