v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
variables.h
Go to the documentation of this file.
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
10#include "src/common/globals.h"
12#include "src/zone/zone.h"
13
14namespace v8 {
15namespace internal {
16
17// The AST refers to variables via VariableProxies - placeholders for the actual
18// variables. Variables themselves are never directly referred to from the AST,
19// they are maintained by scopes, and referred to from VariableProxies and Slots
20// after binding and variable allocation.
21class Variable final : public ZoneObject {
22 public:
51
52 explicit Variable(Variable* other);
53
54 // The source code for an eval() call may refer to a variable that is
55 // in an outer scope about which we don't know anything (it may not
56 // be the script scope). scope() is nullptr in that case. Currently the
57 // scope is only used to follow the context chain length.
58 Scope* scope() const { return scope_; }
59
60 // This is for adjusting the scope of temporaries used when desugaring
61 // parameter initializers.
63
64 Handle<String> name() const { return name_->string(); }
65 const AstRawString* raw_name() const { return name_; }
76 bool is_static() const { return is_static_flag() == IsStaticFlag::kStatic; }
77
96 return;
97 }
98 // Private names are only initialized once by us.
99 if (name_->IsPrivateName()) {
100 return;
101 }
102 // If this variable is dynamically shadowing another variable, then that
103 // variable could also be assigned (in the non-shadowing case).
105 // Avoid repeatedly marking the same tree of variables by only recursing
106 // when this variable's maybe_assigned status actually changes.
107 if (!maybe_assigned()) {
109 }
113 }
115 }
116
120
123
124 bool IsUnallocated() const {
126 }
128 bool IsStackLocal() const { return location() == VariableLocation::LOCAL; }
129 bool IsStackAllocated() const { return IsParameter() || IsStackLocal(); }
131 bool IsLookupSlot() const { return location() == VariableLocation::LOOKUP; }
132 bool IsGlobalObjectProperty() const;
133
134 // True for 'let' and 'const' variables declared in the script scope of a REPL
135 // script.
136 bool IsReplGlobal() const;
137
138 bool is_dynamic() const { return IsDynamicVariableMode(mode()); }
139
140 // Returns the InitializationFlag this Variable was created with.
141 // Scope analysis may allow us to relax this initialization
142 // requirement, which will be reflected in the return value of
143 // binding_needs_init().
147
148 // Whether this variable needs to be initialized with the hole at
149 // declaration time. Only returns valid results after scope analysis.
150 bool binding_needs_init() const {
156
157 // Always initialize if hole initialization was forced during
158 // scope analysis.
159 if (IsHoleInitializationForced()) return true;
160
161 // If initialization was not forced, no need for initialization
162 // for stack allocated variables, since UpdateNeedsHoleCheck()
163 // in scopes.cc has proven that no VariableProxy refers to
164 // this variable in such a way that a runtime hole check
165 // would be generated.
166 if (IsStackAllocated()) return false;
167
168 // Otherwise, defer to the flag set when this Variable was constructed.
170 }
171
183
188
193
194 // Called during scope analysis when a VariableProxy is found to
195 // reference this Variable in such a way that a hole check will
196 // be required at runtime.
205
206 // The first N-1 lexical bindings that need hole checks in a compilation are
207 // numbered, where N is the number of bits in HoleCheckBitmap. This number is
208 // an index into a bitmap that the BytecodeGenerator uses to elide redundant
209 // hole checks.
210 using HoleCheckBitmap = uint64_t;
211
212 // The 0th index is reserved for bindings for which the BytecodeGenerator
213 // should not elide hole checks, such as for bindings beyond the first N-1.
214 //
215 // This index in the bitmap must always be 0.
216 static constexpr uint8_t kUncacheableHoleCheckBitmapIndex = 0;
217 static constexpr uint8_t kHoleCheckBitmapBits =
218 std::numeric_limits<HoleCheckBitmap>::digits;
219
224
226 ZoneVector<Variable*>& list) {
227 DCHECK(v8_flags.ignition_elide_redundant_tdz_checks);
228 uint8_t index = HoleCheckBitmapIndex();
230 index = list.size() + 1;
231 // The bitmap is full.
232 if (index == kHoleCheckBitmapBits) return;
233 AssignHoleCheckBitmapIndex(list, index);
234 }
235 bitmap |= HoleCheckBitmap{1} << index;
236 DCHECK_EQ(
238 }
239
241 uint8_t index = HoleCheckBitmapIndex();
242 bool result = bitmap & (HoleCheckBitmap{1} << index);
244 return result;
245 }
246
247 bool throw_on_const_assignment(LanguageMode language_mode) const {
248 return kind() != SLOPPY_FUNCTION_NAME_VARIABLE || is_strict(language_mode);
249 }
250
251 bool is_this() const { return kind() == THIS_VARIABLE; }
254 }
255
256 bool is_parameter() const { return kind() == PARAMETER_VARIABLE; }
260
267
269 return local_if_not_shadowed_ != nullptr;
270 }
271
275
280
281 int index() const { return index_; }
282
283 bool IsReceiver() const {
285
286 return index_ == -1;
287 }
288
289 bool IsExport() const {
291 DCHECK_NE(index(), 0);
292 return index() > 0;
293 }
294
297 (this->location() == location && this->index() == index));
300 DCHECK_EQ(location, this->location());
301 index_ = index;
302 }
303
310
316
317 // Rewrites the VariableLocation of repl script scope 'lets' to REPL_GLOBAL.
319
321
322 private:
325
326 // If this field is set, this variable references the stored locally bound
327 // variable, but it might be shadowed by variable bindings introduced by with
328 // blocks or sloppy 'eval' calls between the reference scope (inclusive) and
329 // the binding scope (exclusive).
334 uint16_t bit_field_;
336
340
344
346 uint8_t next_index);
347
357
361
362 Variable** next() { return &next_; }
363 friend List;
365};
366} // namespace internal
367} // namespace v8
368
369#endif // V8_AST_VARIABLES_H_
SourcePosition pos
static constexpr T decode(U value)
Definition bit-field.h:66
static constexpr U encode(T value)
Definition bit-field.h:55
static V8_NODISCARD constexpr U update(U previous, T value)
Definition bit-field.h:61
bool is_dynamic() const
Definition variables.h:138
Variable * local_if_not_shadowed() const
Definition variables.h:261
bool binding_needs_init() const
Definition variables.h:150
@ kHasHoleCheckUseInDifferentClosureScope
Definition variables.h:174
VariableMode mode() const
Definition variables.h:66
void ResetHoleCheckBitmapIndex()
Definition variables.h:220
bool IsStackAllocated() const
Definition variables.h:129
static constexpr uint8_t kUncacheableHoleCheckBitmapIndex
Definition variables.h:216
bool is_parameter() const
Definition variables.h:256
const AstRawString * name_
Definition variables.h:324
bool IsParameter() const
Definition variables.h:127
uint16_t hole_check_analysis_bit_field_
Definition variables.h:335
MaybeAssignedFlag maybe_assigned() const
Definition variables.h:88
bool is_this() const
Definition variables.h:251
bool IsReceiver() const
Definition variables.h:283
bool is_sloppy_block_function()
Definition variables.h:257
void set_mode(VariableMode mode)
Definition variables.h:67
void AllocateTo(VariableLocation location, int index)
Definition variables.h:295
bool IsLookupSlot() const
Definition variables.h:131
Variable * local_if_not_shadowed_
Definition variables.h:330
VariableLocation location() const
Definition variables.h:276
void set_local_if_not_shadowed(Variable *local)
Definition variables.h:272
bool IsHoleInitializationForced() const
Definition variables.h:184
bool requires_brand_check() const
Definition variables.h:117
bool HasRememberedHoleCheck(HoleCheckBitmap bitmap) const
Definition variables.h:240
bool has_local_if_not_shadowed() const
Definition variables.h:268
Variable ** next()
Definition variables.h:362
bool has_forced_context_allocation() const
Definition variables.h:78
static InitializationFlag DefaultInitializationFlag(VariableMode mode)
Definition variables.h:311
uint8_t HoleCheckBitmapIndex() const
Definition variables.h:341
void set_initializer_position(int pos)
Definition variables.h:122
VariableKind kind() const
Definition variables.h:279
ForceHoleInitializationFlag force_hole_initialization_flag_field() const
Definition variables.h:179
InitializationFlag initialization_flag() const
Definition variables.h:144
bool HasHoleCheckUseInSameClosureScope() const
Definition variables.h:189
bool IsExport() const
Definition variables.h:289
bool IsGlobalObjectProperty() const
Definition variables.cc:25
bool IsReplGlobal() const
Definition variables.cc:32
bool throw_on_const_assignment(LanguageMode language_mode) const
Definition variables.h:247
Variable(Scope *scope, const AstRawString *name, VariableMode mode, VariableKind kind, InitializationFlag initialization_flag, MaybeAssignedFlag maybe_assigned_flag=kNotAssigned, IsStaticFlag is_static_flag=IsStaticFlag::kNotStatic)
Definition variables.h:23
static constexpr uint8_t kHoleCheckBitmapBits
Definition variables.h:217
void RememberHoleCheckInBitmap(HoleCheckBitmap &bitmap, ZoneVector< Variable * > &list)
Definition variables.h:225
bool is_static() const
Definition variables.h:76
bool IsStackLocal() const
Definition variables.h:128
void set_is_static_flag(IsStaticFlag is_static_flag)
Definition variables.h:70
void ForceHoleInitialization(ForceHoleInitializationFlag flag)
Definition variables.h:197
Scope * scope() const
Definition variables.h:58
void set_scope(Scope *scope)
Definition variables.h:62
const AstRawString * raw_name() const
Definition variables.h:65
void ForceContextAllocation()
Definition variables.h:81
IsStaticFlag is_static_flag() const
Definition variables.h:73
bool IsContextSlot() const
Definition variables.h:130
void AssignHoleCheckBitmapIndex(ZoneVector< Variable * > &list, uint8_t next_index)
Definition variables.cc:50
bool is_sloppy_function_name() const
Definition variables.h:252
bool IsUnallocated() const
Definition variables.h:124
Handle< String > name() const
Definition variables.h:64
ZoneVector< RpoNumber > & result
SnapshotTable< OpIndex, VariableData >::Key Variable
Definition operations.h:82
@ kNeedsInitialization
Definition globals.h:2225
@ kCreatedInitialized
Definition globals.h:2225
constexpr int kNoSourcePosition
Definition globals.h:850
bool IsLexicalVariableMode(VariableMode mode)
Definition globals.h:2155
bool IsDeclaredVariableMode(VariableMode mode)
Definition globals.h:2120
bool IsImmutableLexicalVariableMode(VariableMode mode)
Definition globals.h:2145
@ SLOPPY_FUNCTION_NAME_VARIABLE
Definition globals.h:2113
@ SLOPPY_BLOCK_FUNCTION_VARIABLE
Definition globals.h:2112
@ PARAMETER_VARIABLE
Definition globals.h:2110
bool is_strict(LanguageMode language_mode)
Definition globals.h:777
V8_EXPORT_PRIVATE FlagValues v8_flags
bool IsPrivateMethodOrAccessorVariableMode(VariableMode mode)
Definition globals.h:2135
bool IsDynamicVariableMode(VariableMode mode)
Definition globals.h:2116
bool IsImmutableLexicalOrPrivateVariableMode(VariableMode mode)
Definition globals.h:2150
#define DCHECK_IMPLIES(v1, v2)
Definition logging.h:493
#define DCHECK_NE(v1, v2)
Definition logging.h:486
#define DCHECK(condition)
Definition logging.h:482
#define DCHECK_EQ(v1, v2)
Definition logging.h:485
#define V8_UNLIKELY(condition)
Definition v8config.h:660