blob: cc8f350bec193b472df77ad257376f10d0986306 [file] [log] [blame]
Googlerb56d7092021-11-26 12:38:29 +00001// 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 Gribenkoe4e77d02022-03-17 14:09:39 +00005#ifndef CRUBIT_LIFETIME_ANNOTATIONS_LIFETIME_H_
6#define CRUBIT_LIFETIME_ANNOTATIONS_LIFETIME_H_
Googlerb56d7092021-11-26 12:38:29 +00007
8#include <atomic>
9#include <cassert>
10#include <functional>
11#include <ostream>
12#include <string>
13#include <utility>
14
Lukasz Anforowiczcec7a8a2022-04-27 10:24:51 -070015#include "llvm/ADT/Hashing.h"
Googlerb56d7092021-11-26 12:38:29 +000016
Martin Brænne1a207c52022-04-19 00:05:38 -070017namespace clang {
18namespace tidy {
19namespace lifetimes {
Googlerb56d7092021-11-26 12:38:29 +000020
21// A lifetime variable or constant lifetime.
22class 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
Googler4e1bc132021-12-06 10:10:42 +000054 // Returns a unique numeric ID for the lifetime.
55 int Id() const { return id_; }
56
Googlerb56d7092021-11-26 12:38:29 +000057 // 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
84std::ostream& operator<<(std::ostream& os, Lifetime lifetime);
85
Martin Brænne1a207c52022-04-19 00:05:38 -070086} // namespace lifetimes
87} // namespace tidy
88} // namespace clang
Googlerb56d7092021-11-26 12:38:29 +000089
90namespace llvm {
91
92template <>
Martin Brænne1a207c52022-04-19 00:05:38 -070093struct DenseMapInfo<clang::tidy::lifetimes::Lifetime, void> {
94 static clang::tidy::lifetimes::Lifetime getEmptyKey() {
95 return clang::tidy::lifetimes::Lifetime::InvalidEmpty();
Googlerb56d7092021-11-26 12:38:29 +000096 }
97
Martin Brænne1a207c52022-04-19 00:05:38 -070098 static clang::tidy::lifetimes::Lifetime getTombstoneKey() {
99 return clang::tidy::lifetimes::Lifetime::InvalidTombstone();
Googlerb56d7092021-11-26 12:38:29 +0000100 }
101
Martin Brænne1a207c52022-04-19 00:05:38 -0700102 static unsigned getHashValue(clang::tidy::lifetimes::Lifetime lifetime) {
Googlerb56d7092021-11-26 12:38:29 +0000103 return llvm::hash_value(lifetime.id_);
104 }
105
Martin Brænne1a207c52022-04-19 00:05:38 -0700106 static bool isEqual(clang::tidy::lifetimes::Lifetime lhs,
107 clang::tidy::lifetimes::Lifetime rhs) {
Googlerb56d7092021-11-26 12:38:29 +0000108 return lhs.id_ == rhs.id_;
109 }
110};
111
112} // namespace llvm
113
114namespace std {
115
116template <>
Martin Brænne1a207c52022-04-19 00:05:38 -0700117struct less<clang::tidy::lifetimes::Lifetime> {
118 bool operator()(const clang::tidy::lifetimes::Lifetime& l1,
119 const clang::tidy::lifetimes::Lifetime& l2) const {
Googlerb56d7092021-11-26 12:38:29 +0000120 return l1.id_ < l2.id_;
121 }
122};
123
124} // namespace std
125
Dmitri Gribenkoe4e77d02022-03-17 14:09:39 +0000126#endif // CRUBIT_LIFETIME_ANNOTATIONS_LIFETIME_H_