xref: /freebsd/contrib/llvm-project/llvm/include/llvm/TextAPI/PackedVersion.h (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
1 //===- llvm/TextAPI/PackedVersion.h - PackedVersion -------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Defines the Mach-O packed version format.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_TEXTAPI_PACKEDVERSION_H
14 #define LLVM_TEXTAPI_PACKEDVERSION_H
15 
16 #include "llvm/Support/VersionTuple.h"
17 #include <cstdint>
18 #include <string>
19 #include <utility>
20 
21 namespace llvm {
22 class raw_ostream;
23 class StringRef;
24 
25 namespace MachO {
26 
27 class PackedVersion {
28   uint32_t Version{0};
29 
30 public:
31   constexpr PackedVersion() = default;
32   constexpr PackedVersion(uint32_t RawVersion) : Version(RawVersion) {}
33   PackedVersion(unsigned Major, unsigned Minor, unsigned Subminor)
34       : Version((Major << 16) | ((Minor & 0xff) << 8) | (Subminor & 0xff)) {}
35 
36   PackedVersion(VersionTuple VT) {
37     unsigned Minor = 0, Subminor = 0;
38     if (auto VTMinor = VT.getMinor())
39       Minor = *VTMinor;
40     if (auto VTSub = VT.getSubminor())
41       Subminor = *VTSub;
42     *this = PackedVersion(VT.getMajor(), Minor, Subminor);
43   }
44 
45   bool empty() const { return Version == 0; }
46 
47   /// Retrieve the major version number.
48   unsigned getMajor() const { return Version >> 16; }
49 
50   /// Retrieve the minor version number, if provided.
51   unsigned getMinor() const { return (Version >> 8) & 0xff; }
52 
53   /// Retrieve the subminor version number, if provided.
54   unsigned getSubminor() const { return Version & 0xff; }
55 
56   bool parse32(StringRef Str);
57   std::pair<bool, bool> parse64(StringRef Str);
58 
59   bool operator<(const PackedVersion &O) const { return Version < O.Version; }
60 
61   bool operator==(const PackedVersion &O) const { return Version == O.Version; }
62 
63   bool operator!=(const PackedVersion &O) const { return Version != O.Version; }
64 
65   uint32_t rawValue() const { return Version; }
66 
67   operator std::string() const;
68 
69   void print(raw_ostream &OS) const;
70 };
71 
72 inline raw_ostream &operator<<(raw_ostream &OS, const PackedVersion &Version) {
73   Version.print(OS);
74   return OS;
75 }
76 
77 } // end namespace MachO.
78 } // end namespace llvm.
79 
80 #endif // LLVM_TEXTAPI_PACKEDVERSION_H
81