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