blob: 9e3b99a3c7e56897ae695d10826392f0daf4d42d [file] [log] [blame]
Luca Versari81222852022-08-05 06:36:10 -07001// 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
5#ifndef THIRD_PARTY_CRUBIT_LIFETIME_ANALYSIS_LIFETIME_CONSTRAINTS_H_
6#define THIRD_PARTY_CRUBIT_LIFETIME_ANALYSIS_LIFETIME_CONSTRAINTS_H_
7
Luca Versari91a56ff2022-08-22 01:58:33 -07008#include "lifetime_annotations/function_lifetimes.h"
Luca Versari81222852022-08-05 06:36:10 -07009#include "lifetime_annotations/lifetime.h"
10#include "clang/Analysis/FlowSensitive/DataflowLattice.h"
11#include "llvm/ADT/DenseSet.h"
12
13namespace clang {
14namespace tidy {
15namespace lifetimes {
16
17class LifetimeConstraints {
18 public:
19 // Creates empty constraints.
20 LifetimeConstraints() {}
21
Luca Versari7f2929c2023-01-16 10:15:54 -080022 // Returns the constraints on `callable` that allow `replacement_callable` to
23 // be used where `callable` is requested.
24 static LifetimeConstraints ForCallableSubstitution(
25 const FunctionLifetimes& callable,
26 const FunctionLifetimes& replacement_callable);
27
Luca Versariefeaf272023-01-16 10:19:28 -080028 // Returns the constraints on `callable` and `replacement_callable` that allow
29 // `replacement_callable` to be used where `callable` is requested.
30 static LifetimeConstraints ForCallableSubstitutionFull(
31 const FunctionLifetimes& callable,
32 const FunctionLifetimes& replacement_callable);
33
Luca Versari81222852022-08-05 06:36:10 -070034 // Imposes the constraint shorter <= longer.
35 void AddOutlivesConstraint(Lifetime shorter, Lifetime longer) {
36 outlives_constraints_.insert({shorter, longer});
37 }
38
Luca Versari7f2929c2023-01-16 10:15:54 -080039 // Returns all the lifetimes that this set of constraints implies must outlive
40 // the given lifetime l.
41 llvm::DenseSet<Lifetime> GetOutlivingLifetimes(Lifetime l) const;
42
Luca Versari81222852022-08-05 06:36:10 -070043 // Merges this set of constraints with the provided constraints, returning
44 // the effect of the operation.
45 clang::dataflow::LatticeJoinEffect join(const LifetimeConstraints& other);
46
Luca Versari91a56ff2022-08-22 01:58:33 -070047 // Applies this set of constraints to the given FunctionLifetimes.
48 llvm::Error ApplyToFunctionLifetimes(FunctionLifetimes& function_lifetimes);
49
50 bool operator==(const LifetimeConstraints& other) const {
51 return outlives_constraints_ == other.outlives_constraints_;
52 }
53
Luca Versarie06f94b2022-09-02 02:44:06 -070054 // Accessor for debug purposes.
55 const llvm::DenseSet<std::pair<Lifetime, Lifetime>>& AllConstraints() const {
56 return outlives_constraints_;
57 }
58
Luca Versari81222852022-08-05 06:36:10 -070059 private:
60 // Constraints of the form p.first <= p.second
61 llvm::DenseSet<std::pair<Lifetime, Lifetime>> outlives_constraints_;
62};
63
64} // namespace lifetimes
65} // namespace tidy
66} // namespace clang
67
68#endif // THIRD_PARTY_CRUBIT_LIFETIME_ANALYSIS_LIFETIME_CONSTRAINTS_H_