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