clang 23.0.0git
Loans.h
Go to the documentation of this file.
1//===- Loans.h - Loan and Access Path Definitions --------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the Loan and AccessPath structures, which represent
10// borrows of storage locations, and the LoanManager, which manages the
11// creation and retrieval of loans during lifetime analysis.
12//
13//===----------------------------------------------------------------------===//
14#ifndef LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_LOANS_H
15#define LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_LOANS_H
16
17#include "clang/AST/Decl.h"
18#include "clang/AST/DeclCXX.h"
19#include "clang/AST/ExprCXX.h"
21#include "llvm/Support/raw_ostream.h"
22
24
26inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, LoanID ID) {
27 return OS << ID.Value;
28}
29
30/// Represents the storage location being borrowed, e.g., a specific stack
31/// variable or a field within it: var.field.*
32///
33/// An AccessPath consists of a root which is one of:
34/// - ValueDecl: a local variable or global
35/// - MaterializeTemporaryExpr: a temporary object
36/// - ParmVarDecl: a function parameter (placeholder)
37/// - CXXMethodDecl: the implicit 'this' object (placeholder)
38///
39/// Placeholder paths never expire within the function scope, as they represent
40/// storage from the caller's scope.
41///
42/// TODO: Model access paths of other types, e.g. field, array subscript, heap
43/// and globals.
45public:
52
53private:
54 Kind K;
55 const llvm::PointerUnion<const clang::ValueDecl *,
57 const ParmVarDecl *, const CXXMethodDecl *>
58 Root;
59
60public:
61 AccessPath(const clang::ValueDecl *D) : K(Kind::ValueDecl), Root(D) {}
64 static AccessPath Placeholder(const ParmVarDecl *PVD) {
66 }
69 }
70 AccessPath(const AccessPath &Other) : K(Other.K), Root(Other.Root) {}
71
72 Kind getKind() const { return K; }
73
75 return K == Kind::ValueDecl ? Root.dyn_cast<const clang::ValueDecl *>()
76 : nullptr;
77 }
80 ? Root.dyn_cast<const clang::MaterializeTemporaryExpr *>()
81 : nullptr;
82 }
84 return K == Kind::PlaceholderParam ? Root.dyn_cast<const ParmVarDecl *>()
85 : nullptr;
86 }
88 return K == Kind::PlaceholderThis ? Root.dyn_cast<const CXXMethodDecl *>()
89 : nullptr;
90 }
91
92 bool operator==(const AccessPath &RHS) const {
93 return K == RHS.K && Root == RHS.Root;
94 }
95 bool operator!=(const AccessPath &RHS) const { return !(*this == RHS); }
96 void dump(llvm::raw_ostream &OS) const;
97
98private:
99 AccessPath(Kind K, const ParmVarDecl *PVD) : K(K), Root(PVD) {}
100 AccessPath(Kind K, const CXXMethodDecl *MD) : K(K), Root(MD) {}
101};
102
103/// Represents lending a storage location.
104///
105/// A loan tracks the borrowing relationship created by operations like
106/// taking a pointer/reference (&x), creating a view (std::string_view sv = s),
107/// or receiving a parameter.
108///
109/// Examples:
110/// - `int* p = &x;` creates a loan to `x`
111/// - Parameter loans have no IssueExpr (created at function entry)
112class Loan {
113 const LoanID ID;
114 const AccessPath Path;
115 /// The expression that creates the loan, e.g., &x. Null for placeholder
116 /// loans.
117 const Expr *IssuingExpr;
118
119public:
120 Loan(LoanID ID, AccessPath Path, const Expr *IssuingExpr)
121 : ID(ID), Path(Path), IssuingExpr(IssuingExpr) {}
122 LoanID getID() const { return ID; }
123 const AccessPath &getAccessPath() const { return Path; }
124 const Expr *getIssuingExpr() const { return IssuingExpr; }
125 void dump(llvm::raw_ostream &OS) const;
126};
127
128/// Manages the creation, storage and retrieval of loans.
130public:
131 LoanManager() = default;
132
133 Loan *createLoan(AccessPath Path, const Expr *IssueExpr) {
134 void *Mem = LoanAllocator.Allocate<Loan>();
135 auto *NewLoan = new (Mem) Loan(getNextLoanID(), Path, IssueExpr);
136 AllLoans.push_back(NewLoan);
137 return NewLoan;
138 }
139
140 const Loan *getLoan(LoanID ID) const {
141 assert(ID.Value < AllLoans.size());
142 return AllLoans[ID.Value];
143 }
144
145 llvm::ArrayRef<const Loan *> getLoans() const { return AllLoans; }
146
147private:
148 LoanID getNextLoanID() { return NextLoanID++; }
149
150 LoanID NextLoanID{0};
151 /// TODO(opt): Profile and evaluate the usefullness of small buffer
152 /// optimisation.
154 llvm::BumpPtrAllocator LoanAllocator;
155};
156} // namespace clang::lifetimes::internal
157
158#endif // LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_LOANS_H
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the clang::Expr interface and subclasses for C++ expressions.
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2136
This represents one expression.
Definition Expr.h:112
Represents a prvalue temporary that is written into memory so that a reference can bind to it.
Definition ExprCXX.h:4921
Represents a parameter to a function.
Definition Decl.h:1805
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition Decl.h:712
Represents the storage location being borrowed, e.g., a specific stack variable or a field within it:...
Definition Loans.h:44
bool operator!=(const AccessPath &RHS) const
Definition Loans.h:95
AccessPath(const clang::ValueDecl *D)
Definition Loans.h:61
const clang::ValueDecl * getAsValueDecl() const
Definition Loans.h:74
bool operator==(const AccessPath &RHS) const
Definition Loans.h:92
AccessPath(const clang::MaterializeTemporaryExpr *MTE)
Definition Loans.h:62
const clang::MaterializeTemporaryExpr * getAsMaterializeTemporaryExpr() const
Definition Loans.h:78
void dump(llvm::raw_ostream &OS) const
Definition Loans.cpp:13
const CXXMethodDecl * getAsPlaceholderThis() const
Definition Loans.h:87
const ParmVarDecl * getAsPlaceholderParam() const
Definition Loans.h:83
AccessPath(const AccessPath &Other)
Definition Loans.h:70
static AccessPath Placeholder(const ParmVarDecl *PVD)
Definition Loans.h:64
static AccessPath Placeholder(const CXXMethodDecl *MD)
Definition Loans.h:67
llvm::ArrayRef< const Loan * > getLoans() const
Definition Loans.h:145
Loan * createLoan(AccessPath Path, const Expr *IssueExpr)
Definition Loans.h:133
const Loan * getLoan(LoanID ID) const
Definition Loans.h:140
Represents lending a storage location.
Definition Loans.h:112
Loan(LoanID ID, AccessPath Path, const Expr *IssuingExpr)
Definition Loans.h:120
const Expr * getIssuingExpr() const
Definition Loans.h:124
const AccessPath & getAccessPath() const
Definition Loans.h:123
void dump(llvm::raw_ostream &OS) const
Definition Loans.cpp:34
utils::ID< struct LoanTag > LoanID
Definition Loans.h:25
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, LoanID ID)
Definition Loans.h:26
@ Other
Other implicit parameter.
Definition Decl.h:1761
A generic, type-safe wrapper for an ID, distinguished by its Tag type.
Definition Utils.h:21