Jlm
TestType.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2025 Nico Reißmann <nico.reissmann@gmail.com>
3  * See COPYING for terms of redistribution.
4  */
5 
6 #include <jlm/rvsdg/TestType.hpp>
7 #include <jlm/util/common.hpp>
8 #include <jlm/util/Hash.hpp>
9 #include <jlm/util/strfmt.hpp>
10 
11 #include <unordered_map>
12 
13 namespace jlm::rvsdg
14 {
15 
16 static std::string
17 ToString(const TypeKind kind)
18 {
19  switch (kind)
20  {
21  case TypeKind::Value:
22  return "Value";
23  case TypeKind::State:
24  return "State";
25  default:
26  throw std::logic_error("Unhandled type kind!");
27  }
28 }
29 
30 TestType::~TestType() noexcept = default;
31 
32 std::string
33 TestType::debug_string() const
34 {
35  return util::strfmt("TestType[", ToString(kind_), "]");
36 }
37 
38 bool
39 TestType::operator==(const Type & other) const noexcept
40 {
41  const auto testType = dynamic_cast<const TestType *>(&other);
42  return testType && kind_ == testType->kind_;
43 }
44 
45 std::size_t
46 TestType::ComputeHash() const noexcept
47 {
48  const auto typeHash = typeid(TestType).hash_code();
49  const auto kindHash = std::hash<TypeKind>()(kind_);
50  return util::CombineHashes(typeHash, kindHash);
51 }
52 
54 TestType::Kind() const noexcept
55 {
56  return kind_;
57 }
58 
59 std::shared_ptr<const TestType>
61 {
62  static const TestType stateType(TypeKind::State);
63  return std::shared_ptr<const TestType>(std::shared_ptr<void>(), &stateType);
64 }
65 
66 std::shared_ptr<const TestType>
68 {
69  static const TestType valueType(TypeKind::Value);
70  return std::shared_ptr<const TestType>(std::shared_ptr<void>(), &valueType);
71 }
72 
73 }
~TestType() noexcept override
TypeKind Kind() const noexcept override
Return the kind of this type.
Definition: TestType.cpp:54
constexpr TestType(const TypeKind kind) noexcept
Definition: TestType.hpp:22
static std::shared_ptr< const TestType > createStateType()
Definition: TestType.cpp:60
static std::shared_ptr< const TestType > createValueType()
Definition: TestType.cpp:67
bool operator==(const Type &other) const noexcept override
Definition: TestType.cpp:39
std::size_t ComputeHash() const noexcept override
Definition: TestType.cpp:46
static std::string ToString(const TypeKind kind)
Definition: TestType.cpp:17
TypeKind
The kinds of types supported in rvsdg.
Definition: type.hpp:22
@ State
Designate a state type.
@ Value
Designate a value type.
static std::string strfmt(Args... args)
Definition: strfmt.hpp:35
std::size_t CombineHashes(std::size_t hash, Args... args)
Definition: Hash.hpp:63