1 /*
  2  * Copyright (c) 1997, 2020, 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_OOPS_OOPSHIERARCHY_HPP
 26 #define SHARE_OOPS_OOPSHIERARCHY_HPP
 27 
 28 #include "metaprogramming/integralConstant.hpp"
 29 #include "metaprogramming/primitiveConversions.hpp"
 30 #include "utilities/globalDefinitions.hpp"
 31 
 32 // OBJECT hierarchy
 33 // This hierarchy is a representation hierarchy, i.e. if A is a superclass
 34 // of B, A's representation is a prefix of B's representation.
 35 
 36 // Global offset instead of address for an oop within a java object.
 37 enum class narrowOop : uint32_t { null = 0 };
 38 
 39 // If compressed klass pointers then use narrowKlass.
 40 typedef juint  narrowKlass;
 41 
 42 typedef void* OopOrNarrowOopStar;
 43 
 44 #ifndef CHECK_UNHANDLED_OOPS
 45 
 46 typedef class oopDesc*                    oop;
 47 typedef class   instanceOopDesc*            instanceOop;
 48 typedef class   arrayOopDesc*               arrayOop;
 49 typedef class     objArrayOopDesc*            objArrayOop;
 50 typedef class     typeArrayOopDesc*           typeArrayOop;
 51 
 52 #else
 53 
 54 // When CHECK_UNHANDLED_OOPS is defined, an "oop" is a class with a
 55 // carefully chosen set of constructors and conversion operators to go
 56 // to and from the underlying oopDesc pointer type.
 57 //
 58 // Because oop and its subclasses <type>Oop are class types, arbitrary
 59 // conversions are not accepted by the compiler.  Applying a cast to
 60 // an oop will cause the best matched conversion operator to be
 61 // invoked returning the underlying oopDesc* type if appropriate.
 62 // No copy constructors, explicit user conversions or operators of
 63 // numerical type should be defined within the oop class. Most C++
 64 // compilers will issue a compile time error concerning the overloading
 65 // ambiguity between operators of numerical and pointer types. If
 66 // a conversion to or from an oop to a numerical type is needed,
 67 // use the inline template methods, cast_*_oop, defined below.
 68 //
 69 // Converting NULL to oop to Handle implicit is no longer accepted by the
 70 // compiler because there are too many steps in the conversion.  Use Handle()
 71 // instead, which generates less code anyway.
 72 
 73 class Thread;
 74 class oopDesc;
 75 
 76 extern "C" bool CheckUnhandledOops;
 77 
 78 class oop {
 79   oopDesc* _o;
 80 
 81   void register_oop();
 82   void unregister_oop();
 83 
 84   void register_if_checking() {
 85     if (CheckUnhandledOops) register_oop();
 86   }
 87 
 88 public:
 89   oop()             : _o(nullptr) { register_if_checking(); }
 90   oop(const oop& o) : _o(o._o)    { register_if_checking(); }
 91   oop(oopDesc* o)   : _o(o)       { register_if_checking(); }
 92   ~oop() {
 93     if (CheckUnhandledOops) unregister_oop();
 94   }
 95 
 96   oopDesc* obj() const                 { return _o; }
 97   oopDesc* operator->() const          { return _o; }
 98   operator oopDesc* () const           { return _o; }
 99 
100   bool operator==(const oop& o) const  { return _o == o._o; }
101   bool operator!=(const oop& o) const  { return _o != o._o; }
102 
103   bool operator==(std::nullptr_t) const     { return _o == nullptr; }
104   bool operator!=(std::nullptr_t) const     { return _o != nullptr; }
105 
106   oop& operator=(const oop& o)         { _o = o._o; return *this; }
107 };
108 
109 template<>
110 struct PrimitiveConversions::Translate<oop> : public TrueType {
111   typedef oop Value;
112   typedef oopDesc* Decayed;
113 
114   static Decayed decay(Value x) { return x.obj(); }
115   static Value recover(Decayed x) { return oop(x); }
116 };
117 
118 #define DEF_OOP(type)                                                      \
119    class type##OopDesc;                                                    \
120    class type##Oop : public oop {                                          \
121      public:                                                               \
122        type##Oop() : oop() {}                                              \
123        type##Oop(const type##Oop& o) : oop(o) {}                           \
124        type##Oop(const oop& o) : oop(o) {}                                 \
125        type##Oop(type##OopDesc* o) : oop((oopDesc*)o) {}                   \
126        operator type##OopDesc* () const { return (type##OopDesc*)obj(); }  \
127        type##OopDesc* operator->() const {                                 \
128             return (type##OopDesc*)obj();                                  \
129        }                                                                   \
130        type##Oop& operator=(const type##Oop& o) {                          \
131             oop::operator=(o);                                             \
132             return *this;                                                  \
133        }                                                                   \
134    };                                                                      \
135                                                                            \
136    template<>                                                              \
137    struct PrimitiveConversions::Translate<type##Oop> : public TrueType {   \
138      typedef type##Oop Value;                                              \
139      typedef type##OopDesc* Decayed;                                       \
140                                                                            \
141      static Decayed decay(Value x) { return (type##OopDesc*)x.obj(); }     \
142      static Value recover(Decayed x) { return type##Oop(x); }              \
143    };
144 
145 DEF_OOP(instance);
146 DEF_OOP(array);
147 DEF_OOP(objArray);
148 DEF_OOP(typeArray);
149 
150 #endif // CHECK_UNHANDLED_OOPS
151 
152 // Cast functions to convert to and from oops.
153 template <typename T> inline oop cast_to_oop(T value) {
154   return (oopDesc*)value;
155 }
156 template <typename T> inline T cast_from_oop(oop o) {
157   return (T)(CHECK_UNHANDLED_OOPS_ONLY((oopDesc*))o);
158 }
159 
160 // The metadata hierarchy is separate from the oop hierarchy
161 
162 //      class MetaspaceObj
163 class   ConstMethod;
164 class   ConstantPoolCache;
165 class   MethodData;
166 //      class Metadata
167 class   Method;
168 class   ConstantPool;
169 //      class CHeapObj
170 class   CompiledICHolder;
171 
172 
173 // The klass hierarchy is separate from the oop hierarchy.
174 
175 class Klass;
176 class   InstanceKlass;
177 class     InstanceMirrorKlass;
178 class     InstanceClassLoaderKlass;
179 class     InstanceRefKlass;
180 class   ArrayKlass;
181 class     ObjArrayKlass;
182 class     TypeArrayKlass;
183 
184 #endif // SHARE_OOPS_OOPSHIERARCHY_HPP