v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
scope-info.h
Go to the documentation of this file.
1// Copyright 2015 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_OBJECTS_SCOPE_INFO_H_
6#define V8_OBJECTS_SCOPE_INFO_H_
7
11#include "src/objects/objects.h"
12#include "src/utils/utils.h"
13#include "testing/gtest/include/gtest/gtest_prod.h" // nogncheck
14#include "torque-generated/bit-fields.h"
15
16// Has to be the last include (doesn't have include guards):
18
19namespace v8 {
20namespace internal {
21
22// scope-info-tq.inc uses NameToIndexHashTable.
23class NameToIndexHashTable;
24
25#include "torque-generated/src/objects/scope-info-tq.inc"
26
27class SourceTextModuleInfo;
28class StringSet;
29class Zone;
30
34 // repl_mode flag is needed to disable inlining of 'const' variables in REPL
35 // mode.
41};
42
43// ScopeInfo represents information about different scopes of a source
44// program and the allocation of the scope's variables. Scope information
45// is stored in a compressed form in ScopeInfo objects and is used
46// at runtime (stack dumps, deoptimization, etc.).
47
48// This object provides quick access to scope info details for runtime
49// routines.
50class ScopeInfo : public TorqueGeneratedScopeInfo<ScopeInfo, HeapObject> {
51 public:
52 DEFINE_TORQUE_GENERATED_SCOPE_FLAGS()
53
55 class BodyDescriptor;
56
57 // Return the type of this scope.
58 ScopeType scope_type() const;
59
60 // Return the language mode of this scope.
62
63 // True if this scope is a (var) declaration scope.
64 bool is_declaration_scope() const;
65
66 // Does this scope make a sloppy eval call?
67 bool SloppyEvalCanExtendVars() const;
68
69 // Return the number of context slots for code if a context is allocated. This
70 // number consists of three parts:
71 // 1. Size of header for every context.
72 // 2. One context slot per context allocated local.
73 // 3. One context slot for the function name if it is context allocated.
74 // Parameters allocated in the context count as context allocated locals. If
75 // no contexts are allocated for this scope ContextLength returns 0.
76 int ContextLength() const;
77 int ContextHeaderLength() const;
78
79 // Returns true if the respective contexts have a context extension slot.
80 bool HasContextExtensionSlot() const;
81
82 // Returns true if there is a context with created context extension
83 // (meaningful only for contexts that call sloppy eval, see
84 // SloppyEvalCanExtendVars()).
85 bool SomeContextHasExtension() const;
87
88 // Does this scope declare a "this" binding?
89 bool HasReceiver() const;
90
91 // Does this scope declare a "this" binding, and the "this" binding is stack-
92 // or context-allocated?
93 bool HasAllocatedReceiver() const;
94
95 // Does this scope has class brand (for private methods)? If it's a class
96 // scope, this indicates whether the class has a private brand. If it's a
97 // constructor scope, this indicates whether it needs to initialize the
98 // brand.
99 bool ClassScopeHasPrivateBrand() const;
100
101 // Does this scope contain a saved class variable for checking receivers of
102 // static private methods?
103 bool HasSavedClassVariable() const;
104
105 // Does this scope declare a "new.target" binding?
106 bool HasNewTarget() const;
107
108 // Is this scope the scope of a named function expression?
110
112
113 // See SharedFunctionInfo::HasSharedName.
115
117
120
121 // Does this scope belong to a function?
122 bool HasPositionInfo() const;
123
124 bool IsWrappedFunctionScope() const;
125
126 // Return if contexts are allocated for this scope.
127 bool HasContext() const;
128
129 // Return if this is a function scope with "use asm".
130 inline bool IsAsmModule() const;
131
132 inline bool HasSimpleParameters() const;
133
134 // Return the function_name if present.
136
137 // The function's name if it is non-empty, otherwise the inferred name or an
138 // empty string.
140
141 // Return the function's inferred name if present.
142 // See SharedFunctionInfo::function_identifier.
144
145 // Position information accessors.
146 int StartPosition() const;
147 int EndPosition() const;
148 void SetPositionInfo(int start, int end);
149
150 int UniqueIdInScript() const;
151
153
154 // Return true if the local names are inlined in the scope info object.
155 inline bool HasInlinedLocalNames() const;
156
157 template <typename ScopeInfoPtr>
159
161 DirectHandle<ScopeInfo> scope_info);
162
164 Tagged<ScopeInfo> scope_info, const DisallowGarbageCollection& no_gc);
165
166 // Return the name of a given context local.
167 // It should only be used if inlined local names.
170 int var) const;
171
172 // Return the mode of the given context local.
173 VariableMode ContextLocalMode(int var) const;
174
175 // Return whether the given context local variable is static.
177
178 // Return the initialization flag of the given context local.
180
181 bool ContextLocalIsParameter(int var) const;
182 uint32_t ContextLocalParameterNumber(int var) const;
183
184 // Return the initialization flag of the given context local.
186
187 // Return true if this local was introduced by the compiler, and should not be
188 // exposed to the user in a debugger.
189 static bool VariableIsSynthetic(Tagged<String> name);
190
191 // Lookup support for serialized scope info. Returns the local context slot
192 // index for a given slot name if the slot is present; otherwise
193 // returns a value < 0. The name must be an internalized string.
194 // If the slot is present and mode != nullptr, sets *mode to the corresponding
195 // mode for that variable.
198 VariableLookupResult* lookup_result);
199
200 // Lookup metadata of a MODULE-allocated variable. Return 0 if there is no
201 // module variable with the given name (the index value of a MODULE variable
202 // is never 0).
204 InitializationFlag* init_flag,
205 MaybeAssignedFlag* maybe_assigned_flag);
206
207 int ModuleVariableCount() const;
208
209 // Lookup support for serialized scope info. Returns the function context
210 // slot index if the function name is present and context-allocated (named
211 // function expressions, only), otherwise returns a value < 0. The name
212 // must be an internalized string.
214
215 // Lookup support for serialized scope info. Returns the receiver context
216 // slot index if scope has a "this" binding, and the binding is
217 // context-allocated. Otherwise returns a value < 0.
218 int ReceiverContextSlotIndex() const;
219
220 // Returns the first parameter context slot index.
221 int ParametersStartIndex() const;
222
223 // Lookup support for serialized scope info. Returns the name and index of
224 // the saved class variable in context local slots if scope is a class scope
225 // and it contains static private methods that may be accessed.
226 std::pair<Tagged<String>, int> SavedClassVariable() const;
227
229
230 // Returns true if this ScopeInfo is linked to an outer ScopeInfo.
231 bool HasOuterScopeInfo() const;
232
233 // Returns true if this ScopeInfo was created for a debug-evaluate scope.
234 bool IsDebugEvaluateScope() const;
235
236 // Can be used to mark a ScopeInfo that looks like a with-scope as actually
237 // being a debug-evaluate scope.
239
240 // Return the outer ScopeInfo if present.
242
243 bool is_script_scope() const;
244
245 // Returns true if this ScopeInfo was created for a scope that skips the
246 // closest outer class when resolving private names.
248
249 // REPL mode scopes allow re-declaraction of let and const variables. They
250 // come from debug evaluate but are different to IsDebugEvaluateScope().
251 bool IsReplModeScope() const;
252
253 // For LiveEdit we ignore:
254 // - position info: "unchanged" functions are allowed to move in a script
255 // - module info: SourceTextModuleInfo::Equals compares exact FixedArray
256 // addresses which will never match for separate instances.
257 // - outer scope info: LiveEdit already analyses outer scopes of unchanged
258 // functions. Also checking it here will break in really subtle cases
259 // e.g. changing a let to a const in an outer function, which is fine.
260 bool Equals(Tagged<ScopeInfo> other, bool is_live_edit_compare = false,
261 int* out_last_checked_field = nullptr) const;
262
263 template <typename IsolateT>
264 static Handle<ScopeInfo> Create(IsolateT* isolate, Zone* zone, Scope* scope,
265 MaybeDirectHandle<ScopeInfo> outer_scope);
267 Isolate* isolate, MaybeDirectHandle<ScopeInfo> outer_scope);
269 Isolate* isolate);
272 Isolate* isolate);
274
275 // Serializes empty scope info.
277
278 inline uint32_t Flags() const;
279 inline int ParameterCount() const;
280 inline int ContextLocalCount() const;
281
290
291 static_assert(LanguageModeSize == 1 << LanguageModeBit::kSize);
292 static_assert(FunctionKindBits::is_valid(FunctionKind::kLastFunctionKind));
293
295
296 bool IsEmpty() const;
297
298 // Returns the size in bytes for a ScopeInfo with |length| slots.
299 static constexpr int SizeFor(int length) { return OffsetOfElementAt(length); }
300
301 // Gives access to raw memory which stores the ScopeInfo's data.
302 inline ObjectSlot data_start();
303
304 // Hash based on position info and flags. Falls back to flags + local count.
305 V8_EXPORT_PRIVATE uint32_t Hash();
306
307 private:
309
310 int ContextLocalNamesIndex() const;
311 int ContextLocalInfosIndex() const;
312 int SavedClassVariableInfoIndex() const;
313 int FunctionVariableInfoIndex() const;
314 int InferredFunctionNameIndex() const;
315 int OuterScopeInfoIndex() const;
316 int ModuleInfoIndex() const;
317 int ModuleVariableCountIndex() const;
318 int ModuleVariablesIndex() const;
319 int DependentCodeIndex() const;
320
321 // Raw access by slot index. These functions rely on the fact that everything
322 // in ScopeInfo is tagged. Each slot is tagged-pointer sized. Slot 0 is
323 // 'flags', the first field defined by ScopeInfo after the standard-size
324 // HeapObject header.
325 V8_EXPORT_PRIVATE Tagged<Object> get(int index) const;
326 Tagged<Object> get(PtrComprCageBase cage_base, int index) const;
327 // Setter that doesn't need write barrier.
328 void set(int index, Tagged<Smi> value);
329 // Setter with explicit barrier mode.
330 void set(int index, Tagged<Object> value,
332 void CopyElements(Isolate* isolate, int dst_index, Tagged<ScopeInfo> src,
333 int src_index, int len, WriteBarrierMode mode);
335 // The number of tagged-pointer-sized slots in the ScopeInfo after its
336 // standard HeapObject header.
337 V8_EXPORT_PRIVATE int length() const;
338
339 // Conversions between offset (bytes from the beginning of the object) and
340 // index (number of tagged-pointer-sized slots starting after the standard
341 // HeapObject header).
342 static constexpr int OffsetOfElementAt(int index) {
343 return HeapObject::kHeaderSize + index * kTaggedSize;
344 }
345 static constexpr int ConvertOffsetToIndex(int offset) {
348 return index;
349 }
350
353 BootstrappingType type);
354
355 int Lookup(Handle<String> name, int start, int end, VariableMode* mode,
356 VariableLocation* location, InitializationFlag* init_flag,
357 MaybeAssignedFlag* maybe_assigned_flag);
358
359 // Get metadata of i-th MODULE-allocated variable, where 0 <= i <
360 // ModuleVariableCount. The metadata is returned via out-arguments, which may
361 // be nullptr if the corresponding information is not requested
362 void ModuleVariable(int i, Tagged<String>* name, int* index,
363 VariableMode* mode = nullptr,
364 InitializationFlag* init_flag = nullptr,
365 MaybeAssignedFlag* maybe_assigned_flag = nullptr);
366
367 static const int kFunctionNameEntries =
368 TorqueGeneratedFunctionVariableInfoOffsets::kSize / kTaggedSize;
369 static const int kModuleVariableEntryLength =
370 TorqueGeneratedModuleVariableOffsets::kSize / kTaggedSize;
371
372 // Properties of variables.
373 DEFINE_TORQUE_GENERATED_VARIABLE_PROPERTIES()
374
375 friend class ScopeIterator;
376 friend std::ostream& operator<<(std::ostream& os, VariableAllocationInfo var);
377
379};
380
381std::ostream& operator<<(std::ostream& os, VariableAllocationInfo var);
382
383} // namespace internal
384} // namespace v8
385
386#include "src/objects/object-macros-undef.h"
387
388#endif // V8_OBJECTS_SCOPE_INFO_H_
friend Zone
Definition asm-types.cc:195
static constexpr int kHeaderSize
bool HasPositionInfo() const
bool HasContextExtensionSlot() const
uint32_t ContextLocalParameterNumber(int var) const
bool HasContextAllocatedFunctionName() const
V8_EXPORT_PRIVATE uint32_t Hash()
int FunctionContextSlotIndex(Tagged< String > name) const
static DirectHandle< ScopeInfo > CreateForBootstrapping(Isolate *isolate, BootstrappingType type)
void CopyElements(Isolate *isolate, int dst_index, Tagged< ScopeInfo > src, int src_index, int len, WriteBarrierMode mode)
V8_EXPORT_PRIVATE Tagged< Object > get(int index) const
V8_EXPORT_PRIVATE Tagged< Object > InferredFunctionName() const
void mark_some_context_has_extension()
Tagged< SourceTextModuleInfo > ModuleDescriptorInfo() const
int ParametersStartIndex() const
bool HasNewTarget() const
static DirectHandle< ScopeInfo > CreateGlobalThisBinding(Isolate *isolate)
bool HasSimpleParameters() const
V8_EXPORT_PRIVATE bool HasFunctionName() const
int ContextSlotIndex(DirectHandle< String > name)
Tagged< String > ContextInlinedLocalName(int var) const
void SetFunctionName(Tagged< UnionOf< Smi, String > > name)
void SetInferredFunctionName(Tagged< String > name)
bool ClassScopeHasPrivateBrand() const
bool PrivateNameLookupSkipsOuterClass() const
bool SomeContextHasExtension() const
bool Equals(Tagged< ScopeInfo > other, bool is_live_edit_compare=false, int *out_last_checked_field=nullptr) const
Definition scope-info.cc:25
bool IsReplModeScope() const
uint32_t Flags() const
int ContextLocalInfosIndex() const
bool is_declaration_scope() const
int Lookup(Handle< String > name, int start, int end, VariableMode *mode, VariableLocation *location, InitializationFlag *init_flag, MaybeAssignedFlag *maybe_assigned_flag)
bool HasOuterScopeInfo() const
int ModuleIndex(Tagged< String > name, VariableMode *mode, InitializationFlag *init_flag, MaybeAssignedFlag *maybe_assigned_flag)
static DirectHandle< ScopeInfo > CreateForShadowRealmNativeContext(Isolate *isolate)
void set(int index, Tagged< Smi > value)
FunctionKind function_kind() const
LanguageMode language_mode() const
int ContextLocalNamesIndex() const
Tagged< DependentCode > dependent_code() const
int InferredFunctionNameIndex() const
void ModuleVariable(int i, Tagged< String > *name, int *index, VariableMode *mode=nullptr, InitializationFlag *init_flag=nullptr, MaybeAssignedFlag *maybe_assigned_flag=nullptr)
ObjectSlot RawFieldOfElementAt(int index)
ScopeType scope_type() const
int DependentCodeIndex() const
bool HasAllocatedReceiver() const
V8_EXPORT_PRIVATE Tagged< UnionOf< Smi, String > > FunctionName() const
static const int kFunctionNameEntries
Definition scope-info.h:367
int ContextHeaderLength() const
static const int kModuleVariableEntryLength
Definition scope-info.h:369
bool IsWrappedFunctionScope() const
int UniqueIdInScript() const
static constexpr int OffsetOfElementAt(int index)
Definition scope-info.h:342
static constexpr int SizeFor(int length)
Definition scope-info.h:299
bool SloppyEvalCanExtendVars() const
bool IsDebugEvaluateScope() const
static V8_EXPORT_PRIVATE DirectHandle< ScopeInfo > CreateForEmptyFunction(Isolate *isolate)
bool HasSavedClassVariable() const
int InlinedLocalNamesLookup(Tagged< String > name)
IsStaticFlag ContextLocalIsStaticFlag(int var) const
std::pair< Tagged< String >, int > SavedClassVariable() const
static V8_EXPORT_PRIVATE DirectHandle< ScopeInfo > CreateForWithScope(Isolate *isolate, MaybeDirectHandle< ScopeInfo > outer_scope)
static DirectHandle< ScopeInfo > CreateForNativeContext(Isolate *isolate)
int ModuleVariablesIndex() const
int FunctionVariableInfoIndex() const
int ModuleVariableCountIndex() const
V8_EXPORT_PRIVATE bool HasInferredFunctionName() const
InitializationFlag ContextLocalInitFlag(int var) const
V8_EXPORT_PRIVATE bool HasSharedFunctionName() const
bool is_script_scope() const
static constexpr int ConvertOffsetToIndex(int offset)
Definition scope-info.h:345
int SavedClassVariableInfoIndex() const
int ReceiverContextSlotIndex() const
void SetPositionInfo(int start, int end)
bool HasInlinedLocalNames() const
int OuterScopeInfoIndex() const
MaybeAssignedFlag ContextLocalMaybeAssignedFlag(int var) const
Tagged< String > FunctionDebugName() const
static V8_EXPORT_PRIVATE Tagged< ScopeInfo > Empty(Isolate *isolate)
VariableMode ContextLocalMode(int var) const
V8_EXPORT_PRIVATE int length() const
static Handle< ScopeInfo > Create(IsolateT *isolate, Zone *zone, Scope *scope, MaybeDirectHandle< ScopeInfo > outer_scope)
Definition scope-info.cc:79
bool ContextLocalIsParameter(int var) const
Tagged< ScopeInfo > OuterScopeInfo() const
int ModuleVariableCount() const
static LocalNamesRange< DirectHandle< ScopeInfo > > IterateLocalNames(DirectHandle< ScopeInfo > scope_info)
static bool VariableIsSynthetic(Tagged< String > name)
int start
int end
int32_t offset
STL namespace.
constexpr int kTaggedSize
Definition globals.h:542
@ UPDATE_WRITE_BARRIER
Definition objects.h:55
typename detail::FlattenUnionHelper< Union<>, Ts... >::type UnionOf
Definition union.h:123
static const size_t LanguageModeSize
Definition globals.h:753
#define DECL_PRINTER(Name)
#define TQ_OBJECT_CONSTRUCTORS(Type)
#define DCHECK_EQ(v1, v2)
Definition logging.h:485
#define V8_EXPORT_PRIVATE
Definition macros.h:460
MaybeAssignedFlag maybe_assigned_flag
Definition scope-info.h:40