[orc-rt] Add rtti header and unit tests.
The orc-rt extensible RTTI mechanism is used to provide simple dynamic RTTI checks for orc-rt types that do not depend on standard C++ RTTI (meaning that they will work equally well for programs compiled with -fno-rtti).
This commit is contained in:
parent
ca0a8d99bb
commit
9039b591d0
@ -2,6 +2,7 @@ set(ORC_RT_HEADERS
|
||||
orc-rt-c/orc-rt.h
|
||||
orc-rt/bitmask-enum.h
|
||||
orc-rt/math.h
|
||||
orc-rt/rtti.h
|
||||
orc-rt/span.h
|
||||
)
|
||||
|
||||
|
138
orc-rt/include/orc-rt/rtti.h
Normal file
138
orc-rt/include/orc-rt/rtti.h
Normal file
@ -0,0 +1,138 @@
|
||||
//===------------- rtti.h - RTTI support for ORC RT -------------*- C++ -*-===//
|
||||
//
|
||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
||||
// See https://llvm.org/LICENSE.txt for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// \file
|
||||
//
|
||||
// Provides an extensible RTTI mechanism, that can be used regardless of whether
|
||||
// the runtime is built with -frtti or not. This is predominantly used to
|
||||
// support error handling.
|
||||
//
|
||||
// The RTTIRoot class defines methods for comparing type ids. Implementations
|
||||
// of these methods can be injected into new classes using the RTTIExtends
|
||||
// class template.
|
||||
//
|
||||
// E.g.
|
||||
//
|
||||
// @code{.cpp}
|
||||
// class MyBaseClass : public RTTIExtends<MyBaseClass, RTTIRoot> {
|
||||
// public:
|
||||
// virtual void foo() = 0;
|
||||
// };
|
||||
//
|
||||
// class MyDerivedClass1 : public RTTIExtends<MyDerivedClass1, MyBaseClass> {
|
||||
// public:
|
||||
// void foo() override {}
|
||||
// };
|
||||
//
|
||||
// class MyDerivedClass2 : public RTTIExtends<MyDerivedClass2, MyBaseClass> {
|
||||
// public:
|
||||
// void foo() override {}
|
||||
// };
|
||||
//
|
||||
// void fn() {
|
||||
// std::unique_ptr<MyBaseClass> B = std::make_unique<MyDerivedClass1>();
|
||||
// outs() << isa<MyBaseClass>(B) << "\n"; // Outputs "1".
|
||||
// outs() << isa<MyDerivedClass1>(B) << "\n"; // Outputs "1".
|
||||
// outs() << isa<MyDerivedClass2>(B) << "\n"; // Outputs "0'.
|
||||
// }
|
||||
//
|
||||
// @endcode
|
||||
//
|
||||
// Note:
|
||||
// This header was adapted from llvm/Support/ExtensibleRTTI.h, however the
|
||||
// data structures are not shared and the code need not be kept in sync.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#ifndef ORC_RT_RTTI_H
|
||||
#define ORC_RT_RTTI_H
|
||||
|
||||
namespace orc_rt {
|
||||
|
||||
template <typename ThisT, typename ParentT> class RTTIExtends;
|
||||
|
||||
/// Base class for the extensible RTTI hierarchy.
|
||||
///
|
||||
/// This class defines virtual methods, dynamicClassID and isA, that enable
|
||||
/// type comparisons.
|
||||
class RTTIRoot {
|
||||
public:
|
||||
virtual ~RTTIRoot() = default;
|
||||
|
||||
/// Returns the class ID for this type.
|
||||
static const void *classID() noexcept { return &ID; }
|
||||
|
||||
/// Returns the class ID for the dynamic type of this RTTIRoot instance.
|
||||
virtual const void *dynamicClassID() const noexcept = 0;
|
||||
|
||||
/// Returns true if this class's ID matches the given class ID.
|
||||
virtual bool isA(const void *const ClassID) const noexcept {
|
||||
return ClassID == classID();
|
||||
}
|
||||
|
||||
/// Check whether this instance is a subclass of QueryT.
|
||||
template <typename QueryT> bool isA() const noexcept {
|
||||
return isA(QueryT::classID());
|
||||
}
|
||||
|
||||
static bool classof(const RTTIRoot *R) noexcept { return R->isA<RTTIRoot>(); }
|
||||
|
||||
private:
|
||||
virtual void anchor();
|
||||
|
||||
static char ID;
|
||||
};
|
||||
|
||||
/// Inheritance utility for extensible RTTI.
|
||||
///
|
||||
/// Supports single inheritance only: A class can only have one
|
||||
/// ExtensibleRTTI-parent (i.e. a parent for which the isa<> test will work),
|
||||
/// though it can have many non-ExtensibleRTTI parents.
|
||||
///
|
||||
/// RTTIExtents uses CRTP so the first template argument to RTTIExtends is the
|
||||
/// newly introduced type, and the *second* argument is the parent class.
|
||||
///
|
||||
/// class MyType : public RTTIExtends<MyType, RTTIRoot> {
|
||||
/// ...
|
||||
/// };
|
||||
///
|
||||
/// class MyDerivedType : public RTTIExtends<MyDerivedType, MyType> {
|
||||
/// ...
|
||||
/// };
|
||||
///
|
||||
template <typename ThisT, typename ParentT> class RTTIExtends : public ParentT {
|
||||
public:
|
||||
// Inherit constructors and isA methods from ParentT.
|
||||
using ParentT::isA;
|
||||
using ParentT::ParentT;
|
||||
|
||||
static char ID;
|
||||
|
||||
static const void *classID() noexcept { return &ThisT::ID; }
|
||||
|
||||
const void *dynamicClassID() const noexcept override { return &ThisT::ID; }
|
||||
|
||||
bool isA(const void *const ClassID) const noexcept override {
|
||||
return ClassID == classID() || ParentT::isA(ClassID);
|
||||
}
|
||||
|
||||
static bool classof(const RTTIRoot *R) { return R->isA<ThisT>(); }
|
||||
};
|
||||
|
||||
template <typename ThisT, typename ParentT>
|
||||
char RTTIExtends<ThisT, ParentT>::ID = 0;
|
||||
|
||||
/// Returns true if the given value is an instance of the template type
|
||||
/// parameter.
|
||||
template <typename To, typename From> bool isa(const From &Value) noexcept {
|
||||
return To::classof(&Value);
|
||||
}
|
||||
|
||||
} // namespace orc_rt
|
||||
|
||||
#endif // ORC_RT_RTTI_H
|
@ -1,5 +1,6 @@
|
||||
set(files
|
||||
orc-rt-executor.cpp
|
||||
rtti.cpp
|
||||
)
|
||||
|
||||
add_library(orc-rt-executor STATIC ${files})
|
||||
|
24
orc-rt/lib/executor/rtti.cpp
Normal file
24
orc-rt/lib/executor/rtti.cpp
Normal file
@ -0,0 +1,24 @@
|
||||
//===- rtti.cpp -----------------------------------------------------------===//
|
||||
//
|
||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
||||
// See https://llvm.org/LICENSE.txt for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This file is a part of the ORC runtime support library.
|
||||
//
|
||||
// Note:
|
||||
// This source file was adapted from lib/Support/ExtensibleRTTI.cpp, however
|
||||
// the data structures are not shared and the code need not be kept in sync.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "orc-rt/rtti.h"
|
||||
|
||||
namespace orc_rt {
|
||||
|
||||
char RTTIRoot::ID = 0;
|
||||
void RTTIRoot::anchor() {}
|
||||
|
||||
} // namespace orc_rt
|
@ -14,6 +14,7 @@ endfunction()
|
||||
add_orc_rt_unittest(CoreTests
|
||||
bitmask-enum-test.cpp
|
||||
math-test.cpp
|
||||
rtti-test.cpp
|
||||
span-test.cpp
|
||||
DISABLE_LLVM_LINK_LLVM_DYLIB
|
||||
)
|
||||
|
52
orc-rt/unittests/rtti-test.cpp
Normal file
52
orc-rt/unittests/rtti-test.cpp
Normal file
@ -0,0 +1,52 @@
|
||||
//===-- rtti_test.cpp -----------------------------------------------------===//
|
||||
//
|
||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
||||
// See https://llvm.org/LICENSE.txt for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// Note:
|
||||
// This unit test was adapted from
|
||||
// llvm/unittests/Support/ExtensibleRTTITest.cpp
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "orc-rt/rtti.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
using namespace orc_rt;
|
||||
|
||||
namespace {
|
||||
|
||||
class MyBase : public RTTIExtends<MyBase, RTTIRoot> {};
|
||||
|
||||
class MyDerivedA : public RTTIExtends<MyDerivedA, MyBase> {};
|
||||
|
||||
class MyDerivedB : public RTTIExtends<MyDerivedB, MyBase> {};
|
||||
|
||||
} // end anonymous namespace
|
||||
|
||||
TEST(ExtensibleRTTITest, BaseCheck) {
|
||||
MyBase MB;
|
||||
MyDerivedA MDA;
|
||||
MyDerivedB MDB;
|
||||
|
||||
// Check MB properties.
|
||||
EXPECT_TRUE(isa<RTTIRoot>(MB));
|
||||
EXPECT_TRUE(isa<MyBase>(MB));
|
||||
EXPECT_FALSE(isa<MyDerivedA>(MB));
|
||||
EXPECT_FALSE(isa<MyDerivedB>(MB));
|
||||
|
||||
// Check MDA properties.
|
||||
EXPECT_TRUE(isa<RTTIRoot>(MDA));
|
||||
EXPECT_TRUE(isa<MyBase>(MDA));
|
||||
EXPECT_TRUE(isa<MyDerivedA>(MDA));
|
||||
EXPECT_FALSE(isa<MyDerivedB>(MDA));
|
||||
|
||||
// Check MDB properties.
|
||||
EXPECT_TRUE(isa<RTTIRoot>(MDB));
|
||||
EXPECT_TRUE(isa<MyBase>(MDB));
|
||||
EXPECT_FALSE(isa<MyDerivedA>(MDB));
|
||||
EXPECT_TRUE(isa<MyDerivedB>(MDB));
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user