1 /*
   2  * Copyright (c) 1999, 2012, 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 #include "precompiled.hpp"
  26 #include "ci/ciKlass.hpp"
  27 #include "ci/ciSymbol.hpp"
  28 #include "ci/ciUtilities.hpp"
  29 #include "oops/oop.inline.hpp"
  30 
  31 // ciKlass
  32 //
  33 // This class represents a Klass* in the HotSpot virtual
  34 // machine.
  35 
  36 // ------------------------------------------------------------------
  37 // ciKlass::ciKlass
  38 ciKlass::ciKlass(KlassHandle h_k) : ciType(h_k) {
  39   assert(get_Klass()->is_klass(), "wrong type");
  40   Klass* k = get_Klass();
  41   _layout_helper = k->layout_helper();
  42   Symbol* klass_name = k->name();
  43   assert(klass_name != NULL, "wrong ciKlass constructor");
  44   _name = CURRENT_ENV->get_symbol(klass_name);
  45 }
  46 
  47 // ------------------------------------------------------------------
  48 // ciKlass::ciKlass
  49 //
  50 // Nameless klass variant.
  51 ciKlass::ciKlass(KlassHandle h_k, ciSymbol* name) : ciType(h_k) {
  52   assert(get_Klass()->is_klass(), "wrong type");
  53   _name = name;
  54   _layout_helper = Klass::_lh_neutral_value;
  55 }
  56 
  57 // ------------------------------------------------------------------
  58 // ciKlass::ciKlass
  59 //
  60 // Unloaded klass variant.
  61 ciKlass::ciKlass(ciSymbol* name, BasicType bt) : ciType(bt) {
  62   _name = name;
  63   _layout_helper = Klass::_lh_neutral_value;
  64 }
  65 
  66 // ------------------------------------------------------------------
  67 // ciKlass::is_subtype_of
  68 bool ciKlass::is_subtype_of(ciKlass* that) {
  69   assert(is_loaded() && that->is_loaded(), "must be loaded");
  70   // Check to see if the klasses are identical.
  71   if (this == that) {
  72     return true;
  73   }
  74 
  75   VM_ENTRY_MARK;
  76   Klass* this_klass = get_Klass();
  77   Klass* that_klass = that->get_Klass();
  78   bool result = this_klass->is_subtype_of(that_klass);
  79 
  80   return result;
  81 }
  82 
  83 // ------------------------------------------------------------------
  84 // ciKlass::is_subclass_of
  85 bool ciKlass::is_subclass_of(ciKlass* that) {
  86   assert(this->is_loaded(), err_msg("must be loaded: %s", this->name()->as_quoted_ascii()));
  87   assert(that->is_loaded(), err_msg("must be loaded: %s", that->name()->as_quoted_ascii()));
  88   // Check to see if the klasses are identical.
  89 
  90   VM_ENTRY_MARK;
  91   Klass* this_klass = get_Klass();
  92   Klass* that_klass = that->get_Klass();
  93   bool result = this_klass->is_subclass_of(that_klass);
  94 
  95   return result;
  96 }
  97 
  98 // ------------------------------------------------------------------
  99 // ciKlass::super_depth
 100 juint ciKlass::super_depth() {
 101   assert(is_loaded(), "must be loaded");
 102 
 103   VM_ENTRY_MARK;
 104   Klass* this_klass = get_Klass();
 105   return this_klass->super_depth();
 106 }
 107 
 108 // ------------------------------------------------------------------
 109 // ciKlass::super_check_offset
 110 juint ciKlass::super_check_offset() {
 111   assert(is_loaded(), "must be loaded");
 112 
 113   VM_ENTRY_MARK;
 114   Klass* this_klass = get_Klass();
 115   return this_klass->super_check_offset();
 116 }
 117 
 118 // ------------------------------------------------------------------
 119 // ciKlass::super_of_depth
 120 ciKlass* ciKlass::super_of_depth(juint i) {
 121   assert(is_loaded(), "must be loaded");
 122 
 123   VM_ENTRY_MARK;
 124   Klass* this_klass = get_Klass();
 125   Klass* super = this_klass->primary_super_of_depth(i);
 126   return (super != NULL) ? CURRENT_THREAD_ENV->get_klass(super) : NULL;
 127 }
 128 
 129 // ------------------------------------------------------------------
 130 // ciKlass::can_be_primary_super
 131 bool ciKlass::can_be_primary_super() {
 132   assert(is_loaded(), "must be loaded");
 133 
 134   VM_ENTRY_MARK;
 135   Klass* this_klass = get_Klass();
 136   return this_klass->can_be_primary_super();
 137 }
 138 
 139 // ------------------------------------------------------------------
 140 // ciKlass::least_common_ancestor
 141 //
 142 // Get the shared parent of two klasses.
 143 //
 144 // Implementation note: this method currently goes "over the wall"
 145 // and does all of the work on the VM side.  It could be rewritten
 146 // to use the super() method and do all of the work (aside from the
 147 // lazy computation of super()) in native mode.  This may be
 148 // worthwhile if the compiler is repeatedly requesting the same lca
 149 // computation or possibly if most of the superklasses have already
 150 // been created as ciObjects anyway.  Something to think about...
 151 ciKlass*
 152 ciKlass::least_common_ancestor(ciKlass* that) {
 153   assert(is_loaded() && that->is_loaded(), "must be loaded");
 154   // Check to see if the klasses are identical.
 155   if (this == that) {
 156     return this;
 157   }
 158 
 159   VM_ENTRY_MARK;
 160   Klass* this_klass = get_Klass();
 161   Klass* that_klass = that->get_Klass();
 162   Klass* lca        = this_klass->LCA(that_klass);
 163 
 164   // Many times the LCA will be either this_klass or that_klass.
 165   // Treat these as special cases.
 166   if (lca == that_klass) {
 167     return that;
 168   }
 169   if (this_klass == lca) {
 170     return this;
 171   }
 172 
 173   // Create the ciInstanceKlass for the lca.
 174   ciKlass* result =
 175     CURRENT_THREAD_ENV->get_klass(lca);
 176 
 177   return result;
 178 }
 179 
 180 // ------------------------------------------------------------------
 181 // ciKlass::find_klass
 182 //
 183 // Find a klass using this klass's class loader.
 184 ciKlass* ciKlass::find_klass(ciSymbol* klass_name) {
 185   assert(is_loaded(), "cannot find_klass through an unloaded klass");
 186   return CURRENT_ENV->get_klass_by_name(this,
 187                                         klass_name, false);
 188 }
 189 
 190 // ------------------------------------------------------------------
 191 // ciKlass::java_mirror
 192 //
 193 // Get the instance of java.lang.Class corresponding to this klass.
 194 // If it is an unloaded instance or array klass, return an unloaded
 195 // mirror object of type Class.
 196 ciInstance* ciKlass::java_mirror() {
 197   GUARDED_VM_ENTRY(
 198     if (!is_loaded())
 199       return ciEnv::current()->get_unloaded_klass_mirror(this);
 200     oop java_mirror = get_Klass()->java_mirror();
 201     return CURRENT_ENV->get_instance(java_mirror);
 202   )
 203 }
 204 
 205 // ------------------------------------------------------------------
 206 // ciKlass::modifier_flags
 207 jint ciKlass::modifier_flags() {
 208   assert(is_loaded(), "not loaded");
 209   GUARDED_VM_ENTRY(
 210     return get_Klass()->modifier_flags();
 211   )
 212 }
 213 
 214 // ------------------------------------------------------------------
 215 // ciKlass::access_flags
 216 jint ciKlass::access_flags() {
 217   assert(is_loaded(), "not loaded");
 218   GUARDED_VM_ENTRY(
 219     return get_Klass()->access_flags().as_int();
 220   )
 221 }
 222 
 223 // ------------------------------------------------------------------
 224 // ciKlass::print_impl
 225 //
 226 // Implementation of the print method
 227 void ciKlass::print_impl(outputStream* st) {
 228   st->print(" name=");
 229   print_name_on(st);
 230 }
 231 
 232 // ------------------------------------------------------------------
 233 // ciKlass::print_name
 234 //
 235 // Print the name of this klass
 236 void ciKlass::print_name_on(outputStream* st) {
 237   name()->print_symbol_on(st);
 238 }