Luca Versari | 8122285 | 2022-08-05 06:36:10 -0700 | [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 | |
| 5 | #ifndef THIRD_PARTY_CRUBIT_LIFETIME_ANALYSIS_LIFETIME_CONSTRAINTS_H_ |
| 6 | #define THIRD_PARTY_CRUBIT_LIFETIME_ANALYSIS_LIFETIME_CONSTRAINTS_H_ |
| 7 | |
Luca Versari | 91a56ff | 2022-08-22 01:58:33 -0700 | [diff] [blame] | 8 | #include "lifetime_annotations/function_lifetimes.h" |
Luca Versari | 8122285 | 2022-08-05 06:36:10 -0700 | [diff] [blame] | 9 | #include "lifetime_annotations/lifetime.h" |
| 10 | #include "clang/Analysis/FlowSensitive/DataflowLattice.h" |
| 11 | #include "llvm/ADT/DenseSet.h" |
| 12 | |
| 13 | namespace clang { |
| 14 | namespace tidy { |
| 15 | namespace lifetimes { |
| 16 | |
| 17 | class LifetimeConstraints { |
| 18 | public: |
| 19 | // Creates empty constraints. |
| 20 | LifetimeConstraints() {} |
| 21 | |
Luca Versari | 7f2929c | 2023-01-16 10:15:54 -0800 | [diff] [blame] | 22 | // 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 Versari | efeaf27 | 2023-01-16 10:19:28 -0800 | [diff] [blame] | 28 | // 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 Versari | 8122285 | 2022-08-05 06:36:10 -0700 | [diff] [blame] | 34 | // Imposes the constraint shorter <= longer. |
| 35 | void AddOutlivesConstraint(Lifetime shorter, Lifetime longer) { |
| 36 | outlives_constraints_.insert({shorter, longer}); |
| 37 | } |
| 38 | |
Luca Versari | 7f2929c | 2023-01-16 10:15:54 -0800 | [diff] [blame] | 39 | // 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 Versari | 8122285 | 2022-08-05 06:36:10 -0700 | [diff] [blame] | 43 | // 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 Versari | 91a56ff | 2022-08-22 01:58:33 -0700 | [diff] [blame] | 47 | // 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 Versari | e06f94b | 2022-09-02 02:44:06 -0700 | [diff] [blame] | 54 | // Accessor for debug purposes. |
| 55 | const llvm::DenseSet<std::pair<Lifetime, Lifetime>>& AllConstraints() const { |
| 56 | return outlives_constraints_; |
| 57 | } |
| 58 | |
Luca Versari | 8122285 | 2022-08-05 06:36:10 -0700 | [diff] [blame] | 59 | 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_ |