ASTResultSynthesizer.cpp revision 314564
1292932Sdim//===-- ASTResultSynthesizer.cpp --------------------------------*- C++ -*-===// 2292932Sdim// 3292932Sdim// The LLVM Compiler Infrastructure 4292932Sdim// 5292932Sdim// This file is distributed under the University of Illinois Open Source 6292932Sdim// License. See LICENSE.TXT for details. 7292932Sdim// 8292932Sdim//===----------------------------------------------------------------------===// 9292932Sdim 10292932Sdim#include "ASTResultSynthesizer.h" 11292932Sdim 12292932Sdim#include "ClangPersistentVariables.h" 13292932Sdim 14309124Sdim#include "lldb/Core/Log.h" 15309124Sdim#include "lldb/Symbol/ClangASTContext.h" 16309124Sdim#include "lldb/Symbol/ClangASTImporter.h" 17309124Sdim#include "lldb/Target/Target.h" 18309124Sdim#include "lldb/Utility/LLDBAssert.h" 19292932Sdim#include "stdlib.h" 20292932Sdim#include "clang/AST/ASTContext.h" 21292932Sdim#include "clang/AST/Decl.h" 22292932Sdim#include "clang/AST/DeclCXX.h" 23292932Sdim#include "clang/AST/DeclGroup.h" 24292932Sdim#include "clang/AST/DeclObjC.h" 25292932Sdim#include "clang/AST/Expr.h" 26292932Sdim#include "clang/AST/Stmt.h" 27292932Sdim#include "clang/Parse/Parser.h" 28292932Sdim#include "clang/Sema/SemaDiagnostic.h" 29292932Sdim#include "llvm/Support/Casting.h" 30292932Sdim#include "llvm/Support/raw_ostream.h" 31292932Sdim 32292932Sdimusing namespace llvm; 33292932Sdimusing namespace clang; 34292932Sdimusing namespace lldb_private; 35292932Sdim 36314564SdimASTResultSynthesizer::ASTResultSynthesizer(ASTConsumer *passthrough, 37314564Sdim bool top_level, Target &target) 38314564Sdim : m_ast_context(NULL), m_passthrough(passthrough), m_passthrough_sema(NULL), 39314564Sdim m_target(target), m_sema(NULL), m_top_level(top_level) { 40314564Sdim if (!m_passthrough) 41314564Sdim return; 42292932Sdim 43314564Sdim m_passthrough_sema = dyn_cast<SemaConsumer>(passthrough); 44292932Sdim} 45292932Sdim 46314564SdimASTResultSynthesizer::~ASTResultSynthesizer() {} 47292932Sdim 48314564Sdimvoid ASTResultSynthesizer::Initialize(ASTContext &Context) { 49314564Sdim m_ast_context = &Context; 50292932Sdim 51314564Sdim if (m_passthrough) 52314564Sdim m_passthrough->Initialize(Context); 53292932Sdim} 54292932Sdim 55314564Sdimvoid ASTResultSynthesizer::TransformTopLevelDecl(Decl *D) { 56314564Sdim Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 57292932Sdim 58314564Sdim if (NamedDecl *named_decl = dyn_cast<NamedDecl>(D)) { 59314564Sdim if (log && log->GetVerbose()) { 60314564Sdim if (named_decl->getIdentifier()) 61314564Sdim log->Printf("TransformTopLevelDecl(%s)", 62314564Sdim named_decl->getIdentifier()->getNameStart()); 63314564Sdim else if (ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(D)) 64314564Sdim log->Printf("TransformTopLevelDecl(%s)", 65314564Sdim method_decl->getSelector().getAsString().c_str()); 66314564Sdim else 67314564Sdim log->Printf("TransformTopLevelDecl(<complex>)"); 68314564Sdim } 69292932Sdim 70314564Sdim if (m_top_level) { 71314564Sdim RecordPersistentDecl(named_decl); 72292932Sdim } 73314564Sdim } 74292932Sdim 75314564Sdim if (LinkageSpecDecl *linkage_spec_decl = dyn_cast<LinkageSpecDecl>(D)) { 76314564Sdim RecordDecl::decl_iterator decl_iterator; 77292932Sdim 78314564Sdim for (decl_iterator = linkage_spec_decl->decls_begin(); 79314564Sdim decl_iterator != linkage_spec_decl->decls_end(); ++decl_iterator) { 80314564Sdim TransformTopLevelDecl(*decl_iterator); 81292932Sdim } 82314564Sdim } else if (!m_top_level) { 83314564Sdim if (ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(D)) { 84314564Sdim if (m_ast_context && 85314564Sdim !method_decl->getSelector().getAsString().compare("$__lldb_expr:")) { 86314564Sdim RecordPersistentTypes(method_decl); 87314564Sdim SynthesizeObjCMethodResult(method_decl); 88314564Sdim } 89314564Sdim } else if (FunctionDecl *function_decl = dyn_cast<FunctionDecl>(D)) { 90314564Sdim if (m_ast_context && 91314564Sdim !function_decl->getNameInfo().getAsString().compare("$__lldb_expr")) { 92314564Sdim RecordPersistentTypes(function_decl); 93314564Sdim SynthesizeFunctionResult(function_decl); 94314564Sdim } 95292932Sdim } 96314564Sdim } 97292932Sdim} 98292932Sdim 99314564Sdimbool ASTResultSynthesizer::HandleTopLevelDecl(DeclGroupRef D) { 100314564Sdim DeclGroupRef::iterator decl_iterator; 101292932Sdim 102314564Sdim for (decl_iterator = D.begin(); decl_iterator != D.end(); ++decl_iterator) { 103314564Sdim Decl *decl = *decl_iterator; 104292932Sdim 105314564Sdim TransformTopLevelDecl(decl); 106314564Sdim } 107292932Sdim 108314564Sdim if (m_passthrough) 109314564Sdim return m_passthrough->HandleTopLevelDecl(D); 110314564Sdim return true; 111292932Sdim} 112292932Sdim 113314564Sdimbool ASTResultSynthesizer::SynthesizeFunctionResult(FunctionDecl *FunDecl) { 114314564Sdim Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 115292932Sdim 116314564Sdim if (!m_sema) 117314564Sdim return false; 118292932Sdim 119314564Sdim FunctionDecl *function_decl = FunDecl; 120292932Sdim 121314564Sdim if (!function_decl) 122314564Sdim return false; 123292932Sdim 124314564Sdim if (log && log->GetVerbose()) { 125314564Sdim std::string s; 126314564Sdim raw_string_ostream os(s); 127292932Sdim 128314564Sdim function_decl->print(os); 129292932Sdim 130314564Sdim os.flush(); 131292932Sdim 132314564Sdim log->Printf("Untransformed function AST:\n%s", s.c_str()); 133314564Sdim } 134292932Sdim 135314564Sdim Stmt *function_body = function_decl->getBody(); 136314564Sdim CompoundStmt *compound_stmt = dyn_cast<CompoundStmt>(function_body); 137292932Sdim 138314564Sdim bool ret = SynthesizeBodyResult(compound_stmt, function_decl); 139292932Sdim 140314564Sdim if (log && log->GetVerbose()) { 141314564Sdim std::string s; 142314564Sdim raw_string_ostream os(s); 143292932Sdim 144314564Sdim function_decl->print(os); 145292932Sdim 146314564Sdim os.flush(); 147292932Sdim 148314564Sdim log->Printf("Transformed function AST:\n%s", s.c_str()); 149314564Sdim } 150292932Sdim 151314564Sdim return ret; 152292932Sdim} 153292932Sdim 154314564Sdimbool ASTResultSynthesizer::SynthesizeObjCMethodResult( 155314564Sdim ObjCMethodDecl *MethodDecl) { 156314564Sdim Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 157292932Sdim 158314564Sdim if (!m_sema) 159314564Sdim return false; 160292932Sdim 161314564Sdim if (!MethodDecl) 162314564Sdim return false; 163292932Sdim 164314564Sdim if (log && log->GetVerbose()) { 165314564Sdim std::string s; 166314564Sdim raw_string_ostream os(s); 167292932Sdim 168314564Sdim MethodDecl->print(os); 169292932Sdim 170314564Sdim os.flush(); 171292932Sdim 172314564Sdim log->Printf("Untransformed method AST:\n%s", s.c_str()); 173314564Sdim } 174292932Sdim 175314564Sdim Stmt *method_body = MethodDecl->getBody(); 176292932Sdim 177314564Sdim if (!method_body) 178314564Sdim return false; 179292932Sdim 180314564Sdim CompoundStmt *compound_stmt = dyn_cast<CompoundStmt>(method_body); 181292932Sdim 182314564Sdim bool ret = SynthesizeBodyResult(compound_stmt, MethodDecl); 183292932Sdim 184314564Sdim if (log && log->GetVerbose()) { 185314564Sdim std::string s; 186314564Sdim raw_string_ostream os(s); 187292932Sdim 188314564Sdim MethodDecl->print(os); 189292932Sdim 190314564Sdim os.flush(); 191292932Sdim 192314564Sdim log->Printf("Transformed method AST:\n%s", s.c_str()); 193314564Sdim } 194292932Sdim 195314564Sdim return ret; 196292932Sdim} 197292932Sdim 198314564Sdimbool ASTResultSynthesizer::SynthesizeBodyResult(CompoundStmt *Body, 199314564Sdim DeclContext *DC) { 200314564Sdim Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 201292932Sdim 202314564Sdim ASTContext &Ctx(*m_ast_context); 203292932Sdim 204314564Sdim if (!Body) 205314564Sdim return false; 206292932Sdim 207314564Sdim if (Body->body_empty()) 208314564Sdim return false; 209292932Sdim 210314564Sdim Stmt **last_stmt_ptr = Body->body_end() - 1; 211314564Sdim Stmt *last_stmt = *last_stmt_ptr; 212292932Sdim 213314564Sdim while (dyn_cast<NullStmt>(last_stmt)) { 214314564Sdim if (last_stmt_ptr != Body->body_begin()) { 215314564Sdim last_stmt_ptr--; 216314564Sdim last_stmt = *last_stmt_ptr; 217314564Sdim } else { 218314564Sdim return false; 219292932Sdim } 220314564Sdim } 221292932Sdim 222314564Sdim Expr *last_expr = dyn_cast<Expr>(last_stmt); 223292932Sdim 224314564Sdim if (!last_expr) 225314564Sdim // No auxiliary variable necessary; expression returns void 226314564Sdim return true; 227292932Sdim 228314564Sdim // In C++11, last_expr can be a LValueToRvalue implicit cast. Strip that off 229314564Sdim // if that's the 230314564Sdim // case. 231292932Sdim 232314564Sdim do { 233314564Sdim ImplicitCastExpr *implicit_cast = dyn_cast<ImplicitCastExpr>(last_expr); 234292932Sdim 235314564Sdim if (!implicit_cast) 236314564Sdim break; 237292932Sdim 238314564Sdim if (implicit_cast->getCastKind() != CK_LValueToRValue) 239314564Sdim break; 240292932Sdim 241314564Sdim last_expr = implicit_cast->getSubExpr(); 242314564Sdim } while (0); 243292932Sdim 244314564Sdim // is_lvalue is used to record whether the expression returns an assignable 245314564Sdim // Lvalue or an 246314564Sdim // Rvalue. This is relevant because they are handled differently. 247314564Sdim // 248314564Sdim // For Lvalues 249314564Sdim // 250314564Sdim // - In AST result synthesis (here!) the expression E is transformed into an 251314564Sdim // initialization 252314564Sdim // T *$__lldb_expr_result_ptr = &E. 253314564Sdim // 254314564Sdim // - In structure allocation, a pointer-sized slot is allocated in the 255314564Sdim // struct that is to be 256314564Sdim // passed into the expression. 257314564Sdim // 258314564Sdim // - In IR transformations, reads and writes to $__lldb_expr_result_ptr are 259314564Sdim // redirected at 260314564Sdim // an entry in the struct ($__lldb_arg) passed into the expression. 261314564Sdim // (Other persistent 262314564Sdim // variables are treated similarly, having been materialized as 263314564Sdim // references, but in those 264314564Sdim // cases the value of the reference itself is never modified.) 265314564Sdim // 266314564Sdim // - During materialization, $0 (the result persistent variable) is ignored. 267314564Sdim // 268314564Sdim // - During dematerialization, $0 is marked up as a load address with value 269314564Sdim // equal to the 270314564Sdim // contents of the structure entry. 271314564Sdim // 272314564Sdim // For Rvalues 273314564Sdim // 274314564Sdim // - In AST result synthesis the expression E is transformed into an 275314564Sdim // initialization 276314564Sdim // static T $__lldb_expr_result = E. 277314564Sdim // 278314564Sdim // - In structure allocation, a pointer-sized slot is allocated in the 279314564Sdim // struct that is to be 280314564Sdim // passed into the expression. 281314564Sdim // 282314564Sdim // - In IR transformations, an instruction is inserted at the beginning of 283314564Sdim // the function to 284314564Sdim // dereference the pointer resident in the slot. Reads and writes to 285314564Sdim // $__lldb_expr_result 286314564Sdim // are redirected at that dereferenced version. Guard variables for the 287314564Sdim // static variable 288314564Sdim // are excised. 289314564Sdim // 290314564Sdim // - During materialization, $0 (the result persistent variable) is 291314564Sdim // populated with the location 292314564Sdim // of a newly-allocated area of memory. 293314564Sdim // 294314564Sdim // - During dematerialization, $0 is ignored. 295292932Sdim 296314564Sdim bool is_lvalue = (last_expr->getValueKind() == VK_LValue || 297314564Sdim last_expr->getValueKind() == VK_XValue) && 298314564Sdim (last_expr->getObjectKind() == OK_Ordinary); 299292932Sdim 300314564Sdim QualType expr_qual_type = last_expr->getType(); 301314564Sdim const clang::Type *expr_type = expr_qual_type.getTypePtr(); 302292932Sdim 303314564Sdim if (!expr_type) 304314564Sdim return false; 305292932Sdim 306314564Sdim if (expr_type->isVoidType()) 307314564Sdim return true; 308292932Sdim 309314564Sdim if (log) { 310314564Sdim std::string s = expr_qual_type.getAsString(); 311292932Sdim 312314564Sdim log->Printf("Last statement is an %s with type: %s", 313314564Sdim (is_lvalue ? "lvalue" : "rvalue"), s.c_str()); 314314564Sdim } 315292932Sdim 316314564Sdim clang::VarDecl *result_decl = NULL; 317292932Sdim 318314564Sdim if (is_lvalue) { 319314564Sdim IdentifierInfo *result_ptr_id; 320292932Sdim 321314564Sdim if (expr_type->isFunctionType()) 322314564Sdim result_ptr_id = 323314564Sdim &Ctx.Idents.get("$__lldb_expr_result"); // functions actually should 324314564Sdim // be treated like function 325314564Sdim // pointers 326314564Sdim else 327314564Sdim result_ptr_id = &Ctx.Idents.get("$__lldb_expr_result_ptr"); 328292932Sdim 329314564Sdim m_sema->RequireCompleteType(SourceLocation(), expr_qual_type, 330314564Sdim clang::diag::err_incomplete_type); 331292932Sdim 332314564Sdim QualType ptr_qual_type; 333292932Sdim 334314564Sdim if (expr_qual_type->getAs<ObjCObjectType>() != NULL) 335314564Sdim ptr_qual_type = Ctx.getObjCObjectPointerType(expr_qual_type); 336314564Sdim else 337314564Sdim ptr_qual_type = Ctx.getPointerType(expr_qual_type); 338292932Sdim 339314564Sdim result_decl = 340314564Sdim VarDecl::Create(Ctx, DC, SourceLocation(), SourceLocation(), 341314564Sdim result_ptr_id, ptr_qual_type, NULL, SC_Static); 342292932Sdim 343314564Sdim if (!result_decl) 344314564Sdim return false; 345292932Sdim 346314564Sdim ExprResult address_of_expr = 347314564Sdim m_sema->CreateBuiltinUnaryOp(SourceLocation(), UO_AddrOf, last_expr); 348314564Sdim if (address_of_expr.get()) 349314564Sdim m_sema->AddInitializerToDecl(result_decl, address_of_expr.get(), true); 350292932Sdim else 351314564Sdim return false; 352314564Sdim } else { 353314564Sdim IdentifierInfo &result_id = Ctx.Idents.get("$__lldb_expr_result"); 354292932Sdim 355314564Sdim result_decl = VarDecl::Create(Ctx, DC, SourceLocation(), SourceLocation(), 356314564Sdim &result_id, expr_qual_type, NULL, SC_Static); 357292932Sdim 358314564Sdim if (!result_decl) 359314564Sdim return false; 360292932Sdim 361314564Sdim m_sema->AddInitializerToDecl(result_decl, last_expr, true); 362314564Sdim } 363292932Sdim 364314564Sdim DC->addDecl(result_decl); 365292932Sdim 366314564Sdim /////////////////////////////// 367314564Sdim // call AddInitializerToDecl 368314564Sdim // 369292932Sdim 370314564Sdim // m_sema->AddInitializerToDecl(result_decl, last_expr); 371292932Sdim 372314564Sdim ///////////////////////////////// 373314564Sdim // call ConvertDeclToDeclGroup 374314564Sdim // 375292932Sdim 376314564Sdim Sema::DeclGroupPtrTy result_decl_group_ptr; 377292932Sdim 378314564Sdim result_decl_group_ptr = m_sema->ConvertDeclToDeclGroup(result_decl); 379292932Sdim 380314564Sdim //////////////////////// 381314564Sdim // call ActOnDeclStmt 382314564Sdim // 383292932Sdim 384314564Sdim StmtResult result_initialization_stmt_result(m_sema->ActOnDeclStmt( 385314564Sdim result_decl_group_ptr, SourceLocation(), SourceLocation())); 386292932Sdim 387314564Sdim //////////////////////////////////////////////// 388314564Sdim // replace the old statement with the new one 389314564Sdim // 390292932Sdim 391314564Sdim *last_stmt_ptr = 392314564Sdim reinterpret_cast<Stmt *>(result_initialization_stmt_result.get()); 393292932Sdim 394314564Sdim return true; 395292932Sdim} 396292932Sdim 397314564Sdimvoid ASTResultSynthesizer::HandleTranslationUnit(ASTContext &Ctx) { 398314564Sdim if (m_passthrough) 399314564Sdim m_passthrough->HandleTranslationUnit(Ctx); 400292932Sdim} 401292932Sdim 402314564Sdimvoid ASTResultSynthesizer::RecordPersistentTypes(DeclContext *FunDeclCtx) { 403314564Sdim typedef DeclContext::specific_decl_iterator<TypeDecl> TypeDeclIterator; 404292932Sdim 405314564Sdim for (TypeDeclIterator i = TypeDeclIterator(FunDeclCtx->decls_begin()), 406314564Sdim e = TypeDeclIterator(FunDeclCtx->decls_end()); 407314564Sdim i != e; ++i) { 408314564Sdim MaybeRecordPersistentType(*i); 409314564Sdim } 410292932Sdim} 411292932Sdim 412314564Sdimvoid ASTResultSynthesizer::MaybeRecordPersistentType(TypeDecl *D) { 413314564Sdim if (!D->getIdentifier()) 414314564Sdim return; 415292932Sdim 416314564Sdim StringRef name = D->getName(); 417292932Sdim 418314564Sdim if (name.size() == 0 || name[0] != '$') 419314564Sdim return; 420292932Sdim 421314564Sdim Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 422292932Sdim 423314564Sdim ConstString name_cs(name.str().c_str()); 424292932Sdim 425314564Sdim if (log) 426314564Sdim log->Printf("Recording persistent type %s\n", name_cs.GetCString()); 427292932Sdim 428314564Sdim m_decls.push_back(D); 429309124Sdim} 430292932Sdim 431314564Sdimvoid ASTResultSynthesizer::RecordPersistentDecl(NamedDecl *D) { 432314564Sdim lldbassert(m_top_level); 433309124Sdim 434314564Sdim if (!D->getIdentifier()) 435314564Sdim return; 436309124Sdim 437314564Sdim StringRef name = D->getName(); 438309124Sdim 439314564Sdim if (name.size() == 0) 440314564Sdim return; 441309124Sdim 442314564Sdim Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 443309124Sdim 444314564Sdim ConstString name_cs(name.str().c_str()); 445309124Sdim 446314564Sdim if (log) 447314564Sdim log->Printf("Recording persistent decl %s\n", name_cs.GetCString()); 448309124Sdim 449314564Sdim m_decls.push_back(D); 450292932Sdim} 451292932Sdim 452314564Sdimvoid ASTResultSynthesizer::CommitPersistentDecls() { 453314564Sdim for (clang::NamedDecl *decl : m_decls) { 454314564Sdim StringRef name = decl->getName(); 455314564Sdim ConstString name_cs(name.str().c_str()); 456309124Sdim 457314564Sdim Decl *D_scratch = m_target.GetClangASTImporter()->DeportDecl( 458314564Sdim m_target.GetScratchClangASTContext()->getASTContext(), m_ast_context, 459314564Sdim decl); 460309124Sdim 461314564Sdim if (!D_scratch) { 462314564Sdim Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 463309124Sdim 464314564Sdim if (log) { 465314564Sdim std::string s; 466314564Sdim llvm::raw_string_ostream ss(s); 467314564Sdim decl->dump(ss); 468314564Sdim ss.flush(); 469309124Sdim 470314564Sdim log->Printf("Couldn't commit persistent decl: %s\n", s.c_str()); 471314564Sdim } 472309124Sdim 473314564Sdim continue; 474314564Sdim } 475309124Sdim 476314564Sdim if (NamedDecl *NamedDecl_scratch = dyn_cast<NamedDecl>(D_scratch)) 477314564Sdim llvm::cast<ClangPersistentVariables>( 478314564Sdim m_target.GetPersistentExpressionStateForLanguage( 479314564Sdim lldb::eLanguageTypeC)) 480314564Sdim ->RegisterPersistentDecl(name_cs, NamedDecl_scratch); 481314564Sdim } 482309124Sdim} 483309124Sdim 484314564Sdimvoid ASTResultSynthesizer::HandleTagDeclDefinition(TagDecl *D) { 485314564Sdim if (m_passthrough) 486314564Sdim m_passthrough->HandleTagDeclDefinition(D); 487292932Sdim} 488292932Sdim 489314564Sdimvoid ASTResultSynthesizer::CompleteTentativeDefinition(VarDecl *D) { 490314564Sdim if (m_passthrough) 491314564Sdim m_passthrough->CompleteTentativeDefinition(D); 492292932Sdim} 493292932Sdim 494314564Sdimvoid ASTResultSynthesizer::HandleVTable(CXXRecordDecl *RD) { 495314564Sdim if (m_passthrough) 496314564Sdim m_passthrough->HandleVTable(RD); 497292932Sdim} 498292932Sdim 499314564Sdimvoid ASTResultSynthesizer::PrintStats() { 500314564Sdim if (m_passthrough) 501314564Sdim m_passthrough->PrintStats(); 502292932Sdim} 503292932Sdim 504314564Sdimvoid ASTResultSynthesizer::InitializeSema(Sema &S) { 505314564Sdim m_sema = &S; 506292932Sdim 507314564Sdim if (m_passthrough_sema) 508314564Sdim m_passthrough_sema->InitializeSema(S); 509292932Sdim} 510292932Sdim 511314564Sdimvoid ASTResultSynthesizer::ForgetSema() { 512314564Sdim m_sema = NULL; 513292932Sdim 514314564Sdim if (m_passthrough_sema) 515314564Sdim m_passthrough_sema->ForgetSema(); 516292932Sdim} 517