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