xref: /openbsd-src/gnu/llvm/lldb/source/Plugins/ExpressionParser/Clang/ASTResultSynthesizer.cpp (revision f6aab3d83b51b91c24247ad2c2573574de475a82)
1dda28197Spatrick //===-- ASTResultSynthesizer.cpp ------------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick 
9061da546Spatrick #include "ASTResultSynthesizer.h"
10061da546Spatrick 
11dda28197Spatrick #include "ClangASTImporter.h"
12061da546Spatrick #include "ClangPersistentVariables.h"
13061da546Spatrick 
14dda28197Spatrick #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
15061da546Spatrick #include "lldb/Target/Target.h"
16061da546Spatrick #include "lldb/Utility/LLDBAssert.h"
17*f6aab3d8Srobert #include "lldb/Utility/LLDBLog.h"
18061da546Spatrick #include "lldb/Utility/Log.h"
19061da546Spatrick #include "clang/AST/ASTContext.h"
20061da546Spatrick #include "clang/AST/Decl.h"
21061da546Spatrick #include "clang/AST/DeclCXX.h"
22061da546Spatrick #include "clang/AST/DeclGroup.h"
23061da546Spatrick #include "clang/AST/DeclObjC.h"
24061da546Spatrick #include "clang/AST/Expr.h"
25061da546Spatrick #include "clang/AST/Stmt.h"
26061da546Spatrick #include "clang/Parse/Parser.h"
27061da546Spatrick #include "clang/Sema/SemaDiagnostic.h"
28061da546Spatrick #include "llvm/Support/Casting.h"
29061da546Spatrick #include "llvm/Support/raw_ostream.h"
30be691f3bSpatrick #include <cstdlib>
31061da546Spatrick 
32061da546Spatrick using namespace llvm;
33061da546Spatrick using namespace clang;
34061da546Spatrick using namespace lldb_private;
35061da546Spatrick 
ASTResultSynthesizer(ASTConsumer * passthrough,bool top_level,Target & target)36061da546Spatrick ASTResultSynthesizer::ASTResultSynthesizer(ASTConsumer *passthrough,
37061da546Spatrick                                            bool top_level, Target &target)
38061da546Spatrick     : m_ast_context(nullptr), m_passthrough(passthrough),
39061da546Spatrick       m_passthrough_sema(nullptr), m_target(target), m_sema(nullptr),
40061da546Spatrick       m_top_level(top_level) {
41061da546Spatrick   if (!m_passthrough)
42061da546Spatrick     return;
43061da546Spatrick 
44061da546Spatrick   m_passthrough_sema = dyn_cast<SemaConsumer>(passthrough);
45061da546Spatrick }
46061da546Spatrick 
47be691f3bSpatrick ASTResultSynthesizer::~ASTResultSynthesizer() = default;
48061da546Spatrick 
Initialize(ASTContext & Context)49061da546Spatrick void ASTResultSynthesizer::Initialize(ASTContext &Context) {
50061da546Spatrick   m_ast_context = &Context;
51061da546Spatrick 
52061da546Spatrick   if (m_passthrough)
53061da546Spatrick     m_passthrough->Initialize(Context);
54061da546Spatrick }
55061da546Spatrick 
TransformTopLevelDecl(Decl * D)56061da546Spatrick void ASTResultSynthesizer::TransformTopLevelDecl(Decl *D) {
57*f6aab3d8Srobert   Log *log = GetLog(LLDBLog::Expressions);
58061da546Spatrick 
59061da546Spatrick   if (NamedDecl *named_decl = dyn_cast<NamedDecl>(D)) {
60061da546Spatrick     if (log && log->GetVerbose()) {
61061da546Spatrick       if (named_decl->getIdentifier())
62061da546Spatrick         LLDB_LOGF(log, "TransformTopLevelDecl(%s)",
63061da546Spatrick                   named_decl->getIdentifier()->getNameStart());
64061da546Spatrick       else if (ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(D))
65061da546Spatrick         LLDB_LOGF(log, "TransformTopLevelDecl(%s)",
66061da546Spatrick                   method_decl->getSelector().getAsString().c_str());
67061da546Spatrick       else
68061da546Spatrick         LLDB_LOGF(log, "TransformTopLevelDecl(<complex>)");
69061da546Spatrick     }
70061da546Spatrick 
71061da546Spatrick     if (m_top_level) {
72061da546Spatrick       RecordPersistentDecl(named_decl);
73061da546Spatrick     }
74061da546Spatrick   }
75061da546Spatrick 
76061da546Spatrick   if (LinkageSpecDecl *linkage_spec_decl = dyn_cast<LinkageSpecDecl>(D)) {
77061da546Spatrick     RecordDecl::decl_iterator decl_iterator;
78061da546Spatrick 
79061da546Spatrick     for (decl_iterator = linkage_spec_decl->decls_begin();
80061da546Spatrick          decl_iterator != linkage_spec_decl->decls_end(); ++decl_iterator) {
81061da546Spatrick       TransformTopLevelDecl(*decl_iterator);
82061da546Spatrick     }
83061da546Spatrick   } else if (!m_top_level) {
84061da546Spatrick     if (ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(D)) {
85061da546Spatrick       if (m_ast_context &&
86061da546Spatrick           !method_decl->getSelector().getAsString().compare("$__lldb_expr:")) {
87061da546Spatrick         RecordPersistentTypes(method_decl);
88061da546Spatrick         SynthesizeObjCMethodResult(method_decl);
89061da546Spatrick       }
90061da546Spatrick     } else if (FunctionDecl *function_decl = dyn_cast<FunctionDecl>(D)) {
91061da546Spatrick       // When completing user input the body of the function may be a nullptr.
92061da546Spatrick       if (m_ast_context && function_decl->hasBody() &&
93061da546Spatrick           !function_decl->getNameInfo().getAsString().compare("$__lldb_expr")) {
94061da546Spatrick         RecordPersistentTypes(function_decl);
95061da546Spatrick         SynthesizeFunctionResult(function_decl);
96061da546Spatrick       }
97061da546Spatrick     }
98061da546Spatrick   }
99061da546Spatrick }
100061da546Spatrick 
HandleTopLevelDecl(DeclGroupRef D)101061da546Spatrick bool ASTResultSynthesizer::HandleTopLevelDecl(DeclGroupRef D) {
102061da546Spatrick   DeclGroupRef::iterator decl_iterator;
103061da546Spatrick 
104061da546Spatrick   for (decl_iterator = D.begin(); decl_iterator != D.end(); ++decl_iterator) {
105061da546Spatrick     Decl *decl = *decl_iterator;
106061da546Spatrick 
107061da546Spatrick     TransformTopLevelDecl(decl);
108061da546Spatrick   }
109061da546Spatrick 
110061da546Spatrick   if (m_passthrough)
111061da546Spatrick     return m_passthrough->HandleTopLevelDecl(D);
112061da546Spatrick   return true;
113061da546Spatrick }
114061da546Spatrick 
SynthesizeFunctionResult(FunctionDecl * FunDecl)115061da546Spatrick bool ASTResultSynthesizer::SynthesizeFunctionResult(FunctionDecl *FunDecl) {
116*f6aab3d8Srobert   Log *log = GetLog(LLDBLog::Expressions);
117061da546Spatrick 
118061da546Spatrick   if (!m_sema)
119061da546Spatrick     return false;
120061da546Spatrick 
121061da546Spatrick   FunctionDecl *function_decl = FunDecl;
122061da546Spatrick 
123061da546Spatrick   if (!function_decl)
124061da546Spatrick     return false;
125061da546Spatrick 
126061da546Spatrick   if (log && log->GetVerbose()) {
127061da546Spatrick     std::string s;
128061da546Spatrick     raw_string_ostream os(s);
129061da546Spatrick 
130061da546Spatrick     function_decl->print(os);
131061da546Spatrick 
132061da546Spatrick     os.flush();
133061da546Spatrick 
134061da546Spatrick     LLDB_LOGF(log, "Untransformed function AST:\n%s", s.c_str());
135061da546Spatrick   }
136061da546Spatrick 
137061da546Spatrick   Stmt *function_body = function_decl->getBody();
138061da546Spatrick   CompoundStmt *compound_stmt = dyn_cast<CompoundStmt>(function_body);
139061da546Spatrick 
140061da546Spatrick   bool ret = SynthesizeBodyResult(compound_stmt, function_decl);
141061da546Spatrick 
142061da546Spatrick   if (log && log->GetVerbose()) {
143061da546Spatrick     std::string s;
144061da546Spatrick     raw_string_ostream os(s);
145061da546Spatrick 
146061da546Spatrick     function_decl->print(os);
147061da546Spatrick 
148061da546Spatrick     os.flush();
149061da546Spatrick 
150061da546Spatrick     LLDB_LOGF(log, "Transformed function AST:\n%s", s.c_str());
151061da546Spatrick   }
152061da546Spatrick 
153061da546Spatrick   return ret;
154061da546Spatrick }
155061da546Spatrick 
SynthesizeObjCMethodResult(ObjCMethodDecl * MethodDecl)156061da546Spatrick bool ASTResultSynthesizer::SynthesizeObjCMethodResult(
157061da546Spatrick     ObjCMethodDecl *MethodDecl) {
158*f6aab3d8Srobert   Log *log = GetLog(LLDBLog::Expressions);
159061da546Spatrick 
160061da546Spatrick   if (!m_sema)
161061da546Spatrick     return false;
162061da546Spatrick 
163061da546Spatrick   if (!MethodDecl)
164061da546Spatrick     return false;
165061da546Spatrick 
166061da546Spatrick   if (log && log->GetVerbose()) {
167061da546Spatrick     std::string s;
168061da546Spatrick     raw_string_ostream os(s);
169061da546Spatrick 
170061da546Spatrick     MethodDecl->print(os);
171061da546Spatrick 
172061da546Spatrick     os.flush();
173061da546Spatrick 
174061da546Spatrick     LLDB_LOGF(log, "Untransformed method AST:\n%s", s.c_str());
175061da546Spatrick   }
176061da546Spatrick 
177061da546Spatrick   Stmt *method_body = MethodDecl->getBody();
178061da546Spatrick 
179061da546Spatrick   if (!method_body)
180061da546Spatrick     return false;
181061da546Spatrick 
182061da546Spatrick   CompoundStmt *compound_stmt = dyn_cast<CompoundStmt>(method_body);
183061da546Spatrick 
184061da546Spatrick   bool ret = SynthesizeBodyResult(compound_stmt, MethodDecl);
185061da546Spatrick 
186061da546Spatrick   if (log && log->GetVerbose()) {
187061da546Spatrick     std::string s;
188061da546Spatrick     raw_string_ostream os(s);
189061da546Spatrick 
190061da546Spatrick     MethodDecl->print(os);
191061da546Spatrick 
192061da546Spatrick     os.flush();
193061da546Spatrick 
194061da546Spatrick     LLDB_LOGF(log, "Transformed method AST:\n%s", s.c_str());
195061da546Spatrick   }
196061da546Spatrick 
197061da546Spatrick   return ret;
198061da546Spatrick }
199061da546Spatrick 
200*f6aab3d8Srobert /// Returns true if LLDB can take the address of the given lvalue for the sake
201*f6aab3d8Srobert /// of capturing the expression result. Returns false if LLDB should instead
202*f6aab3d8Srobert /// store the expression result in a result variable.
CanTakeAddressOfLValue(const Expr * lvalue_expr)203*f6aab3d8Srobert static bool CanTakeAddressOfLValue(const Expr *lvalue_expr) {
204*f6aab3d8Srobert   assert(lvalue_expr->getValueKind() == VK_LValue &&
205*f6aab3d8Srobert          "lvalue_expr not a lvalue");
206*f6aab3d8Srobert 
207*f6aab3d8Srobert   QualType qt = lvalue_expr->getType();
208*f6aab3d8Srobert   // If the lvalue has const-qualified non-volatile integral or enum type, then
209*f6aab3d8Srobert   // the underlying value might come from a const static data member as
210*f6aab3d8Srobert   // described in C++11 [class.static.data]p3. If that's the case, then the
211*f6aab3d8Srobert   // value might not have an address if the user didn't also define the member
212*f6aab3d8Srobert   // in a namespace scope. Taking the address would cause that LLDB later fails
213*f6aab3d8Srobert   // to link the expression, so those lvalues should be stored in a result
214*f6aab3d8Srobert   // variable.
215*f6aab3d8Srobert   if (qt->isIntegralOrEnumerationType() && qt.isConstQualified() &&
216*f6aab3d8Srobert       !qt.isVolatileQualified())
217*f6aab3d8Srobert     return false;
218*f6aab3d8Srobert   return true;
219*f6aab3d8Srobert }
220*f6aab3d8Srobert 
SynthesizeBodyResult(CompoundStmt * Body,DeclContext * DC)221061da546Spatrick bool ASTResultSynthesizer::SynthesizeBodyResult(CompoundStmt *Body,
222061da546Spatrick                                                 DeclContext *DC) {
223*f6aab3d8Srobert   Log *log = GetLog(LLDBLog::Expressions);
224061da546Spatrick 
225061da546Spatrick   ASTContext &Ctx(*m_ast_context);
226061da546Spatrick 
227061da546Spatrick   if (!Body)
228061da546Spatrick     return false;
229061da546Spatrick 
230061da546Spatrick   if (Body->body_empty())
231061da546Spatrick     return false;
232061da546Spatrick 
233061da546Spatrick   Stmt **last_stmt_ptr = Body->body_end() - 1;
234061da546Spatrick   Stmt *last_stmt = *last_stmt_ptr;
235061da546Spatrick 
236*f6aab3d8Srobert   while (isa<NullStmt>(last_stmt)) {
237061da546Spatrick     if (last_stmt_ptr != Body->body_begin()) {
238061da546Spatrick       last_stmt_ptr--;
239061da546Spatrick       last_stmt = *last_stmt_ptr;
240061da546Spatrick     } else {
241061da546Spatrick       return false;
242061da546Spatrick     }
243061da546Spatrick   }
244061da546Spatrick 
245061da546Spatrick   Expr *last_expr = dyn_cast<Expr>(last_stmt);
246061da546Spatrick 
247061da546Spatrick   if (!last_expr)
248061da546Spatrick     // No auxiliary variable necessary; expression returns void
249061da546Spatrick     return true;
250061da546Spatrick 
251061da546Spatrick   // In C++11, last_expr can be a LValueToRvalue implicit cast.  Strip that off
252061da546Spatrick   // if that's the case.
253061da546Spatrick 
254061da546Spatrick   do {
255061da546Spatrick     ImplicitCastExpr *implicit_cast = dyn_cast<ImplicitCastExpr>(last_expr);
256061da546Spatrick 
257061da546Spatrick     if (!implicit_cast)
258061da546Spatrick       break;
259061da546Spatrick 
260061da546Spatrick     if (implicit_cast->getCastKind() != CK_LValueToRValue)
261061da546Spatrick       break;
262061da546Spatrick 
263061da546Spatrick     last_expr = implicit_cast->getSubExpr();
264061da546Spatrick   } while (false);
265061da546Spatrick 
266061da546Spatrick   // is_lvalue is used to record whether the expression returns an assignable
267061da546Spatrick   // Lvalue or an Rvalue.  This is relevant because they are handled
268061da546Spatrick   // differently.
269061da546Spatrick   //
270061da546Spatrick   // For Lvalues
271061da546Spatrick   //
272061da546Spatrick   //   - In AST result synthesis (here!) the expression E is transformed into an
273dda28197Spatrick   //     initialization T *$__lldb_expr_result_ptr = &E.
274061da546Spatrick   //
275061da546Spatrick   //   - In structure allocation, a pointer-sized slot is allocated in the
276dda28197Spatrick   //     struct that is to be passed into the expression.
277061da546Spatrick   //
278061da546Spatrick   //   - In IR transformations, reads and writes to $__lldb_expr_result_ptr are
279dda28197Spatrick   //     redirected at an entry in the struct ($__lldb_arg) passed into the
280dda28197Spatrick   //     expression. (Other persistent variables are treated similarly, having
281dda28197Spatrick   //     been materialized as references, but in those cases the value of the
282dda28197Spatrick   //     reference itself is never modified.)
283061da546Spatrick   //
284061da546Spatrick   //   - During materialization, $0 (the result persistent variable) is ignored.
285061da546Spatrick   //
286061da546Spatrick   //   - During dematerialization, $0 is marked up as a load address with value
287dda28197Spatrick   //     equal to the contents of the structure entry.
288061da546Spatrick   //
289*f6aab3d8Srobert   //   - Note: if we cannot take an address of the resulting Lvalue (e.g. it's
290*f6aab3d8Srobert   //     a static const member without an out-of-class definition), then we
291*f6aab3d8Srobert   //     follow the Rvalue route.
292*f6aab3d8Srobert   //
293061da546Spatrick   // For Rvalues
294061da546Spatrick   //
295061da546Spatrick   //   - In AST result synthesis the expression E is transformed into an
296dda28197Spatrick   //     initialization static T $__lldb_expr_result = E.
297061da546Spatrick   //
298061da546Spatrick   //   - In structure allocation, a pointer-sized slot is allocated in the
299dda28197Spatrick   //     struct that is to be passed into the expression.
300061da546Spatrick   //
301061da546Spatrick   //   - In IR transformations, an instruction is inserted at the beginning of
302dda28197Spatrick   //     the function to dereference the pointer resident in the slot. Reads and
303dda28197Spatrick   //     writes to $__lldb_expr_result are redirected at that dereferenced
304dda28197Spatrick   //     version. Guard variables for the static variable are excised.
305061da546Spatrick   //
306061da546Spatrick   //   - During materialization, $0 (the result persistent variable) is
307dda28197Spatrick   //     populated with the location of a newly-allocated area of memory.
308061da546Spatrick   //
309061da546Spatrick   //   - During dematerialization, $0 is ignored.
310061da546Spatrick 
311061da546Spatrick   bool is_lvalue = last_expr->getValueKind() == VK_LValue &&
312061da546Spatrick                    last_expr->getObjectKind() == OK_Ordinary;
313061da546Spatrick 
314061da546Spatrick   QualType expr_qual_type = last_expr->getType();
315061da546Spatrick   const clang::Type *expr_type = expr_qual_type.getTypePtr();
316061da546Spatrick 
317061da546Spatrick   if (!expr_type)
318061da546Spatrick     return false;
319061da546Spatrick 
320061da546Spatrick   if (expr_type->isVoidType())
321061da546Spatrick     return true;
322061da546Spatrick 
323061da546Spatrick   if (log) {
324061da546Spatrick     std::string s = expr_qual_type.getAsString();
325061da546Spatrick 
326061da546Spatrick     LLDB_LOGF(log, "Last statement is an %s with type: %s",
327061da546Spatrick               (is_lvalue ? "lvalue" : "rvalue"), s.c_str());
328061da546Spatrick   }
329061da546Spatrick 
330061da546Spatrick   clang::VarDecl *result_decl = nullptr;
331061da546Spatrick 
332*f6aab3d8Srobert   if (is_lvalue && CanTakeAddressOfLValue(last_expr)) {
333061da546Spatrick     IdentifierInfo *result_ptr_id;
334061da546Spatrick 
335061da546Spatrick     if (expr_type->isFunctionType())
336061da546Spatrick       result_ptr_id =
337061da546Spatrick           &Ctx.Idents.get("$__lldb_expr_result"); // functions actually should
338061da546Spatrick                                                   // be treated like function
339061da546Spatrick                                                   // pointers
340061da546Spatrick     else
341061da546Spatrick       result_ptr_id = &Ctx.Idents.get("$__lldb_expr_result_ptr");
342061da546Spatrick 
343dda28197Spatrick     m_sema->RequireCompleteType(last_expr->getSourceRange().getBegin(),
344dda28197Spatrick                                 expr_qual_type,
345061da546Spatrick                                 clang::diag::err_incomplete_type);
346061da546Spatrick 
347061da546Spatrick     QualType ptr_qual_type;
348061da546Spatrick 
349061da546Spatrick     if (expr_qual_type->getAs<ObjCObjectType>() != nullptr)
350061da546Spatrick       ptr_qual_type = Ctx.getObjCObjectPointerType(expr_qual_type);
351061da546Spatrick     else
352061da546Spatrick       ptr_qual_type = Ctx.getPointerType(expr_qual_type);
353061da546Spatrick 
354061da546Spatrick     result_decl =
355061da546Spatrick         VarDecl::Create(Ctx, DC, SourceLocation(), SourceLocation(),
356061da546Spatrick                         result_ptr_id, ptr_qual_type, nullptr, SC_Static);
357061da546Spatrick 
358061da546Spatrick     if (!result_decl)
359061da546Spatrick       return false;
360061da546Spatrick 
361061da546Spatrick     ExprResult address_of_expr =
362061da546Spatrick         m_sema->CreateBuiltinUnaryOp(SourceLocation(), UO_AddrOf, last_expr);
363061da546Spatrick     if (address_of_expr.get())
364061da546Spatrick       m_sema->AddInitializerToDecl(result_decl, address_of_expr.get(), true);
365061da546Spatrick     else
366061da546Spatrick       return false;
367061da546Spatrick   } else {
368061da546Spatrick     IdentifierInfo &result_id = Ctx.Idents.get("$__lldb_expr_result");
369061da546Spatrick 
370061da546Spatrick     result_decl =
371061da546Spatrick         VarDecl::Create(Ctx, DC, SourceLocation(), SourceLocation(), &result_id,
372061da546Spatrick                         expr_qual_type, nullptr, SC_Static);
373061da546Spatrick 
374061da546Spatrick     if (!result_decl)
375061da546Spatrick       return false;
376061da546Spatrick 
377061da546Spatrick     m_sema->AddInitializerToDecl(result_decl, last_expr, true);
378061da546Spatrick   }
379061da546Spatrick 
380061da546Spatrick   DC->addDecl(result_decl);
381061da546Spatrick 
382061da546Spatrick   ///////////////////////////////
383061da546Spatrick   // call AddInitializerToDecl
384061da546Spatrick   //
385061da546Spatrick 
386061da546Spatrick   // m_sema->AddInitializerToDecl(result_decl, last_expr);
387061da546Spatrick 
388061da546Spatrick   /////////////////////////////////
389061da546Spatrick   // call ConvertDeclToDeclGroup
390061da546Spatrick   //
391061da546Spatrick 
392061da546Spatrick   Sema::DeclGroupPtrTy result_decl_group_ptr;
393061da546Spatrick 
394061da546Spatrick   result_decl_group_ptr = m_sema->ConvertDeclToDeclGroup(result_decl);
395061da546Spatrick 
396061da546Spatrick   ////////////////////////
397061da546Spatrick   // call ActOnDeclStmt
398061da546Spatrick   //
399061da546Spatrick 
400061da546Spatrick   StmtResult result_initialization_stmt_result(m_sema->ActOnDeclStmt(
401061da546Spatrick       result_decl_group_ptr, SourceLocation(), SourceLocation()));
402061da546Spatrick 
403061da546Spatrick   ////////////////////////////////////////////////
404061da546Spatrick   // replace the old statement with the new one
405061da546Spatrick   //
406061da546Spatrick 
407061da546Spatrick   *last_stmt_ptr = static_cast<Stmt *>(result_initialization_stmt_result.get());
408061da546Spatrick 
409061da546Spatrick   return true;
410061da546Spatrick }
411061da546Spatrick 
HandleTranslationUnit(ASTContext & Ctx)412061da546Spatrick void ASTResultSynthesizer::HandleTranslationUnit(ASTContext &Ctx) {
413061da546Spatrick   if (m_passthrough)
414061da546Spatrick     m_passthrough->HandleTranslationUnit(Ctx);
415061da546Spatrick }
416061da546Spatrick 
RecordPersistentTypes(DeclContext * FunDeclCtx)417061da546Spatrick void ASTResultSynthesizer::RecordPersistentTypes(DeclContext *FunDeclCtx) {
418061da546Spatrick   typedef DeclContext::specific_decl_iterator<TypeDecl> TypeDeclIterator;
419061da546Spatrick 
420061da546Spatrick   for (TypeDeclIterator i = TypeDeclIterator(FunDeclCtx->decls_begin()),
421061da546Spatrick                         e = TypeDeclIterator(FunDeclCtx->decls_end());
422061da546Spatrick        i != e; ++i) {
423061da546Spatrick     MaybeRecordPersistentType(*i);
424061da546Spatrick   }
425061da546Spatrick }
426061da546Spatrick 
MaybeRecordPersistentType(TypeDecl * D)427061da546Spatrick void ASTResultSynthesizer::MaybeRecordPersistentType(TypeDecl *D) {
428061da546Spatrick   if (!D->getIdentifier())
429061da546Spatrick     return;
430061da546Spatrick 
431061da546Spatrick   StringRef name = D->getName();
432061da546Spatrick 
433061da546Spatrick   if (name.size() == 0 || name[0] != '$')
434061da546Spatrick     return;
435061da546Spatrick 
436*f6aab3d8Srobert   Log *log = GetLog(LLDBLog::Expressions);
437061da546Spatrick 
438061da546Spatrick   ConstString name_cs(name.str().c_str());
439061da546Spatrick 
440061da546Spatrick   LLDB_LOGF(log, "Recording persistent type %s\n", name_cs.GetCString());
441061da546Spatrick 
442061da546Spatrick   m_decls.push_back(D);
443061da546Spatrick }
444061da546Spatrick 
RecordPersistentDecl(NamedDecl * D)445061da546Spatrick void ASTResultSynthesizer::RecordPersistentDecl(NamedDecl *D) {
446061da546Spatrick   lldbassert(m_top_level);
447061da546Spatrick 
448061da546Spatrick   if (!D->getIdentifier())
449061da546Spatrick     return;
450061da546Spatrick 
451061da546Spatrick   StringRef name = D->getName();
452061da546Spatrick 
453061da546Spatrick   if (name.size() == 0)
454061da546Spatrick     return;
455061da546Spatrick 
456*f6aab3d8Srobert   Log *log = GetLog(LLDBLog::Expressions);
457061da546Spatrick 
458061da546Spatrick   ConstString name_cs(name.str().c_str());
459061da546Spatrick 
460061da546Spatrick   LLDB_LOGF(log, "Recording persistent decl %s\n", name_cs.GetCString());
461061da546Spatrick 
462061da546Spatrick   m_decls.push_back(D);
463061da546Spatrick }
464061da546Spatrick 
CommitPersistentDecls()465061da546Spatrick void ASTResultSynthesizer::CommitPersistentDecls() {
466061da546Spatrick   auto *state =
467061da546Spatrick       m_target.GetPersistentExpressionStateForLanguage(lldb::eLanguageTypeC);
468061da546Spatrick   if (!state)
469061da546Spatrick     return;
470061da546Spatrick 
471061da546Spatrick   auto *persistent_vars = llvm::cast<ClangPersistentVariables>(state);
472be691f3bSpatrick 
473*f6aab3d8Srobert   lldb::TypeSystemClangSP scratch_ts_sp = ScratchTypeSystemClang::GetForTarget(
474be691f3bSpatrick       m_target, m_ast_context->getLangOpts());
475061da546Spatrick 
476061da546Spatrick   for (clang::NamedDecl *decl : m_decls) {
477061da546Spatrick     StringRef name = decl->getName();
478061da546Spatrick     ConstString name_cs(name.str().c_str());
479061da546Spatrick 
480dda28197Spatrick     Decl *D_scratch = persistent_vars->GetClangASTImporter()->DeportDecl(
481*f6aab3d8Srobert         &scratch_ts_sp->getASTContext(), decl);
482061da546Spatrick 
483061da546Spatrick     if (!D_scratch) {
484*f6aab3d8Srobert       Log *log = GetLog(LLDBLog::Expressions);
485061da546Spatrick 
486061da546Spatrick       if (log) {
487061da546Spatrick         std::string s;
488061da546Spatrick         llvm::raw_string_ostream ss(s);
489061da546Spatrick         decl->dump(ss);
490061da546Spatrick         ss.flush();
491061da546Spatrick 
492061da546Spatrick         LLDB_LOGF(log, "Couldn't commit persistent  decl: %s\n", s.c_str());
493061da546Spatrick       }
494061da546Spatrick 
495061da546Spatrick       continue;
496061da546Spatrick     }
497061da546Spatrick 
498061da546Spatrick     if (NamedDecl *NamedDecl_scratch = dyn_cast<NamedDecl>(D_scratch))
499061da546Spatrick       persistent_vars->RegisterPersistentDecl(name_cs, NamedDecl_scratch,
500*f6aab3d8Srobert                                               scratch_ts_sp);
501061da546Spatrick   }
502061da546Spatrick }
503061da546Spatrick 
HandleTagDeclDefinition(TagDecl * D)504061da546Spatrick void ASTResultSynthesizer::HandleTagDeclDefinition(TagDecl *D) {
505061da546Spatrick   if (m_passthrough)
506061da546Spatrick     m_passthrough->HandleTagDeclDefinition(D);
507061da546Spatrick }
508061da546Spatrick 
CompleteTentativeDefinition(VarDecl * D)509061da546Spatrick void ASTResultSynthesizer::CompleteTentativeDefinition(VarDecl *D) {
510061da546Spatrick   if (m_passthrough)
511061da546Spatrick     m_passthrough->CompleteTentativeDefinition(D);
512061da546Spatrick }
513061da546Spatrick 
HandleVTable(CXXRecordDecl * RD)514061da546Spatrick void ASTResultSynthesizer::HandleVTable(CXXRecordDecl *RD) {
515061da546Spatrick   if (m_passthrough)
516061da546Spatrick     m_passthrough->HandleVTable(RD);
517061da546Spatrick }
518061da546Spatrick 
PrintStats()519061da546Spatrick void ASTResultSynthesizer::PrintStats() {
520061da546Spatrick   if (m_passthrough)
521061da546Spatrick     m_passthrough->PrintStats();
522061da546Spatrick }
523061da546Spatrick 
InitializeSema(Sema & S)524061da546Spatrick void ASTResultSynthesizer::InitializeSema(Sema &S) {
525061da546Spatrick   m_sema = &S;
526061da546Spatrick 
527061da546Spatrick   if (m_passthrough_sema)
528061da546Spatrick     m_passthrough_sema->InitializeSema(S);
529061da546Spatrick }
530061da546Spatrick 
ForgetSema()531061da546Spatrick void ASTResultSynthesizer::ForgetSema() {
532061da546Spatrick   m_sema = nullptr;
533061da546Spatrick 
534061da546Spatrick   if (m_passthrough_sema)
535061da546Spatrick     m_passthrough_sema->ForgetSema();
536061da546Spatrick }
537