/*
 * Copyright (c) 1997, 2026, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

#ifndef SHARE_OOPS_OBJARRAYKLASS_HPP
#define SHARE_OOPS_OBJARRAYKLASS_HPP

#include "oops/arrayKlass.hpp"
#include "utilities/macros.hpp"

class ClassLoaderData;

// ObjArrayKlass is the klass for objArrays

class ObjArrayKlass : public ArrayKlass {
  friend class Deoptimization;
  friend class oopFactory;
  friend class VMStructs;
  friend class ciInlineKlass;

 public:
  static const KlassKind Kind = ObjArrayKlassKind;

 private:
  // If you add a new field that points to any metaspace object, you
  // must add this field to ObjArrayKlass::metaspace_pointers_do().
  Klass* _element_klass;            // The klass of the elements of this array type
  Klass* _bottom_klass;             // The one-dimensional type (InstanceKlass or TypeArrayKlass)
  ObjArrayKlass* _next_refined_array_klass;

  static ArrayDescription array_layout_selection(Klass* element, ArrayProperties properties);
  ObjArrayKlass* allocate_klass_from_description(ArrayDescription ad, TRAPS);

  inline ObjArrayKlass* next_refined_array_klass_acquire() const;
  inline void release_set_next_refined_klass(ObjArrayKlass* ak);

 protected:
  // Constructor
  ObjArrayKlass(int n, Klass* element_klass, Symbol* name, KlassKind kind, ArrayProperties props);
  static ObjArrayKlass* allocate_klass(ClassLoaderData* loader_data, int n, Klass* k, Symbol* name, ArrayProperties props, TRAPS);

  ObjArrayKlass* allocate_klass_with_properties(ArrayProperties props, TRAPS);
  objArrayOop allocate_instance(int length, ArrayProperties props, TRAPS);

 protected:
  // Create array_name for element klass
  static Symbol* create_element_klass_array_name(JavaThread* current, Klass* element_klass);

 public:
  // For dummy objects
  ObjArrayKlass() {}

  Klass* element_klass() const      { return _element_klass; }

  ObjArrayKlass* klass_with_properties(ArrayProperties props, TRAPS);
  ObjArrayKlass* klass_from_description(ArrayDescription adesc, TRAPS);

  ObjArrayKlass* next_refined_array_klass() const   { return _next_refined_array_klass; }
  bool find_refined_array_klass(ObjArrayKlass* k);

  // Compiler/Interpreter offset
  static ByteSize element_klass_offset()            { return byte_offset_of(ObjArrayKlass, _element_klass); }
  static ByteSize next_refined_array_klass_offset() { return byte_offset_of(ObjArrayKlass, _next_refined_array_klass); }

  Klass* bottom_klass() const       { return _bottom_klass; }
  Klass** bottom_klass_addr()       { return &_bottom_klass; }

  ModuleEntry* module() const override;
  PackageEntry* package() const override;

  // Dispatched operation
  bool can_be_primary_super_slow() const override;
  GrowableArray<Klass*>* compute_secondary_supers(int num_extra_slots,
                                                  Array<InstanceKlass*>* transitive_interfaces) override;
  DEBUG_ONLY(bool is_objArray_klass_slow() const override { return true; })
  size_t oop_size(oop obj) const override;

  // Allocation
  static ObjArrayKlass* allocate_objArray_klass(ClassLoaderData* loader_data,
                                                int n, Klass* element_klass, TRAPS);

  oop multi_allocate(int rank, jint* sizes, TRAPS) override;
  virtual objArrayOop allocate_instance(int length, TRAPS);

  // Copying
  void copy_array(arrayOop s, int src_pos, arrayOop d, int dst_pos, int length, TRAPS) override;
  void array_copy_offsets_and_range_check(arrayOop s, int src_pos,
                                          arrayOop d, int dst_pos, int length, TRAPS);

  // Compute protection domain
  oop protection_domain() const override { return bottom_klass()->protection_domain(); }

  void metaspace_pointers_do(MetaspaceClosure* iter) override;

#if INCLUDE_CDS
  void remove_unshareable_info() override;
  void remove_java_mirror() override;
  void restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS);
#endif

 public:
  static ObjArrayKlass* cast(Klass* k) {
    return const_cast<ObjArrayKlass*>(cast(const_cast<const Klass*>(k)));
  }

  static const ObjArrayKlass* cast(const Klass* k) {
    assert(k->is_objArray_klass(), "cast to ObjArrayKlass");
    return static_cast<const ObjArrayKlass*>(k);
  }

  // Sizing
  static int header_size()                { return sizeof(ObjArrayKlass)/wordSize; }
  int size() const override               { return ArrayKlass::static_size(header_size()); }

  // Initialization (virtual from Klass)
  void initialize(TRAPS) override;

  // Oop fields (and metadata) iterators
  //
  // The ObjArrayKlass iterators also visits the Object's klass.

  // Iterate over oop elements and metadata.
  template <typename T, typename OopClosureType>
  inline void oop_oop_iterate(oop obj, OopClosureType* closure);

  // Iterate over oop elements and metadata.
  template <typename T, typename OopClosureType>
  inline void oop_oop_iterate_reverse(oop obj, OopClosureType* closure);

  // Iterate over oop elements within mr, and metadata.
  template <typename T, typename OopClosureType>
  inline void oop_oop_iterate_bounded(oop obj, OopClosureType* closure, MemRegion mr);

  // Iterate over all oop elements, and no metadata.
  template <typename T, class OopClosureType>
  inline void oop_oop_iterate_elements(objArrayOop a, OopClosureType* closure);

  // Iterate over oop elements within index range [start, end), and no metadata.
  template <typename T, class OopClosureType>
  inline void oop_oop_iterate_elements_range(objArrayOop a, OopClosureType* closure, int start, int end);

 private:
  // Iterate over all oop elements bounded by addresses [low, high), and no metadata.
  template <typename T, class OopClosureType>
  inline void oop_oop_iterate_elements_bounded(objArrayOop a, OopClosureType* closure, void* low, void* high);

 public:
  u2 compute_modifier_flags() const override;

 public:
  // Printing
  void print_on(outputStream* st) const override;
  void print_value_on(outputStream* st) const override;

  void oop_print_value_on(oop obj, outputStream* st) override;
#ifndef PRODUCT
  void oop_print_on      (oop obj, outputStream* st) override;
#endif //PRODUCT

  const char* internal_name() const override;

  // Verification
  void verify_on(outputStream* st) override;

  void oop_verify_on(oop obj, outputStream* st) override;
};

#endif // SHARE_OOPS_OBJARRAYKLASS_HPP
