| 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" |
| 11 #include "src/zone.h" | 11 #include "src/zone.h" |
| 12 | 12 |
| 13 namespace v8 { | 13 namespace v8 { |
| 14 namespace internal { | 14 namespace internal { |
| 15 | 15 |
| 16 class ParseInfo; | 16 class ParseInfo; |
| 17 | 17 |
| 18 // A hash map to support fast variable declaration and lookup. | 18 // A hash map to support fast variable declaration and lookup. |
| 19 class VariableMap: public ZoneHashMap { | 19 class VariableMap: public ZoneHashMap { |
| 20 public: | 20 public: |
| 21 explicit VariableMap(Zone* zone); | 21 explicit VariableMap(Zone* zone); |
| 22 | 22 |
| 23 Variable* Declare(Zone* zone, Scope* scope, const AstRawString* name, | 23 Variable* Declare(Zone* zone, Scope* scope, const AstRawString* name, |
| 24 VariableMode mode, Variable::Kind kind, | 24 VariableMode mode, Variable::Kind kind, |
| 25 InitializationFlag initialization_flag, | 25 InitializationFlag initialization_flag, |
| 26 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned, | 26 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned, |
| 27 bool* added = nullptr); | 27 bool* added = nullptr); |
| 28 | 28 |
| 29 Variable* Lookup(const AstRawString* name); | 29 Variable* Lookup(const AstRawString* name); |
| 30 void Remove(Variable* var); |
| 31 void Add(Zone* zone, Variable* var); |
| 30 }; | 32 }; |
| 31 | 33 |
| 32 | 34 |
| 33 // Sloppy block-scoped function declarations to var-bind | 35 // Sloppy block-scoped function declarations to var-bind |
| 34 class SloppyBlockFunctionMap : public ZoneHashMap { | 36 class SloppyBlockFunctionMap : public ZoneHashMap { |
| 35 public: | 37 public: |
| 36 explicit SloppyBlockFunctionMap(Zone* zone); | 38 explicit SloppyBlockFunctionMap(Zone* zone); |
| 37 void Declare(Zone* zone, const AstRawString* name, | 39 void Declare(Zone* zone, const AstRawString* name, |
| 38 SloppyBlockFunctionStatement* statement); | 40 SloppyBlockFunctionStatement* statement); |
| 39 }; | 41 }; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 class Snapshot final BASE_EMBEDDED { | 79 class Snapshot final BASE_EMBEDDED { |
| 78 public: | 80 public: |
| 79 explicit Snapshot(Scope* scope); | 81 explicit Snapshot(Scope* scope); |
| 80 | 82 |
| 81 void Reparent(DeclarationScope* new_parent) const; | 83 void Reparent(DeclarationScope* new_parent) const; |
| 82 | 84 |
| 83 private: | 85 private: |
| 84 Scope* outer_scope_; | 86 Scope* outer_scope_; |
| 85 Scope* top_inner_scope_; | 87 Scope* top_inner_scope_; |
| 86 VariableProxy* top_unresolved_; | 88 VariableProxy* top_unresolved_; |
| 87 int top_temp_; | 89 int top_local_; |
| 90 int top_decl_; |
| 88 }; | 91 }; |
| 89 | 92 |
| 90 // Compute top scope and allocate variables. For lazy compilation the top | 93 // Compute top scope and allocate variables. For lazy compilation the top |
| 91 // scope only contains the single lazily compiled function, so this | 94 // scope only contains the single lazily compiled function, so this |
| 92 // doesn't re-allocate variables repeatedly. | 95 // doesn't re-allocate variables repeatedly. |
| 93 static void Analyze(ParseInfo* info); | 96 static void Analyze(ParseInfo* info); |
| 94 | 97 |
| 95 enum class DeserializationMode { kDeserializeOffHeap, kKeepScopeInfo }; | 98 enum class DeserializationMode { kDeserializeOffHeap, kKeepScopeInfo }; |
| 96 | 99 |
| 97 static Scope* DeserializeScopeChain(Isolate* isolate, Zone* zone, | 100 static Scope* DeserializeScopeChain(Isolate* isolate, Zone* zone, |
| (...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 | 427 |
| 425 private: | 428 private: |
| 426 Variable* Declare(Zone* zone, Scope* scope, const AstRawString* name, | 429 Variable* Declare(Zone* zone, Scope* scope, const AstRawString* name, |
| 427 VariableMode mode, Variable::Kind kind, | 430 VariableMode mode, Variable::Kind kind, |
| 428 InitializationFlag initialization_flag, | 431 InitializationFlag initialization_flag, |
| 429 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned) { | 432 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned) { |
| 430 bool added; | 433 bool added; |
| 431 Variable* var = | 434 Variable* var = |
| 432 variables_.Declare(zone, scope, name, mode, kind, initialization_flag, | 435 variables_.Declare(zone, scope, name, mode, kind, initialization_flag, |
| 433 maybe_assigned_flag, &added); | 436 maybe_assigned_flag, &added); |
| 434 if (added) ordered_variables_.Add(var, zone); | 437 if (added) locals_.Add(var, zone); |
| 435 return var; | 438 return var; |
| 436 } | 439 } |
| 437 Zone* zone_; | 440 Zone* zone_; |
| 438 | 441 |
| 439 // Scope tree. | 442 // Scope tree. |
| 440 Scope* outer_scope_; // the immediately enclosing outer scope, or NULL | 443 Scope* outer_scope_; // the immediately enclosing outer scope, or NULL |
| 441 Scope* inner_scope_; // an inner scope of this scope | 444 Scope* inner_scope_; // an inner scope of this scope |
| 442 Scope* sibling_; // a sibling inner scope of the outer scope of this scope. | 445 Scope* sibling_; // a sibling inner scope of the outer scope of this scope. |
| 443 | 446 |
| 444 // The variables declared in this scope: | 447 // The variables declared in this scope: |
| 445 // | 448 // |
| 446 // All user-declared variables (incl. parameters). For script scopes | 449 // All user-declared variables (incl. parameters). For script scopes |
| 447 // variables may be implicitly 'declared' by being used (possibly in | 450 // variables may be implicitly 'declared' by being used (possibly in |
| 448 // an inner scope) with no intervening with statements or eval calls. | 451 // an inner scope) with no intervening with statements or eval calls. |
| 449 VariableMap variables_; | 452 VariableMap variables_; |
| 450 // In case of non-scopeinfo-backed scopes, this contains the variables of the | 453 // In case of non-scopeinfo-backed scopes, this contains the variables of the |
| 451 // map above in order of addition. | 454 // map above in order of addition. |
| 452 // TODO(verwaest): Thread through Variable. | 455 // TODO(verwaest): Thread through Variable. |
| 453 ZoneList<Variable*> ordered_variables_; | 456 ZoneList<Variable*> locals_; |
| 454 // Unresolved variables referred to from this scope. The proxies themselves | 457 // Unresolved variables referred to from this scope. The proxies themselves |
| 455 // form a linked list of all unresolved proxies. | 458 // form a linked list of all unresolved proxies. |
| 456 VariableProxy* unresolved_; | 459 VariableProxy* unresolved_; |
| 457 // Declarations. | 460 // Declarations. |
| 458 ZoneList<Declaration*> decls_; | 461 ZoneList<Declaration*> decls_; |
| 459 | 462 |
| 460 // Serialized scope info support. | 463 // Serialized scope info support. |
| 461 Handle<ScopeInfo> scope_info_; | 464 Handle<ScopeInfo> scope_info_; |
| 462 // Debugging support. | 465 // Debugging support. |
| 463 #ifdef DEBUG | 466 #ifdef DEBUG |
| (...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 734 | 737 |
| 735 Variable* this_function_var() const { | 738 Variable* this_function_var() const { |
| 736 // This is only used in derived constructors atm. | 739 // This is only used in derived constructors atm. |
| 737 DCHECK(this_function_ == nullptr || | 740 DCHECK(this_function_ == nullptr || |
| 738 (is_function_scope() && (IsClassConstructor(function_kind()) || | 741 (is_function_scope() && (IsClassConstructor(function_kind()) || |
| 739 IsConciseMethod(function_kind()) || | 742 IsConciseMethod(function_kind()) || |
| 740 IsAccessorFunction(function_kind())))); | 743 IsAccessorFunction(function_kind())))); |
| 741 return this_function_; | 744 return this_function_; |
| 742 } | 745 } |
| 743 | 746 |
| 744 // Adds a temporary variable in this scope's TemporaryScope. This is for | 747 // Adds a local variable in this scope's locals list. This is for adjusting |
| 745 // adjusting the scope of temporaries used when desugaring parameter | 748 // the scope of temporaries and do-expression vars when desugaring parameter |
| 746 // initializers. | 749 // initializers. |
| 747 void AddTemporary(Variable* var) { | 750 void AddLocal(Variable* var) { |
| 748 DCHECK(!already_resolved_); | 751 DCHECK(!already_resolved_); |
| 749 // Temporaries are only placed in ClosureScopes. | 752 // Temporaries are only placed in ClosureScopes. |
| 750 DCHECK_EQ(GetClosureScope(), this); | 753 DCHECK_EQ(GetClosureScope(), this); |
| 751 temps_.Add(var, zone()); | 754 locals_.Add(var, zone()); |
| 752 } | 755 } |
| 753 | 756 |
| 754 ZoneList<Variable*>* temps() { return &temps_; } | |
| 755 | |
| 756 void DeclareSloppyBlockFunction(const AstRawString* name, | 757 void DeclareSloppyBlockFunction(const AstRawString* name, |
| 757 SloppyBlockFunctionStatement* statement) { | 758 SloppyBlockFunctionStatement* statement) { |
| 758 sloppy_block_function_map_.Declare(zone(), name, statement); | 759 sloppy_block_function_map_.Declare(zone(), name, statement); |
| 759 } | 760 } |
| 760 | 761 |
| 761 SloppyBlockFunctionMap* sloppy_block_function_map() { | 762 SloppyBlockFunctionMap* sloppy_block_function_map() { |
| 762 return &sloppy_block_function_map_; | 763 return &sloppy_block_function_map_; |
| 763 } | 764 } |
| 764 | 765 |
| 765 // Resolve and fill in the allocation information for all variables | 766 // Resolve and fill in the allocation information for all variables |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 822 bool force_eager_compilation_ : 1; | 823 bool force_eager_compilation_ : 1; |
| 823 // This function scope has a rest parameter. | 824 // This function scope has a rest parameter. |
| 824 bool has_rest_ : 1; | 825 bool has_rest_ : 1; |
| 825 // This scope has a parameter called "arguments". | 826 // This scope has a parameter called "arguments". |
| 826 bool has_arguments_parameter_ : 1; | 827 bool has_arguments_parameter_ : 1; |
| 827 // This scope uses "super" property ('super.foo'). | 828 // This scope uses "super" property ('super.foo'). |
| 828 bool scope_uses_super_property_ : 1; | 829 bool scope_uses_super_property_ : 1; |
| 829 | 830 |
| 830 // Info about the parameter list of a function. | 831 // Info about the parameter list of a function. |
| 831 int arity_; | 832 int arity_; |
| 832 // Compiler-allocated (user-invisible) temporaries. | |
| 833 ZoneList<Variable*> temps_; | |
| 834 // Parameter list in source order. | 833 // Parameter list in source order. |
| 835 ZoneList<Variable*> params_; | 834 ZoneList<Variable*> params_; |
| 836 // Map of function names to lists of functions defined in sloppy blocks | 835 // Map of function names to lists of functions defined in sloppy blocks |
| 837 SloppyBlockFunctionMap sloppy_block_function_map_; | 836 SloppyBlockFunctionMap sloppy_block_function_map_; |
| 838 // Convenience variable. | 837 // Convenience variable. |
| 839 Variable* receiver_; | 838 Variable* receiver_; |
| 840 // Function variable, if any; function scopes only. | 839 // Function variable, if any; function scopes only. |
| 841 Variable* function_; | 840 Variable* function_; |
| 842 // new.target variable, function scopes only. | 841 // new.target variable, function scopes only. |
| 843 Variable* new_target_; | 842 Variable* new_target_; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 862 void AllocateModuleVariables(); | 861 void AllocateModuleVariables(); |
| 863 | 862 |
| 864 private: | 863 private: |
| 865 ModuleDescriptor* module_descriptor_; | 864 ModuleDescriptor* module_descriptor_; |
| 866 }; | 865 }; |
| 867 | 866 |
| 868 } // namespace internal | 867 } // namespace internal |
| 869 } // namespace v8 | 868 } // namespace v8 |
| 870 | 869 |
| 871 #endif // V8_AST_SCOPES_H_ | 870 #endif // V8_AST_SCOPES_H_ |
| OLD | NEW |