1 /*
2 * Copyright (c) 1999, 2026, 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
73 // Fields declared in the bytecode (without nested fields in flat fields),
74 // ordered in JavaFieldStream order, with superclasses first (i.e. from lang.java.Object
75 // to most derived class).
76 const GrowableArray<ciField*>* _declared_nonstatic_fields;
77
78 // Fields laid out in memory (flat fields are expanded into their components). The ciField object
79 // for each primitive component has the holder being this ciInstanceKlass or one of its
80 // superclasses.
81 // Fields are in the same order as in _declared_nonstatic_fields, but flat fields are replaced by
82 // the list of their own fields, ordered the same way (hierarchy traversed top-down, in
83 // JavaFieldStream order).
84 const GrowableArray<ciField*>* _nonstatic_fields;
85
86 int _has_injected_fields; // any non static injected fields? lazily initialized.
87
88 // The possible values of the _implementor fall into following three cases:
89 // null: no implementor.
90 // A ciInstanceKlass that's not itself: one implementor.
91 // Itself: more than one implementor.
92 ciInstanceKlass* _implementor;
93 GrowableArray<ciInstanceKlass*>* _transitive_interfaces;
94
95 void compute_injected_fields();
96 bool compute_injected_fields_helper();
97 void compute_transitive_interfaces();
98
99 ciField* get_nonstatic_field_by_offset(int field_offset);
100
101 protected:
102 ciInstanceKlass(Klass* k);
103 ciInstanceKlass(ciSymbol* name, jobject loader, BasicType bt = T_OBJECT); // for unloaded klasses
104
105 InstanceKlass* get_instanceKlass() const {
106 return InstanceKlass::cast(get_Klass());
107 }
108
109 oop loader();
110 jobject loader_handle();
111
112 const char* type_string() { return "ciInstanceKlass"; }
113
114 bool is_in_package_impl(const char* packagename, int len);
115
116 void print_impl(outputStream* st);
117
118 ciConstantPoolCache* field_cache();
119
120 bool is_shared() { return _is_shared; }
121
122 InstanceKlass::ClassState compute_init_state();
123 bool compute_shared_has_subklass();
124 void compute_nonstatic_fields();
125 void compute_nonstatic_fields_impl(const GrowableArray<ciField*>* super_declared_fields, const GrowableArray<ciField*>* super_fields);
126 bool compute_has_trusted_loader();
127
128 public:
129 // Has this klass been initialized?
130 bool is_initialized() {
131 InstanceKlass::ClassState state = compute_init_state();
132 return state == InstanceKlass::fully_initialized;
133 }
134 bool is_not_initialized() {
135 InstanceKlass::ClassState state = compute_init_state();
136 return state < InstanceKlass::being_initialized;
137 }
138 // Is this klass being initialized?
139 bool is_being_initialized() {
140 InstanceKlass::ClassState state = compute_init_state();
141 return state == InstanceKlass::being_initialized;
142 }
143 // Has this klass been linked?
144 bool is_linked() {
145 InstanceKlass::ClassState state = compute_init_state();
146 return state >= InstanceKlass::linked;
147 }
148 // Is this klass in error state?
149 bool is_in_error_state() {
150 InstanceKlass::ClassState state = compute_init_state();
151 return state == InstanceKlass::initialization_error;
152 }
153
154 // General klass information.
155 ciFlags flags() {
156 assert(is_loaded(), "must be loaded");
157 return _flags;
158 }
159
160 // Fetch Klass::access_flags.
161 jint access_flags() { return flags().as_int(); }
162
163 bool has_finalizer() {
164 assert(is_loaded(), "must be loaded");
165 return _has_finalizer; }
166 bool has_subklass() {
167 assert(is_loaded(), "must be loaded");
168 // Ignore cached subklass_false case.
169 // It could be invalidated by concurrent class loading and
170 // can result in type paradoxes during compilation when
171 // a subclass is observed, but has_subklass() returns false.
172 if (_has_subklass == subklass_true) {
173 return true;
174 }
175 if (flags().is_final()) {
176 return false;
177 }
178 return compute_shared_has_subklass();
179 }
180
181 jint layout_helper_size_in_bytes() {
182 return Klass::layout_helper_size_in_bytes(layout_helper());
183 }
184 jint size_helper() {
185 return (Klass::layout_helper_size_in_bytes(layout_helper())
186 >> LogHeapWordSize);
187 }
188 jint has_nonstatic_fields() {
189 assert(is_loaded(), "must be loaded");
190 return _has_nonstatic_fields; }
191 ciInstanceKlass* super();
192 jint nof_implementors() {
193 ciInstanceKlass* impl;
194 assert(is_loaded(), "must be loaded");
195 impl = implementor();
196 if (impl == nullptr) {
197 return 0;
198 } else if (impl != this) {
199 return 1;
200 } else {
201 return 2;
202 }
203 }
204 bool has_nonstatic_concrete_methods() {
205 assert(is_loaded(), "must be loaded");
206 return _has_nonstatic_concrete_methods;
207 }
208
209 bool is_hidden() const {
210 return _is_hidden;
211 }
212
213 bool is_record() const {
214 return _is_record;
215 }
216
217 bool trust_final_fields() const {
218 return _trust_final_fields;
219 }
220
221 ciInstanceKlass* get_canonical_holder(int offset);
222 ciField* get_field_by_offset(int field_offset, bool is_static);
223 ciField* get_field_by_name(ciSymbol* name, ciSymbol* signature, bool is_static);
224 // Get field descriptor at field_offset ignoring flattening
225 ciField* get_non_flat_field_by_offset(int field_offset);
226 // Get the index of the declared field that contains this offset
227 int field_index_by_offset(int offset);
228
229 // Total number of nonstatic fields (including inherited)
230 int nof_declared_nonstatic_fields() {
231 if (_declared_nonstatic_fields == nullptr) {
232 compute_nonstatic_fields();
233 }
234 return _declared_nonstatic_fields->length();
235 }
236 BasicType get_field_type_by_offset(int field_offset, bool is_static);
237
238 int nof_nonstatic_fields() {
239 if (_nonstatic_fields == nullptr) {
240 compute_nonstatic_fields();
241 }
242 return _nonstatic_fields->length();
243 }
244
245 bool has_injected_fields() {
246 if (_has_injected_fields == -1) {
247 compute_injected_fields();
248 }
249 return _has_injected_fields > 0 ? true : false;
250 }
251
252 bool has_object_fields() const;
253
254 ciField* declared_nonstatic_field_at(int i) {
255 assert(_declared_nonstatic_fields != nullptr, "should be initialized");
256 return _declared_nonstatic_fields->at(i);
257 }
258
259 ciField* nonstatic_field_at(int i) {
260 assert(_nonstatic_fields != nullptr, "");
261 return _nonstatic_fields->at(i);
262 }
263
264 ciInstanceKlass* unique_concrete_subklass();
265 bool has_finalizable_subclass();
266
267 bool has_class_initializer();
268
269 bool contains_field_offset(int offset) const;
270
271 // Get the instance of java.lang.Class corresponding to
272 // this klass. This instance is used for locking of
273 // synchronized static methods of this klass.
274 ciInstance* java_mirror();
275
276 // Java access flags
277 bool is_public () { return flags().is_public(); }
278 bool is_final () { return flags().is_final(); }
279 bool is_interface () { return flags().is_interface(); }
280 bool is_abstract () { return flags().is_abstract(); }
281 bool is_abstract_value_klass() { return is_abstract() && !flags().is_identity(); }
282
283 ciMethod* find_method(ciSymbol* name, ciSymbol* signature);
284 // Note: To find a method from name and type strings, use ciSymbol::make,
285 // but consider adding to vmSymbols.hpp instead.
286
287 bool is_leaf_type();
288 ciInstanceKlass* implementor();
289
290 ciInstanceKlass* unique_implementor() {
291 assert(is_loaded(), "must be loaded");
292 assert(is_interface(), "must be");
293 ciInstanceKlass* impl = implementor();
294 return (impl != this ? impl : nullptr);
295 }
296
297 virtual bool can_be_inline_klass(bool is_exact = false);
298
299 // Is the defining class loader of this class the default loader?
300 bool uses_default_loader() const;
301
302 bool is_java_lang_Object() const;
303
304 BasicType box_klass_type() const;
305 bool is_box_klass() const;
306 bool is_boxed_value_offset(int offset) const;
307
308 // Is this klass in the given package?
309 bool is_in_package(const char* packagename) {
310 return is_in_package(packagename, (int) strlen(packagename));
311 }
312 bool is_in_package(const char* packagename, int len);
313
314 // What kind of ciObject is this?
315 bool is_instance_klass() const { return true; }
316
317 virtual ciKlass* exact_klass() {
318 if (is_loaded() && is_final() && !is_interface()) {
319 return this;
320 }
321 return nullptr;
322 }
323
324 bool can_be_instantiated() {
325 assert(is_loaded(), "must be loaded");
326 return !is_interface() && !is_abstract();
327 }
328
329 bool has_trusted_loader() const {
330 return _has_trusted_loader;
331 }
332 GrowableArray<ciInstanceKlass*>* transitive_interfaces() const;
333
334 // Replay support
335
336 // Dump the current state of this klass for compilation replay.
337 virtual void dump_replay_data(outputStream* out);
338
339 static void dump_replay_instanceKlass(outputStream* out, InstanceKlass* ik);
340
341
342 // Return stable class name suitable for replay file.
343 const char *replay_name() const;
344
345 #ifdef ASSERT
346 bool debug_final_field_at(int offset);
347 bool debug_stable_field_at(int offset);
348 #endif
349 };
350
351 #endif // SHARE_CI_CIINSTANCEKLASS_HPP