Line data Source code
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
3 : // found in the LICENSE file.
4 :
5 : #ifndef V8_AST_VARIABLES_H_
6 : #define V8_AST_VARIABLES_H_
7 :
8 : #include "src/ast/ast-value-factory.h"
9 : #include "src/globals.h"
10 : #include "src/zone/zone.h"
11 :
12 : namespace v8 {
13 : namespace internal {
14 :
15 : // The AST refers to variables via VariableProxies - placeholders for the actual
16 : // variables. Variables themselves are never directly referred to from the AST,
17 : // they are maintained by scopes, and referred to from VariableProxies and Slots
18 : // after binding and variable allocation.
19 : class Variable final : public ZoneObject {
20 : public:
21 : Variable(Scope* scope, const AstRawString* name, VariableMode mode,
22 : VariableKind kind, InitializationFlag initialization_flag,
23 : MaybeAssignedFlag maybe_assigned_flag = kNotAssigned)
24 : : scope_(scope),
25 : name_(name),
26 : local_if_not_shadowed_(nullptr),
27 : next_(nullptr),
28 : index_(-1),
29 : initializer_position_(kNoSourcePosition),
30 : bit_field_(MaybeAssignedFlagField::encode(maybe_assigned_flag) |
31 : InitializationFlagField::encode(initialization_flag) |
32 : VariableModeField::encode(mode) |
33 : IsUsedField::encode(false) |
34 : ForceContextAllocationField::encode(false) |
35 : ForceHoleInitializationField::encode(false) |
36 : LocationField::encode(VariableLocation::UNALLOCATED) |
37 192676752 : VariableKindField::encode(kind)) {
38 : // Var declared variables never need initialization.
39 : DCHECK(!(mode == VAR && initialization_flag == kNeedsInitialization));
40 : }
41 :
42 : explicit Variable(Variable* other);
43 :
44 : // The source code for an eval() call may refer to a variable that is
45 : // in an outer scope about which we don't know anything (it may not
46 : // be the script scope). scope() is nullptr in that case. Currently the
47 : // scope is only used to follow the context chain length.
48 : Scope* scope() const { return scope_; }
49 :
50 : // This is for adjusting the scope of temporaries used when desugaring
51 : // parameter initializers.
52 365 : void set_scope(Scope* scope) { scope_ = scope; }
53 :
54 11173548 : Handle<String> name() const { return name_->string(); }
55 : const AstRawString* raw_name() const { return name_; }
56 : VariableMode mode() const { return VariableModeField::decode(bit_field_); }
57 : bool has_forced_context_allocation() const {
58 : return ForceContextAllocationField::decode(bit_field_);
59 : }
60 : void ForceContextAllocation() {
61 : DCHECK(IsUnallocated() || IsContextSlot() ||
62 : location() == VariableLocation::MODULE);
63 16528926 : bit_field_ = ForceContextAllocationField::update(bit_field_, true);
64 : }
65 : bool is_used() { return IsUsedField::decode(bit_field_); }
66 167974854 : void set_is_used() { bit_field_ = IsUsedField::update(bit_field_, true); }
67 : MaybeAssignedFlag maybe_assigned() const {
68 : return MaybeAssignedFlagField::decode(bit_field_);
69 : }
70 : void set_maybe_assigned() {
71 24428313 : bit_field_ = MaybeAssignedFlagField::update(bit_field_, kMaybeAssigned);
72 : }
73 :
74 : int initializer_position() { return initializer_position_; }
75 14037061 : void set_initializer_position(int pos) { initializer_position_ = pos; }
76 :
77 32461207 : bool IsUnallocated() const {
78 : return location() == VariableLocation::UNALLOCATED;
79 : }
80 2 : bool IsParameter() const { return location() == VariableLocation::PARAMETER; }
81 35 : bool IsStackLocal() const { return location() == VariableLocation::LOCAL; }
82 2767821 : bool IsStackAllocated() const { return IsParameter() || IsStackLocal(); }
83 285910 : bool IsContextSlot() const { return location() == VariableLocation::CONTEXT; }
84 : bool IsLookupSlot() const { return location() == VariableLocation::LOOKUP; }
85 : bool IsGlobalObjectProperty() const;
86 :
87 22604374 : bool is_dynamic() const { return IsDynamicVariableMode(mode()); }
88 :
89 : // Returns the InitializationFlag this Variable was created with.
90 : // Scope analysis may allow us to relax this initialization
91 : // requirement, which will be reflected in the return value of
92 : // binding_needs_init().
93 : InitializationFlag initialization_flag() const {
94 : return InitializationFlagField::decode(bit_field_);
95 : }
96 :
97 : // Whether this variable needs to be initialized with the hole at
98 : // declaration time. Only returns valid results after scope analysis.
99 2802328 : bool binding_needs_init() const {
100 : DCHECK_IMPLIES(initialization_flag() == kNeedsInitialization,
101 : IsLexicalVariableMode(mode()));
102 : DCHECK_IMPLIES(ForceHoleInitializationField::decode(bit_field_),
103 : initialization_flag() == kNeedsInitialization);
104 :
105 : // Always initialize if hole initialization was forced during
106 : // scope analysis.
107 5604656 : if (ForceHoleInitializationField::decode(bit_field_)) return true;
108 :
109 : // If initialization was not forced, no need for initialization
110 : // for stack allocated variables, since UpdateNeedsHoleCheck()
111 : // in scopes.cc has proven that no VariableProxy refers to
112 : // this variable in such a way that a runtime hole check
113 : // would be generated.
114 2767821 : if (IsStackAllocated()) return false;
115 :
116 : // Otherwise, defer to the flag set when this Variable was constructed.
117 1306814 : return initialization_flag() == kNeedsInitialization;
118 : }
119 :
120 : // Called during scope analysis when a VariableProxy is found to
121 : // reference this Variable in such a way that a hole check will
122 : // be required at runtime.
123 : void ForceHoleInitialization() {
124 : DCHECK_EQ(kNeedsInitialization, initialization_flag());
125 : DCHECK(IsLexicalVariableMode(mode()));
126 789990 : bit_field_ = ForceHoleInitializationField::update(bit_field_, true);
127 : }
128 :
129 32981 : bool throw_on_const_assignment(LanguageMode language_mode) const {
130 32981 : return kind() != SLOPPY_FUNCTION_NAME_VARIABLE || is_strict(language_mode);
131 : }
132 :
133 844 : bool is_function() const { return kind() == FUNCTION_VARIABLE; }
134 63945631 : bool is_this() const { return kind() == THIS_VARIABLE; }
135 : bool is_sloppy_function_name() const {
136 : return kind() == SLOPPY_FUNCTION_NAME_VARIABLE;
137 : }
138 :
139 : Variable* local_if_not_shadowed() const {
140 : DCHECK(mode() == DYNAMIC_LOCAL && local_if_not_shadowed_ != nullptr);
141 : return local_if_not_shadowed_;
142 : }
143 :
144 : void set_local_if_not_shadowed(Variable* local) {
145 5323 : local_if_not_shadowed_ = local;
146 : }
147 :
148 : VariableLocation location() const {
149 : return LocationField::decode(bit_field_);
150 : }
151 : VariableKind kind() const { return VariableKindField::decode(bit_field_); }
152 :
153 : int index() const { return index_; }
154 :
155 : bool IsReceiver() const {
156 : DCHECK(IsParameter());
157 :
158 : return index_ == -1;
159 : }
160 :
161 20353 : bool IsExport() const {
162 : DCHECK_EQ(location(), VariableLocation::MODULE);
163 : DCHECK_NE(index(), 0);
164 : return index() > 0;
165 : }
166 :
167 : void AllocateTo(VariableLocation location, int index) {
168 : DCHECK(IsUnallocated() ||
169 : (this->location() == location && this->index() == index));
170 : DCHECK_IMPLIES(location == VariableLocation::MODULE, index != 0);
171 28663250 : bit_field_ = LocationField::update(bit_field_, location);
172 : DCHECK_EQ(location, this->location());
173 16880768 : index_ = index;
174 : }
175 :
176 102482 : static InitializationFlag DefaultInitializationFlag(VariableMode mode) {
177 : DCHECK(IsDeclaredVariableMode(mode));
178 9414151 : return mode == VAR ? kCreatedInitialized : kNeedsInitialization;
179 : }
180 :
181 : typedef ThreadedList<Variable> List;
182 :
183 : private:
184 : Scope* scope_;
185 : const AstRawString* name_;
186 :
187 : // If this field is set, this variable references the stored locally bound
188 : // variable, but it might be shadowed by variable bindings introduced by
189 : // sloppy 'eval' calls between the reference scope (inclusive) and the
190 : // binding scope (exclusive).
191 : Variable* local_if_not_shadowed_;
192 : Variable* next_;
193 : int index_;
194 : int initializer_position_;
195 : uint16_t bit_field_;
196 :
197 : class VariableModeField : public BitField16<VariableMode, 0, 3> {};
198 : class VariableKindField
199 : : public BitField16<VariableKind, VariableModeField::kNext, 3> {};
200 : class LocationField
201 : : public BitField16<VariableLocation, VariableKindField::kNext, 3> {};
202 : class ForceContextAllocationField
203 : : public BitField16<bool, LocationField::kNext, 1> {};
204 : class IsUsedField
205 : : public BitField16<bool, ForceContextAllocationField::kNext, 1> {};
206 : class InitializationFlagField
207 : : public BitField16<InitializationFlag, IsUsedField::kNext, 1> {};
208 : class ForceHoleInitializationField
209 : : public BitField16<bool, InitializationFlagField::kNext, 1> {};
210 : class MaybeAssignedFlagField
211 : : public BitField16<MaybeAssignedFlag,
212 : ForceHoleInitializationField::kNext, 1> {};
213 : Variable** next() { return &next_; }
214 : friend List;
215 : };
216 : } // namespace internal
217 : } // namespace v8
218 :
219 : #endif // V8_AST_VARIABLES_H_
|