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