1//===- Loans.cpp - Loan Implementation --------------------------*- 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#include "clang/Analysis/Analyses/LifetimeSafety/Loans.h"
10
11namespace clang::lifetimes::internal {
12
13void AccessPath::dump(llvm::raw_ostream &OS) const {
14 if (const clang::ValueDecl *VD = getAsValueDecl())
15 OS << VD->getNameAsString();
16 else if (const clang::MaterializeTemporaryExpr *MTE =
17 getAsMaterializeTemporaryExpr())
18 OS << "MaterializeTemporaryExpr at " << MTE;
19 else if (const PlaceholderBase *PB = getAsPlaceholderBase()) {
20 if (const auto *PVD = PB->getParmVarDecl())
21 OS << "$" << PVD->getNameAsString();
22 else if (PB->getImplicitThisParent())
23 OS << "$this";
24 else if (const auto *FD = PB->getFieldDecl())
25 OS << "$this." << FD->getNameAsString();
26 } else if (const auto *E = getAsAllocation()) {
27 if (isa<CXXNewExpr>(Val: E))
28 OS << "NewAllocation at " << E;
29 else if (isa<CallExpr>(Val: E))
30 OS << "HeapAllocation at " << E;
31 else
32 llvm_unreachable("unexpected allocation expression");
33 } else
34 llvm_unreachable("access path base invalid");
35 for (const auto &E : Elements)
36 E.dump(OS);
37}
38
39void Loan::dump(llvm::raw_ostream &OS) const {
40 OS << getID() << " (Path: ";
41 Path.dump(OS);
42 OS << ")";
43}
44
45const PlaceholderBase *
46LoanManager::getOrCreatePlaceholderBase(const ParmVarDecl *PVD) {
47 llvm::FoldingSetNodeID ID;
48 PlaceholderBase::Profile(ID, ParamMethodOrField: PVD);
49 llvm::FoldingSetInsertToken InsertToken;
50 if (PlaceholderBase *Existing = PlaceholderBases.lookup(ID, Token&: InsertToken))
51 return Existing;
52
53 void *Mem = LoanAllocator.Allocate<PlaceholderBase>();
54 PlaceholderBase *NewPB = new (Mem) PlaceholderBase(PVD);
55 PlaceholderBases.insert(N: NewPB, Token: InsertToken);
56 return NewPB;
57}
58
59const PlaceholderBase *
60LoanManager::getOrCreatePlaceholderBase(const CXXMethodDecl *MD) {
61 llvm::FoldingSetNodeID ID;
62 PlaceholderBase::Profile(ID, ParamMethodOrField: MD);
63 llvm::FoldingSetInsertToken InsertToken;
64 if (PlaceholderBase *Existing = PlaceholderBases.lookup(ID, Token&: InsertToken))
65 return Existing;
66
67 void *Mem = LoanAllocator.Allocate<PlaceholderBase>();
68 PlaceholderBase *NewPB = new (Mem) PlaceholderBase(MD);
69 PlaceholderBases.insert(N: NewPB, Token: InsertToken);
70 return NewPB;
71}
72
73const PlaceholderBase *
74LoanManager::getOrCreatePlaceholderBase(const FieldDecl *FD) {
75 llvm::FoldingSetNodeID ID;
76 PlaceholderBase::Profile(ID, ParamMethodOrField: FD);
77 llvm::FoldingSetInsertToken InsertToken;
78 if (PlaceholderBase *Existing = PlaceholderBases.lookup(ID, Token&: InsertToken))
79 return Existing;
80
81 void *Mem = LoanAllocator.Allocate<PlaceholderBase>();
82 PlaceholderBase *NewPB = new (Mem) PlaceholderBase(FD);
83 PlaceholderBases.insert(N: NewPB, Token: InsertToken);
84 return NewPB;
85}
86} // namespace clang::lifetimes::internal
87