| 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_AST_SCOPES_H_ | 5 #ifndef V8_AST_SCOPES_H_ |
| 6 #define V8_AST_SCOPES_H_ | 6 #define V8_AST_SCOPES_H_ |
| 7 | 7 |
| 8 #include "src/ast/ast.h" | 8 #include "src/ast/ast.h" |
| 9 #include "src/base/hashmap.h" | 9 #include "src/base/hashmap.h" |
| 10 #include "src/globals.h" | 10 #include "src/globals.h" |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 | 62 |
| 63 // Global invariants after AST construction: Each reference (i.e. identifier) | 63 // Global invariants after AST construction: Each reference (i.e. identifier) |
| 64 // to a JavaScript variable (including global properties) is represented by a | 64 // to a JavaScript variable (including global properties) is represented by a |
| 65 // VariableProxy node. Immediately after AST construction and before variable | 65 // VariableProxy node. Immediately after AST construction and before variable |
| 66 // allocation, most VariableProxy nodes are "unresolved", i.e. not bound to a | 66 // allocation, most VariableProxy nodes are "unresolved", i.e. not bound to a |
| 67 // corresponding variable (though some are bound during parse time). Variable | 67 // corresponding variable (though some are bound during parse time). Variable |
| 68 // allocation binds each unresolved VariableProxy to one Variable and assigns | 68 // allocation binds each unresolved VariableProxy to one Variable and assigns |
| 69 // a location. Note that many VariableProxy nodes may refer to the same Java- | 69 // a location. Note that many VariableProxy nodes may refer to the same Java- |
| 70 // Script variable. | 70 // Script variable. |
| 71 | 71 |
| 72 class DeclarationScope; | 72 // JS environments are represented in the parser using Scope, DeclarationScope |
| 73 | 73 // and ModuleScope. DeclarationScope is used for any scope that hosts 'var' |
| 74 // JS environments are represented in the parser using two scope classes, Scope | 74 // declarations. This includes script, module, eval, varblock, and function |
| 75 // and its subclass DeclarationScope. DeclarationScope is used for any scope | 75 // scope. ModuleScope further specializes DeclarationScope. |
| 76 // that hosts 'var' declarations. This includes script, module, eval, varblock, | |
| 77 // and function scope. All fields required by such scopes are only available on | |
| 78 // DeclarationScope. | |
| 79 class Scope: public ZoneObject { | 76 class Scope: public ZoneObject { |
| 80 public: | 77 public: |
| 81 // --------------------------------------------------------------------------- | 78 // --------------------------------------------------------------------------- |
| 82 // Construction | 79 // Construction |
| 83 | 80 |
| 84 Scope(Zone* zone, Scope* outer_scope, ScopeType scope_type); | 81 Scope(Zone* zone, Scope* outer_scope, ScopeType scope_type); |
| 85 | 82 |
| 86 #ifdef DEBUG | 83 #ifdef DEBUG |
| 87 // The scope name is only used for printing/debugging. | 84 // The scope name is only used for printing/debugging. |
| 88 void SetScopeName(const AstRawString* scope_name) { | 85 void SetScopeName(const AstRawString* scope_name) { |
| 89 scope_name_ = scope_name; | 86 scope_name_ = scope_name; |
| 90 } | 87 } |
| 91 #endif | 88 #endif |
| 92 | 89 |
| 93 // TODO(verwaest): Is this needed on Scope? | 90 // TODO(verwaest): Is this needed on Scope? |
| 94 int num_parameters() const; | 91 int num_parameters() const; |
| 95 | 92 |
| 96 DeclarationScope* AsDeclarationScope(); | 93 DeclarationScope* AsDeclarationScope(); |
| 97 const DeclarationScope* AsDeclarationScope() const; | 94 const DeclarationScope* AsDeclarationScope() const; |
| 95 ModuleScope* AsModuleScope(); |
| 96 const ModuleScope* AsModuleScope() const; |
| 98 | 97 |
| 99 class Snapshot final BASE_EMBEDDED { | 98 class Snapshot final BASE_EMBEDDED { |
| 100 public: | 99 public: |
| 101 explicit Snapshot(Scope* scope); | 100 explicit Snapshot(Scope* scope); |
| 102 | 101 |
| 103 void Reparent(DeclarationScope* new_parent) const; | 102 void Reparent(DeclarationScope* new_parent) const; |
| 104 | 103 |
| 105 private: | 104 private: |
| 106 Scope* outer_scope_; | 105 Scope* outer_scope_; |
| 107 Scope* top_inner_scope_; | 106 Scope* top_inner_scope_; |
| (...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 437 void CheckScopePositions(); | 436 void CheckScopePositions(); |
| 438 | 437 |
| 439 // Check that all Scopes in the scope tree use the same Zone. | 438 // Check that all Scopes in the scope tree use the same Zone. |
| 440 void CheckZones(); | 439 void CheckZones(); |
| 441 #endif | 440 #endif |
| 442 | 441 |
| 443 // Retrieve `IsSimpleParameterList` of current or outer function. | 442 // Retrieve `IsSimpleParameterList` of current or outer function. |
| 444 bool HasSimpleParameters(); | 443 bool HasSimpleParameters(); |
| 445 void set_is_debug_evaluate_scope() { is_debug_evaluate_scope_ = true; } | 444 void set_is_debug_evaluate_scope() { is_debug_evaluate_scope_ = true; } |
| 446 | 445 |
| 446 protected: |
| 447 void set_language_mode(LanguageMode language_mode) { |
| 448 language_mode_ = language_mode; |
| 449 } |
| 450 |
| 447 private: | 451 private: |
| 448 Zone* zone_; | 452 Zone* zone_; |
| 449 | 453 |
| 450 // Scope tree. | 454 // Scope tree. |
| 451 Scope* outer_scope_; // the immediately enclosing outer scope, or NULL | 455 Scope* outer_scope_; // the immediately enclosing outer scope, or NULL |
| 452 Scope* inner_scope_; // an inner scope of this scope | 456 Scope* inner_scope_; // an inner scope of this scope |
| 453 Scope* sibling_; // a sibling inner scope of the outer scope of this scope. | 457 Scope* sibling_; // a sibling inner scope of the outer scope of this scope. |
| 454 | 458 |
| 455 // The variables declared in this scope: | 459 // The variables declared in this scope: |
| 456 // | 460 // |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 666 } | 670 } |
| 667 | 671 |
| 668 bool NeedsHomeObject() const { | 672 bool NeedsHomeObject() const { |
| 669 return scope_uses_super_property_ || | 673 return scope_uses_super_property_ || |
| 670 ((scope_calls_eval_ || inner_scope_calls_eval_) && | 674 ((scope_calls_eval_ || inner_scope_calls_eval_) && |
| 671 (IsConciseMethod(function_kind()) || | 675 (IsConciseMethod(function_kind()) || |
| 672 IsAccessorFunction(function_kind()) || | 676 IsAccessorFunction(function_kind()) || |
| 673 IsClassConstructor(function_kind()))); | 677 IsClassConstructor(function_kind()))); |
| 674 } | 678 } |
| 675 | 679 |
| 676 // The ModuleDescriptor for this scope; only for module scopes. | |
| 677 // TODO(verwaest): Move to ModuleScope? | |
| 678 ModuleDescriptor* module() const { | |
| 679 DCHECK(is_module_scope()); | |
| 680 DCHECK_NOT_NULL(module_descriptor_); | |
| 681 return module_descriptor_; | |
| 682 } | |
| 683 | |
| 684 void DeclareThis(AstValueFactory* ast_value_factory); | 680 void DeclareThis(AstValueFactory* ast_value_factory); |
| 685 void DeclareDefaultFunctionVariables(AstValueFactory* ast_value_factory); | 681 void DeclareDefaultFunctionVariables(AstValueFactory* ast_value_factory); |
| 686 | 682 |
| 687 // This lookup corresponds to a lookup in the "intermediate" scope sitting | 683 // This lookup corresponds to a lookup in the "intermediate" scope sitting |
| 688 // between this scope and the outer scope. (ECMA-262, 3rd., requires that | 684 // between this scope and the outer scope. (ECMA-262, 3rd., requires that |
| 689 // the name of named function literal is kept in an intermediate scope | 685 // the name of named function literal is kept in an intermediate scope |
| 690 // in between this scope and the next outer scope.) | 686 // in between this scope and the next outer scope.) |
| 691 Variable* LookupFunctionVar(const AstRawString* name); | 687 Variable* LookupFunctionVar(const AstRawString* name); |
| 692 | 688 |
| 693 // Declare the function variable for a function literal. This variable | 689 // Declare the function variable for a function literal. This variable |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 840 Handle<StringSet> CollectNonLocals(ParseInfo* info, | 836 Handle<StringSet> CollectNonLocals(ParseInfo* info, |
| 841 Handle<StringSet> non_locals); | 837 Handle<StringSet> non_locals); |
| 842 | 838 |
| 843 #ifdef DEBUG | 839 #ifdef DEBUG |
| 844 void PrintParameters(); | 840 void PrintParameters(); |
| 845 #endif | 841 #endif |
| 846 | 842 |
| 847 void AllocateLocals(); | 843 void AllocateLocals(); |
| 848 void AllocateParameterLocals(); | 844 void AllocateParameterLocals(); |
| 849 void AllocateReceiver(); | 845 void AllocateReceiver(); |
| 850 // Set MODULE as VariableLocation for all variables that will live in some | |
| 851 // module's export table. | |
| 852 void AllocateModuleVariables(); | |
| 853 | 846 |
| 854 private: | 847 private: |
| 855 void AllocateParameter(Variable* var, int index); | 848 void AllocateParameter(Variable* var, int index); |
| 856 | 849 |
| 857 void SetDefaults(); | 850 void SetDefaults(); |
| 858 | 851 |
| 859 // If the scope is a function scope, this is the function kind. | 852 // If the scope is a function scope, this is the function kind. |
| 860 const FunctionKind function_kind_; | 853 const FunctionKind function_kind_; |
| 861 | 854 |
| 862 bool has_simple_parameters_ : 1; | 855 bool has_simple_parameters_ : 1; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 874 // Convenience variable. | 867 // Convenience variable. |
| 875 Variable* receiver_; | 868 Variable* receiver_; |
| 876 // Function variable, if any; function scopes only. | 869 // Function variable, if any; function scopes only. |
| 877 Variable* function_; | 870 Variable* function_; |
| 878 // new.target variable, function scopes only. | 871 // new.target variable, function scopes only. |
| 879 Variable* new_target_; | 872 Variable* new_target_; |
| 880 // Convenience variable; function scopes only. | 873 // Convenience variable; function scopes only. |
| 881 Variable* arguments_; | 874 Variable* arguments_; |
| 882 // Convenience variable; Subclass constructor only | 875 // Convenience variable; Subclass constructor only |
| 883 Variable* this_function_; | 876 Variable* this_function_; |
| 884 // Module descriptor; module scopes only. | 877 }; |
| 878 |
| 879 class ModuleScope final : public DeclarationScope { |
| 880 public: |
| 881 ModuleScope(Zone* zone, DeclarationScope* script_scope, |
| 882 AstValueFactory* ast_value_factory); |
| 883 |
| 884 ModuleDescriptor* module() const { |
| 885 DCHECK_NOT_NULL(module_descriptor_); |
| 886 return module_descriptor_; |
| 887 } |
| 888 |
| 889 // Set MODULE as VariableLocation for all variables that will live in some |
| 890 // module's export table. |
| 891 void AllocateModuleVariables(); |
| 892 |
| 893 private: |
| 885 ModuleDescriptor* module_descriptor_; | 894 ModuleDescriptor* module_descriptor_; |
| 886 }; | 895 }; |
| 887 | 896 |
| 888 } // namespace internal | 897 } // namespace internal |
| 889 } // namespace v8 | 898 } // namespace v8 |
| 890 | 899 |
| 891 #endif // V8_AST_SCOPES_H_ | 900 #endif // V8_AST_SCOPES_H_ |
| OLD | NEW |