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 *E = getAsNewAllocation())
25 OS << "NewAllocation at " << E;
26 else
27 llvm_unreachable("access path base invalid");
28 for (const auto &E : Elements)
29 E.dump(OS);
30}
31
32void Loan::dump(llvm::raw_ostream &OS) const {
33 OS << getID() << " (Path: ";
34 Path.dump(OS);
35 OS << ")";
36}
37
38const PlaceholderBase *
39LoanManager::getOrCreatePlaceholderBase(const ParmVarDecl *PVD) {
40 llvm::FoldingSetNodeID ID;
41 ID.AddPointer(Ptr: PVD);
42 void *InsertPos = nullptr;
43 if (PlaceholderBase *Existing =
44 PlaceholderBases.FindNodeOrInsertPos(ID, InsertPos))
45 return Existing;
46
47 void *Mem = LoanAllocator.Allocate<PlaceholderBase>();
48 PlaceholderBase *NewPB = new (Mem) PlaceholderBase(PVD);
49 PlaceholderBases.InsertNode(N: NewPB, InsertPos);
50 return NewPB;
51}
52
53const PlaceholderBase *
54LoanManager::getOrCreatePlaceholderBase(const CXXMethodDecl *MD) {
55 llvm::FoldingSetNodeID ID;
56 ID.AddPointer(Ptr: MD);
57 void *InsertPos = nullptr;
58 if (PlaceholderBase *Existing =
59 PlaceholderBases.FindNodeOrInsertPos(ID, InsertPos))
60 return Existing;
61
62 void *Mem = LoanAllocator.Allocate<PlaceholderBase>();
63 PlaceholderBase *NewPB = new (Mem) PlaceholderBase(MD);
64 PlaceholderBases.InsertNode(N: NewPB, InsertPos);
65 return NewPB;
66}
67} // namespace clang::lifetimes::internal
68