OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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_VARIABLES_H_ | 5 #ifndef V8_AST_VARIABLES_H_ |
6 #define V8_AST_VARIABLES_H_ | 6 #define V8_AST_VARIABLES_H_ |
7 | 7 |
8 #include "src/ast/ast-value-factory.h" | 8 #include "src/ast/ast-value-factory.h" |
9 #include "src/zone.h" | 9 #include "src/zone.h" |
10 | 10 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 force_context_allocation_ = true; | 48 force_context_allocation_ = true; |
49 } | 49 } |
50 bool is_used() { return is_used_; } | 50 bool is_used() { return is_used_; } |
51 void set_is_used() { is_used_ = true; } | 51 void set_is_used() { is_used_ = true; } |
52 MaybeAssignedFlag maybe_assigned() const { return maybe_assigned_; } | 52 MaybeAssignedFlag maybe_assigned() const { return maybe_assigned_; } |
53 void set_maybe_assigned() { maybe_assigned_ = kMaybeAssigned; } | 53 void set_maybe_assigned() { maybe_assigned_ = kMaybeAssigned; } |
54 | 54 |
55 int initializer_position() { return initializer_position_; } | 55 int initializer_position() { return initializer_position_; } |
56 void set_initializer_position(int pos) { initializer_position_ = pos; } | 56 void set_initializer_position(int pos) { initializer_position_ = pos; } |
57 | 57 |
58 bool IsVariable(Handle<String> n) const { | |
59 return !is_this() && name().is_identical_to(n); | |
60 } | |
61 | |
62 bool IsUnallocated() const { | 58 bool IsUnallocated() const { |
63 return location_ == VariableLocation::UNALLOCATED; | 59 return location_ == VariableLocation::UNALLOCATED; |
64 } | 60 } |
65 bool IsParameter() const { return location_ == VariableLocation::PARAMETER; } | 61 bool IsParameter() const { return location_ == VariableLocation::PARAMETER; } |
66 bool IsStackLocal() const { return location_ == VariableLocation::LOCAL; } | 62 bool IsStackLocal() const { return location_ == VariableLocation::LOCAL; } |
67 bool IsStackAllocated() const { return IsParameter() || IsStackLocal(); } | 63 bool IsStackAllocated() const { return IsParameter() || IsStackLocal(); } |
68 bool IsContextSlot() const { return location_ == VariableLocation::CONTEXT; } | 64 bool IsContextSlot() const { return location_ == VariableLocation::CONTEXT; } |
69 bool IsGlobalSlot() const { return location_ == VariableLocation::GLOBAL; } | 65 bool IsGlobalSlot() const { return location_ == VariableLocation::GLOBAL; } |
70 bool IsUnallocatedOrGlobalSlot() const { | 66 bool IsUnallocatedOrGlobalSlot() const { |
71 return IsUnallocated() || IsGlobalSlot(); | 67 return IsUnallocated() || IsGlobalSlot(); |
(...skipping 11 matching lines...) Expand all Loading... |
83 bool is_function() const { return kind_ == FUNCTION; } | 79 bool is_function() const { return kind_ == FUNCTION; } |
84 bool is_this() const { return kind_ == THIS; } | 80 bool is_this() const { return kind_ == THIS; } |
85 bool is_arguments() const { return kind_ == ARGUMENTS; } | 81 bool is_arguments() const { return kind_ == ARGUMENTS; } |
86 | 82 |
87 // For script scopes, the "this" binding is provided by a ScriptContext added | 83 // For script scopes, the "this" binding is provided by a ScriptContext added |
88 // to the global's ScriptContextTable. This binding might not statically | 84 // to the global's ScriptContextTable. This binding might not statically |
89 // resolve to a Variable::THIS binding, instead being DYNAMIC_LOCAL. However | 85 // resolve to a Variable::THIS binding, instead being DYNAMIC_LOCAL. However |
90 // any variable named "this" does indeed refer to a Variable::THIS binding; | 86 // any variable named "this" does indeed refer to a Variable::THIS binding; |
91 // the grammar ensures this to be the case. So wherever a "this" binding | 87 // the grammar ensures this to be the case. So wherever a "this" binding |
92 // might be provided by the global, use HasThisName instead of is_this(). | 88 // might be provided by the global, use HasThisName instead of is_this(). |
93 bool HasThisName(Isolate* isolate) const { | 89 bool HasThisName(Isolate* isolate, |
94 return is_this() || *name() == *isolate->factory()->this_string(); | 90 HandleDereferenceMode deref_mode = |
| 91 HandleDereferenceMode::kAllowed) const { |
| 92 // Note: it is safe to dereference isolate->factory()->this_string() here |
| 93 // regardless of |deref_mode| because it is a constant root and so will |
| 94 // never be updated or moved. |
| 95 return is_this() || |
| 96 name_is_identical_to(isolate->factory()->this_string(), deref_mode); |
95 } | 97 } |
96 | 98 |
97 // True if the variable is named eval and not known to be shadowed. | 99 // True if the variable is named eval and not known to be shadowed. |
98 bool is_possibly_eval(Isolate* isolate) const { | 100 bool is_possibly_eval(Isolate* isolate, |
99 return IsVariable(isolate->factory()->eval_string()); | 101 HandleDereferenceMode deref_mode = |
| 102 HandleDereferenceMode::kAllowed) const { |
| 103 // Note: it is safe to dereference isolate->factory()->eval_string() here |
| 104 // regardless of |deref_mode| because it is a constant root and so will |
| 105 // never be updated or moved. |
| 106 return !is_this() && |
| 107 name_is_identical_to(isolate->factory()->eval_string(), deref_mode); |
100 } | 108 } |
101 | 109 |
102 Variable* local_if_not_shadowed() const { | 110 Variable* local_if_not_shadowed() const { |
103 DCHECK(mode_ == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL); | 111 DCHECK(mode_ == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL); |
104 return local_if_not_shadowed_; | 112 return local_if_not_shadowed_; |
105 } | 113 } |
106 | 114 |
107 void set_local_if_not_shadowed(Variable* local) { | 115 void set_local_if_not_shadowed(Variable* local) { |
108 local_if_not_shadowed_ = local; | 116 local_if_not_shadowed_ = local; |
109 } | 117 } |
110 | 118 |
111 VariableLocation location() const { return location_; } | 119 VariableLocation location() const { return location_; } |
112 int index() const { return index_; } | 120 int index() const { return index_; } |
113 InitializationFlag initialization_flag() const { | 121 InitializationFlag initialization_flag() const { |
114 return initialization_flag_; | 122 return initialization_flag_; |
115 } | 123 } |
116 | 124 |
117 void AllocateTo(VariableLocation location, int index) { | 125 void AllocateTo(VariableLocation location, int index) { |
118 location_ = location; | 126 location_ = location; |
119 index_ = index; | 127 index_ = index; |
120 } | 128 } |
121 | 129 |
122 static int CompareIndex(Variable* const* v, Variable* const* w); | 130 static int CompareIndex(Variable* const* v, Variable* const* w); |
123 | 131 |
124 private: | 132 private: |
| 133 bool name_is_identical_to(Handle<Object> object, |
| 134 HandleDereferenceMode deref_mode) const { |
| 135 if (deref_mode == HandleDereferenceMode::kAllowed) { |
| 136 return *name() == *object; |
| 137 } else { |
| 138 // If handle dereference isn't allowed use the handle address for |
| 139 // identity. This depends on the variable name being internalized in a |
| 140 // CanonicalHandleScope, so that all handles created during the |
| 141 // internalization with identical values have identical locations, and any |
| 142 // handles created which point to roots have the root handle's location. |
| 143 return name().address() == object.address(); |
| 144 } |
| 145 } |
| 146 |
125 Scope* scope_; | 147 Scope* scope_; |
126 const AstRawString* name_; | 148 const AstRawString* name_; |
127 VariableMode mode_; | 149 VariableMode mode_; |
128 Kind kind_; | 150 Kind kind_; |
129 VariableLocation location_; | 151 VariableLocation location_; |
130 int index_; | 152 int index_; |
131 int initializer_position_; | 153 int initializer_position_; |
132 | 154 |
133 // If this field is set, this variable references the stored locally bound | 155 // If this field is set, this variable references the stored locally bound |
134 // variable, but it might be shadowed by variable bindings introduced by | 156 // variable, but it might be shadowed by variable bindings introduced by |
135 // sloppy 'eval' calls between the reference scope (inclusive) and the | 157 // sloppy 'eval' calls between the reference scope (inclusive) and the |
136 // binding scope (exclusive). | 158 // binding scope (exclusive). |
137 Variable* local_if_not_shadowed_; | 159 Variable* local_if_not_shadowed_; |
138 | 160 |
139 // Usage info. | 161 // Usage info. |
140 bool force_context_allocation_; // set by variable resolver | 162 bool force_context_allocation_; // set by variable resolver |
141 bool is_used_; | 163 bool is_used_; |
142 InitializationFlag initialization_flag_; | 164 InitializationFlag initialization_flag_; |
143 MaybeAssignedFlag maybe_assigned_; | 165 MaybeAssignedFlag maybe_assigned_; |
144 }; | 166 }; |
145 } // namespace internal | 167 } // namespace internal |
146 } // namespace v8 | 168 } // namespace v8 |
147 | 169 |
148 #endif // V8_AST_VARIABLES_H_ | 170 #endif // V8_AST_VARIABLES_H_ |
OLD | NEW |