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 #ifndef SHARE_VM_CI_CIFIELD_HPP
  26 #define SHARE_VM_CI_CIFIELD_HPP
  27 
  28 #include "ci/ciClassList.hpp"
  29 #include "ci/ciConstant.hpp"
  30 #include "ci/ciFlags.hpp"
  31 #include "ci/ciInstance.hpp"
  32 
  33 // ciField
  34 //
  35 // This class represents the result of a field lookup in the VM.
  36 // The lookup may not succeed, in which case the information in
  37 // the ciField will be incomplete.
  38 class ciField : public ResourceObj {
  39   CI_PACKAGE_ACCESS
  40   friend class ciEnv;
  41   friend class ciInstanceKlass;
  42 
  43 private:
  44   ciFlags          _flags;
  45   ciInstanceKlass* _holder;
  46   ciSymbol*        _name;
  47   ciSymbol*        _signature;
  48   ciType*          _type;
  49   int              _offset;
  50   bool             _is_constant;
  51   ciInstanceKlass* _known_to_link_with;
  52   ciConstant       _constant_value;
  53 
  54   // Used for will_link
  55   int              _cp_index;
  56 
  57   ciType* compute_type();
  58   ciType* compute_type_impl();
  59 
  60   ciField(ciInstanceKlass* klass, int index);
  61   ciField(fieldDescriptor* fd);
  62 
  63   // shared constructor code
  64   void initialize_from(fieldDescriptor* fd);
  65 
  66   // The implementation of the print method.
  67   void print_impl(outputStream* st);
  68 
  69 public:
  70   ciFlags flags() { return _flags; }
  71 
  72   // Of which klass is this field a member?
  73   //
  74   // Usage note: the declared holder of a field is the class
  75   // referenced by name in the bytecodes.  The canonical holder
  76   // is the most general class which holds the field.  This
  77   // method returns the canonical holder.  The declared holder
  78   // can be accessed via a method in ciBytecodeStream.
  79   //
  80   // Ex.
  81   //     class A {
  82   //       public int f = 7;
  83   //     }
  84   //     class B extends A {
  85   //       public void test() {
  86   //         System.out.println(f);
  87   //       }
  88   //     }
  89   //
  90   //   A java compiler is permitted to compile the access to
  91   //   field f as:
  92   //
  93   //     getfield B.f
  94   //
  95   //   In that case the declared holder of f would be B and
  96   //   the canonical holder of f would be A.
  97   ciInstanceKlass* holder() { return _holder; }
  98 
  99   // Name of this field?
 100   ciSymbol* name() { return _name; }
 101 
 102   // Signature of this field?
 103   ciSymbol* signature() { return _signature; }
 104 
 105   // Of what type is this field?
 106   ciType* type() { return (_type == NULL) ? compute_type() : _type; }
 107 
 108   // How is this field actually stored in memory?
 109   BasicType layout_type() { return type2field[(_type == NULL) ? T_OBJECT : _type->basic_type()]; }
 110 
 111   // How big is this field in memory?
 112   int size_in_bytes() { return type2aelembytes(layout_type()); }
 113 
 114   // What is the offset of this field?
 115   int offset() {
 116     assert(_offset >= 1, "illegal call to offset()");
 117     return _offset;
 118   }
 119 
 120   // Same question, explicit units.  (Fields are aligned to the byte level.)
 121   int offset_in_bytes() {
 122     return offset();
 123   }
 124 
 125   // // Is this field shared?
 126   // bool is_shared() {
 127   //   // non-static fields of shared holders are cached
 128   //   return _holder->is_shared() && !is_static();
 129   // }
 130 
 131   // Is this field a constant?
 132   //
 133   // Clarification: A field is considered constant if:
 134   //   1. The field is both static and final
 135   //   2. The canonical holder of the field has undergone
 136   //      static initialization.
 137   //   3. If the field is an object or array, then the oop
 138   //      in question is allocated in perm space.
 139   //   4. The field is not one of the special static/final
 140   //      non-constant fields.  These are java.lang.System.in
 141   //      and java.lang.System.out.  Abomination.
 142   //
 143   // Note: the check for case 4 is not yet implemented.
 144   bool is_constant() { return _is_constant; }
 145 
 146   // Get the constant value of this field.
 147   ciConstant constant_value() {
 148     assert(is_static() && is_constant(), "illegal call to constant_value()");
 149     return _constant_value;
 150   }
 151 
 152   // Get the constant value of non-static final field in the given
 153   // object.
 154   ciConstant constant_value_of(ciObject* object) {
 155     assert(!is_static() && is_constant(), "only if field is non-static constant");
 156     assert(object->is_instance(), "must be instance");
 157     return object->as_instance()->field_value(this);
 158   }
 159 
 160   // Check for link time errors.  Accessing a field from a
 161   // certain class via a certain bytecode may or may not be legal.
 162   // This call checks to see if an exception may be raised by
 163   // an access of this field.
 164   //
 165   // Usage note: if the same field is accessed multiple times
 166   // in the same compilation, will_link will need to be checked
 167   // at each point of access.
 168   bool will_link(ciInstanceKlass* accessing_klass,
 169                  Bytecodes::Code bc);
 170 
 171   // Java access flags
 172   bool is_public      () { return flags().is_public(); }
 173   bool is_private     () { return flags().is_private(); }
 174   bool is_protected   () { return flags().is_protected(); }
 175   bool is_static      () { return flags().is_static(); }
 176   bool is_final       () { return flags().is_final(); }
 177   bool is_volatile    () { return flags().is_volatile(); }
 178   bool is_transient   () { return flags().is_transient(); }
 179 
 180   // Debugging output
 181   void print();
 182   void print_name_on(outputStream* st);
 183 };
 184 
 185 #endif // SHARE_VM_CI_CIFIELD_HPP