1 //===- llvm/TextAPI/MachO/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_MACHO_ARCHITECTURE_SET_H
14 #define LLVM_TEXTAPI_MACHO_ARCHITECTURE_SET_H
16 #include "llvm/Support/raw_ostream.h"
17 #include "llvm/TextAPI/MachO/Architecture.h"
26 class ArchitectureSet
{
28 using ArchSetType
= uint32_t;
30 const static ArchSetType EndIndexVal
=
31 std::numeric_limits
<ArchSetType
>::max();
32 ArchSetType ArchSet
{0};
35 constexpr ArchitectureSet() = default;
36 constexpr ArchitectureSet(ArchSetType Raw
) : ArchSet(Raw
) {}
37 ArchitectureSet(Architecture Arch
) : ArchitectureSet() { set(Arch
); }
38 ArchitectureSet(const std::vector
<Architecture
> &Archs
);
40 void set(Architecture Arch
) {
41 if (Arch
== AK_unknown
)
43 ArchSet
|= 1U << static_cast<int>(Arch
);
46 void clear(Architecture Arch
) { ArchSet
&= ~(1U << static_cast<int>(Arch
)); }
48 bool has(Architecture Arch
) const {
49 return ArchSet
& (1U << static_cast<int>(Arch
));
52 bool contains(ArchitectureSet Archs
) const {
53 return (ArchSet
& Archs
.ArchSet
) == Archs
.ArchSet
;
58 bool empty() const { return ArchSet
== 0; }
60 ArchSetType
rawValue() const { return ArchSet
; }
63 return has(AK_i386
) || has(AK_x86_64
) || has(AK_x86_64h
);
66 template <typename Ty
>
68 : public std::iterator
<std::forward_iterator_tag
, Architecture
, size_t> {
73 void findNextSetBit() {
74 if (Index
== EndIndexVal
)
76 while (++Index
< sizeof(Ty
) * 8) {
77 if (*ArchSet
& (1UL << Index
))
85 arch_iterator(Ty
*ArchSet
, ArchSetType Index
= 0)
86 : Index(Index
), ArchSet(ArchSet
) {
87 if (Index
!= EndIndexVal
&& !(*ArchSet
& (1UL << Index
)))
91 Architecture
operator*() const { return static_cast<Architecture
>(Index
); }
93 arch_iterator
&operator++() {
98 arch_iterator
operator++(int) {
104 bool operator==(const arch_iterator
&o
) const {
105 return std::tie(Index
, ArchSet
) == std::tie(o
.Index
, o
.ArchSet
);
108 bool operator!=(const arch_iterator
&o
) const { return !(*this == o
); }
111 ArchitectureSet
operator&(const ArchitectureSet
&o
) {
112 return {ArchSet
& o
.ArchSet
};
115 ArchitectureSet
operator|(const ArchitectureSet
&o
) {
116 return {ArchSet
| o
.ArchSet
};
119 ArchitectureSet
&operator|=(const ArchitectureSet
&o
) {
120 ArchSet
|= o
.ArchSet
;
124 ArchitectureSet
&operator|=(const Architecture
&Arch
) {
129 bool operator==(const ArchitectureSet
&o
) const {
130 return ArchSet
== o
.ArchSet
;
133 bool operator!=(const ArchitectureSet
&o
) const {
134 return ArchSet
!= o
.ArchSet
;
137 bool operator<(const ArchitectureSet
&o
) const { return ArchSet
< o
.ArchSet
; }
139 using iterator
= arch_iterator
<ArchSetType
>;
140 using const_iterator
= arch_iterator
<const ArchSetType
>;
142 iterator
begin() { return {&ArchSet
}; }
143 iterator
end() { return {&ArchSet
, EndIndexVal
}; }
145 const_iterator
begin() const { return {&ArchSet
}; }
146 const_iterator
end() const { return {&ArchSet
, EndIndexVal
}; }
148 operator std::string() const;
149 operator std::vector
<Architecture
>() const;
150 void print(raw_ostream
&OS
) const;
153 inline ArchitectureSet
operator|(const Architecture
&lhs
,
154 const Architecture
&rhs
) {
155 return ArchitectureSet(lhs
) | ArchitectureSet(rhs
);
158 raw_ostream
&operator<<(raw_ostream
&OS
, ArchitectureSet Set
);
160 } // end namespace MachO.
161 } // end namespace llvm.
163 #endif // LLVM_TEXTAPI_MACHO_ARCHITECTURE_SET_H