1 //===- llvm/Support/FileSystem/UniqueID.h - UniqueID for files --*- 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 // This file is cut out of llvm/Support/FileSystem.h to allow UniqueID to be
10 // reused without bloating the includes.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_SUPPORT_FILESYSTEM_UNIQUEID_H
15 #define LLVM_SUPPORT_FILESYSTEM_UNIQUEID_H
16 
17 #include "llvm/ADT/DenseMapInfo.h"
18 #include "llvm/ADT/Hashing.h"
19 #include <cstdint>
20 #include <utility>
21 
22 namespace llvm {
23 namespace sys {
24 namespace fs {
25 
26 class UniqueID {
27   uint64_t Device;
28   uint64_t File;
29 
30 public:
31   UniqueID() = default;
32   UniqueID(uint64_t Device, uint64_t File) : Device(Device), File(File) {}
33 
34   bool operator==(const UniqueID &Other) const {
35     return Device == Other.Device && File == Other.File;
36   }
37   bool operator!=(const UniqueID &Other) const { return !(*this == Other); }
38   bool operator<(const UniqueID &Other) const {
39     /// Don't use std::tie since it bloats the compile time of this header.
40     if (Device < Other.Device)
41       return true;
42     if (Other.Device < Device)
43       return false;
44     return File < Other.File;
45   }
46 
47   uint64_t getDevice() const { return Device; }
48   uint64_t getFile() const { return File; }
49 };
50 
51 } // end namespace fs
52 } // end namespace sys
53 
54 // Support UniqueIDs as DenseMap keys.
55 template <> struct DenseMapInfo<llvm::sys::fs::UniqueID> {
56   static inline llvm::sys::fs::UniqueID getEmptyKey() {
57     auto EmptyKey = DenseMapInfo<std::pair<uint64_t, uint64_t>>::getEmptyKey();
58     return {EmptyKey.first, EmptyKey.second};
59   }
60 
61   static inline llvm::sys::fs::UniqueID getTombstoneKey() {
62     auto TombstoneKey =
63         DenseMapInfo<std::pair<uint64_t, uint64_t>>::getTombstoneKey();
64     return {TombstoneKey.first, TombstoneKey.second};
65   }
66 
67   static hash_code getHashValue(const llvm::sys::fs::UniqueID &Tag) {
68     return hash_value(std::make_pair(Tag.getDevice(), Tag.getFile()));
69   }
70 
71   static bool isEqual(const llvm::sys::fs::UniqueID &LHS,
72                       const llvm::sys::fs::UniqueID &RHS) {
73     return LHS == RHS;
74   }
75 };
76 
77 } // end namespace llvm
78 
79 #endif // LLVM_SUPPORT_FILESYSTEM_UNIQUEID_H
80