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 virtual ~VariableMap(); | 23 Variable* Declare(Zone* zone, Scope* scope, const AstRawString* name, |
24 | 24 VariableMode mode, Variable::Kind kind, |
25 Variable* Declare(Scope* scope, const AstRawString* name, VariableMode mode, | 25 InitializationFlag initialization_flag, |
26 Variable::Kind kind, InitializationFlag initialization_flag, | |
27 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned); | 26 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned); |
28 | 27 |
29 Variable* Lookup(const AstRawString* name); | 28 Variable* Lookup(const AstRawString* name); |
30 | |
31 Zone* zone() const { return zone_; } | |
32 | |
33 private: | |
34 Zone* zone_; | |
35 }; | 29 }; |
36 | 30 |
37 | 31 |
38 // The dynamic scope part holds hash maps for the variables that will | 32 // The dynamic scope part holds hash maps for the variables that will |
39 // be looked up dynamically from within eval and with scopes. The objects | 33 // be looked up dynamically from within eval and with scopes. The objects |
40 // are allocated on-demand from Scope::NonLocal to avoid wasting memory | 34 // are allocated on-demand from Scope::NonLocal to avoid wasting memory |
41 // and setup time for scopes that don't need them. | 35 // and setup time for scopes that don't need them. |
42 class DynamicScopePart : public ZoneObject { | 36 class DynamicScopePart : public ZoneObject { |
43 public: | 37 public: |
44 explicit DynamicScopePart(Zone* zone) { | 38 explicit DynamicScopePart(Zone* zone) { |
45 for (int i = 0; i < 3; i++) | 39 for (int i = 0; i < 3; i++) |
46 maps_[i] = new(zone->New(sizeof(VariableMap))) VariableMap(zone); | 40 maps_[i] = new(zone->New(sizeof(VariableMap))) VariableMap(zone); |
47 } | 41 } |
48 | 42 |
49 VariableMap* GetMap(VariableMode mode) { | 43 VariableMap* GetMap(VariableMode mode) { |
50 int index = mode - DYNAMIC; | 44 int index = mode - DYNAMIC; |
51 DCHECK(index >= 0 && index < 3); | 45 DCHECK(index >= 0 && index < 3); |
52 return maps_[index]; | 46 return maps_[index]; |
53 } | 47 } |
54 | 48 |
55 private: | 49 private: |
56 VariableMap *maps_[3]; | 50 VariableMap *maps_[3]; |
57 }; | 51 }; |
58 | 52 |
59 | 53 |
60 // Sloppy block-scoped function declarations to var-bind | 54 // Sloppy block-scoped function declarations to var-bind |
61 class SloppyBlockFunctionMap : public ZoneHashMap { | 55 class SloppyBlockFunctionMap : public ZoneHashMap { |
62 public: | 56 public: |
63 explicit SloppyBlockFunctionMap(Zone* zone); | 57 explicit SloppyBlockFunctionMap(Zone* zone); |
64 | 58 void Declare(Zone* zone, const AstRawString* name, |
65 virtual ~SloppyBlockFunctionMap(); | |
66 | |
67 void Declare(const AstRawString* name, | |
68 SloppyBlockFunctionStatement* statement); | 59 SloppyBlockFunctionStatement* statement); |
69 | |
70 typedef ZoneVector<SloppyBlockFunctionStatement*> Vector; | 60 typedef ZoneVector<SloppyBlockFunctionStatement*> Vector; |
71 | |
72 private: | |
73 Zone* zone_; | |
74 }; | 61 }; |
75 | 62 |
76 | 63 |
77 // Global invariants after AST construction: Each reference (i.e. identifier) | 64 // Global invariants after AST construction: Each reference (i.e. identifier) |
78 // to a JavaScript variable (including global properties) is represented by a | 65 // to a JavaScript variable (including global properties) is represented by a |
79 // VariableProxy node. Immediately after AST construction and before variable | 66 // VariableProxy node. Immediately after AST construction and before variable |
80 // allocation, most VariableProxy nodes are "unresolved", i.e. not bound to a | 67 // allocation, most VariableProxy nodes are "unresolved", i.e. not bound to a |
81 // corresponding variable (though some are bound during parse time). Variable | 68 // corresponding variable (though some are bound during parse time). Variable |
82 // allocation binds each unresolved VariableProxy to one Variable and assigns | 69 // allocation binds each unresolved VariableProxy to one Variable and assigns |
83 // a location. Note that many VariableProxy nodes may refer to the same Java- | 70 // a location. Note that many VariableProxy nodes may refer to the same Java- |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 | 130 |
144 // Inserts outer_scope into this scope's scope chain (and removes this | 131 // Inserts outer_scope into this scope's scope chain (and removes this |
145 // from the current outer_scope_'s inner scope list). | 132 // from the current outer_scope_'s inner scope list). |
146 // Assumes outer_scope_ is non-null. | 133 // Assumes outer_scope_ is non-null. |
147 void ReplaceOuterScope(Scope* outer_scope); | 134 void ReplaceOuterScope(Scope* outer_scope); |
148 | 135 |
149 // Propagates any eagerly-gathered scope usage flags (such as calls_eval()) | 136 // Propagates any eagerly-gathered scope usage flags (such as calls_eval()) |
150 // to the passed-in scope. | 137 // to the passed-in scope. |
151 void PropagateUsageFlagsToScope(Scope* other); | 138 void PropagateUsageFlagsToScope(Scope* other); |
152 | 139 |
153 Zone* zone() const { return variables_.zone(); } | 140 Zone* zone() const { return zone_; } |
154 | 141 |
155 // --------------------------------------------------------------------------- | 142 // --------------------------------------------------------------------------- |
156 // Declarations | 143 // Declarations |
157 | 144 |
158 // Lookup a variable in this scope. Returns the variable or NULL if not found. | 145 // Lookup a variable in this scope. Returns the variable or NULL if not found. |
159 Variable* LookupLocal(const AstRawString* name); | 146 Variable* LookupLocal(const AstRawString* name); |
160 | 147 |
161 // Lookup a variable in this scope or outer scopes. | 148 // Lookup a variable in this scope or outer scopes. |
162 // Returns the variable or NULL if not found. | 149 // Returns the variable or NULL if not found. |
163 Variable* Lookup(const AstRawString* name); | 150 Variable* Lookup(const AstRawString* name); |
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
457 void CheckScopePositions(); | 444 void CheckScopePositions(); |
458 | 445 |
459 // Check that all Scopes in the scope tree use the same Zone. | 446 // Check that all Scopes in the scope tree use the same Zone. |
460 void CheckZones(); | 447 void CheckZones(); |
461 #endif | 448 #endif |
462 | 449 |
463 // Retrieve `IsSimpleParameterList` of current or outer function. | 450 // Retrieve `IsSimpleParameterList` of current or outer function. |
464 bool HasSimpleParameters(); | 451 bool HasSimpleParameters(); |
465 | 452 |
466 private: | 453 private: |
| 454 Zone* zone_; |
| 455 |
467 // Scope tree. | 456 // Scope tree. |
468 Scope* outer_scope_; // the immediately enclosing outer scope, or NULL | 457 Scope* outer_scope_; // the immediately enclosing outer scope, or NULL |
469 Scope* inner_scope_; // an inner scope of this scope | 458 Scope* inner_scope_; // an inner scope of this scope |
470 Scope* sibling_; // a sibling inner scope of the outer scope of this scope. | 459 Scope* sibling_; // a sibling inner scope of the outer scope of this scope. |
471 | 460 |
472 // The variables declared in this scope: | 461 // The variables declared in this scope: |
473 // | 462 // |
474 // All user-declared variables (incl. parameters). For script scopes | 463 // All user-declared variables (incl. parameters). For script scopes |
475 // variables may be implicitly 'declared' by being used (possibly in | 464 // variables may be implicitly 'declared' by being used (possibly in |
476 // an inner scope) with no intervening with statements or eval calls. | 465 // an inner scope) with no intervening with statements or eval calls. |
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
828 // adjusting the scope of temporaries used when desugaring parameter | 817 // adjusting the scope of temporaries used when desugaring parameter |
829 // initializers. | 818 // initializers. |
830 void AddTemporary(Variable* var) { | 819 void AddTemporary(Variable* var) { |
831 // Temporaries are only placed in ClosureScopes. | 820 // Temporaries are only placed in ClosureScopes. |
832 DCHECK_EQ(GetClosureScope(), this); | 821 DCHECK_EQ(GetClosureScope(), this); |
833 temps_.Add(var, zone()); | 822 temps_.Add(var, zone()); |
834 } | 823 } |
835 | 824 |
836 ZoneList<Variable*>* temps() { return &temps_; } | 825 ZoneList<Variable*>* temps() { return &temps_; } |
837 | 826 |
| 827 void DeclareSloppyBlockFunction(const AstRawString* name, |
| 828 SloppyBlockFunctionStatement* statement) { |
| 829 sloppy_block_function_map_.Declare(zone(), name, statement); |
| 830 } |
| 831 |
838 SloppyBlockFunctionMap* sloppy_block_function_map() { | 832 SloppyBlockFunctionMap* sloppy_block_function_map() { |
839 return &sloppy_block_function_map_; | 833 return &sloppy_block_function_map_; |
840 } | 834 } |
841 | 835 |
842 // Resolve and fill in the allocation information for all variables | 836 // Resolve and fill in the allocation information for all variables |
843 // in this scopes. Must be called *after* all scopes have been | 837 // in this scopes. Must be called *after* all scopes have been |
844 // processed (parsed) to ensure that unresolved variables can be | 838 // processed (parsed) to ensure that unresolved variables can be |
845 // resolved properly. | 839 // resolved properly. |
846 // | 840 // |
847 // In the case of code compiled and run using 'eval', the context | 841 // In the case of code compiled and run using 'eval', the context |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
898 // Convenience variable; Subclass constructor only | 892 // Convenience variable; Subclass constructor only |
899 Variable* this_function_; | 893 Variable* this_function_; |
900 // Module descriptor; module scopes only. | 894 // Module descriptor; module scopes only. |
901 ModuleDescriptor* module_descriptor_; | 895 ModuleDescriptor* module_descriptor_; |
902 }; | 896 }; |
903 | 897 |
904 } // namespace internal | 898 } // namespace internal |
905 } // namespace v8 | 899 } // namespace v8 |
906 | 900 |
907 #endif // V8_AST_SCOPES_H_ | 901 #endif // V8_AST_SCOPES_H_ |
OLD | NEW |