Googler | b56d709 | 2021-11-26 12:38:29 +0000 | [diff] [blame] | 1 | // Part of the Crubit project, under the Apache License v2.0 with LLVM |
| 2 | // Exceptions. See /LICENSE for license information. |
| 3 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 4 | |
Dmitri Gribenko | e4e77d0 | 2022-03-17 14:09:39 +0000 | [diff] [blame] | 5 | #ifndef CRUBIT_LIFETIME_ANNOTATIONS_LIFETIME_H_ |
| 6 | #define CRUBIT_LIFETIME_ANNOTATIONS_LIFETIME_H_ |
Googler | b56d709 | 2021-11-26 12:38:29 +0000 | [diff] [blame] | 7 | |
| 8 | #include <atomic> |
| 9 | #include <cassert> |
| 10 | #include <functional> |
| 11 | #include <ostream> |
| 12 | #include <string> |
| 13 | #include <utility> |
| 14 | |
Lukasz Anforowicz | cec7a8a | 2022-04-27 10:24:51 -0700 | [diff] [blame] | 15 | #include "llvm/ADT/Hashing.h" |
Googler | b56d709 | 2021-11-26 12:38:29 +0000 | [diff] [blame] | 16 | |
Martin Brænne | 1a207c5 | 2022-04-19 00:05:38 -0700 | [diff] [blame] | 17 | namespace clang { |
| 18 | namespace tidy { |
| 19 | namespace lifetimes { |
Googler | b56d709 | 2021-11-26 12:38:29 +0000 | [diff] [blame] | 20 | |
| 21 | // A lifetime variable or constant lifetime. |
| 22 | class Lifetime { |
| 23 | public: |
| 24 | // Creates an invalid lifetime. |
| 25 | // |
| 26 | // This is provided because containers need default constructors. It is not |
| 27 | // legal to perform any operations on an invalid lifetime except to copy or |
| 28 | // delete it. |
| 29 | // |
| 30 | // Use one of the static member functions below to create a valid lifetime. |
| 31 | Lifetime(); |
| 32 | |
| 33 | Lifetime(const Lifetime&) = default; |
| 34 | Lifetime& operator=(const Lifetime&) = default; |
| 35 | |
| 36 | // Creates a new lifetime variable. |
| 37 | static Lifetime CreateVariable(); |
| 38 | |
| 39 | // Returns the 'static lifetime constant. |
| 40 | static Lifetime Static(); |
| 41 | |
| 42 | // Creates a new local lifetime constant. |
| 43 | static Lifetime CreateLocal(); |
| 44 | |
| 45 | // Returns whether this lifetime is a lifetime variable. |
| 46 | bool IsVariable() const; |
| 47 | |
| 48 | // Returns whether this lifetime is a constant lifetime. |
| 49 | bool IsConstant() const; |
| 50 | |
| 51 | // Returns whether this lifetime is a local lifetime. |
| 52 | bool IsLocal() const; |
| 53 | |
Googler | 4e1bc13 | 2021-12-06 10:10:42 +0000 | [diff] [blame] | 54 | // Returns a unique numeric ID for the lifetime. |
| 55 | int Id() const { return id_; } |
| 56 | |
Googler | b56d709 | 2021-11-26 12:38:29 +0000 | [diff] [blame] | 57 | // Returns a textual representation of the lifetime for debug logging. |
| 58 | std::string DebugString() const; |
| 59 | |
| 60 | bool operator==(Lifetime other) const { |
| 61 | assert(IsValid()); |
| 62 | assert(other.IsValid()); |
| 63 | return id_ == other.id_; |
| 64 | } |
| 65 | |
| 66 | bool operator!=(Lifetime other) const { return !(*this == other); } |
| 67 | |
| 68 | private: |
| 69 | explicit Lifetime(int id); |
| 70 | |
| 71 | static Lifetime InvalidEmpty(); |
| 72 | static Lifetime InvalidTombstone(); |
| 73 | |
| 74 | bool IsValid() const; |
| 75 | |
| 76 | friend class llvm::DenseMapInfo<Lifetime, void>; |
| 77 | friend class std::less<Lifetime>; |
| 78 | |
| 79 | int id_; |
| 80 | static std::atomic<int> next_variable_id_; |
| 81 | static std::atomic<int> next_local_id_; |
| 82 | }; |
| 83 | |
| 84 | std::ostream& operator<<(std::ostream& os, Lifetime lifetime); |
| 85 | |
Martin Brænne | 1a207c5 | 2022-04-19 00:05:38 -0700 | [diff] [blame] | 86 | } // namespace lifetimes |
| 87 | } // namespace tidy |
| 88 | } // namespace clang |
Googler | b56d709 | 2021-11-26 12:38:29 +0000 | [diff] [blame] | 89 | |
| 90 | namespace llvm { |
| 91 | |
| 92 | template <> |
Martin Brænne | 1a207c5 | 2022-04-19 00:05:38 -0700 | [diff] [blame] | 93 | struct DenseMapInfo<clang::tidy::lifetimes::Lifetime, void> { |
| 94 | static clang::tidy::lifetimes::Lifetime getEmptyKey() { |
| 95 | return clang::tidy::lifetimes::Lifetime::InvalidEmpty(); |
Googler | b56d709 | 2021-11-26 12:38:29 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Martin Brænne | 1a207c5 | 2022-04-19 00:05:38 -0700 | [diff] [blame] | 98 | static clang::tidy::lifetimes::Lifetime getTombstoneKey() { |
| 99 | return clang::tidy::lifetimes::Lifetime::InvalidTombstone(); |
Googler | b56d709 | 2021-11-26 12:38:29 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Martin Brænne | 1a207c5 | 2022-04-19 00:05:38 -0700 | [diff] [blame] | 102 | static unsigned getHashValue(clang::tidy::lifetimes::Lifetime lifetime) { |
Googler | b56d709 | 2021-11-26 12:38:29 +0000 | [diff] [blame] | 103 | return llvm::hash_value(lifetime.id_); |
| 104 | } |
| 105 | |
Martin Brænne | 1a207c5 | 2022-04-19 00:05:38 -0700 | [diff] [blame] | 106 | static bool isEqual(clang::tidy::lifetimes::Lifetime lhs, |
| 107 | clang::tidy::lifetimes::Lifetime rhs) { |
Googler | b56d709 | 2021-11-26 12:38:29 +0000 | [diff] [blame] | 108 | return lhs.id_ == rhs.id_; |
| 109 | } |
| 110 | }; |
| 111 | |
| 112 | } // namespace llvm |
| 113 | |
| 114 | namespace std { |
| 115 | |
| 116 | template <> |
Martin Brænne | 1a207c5 | 2022-04-19 00:05:38 -0700 | [diff] [blame] | 117 | struct less<clang::tidy::lifetimes::Lifetime> { |
| 118 | bool operator()(const clang::tidy::lifetimes::Lifetime& l1, |
| 119 | const clang::tidy::lifetimes::Lifetime& l2) const { |
Googler | b56d709 | 2021-11-26 12:38:29 +0000 | [diff] [blame] | 120 | return l1.id_ < l2.id_; |
| 121 | } |
| 122 | }; |
| 123 | |
| 124 | } // namespace std |
| 125 | |
Dmitri Gribenko | e4e77d0 | 2022-03-17 14:09:39 +0000 | [diff] [blame] | 126 | #endif // CRUBIT_LIFETIME_ANNOTATIONS_LIFETIME_H_ |