1 //===- llvm/TextAPI/ArchitectureSet.h - ArchitectureSet ---------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Defines the architecture set.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_TEXTAPI_ARCHITECTURESET_H
14 #define LLVM_TEXTAPI_ARCHITECTURESET_H
16 #include "llvm/TextAPI/Architecture.h"
29 class ArchitectureSet
{
31 using ArchSetType
= uint32_t;
33 const static ArchSetType EndIndexVal
=
34 std::numeric_limits
<ArchSetType
>::max();
35 ArchSetType ArchSet
{0};
38 constexpr ArchitectureSet() = default;
39 constexpr ArchitectureSet(ArchSetType Raw
) : ArchSet(Raw
) {}
40 ArchitectureSet(Architecture Arch
) : ArchitectureSet() { set(Arch
); }
41 ArchitectureSet(const std::vector
<Architecture
> &Archs
);
43 static ArchitectureSet
All() { return ArchitectureSet(EndIndexVal
); }
45 void set(Architecture Arch
) {
46 if (Arch
== AK_unknown
)
48 ArchSet
|= 1U << static_cast<int>(Arch
);
51 ArchitectureSet
clear(Architecture Arch
) {
52 ArchSet
&= ~(1U << static_cast<int>(Arch
));
56 bool has(Architecture Arch
) const {
57 return ArchSet
& (1U << static_cast<int>(Arch
));
60 bool contains(ArchitectureSet Archs
) const {
61 return (ArchSet
& Archs
.ArchSet
) == Archs
.ArchSet
;
66 bool empty() const { return ArchSet
== 0; }
68 ArchSetType
rawValue() const { return ArchSet
; }
71 return has(AK_i386
) || has(AK_x86_64
) || has(AK_x86_64h
);
74 template <typename Ty
> class arch_iterator
{
76 using iterator_category
= std::forward_iterator_tag
;
77 using value_type
= Architecture
;
78 using difference_type
= std::size_t;
79 using pointer
= value_type
*;
80 using reference
= value_type
&;
86 void findNextSetBit() {
87 if (Index
== EndIndexVal
)
89 while (++Index
< sizeof(Ty
) * 8) {
90 if (*ArchSet
& (1UL << Index
))
98 arch_iterator(Ty
*ArchSet
, ArchSetType Index
= 0)
99 : Index(Index
), ArchSet(ArchSet
) {
100 if (Index
!= EndIndexVal
&& !(*ArchSet
& (1UL << Index
)))
104 Architecture
operator*() const { return static_cast<Architecture
>(Index
); }
106 arch_iterator
&operator++() {
111 arch_iterator
operator++(int) {
117 bool operator==(const arch_iterator
&o
) const {
118 return std::tie(Index
, ArchSet
) == std::tie(o
.Index
, o
.ArchSet
);
121 bool operator!=(const arch_iterator
&o
) const { return !(*this == o
); }
124 ArchitectureSet
operator&(const ArchitectureSet
&o
) {
125 return {ArchSet
& o
.ArchSet
};
128 ArchitectureSet
operator|(const ArchitectureSet
&o
) {
129 return {ArchSet
| o
.ArchSet
};
132 ArchitectureSet
&operator|=(const ArchitectureSet
&o
) {
133 ArchSet
|= o
.ArchSet
;
137 ArchitectureSet
&operator|=(const Architecture
&Arch
) {
142 bool operator==(const ArchitectureSet
&o
) const {
143 return ArchSet
== o
.ArchSet
;
146 bool operator!=(const ArchitectureSet
&o
) const {
147 return ArchSet
!= o
.ArchSet
;
150 bool operator<(const ArchitectureSet
&o
) const { return ArchSet
< o
.ArchSet
; }
152 using iterator
= arch_iterator
<ArchSetType
>;
153 using const_iterator
= arch_iterator
<const ArchSetType
>;
155 iterator
begin() { return {&ArchSet
}; }
156 iterator
end() { return {&ArchSet
, EndIndexVal
}; }
158 const_iterator
begin() const { return {&ArchSet
}; }
159 const_iterator
end() const { return {&ArchSet
, EndIndexVal
}; }
161 operator std::string() const;
162 operator std::vector
<Architecture
>() const;
163 void print(raw_ostream
&OS
) const;
166 inline ArchitectureSet
operator|(const Architecture
&lhs
,
167 const Architecture
&rhs
) {
168 return ArchitectureSet(lhs
) | ArchitectureSet(rhs
);
171 raw_ostream
&operator<<(raw_ostream
&OS
, ArchitectureSet Set
);
173 } // end namespace MachO.
174 } // end namespace llvm.
176 #endif // LLVM_TEXTAPI_ARCHITECTURESET_H