blob: 55f2623e54a35f920c05b3ccad3d90d1cb3ecd95 [file] [log] [blame]
// Part of the Crubit project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef CRUBIT_LIFETIME_ANALYSIS_LIFETIME_CONSTRAINTS_H_
#define CRUBIT_LIFETIME_ANALYSIS_LIFETIME_CONSTRAINTS_H_
#include <utility>
#include "lifetime_annotations/function_lifetimes.h"
#include "lifetime_annotations/lifetime.h"
#include "clang/Analysis/FlowSensitive/DataflowLattice.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/Support/Error.h"
namespace clang {
namespace tidy {
namespace lifetimes {
class LifetimeConstraints {
public:
// Creates empty constraints.
LifetimeConstraints() = default;
// Returns the constraints on `callable` that allow `replacement_callable` to
// be used where `callable` is requested.
static LifetimeConstraints ForCallableSubstitution(
const FunctionLifetimes& callable,
const FunctionLifetimes& replacement_callable);
// Returns the constraints on `callable` and `replacement_callable` that allow
// `replacement_callable` to be used where `callable` is requested.
static LifetimeConstraints ForCallableSubstitutionFull(
const FunctionLifetimes& callable,
const FunctionLifetimes& replacement_callable);
// Imposes the constraint shorter <= longer.
void AddOutlivesConstraint(Lifetime shorter, Lifetime longer) {
outlives_constraints_.insert({shorter, longer});
}
// Returns all the lifetimes that this set of constraints implies must outlive
// the given lifetime l.
llvm::DenseSet<Lifetime> GetOutlivingLifetimes(Lifetime l) const;
// Merges this set of constraints with the provided constraints, returning
// the effect of the operation.
clang::dataflow::LatticeJoinEffect join(const LifetimeConstraints& other);
// Applies this set of constraints to the given FunctionLifetimes.
llvm::Error ApplyToFunctionLifetimes(FunctionLifetimes& function_lifetimes);
bool operator==(const LifetimeConstraints& other) const {
return outlives_constraints_ == other.outlives_constraints_;
}
// Accessor for debug purposes.
const llvm::DenseSet<std::pair<Lifetime, Lifetime>>& AllConstraints() const {
return outlives_constraints_;
}
private:
// Constraints of the form p.first <= p.second
llvm::DenseSet<std::pair<Lifetime, Lifetime>> outlives_constraints_;
};
} // namespace lifetimes
} // namespace tidy
} // namespace clang
#endif // CRUBIT_LIFETIME_ANALYSIS_LIFETIME_CONSTRAINTS_H_