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