1 #ifdef USE_PRAGMA_IDENT_HDR
   2 #pragma ident "@(#)ciField.hpp  1.23 07/09/28 10:23:24 JVM"
   3 #endif
   4 /*
   5  * Copyright 1999-2006 Sun Microsystems, Inc.  All Rights Reserved.
   6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   7  *
   8  * This code is free software; you can redistribute it and/or modify it
   9  * under the terms of the GNU General Public License version 2 only, as
  10  * published by the Free Software Foundation.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  23  * CA 95054 USA or visit www.sun.com if you need additional information or
  24  * have any questions.
  25  *  
  26  */
  27 
  28 // ciField
  29 //
  30 // This class represents the result of a field lookup in the VM.
  31 // The lookup may not succeed, in which case the information in
  32 // the ciField will be incomplete.
  33 class ciField : public ResourceObj {
  34   CI_PACKAGE_ACCESS
  35   friend class ciEnv;
  36   friend class ciInstanceKlass;
  37   friend class NonStaticFieldFiller;
  38 
  39 private:
  40   ciFlags          _flags;
  41   ciInstanceKlass* _holder;
  42   ciSymbol*        _name;
  43   ciSymbol*        _signature;
  44   ciType*          _type;
  45   int              _offset;
  46   bool             _is_constant;
  47   ciInstanceKlass* _known_to_link_with;
  48   ciConstant       _constant_value;
  49 
  50   // Used for will_link
  51   int              _cp_index;
  52 
  53   ciType* compute_type();
  54   ciType* compute_type_impl();
  55 
  56   ciField(ciInstanceKlass* klass, int index);
  57   ciField(fieldDescriptor* fd);
  58 
  59   // shared constructor code
  60   void initialize_from(fieldDescriptor* fd);
  61 
  62   // The implementation of the print method.
  63   void print_impl(outputStream* st);
  64 
  65 public:
  66   ciFlags flags() { return _flags; }
  67 
  68   // Of which klass is this field a member?
  69   //
  70   // Usage note: the declared holder of a field is the class
  71   // referenced by name in the bytecodes.  The canonical holder
  72   // is the most general class which holds the field.  This
  73   // method returns the canonical holder.  The declared holder
  74   // can be accessed via a method in ciBytecodeStream.
  75   //
  76   // Ex.
  77   //     class A {
  78   //       public int f = 7;
  79   //     }
  80   //     class B extends A {
  81   //       public void test() {
  82   //         System.out.println(f);
  83   //       }
  84   //     }
  85   //
  86   //   A java compiler is permitted to compile the access to
  87   //   field f as:
  88   //   
  89   //     getfield B.f
  90   //
  91   //   In that case the declared holder of f would be B and
  92   //   the canonical holder of f would be A.
  93   ciInstanceKlass* holder() { return _holder; }
  94 
  95   // Name of this field?
  96   ciSymbol* name() { return _name; }
  97 
  98   // Signature of this field?
  99   ciSymbol* signature() { return _signature; }
 100 
 101   // Of what type is this field?
 102   ciType* type() { return (_type == NULL) ? compute_type() : _type; }
 103 
 104   // How is this field actually stored in memory?
 105   BasicType layout_type() { return type2field[(_type == NULL) ? T_OBJECT : _type->basic_type()]; }
 106 
 107   // How big is this field in memory?
 108   int size_in_bytes() { return type2aelembytes[layout_type()]; }
 109 
 110   // What is the offset of this field?
 111   int offset() {
 112     assert(_offset >= 1, "illegal call to offset()");
 113     return _offset;
 114   }
 115 
 116   // Same question, explicit units.  (Fields are aligned to the byte level.)
 117   int offset_in_bytes() {
 118     return offset();
 119   }
 120 
 121   // Is this field shared?
 122   bool is_shared() {
 123     // non-static fields of shared holders are cached
 124     return _holder->is_shared() && !is_static();
 125   }
 126 
 127   // Is this field a constant?
 128   //
 129   // Clarification: A field is considered constant if:
 130   //   1. The field is both static and final
 131   //   2. The canonical holder of the field has undergone
 132   //      static initialization.
 133   //   3. If the field is an object or array, then the oop
 134   //      in question is allocated in perm space.
 135   //   4. The field is not one of the special static/final
 136   //      non-constant fields.  These are java.lang.System.in
 137   //      and java.lang.System.out.  Abomination.
 138   //
 139   // Note: the check for case 4 is not yet implemented.
 140   bool is_constant() { return _is_constant; }
 141 
 142   // Get the constant value of this field.
 143   ciConstant constant_value() {
 144     assert(is_constant(), "illegal call to constant_value()");
 145     return _constant_value;
 146   }
 147 
 148   // Check for link time errors.  Accessing a field from a
 149   // certain class via a certain bytecode may or may not be legal.
 150   // This call checks to see if an exception may be raised by
 151   // an access of this field.
 152   //
 153   // Usage note: if the same field is accessed multiple times
 154   // in the same compilation, will_link will need to be checked
 155   // at each point of access.
 156   bool will_link(ciInstanceKlass* accessing_klass,
 157                  Bytecodes::Code bc);
 158 
 159   // Java access flags
 160   bool is_public      () { return flags().is_public(); }
 161   bool is_private     () { return flags().is_private(); }
 162   bool is_protected   () { return flags().is_protected(); }
 163   bool is_static      () { return flags().is_static(); }
 164   bool is_final       () { return flags().is_final(); }
 165   bool is_volatile    () { return flags().is_volatile(); }
 166   bool is_transient   () { return flags().is_transient(); }
 167 
 168   // Debugging output
 169   void print();
 170   void print_name_on(outputStream* st);
 171 };