1 /*
  2  * Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #ifndef SHARE_OOPS_ARRAYKLASS_HPP
 26 #define SHARE_OOPS_ARRAYKLASS_HPP
 27 
 28 #include "oops/klass.hpp"
 29 #include "oops/layoutKind.hpp"
 30 
 31 class fieldDescriptor;
 32 class klassVtable;
 33 class ObjArrayKlass;
 34 
 35 // ArrayKlass is the abstract baseclass for all array classes
 36 
 37 class ArrayKlass: public Klass {
 38   friend class VMStructs;
 39 
 40  public:
 41   enum ArrayProperties : uint32_t {
 42     DEFAULT         = 0,          // NULLABLE and ATOMIC
 43     NULL_RESTRICTED = 1 << 0,
 44     NON_ATOMIC      = 1 << 1,
 45     // FINAL           = 1 << 2,
 46     // VOLATILE        = 1 << 3
 47     INVALID         = 1 << 4,
 48     DUMMY           = 1 << 5      // Just to transition the code, to be removed ASAP
 49   };
 50 
 51   static bool is_null_restricted(ArrayProperties props) { return (props & NULL_RESTRICTED) != 0; }
 52   static bool is_non_atomic(ArrayProperties props) { return (props & NON_ATOMIC) != 0; }
 53 
 54   static ArrayProperties array_properties_from_layout(LayoutKind lk);
 55   static const char* array_properties_as_string(ArrayProperties props);
 56 
 57  private:
 58   // If you add a new field that points to any metaspace object, you
 59   // must add this field to ArrayKlass::metaspace_pointers_do().
 60   int      _dimension;         // This is n'th-dimensional array.
 61   ArrayProperties _properties;
 62   ObjArrayKlass* volatile _higher_dimension;  // Refers the (n+1)'th-dimensional array (if present).
 63   ArrayKlass* volatile    _lower_dimension;   // Refers the (n-1)'th-dimensional array (if present).
 64 
 65  protected:
 66   // Constructors
 67   // The constructor with the Symbol argument does the real array
 68   // initialization, the other is a dummy
 69   ArrayKlass(Symbol* name, KlassKind kind, ArrayProperties props, markWord prototype_header = markWord::prototype());
 70   ArrayKlass();
 71 
 72   // Create array_name for element klass
 73   static Symbol* create_element_klass_array_name(Klass* element_klass, TRAPS);
 74 
 75  public:
 76 
 77   // Testing operation
 78   DEBUG_ONLY(bool is_array_klass_slow() const override { return true; })
 79 
 80   // Returns the ObjArrayKlass for n'th dimension.
 81   ArrayKlass* array_klass(int n, TRAPS) override;
 82   ArrayKlass* array_klass_or_null(int n) override;
 83 
 84   // Returns the array class with this class as element type.
 85   ArrayKlass* array_klass(TRAPS) override;
 86   ArrayKlass* array_klass_or_null() override;
 87 
 88   // Instance variables
 89   int dimension() const                 { return _dimension;      }
 90   void set_dimension(int dimension)     { _dimension = dimension; }
 91 
 92   ArrayProperties properties() const { return _properties; }
 93   void set_properties(ArrayProperties props) { _properties = props; }
 94   static ByteSize properties_offset() { return byte_offset_of(ArrayKlass, _properties); }
 95 
 96   ObjArrayKlass* higher_dimension() const     { return _higher_dimension; }
 97   inline ObjArrayKlass* higher_dimension_acquire() const; // load with acquire semantics
 98   void set_higher_dimension(ObjArrayKlass* k) { _higher_dimension = k; }
 99   inline void release_set_higher_dimension(ObjArrayKlass* k); // store with release semantics
100 
101   ArrayKlass* lower_dimension() const      { return _lower_dimension; }
102   void set_lower_dimension(ArrayKlass* k)  { _lower_dimension = k; }
103 
104   // offset of first element, including any padding for the sake of alignment
105   int  array_header_in_bytes() const    { return layout_helper_header_size(layout_helper()); }
106   int  log2_element_size() const        { return layout_helper_log2_element_size(layout_helper()); }
107   // type of elements (T_OBJECT for both oop arrays and array-arrays)
108   BasicType element_type() const        { return layout_helper_element_type(layout_helper()); }
109 
110   InstanceKlass* java_super() const override;
111 
112   // Allocation
113   // Sizes points to the first dimension of the array, subsequent dimensions
114   // are always in higher memory.  The callers of these set that up.
115   virtual oop multi_allocate(int rank, jint* sizes, TRAPS);
116 
117   // find field according to JVM spec 5.4.3.2, returns the klass in which the field is defined
118   Klass* find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const override;
119 
120   // Lookup operations
121   Method* uncached_lookup_method(const Symbol* name,
122                                  const Symbol* signature,
123                                  OverpassLookupMode overpass_mode,
124                                  PrivateLookupMode private_mode = PrivateLookupMode::find) const override;
125 
126   static ArrayKlass* cast(Klass* k) {
127     return const_cast<ArrayKlass*>(cast(const_cast<const Klass*>(k)));
128   }
129 
130   static const ArrayKlass* cast(const Klass* k) {
131     assert(k->is_array_klass(), "cast to ArrayKlass");
132     return static_cast<const ArrayKlass*>(k);
133   }
134 
135   GrowableArray<Klass*>* compute_secondary_supers(int num_extra_slots,
136                                                   Array<InstanceKlass*>* transitive_interfaces) override;
137 
138   oop component_mirror() const;
139 
140   // Sizing
141   static int static_size(int header_size);
142 
143   void metaspace_pointers_do(MetaspaceClosure* iter) override;
144 
145   // Return a handle.
146   static void     complete_create_array_klass(ArrayKlass* k, Klass* super_klass, ModuleEntry* module, TRAPS);
147 
148   // JVMTI support
149   jint jvmti_class_status() const override;
150 
151 #if INCLUDE_CDS
152   // CDS support - remove and restore oops from metadata. Oops are not shared.
153   void remove_unshareable_info() override;
154   void remove_java_mirror() override;
155   void restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS);
156   void cds_print_value_on(outputStream* st) const;
157 #endif
158 
159   void log_array_class_load(Klass* k);
160   // Printing
161   void print_on(outputStream* st) const override;
162   void print_value_on(outputStream* st) const override;
163 
164   void oop_print_on(oop obj, outputStream* st) override;
165 
166   // Verification
167   void verify_on(outputStream* st) override;
168 
169   void oop_verify_on(oop obj, outputStream* st) override;
170 };
171 
172 class ArrayDescription : public StackObj {
173   public:
174    Klass::KlassKind _kind;
175    ArrayKlass::ArrayProperties _properties;
176    LayoutKind _layout_kind;
177    ArrayDescription(Klass::KlassKind k, ArrayKlass::ArrayProperties p, LayoutKind lk) { _kind = k; _properties = p; _layout_kind = lk; }
178  };
179 
180 #endif // SHARE_OOPS_ARRAYKLASS_HPP