19 #include "clang/AST/ASTContext.h"
20 #include "clang/AST/Decl.h"
21 #include "clang/AST/DeclCXX.h"
22 #include "clang/AST/DeclGroup.h"
23 #include "clang/AST/DeclObjC.h"
24 #include "clang/AST/Expr.h"
25 #include "clang/AST/Stmt.h"
26 #include "clang/Parse/Parser.h"
27 #include "clang/Sema/SemaDiagnostic.h"
28 #include "llvm/Support/Casting.h"
29 #include "llvm/Support/raw_ostream.h"
33 using namespace clang;
36 ASTResultSynthesizer::ASTResultSynthesizer(ASTConsumer *passthrough,
37 bool top_level,
Target &target)
38 : m_ast_context(nullptr), m_passthrough(passthrough),
39 m_passthrough_sema(nullptr), m_target(target), m_sema(nullptr),
40 m_top_level(top_level) {
59 if (NamedDecl *named_decl = dyn_cast<NamedDecl>(D)) {
61 if (named_decl->getIdentifier())
62 LLDB_LOGF(log,
"TransformTopLevelDecl(%s)",
63 named_decl->getIdentifier()->getNameStart());
64 else if (ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(D))
65 LLDB_LOGF(log,
"TransformTopLevelDecl(%s)",
66 method_decl->getSelector().getAsString().c_str());
68 LLDB_LOGF(log,
"TransformTopLevelDecl(<complex>)");
76 if (LinkageSpecDecl *linkage_spec_decl = dyn_cast<LinkageSpecDecl>(D)) {
77 RecordDecl::decl_iterator decl_iterator;
79 for (decl_iterator = linkage_spec_decl->decls_begin();
80 decl_iterator != linkage_spec_decl->decls_end(); ++decl_iterator) {
84 if (ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(D)) {
86 !method_decl->getSelector().getAsString().compare(
"$__lldb_expr:")) {
90 }
else if (FunctionDecl *function_decl = dyn_cast<FunctionDecl>(D)) {
93 !function_decl->getNameInfo().getAsString().compare(
"$__lldb_expr")) {
102 DeclGroupRef::iterator decl_iterator;
104 for (decl_iterator = D.begin(); decl_iterator != D.end(); ++decl_iterator) {
105 Decl *decl = *decl_iterator;
121 FunctionDecl *function_decl = FunDecl;
128 raw_string_ostream os(s);
130 function_decl->print(os);
134 LLDB_LOGF(log,
"Untransformed function AST:\n%s", s.c_str());
137 Stmt *function_body = function_decl->getBody();
138 CompoundStmt *compound_stmt = dyn_cast<CompoundStmt>(function_body);
144 raw_string_ostream os(s);
146 function_decl->print(os);
150 LLDB_LOGF(log,
"Transformed function AST:\n%s", s.c_str());
157 ObjCMethodDecl *MethodDecl) {
168 raw_string_ostream os(s);
170 MethodDecl->print(os);
174 LLDB_LOGF(log,
"Untransformed method AST:\n%s", s.c_str());
177 Stmt *method_body = MethodDecl->getBody();
182 CompoundStmt *compound_stmt = dyn_cast<CompoundStmt>(method_body);
188 raw_string_ostream os(s);
190 MethodDecl->print(os);
194 LLDB_LOGF(log,
"Transformed method AST:\n%s", s.c_str());
209 if (Body->body_empty())
212 Stmt **last_stmt_ptr = Body->body_end() - 1;
213 Stmt *last_stmt = *last_stmt_ptr;
215 while (isa<NullStmt>(last_stmt)) {
216 if (last_stmt_ptr != Body->body_begin()) {
218 last_stmt = *last_stmt_ptr;
224 Expr *last_expr = dyn_cast<Expr>(last_stmt);
234 ImplicitCastExpr *implicit_cast = dyn_cast<ImplicitCastExpr>(last_expr);
239 if (implicit_cast->getCastKind() != CK_LValueToRValue)
242 last_expr = implicit_cast->getSubExpr();
286 bool is_lvalue = last_expr->getValueKind() == VK_LValue &&
287 last_expr->getObjectKind() == OK_Ordinary;
289 QualType expr_qual_type = last_expr->getType();
290 const clang::Type *expr_type = expr_qual_type.getTypePtr();
295 if (expr_type->isVoidType())
301 LLDB_LOGF(log,
"Last statement is an %s with type: %s",
302 (is_lvalue ?
"lvalue" :
"rvalue"), s.c_str());
305 clang::VarDecl *result_decl =
nullptr;
308 IdentifierInfo *result_ptr_id;
310 if (expr_type->isFunctionType())
312 &Ctx.Idents.get(
"$__lldb_expr_result");
316 result_ptr_id = &Ctx.Idents.get(
"$__lldb_expr_result_ptr");
318 m_sema->RequireCompleteType(last_expr->getSourceRange().getBegin(),
320 clang::diag::err_incomplete_type);
322 QualType ptr_qual_type;
324 if (expr_qual_type->getAs<ObjCObjectType>() !=
nullptr)
325 ptr_qual_type = Ctx.getObjCObjectPointerType(expr_qual_type);
327 ptr_qual_type = Ctx.getPointerType(expr_qual_type);
330 VarDecl::Create(Ctx, DC, SourceLocation(), SourceLocation(),
331 result_ptr_id, ptr_qual_type,
nullptr, SC_Static);
336 ExprResult address_of_expr =
337 m_sema->CreateBuiltinUnaryOp(SourceLocation(), UO_AddrOf, last_expr);
338 if (address_of_expr.get())
339 m_sema->AddInitializerToDecl(result_decl, address_of_expr.get(),
true);
343 IdentifierInfo &result_id = Ctx.Idents.get(
"$__lldb_expr_result");
346 VarDecl::Create(Ctx, DC, SourceLocation(), SourceLocation(), &result_id,
347 expr_qual_type,
nullptr, SC_Static);
352 m_sema->AddInitializerToDecl(result_decl, last_expr,
true);
355 DC->addDecl(result_decl);
367 Sema::DeclGroupPtrTy result_decl_group_ptr;
369 result_decl_group_ptr =
m_sema->ConvertDeclToDeclGroup(result_decl);
375 StmtResult result_initialization_stmt_result(
m_sema->ActOnDeclStmt(
376 result_decl_group_ptr, SourceLocation(), SourceLocation()));
382 *last_stmt_ptr =
static_cast<Stmt *
>(result_initialization_stmt_result.get());
393 typedef DeclContext::specific_decl_iterator<TypeDecl> TypeDeclIterator;
395 for (TypeDeclIterator i = TypeDeclIterator(FunDeclCtx->decls_begin()),
396 e = TypeDeclIterator(FunDeclCtx->decls_end());
403 if (!D->getIdentifier())
406 StringRef name = D->getName();
408 if (name.size() == 0 || name[0] !=
'$')
423 if (!D->getIdentifier())
426 StringRef name = D->getName();
428 if (name.size() == 0)
446 auto *persistent_vars = llvm::cast<ClangPersistentVariables>(state);
451 for (clang::NamedDecl *decl :
m_decls) {
452 StringRef name = decl->getName();
455 Decl *D_scratch = persistent_vars->GetClangASTImporter()->DeportDecl(
463 llvm::raw_string_ostream ss(s);
467 LLDB_LOGF(log,
"Couldn't commit persistent decl: %s\n", s.c_str());
473 if (NamedDecl *NamedDecl_scratch = dyn_cast<NamedDecl>(D_scratch))
474 persistent_vars->RegisterPersistentDecl(name_cs, NamedDecl_scratch,