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_ciclass RootVisitor;
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciusing TraceRootCallback = void (*)(RootVisitor&, const void* object);
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci// Implementation of the default TraceTrait handling GarbageCollected and
241cb0ef41Sopenharmony_ci// GarbageCollectedMixin.
251cb0ef41Sopenharmony_citemplate <typename T,
261cb0ef41Sopenharmony_ci          bool =
271cb0ef41Sopenharmony_ci              IsGarbageCollectedMixinTypeV<typename std::remove_const<T>::type>>
281cb0ef41Sopenharmony_cistruct TraceTraitImpl;
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci}  // namespace internal
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci/**
331cb0ef41Sopenharmony_ci * Callback for invoking tracing on a given object.
341cb0ef41Sopenharmony_ci *
351cb0ef41Sopenharmony_ci * \param visitor The visitor to dispatch to.
361cb0ef41Sopenharmony_ci * \param object The object to invoke tracing on.
371cb0ef41Sopenharmony_ci */
381cb0ef41Sopenharmony_ciusing TraceCallback = void (*)(Visitor* visitor, const void* object);
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci/**
411cb0ef41Sopenharmony_ci * Describes how to trace an object, i.e., how to visit all Oilpan-relevant
421cb0ef41Sopenharmony_ci * fields of an object.
431cb0ef41Sopenharmony_ci */
441cb0ef41Sopenharmony_cistruct TraceDescriptor {
451cb0ef41Sopenharmony_ci  /**
461cb0ef41Sopenharmony_ci   * Adjusted base pointer, i.e., the pointer to the class inheriting directly
471cb0ef41Sopenharmony_ci   * from GarbageCollected, of the object that is being traced.
481cb0ef41Sopenharmony_ci   */
491cb0ef41Sopenharmony_ci  const void* base_object_payload;
501cb0ef41Sopenharmony_ci  /**
511cb0ef41Sopenharmony_ci   * Callback for tracing the object.
521cb0ef41Sopenharmony_ci   */
531cb0ef41Sopenharmony_ci  TraceCallback callback;
541cb0ef41Sopenharmony_ci};
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_cinamespace internal {
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_cistruct V8_EXPORT TraceTraitFromInnerAddressImpl {
591cb0ef41Sopenharmony_ci  static TraceDescriptor GetTraceDescriptor(const void* address);
601cb0ef41Sopenharmony_ci};
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci/**
631cb0ef41Sopenharmony_ci * Trait specifying how the garbage collector processes an object of type T.
641cb0ef41Sopenharmony_ci *
651cb0ef41Sopenharmony_ci * Advanced users may override handling by creating a specialization for their
661cb0ef41Sopenharmony_ci * type.
671cb0ef41Sopenharmony_ci */
681cb0ef41Sopenharmony_citemplate <typename T>
691cb0ef41Sopenharmony_cistruct TraceTraitBase {
701cb0ef41Sopenharmony_ci  static_assert(internal::IsTraceableV<T>, "T must have a Trace() method");
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci  /**
731cb0ef41Sopenharmony_ci   * Accessor for retrieving a TraceDescriptor to process an object of type T.
741cb0ef41Sopenharmony_ci   *
751cb0ef41Sopenharmony_ci   * \param self The object to be processed.
761cb0ef41Sopenharmony_ci   * \returns a TraceDescriptor to process the object.
771cb0ef41Sopenharmony_ci   */
781cb0ef41Sopenharmony_ci  static TraceDescriptor GetTraceDescriptor(const void* self) {
791cb0ef41Sopenharmony_ci    return internal::TraceTraitImpl<T>::GetTraceDescriptor(
801cb0ef41Sopenharmony_ci        static_cast<const T*>(self));
811cb0ef41Sopenharmony_ci  }
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci  /**
841cb0ef41Sopenharmony_ci   * Function invoking the tracing for an object of type T.
851cb0ef41Sopenharmony_ci   *
861cb0ef41Sopenharmony_ci   * \param visitor The visitor to dispatch to.
871cb0ef41Sopenharmony_ci   * \param self The object to invoke tracing on.
881cb0ef41Sopenharmony_ci   */
891cb0ef41Sopenharmony_ci  static void Trace(Visitor* visitor, const void* self) {
901cb0ef41Sopenharmony_ci    static_cast<const T*>(self)->Trace(visitor);
911cb0ef41Sopenharmony_ci  }
921cb0ef41Sopenharmony_ci};
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci}  // namespace internal
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_citemplate <typename T>
971cb0ef41Sopenharmony_cistruct TraceTrait : public internal::TraceTraitBase<T> {};
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_cinamespace internal {
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_citemplate <typename T>
1021cb0ef41Sopenharmony_cistruct TraceTraitImpl<T, false> {
1031cb0ef41Sopenharmony_ci  static_assert(IsGarbageCollectedTypeV<T>,
1041cb0ef41Sopenharmony_ci                "T must be of type GarbageCollected or GarbageCollectedMixin");
1051cb0ef41Sopenharmony_ci  static TraceDescriptor GetTraceDescriptor(const void* self) {
1061cb0ef41Sopenharmony_ci    return {self, TraceTrait<T>::Trace};
1071cb0ef41Sopenharmony_ci  }
1081cb0ef41Sopenharmony_ci};
1091cb0ef41Sopenharmony_ci
1101cb0ef41Sopenharmony_citemplate <typename T>
1111cb0ef41Sopenharmony_cistruct TraceTraitImpl<T, true> {
1121cb0ef41Sopenharmony_ci  static TraceDescriptor GetTraceDescriptor(const void* self) {
1131cb0ef41Sopenharmony_ci    return internal::TraceTraitFromInnerAddressImpl::GetTraceDescriptor(self);
1141cb0ef41Sopenharmony_ci  }
1151cb0ef41Sopenharmony_ci};
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_ci}  // namespace internal
1181cb0ef41Sopenharmony_ci}  // namespace cppgc
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci#endif  // INCLUDE_CPPGC_TRACE_TRAIT_H_
121