Jlm
FunctionType.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Nico Reißmann <nico.reissmann@gmail.com>
3  * Copyright 2025 Helge Bahmann <hcb@chaoticmind.net>
4  * See COPYING for terms of redistribution.
5  */
6 
7 #include <sstream>
8 
10 #include <jlm/util/Hash.hpp>
11 
12 namespace jlm::rvsdg
13 {
14 
15 FunctionType::~FunctionType() noexcept = default;
16 
18  std::vector<std::shared_ptr<const jlm::rvsdg::Type>> argumentTypes,
19  std::vector<std::shared_ptr<const jlm::rvsdg::Type>> resultTypes)
20  : jlm::rvsdg::Type(),
21  ArgumentTypes_(std::move(argumentTypes)),
22  ResultTypes_(std::move(resultTypes))
23 {}
24 
25 std::string
27 {
28  std::stringstream ss;
29  ss << "fun (";
30  bool first = true;
31  for (const auto & argtype : ArgumentTypes_)
32  {
33  if (!first)
34  {
35  ss << ", ";
36  }
37  first = false;
38  ss << argtype->debug_string();
39  }
40 
41  ss << ") -> (";
42  first = true;
43  for (const auto & restype : ResultTypes_)
44  {
45  if (!first)
46  {
47  ss << ", ";
48  }
49  first = false;
50  ss << restype->debug_string();
51  }
52  ss << ")";
53  return ss.str();
54 }
55 
56 bool
57 FunctionType::operator==(const jlm::rvsdg::Type & other) const noexcept
58 {
59  if (auto fn = dynamic_cast<const FunctionType *>(&other))
60  {
61  if (ArgumentTypes_.size() != fn->ArgumentTypes_.size()
62  || ResultTypes_.size() != fn->ResultTypes_.size())
63  {
64  return false;
65  }
66  for (std::size_t n = 0; n < ArgumentTypes_.size(); ++n)
67  {
68  if (*ArgumentTypes_[n] != *fn->ArgumentTypes_[n])
69  {
70  return false;
71  }
72  }
73  for (std::size_t n = 0; n < ResultTypes_.size(); ++n)
74  {
75  if (*ResultTypes_[n] != *fn->ResultTypes_[n])
76  {
77  return false;
78  }
79  }
80  return true;
81  }
82  else
83  {
84  return false;
85  }
86 }
87 
88 std::size_t
89 FunctionType::ComputeHash() const noexcept
90 {
91  std::size_t seed = typeid(FunctionType).hash_code();
92 
94  for (auto argumentType : ArgumentTypes_)
95  {
96  util::combineHashesWithSeed(seed, argumentType->ComputeHash());
97  }
98 
100  for (auto resultType : ResultTypes_)
101  {
102  util::combineHashesWithSeed(seed, resultType->ComputeHash());
103  }
104 
105  return seed;
106 }
107 
108 TypeKind
109 FunctionType::Kind() const noexcept
110 {
111  return TypeKind::Value;
112 }
113 
114 std::shared_ptr<const FunctionType>
116  std::vector<std::shared_ptr<const jlm::rvsdg::Type>> argumentTypes,
117  std::vector<std::shared_ptr<const jlm::rvsdg::Type>> resultTypes)
118 {
119  return std::make_shared<FunctionType>(std::move(argumentTypes), std::move(resultTypes));
120 }
121 
122 }
Function type class.
FunctionType(std::vector< std::shared_ptr< const jlm::rvsdg::Type >> argumentTypes, std::vector< std::shared_ptr< const jlm::rvsdg::Type >> resultTypes)
std::vector< std::shared_ptr< const jlm::rvsdg::Type > > ResultTypes_
static std::shared_ptr< const FunctionType > Create(std::vector< std::shared_ptr< const jlm::rvsdg::Type >> argumentTypes, std::vector< std::shared_ptr< const jlm::rvsdg::Type >> resultTypes)
~FunctionType() noexcept override
std::string debug_string() const override
bool operator==(const jlm::rvsdg::Type &other) const noexcept override
TypeKind Kind() const noexcept override
Return the kind of this type.
std::vector< std::shared_ptr< const jlm::rvsdg::Type > > ArgumentTypes_
std::size_t ComputeHash() const noexcept override
TypeKind
The kinds of types supported in rvsdg.
Definition: type.hpp:22
@ Value
Designate a value type.
void combineHashesWithSeed(std::size_t &seed, std::size_t hash, Args... args)
Definition: Hash.hpp:45