1 /*
  2  * Copyright (c) 1999, 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_CI_CIINSTANCEKLASS_HPP
 26 #define SHARE_CI_CIINSTANCEKLASS_HPP
 27 
 28 #include "ci/ciConstantPoolCache.hpp"
 29 #include "ci/ciFlags.hpp"
 30 #include "ci/ciKlass.hpp"
 31 #include "ci/ciSymbol.hpp"
 32 #include "oops/instanceKlass.hpp"
 33 
 34 // ciInstanceKlass
 35 //
 36 // This class represents a Klass* in the HotSpot virtual machine
 37 // whose Klass part is an InstanceKlass.  It may or may not
 38 // be loaded.
 39 class ciInstanceKlass : public ciKlass {
 40   CI_PACKAGE_ACCESS
 41   friend class ciBytecodeStream;
 42   friend class ciEnv;
 43   friend class ciExceptionHandler;
 44   friend class ciMethod;
 45   friend class ciField;
 46   friend class ciReplay;
 47   friend class CompileTrainingData;
 48 
 49 private:
 50   enum SubklassValue { subklass_unknown, subklass_false, subklass_true };
 51 
 52   jobject                _loader;
 53 
 54   InstanceKlass::ClassState _init_state;           // state of class
 55   bool                   _is_shared;
 56   bool                   _has_finalizer;
 57   SubklassValue          _has_subklass;
 58   bool                   _has_nonstatic_fields;
 59   bool                   _has_nonstatic_concrete_methods;
 60   bool                   _is_hidden;
 61   bool                   _is_record;
 62   bool                   _trust_final_fields;
 63   bool                   _has_trusted_loader;
 64 
 65   ciFlags                _flags;
 66 
 67   // Lazy fields get filled in only upon request.
 68   ciInstanceKlass*       _super;
 69   ciInstance*            _java_mirror;
 70 
 71   ciConstantPoolCache*   _field_cache;  // cached map index->field
 72   GrowableArray<ciField*>* _nonstatic_fields;  // ordered by JavaFieldStream
 73   int                    _has_injected_fields; // any non static injected fields? lazily initialized.
 74 
 75   // The possible values of the _implementor fall into following three cases:
 76   //   null: no implementor.
 77   //   A ciInstanceKlass that's not itself: one implementor.
 78   //   Itself: more than one implementor.
 79   ciInstanceKlass*       _implementor;
 80   GrowableArray<ciInstanceKlass*>* _transitive_interfaces;
 81 
 82   void compute_injected_fields();
 83   bool compute_injected_fields_helper();
 84   void compute_transitive_interfaces();
 85 
 86   ciField* get_nonstatic_field_by_offset(int field_offset);
 87 
 88 protected:
 89   ciInstanceKlass(Klass* k);
 90   ciInstanceKlass(ciSymbol* name, jobject loader);
 91 
 92   InstanceKlass* get_instanceKlass() const {
 93     return InstanceKlass::cast(get_Klass());
 94   }
 95 
 96   oop loader();
 97   jobject loader_handle();
 98 
 99   const char* type_string() { return "ciInstanceKlass"; }
100 
101   bool is_in_package_impl(const char* packagename, int len);
102 
103   void print_impl(outputStream* st);
104 
105   ciConstantPoolCache* field_cache();
106 
107   bool is_shared() { return _is_shared; }
108 
109   InstanceKlass::ClassState compute_shared_init_state();
110   bool compute_shared_has_subklass();
111   int  compute_nonstatic_fields();
112   GrowableArray<ciField*>* compute_nonstatic_fields_impl(GrowableArray<ciField*>* super_fields);
113   bool compute_has_trusted_loader();
114 
115   // Update the init_state for shared klasses
116   InstanceKlass::ClassState update_if_shared() {
117     if (_is_shared && is_loaded()) {
118       return compute_shared_init_state();
119     }
120     return _init_state;
121   }
122 
123   static InstanceKlass::ClassState compute_init_state(InstanceKlass* ik);
124 
125 public:
126   // Has this klass been initialized?
127   bool                   is_initialized() {
128     InstanceKlass::ClassState state = update_if_shared();
129     return state == InstanceKlass::fully_initialized;
130   }
131   bool                   is_not_initialized() {
132     InstanceKlass::ClassState state = update_if_shared();
133     return state < InstanceKlass::being_initialized;
134   }
135   // Is this klass being initialized?
136   bool                   is_being_initialized() {
137     InstanceKlass::ClassState state = update_if_shared();
138     return state == InstanceKlass::being_initialized;
139   }
140   // Has this klass been linked?
141   bool                   is_linked() {
142     InstanceKlass::ClassState state = update_if_shared();
143     return state >= InstanceKlass::linked;
144   }
145   // Is this klass in error state?
146   bool                   is_in_error_state() {
147     InstanceKlass::ClassState state = update_if_shared();
148     return state == InstanceKlass::initialization_error;
149   }
150 
151   // General klass information.
152   ciFlags                flags()          {
153     assert(is_loaded(), "must be loaded");
154     return _flags;
155   }
156 
157   // Fetch Klass::access_flags.
158   jint                   access_flags() { return flags().as_int(); }
159 
160   bool                   has_finalizer()  {
161     assert(is_loaded(), "must be loaded");
162     return _has_finalizer; }
163   bool                   has_subklass()   {
164     assert(is_loaded(), "must be loaded");
165     // Ignore cached subklass_false case.
166     // It could be invalidated by concurrent class loading and
167     // can result in type paradoxes during compilation when
168     // a subclass is observed, but has_subklass() returns false.
169     if (_has_subklass == subklass_true) {
170       return true;
171     }
172     if (flags().is_final()) {
173       return false;
174     }
175     return compute_shared_has_subklass();
176   }
177 
178   jint                   layout_helper_size_in_bytes()  {
179     return Klass::layout_helper_size_in_bytes(layout_helper());
180   }
181   jint                   size_helper()  {
182     return (Klass::layout_helper_size_in_bytes(layout_helper())
183             >> LogHeapWordSize);
184   }
185   jint                   has_nonstatic_fields()  {
186     assert(is_loaded(), "must be loaded");
187     return _has_nonstatic_fields; }
188   ciInstanceKlass*       super();
189   jint                   nof_implementors() {
190     ciInstanceKlass* impl;
191     assert(is_loaded(), "must be loaded");
192     impl = implementor();
193     if (impl == nullptr) {
194       return 0;
195     } else if (impl != this) {
196       return 1;
197     } else {
198       return 2;
199     }
200   }
201   bool has_nonstatic_concrete_methods()  {
202     assert(is_loaded(), "must be loaded");
203     return _has_nonstatic_concrete_methods;
204   }
205 
206   bool is_hidden() const {
207     return _is_hidden;
208   }
209 
210   bool is_record() const {
211     return _is_record;
212   }
213 
214   bool trust_final_fields() const {
215     return _trust_final_fields;
216   }
217 
218   ciInstanceKlass* get_canonical_holder(int offset);
219   ciField* get_field_by_offset(int field_offset, bool is_static);
220   ciField* get_field_by_name(ciSymbol* name, ciSymbol* signature, bool is_static);
221   BasicType get_field_type_by_offset(int field_offset, bool is_static);
222 
223   // total number of nonstatic fields (including inherited):
224   int nof_nonstatic_fields() {
225     if (_nonstatic_fields == nullptr)
226       return compute_nonstatic_fields();
227     else
228       return _nonstatic_fields->length();
229   }
230 
231   bool has_injected_fields() {
232     if (_has_injected_fields == -1) {
233       compute_injected_fields();
234     }
235     return _has_injected_fields > 0 ? true : false;
236   }
237 
238   bool has_object_fields() const;
239 
240   // nth nonstatic field (presented by ascending address)
241   ciField* nonstatic_field_at(int i) {
242     assert(_nonstatic_fields != nullptr, "");
243     return _nonstatic_fields->at(i);
244   }
245 
246   ciInstanceKlass* unique_concrete_subklass();
247   bool has_finalizable_subclass();
248 
249   bool has_class_initializer();
250 
251   bool contains_field_offset(int offset);
252 
253   // Get the instance of java.lang.Class corresponding to
254   // this klass.  This instance is used for locking of
255   // synchronized static methods of this klass.
256   ciInstance*            java_mirror();
257 
258   // Java access flags
259   bool is_public      () { return flags().is_public(); }
260   bool is_final       () { return flags().is_final(); }
261   bool is_super       () { return flags().is_super(); }
262   bool is_interface   () { return flags().is_interface(); }
263   bool is_abstract    () { return flags().is_abstract(); }
264 
265   ciMethod* find_method(ciSymbol* name, ciSymbol* signature);
266   // Note:  To find a method from name and type strings, use ciSymbol::make,
267   // but consider adding to vmSymbols.hpp instead.
268 
269   bool is_leaf_type();
270   ciInstanceKlass* implementor();
271 
272   ciInstanceKlass* unique_implementor() {
273     assert(is_loaded(), "must be loaded");
274     assert(is_interface(), "must be");
275     ciInstanceKlass* impl = implementor();
276     return (impl != this ? impl : nullptr);
277   }
278 
279   // Is the defining class loader of this class the default loader?
280   bool uses_default_loader() const;
281 
282   bool is_java_lang_Object() const;
283 
284   BasicType box_klass_type() const;
285   bool is_box_klass() const;
286   bool is_boxed_value_offset(int offset) const;
287 
288   // Is this klass in the given package?
289   bool is_in_package(const char* packagename) {
290     return is_in_package(packagename, (int) strlen(packagename));
291   }
292   bool is_in_package(const char* packagename, int len);
293 
294   // What kind of ciObject is this?
295   bool is_instance_klass() const { return true; }
296 
297   virtual ciKlass* exact_klass() {
298     if (is_loaded() && is_final() && !is_interface()) {
299       return this;
300     }
301     return nullptr;
302   }
303 
304   bool can_be_instantiated() {
305     assert(is_loaded(), "must be loaded");
306     return !is_interface() && !is_abstract();
307   }
308 
309   bool has_trusted_loader() const {
310     return _has_trusted_loader;
311   }
312   GrowableArray<ciInstanceKlass*>* transitive_interfaces() const;
313 
314   // Replay support
315 
316   // Dump the current state of this klass for compilation replay.
317   virtual void dump_replay_data(outputStream* out);
318 
319   static void dump_replay_instanceKlass(outputStream* out, InstanceKlass* ik);
320 
321 
322   // Return stable class name suitable for replay file.
323   const char *replay_name() const;
324 
325 #ifdef ASSERT
326   bool debug_final_field_at(int offset);
327   bool debug_stable_field_at(int offset);
328 #endif
329 };
330 
331 #endif // SHARE_CI_CIINSTANCEKLASS_HPP