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