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