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
3 // found in the LICENSE file.
4 
5 #ifndef V8_HEAP_OBJECTS_VISITING_H_
6 #define V8_HEAP_OBJECTS_VISITING_H_
7 
8 #include "src/objects/fixed-array.h"
9 #include "src/objects/map.h"
10 #include "src/objects/objects.h"
11 #include "src/objects/visitors.h"
12 
13 namespace v8 {
14 namespace internal {
15 
16 #define TYPED_VISITOR_ID_LIST(V)        \
17   V(AllocationSite)                     \
18   V(BigInt)                             \
19   V(ByteArray)                          \
20   V(BytecodeArray)                      \
21   V(Cell)                               \
22   V(Code)                               \
23   V(CodeDataContainer)                  \
24   V(CoverageInfo)                       \
25   V(DataHandler)                        \
26   V(EmbedderDataArray)                  \
27   V(EphemeronHashTable)                 \
28   V(FeedbackCell)                       \
29   V(FeedbackMetadata)                   \
30   V(FixedDoubleArray)                   \
31   V(JSArrayBuffer)                      \
32   V(JSDataView)                         \
33   V(JSFunction)                         \
34   V(JSObject)                           \
35   V(JSTypedArray)                       \
36   V(WeakCell)                           \
37   V(JSWeakCollection)                   \
38   V(JSWeakRef)                          \
39   V(Map)                                \
40   V(NativeContext)                      \
41   V(PreparseData)                       \
42   V(PropertyArray)                      \
43   V(PropertyCell)                       \
44   V(PrototypeInfo)                      \
45   V(SmallOrderedHashMap)                \
46   V(SmallOrderedHashSet)                \
47   V(SmallOrderedNameDictionary)         \
48   V(SourceTextModule)                   \
49   V(SwissNameDictionary)                \
50   V(Symbol)                             \
51   V(SyntheticModule)                    \
52   V(TransitionArray)                    \
53   IF_WASM(V, WasmArray)                 \
54   IF_WASM(V, WasmCapiFunctionData)      \
55   IF_WASM(V, WasmExportedFunctionData)  \
56   IF_WASM(V, WasmFunctionData)          \
57   IF_WASM(V, WasmIndirectFunctionTable) \
58   IF_WASM(V, WasmInstanceObject)        \
59   IF_WASM(V, WasmJSFunctionData)        \
60   IF_WASM(V, WasmStruct)                \
61   IF_WASM(V, WasmTypeInfo)
62 
63 #define FORWARD_DECLARE(TypeName) class TypeName;
64 TYPED_VISITOR_ID_LIST(FORWARD_DECLARE)
TORQUE_VISITOR_ID_LIST(FORWARD_DECLARE)65 TORQUE_VISITOR_ID_LIST(FORWARD_DECLARE)
66 #undef FORWARD_DECLARE
67 
68 // The base class for visitors that need to dispatch on object type. The default
69 // behavior of all visit functions is to iterate body of the given object using
70 // the BodyDescriptor of the object.
71 //
72 // The visit functions return the size of the object cast to ResultType.
73 //
74 // This class is intended to be used in the following way:
75 //
76 //   class SomeVisitor : public HeapVisitor<ResultType, SomeVisitor> {
77 //     ...
78 //   }
79 template <typename ResultType, typename ConcreteVisitor>
80 class HeapVisitor : public ObjectVisitorWithCageBases {
81  public:
82   inline HeapVisitor(PtrComprCageBase cage_base,
83                      PtrComprCageBase code_cage_base);
84   inline explicit HeapVisitor(Isolate* isolate);
85   inline explicit HeapVisitor(Heap* heap);
86 
87   V8_INLINE ResultType Visit(HeapObject object);
88   V8_INLINE ResultType Visit(Map map, HeapObject object);
89   // A callback for visiting the map pointer in the object header.
90   V8_INLINE void VisitMapPointer(HeapObject host);
91 
92  protected:
93   // A guard predicate for visiting the object.
94   // If it returns false then the default implementations of the Visit*
95   // functions bailout from iterating the object pointers.
96   V8_INLINE bool ShouldVisit(HeapObject object) { return true; }
97   // Guard predicate for visiting the objects map pointer separately.
98   V8_INLINE bool ShouldVisitMapPointer() { return true; }
99   // If this predicate returns false, then the heap visitor will fail
100   // in default Visit implemention for subclasses of JSObject.
101   V8_INLINE bool AllowDefaultJSObjectVisit() { return true; }
102 
103 #define VISIT(TypeName) \
104   V8_INLINE ResultType Visit##TypeName(Map map, TypeName object);
105   TYPED_VISITOR_ID_LIST(VISIT)
106   TORQUE_VISITOR_ID_LIST(VISIT)
107 #undef VISIT
108   V8_INLINE ResultType VisitShortcutCandidate(Map map, ConsString object);
109   V8_INLINE ResultType VisitDataObject(Map map, HeapObject object);
110   V8_INLINE ResultType VisitJSObjectFast(Map map, JSObject object);
111   V8_INLINE ResultType VisitJSApiObject(Map map, JSObject object);
112   V8_INLINE ResultType VisitStruct(Map map, HeapObject object);
113   V8_INLINE ResultType VisitFreeSpace(Map map, FreeSpace object);
114 
115   template <typename T>
116   static V8_INLINE T Cast(HeapObject object);
117 };
118 
119 template <typename ConcreteVisitor>
120 class NewSpaceVisitor : public HeapVisitor<int, ConcreteVisitor> {
121  public:
122   V8_INLINE NewSpaceVisitor(Isolate* isolate);
123 
ShouldVisitMapPointer()124   V8_INLINE bool ShouldVisitMapPointer() { return false; }
125 
126   // Special cases for young generation.
127 
128   V8_INLINE int VisitNativeContext(Map map, NativeContext object);
129   V8_INLINE int VisitJSApiObject(Map map, JSObject object);
130 
VisitBytecodeArray(Map map,BytecodeArray object)131   int VisitBytecodeArray(Map map, BytecodeArray object) {
132     UNREACHABLE();
133     return 0;
134   }
135 
136   int VisitSharedFunctionInfo(Map map, SharedFunctionInfo object);
137   int VisitWeakCell(Map map, WeakCell weak_cell);
138 };
139 
140 class WeakObjectRetainer;
141 
142 // A weak list is single linked list where each element has a weak pointer to
143 // the next element. Given the head of the list, this function removes dead
144 // elements from the list and if requested records slots for next-element
145 // pointers. The template parameter T is a WeakListVisitor that defines how to
146 // access the next-element pointers.
147 template <class T>
148 Object VisitWeakList(Heap* heap, Object list, WeakObjectRetainer* retainer);
149 }  // namespace internal
150 }  // namespace v8
151 
152 #endif  // V8_HEAP_OBJECTS_VISITING_H_
153