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 #include "torque-generated/field-offsets.h"
13
14 namespace v8 {
15 namespace internal {
16
17 #define TYPED_VISITOR_ID_LIST(V) \
18 V(AllocationSite) \
19 V(BigInt) \
20 V(ByteArray) \
21 V(BytecodeArray) \
22 V(Cell) \
23 V(Code) \
24 V(CodeDataContainer) \
25 V(Context) \
26 V(CoverageInfo) \
27 V(DataHandler) \
28 V(EmbedderDataArray) \
29 V(EphemeronHashTable) \
30 V(FeedbackCell) \
31 V(FeedbackMetadata) \
32 V(FixedDoubleArray) \
33 V(JSArrayBuffer) \
34 V(JSDataView) \
35 V(JSFunction) \
36 V(JSObject) \
37 V(JSTypedArray) \
38 V(WeakCell) \
39 V(JSWeakCollection) \
40 V(JSWeakRef) \
41 V(Map) \
42 V(NativeContext) \
43 V(PreparseData) \
44 V(PropertyArray) \
45 V(PropertyCell) \
46 V(PrototypeInfo) \
47 V(SharedFunctionInfo) \
48 V(SmallOrderedHashMap) \
49 V(SmallOrderedHashSet) \
50 V(SmallOrderedNameDictionary) \
51 V(SourceTextModule) \
52 V(Symbol) \
53 V(SyntheticModule) \
54 V(TransitionArray) \
55 V(UncompiledDataWithoutPreparseData) \
56 V(UncompiledDataWithPreparseData) \
57 V(WasmArray) \
58 V(WasmIndirectFunctionTable) \
59 V(WasmInstanceObject) \
60 V(WasmStruct) \
61 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 ObjectVisitor {
81 public:
82 V8_INLINE ResultType Visit(HeapObject object);
83 V8_INLINE ResultType Visit(Map map, HeapObject object);
84
85 protected:
86 // A guard predicate for visiting the object.
87 // If it returns false then the default implementations of the Visit*
88 // functions bailout from iterating the object pointers.
89 V8_INLINE bool ShouldVisit(HeapObject object) { return true; }
90 // Guard predicate for visiting the objects map pointer separately.
91 V8_INLINE bool ShouldVisitMapPointer() { return true; }
92 // A callback for visiting the map pointer in the object header.
93 V8_INLINE void VisitMapPointer(HeapObject host);
94 // If this predicate returns false, then the heap visitor will fail
95 // in default Visit implemention for subclasses of JSObject.
96 V8_INLINE bool AllowDefaultJSObjectVisit() { return true; }
97
98 #define VISIT(TypeName) \
99 V8_INLINE ResultType Visit##TypeName(Map map, TypeName object);
100 TYPED_VISITOR_ID_LIST(VISIT)
101 TORQUE_VISITOR_ID_LIST(VISIT)
102 #undef VISIT
103 V8_INLINE ResultType VisitShortcutCandidate(Map map, ConsString object);
104 V8_INLINE ResultType VisitDataObject(Map map, HeapObject object);
105 V8_INLINE ResultType VisitJSObjectFast(Map map, JSObject object);
106 V8_INLINE ResultType VisitJSApiObject(Map map, JSObject object);
107 V8_INLINE ResultType VisitStruct(Map map, HeapObject object);
108 V8_INLINE ResultType VisitFreeSpace(Map map, FreeSpace object);
109
110 template <typename T>
111 static V8_INLINE T Cast(HeapObject object);
112 };
113
114 template <typename ConcreteVisitor>
115 class NewSpaceVisitor : public HeapVisitor<int, ConcreteVisitor> {
116 public:
ShouldVisitMapPointer()117 V8_INLINE bool ShouldVisitMapPointer() { return false; }
118
119 // Special cases for young generation.
120
121 V8_INLINE int VisitNativeContext(Map map, NativeContext object);
122 V8_INLINE int VisitJSApiObject(Map map, JSObject object);
123
VisitBytecodeArray(Map map,BytecodeArray object)124 int VisitBytecodeArray(Map map, BytecodeArray object) {
125 UNREACHABLE();
126 return 0;
127 }
128
129 int VisitSharedFunctionInfo(Map map, SharedFunctionInfo object);
130 int VisitWeakCell(Map map, WeakCell weak_cell);
131 };
132
133 class WeakObjectRetainer;
134
135 // A weak list is single linked list where each element has a weak pointer to
136 // the next element. Given the head of the list, this function removes dead
137 // elements from the list and if requested records slots for next-element
138 // pointers. The template parameter T is a WeakListVisitor that defines how to
139 // access the next-element pointers.
140 template <class T>
141 Object VisitWeakList(Heap* heap, Object list, WeakObjectRetainer* retainer);
142 } // namespace internal
143 } // namespace v8
144
145 #endif // V8_HEAP_OBJECTS_VISITING_H_
146