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