Chromium Code Reviews| 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 #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/pending-compilation-error-handler.h" | 9 #include "src/pending-compilation-error-handler.h" |
| 10 #include "src/zone.h" | 10 #include "src/zone.h" |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 66 // allocation binds each unresolved VariableProxy to one Variable and assigns | 66 // allocation binds each unresolved VariableProxy to one Variable and assigns |
| 67 // a location. Note that many VariableProxy nodes may refer to the same Java- | 67 // a location. Note that many VariableProxy nodes may refer to the same Java- |
| 68 // Script variable. | 68 // Script variable. |
| 69 | 69 |
| 70 class Scope: public ZoneObject { | 70 class Scope: public ZoneObject { |
| 71 public: | 71 public: |
| 72 // --------------------------------------------------------------------------- | 72 // --------------------------------------------------------------------------- |
| 73 // Construction | 73 // Construction |
| 74 | 74 |
| 75 Scope(Zone* zone, Scope* outer_scope, ScopeType scope_type, | 75 Scope(Zone* zone, Scope* outer_scope, ScopeType scope_type, |
| 76 AstValueFactory* value_factory); | 76 AstValueFactory* value_factory, |
| 77 FunctionKind function_kind = kNormalFunction); | |
| 77 | 78 |
| 78 // Compute top scope and allocate variables. For lazy compilation the top | 79 // Compute top scope and allocate variables. For lazy compilation the top |
| 79 // scope only contains the single lazily compiled function, so this | 80 // scope only contains the single lazily compiled function, so this |
| 80 // doesn't re-allocate variables repeatedly. | 81 // doesn't re-allocate variables repeatedly. |
| 81 static bool Analyze(CompilationInfo* info); | 82 static bool Analyze(CompilationInfo* info); |
| 82 | 83 |
| 83 static Scope* DeserializeScopeChain(Isolate* isolate, Zone* zone, | 84 static Scope* DeserializeScopeChain(Isolate* isolate, Zone* zone, |
| 84 Context* context, Scope* script_scope); | 85 Context* context, Scope* script_scope); |
| 85 | 86 |
| 86 // The scope name is only used for printing/debugging. | 87 // The scope name is only used for printing/debugging. |
| 87 void SetScopeName(const AstRawString* scope_name) { | 88 void SetScopeName(const AstRawString* scope_name) { |
| 88 scope_name_ = scope_name; | 89 scope_name_ = scope_name; |
| 89 } | 90 } |
| 90 | 91 |
| 91 void Initialize(bool subclass_constructor = false); | 92 void Initialize(); |
| 92 | 93 |
| 93 // Checks if the block scope is redundant, i.e. it does not contain any | 94 // Checks if the block scope is redundant, i.e. it does not contain any |
| 94 // block scoped declarations. In that case it is removed from the scope | 95 // block scoped declarations. In that case it is removed from the scope |
| 95 // tree and its children are reparented. | 96 // tree and its children are reparented. |
| 96 Scope* FinalizeBlockScope(); | 97 Scope* FinalizeBlockScope(); |
| 97 | 98 |
| 98 Zone* zone() const { return zone_; } | 99 Zone* zone() const { return zone_; } |
| 99 | 100 |
| 100 // --------------------------------------------------------------------------- | 101 // --------------------------------------------------------------------------- |
| 101 // Declarations | 102 // Declarations |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 274 bool is_eval_scope() const { return scope_type_ == EVAL_SCOPE; } | 275 bool is_eval_scope() const { return scope_type_ == EVAL_SCOPE; } |
| 275 bool is_function_scope() const { | 276 bool is_function_scope() const { |
| 276 return scope_type_ == FUNCTION_SCOPE || scope_type_ == ARROW_SCOPE; | 277 return scope_type_ == FUNCTION_SCOPE || scope_type_ == ARROW_SCOPE; |
| 277 } | 278 } |
| 278 bool is_module_scope() const { return scope_type_ == MODULE_SCOPE; } | 279 bool is_module_scope() const { return scope_type_ == MODULE_SCOPE; } |
| 279 bool is_script_scope() const { return scope_type_ == SCRIPT_SCOPE; } | 280 bool is_script_scope() const { return scope_type_ == SCRIPT_SCOPE; } |
| 280 bool is_catch_scope() const { return scope_type_ == CATCH_SCOPE; } | 281 bool is_catch_scope() const { return scope_type_ == CATCH_SCOPE; } |
| 281 bool is_block_scope() const { return scope_type_ == BLOCK_SCOPE; } | 282 bool is_block_scope() const { return scope_type_ == BLOCK_SCOPE; } |
| 282 bool is_with_scope() const { return scope_type_ == WITH_SCOPE; } | 283 bool is_with_scope() const { return scope_type_ == WITH_SCOPE; } |
| 283 bool is_arrow_scope() const { return scope_type_ == ARROW_SCOPE; } | 284 bool is_arrow_scope() const { return scope_type_ == ARROW_SCOPE; } |
| 285 void tag_as_class_scope() { | |
| 286 DCHECK(is_block_scope()); | |
| 287 block_scope_is_class_scope_ = true; | |
| 288 } | |
| 289 bool is_class_scope() const { | |
| 290 return is_block_scope() && block_scope_is_class_scope_; | |
| 291 } | |
| 284 bool is_declaration_scope() const { | 292 bool is_declaration_scope() const { |
| 285 return is_eval_scope() || is_function_scope() || | 293 return is_eval_scope() || is_function_scope() || |
| 286 is_module_scope() || is_script_scope(); | 294 is_module_scope() || is_script_scope(); |
| 287 } | 295 } |
| 288 bool is_strict_eval_scope() const { | 296 bool is_strict_eval_scope() const { |
| 289 return is_eval_scope() && is_strict(language_mode_); | 297 return is_eval_scope() && is_strict(language_mode_); |
| 290 } | 298 } |
| 291 | 299 |
| 292 // Information about which scopes calls eval. | 300 // Information about which scopes calls eval. |
| 293 bool calls_eval() const { return scope_calls_eval_; } | 301 bool calls_eval() const { return scope_calls_eval_; } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 325 if (outer_scope() == nullptr) return nullptr; | 333 if (outer_scope() == nullptr) return nullptr; |
| 326 return outer_scope()->NearestOuterEvalScope(); | 334 return outer_scope()->NearestOuterEvalScope(); |
| 327 } | 335 } |
| 328 | 336 |
| 329 // --------------------------------------------------------------------------- | 337 // --------------------------------------------------------------------------- |
| 330 // Accessors. | 338 // Accessors. |
| 331 | 339 |
| 332 // The type of this scope. | 340 // The type of this scope. |
| 333 ScopeType scope_type() const { return scope_type_; } | 341 ScopeType scope_type() const { return scope_type_; } |
| 334 | 342 |
| 343 FunctionKind function_kind() const { return function_kind_; } | |
| 344 | |
| 335 // The language mode of this scope. | 345 // The language mode of this scope. |
| 336 LanguageMode language_mode() const { return language_mode_; } | 346 LanguageMode language_mode() const { return language_mode_; } |
| 337 | 347 |
| 338 // The variable corresponding to the 'this' value. | 348 // The variable corresponding to the 'this' value. |
| 339 Variable* receiver() { return receiver_; } | 349 Variable* receiver() { return receiver_; } |
| 340 | 350 |
| 341 // The variable corresponding to the 'new.target' value. | 351 // The variable corresponding to the 'new.target' value. |
| 342 Variable* new_target_var() { return new_target_; } | 352 Variable* new_target_var() { return new_target_; } |
| 343 | 353 |
| 344 // The variable holding the function literal for named function | 354 // The variable holding the function literal for named function |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 494 // Implementation. | 504 // Implementation. |
| 495 protected: | 505 protected: |
| 496 friend class ParserFactory; | 506 friend class ParserFactory; |
| 497 | 507 |
| 498 // Scope tree. | 508 // Scope tree. |
| 499 Scope* outer_scope_; // the immediately enclosing outer scope, or NULL | 509 Scope* outer_scope_; // the immediately enclosing outer scope, or NULL |
| 500 ZoneList<Scope*> inner_scopes_; // the immediately enclosed inner scopes | 510 ZoneList<Scope*> inner_scopes_; // the immediately enclosed inner scopes |
| 501 | 511 |
| 502 // The scope type. | 512 // The scope type. |
| 503 ScopeType scope_type_; | 513 ScopeType scope_type_; |
| 514 // Some block scopes are tagged as class scopes. | |
|
rossberg
2015/03/09 10:41:46
Yeah, I think this is fine for the time being, but
marja
2015/03/09 13:44:06
It'll also depend on the object literal solution..
| |
| 515 bool block_scope_is_class_scope_; | |
| 516 // If the scope is a function scope, this is the function kind. | |
| 517 FunctionKind function_kind_; | |
| 504 | 518 |
| 505 // Debugging support. | 519 // Debugging support. |
| 506 const AstRawString* scope_name_; | 520 const AstRawString* scope_name_; |
| 507 | 521 |
| 508 // The variables declared in this scope: | 522 // The variables declared in this scope: |
| 509 // | 523 // |
| 510 // All user-declared variables (incl. parameters). For script scopes | 524 // All user-declared variables (incl. parameters). For script scopes |
| 511 // variables may be implicitly 'declared' by being used (possibly in | 525 // variables may be implicitly 'declared' by being used (possibly in |
| 512 // an inner scope) with no intervening with statements or eval calls. | 526 // an inner scope) with no intervening with statements or eval calls. |
| 513 VariableMap variables_; | 527 VariableMap variables_; |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 651 // parameter should be set to the calling context of 'eval'. | 665 // parameter should be set to the calling context of 'eval'. |
| 652 Variable* LookupRecursive(VariableProxy* proxy, BindingKind* binding_kind, | 666 Variable* LookupRecursive(VariableProxy* proxy, BindingKind* binding_kind, |
| 653 AstNodeFactory* factory); | 667 AstNodeFactory* factory); |
| 654 MUST_USE_RESULT | 668 MUST_USE_RESULT |
| 655 bool ResolveVariable(CompilationInfo* info, VariableProxy* proxy, | 669 bool ResolveVariable(CompilationInfo* info, VariableProxy* proxy, |
| 656 AstNodeFactory* factory); | 670 AstNodeFactory* factory); |
| 657 MUST_USE_RESULT | 671 MUST_USE_RESULT |
| 658 bool ResolveVariablesRecursively(CompilationInfo* info, | 672 bool ResolveVariablesRecursively(CompilationInfo* info, |
| 659 AstNodeFactory* factory); | 673 AstNodeFactory* factory); |
| 660 | 674 |
| 675 bool CheckStrongModeDeclaration(VariableProxy* proxy, Variable* var); | |
| 676 | |
| 661 // Scope analysis. | 677 // Scope analysis. |
| 662 void PropagateScopeInfo(bool outer_scope_calls_sloppy_eval); | 678 void PropagateScopeInfo(bool outer_scope_calls_sloppy_eval); |
| 663 bool HasTrivialContext() const; | 679 bool HasTrivialContext() const; |
| 664 | 680 |
| 665 // Predicates. | 681 // Predicates. |
| 666 bool MustAllocate(Variable* var); | 682 bool MustAllocate(Variable* var); |
| 667 bool MustAllocateInContext(Variable* var); | 683 bool MustAllocateInContext(Variable* var); |
| 668 bool HasArgumentsParameter(Isolate* isolate); | 684 bool HasArgumentsParameter(Isolate* isolate); |
| 669 | 685 |
| 670 // Variable allocation. | 686 // Variable allocation. |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 696 Scope(Zone* zone, Scope* inner_scope, const AstRawString* catch_variable_name, | 712 Scope(Zone* zone, Scope* inner_scope, const AstRawString* catch_variable_name, |
| 697 AstValueFactory* value_factory); | 713 AstValueFactory* value_factory); |
| 698 | 714 |
| 699 void AddInnerScope(Scope* inner_scope) { | 715 void AddInnerScope(Scope* inner_scope) { |
| 700 if (inner_scope != NULL) { | 716 if (inner_scope != NULL) { |
| 701 inner_scopes_.Add(inner_scope, zone_); | 717 inner_scopes_.Add(inner_scope, zone_); |
| 702 inner_scope->outer_scope_ = this; | 718 inner_scope->outer_scope_ = this; |
| 703 } | 719 } |
| 704 } | 720 } |
| 705 | 721 |
| 706 void SetDefaults(ScopeType type, | 722 void SetDefaults(ScopeType type, Scope* outer_scope, |
| 707 Scope* outer_scope, | 723 Handle<ScopeInfo> scope_info, |
| 708 Handle<ScopeInfo> scope_info); | 724 FunctionKind function_kind = kNormalFunction); |
| 725 | |
| 726 // If this scope is a method scope of a class, return the corresponding | |
| 727 // class variable, otherwise nullptr. | |
| 728 Variable* ClassVariableForMethod() const; | |
| 709 | 729 |
| 710 AstValueFactory* ast_value_factory_; | 730 AstValueFactory* ast_value_factory_; |
| 711 Zone* zone_; | 731 Zone* zone_; |
| 712 | 732 |
| 713 PendingCompilationErrorHandler pending_error_handler_; | 733 PendingCompilationErrorHandler pending_error_handler_; |
| 714 }; | 734 }; |
| 715 | 735 |
| 716 } } // namespace v8::internal | 736 } } // namespace v8::internal |
| 717 | 737 |
| 718 #endif // V8_SCOPES_H_ | 738 #endif // V8_SCOPES_H_ |
| OLD | NEW |