11cb0ef41Sopenharmony_ci// Copyright 2020 the V8 project authors. All rights reserved. 21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be 31cb0ef41Sopenharmony_ci// found in the LICENSE file. 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci#ifndef INCLUDE_CPPGC_TRACE_TRAIT_H_ 61cb0ef41Sopenharmony_ci#define INCLUDE_CPPGC_TRACE_TRAIT_H_ 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ci#include <type_traits> 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ci#include "cppgc/type-traits.h" 111cb0ef41Sopenharmony_ci#include "v8config.h" // NOLINT(build/include_directory) 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_cinamespace cppgc { 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ciclass Visitor; 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_cinamespace internal { 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_ci// Implementation of the default TraceTrait handling GarbageCollected and 201cb0ef41Sopenharmony_ci// GarbageCollectedMixin. 211cb0ef41Sopenharmony_citemplate <typename T, 221cb0ef41Sopenharmony_ci bool = 231cb0ef41Sopenharmony_ci IsGarbageCollectedMixinTypeV<typename std::remove_const<T>::type>> 241cb0ef41Sopenharmony_cistruct TraceTraitImpl; 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ci} // namespace internal 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ci/** 291cb0ef41Sopenharmony_ci * Callback for invoking tracing on a given object. 301cb0ef41Sopenharmony_ci * 311cb0ef41Sopenharmony_ci * \param visitor The visitor to dispatch to. 321cb0ef41Sopenharmony_ci * \param object The object to invoke tracing on. 331cb0ef41Sopenharmony_ci */ 341cb0ef41Sopenharmony_ciusing TraceCallback = void (*)(Visitor* visitor, const void* object); 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ci/** 371cb0ef41Sopenharmony_ci * Describes how to trace an object, i.e., how to visit all Oilpan-relevant 381cb0ef41Sopenharmony_ci * fields of an object. 391cb0ef41Sopenharmony_ci */ 401cb0ef41Sopenharmony_cistruct TraceDescriptor { 411cb0ef41Sopenharmony_ci /** 421cb0ef41Sopenharmony_ci * Adjusted base pointer, i.e., the pointer to the class inheriting directly 431cb0ef41Sopenharmony_ci * from GarbageCollected, of the object that is being traced. 441cb0ef41Sopenharmony_ci */ 451cb0ef41Sopenharmony_ci const void* base_object_payload; 461cb0ef41Sopenharmony_ci /** 471cb0ef41Sopenharmony_ci * Callback for tracing the object. 481cb0ef41Sopenharmony_ci */ 491cb0ef41Sopenharmony_ci TraceCallback callback; 501cb0ef41Sopenharmony_ci}; 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_cinamespace internal { 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_cistruct V8_EXPORT TraceTraitFromInnerAddressImpl { 551cb0ef41Sopenharmony_ci static TraceDescriptor GetTraceDescriptor(const void* address); 561cb0ef41Sopenharmony_ci}; 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ci/** 591cb0ef41Sopenharmony_ci * Trait specifying how the garbage collector processes an object of type T. 601cb0ef41Sopenharmony_ci * 611cb0ef41Sopenharmony_ci * Advanced users may override handling by creating a specialization for their 621cb0ef41Sopenharmony_ci * type. 631cb0ef41Sopenharmony_ci */ 641cb0ef41Sopenharmony_citemplate <typename T> 651cb0ef41Sopenharmony_cistruct TraceTraitBase { 661cb0ef41Sopenharmony_ci static_assert(internal::IsTraceableV<T>, "T must have a Trace() method"); 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ci /** 691cb0ef41Sopenharmony_ci * Accessor for retrieving a TraceDescriptor to process an object of type T. 701cb0ef41Sopenharmony_ci * 711cb0ef41Sopenharmony_ci * \param self The object to be processed. 721cb0ef41Sopenharmony_ci * \returns a TraceDescriptor to process the object. 731cb0ef41Sopenharmony_ci */ 741cb0ef41Sopenharmony_ci static TraceDescriptor GetTraceDescriptor(const void* self) { 751cb0ef41Sopenharmony_ci return internal::TraceTraitImpl<T>::GetTraceDescriptor( 761cb0ef41Sopenharmony_ci static_cast<const T*>(self)); 771cb0ef41Sopenharmony_ci } 781cb0ef41Sopenharmony_ci 791cb0ef41Sopenharmony_ci /** 801cb0ef41Sopenharmony_ci * Function invoking the tracing for an object of type T. 811cb0ef41Sopenharmony_ci * 821cb0ef41Sopenharmony_ci * \param visitor The visitor to dispatch to. 831cb0ef41Sopenharmony_ci * \param self The object to invoke tracing on. 841cb0ef41Sopenharmony_ci */ 851cb0ef41Sopenharmony_ci static void Trace(Visitor* visitor, const void* self) { 861cb0ef41Sopenharmony_ci static_cast<const T*>(self)->Trace(visitor); 871cb0ef41Sopenharmony_ci } 881cb0ef41Sopenharmony_ci}; 891cb0ef41Sopenharmony_ci 901cb0ef41Sopenharmony_ci} // namespace internal 911cb0ef41Sopenharmony_ci 921cb0ef41Sopenharmony_citemplate <typename T> 931cb0ef41Sopenharmony_cistruct TraceTrait : public internal::TraceTraitBase<T> {}; 941cb0ef41Sopenharmony_ci 951cb0ef41Sopenharmony_cinamespace internal { 961cb0ef41Sopenharmony_ci 971cb0ef41Sopenharmony_citemplate <typename T> 981cb0ef41Sopenharmony_cistruct TraceTraitImpl<T, false> { 991cb0ef41Sopenharmony_ci static_assert(IsGarbageCollectedTypeV<T>, 1001cb0ef41Sopenharmony_ci "T must be of type GarbageCollected or GarbageCollectedMixin"); 1011cb0ef41Sopenharmony_ci static TraceDescriptor GetTraceDescriptor(const void* self) { 1021cb0ef41Sopenharmony_ci return {self, TraceTrait<T>::Trace}; 1031cb0ef41Sopenharmony_ci } 1041cb0ef41Sopenharmony_ci}; 1051cb0ef41Sopenharmony_ci 1061cb0ef41Sopenharmony_citemplate <typename T> 1071cb0ef41Sopenharmony_cistruct TraceTraitImpl<T, true> { 1081cb0ef41Sopenharmony_ci static TraceDescriptor GetTraceDescriptor(const void* self) { 1091cb0ef41Sopenharmony_ci return internal::TraceTraitFromInnerAddressImpl::GetTraceDescriptor(self); 1101cb0ef41Sopenharmony_ci } 1111cb0ef41Sopenharmony_ci}; 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ci} // namespace internal 1141cb0ef41Sopenharmony_ci} // namespace cppgc 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_ci#endif // INCLUDE_CPPGC_TRACE_TRAIT_H_ 117