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/base/compiler-specific.h" | 8 #include "src/base/compiler-specific.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/objects.h" | 11 #include "src/objects.h" |
12 #include "src/objects/scope-info.h" | 12 #include "src/objects/scope-info.h" |
13 #include "src/zone/zone.h" | 13 #include "src/zone/zone.h" |
14 | 14 |
15 namespace v8 { | 15 namespace v8 { |
16 namespace internal { | 16 namespace internal { |
17 | 17 |
18 class AstNodeFactory; | 18 class AstNodeFactory; |
19 class AstValueFactory; | 19 class AstValueFactory; |
20 class AstRawString; | 20 class AstRawString; |
21 class Declaration; | 21 class Declaration; |
22 class ParseInfo; | 22 class ParseInfo; |
23 class SloppyBlockFunctionStatement; | 23 class SloppyBlockFunctionStatement; |
| 24 class Statement; |
24 class StringSet; | 25 class StringSet; |
25 class VariableProxy; | 26 class VariableProxy; |
26 | 27 |
27 // A hash map to support fast variable declaration and lookup. | 28 // A hash map to support fast variable declaration and lookup. |
28 class VariableMap: public ZoneHashMap { | 29 class VariableMap: public ZoneHashMap { |
29 public: | 30 public: |
30 explicit VariableMap(Zone* zone); | 31 explicit VariableMap(Zone* zone); |
31 | 32 |
32 Variable* Declare(Zone* zone, Scope* scope, const AstRawString* name, | 33 Variable* Declare(Zone* zone, Scope* scope, const AstRawString* name, |
33 VariableMode mode, VariableKind kind, | 34 VariableMode mode, VariableKind kind, |
34 InitializationFlag initialization_flag, | 35 InitializationFlag initialization_flag, |
35 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned, | 36 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned, |
36 bool* added = nullptr); | 37 bool* added = nullptr); |
37 | 38 |
38 // Records that "name" exists (if not recorded yet) but doesn't create a | 39 // Records that "name" exists (if not recorded yet) but doesn't create a |
39 // Variable. Useful for preparsing. | 40 // Variable. Useful for preparsing. |
40 void DeclareName(Zone* zone, const AstRawString* name); | 41 void DeclareName(Zone* zone, const AstRawString* name, VariableMode mode); |
41 | 42 |
42 Variable* Lookup(const AstRawString* name); | 43 Variable* Lookup(const AstRawString* name); |
43 void Remove(Variable* var); | 44 void Remove(Variable* var); |
44 void Add(Zone* zone, Variable* var); | 45 void Add(Zone* zone, Variable* var); |
45 }; | 46 }; |
46 | 47 |
47 | 48 |
48 // Sloppy block-scoped function declarations to var-bind | 49 // Sloppy block-scoped function declarations to var-bind |
49 class SloppyBlockFunctionMap : public ZoneHashMap { | 50 class SloppyBlockFunctionMap : public ZoneHashMap { |
50 public: | 51 public: |
| 52 class Delegate : public ZoneObject { |
| 53 public: |
| 54 explicit Delegate(Scope* scope, |
| 55 SloppyBlockFunctionStatement* statement = nullptr) |
| 56 : scope_(scope), statement_(statement), next_(nullptr) {} |
| 57 void set_statement(Statement* statement); |
| 58 void set_next(Delegate* next) { next_ = next; } |
| 59 Delegate* next() const { return next_; } |
| 60 Scope* scope() const { return scope_; } |
| 61 |
| 62 private: |
| 63 Scope* scope_; |
| 64 SloppyBlockFunctionStatement* statement_; |
| 65 Delegate* next_; |
| 66 }; |
| 67 |
51 explicit SloppyBlockFunctionMap(Zone* zone); | 68 explicit SloppyBlockFunctionMap(Zone* zone); |
52 void Declare(Zone* zone, const AstRawString* name, | 69 void Declare(Zone* zone, const AstRawString* name, Delegate* delegate); |
53 SloppyBlockFunctionStatement* statement); | |
54 }; | 70 }; |
55 | 71 |
56 enum class AnalyzeMode { kRegular, kDebugger }; | 72 enum class AnalyzeMode { kRegular, kDebugger }; |
57 | 73 |
58 // Global invariants after AST construction: Each reference (i.e. identifier) | 74 // Global invariants after AST construction: Each reference (i.e. identifier) |
59 // to a JavaScript variable (including global properties) is represented by a | 75 // to a JavaScript variable (including global properties) is represented by a |
60 // VariableProxy node. Immediately after AST construction and before variable | 76 // VariableProxy node. Immediately after AST construction and before variable |
61 // allocation, most VariableProxy nodes are "unresolved", i.e. not bound to a | 77 // allocation, most VariableProxy nodes are "unresolved", i.e. not bound to a |
62 // corresponding variable (though some are bound during parse time). Variable | 78 // corresponding variable (though some are bound during parse time). Variable |
63 // allocation binds each unresolved VariableProxy to one Variable and assigns | 79 // allocation binds each unresolved VariableProxy to one Variable and assigns |
(...skipping 676 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
740 IsConciseMethod(function_kind()) || | 756 IsConciseMethod(function_kind()) || |
741 IsAccessorFunction(function_kind())))); | 757 IsAccessorFunction(function_kind())))); |
742 return this_function_; | 758 return this_function_; |
743 } | 759 } |
744 | 760 |
745 // Adds a local variable in this scope's locals list. This is for adjusting | 761 // Adds a local variable in this scope's locals list. This is for adjusting |
746 // the scope of temporaries and do-expression vars when desugaring parameter | 762 // the scope of temporaries and do-expression vars when desugaring parameter |
747 // initializers. | 763 // initializers. |
748 void AddLocal(Variable* var); | 764 void AddLocal(Variable* var); |
749 | 765 |
750 void DeclareSloppyBlockFunction(const AstRawString* name, | 766 void DeclareSloppyBlockFunction( |
751 SloppyBlockFunctionStatement* statement) { | 767 const AstRawString* name, Scope* scope, |
752 sloppy_block_function_map_.Declare(zone(), name, statement); | 768 SloppyBlockFunctionStatement* statement = nullptr); |
753 } | |
754 | 769 |
755 // Go through sloppy_block_function_map_ and hoist those (into this scope) | 770 // Go through sloppy_block_function_map_ and hoist those (into this scope) |
756 // which should be hoisted. | 771 // which should be hoisted. |
757 void HoistSloppyBlockFunctions(AstNodeFactory* factory); | 772 void HoistSloppyBlockFunctions(AstNodeFactory* factory); |
758 | 773 |
759 SloppyBlockFunctionMap* sloppy_block_function_map() { | 774 SloppyBlockFunctionMap* sloppy_block_function_map() { |
760 return &sloppy_block_function_map_; | 775 return &sloppy_block_function_map_; |
761 } | 776 } |
762 | 777 |
763 // Compute top scope and allocate variables. For lazy compilation the top | 778 // Compute top scope and allocate variables. For lazy compilation the top |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
874 void AllocateModuleVariables(); | 889 void AllocateModuleVariables(); |
875 | 890 |
876 private: | 891 private: |
877 ModuleDescriptor* module_descriptor_; | 892 ModuleDescriptor* module_descriptor_; |
878 }; | 893 }; |
879 | 894 |
880 } // namespace internal | 895 } // namespace internal |
881 } // namespace v8 | 896 } // namespace v8 |
882 | 897 |
883 #endif // V8_AST_SCOPES_H_ | 898 #endif // V8_AST_SCOPES_H_ |
OLD | NEW |