Path: blob/master/src/hotspot/share/utilities/fakeRttiSupport.hpp
40949 views
/*1* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_UTILITIES_FAKERTTISUPPORT_HPP25#define SHARE_UTILITIES_FAKERTTISUPPORT_HPP2627#include "utilities/globalDefinitions.hpp"28#include "utilities/debug.hpp"2930// Provides support for checked downcasts in a hierarchy of classes.31// The base class provides a member of this type, specialized on that32// base class and an associated tag type. Tags are small non-negative33// integer values uniquely associated with distinct classes in the34// hierarchy. A tag type is often an enum type.35//36// The concrete class specifies the concrete tag.37//38// The tag set specifies the set of classes in the derivation39// sequence. Classes in the derivation sequence add their associated40// tag during construction. Given the tag associated with a class, an41// object is an instance of that class if the tag is included in the42// object's set of recorded tags.43//44// A tag T is present in a tag set if the T'th bit of the tag set is45// one.46//47// Note: The representation of a tag set being uintx sets an upper48// bound on the size of a class hierarchy this utility can be used49// with.50template<typename T, typename TagType>51class FakeRttiSupport {52friend class VMStructs;53public:54// Construct with the indicated concrete tag, and include the55// concrete tag in the associated tag set.56explicit FakeRttiSupport(TagType concrete_tag) :57_tag_set(tag_bit(concrete_tag)), _concrete_tag(concrete_tag) { }5859// Construct with the indicated concrete tag and tag set.60// Note: This constructor is public only to allow clients to set up61// "unusual" (or perhaps buggy) fake RTTI configurations.62FakeRttiSupport(TagType concrete_tag, uintx tag_set) :63_tag_set(tag_set), _concrete_tag(validate_tag(concrete_tag)) { }6465// Get the concrete tag.66TagType concrete_tag() const { return _concrete_tag; }6768// Test whether tag is in the tag set.69bool has_tag(TagType tag) const {70return (_tag_set & tag_bit(tag)) != 0;71}7273// Return a new support object which is the same as this, except tag74// has been added to the tag set. The tag must not already be75// present in the tag set.76FakeRttiSupport add_tag(TagType tag) const {77uintx tbit = tag_bit(tag);78assert((_tag_set & tbit) == 0,79"Tag " UINTX_FORMAT " is already present in tag set: " UINTX_FORMAT,80(uintx)tag, _tag_set);81return FakeRttiSupport(_concrete_tag, _tag_set | tbit);82}8384private:85uintx _tag_set;86TagType _concrete_tag;8788static uintx tag_bit(TagType tag) {89return ((uintx)1) << validate_tag(tag);90}9192static TagType validate_tag(TagType tag) {93assert(0 <= tag, "Tag " INTX_FORMAT " is negative", (intx)tag);94assert(tag < BitsPerWord,95"Tag " UINTX_FORMAT " is too large", (uintx)tag);96return tag;97}98};99100#endif // SHARE_UTILITIES_FAKERTTISUPPORT_HPP101102103