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